1 /*
2  * Single-precision cos function.
3  *
4  * Copyright (c) 2018, Arm Limited.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #if WANT_SINGLEPREC
9 #include "single/s_cosf.c"
10 #else
11 
12 #include <stdint.h>
13 #include <math.h>
14 #include "math_config.h"
15 #include "sincosf.h"
16 
17 /* Fast cosf implementation.  Worst-case ULP is 0.5607, maximum relative
18    error is 0.5303 * 2^-23.  A single-step range reduction is used for
19    small values.  Large inputs have their range reduced using fast integer
20    arithmetic.  */
21 float
cosf(float y)22 cosf (float y)
23 {
24   double x = y;
25   double s;
26   int n;
27   const sincos_t *p = &__sincosf_table[0];
28 
29   if (abstop12 (y) < abstop12 (pio4))
30     {
31       double x2 = x * x;
32 
33       if (unlikely (abstop12 (y) < abstop12 (0x1p-12f)))
34 	return 1.0f;
35 
36       return sinf_poly (x, x2, p, 1);
37     }
38   else if (likely (abstop12 (y) < abstop12 (120.0f)))
39     {
40       x = reduce_fast (x, p, &n);
41 
42       /* Setup the signs for sin and cos.  */
43       s = p->sign[n & 3];
44 
45       if (n & 2)
46 	p = &__sincosf_table[1];
47 
48       return sinf_poly (x * s, x * x, p, n ^ 1);
49     }
50   else if (abstop12 (y) < abstop12 (INFINITY))
51     {
52       uint32_t xi = asuint (y);
53       int sign = xi >> 31;
54 
55       x = reduce_large (xi, &n);
56 
57       /* Setup signs for sin and cos - include original sign.  */
58       s = p->sign[(n + sign) & 3];
59 
60       if ((n + sign) & 2)
61 	p = &__sincosf_table[1];
62 
63       return sinf_poly (x * s, x * x, p, n ^ 1);
64     }
65   else
66     return __math_invalidf (y);
67 }
68 
69 #endif
70