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