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