xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/experimental/coshf_1u9.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Single-precision cosh(x) function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 2022-2024, Arm Limited.
5*f3087befSAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner  */
7*f3087befSAndrew Turner 
8*f3087befSAndrew Turner #include "mathlib.h"
9*f3087befSAndrew Turner #include "math_config.h"
10*f3087befSAndrew Turner #include "test_sig.h"
11*f3087befSAndrew Turner #include "test_defs.h"
12*f3087befSAndrew Turner 
13*f3087befSAndrew Turner #define AbsMask 0x7fffffff
14*f3087befSAndrew Turner #define TinyBound 0x20000000 /* 0x1p-63: Round to 1 below this.  */
15*f3087befSAndrew Turner /* 0x1.5a92d8p+6: expf overflows above this, so have to use special case.  */
16*f3087befSAndrew Turner #define SpecialBound 0x42ad496c
17*f3087befSAndrew Turner 
18*f3087befSAndrew Turner static NOINLINE float
specialcase(float x,uint32_t iax)19*f3087befSAndrew Turner specialcase (float x, uint32_t iax)
20*f3087befSAndrew Turner {
21*f3087befSAndrew Turner   if (iax == 0x7f800000)
22*f3087befSAndrew Turner     return INFINITY;
23*f3087befSAndrew Turner   if (iax > 0x7f800000)
24*f3087befSAndrew Turner     return __math_invalidf (x);
25*f3087befSAndrew Turner   if (iax <= TinyBound)
26*f3087befSAndrew Turner     /* For tiny x, avoid underflow by just returning 1.  */
27*f3087befSAndrew Turner     return 1;
28*f3087befSAndrew Turner   /* Otherwise SpecialBound <= |x| < Inf. x is too large to calculate exp(x)
29*f3087befSAndrew Turner      without overflow, so use exp(|x|/2) instead. For large x cosh(x) is
30*f3087befSAndrew Turner      dominated by exp(x), so return:
31*f3087befSAndrew Turner      cosh(x) ~= (exp(|x|/2))^2 / 2.  */
32*f3087befSAndrew Turner   float t = expf (asfloat (iax) / 2);
33*f3087befSAndrew Turner   return (0.5 * t) * t;
34*f3087befSAndrew Turner }
35*f3087befSAndrew Turner 
36*f3087befSAndrew Turner /* Approximation for single-precision cosh(x) using exp.
37*f3087befSAndrew Turner    cosh(x) = (exp(x) + exp(-x)) / 2.
38*f3087befSAndrew Turner    The maximum error is 1.89 ULP, observed for |x| > SpecialBound:
39*f3087befSAndrew Turner    coshf(0x1.65898cp+6) got 0x1.f00aep+127 want 0x1.f00adcp+127.
40*f3087befSAndrew Turner    The maximum error observed for TinyBound < |x| < SpecialBound is 1.02 ULP:
41*f3087befSAndrew Turner    coshf(0x1.50a3cp+0) got 0x1.ff21dcp+0 want 0x1.ff21dap+0.  */
42*f3087befSAndrew Turner float
coshf(float x)43*f3087befSAndrew Turner coshf (float x)
44*f3087befSAndrew Turner {
45*f3087befSAndrew Turner   uint32_t ix = asuint (x);
46*f3087befSAndrew Turner   uint32_t iax = ix & AbsMask;
47*f3087befSAndrew Turner   float ax = asfloat (iax);
48*f3087befSAndrew Turner 
49*f3087befSAndrew Turner   if (unlikely (iax <= TinyBound || iax >= SpecialBound))
50*f3087befSAndrew Turner     {
51*f3087befSAndrew Turner       /* x is tiny, large or special.  */
52*f3087befSAndrew Turner       return specialcase (x, iax);
53*f3087befSAndrew Turner     }
54*f3087befSAndrew Turner 
55*f3087befSAndrew Turner   /* Compute cosh using the definition:
56*f3087befSAndrew Turner      coshf(x) = exp(x) / 2 + exp(-x) / 2.  */
57*f3087befSAndrew Turner   float t = expf (ax);
58*f3087befSAndrew Turner   return 0.5f * t + 0.5f / t;
59*f3087befSAndrew Turner }
60*f3087befSAndrew Turner 
61*f3087befSAndrew Turner TEST_SIG (S, F, 1, cosh, -10.0, 10.0)
62*f3087befSAndrew Turner TEST_ULP (coshf, 1.89)
63*f3087befSAndrew Turner TEST_SYM_INTERVAL (coshf, 0, 0x1p-63, 100)
64*f3087befSAndrew Turner TEST_SYM_INTERVAL (coshf, 0, 0x1.5a92d8p+6, 80000)
65*f3087befSAndrew Turner TEST_SYM_INTERVAL (coshf, 0x1.5a92d8p+6, inf, 2000)
66