1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision vector cbrt(x) function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2022-2023, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li
8*412f47f9SXin Li #include "v_math.h"
9*412f47f9SXin Li #include "pl_sig.h"
10*412f47f9SXin Li #include "pl_test.h"
11*412f47f9SXin Li #include "poly_advsimd_f64.h"
12*412f47f9SXin Li
13*412f47f9SXin Li const static struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li float64x2_t poly[4], one_third, shift;
16*412f47f9SXin Li int64x2_t exp_bias;
17*412f47f9SXin Li uint64x2_t abs_mask, tiny_bound;
18*412f47f9SXin Li uint32x4_t thresh;
19*412f47f9SXin Li double table[5];
20*412f47f9SXin Li } data = {
21*412f47f9SXin Li .shift = V2 (0x1.8p52),
22*412f47f9SXin Li .poly = { /* Generated with fpminimax in [0.5, 1]. */
23*412f47f9SXin Li V2 (0x1.c14e8ee44767p-2), V2 (0x1.dd2d3f99e4c0ep-1),
24*412f47f9SXin Li V2 (-0x1.08e83026b7e74p-1), V2 (0x1.2c74eaa3ba428p-3) },
25*412f47f9SXin Li .exp_bias = V2 (1022),
26*412f47f9SXin Li .abs_mask = V2(0x7fffffffffffffff),
27*412f47f9SXin Li .tiny_bound = V2(0x0010000000000000), /* Smallest normal. */
28*412f47f9SXin Li .thresh = V4(0x7fe00000), /* asuint64 (infinity) - tiny_bound. */
29*412f47f9SXin Li .one_third = V2(0x1.5555555555555p-2),
30*412f47f9SXin Li .table = { /* table[i] = 2^((i - 2) / 3). */
31*412f47f9SXin Li 0x1.428a2f98d728bp-1, 0x1.965fea53d6e3dp-1, 0x1p0,
32*412f47f9SXin Li 0x1.428a2f98d728bp0, 0x1.965fea53d6e3dp0 }
33*412f47f9SXin Li };
34*412f47f9SXin Li
35*412f47f9SXin Li #define MantissaMask v_u64 (0x000fffffffffffff)
36*412f47f9SXin Li
37*412f47f9SXin Li static float64x2_t NOINLINE VPCS_ATTR
special_case(float64x2_t x,float64x2_t y,uint32x2_t special)38*412f47f9SXin Li special_case (float64x2_t x, float64x2_t y, uint32x2_t special)
39*412f47f9SXin Li {
40*412f47f9SXin Li return v_call_f64 (cbrt, x, y, vmovl_u32 (special));
41*412f47f9SXin Li }
42*412f47f9SXin Li
43*412f47f9SXin Li /* Approximation for double-precision vector cbrt(x), using low-order polynomial
44*412f47f9SXin Li and two Newton iterations. Greatest observed error is 1.79 ULP. Errors repeat
45*412f47f9SXin Li according to the exponent, for instance an error observed for double value
46*412f47f9SXin Li m * 2^e will be observed for any input m * 2^(e + 3*i), where i is an
47*412f47f9SXin Li integer.
48*412f47f9SXin Li __v_cbrt(0x1.fffff403f0bc6p+1) got 0x1.965fe72821e9bp+0
49*412f47f9SXin Li want 0x1.965fe72821e99p+0. */
V_NAME_D1(cbrt)50*412f47f9SXin Li VPCS_ATTR float64x2_t V_NAME_D1 (cbrt) (float64x2_t x)
51*412f47f9SXin Li {
52*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
53*412f47f9SXin Li uint64x2_t iax = vreinterpretq_u64_f64 (vabsq_f64 (x));
54*412f47f9SXin Li
55*412f47f9SXin Li /* Subnormal, +/-0 and special values. */
56*412f47f9SXin Li uint32x2_t special
57*412f47f9SXin Li = vcge_u32 (vsubhn_u64 (iax, d->tiny_bound), vget_low_u32 (d->thresh));
58*412f47f9SXin Li
59*412f47f9SXin Li /* Decompose |x| into m * 2^e, where m is in [0.5, 1.0]. This is a vector
60*412f47f9SXin Li version of frexp, which gets subnormal values wrong - these have to be
61*412f47f9SXin Li special-cased as a result. */
62*412f47f9SXin Li float64x2_t m = vbslq_f64 (MantissaMask, x, v_f64 (0.5));
63*412f47f9SXin Li int64x2_t exp_bias = d->exp_bias;
64*412f47f9SXin Li uint64x2_t ia12 = vshrq_n_u64 (iax, 52);
65*412f47f9SXin Li int64x2_t e = vsubq_s64 (vreinterpretq_s64_u64 (ia12), exp_bias);
66*412f47f9SXin Li
67*412f47f9SXin Li /* Calculate rough approximation for cbrt(m) in [0.5, 1.0], starting point for
68*412f47f9SXin Li Newton iterations. */
69*412f47f9SXin Li float64x2_t p = v_pairwise_poly_3_f64 (m, vmulq_f64 (m, m), d->poly);
70*412f47f9SXin Li float64x2_t one_third = d->one_third;
71*412f47f9SXin Li /* Two iterations of Newton's method for iteratively approximating cbrt. */
72*412f47f9SXin Li float64x2_t m_by_3 = vmulq_f64 (m, one_third);
73*412f47f9SXin Li float64x2_t two_thirds = vaddq_f64 (one_third, one_third);
74*412f47f9SXin Li float64x2_t a
75*412f47f9SXin Li = vfmaq_f64 (vdivq_f64 (m_by_3, vmulq_f64 (p, p)), two_thirds, p);
76*412f47f9SXin Li a = vfmaq_f64 (vdivq_f64 (m_by_3, vmulq_f64 (a, a)), two_thirds, a);
77*412f47f9SXin Li
78*412f47f9SXin Li /* Assemble the result by the following:
79*412f47f9SXin Li
80*412f47f9SXin Li cbrt(x) = cbrt(m) * 2 ^ (e / 3).
81*412f47f9SXin Li
82*412f47f9SXin Li We can get 2 ^ round(e / 3) using ldexp and integer divide, but since e is
83*412f47f9SXin Li not necessarily a multiple of 3 we lose some information.
84*412f47f9SXin Li
85*412f47f9SXin Li Let q = 2 ^ round(e / 3), then t = 2 ^ (e / 3) / q.
86*412f47f9SXin Li
87*412f47f9SXin Li Then we know t = 2 ^ (i / 3), where i is the remainder from e / 3, which is
88*412f47f9SXin Li an integer in [-2, 2], and can be looked up in the table T. Hence the
89*412f47f9SXin Li result is assembled as:
90*412f47f9SXin Li
91*412f47f9SXin Li cbrt(x) = cbrt(m) * t * 2 ^ round(e / 3) * sign. */
92*412f47f9SXin Li
93*412f47f9SXin Li float64x2_t ef = vcvtq_f64_s64 (e);
94*412f47f9SXin Li float64x2_t eb3f = vrndnq_f64 (vmulq_f64 (ef, one_third));
95*412f47f9SXin Li int64x2_t em3 = vcvtq_s64_f64 (vfmsq_f64 (ef, eb3f, v_f64 (3)));
96*412f47f9SXin Li int64x2_t ey = vcvtq_s64_f64 (eb3f);
97*412f47f9SXin Li
98*412f47f9SXin Li float64x2_t my = (float64x2_t){ d->table[em3[0] + 2], d->table[em3[1] + 2] };
99*412f47f9SXin Li my = vmulq_f64 (my, a);
100*412f47f9SXin Li
101*412f47f9SXin Li /* Vector version of ldexp. */
102*412f47f9SXin Li float64x2_t y = vreinterpretq_f64_s64 (
103*412f47f9SXin Li vshlq_n_s64 (vaddq_s64 (ey, vaddq_s64 (exp_bias, v_s64 (1))), 52));
104*412f47f9SXin Li y = vmulq_f64 (y, my);
105*412f47f9SXin Li
106*412f47f9SXin Li if (unlikely (v_any_u32h (special)))
107*412f47f9SXin Li return special_case (x, vbslq_f64 (d->abs_mask, y, x), special);
108*412f47f9SXin Li
109*412f47f9SXin Li /* Copy sign. */
110*412f47f9SXin Li return vbslq_f64 (d->abs_mask, y, x);
111*412f47f9SXin Li }
112*412f47f9SXin Li
113*412f47f9SXin Li PL_TEST_ULP (V_NAME_D1 (cbrt), 1.30)
114*412f47f9SXin Li PL_SIG (V, D, 1, cbrt, -10.0, 10.0)
115*412f47f9SXin Li PL_TEST_EXPECT_FENV_ALWAYS (V_NAME_D1 (cbrt))
116*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (cbrt), 0, inf, 1000000)
117