• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Single-precision vector cbrt(x) function.
3  *
4  * Copyright (c) 2022-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "v_math.h"
9 #include "mathlib.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 #if V_SUPPORTED
14 
15 #define AbsMask 0x7fffffff
16 #define SignMask v_u32 (0x80000000)
17 #define TwoThirds v_f32 (0x1.555556p-1f)
18 #define SmallestNormal 0x00800000
19 #define MantissaMask 0x007fffff
20 #define HalfExp 0x3f000000
21 
22 #define C(i) v_f32 (__cbrtf_data.poly[i])
23 #define T(i) v_lookup_f32 (__cbrtf_data.table, i)
24 
25 static NOINLINE v_f32_t
specialcase(v_f32_t x,v_f32_t y,v_u32_t special)26 specialcase (v_f32_t x, v_f32_t y, v_u32_t special)
27 {
28   return v_call_f32 (cbrtf, x, y, special);
29 }
30 
31 /* Approximation for vector single-precision cbrt(x) using Newton iteration with
32    initial guess obtained by a low-order polynomial. Greatest error is 1.5 ULP.
33    This is observed for every value where the mantissa is 0x1.81410e and the
34    exponent is a multiple of 3, for example:
35    __v_cbrtf(0x1.81410ep+30) got 0x1.255d96p+10
36 			    want 0x1.255d92p+10.  */
V_NAME(cbrtf)37 VPCS_ATTR v_f32_t V_NAME (cbrtf) (v_f32_t x)
38 {
39   v_u32_t ix = v_as_u32_f32 (x);
40   v_u32_t iax = ix & AbsMask;
41 
42   /* Subnormal, +/-0 and special values.  */
43   v_u32_t special = v_cond_u32 ((iax < SmallestNormal) | (iax >= 0x7f800000));
44 
45   /* Decompose |x| into m * 2^e, where m is in [0.5, 1.0]. This is a vector
46      version of frexpf, which gets subnormal values wrong - these have to be
47      special-cased as a result.  */
48   v_f32_t m = v_as_f32_u32 ((iax & MantissaMask) | HalfExp);
49   v_s32_t e = v_as_s32_u32 (iax >> 23) - 126;
50 
51   /* p is a rough approximation for cbrt(m) in [0.5, 1.0]. The better this is,
52      the less accurate the next stage of the algorithm needs to be. An order-4
53      polynomial is enough for one Newton iteration.  */
54   v_f32_t p_01 = v_fma_f32 (C (1), m, C (0));
55   v_f32_t p_23 = v_fma_f32 (C (3), m, C (2));
56   v_f32_t p = v_fma_f32 (m * m, p_23, p_01);
57 
58   /* One iteration of Newton's method for iteratively approximating cbrt.  */
59   v_f32_t m_by_3 = m / 3;
60   v_f32_t a = v_fma_f32 (TwoThirds, p, m_by_3 / (p * p));
61 
62   /* Assemble the result by the following:
63 
64      cbrt(x) = cbrt(m) * 2 ^ (e / 3).
65 
66      We can get 2 ^ round(e / 3) using ldexp and integer divide, but since e is
67      not necessarily a multiple of 3 we lose some information.
68 
69      Let q = 2 ^ round(e / 3), then t = 2 ^ (e / 3) / q.
70 
71      Then we know t = 2 ^ (i / 3), where i is the remainder from e / 3, which is
72      an integer in [-2, 2], and can be looked up in the table T. Hence the
73      result is assembled as:
74 
75      cbrt(x) = cbrt(m) * t * 2 ^ round(e / 3) * sign.  */
76 
77   v_s32_t ey = e / 3;
78   v_f32_t my = a * T (v_as_u32_s32 (e % 3 + 2));
79 
80   /* Vector version of ldexpf.  */
81   v_f32_t y = v_as_f32_u32 ((v_as_u32_s32 (ey + 127) << 23)) * my;
82   /* Copy sign.  */
83   y = v_as_f32_u32 (v_bsl_u32 (SignMask, ix, v_as_u32_f32 (y)));
84 
85   if (unlikely (v_any_u32 (special)))
86     return specialcase (x, y, special);
87   return y;
88 }
89 VPCS_ALIAS
90 
91 PL_SIG (V, F, 1, cbrt, -10.0, 10.0)
92 PL_TEST_ULP (V_NAME (cbrtf), 1.03)
93 PL_TEST_EXPECT_FENV_ALWAYS (V_NAME (cbrtf))
94 PL_TEST_INTERVAL (V_NAME (cbrtf), 0, inf, 1000000)
95 PL_TEST_INTERVAL (V_NAME (cbrtf), -0, -inf, 1000000)
96 #endif
97