blob: 831b39e85e766b2851135d63bbd5a858084b7b87 [file] [log] [blame]
Wilco Dijkstra269dc162018-05-16 15:39:22 +01001/*
2 * Single-precision cos function.
3 *
4 * Copyright (c) 2018, Arm Limited.
Szabolcs Nagy11253b02018-11-12 11:10:57 +00005 * SPDX-License-Identifier: MIT
Wilco Dijkstra269dc162018-05-16 15:39:22 +01006 */
7
Wilco Dijkstra269dc162018-05-16 15:39:22 +01008#include <stdint.h>
9#include <math.h>
10#include "math_config.h"
11#include "sincosf.h"
12
Wilco Dijkstrab2fc9892018-08-08 15:03:29 +010013/* Fast cosf implementation. Worst-case ULP is 0.5607, maximum relative
14 error is 0.5303 * 2^-23. A single-step range reduction is used for
Wilco Dijkstra269dc162018-05-16 15:39:22 +010015 small values. Large inputs have their range reduced using fast integer
Wilco Dijkstrab2fc9892018-08-08 15:03:29 +010016 arithmetic. */
Wilco Dijkstra269dc162018-05-16 15:39:22 +010017float
18cosf (float y)
19{
20 double x = y;
21 double s;
22 int n;
Wilco Dijkstra3262ef22018-07-04 17:45:15 +010023 const sincos_t *p = &__sincosf_table[0];
Wilco Dijkstra269dc162018-05-16 15:39:22 +010024
25 if (abstop12 (y) < abstop12 (pio4))
26 {
27 double x2 = x * x;
28
29 if (unlikely (abstop12 (y) < abstop12 (0x1p-12f)))
30 return 1.0f;
31
32 return sinf_poly (x, x2, p, 1);
33 }
34 else if (likely (abstop12 (y) < abstop12 (120.0f)))
35 {
36 x = reduce_fast (x, p, &n);
37
38 /* Setup the signs for sin and cos. */
39 s = p->sign[n & 3];
40
41 if (n & 2)
Wilco Dijkstra3262ef22018-07-04 17:45:15 +010042 p = &__sincosf_table[1];
Wilco Dijkstra269dc162018-05-16 15:39:22 +010043
44 return sinf_poly (x * s, x * x, p, n ^ 1);
45 }
46 else if (abstop12 (y) < abstop12 (INFINITY))
47 {
48 uint32_t xi = asuint (y);
49 int sign = xi >> 31;
50
51 x = reduce_large (xi, &n);
52
53 /* Setup signs for sin and cos - include original sign. */
54 s = p->sign[(n + sign) & 3];
55
56 if ((n + sign) & 2)
Wilco Dijkstra3262ef22018-07-04 17:45:15 +010057 p = &__sincosf_table[1];
Wilco Dijkstra269dc162018-05-16 15:39:22 +010058
59 return sinf_poly (x * s, x * x, p, n ^ 1);
60 }
61 else
62 return __math_invalidf (y);
63}