1 /* Single-precision sincos 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 <stdint.h>
33 #include <math.h>
34 #include "math_config.h"
35 #include "sincosf.h"
36 
37 /* Fast sincosf implementation.  Worst-case ULP is 0.5607, maximum relative
38    error is 0.5303 * 2^-23.  A single-step range reduction is used for
39    small values.  Large inputs have their range reduced using fast integer
40    arithmetic.  */
41 void
sincosf(float y,float * sinp,float * cosp)42 sincosf (float y, float *sinp, float *cosp)
43 {
44   double x = (double) y;
45   double s;
46   int n;
47   const sincos_t *p = &__sincosf_table[0];
48 
49   if (abstop12 (y) < abstop12 (pio4))
50     {
51       double x2 = x * x;
52 
53       if (unlikely (abstop12 (y) < abstop12 (0x1p-12f)))
54 	{
55 	  if (unlikely (abstop12 (y) < abstop12 (0x1p-126f)))
56 	    /* Force underflow for tiny y.  */
57 	    force_eval_float (x2);
58 	  *sinp = y;
59 	  *cosp = 1.0f;
60 	  return;
61 	}
62 
63       sincosf_poly (x, x2, p, 0, sinp, cosp);
64     }
65   else if (abstop12 (y) < abstop12 (120.0f))
66     {
67       x = reduce_fast (x, p, &n);
68 
69       /* Setup the signs for sin and cos.  */
70       s = p->sign[n & 3];
71 
72       if (n & 2)
73 	p = &__sincosf_table[1];
74 
75       sincosf_poly (x * s, x * x, p, n, sinp, cosp);
76     }
77   else if (likely (abstop12 (y) < abstop12 (INFINITY)))
78     {
79       uint32_t xi = asuint (y);
80       int sign = xi >> 31;
81 
82       x = reduce_large (xi, &n);
83 
84       /* Setup signs for sin and cos - include original sign.  */
85       s = p->sign[(n + sign) & 3];
86 
87       if ((n + sign) & 2)
88 	p = &__sincosf_table[1];
89 
90       sincosf_poly (x * s, x * x, p, n, sinp, cosp);
91     }
92   else
93     {
94       /* Return NaN if Inf or NaN for both sin and cos.  */
95       *sinp = *cosp = y - y;
96 #if WANT_ERRNO
97       /* Needed to set errno for +-Inf, the add is a hack to work
98 	 around a gcc register allocation issue: just passing y
99 	 affects code generation in the fast path.  */
100       __math_invalidf (y + y);
101 #endif
102     }
103 }
104 
105 #endif
106