xref: /freebsd/contrib/arm-optimized-routines/math/cosf.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
131914882SAlex Richardson /*
231914882SAlex Richardson  * Single-precision cos function.
331914882SAlex Richardson  *
4*f3087befSAndrew Turner  * Copyright (c) 2018-2024, Arm Limited.
5072a4ba8SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson  */
731914882SAlex Richardson 
831914882SAlex Richardson #include <stdint.h>
931914882SAlex Richardson #include <math.h>
1031914882SAlex Richardson #include "math_config.h"
1131914882SAlex Richardson #include "sincosf.h"
12*f3087befSAndrew Turner #include "test_defs.h"
13*f3087befSAndrew Turner #include "test_sig.h"
1431914882SAlex Richardson 
1531914882SAlex Richardson /* Fast cosf implementation.  Worst-case ULP is 0.5607, maximum relative
1631914882SAlex Richardson    error is 0.5303 * 2^-23.  A single-step range reduction is used for
1731914882SAlex Richardson    small values.  Large inputs have their range reduced using fast integer
1831914882SAlex Richardson    arithmetic.  */
1931914882SAlex Richardson float
cosf(float y)2031914882SAlex Richardson cosf (float y)
2131914882SAlex Richardson {
2231914882SAlex Richardson   double x = y;
2331914882SAlex Richardson   double s;
2431914882SAlex Richardson   int n;
2531914882SAlex Richardson   const sincos_t *p = &__sincosf_table[0];
2631914882SAlex Richardson 
27d49ad206SAndrew Turner   if (abstop12 (y) < abstop12 (pio4f))
2831914882SAlex Richardson     {
2931914882SAlex Richardson       double x2 = x * x;
3031914882SAlex Richardson 
3131914882SAlex Richardson       if (unlikely (abstop12 (y) < abstop12 (0x1p-12f)))
3231914882SAlex Richardson 	return 1.0f;
3331914882SAlex Richardson 
3431914882SAlex Richardson       return sinf_poly (x, x2, p, 1);
3531914882SAlex Richardson     }
3631914882SAlex Richardson   else if (likely (abstop12 (y) < abstop12 (120.0f)))
3731914882SAlex Richardson     {
3831914882SAlex Richardson       x = reduce_fast (x, p, &n);
3931914882SAlex Richardson 
4031914882SAlex Richardson       /* Setup the signs for sin and cos.  */
4131914882SAlex Richardson       s = p->sign[n & 3];
4231914882SAlex Richardson 
4331914882SAlex Richardson       if (n & 2)
4431914882SAlex Richardson 	p = &__sincosf_table[1];
4531914882SAlex Richardson 
4631914882SAlex Richardson       return sinf_poly (x * s, x * x, p, n ^ 1);
4731914882SAlex Richardson     }
4831914882SAlex Richardson   else if (abstop12 (y) < abstop12 (INFINITY))
4931914882SAlex Richardson     {
5031914882SAlex Richardson       uint32_t xi = asuint (y);
5131914882SAlex Richardson       int sign = xi >> 31;
5231914882SAlex Richardson 
5331914882SAlex Richardson       x = reduce_large (xi, &n);
5431914882SAlex Richardson 
5531914882SAlex Richardson       /* Setup signs for sin and cos - include original sign.  */
5631914882SAlex Richardson       s = p->sign[(n + sign) & 3];
5731914882SAlex Richardson 
5831914882SAlex Richardson       if ((n + sign) & 2)
5931914882SAlex Richardson 	p = &__sincosf_table[1];
6031914882SAlex Richardson 
6131914882SAlex Richardson       return sinf_poly (x * s, x * x, p, n ^ 1);
6231914882SAlex Richardson     }
6331914882SAlex Richardson   else
6431914882SAlex Richardson     return __math_invalidf (y);
6531914882SAlex Richardson }
66*f3087befSAndrew Turner 
67*f3087befSAndrew Turner TEST_SIG (S, F, 1, cos, -3.1, 3.1)
68*f3087befSAndrew Turner TEST_ULP (cosf, 0.06)
69*f3087befSAndrew Turner TEST_ULP_NONNEAREST (cosf, 0.5)
70*f3087befSAndrew Turner TEST_INTERVAL (cosf, 0, 0xffff0000, 10000)
71*f3087befSAndrew Turner TEST_SYM_INTERVAL (cosf, 0x1p-14, 0x1p54, 50000)
72