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