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