xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_cbrtf_1u7.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-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_f32.h"
12*412f47f9SXin Li 
13*412f47f9SXin Li const static struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li   float32x4_t poly[4], one_third;
16*412f47f9SXin Li   float table[5];
17*412f47f9SXin Li } data = {
18*412f47f9SXin Li   .poly = { /* Very rough approximation of cbrt(x) in [0.5, 1], generated with
19*412f47f9SXin Li                FPMinimax.  */
20*412f47f9SXin Li 	    V4 (0x1.c14e96p-2), V4 (0x1.dd2d3p-1), V4 (-0x1.08e81ap-1),
21*412f47f9SXin Li 	    V4 (0x1.2c74c2p-3) },
22*412f47f9SXin Li   .table = { /* table[i] = 2^((i - 2) / 3).  */
23*412f47f9SXin Li 	    0x1.428a3p-1, 0x1.965feap-1, 0x1p0, 0x1.428a3p0, 0x1.965feap0 },
24*412f47f9SXin Li   .one_third = V4 (0x1.555556p-2f),
25*412f47f9SXin Li };
26*412f47f9SXin Li 
27*412f47f9SXin Li #define SignMask v_u32 (0x80000000)
28*412f47f9SXin Li #define SmallestNormal v_u32 (0x00800000)
29*412f47f9SXin Li #define Thresh vdup_n_u16 (0x7f00) /* asuint(INFINITY) - SmallestNormal.  */
30*412f47f9SXin Li #define MantissaMask v_u32 (0x007fffff)
31*412f47f9SXin Li #define HalfExp v_u32 (0x3f000000)
32*412f47f9SXin Li 
33*412f47f9SXin Li static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t x,float32x4_t y,uint16x4_t special)34*412f47f9SXin Li special_case (float32x4_t x, float32x4_t y, uint16x4_t special)
35*412f47f9SXin Li {
36*412f47f9SXin Li   return v_call_f32 (cbrtf, x, y, vmovl_u16 (special));
37*412f47f9SXin Li }
38*412f47f9SXin Li 
39*412f47f9SXin Li static inline float32x4_t
shifted_lookup(const float * table,int32x4_t i)40*412f47f9SXin Li shifted_lookup (const float *table, int32x4_t i)
41*412f47f9SXin Li {
42*412f47f9SXin Li   return (float32x4_t){ table[i[0] + 2], table[i[1] + 2], table[i[2] + 2],
43*412f47f9SXin Li 			table[i[3] + 2] };
44*412f47f9SXin Li }
45*412f47f9SXin Li 
46*412f47f9SXin Li /* Approximation for vector single-precision cbrt(x) using Newton iteration
47*412f47f9SXin Li    with initial guess obtained by a low-order polynomial. Greatest error
48*412f47f9SXin Li    is 1.64 ULP. This is observed for every value where the mantissa is
49*412f47f9SXin Li    0x1.85a2aa and the exponent is a multiple of 3, for example:
50*412f47f9SXin Li    _ZGVnN4v_cbrtf(0x1.85a2aap+3) got 0x1.267936p+1
51*412f47f9SXin Li 				want 0x1.267932p+1.  */
V_NAME_F1(cbrt)52*412f47f9SXin Li VPCS_ATTR float32x4_t V_NAME_F1 (cbrt) (float32x4_t x)
53*412f47f9SXin Li {
54*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
55*412f47f9SXin Li   uint32x4_t iax = vreinterpretq_u32_f32 (vabsq_f32 (x));
56*412f47f9SXin Li 
57*412f47f9SXin Li   /* Subnormal, +/-0 and special values.  */
58*412f47f9SXin Li   uint16x4_t special = vcge_u16 (vsubhn_u32 (iax, SmallestNormal), Thresh);
59*412f47f9SXin Li 
60*412f47f9SXin Li   /* Decompose |x| into m * 2^e, where m is in [0.5, 1.0]. This is a vector
61*412f47f9SXin Li      version of frexpf, which gets subnormal values wrong - these have to be
62*412f47f9SXin Li      special-cased as a result.  */
63*412f47f9SXin Li   float32x4_t m = vbslq_f32 (MantissaMask, x, v_f32 (0.5));
64*412f47f9SXin Li   int32x4_t e
65*412f47f9SXin Li       = vsubq_s32 (vreinterpretq_s32_u32 (vshrq_n_u32 (iax, 23)), v_s32 (126));
66*412f47f9SXin Li 
67*412f47f9SXin Li   /* p is a rough approximation for cbrt(m) in [0.5, 1.0]. The better this is,
68*412f47f9SXin Li      the less accurate the next stage of the algorithm needs to be. An order-4
69*412f47f9SXin Li      polynomial is enough for one Newton iteration.  */
70*412f47f9SXin Li   float32x4_t p = v_pairwise_poly_3_f32 (m, vmulq_f32 (m, m), d->poly);
71*412f47f9SXin Li 
72*412f47f9SXin Li   float32x4_t one_third = d->one_third;
73*412f47f9SXin Li   float32x4_t two_thirds = vaddq_f32 (one_third, one_third);
74*412f47f9SXin Li 
75*412f47f9SXin Li   /* One iteration of Newton's method for iteratively approximating cbrt.  */
76*412f47f9SXin Li   float32x4_t m_by_3 = vmulq_f32 (m, one_third);
77*412f47f9SXin Li   float32x4_t a
78*412f47f9SXin Li       = vfmaq_f32 (vdivq_f32 (m_by_3, vmulq_f32 (p, p)), two_thirds, p);
79*412f47f9SXin Li 
80*412f47f9SXin Li   /* Assemble the result by the following:
81*412f47f9SXin Li 
82*412f47f9SXin Li      cbrt(x) = cbrt(m) * 2 ^ (e / 3).
83*412f47f9SXin Li 
84*412f47f9SXin Li      We can get 2 ^ round(e / 3) using ldexp and integer divide, but since e is
85*412f47f9SXin Li      not necessarily a multiple of 3 we lose some information.
86*412f47f9SXin Li 
87*412f47f9SXin Li      Let q = 2 ^ round(e / 3), then t = 2 ^ (e / 3) / q.
88*412f47f9SXin Li 
89*412f47f9SXin Li      Then we know t = 2 ^ (i / 3), where i is the remainder from e / 3, which
90*412f47f9SXin Li      is an integer in [-2, 2], and can be looked up in the table T. Hence the
91*412f47f9SXin Li      result is assembled as:
92*412f47f9SXin Li 
93*412f47f9SXin Li      cbrt(x) = cbrt(m) * t * 2 ^ round(e / 3) * sign.  */
94*412f47f9SXin Li   float32x4_t ef = vmulq_f32 (vcvtq_f32_s32 (e), one_third);
95*412f47f9SXin Li   int32x4_t ey = vcvtq_s32_f32 (ef);
96*412f47f9SXin Li   int32x4_t em3 = vsubq_s32 (e, vmulq_s32 (ey, v_s32 (3)));
97*412f47f9SXin Li 
98*412f47f9SXin Li   float32x4_t my = shifted_lookup (d->table, em3);
99*412f47f9SXin Li   my = vmulq_f32 (my, a);
100*412f47f9SXin Li 
101*412f47f9SXin Li   /* Vector version of ldexpf.  */
102*412f47f9SXin Li   float32x4_t y
103*412f47f9SXin Li       = vreinterpretq_f32_s32 (vshlq_n_s32 (vaddq_s32 (ey, v_s32 (127)), 23));
104*412f47f9SXin Li   y = vmulq_f32 (y, my);
105*412f47f9SXin Li 
106*412f47f9SXin Li   if (unlikely (v_any_u16h (special)))
107*412f47f9SXin Li     return special_case (x, vbslq_f32 (SignMask, x, y), special);
108*412f47f9SXin Li 
109*412f47f9SXin Li   /* Copy sign.  */
110*412f47f9SXin Li   return vbslq_f32 (SignMask, x, y);
111*412f47f9SXin Li }
112*412f47f9SXin Li 
113*412f47f9SXin Li PL_SIG (V, F, 1, cbrt, -10.0, 10.0)
114*412f47f9SXin Li PL_TEST_ULP (V_NAME_F1 (cbrt), 1.15)
115*412f47f9SXin Li PL_TEST_EXPECT_FENV_ALWAYS (V_NAME_F1 (cbrt))
116*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (cbrt), 0, inf, 1000000)
117