blob: 770b294c2379110a5719847badbb682935a754dd [file] [log] [blame]
Wilco Dijkstra269dc162018-05-16 15:39:22 +01001/*
2 * Single-precision sin 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 <math.h>
9#include "math_config.h"
10#include "sincosf.h"
11
Wilco Dijkstrab2fc9892018-08-08 15:03:29 +010012/* 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 Dijkstra269dc162018-05-16 15:39:22 +010014 small values. Large inputs have their range reduced using fast integer
Wilco Dijkstrab2fc9892018-08-08 15:03:29 +010015 arithmetic. */
Wilco Dijkstra269dc162018-05-16 15:39:22 +010016float
17sinf (float y)
18{
19 double x = y;
20 double s;
21 int n;
Wilco Dijkstra3262ef22018-07-04 17:45:15 +010022 const sincos_t *p = &__sincosf_table[0];
Wilco Dijkstra269dc162018-05-16 15:39:22 +010023
24 if (abstop12 (y) < abstop12 (pio4))
25 {
26 s = x * x;
27
28 if (unlikely (abstop12 (y) < abstop12 (0x1p-12f)))
Szabolcs Nagy5e838912018-06-29 09:56:54 +010029 {
30 if (unlikely (abstop12 (y) < abstop12 (0x1p-126f)))
31 /* Force underflow for tiny y. */
32 force_eval_float (s);
33 return y;
34 }
Wilco Dijkstra269dc162018-05-16 15:39:22 +010035
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 Dijkstra3262ef22018-07-04 17:45:15 +010046 p = &__sincosf_table[1];
Wilco Dijkstra269dc162018-05-16 15:39:22 +010047
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 Dijkstra3262ef22018-07-04 17:45:15 +010061 p = &__sincosf_table[1];
Wilco Dijkstra269dc162018-05-16 15:39:22 +010062
63 return sinf_poly (x * s, x * x, p, n);
64 }
65 else
66 return __math_invalidf (y);
67}