1 /*
2 * Single-precision asinh(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 "poly_scalar_f32.h"
9 #include "math_config.h"
10 #include "test_sig.h"
11 #include "test_defs.h"
12
13 #define AbsMask (0x7fffffff)
14 #define SqrtFltMax (0x1.749e96p+10f)
15 #define Ln2 (0x1.62e4p-1f)
16 #define One (0x3f8)
17 #define ExpM12 (0x398)
18
19 /* asinhf approximation using a variety of approaches on different intervals:
20
21 |x| < 2^-12: Return x. Function is exactly rounded in this region.
22
23 |x| < 1.0: Use custom order-8 polynomial. The largest observed
24 error in this region is 1.3ulps:
25 asinhf(0x1.f0f74cp-1) got 0x1.b88de4p-1 want 0x1.b88de2p-1.
26
27 |x| <= SqrtFltMax: Calculate the result directly using the
28 definition of asinh(x) = ln(x + sqrt(x*x + 1)). The largest
29 observed error in this region is 1.99ulps.
30 asinhf(0x1.00e358p+0) got 0x1.c4849ep-1 want 0x1.c484a2p-1.
31
32 |x| > SqrtFltMax: We cannot square x without overflow at a low
33 cost. At very large x, asinh(x) ~= ln(2x). At huge x we cannot
34 even double x without overflow, so calculate this as ln(x) +
35 ln(2). This largest observed error in this region is 3.39ulps.
36 asinhf(0x1.749e9ep+10) got 0x1.fffff8p+2 want 0x1.fffffep+2. */
37 float
asinhf(float x)38 asinhf (float x)
39 {
40 uint32_t ix = asuint (x);
41 uint32_t ia = ix & AbsMask;
42 uint32_t ia12 = ia >> 20;
43 float ax = asfloat (ia);
44 uint32_t sign = ix & ~AbsMask;
45
46 if (unlikely (ia12 < ExpM12 || ia == 0x7f800000))
47 return x;
48
49 if (unlikely (ia12 >= 0x7f8))
50 return __math_invalidf (x);
51
52 if (ia12 < One)
53 {
54 float x2 = ax * ax;
55 float p = estrin_7_f32 (ax, x2, x2 * x2, __asinhf_data.coeffs);
56 float y = fmaf (x2, p, ax);
57 return asfloat (asuint (y) | sign);
58 }
59
60 if (unlikely (ax > SqrtFltMax))
61 {
62 return asfloat (asuint (logf (ax) + Ln2) | sign);
63 }
64
65 return asfloat (asuint (logf (ax + sqrtf (ax * ax + 1))) | sign);
66 }
67
68 TEST_SIG (S, F, 1, asinh, -10.0, 10.0)
69 TEST_ULP (asinhf, 2.9)
70 TEST_INTERVAL (asinhf, 0, 0x1p-12, 5000)
71 TEST_INTERVAL (asinhf, 0x1p-12, 1.0, 50000)
72 TEST_INTERVAL (asinhf, 1.0, 0x1p11, 50000)
73 TEST_INTERVAL (asinhf, 0x1p11, 0x1p127, 20000)
74