1 /* Single-precision cos 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 cosf 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 float
cosf(float y)42 cosf (float y)
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 return 1.0f;
55
56 return sinf_poly (x, x2, p, 1);
57 }
58 else if (likely (abstop12 (y) < abstop12 (120.0f)))
59 {
60 x = reduce_fast (x, p, &n);
61
62 /* Setup the signs for sin and cos. */
63 s = p->sign[n & 3];
64
65 if (n & 2)
66 p = &__sincosf_table[1];
67
68 return sinf_poly (x * s, x * x, p, n ^ 1);
69 }
70 else if (abstop12 (y) < abstop12 (INFINITY))
71 {
72 uint32_t xi = asuint (y);
73 int sign = xi >> 31;
74
75 x = reduce_large (xi, &n);
76
77 /* Setup signs for sin and cos - include original sign. */
78 s = p->sign[(n + sign) & 3];
79
80 if ((n + sign) & 2)
81 p = &__sincosf_table[1];
82
83 return sinf_poly (x * s, x * x, p, n ^ 1);
84 }
85 else
86 return __math_invalidf (y);
87 }
88
89 #endif
90