1 /* Single-precision sin function.
2    Copyright (c) 2018 Arm Ltd.  All rights reserved.
3 
4    SPDX-License-Identifier: BSD-3-Clause
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9    1. Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11    2. Redistributions in binary form must reproduce the above copyright
12       notice, this list of conditions and the following disclaimer in the
13       documentation and/or other materials provided with the distribution.
14    3. The name of the company may not be used to endorse or promote
15       products derived from this software without specific prior written
16       permission.
17 
18    THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
19    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21    IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
28 
29 #include "fdlibm.h"
30 #if !__OBSOLETE_MATH_FLOAT
31 
32 #include <math.h>
33 #include "math_config.h"
34 #include "sincosf.h"
35 
36 /* Fast sinf implementation.  Worst-case ULP is 0.5607, maximum relative
37    error is 0.5303 * 2^-23.  A single-step range reduction is used for
38    small values.  Large inputs have their range reduced using fast integer
39    arithmetic.  */
40 float
sinf(float y)41 sinf (float y)
42 {
43   double x = (double) y;
44   double s;
45   int n;
46   const sincos_t *p = &__sincosf_table[0];
47 
48   if (abstop12 (y) < abstop12 (pio4))
49     {
50       s = x * x;
51 
52       if (unlikely (abstop12 (y) < abstop12 (0x1p-12f)))
53 	{
54 	  if (unlikely (abstop12 (y) < abstop12 (0x1p-126f)))
55 	    /* Force underflow for tiny y.  */
56 	    force_eval_float (s);
57 	  return y;
58 	}
59 
60       return sinf_poly (x, s, p, 0);
61     }
62   else if (likely (abstop12 (y) < abstop12 (120.0f)))
63     {
64       x = reduce_fast (x, p, &n);
65 
66       /* Setup the signs for sin and cos.  */
67       s = p->sign[n & 3];
68 
69       if (n & 2)
70 	p = &__sincosf_table[1];
71 
72       return sinf_poly (x * s, x * x, p, n);
73     }
74   else if (abstop12 (y) < abstop12 (INFINITY))
75     {
76       uint32_t xi = asuint (y);
77       int sign = xi >> 31;
78 
79       x = reduce_large (xi, &n);
80 
81       /* Setup signs for sin and cos - include original sign.  */
82       s = p->sign[(n + sign) & 3];
83 
84       if ((n + sign) & 2)
85 	p = &__sincosf_table[1];
86 
87       return sinf_poly (x * s, x * x, p, n);
88     }
89   else
90     return __math_invalidf (y);
91 }
92 
93 #endif
94