Wilco Dijkstra | 269dc16 | 2018-05-16 15:39:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Single-precision sin function. |
| 3 | * |
| 4 | * Copyright (c) 2018, Arm Limited. |
Szabolcs Nagy | 11253b0 | 2018-11-12 11:10:57 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: MIT |
Wilco Dijkstra | 269dc16 | 2018-05-16 15:39:22 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Wilco Dijkstra | 269dc16 | 2018-05-16 15:39:22 +0100 | [diff] [blame] | 8 | #include <math.h> |
| 9 | #include "math_config.h" |
| 10 | #include "sincosf.h" |
| 11 | |
Wilco Dijkstra | b2fc989 | 2018-08-08 15:03:29 +0100 | [diff] [blame] | 12 | /* Fast sinf implementation. Worst-case ULP is 0.5607, maximum relative |
| 13 | error is 0.5303 * 2^-23. A single-step range reduction is used for |
Wilco Dijkstra | 269dc16 | 2018-05-16 15:39:22 +0100 | [diff] [blame] | 14 | small values. Large inputs have their range reduced using fast integer |
Wilco Dijkstra | b2fc989 | 2018-08-08 15:03:29 +0100 | [diff] [blame] | 15 | arithmetic. */ |
Wilco Dijkstra | 269dc16 | 2018-05-16 15:39:22 +0100 | [diff] [blame] | 16 | float |
| 17 | sinf (float y) |
| 18 | { |
| 19 | double x = y; |
| 20 | double s; |
| 21 | int n; |
Wilco Dijkstra | 3262ef2 | 2018-07-04 17:45:15 +0100 | [diff] [blame] | 22 | const sincos_t *p = &__sincosf_table[0]; |
Wilco Dijkstra | 269dc16 | 2018-05-16 15:39:22 +0100 | [diff] [blame] | 23 | |
| 24 | if (abstop12 (y) < abstop12 (pio4)) |
| 25 | { |
| 26 | s = x * x; |
| 27 | |
| 28 | if (unlikely (abstop12 (y) < abstop12 (0x1p-12f))) |
Szabolcs Nagy | 5e83891 | 2018-06-29 09:56:54 +0100 | [diff] [blame] | 29 | { |
| 30 | if (unlikely (abstop12 (y) < abstop12 (0x1p-126f))) |
| 31 | /* Force underflow for tiny y. */ |
| 32 | force_eval_float (s); |
| 33 | return y; |
| 34 | } |
Wilco Dijkstra | 269dc16 | 2018-05-16 15:39:22 +0100 | [diff] [blame] | 35 | |
| 36 | return sinf_poly (x, s, p, 0); |
| 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) |
Wilco Dijkstra | 3262ef2 | 2018-07-04 17:45:15 +0100 | [diff] [blame] | 46 | p = &__sincosf_table[1]; |
Wilco Dijkstra | 269dc16 | 2018-05-16 15:39:22 +0100 | [diff] [blame] | 47 | |
| 48 | return sinf_poly (x * s, x * x, p, n); |
| 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) |
Wilco Dijkstra | 3262ef2 | 2018-07-04 17:45:15 +0100 | [diff] [blame] | 61 | p = &__sincosf_table[1]; |
Wilco Dijkstra | 269dc16 | 2018-05-16 15:39:22 +0100 | [diff] [blame] | 62 | |
| 63 | return sinf_poly (x * s, x * x, p, n); |
| 64 | } |
| 65 | else |
| 66 | return __math_invalidf (y); |
| 67 | } |