xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/asinh.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision vector asinh(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 "test_defs.h"
9*f3087befSAndrew Turner #include "test_sig.h"
10*f3087befSAndrew Turner #include "v_math.h"
11*f3087befSAndrew Turner 
12*f3087befSAndrew Turner const static struct data
13*f3087befSAndrew Turner {
14*f3087befSAndrew Turner   uint64x2_t huge_bound, abs_mask, off, mask;
15*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
16*f3087befSAndrew Turner   float64x2_t tiny_bound;
17*f3087befSAndrew Turner #endif
18*f3087befSAndrew Turner   float64x2_t lc0, lc2;
19*f3087befSAndrew Turner   double lc1, lc3, ln2, lc4;
20*f3087befSAndrew Turner 
21*f3087befSAndrew Turner   float64x2_t c0, c2, c4, c6, c8, c10, c12, c14, c16, c17;
22*f3087befSAndrew Turner   double c1, c3, c5, c7, c9, c11, c13, c15;
23*f3087befSAndrew Turner 
24*f3087befSAndrew Turner } data = {
25*f3087befSAndrew Turner 
26*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
27*f3087befSAndrew Turner   .tiny_bound = V2 (0x1p-26),
28*f3087befSAndrew Turner #endif
29*f3087befSAndrew Turner   /* Even terms of polynomial s.t. asinh(x) is approximated by
30*f3087befSAndrew Turner      asinh(x) ~= x + x^3 * (C0 + C1 * x + C2 * x^2 + C3 * x^3 + ...).
31*f3087befSAndrew Turner      Generated using Remez, f = (asinh(sqrt(x)) - sqrt(x))/x^(3/2).  */
32*f3087befSAndrew Turner 
33*f3087befSAndrew Turner   .c0 = V2 (-0x1.55555555554a7p-3),
34*f3087befSAndrew Turner   .c1 = 0x1.3333333326c7p-4,
35*f3087befSAndrew Turner   .c2 = V2 (-0x1.6db6db68332e6p-5),
36*f3087befSAndrew Turner   .c3 = 0x1.f1c71b26fb40dp-6,
37*f3087befSAndrew Turner   .c4 = V2 (-0x1.6e8b8b654a621p-6),
38*f3087befSAndrew Turner   .c5 = 0x1.1c4daa9e67871p-6,
39*f3087befSAndrew Turner   .c6 = V2 (-0x1.c9871d10885afp-7),
40*f3087befSAndrew Turner   .c7 = 0x1.7a16e8d9d2ecfp-7,
41*f3087befSAndrew Turner   .c8 = V2 (-0x1.3ddca533e9f54p-7),
42*f3087befSAndrew Turner   .c9 = 0x1.0becef748dafcp-7,
43*f3087befSAndrew Turner   .c10 = V2 (-0x1.b90c7099dd397p-8),
44*f3087befSAndrew Turner   .c11 = 0x1.541f2bb1ffe51p-8,
45*f3087befSAndrew Turner   .c12 = V2 (-0x1.d217026a669ecp-9),
46*f3087befSAndrew Turner   .c13 = 0x1.0b5c7977aaf7p-9,
47*f3087befSAndrew Turner   .c14 = V2 (-0x1.e0f37daef9127p-11),
48*f3087befSAndrew Turner   .c15 = 0x1.388b5fe542a6p-12,
49*f3087befSAndrew Turner   .c16 = V2 (-0x1.021a48685e287p-14),
50*f3087befSAndrew Turner   .c17 = V2 (0x1.93d4ba83d34dap-18),
51*f3087befSAndrew Turner 
52*f3087befSAndrew Turner   .lc0 = V2 (-0x1.ffffffffffff7p-2),
53*f3087befSAndrew Turner   .lc1 = 0x1.55555555170d4p-2,
54*f3087befSAndrew Turner   .lc2 = V2 (-0x1.0000000399c27p-2),
55*f3087befSAndrew Turner   .lc3 = 0x1.999b2e90e94cap-3,
56*f3087befSAndrew Turner   .lc4 = -0x1.554e550bd501ep-3,
57*f3087befSAndrew Turner   .ln2 = 0x1.62e42fefa39efp-1,
58*f3087befSAndrew Turner 
59*f3087befSAndrew Turner   .off = V2 (0x3fe6900900000000),
60*f3087befSAndrew Turner   .huge_bound = V2 (0x5fe0000000000000),
61*f3087befSAndrew Turner   .abs_mask = V2 (0x7fffffffffffffff),
62*f3087befSAndrew Turner   .mask = V2 (0xfffULL << 52),
63*f3087befSAndrew Turner };
64*f3087befSAndrew Turner 
65*f3087befSAndrew Turner static float64x2_t NOINLINE VPCS_ATTR
special_case(float64x2_t x,float64x2_t y,uint64x2_t abs_mask,uint64x2_t special)66*f3087befSAndrew Turner special_case (float64x2_t x, float64x2_t y, uint64x2_t abs_mask,
67*f3087befSAndrew Turner 	      uint64x2_t special)
68*f3087befSAndrew Turner {
69*f3087befSAndrew Turner   /* Copy sign.  */
70*f3087befSAndrew Turner   y = vbslq_f64 (abs_mask, y, x);
71*f3087befSAndrew Turner   return v_call_f64 (asinh, x, y, special);
72*f3087befSAndrew Turner }
73*f3087befSAndrew Turner 
74*f3087befSAndrew Turner #define N (1 << V_LOG_TABLE_BITS)
75*f3087befSAndrew Turner #define IndexMask (N - 1)
76*f3087befSAndrew Turner 
77*f3087befSAndrew Turner struct entry
78*f3087befSAndrew Turner {
79*f3087befSAndrew Turner   float64x2_t invc;
80*f3087befSAndrew Turner   float64x2_t logc;
81*f3087befSAndrew Turner };
82*f3087befSAndrew Turner 
83*f3087befSAndrew Turner static inline struct entry
lookup(uint64x2_t i)84*f3087befSAndrew Turner lookup (uint64x2_t i)
85*f3087befSAndrew Turner {
86*f3087befSAndrew Turner   /* Since N is a power of 2, n % N = n & (N - 1).  */
87*f3087befSAndrew Turner   struct entry e;
88*f3087befSAndrew Turner   uint64_t i0 = (vgetq_lane_u64 (i, 0) >> (52 - V_LOG_TABLE_BITS)) & IndexMask;
89*f3087befSAndrew Turner   uint64_t i1 = (vgetq_lane_u64 (i, 1) >> (52 - V_LOG_TABLE_BITS)) & IndexMask;
90*f3087befSAndrew Turner   float64x2_t e0 = vld1q_f64 (&__v_log_data.table[i0].invc);
91*f3087befSAndrew Turner   float64x2_t e1 = vld1q_f64 (&__v_log_data.table[i1].invc);
92*f3087befSAndrew Turner   e.invc = vuzp1q_f64 (e0, e1);
93*f3087befSAndrew Turner   e.logc = vuzp2q_f64 (e0, e1);
94*f3087befSAndrew Turner   return e;
95*f3087befSAndrew Turner }
96*f3087befSAndrew Turner 
97*f3087befSAndrew Turner static inline float64x2_t
log_inline(float64x2_t xm,const struct data * d)98*f3087befSAndrew Turner log_inline (float64x2_t xm, const struct data *d)
99*f3087befSAndrew Turner {
100*f3087befSAndrew Turner 
101*f3087befSAndrew Turner   uint64x2_t u = vreinterpretq_u64_f64 (xm);
102*f3087befSAndrew Turner   uint64x2_t u_off = vsubq_u64 (u, d->off);
103*f3087befSAndrew Turner 
104*f3087befSAndrew Turner   int64x2_t k = vshrq_n_s64 (vreinterpretq_s64_u64 (u_off), 52);
105*f3087befSAndrew Turner   uint64x2_t iz = vsubq_u64 (u, vandq_u64 (u_off, d->mask));
106*f3087befSAndrew Turner   float64x2_t z = vreinterpretq_f64_u64 (iz);
107*f3087befSAndrew Turner 
108*f3087befSAndrew Turner   struct entry e = lookup (u_off);
109*f3087befSAndrew Turner 
110*f3087befSAndrew Turner   /* log(x) = log1p(z/c-1) + log(c) + k*Ln2.  */
111*f3087befSAndrew Turner   float64x2_t r = vfmaq_f64 (v_f64 (-1.0), z, e.invc);
112*f3087befSAndrew Turner   float64x2_t kd = vcvtq_f64_s64 (k);
113*f3087befSAndrew Turner 
114*f3087befSAndrew Turner   /* hi = r + log(c) + k*Ln2.  */
115*f3087befSAndrew Turner   float64x2_t ln2_and_lc4 = vld1q_f64 (&d->ln2);
116*f3087befSAndrew Turner   float64x2_t hi = vfmaq_laneq_f64 (vaddq_f64 (e.logc, r), kd, ln2_and_lc4, 0);
117*f3087befSAndrew Turner 
118*f3087befSAndrew Turner   /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
119*f3087befSAndrew Turner   float64x2_t odd_coeffs = vld1q_f64 (&d->lc1);
120*f3087befSAndrew Turner   float64x2_t r2 = vmulq_f64 (r, r);
121*f3087befSAndrew Turner   float64x2_t y = vfmaq_laneq_f64 (d->lc2, r, odd_coeffs, 1);
122*f3087befSAndrew Turner   float64x2_t p = vfmaq_laneq_f64 (d->lc0, r, odd_coeffs, 0);
123*f3087befSAndrew Turner   y = vfmaq_laneq_f64 (y, r2, ln2_and_lc4, 1);
124*f3087befSAndrew Turner   y = vfmaq_f64 (p, r2, y);
125*f3087befSAndrew Turner   return vfmaq_f64 (hi, y, r2);
126*f3087befSAndrew Turner }
127*f3087befSAndrew Turner 
128*f3087befSAndrew Turner /* Double-precision implementation of vector asinh(x).
129*f3087befSAndrew Turner    asinh is very sensitive around 1, so it is impractical to devise a single
130*f3087befSAndrew Turner    low-cost algorithm which is sufficiently accurate on a wide range of input.
131*f3087befSAndrew Turner    Instead we use two different algorithms:
132*f3087befSAndrew Turner    asinh(x) = sign(x) * log(|x| + sqrt(x^2 + 1)      if |x| >= 1
133*f3087befSAndrew Turner 	    = sign(x) * (|x| + |x|^3 * P(x^2))       otherwise
134*f3087befSAndrew Turner    where log(x) is an optimized log approximation, and P(x) is a polynomial
135*f3087befSAndrew Turner    shared with the scalar routine. The greatest observed error 2.79 ULP, in
136*f3087befSAndrew Turner    |x| >= 1:
137*f3087befSAndrew Turner    _ZGVnN2v_asinh(0x1.2cd9d73ea76a6p+0) got 0x1.ffffd003219dap-1
138*f3087befSAndrew Turner 				       want  0x1.ffffd003219ddp-1.  */
V_NAME_D1(asinh)139*f3087befSAndrew Turner VPCS_ATTR float64x2_t V_NAME_D1 (asinh) (float64x2_t x)
140*f3087befSAndrew Turner {
141*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
142*f3087befSAndrew Turner   float64x2_t ax = vabsq_f64 (x);
143*f3087befSAndrew Turner 
144*f3087befSAndrew Turner   uint64x2_t gt1 = vcgeq_f64 (ax, v_f64 (1));
145*f3087befSAndrew Turner 
146*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
147*f3087befSAndrew Turner   uint64x2_t iax = vreinterpretq_u64_f64 (ax);
148*f3087befSAndrew Turner   uint64x2_t special = vcgeq_u64 (iax, (d->huge_bound));
149*f3087befSAndrew Turner   uint64x2_t tiny = vcltq_f64 (ax, d->tiny_bound);
150*f3087befSAndrew Turner   special = vorrq_u64 (special, tiny);
151*f3087befSAndrew Turner #else
152*f3087befSAndrew Turner   uint64x2_t special = vcgeq_f64 (ax, vreinterpretq_f64_u64 (d->huge_bound));
153*f3087befSAndrew Turner #endif
154*f3087befSAndrew Turner 
155*f3087befSAndrew Turner   /* Option 1: |x| >= 1.
156*f3087befSAndrew Turner      Compute asinh(x) according by asinh(x) = log(x + sqrt(x^2 + 1)).
157*f3087befSAndrew Turner      If WANT_SIMD_EXCEPT is enabled, sidestep special values, which will
158*f3087befSAndrew Turner      overflow, by setting special lanes to 1. These will be fixed later.  */
159*f3087befSAndrew Turner   float64x2_t option_1 = v_f64 (0);
160*f3087befSAndrew Turner   if (likely (v_any_u64 (gt1)))
161*f3087befSAndrew Turner     {
162*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
163*f3087befSAndrew Turner       float64x2_t xm = v_zerofy_f64 (ax, special);
164*f3087befSAndrew Turner #else
165*f3087befSAndrew Turner       float64x2_t xm = ax;
166*f3087befSAndrew Turner #endif
167*f3087befSAndrew Turner       option_1 = log_inline (
168*f3087befSAndrew Turner 	  vaddq_f64 (xm, vsqrtq_f64 (vfmaq_f64 (v_f64 (1), xm, xm))), d);
169*f3087befSAndrew Turner     }
170*f3087befSAndrew Turner 
171*f3087befSAndrew Turner   /* Option 2: |x| < 1.
172*f3087befSAndrew Turner      Compute asinh(x) using a polynomial.
173*f3087befSAndrew Turner      If WANT_SIMD_EXCEPT is enabled, sidestep special lanes, which will
174*f3087befSAndrew Turner      overflow, and tiny lanes, which will underflow, by setting them to 0. They
175*f3087befSAndrew Turner      will be fixed later, either by selecting x or falling back to the scalar
176*f3087befSAndrew Turner      special-case. The largest observed error in this region is 1.47 ULPs:
177*f3087befSAndrew Turner      _ZGVnN2v_asinh(0x1.fdfcd00cc1e6ap-1) got 0x1.c1d6bf874019bp-1
178*f3087befSAndrew Turner 					 want 0x1.c1d6bf874019cp-1.  */
179*f3087befSAndrew Turner   float64x2_t option_2 = v_f64 (0);
180*f3087befSAndrew Turner 
181*f3087befSAndrew Turner   if (likely (v_any_u64 (vceqzq_u64 (gt1))))
182*f3087befSAndrew Turner     {
183*f3087befSAndrew Turner 
184*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
185*f3087befSAndrew Turner       ax = v_zerofy_f64 (ax, vorrq_u64 (tiny, gt1));
186*f3087befSAndrew Turner #endif
187*f3087befSAndrew Turner       float64x2_t x2 = vmulq_f64 (ax, ax), z2 = vmulq_f64 (x2, x2);
188*f3087befSAndrew Turner       /* Order-17 Pairwise Horner scheme.  */
189*f3087befSAndrew Turner       float64x2_t c13 = vld1q_f64 (&d->c1);
190*f3087befSAndrew Turner       float64x2_t c57 = vld1q_f64 (&d->c5);
191*f3087befSAndrew Turner       float64x2_t c911 = vld1q_f64 (&d->c9);
192*f3087befSAndrew Turner       float64x2_t c1315 = vld1q_f64 (&d->c13);
193*f3087befSAndrew Turner 
194*f3087befSAndrew Turner       float64x2_t p01 = vfmaq_laneq_f64 (d->c0, x2, c13, 0);
195*f3087befSAndrew Turner       float64x2_t p23 = vfmaq_laneq_f64 (d->c2, x2, c13, 1);
196*f3087befSAndrew Turner       float64x2_t p45 = vfmaq_laneq_f64 (d->c4, x2, c57, 0);
197*f3087befSAndrew Turner       float64x2_t p67 = vfmaq_laneq_f64 (d->c6, x2, c57, 1);
198*f3087befSAndrew Turner       float64x2_t p89 = vfmaq_laneq_f64 (d->c8, x2, c911, 0);
199*f3087befSAndrew Turner       float64x2_t p1011 = vfmaq_laneq_f64 (d->c10, x2, c911, 1);
200*f3087befSAndrew Turner       float64x2_t p1213 = vfmaq_laneq_f64 (d->c12, x2, c1315, 0);
201*f3087befSAndrew Turner       float64x2_t p1415 = vfmaq_laneq_f64 (d->c14, x2, c1315, 1);
202*f3087befSAndrew Turner       float64x2_t p1617 = vfmaq_f64 (d->c16, x2, d->c17);
203*f3087befSAndrew Turner 
204*f3087befSAndrew Turner       float64x2_t p = vfmaq_f64 (p1415, z2, p1617);
205*f3087befSAndrew Turner       p = vfmaq_f64 (p1213, z2, p);
206*f3087befSAndrew Turner       p = vfmaq_f64 (p1011, z2, p);
207*f3087befSAndrew Turner       p = vfmaq_f64 (p89, z2, p);
208*f3087befSAndrew Turner 
209*f3087befSAndrew Turner       p = vfmaq_f64 (p67, z2, p);
210*f3087befSAndrew Turner       p = vfmaq_f64 (p45, z2, p);
211*f3087befSAndrew Turner 
212*f3087befSAndrew Turner       p = vfmaq_f64 (p23, z2, p);
213*f3087befSAndrew Turner 
214*f3087befSAndrew Turner       p = vfmaq_f64 (p01, z2, p);
215*f3087befSAndrew Turner       option_2 = vfmaq_f64 (ax, p, vmulq_f64 (ax, x2));
216*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
217*f3087befSAndrew Turner       option_2 = vbslq_f64 (tiny, x, option_2);
218*f3087befSAndrew Turner #endif
219*f3087befSAndrew Turner     }
220*f3087befSAndrew Turner 
221*f3087befSAndrew Turner   /* Choose the right option for each lane.  */
222*f3087befSAndrew Turner   float64x2_t y = vbslq_f64 (gt1, option_1, option_2);
223*f3087befSAndrew Turner   if (unlikely (v_any_u64 (special)))
224*f3087befSAndrew Turner     {
225*f3087befSAndrew Turner       return special_case (x, y, d->abs_mask, special);
226*f3087befSAndrew Turner     }
227*f3087befSAndrew Turner   /* Copy sign.  */
228*f3087befSAndrew Turner   return vbslq_f64 (d->abs_mask, y, x);
229*f3087befSAndrew Turner }
230*f3087befSAndrew Turner 
231*f3087befSAndrew Turner TEST_SIG (V, D, 1, asinh, -10.0, 10.0)
232*f3087befSAndrew Turner TEST_ULP (V_NAME_D1 (asinh), 2.29)
233*f3087befSAndrew Turner TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (asinh), WANT_SIMD_EXCEPT)
234*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (asinh), 0, 0x1p-26, 50000)
235*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (asinh), 0x1p-26, 1, 50000)
236*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (asinh), 1, 0x1p511, 50000)
237*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_D1 (asinh), 0x1p511, inf, 40000)
238*f3087befSAndrew Turner /* Test vector asinh 3 times, with control lane < 1, > 1 and special.
239*f3087befSAndrew Turner    Ensures the v_sel is choosing the right option in all cases.  */
240*f3087befSAndrew Turner TEST_CONTROL_VALUE (V_NAME_D1 (asinh), 0.5)
241*f3087befSAndrew Turner TEST_CONTROL_VALUE (V_NAME_D1 (asinh), 2)
242*f3087befSAndrew Turner TEST_CONTROL_VALUE (V_NAME_D1 (asinh), 0x1p600)
243