1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Single-precision sinh(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 Half 0x3f000000
15*f3087befSAndrew Turner /* 0x1.62e43p+6, 2^7*ln2, minimum value for which expm1f overflows. */
16*f3087befSAndrew Turner #define Expm1OFlowLimit 0x42b17218
17*f3087befSAndrew Turner /* 0x1.65a9fap+6, minimum positive value for which sinhf should overflow. */
18*f3087befSAndrew Turner #define OFlowLimit 0x42b2d4fd
19*f3087befSAndrew Turner
20*f3087befSAndrew Turner /* Approximation for single-precision sinh(x) using expm1.
21*f3087befSAndrew Turner sinh(x) = (exp(x) - exp(-x)) / 2.
22*f3087befSAndrew Turner The maximum error is 2.26 ULP:
23*f3087befSAndrew Turner sinhf(0x1.e34a9ep-4) got 0x1.e469ep-4 want 0x1.e469e4p-4. */
24*f3087befSAndrew Turner float
sinhf(float x)25*f3087befSAndrew Turner sinhf (float x)
26*f3087befSAndrew Turner {
27*f3087befSAndrew Turner uint32_t ix = asuint (x);
28*f3087befSAndrew Turner uint32_t iax = ix & AbsMask;
29*f3087befSAndrew Turner float ax = asfloat (iax);
30*f3087befSAndrew Turner uint32_t sign = ix & ~AbsMask;
31*f3087befSAndrew Turner float halfsign = asfloat (Half | sign);
32*f3087befSAndrew Turner
33*f3087befSAndrew Turner if (unlikely (iax >= Expm1OFlowLimit))
34*f3087befSAndrew Turner {
35*f3087befSAndrew Turner /* Special values and overflow. */
36*f3087befSAndrew Turner if (iax >= 0x7fc00001 || iax == 0x7f800000)
37*f3087befSAndrew Turner return x;
38*f3087befSAndrew Turner if (iax >= 0x7f800000)
39*f3087befSAndrew Turner return __math_invalidf (x);
40*f3087befSAndrew Turner if (iax >= OFlowLimit)
41*f3087befSAndrew Turner return __math_oflowf (sign);
42*f3087befSAndrew Turner
43*f3087befSAndrew Turner /* expm1f overflows a little before sinhf, (~88.7 vs ~89.4). We have to
44*f3087befSAndrew Turner fill this gap by using a different algorithm, in this case we use a
45*f3087befSAndrew Turner double-precision exp helper. For large x sinh(x) dominated by exp(x),
46*f3087befSAndrew Turner however we cannot compute exp without overflow either. We use the
47*f3087befSAndrew Turner identity:
48*f3087befSAndrew Turner exp(a) = (exp(a / 2)) ^ 2.
49*f3087befSAndrew Turner to compute sinh(x) ~= (exp(|x| / 2)) ^ 2 / 2 for x > 0
50*f3087befSAndrew Turner ~= (exp(|x| / 2)) ^ 2 / -2 for x < 0.
51*f3087befSAndrew Turner Greatest error in this region is 1.89 ULP:
52*f3087befSAndrew Turner sinhf(0x1.65898cp+6) got 0x1.f00aep+127 want 0x1.f00adcp+127. */
53*f3087befSAndrew Turner float e = expf (ax / 2);
54*f3087befSAndrew Turner return (e * halfsign) * e;
55*f3087befSAndrew Turner }
56*f3087befSAndrew Turner
57*f3087befSAndrew Turner /* Use expm1f to retain acceptable precision for small numbers.
58*f3087befSAndrew Turner Let t = e^(|x|) - 1. */
59*f3087befSAndrew Turner float t = expm1f (ax);
60*f3087befSAndrew Turner /* Then sinh(x) = (t + t / (t + 1)) / 2 for x > 0
61*f3087befSAndrew Turner (t + t / (t + 1)) / -2 for x < 0. */
62*f3087befSAndrew Turner return (t + t / (t + 1)) * halfsign;
63*f3087befSAndrew Turner }
64*f3087befSAndrew Turner
65*f3087befSAndrew Turner TEST_SIG (S, F, 1, sinh, -10.0, 10.0)
66*f3087befSAndrew Turner TEST_ULP (sinhf, 1.76)
67*f3087befSAndrew Turner TEST_SYM_INTERVAL (sinhf, 0, 0x1.62e43p+6, 100000)
68*f3087befSAndrew Turner TEST_SYM_INTERVAL (sinhf, 0x1.62e43p+6, 0x1.65a9fap+6, 100)
69*f3087befSAndrew Turner TEST_SYM_INTERVAL (sinhf, 0x1.65a9fap+6, inf, 100)
70