xref: /freebsd/contrib/arm-optimized-routines/math/sinf.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*
2  * Single-precision sin function.
3  *
4  * Copyright (c) 2018-2024, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include <math.h>
9 #include "math_config.h"
10 #include "sincosf.h"
11 #include "test_defs.h"
12 #include "test_sig.h"
13 
14 /* Fast sinf implementation.  Worst-case ULP is 0.5607, maximum relative
15    error is 0.5303 * 2^-23.  A single-step range reduction is used for
16    small values.  Large inputs have their range reduced using fast integer
17    arithmetic.  */
18 float
19 sinf (float y)
20 {
21   double x = y;
22   double s;
23   int n;
24   const sincos_t *p = &__sincosf_table[0];
25 
26   if (abstop12 (y) < abstop12 (pio4f))
27     {
28       s = x * x;
29 
30       if (unlikely (abstop12 (y) < abstop12 (0x1p-12f)))
31 	{
32 	  if (unlikely (abstop12 (y) < abstop12 (0x1p-126f)))
33 	    /* Force underflow for tiny y.  */
34 	    force_eval_float (s);
35 	  return y;
36 	}
37 
38       return sinf_poly (x, s, p, 0);
39     }
40   else if (likely (abstop12 (y) < abstop12 (120.0f)))
41     {
42       x = reduce_fast (x, p, &n);
43 
44       /* Setup the signs for sin and cos.  */
45       s = p->sign[n & 3];
46 
47       if (n & 2)
48 	p = &__sincosf_table[1];
49 
50       return sinf_poly (x * s, x * x, p, n);
51     }
52   else if (abstop12 (y) < abstop12 (INFINITY))
53     {
54       uint32_t xi = asuint (y);
55       int sign = xi >> 31;
56 
57       x = reduce_large (xi, &n);
58 
59       /* Setup signs for sin and cos - include original sign.  */
60       s = p->sign[(n + sign) & 3];
61 
62       if ((n + sign) & 2)
63 	p = &__sincosf_table[1];
64 
65       return sinf_poly (x * s, x * x, p, n);
66     }
67   else
68     return __math_invalidf (y);
69 }
70 
71 TEST_SIG (S, F, 1, sin, -3.1, 3.1)
72 TEST_ULP (sinf, 0.06)
73 TEST_ULP_NONNEAREST (sinf, 0.5)
74 TEST_INTERVAL (sinf, 0, 0xffff0000, 10000)
75 TEST_SYM_INTERVAL (sinf, 0x1p-14, 0x1p54, 50000)
76