xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/sv_cbrt_2u.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Double-precision SVE cbrt(x) function.
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 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 "sv_math.h"
9*412f47f9SXin Li #include "pl_sig.h"
10*412f47f9SXin Li #include "pl_test.h"
11*412f47f9SXin Li #include "poly_sve_f64.h"
12*412f47f9SXin Li 
13*412f47f9SXin Li const static struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li   float64_t poly[4];
16*412f47f9SXin Li   float64_t table[5];
17*412f47f9SXin Li   float64_t one_third, two_thirds, shift;
18*412f47f9SXin Li   int64_t exp_bias;
19*412f47f9SXin Li   uint64_t tiny_bound, thresh;
20*412f47f9SXin Li } data = {
21*412f47f9SXin Li   /* Generated with FPMinimax in [0.5, 1].  */
22*412f47f9SXin Li   .poly = { 0x1.c14e8ee44767p-2, 0x1.dd2d3f99e4c0ep-1, -0x1.08e83026b7e74p-1,
23*412f47f9SXin Li 	    0x1.2c74eaa3ba428p-3, },
24*412f47f9SXin Li   /* table[i] = 2^((i - 2) / 3).  */
25*412f47f9SXin Li   .table = { 0x1.428a2f98d728bp-1, 0x1.965fea53d6e3dp-1, 0x1p0,
26*412f47f9SXin Li 	     0x1.428a2f98d728bp0, 0x1.965fea53d6e3dp0, },
27*412f47f9SXin Li   .one_third = 0x1.5555555555555p-2,
28*412f47f9SXin Li   .two_thirds = 0x1.5555555555555p-1,
29*412f47f9SXin Li   .shift = 0x1.8p52,
30*412f47f9SXin Li   .exp_bias = 1022,
31*412f47f9SXin Li   .tiny_bound = 0x0010000000000000, /* Smallest normal.  */
32*412f47f9SXin Li   .thresh = 0x7fe0000000000000, /* asuint64 (infinity) - tiny_bound.  */
33*412f47f9SXin Li };
34*412f47f9SXin Li 
35*412f47f9SXin Li #define MantissaMask 0x000fffffffffffff
36*412f47f9SXin Li #define HalfExp 0x3fe0000000000000
37*412f47f9SXin Li 
38*412f47f9SXin Li static svfloat64_t NOINLINE
special_case(svfloat64_t x,svfloat64_t y,svbool_t special)39*412f47f9SXin Li special_case (svfloat64_t x, svfloat64_t y, svbool_t special)
40*412f47f9SXin Li {
41*412f47f9SXin Li   return sv_call_f64 (cbrt, x, y, special);
42*412f47f9SXin Li }
43*412f47f9SXin Li 
44*412f47f9SXin Li static inline svfloat64_t
shifted_lookup(const svbool_t pg,const float64_t * table,svint64_t i)45*412f47f9SXin Li shifted_lookup (const svbool_t pg, const float64_t *table, svint64_t i)
46*412f47f9SXin Li {
47*412f47f9SXin Li   return svld1_gather_index (pg, table, svadd_x (pg, i, 2));
48*412f47f9SXin Li }
49*412f47f9SXin Li 
50*412f47f9SXin Li /* Approximation for double-precision vector cbrt(x), using low-order
51*412f47f9SXin Li    polynomial and two Newton iterations. Greatest observed error is 1.79 ULP.
52*412f47f9SXin Li    Errors repeat according to the exponent, for instance an error observed for
53*412f47f9SXin Li    double value m * 2^e will be observed for any input m * 2^(e + 3*i), where i
54*412f47f9SXin Li    is an integer.
55*412f47f9SXin Li    _ZGVsMxv_cbrt (0x0.3fffb8d4413f3p-1022) got 0x1.965f53b0e5d97p-342
56*412f47f9SXin Li 					  want 0x1.965f53b0e5d95p-342.  */
SV_NAME_D1(cbrt)57*412f47f9SXin Li svfloat64_t SV_NAME_D1 (cbrt) (svfloat64_t x, const svbool_t pg)
58*412f47f9SXin Li {
59*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
60*412f47f9SXin Li 
61*412f47f9SXin Li   svfloat64_t ax = svabs_x (pg, x);
62*412f47f9SXin Li   svuint64_t iax = svreinterpret_u64 (ax);
63*412f47f9SXin Li   svuint64_t sign = sveor_x (pg, svreinterpret_u64 (x), iax);
64*412f47f9SXin Li 
65*412f47f9SXin Li   /* Subnormal, +/-0 and special values.  */
66*412f47f9SXin Li   svbool_t special = svcmpge (pg, svsub_x (pg, iax, d->tiny_bound), d->thresh);
67*412f47f9SXin Li 
68*412f47f9SXin Li   /* Decompose |x| into m * 2^e, where m is in [0.5, 1.0]. This is a vector
69*412f47f9SXin Li      version of frexp, which gets subnormal values wrong - these have to be
70*412f47f9SXin Li      special-cased as a result.  */
71*412f47f9SXin Li   svfloat64_t m = svreinterpret_f64 (svorr_x (
72*412f47f9SXin Li       pg, svand_x (pg, svreinterpret_u64 (x), MantissaMask), HalfExp));
73*412f47f9SXin Li   svint64_t e
74*412f47f9SXin Li       = svsub_x (pg, svreinterpret_s64 (svlsr_x (pg, iax, 52)), d->exp_bias);
75*412f47f9SXin Li 
76*412f47f9SXin Li   /* Calculate rough approximation for cbrt(m) in [0.5, 1.0], starting point
77*412f47f9SXin Li      for Newton iterations.  */
78*412f47f9SXin Li   svfloat64_t p
79*412f47f9SXin Li       = sv_pairwise_poly_3_f64_x (pg, m, svmul_x (pg, m, m), d->poly);
80*412f47f9SXin Li 
81*412f47f9SXin Li   /* Two iterations of Newton's method for iteratively approximating cbrt.  */
82*412f47f9SXin Li   svfloat64_t m_by_3 = svmul_x (pg, m, d->one_third);
83*412f47f9SXin Li   svfloat64_t a = svmla_x (pg, svdiv_x (pg, m_by_3, svmul_x (pg, p, p)), p,
84*412f47f9SXin Li 			   d->two_thirds);
85*412f47f9SXin Li   a = svmla_x (pg, svdiv_x (pg, m_by_3, svmul_x (pg, a, a)), a, d->two_thirds);
86*412f47f9SXin Li 
87*412f47f9SXin Li   /* Assemble the result by the following:
88*412f47f9SXin Li 
89*412f47f9SXin Li      cbrt(x) = cbrt(m) * 2 ^ (e / 3).
90*412f47f9SXin Li 
91*412f47f9SXin Li      We can get 2 ^ round(e / 3) using ldexp and integer divide, but since e is
92*412f47f9SXin Li      not necessarily a multiple of 3 we lose some information.
93*412f47f9SXin Li 
94*412f47f9SXin Li      Let q = 2 ^ round(e / 3), then t = 2 ^ (e / 3) / q.
95*412f47f9SXin Li 
96*412f47f9SXin Li      Then we know t = 2 ^ (i / 3), where i is the remainder from e / 3, which
97*412f47f9SXin Li      is an integer in [-2, 2], and can be looked up in the table T. Hence the
98*412f47f9SXin Li      result is assembled as:
99*412f47f9SXin Li 
100*412f47f9SXin Li      cbrt(x) = cbrt(m) * t * 2 ^ round(e / 3) * sign.  */
101*412f47f9SXin Li   svfloat64_t eb3f = svmul_x (pg, svcvt_f64_x (pg, e), d->one_third);
102*412f47f9SXin Li   svint64_t ey = svcvt_s64_x (pg, eb3f);
103*412f47f9SXin Li   svint64_t em3 = svmls_x (pg, e, ey, 3);
104*412f47f9SXin Li 
105*412f47f9SXin Li   svfloat64_t my = shifted_lookup (pg, d->table, em3);
106*412f47f9SXin Li   my = svmul_x (pg, my, a);
107*412f47f9SXin Li 
108*412f47f9SXin Li   /* Vector version of ldexp.  */
109*412f47f9SXin Li   svfloat64_t y = svscale_x (pg, my, ey);
110*412f47f9SXin Li 
111*412f47f9SXin Li   if (unlikely (svptest_any (pg, special)))
112*412f47f9SXin Li     return special_case (
113*412f47f9SXin Li 	x, svreinterpret_f64 (svorr_x (pg, svreinterpret_u64 (y), sign)),
114*412f47f9SXin Li 	special);
115*412f47f9SXin Li 
116*412f47f9SXin Li   /* Copy sign.  */
117*412f47f9SXin Li   return svreinterpret_f64 (svorr_x (pg, svreinterpret_u64 (y), sign));
118*412f47f9SXin Li }
119*412f47f9SXin Li 
120*412f47f9SXin Li PL_SIG (SV, D, 1, cbrt, -10.0, 10.0)
121*412f47f9SXin Li PL_TEST_ULP (SV_NAME_D1 (cbrt), 1.30)
122*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_D1 (cbrt), 0, inf, 1000000)
123