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