1 | /* Double-precision vector (SVE) exp10 function. |
2 | |
3 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
4 | This file is part of the GNU C Library. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include "sv_math.h" |
21 | #include "poly_sve_f64.h" |
22 | |
23 | #define SpecialBound 307.0 /* floor (log10 (2^1023)). */ |
24 | |
25 | static const struct data |
26 | { |
27 | double poly[5]; |
28 | double shift, log10_2, log2_10_hi, log2_10_lo, scale_thres, special_bound; |
29 | } data = { |
30 | /* Coefficients generated using Remez algorithm. |
31 | rel error: 0x1.9fcb9b3p-60 |
32 | abs error: 0x1.a20d9598p-60 in [ -log10(2)/128, log10(2)/128 ] |
33 | max ulp err 0.52 +0.5. */ |
34 | .poly = { 0x1.26bb1bbb55516p1, 0x1.53524c73cd32ap1, 0x1.0470591daeafbp1, |
35 | 0x1.2bd77b1361ef6p0, 0x1.142b5d54e9621p-1 }, |
36 | /* 1.5*2^46+1023. This value is further explained below. */ |
37 | .shift = 0x1.800000000ffc0p+46, |
38 | .log10_2 = 0x1.a934f0979a371p1, /* 1/log2(10). */ |
39 | .log2_10_hi = 0x1.34413509f79ffp-2, /* log2(10). */ |
40 | .log2_10_lo = -0x1.9dc1da994fd21p-59, |
41 | .scale_thres = 1280.0, |
42 | .special_bound = SpecialBound, |
43 | }; |
44 | |
45 | #define SpecialOffset 0x6000000000000000 /* 0x1p513. */ |
46 | /* SpecialBias1 + SpecialBias1 = asuint(1.0). */ |
47 | #define SpecialBias1 0x7000000000000000 /* 0x1p769. */ |
48 | #define SpecialBias2 0x3010000000000000 /* 0x1p-254. */ |
49 | |
50 | /* Update of both special and non-special cases, if any special case is |
51 | detected. */ |
52 | static inline svfloat64_t |
53 | special_case (svbool_t pg, svfloat64_t s, svfloat64_t y, svfloat64_t n, |
54 | const struct data *d) |
55 | { |
56 | /* s=2^n may overflow, break it up into s=s1*s2, |
57 | such that exp = s + s*y can be computed as s1*(s2+s2*y) |
58 | and s1*s1 overflows only if n>0. */ |
59 | |
60 | /* If n<=0 then set b to 0x6, 0 otherwise. */ |
61 | svbool_t p_sign = svcmple (pg, n, 0.0); /* n <= 0. */ |
62 | svuint64_t b = svdup_u64_z (p_sign, SpecialOffset); |
63 | |
64 | /* Set s1 to generate overflow depending on sign of exponent n. */ |
65 | svfloat64_t s1 = svreinterpret_f64 (svsubr_x (pg, b, SpecialBias1)); |
66 | /* Offset s to avoid overflow in final result if n is below threshold. */ |
67 | svfloat64_t s2 = svreinterpret_f64 ( |
68 | svadd_x (pg, svsub_x (pg, svreinterpret_u64 (s), SpecialBias2), b)); |
69 | |
70 | /* |n| > 1280 => 2^(n) overflows. */ |
71 | svbool_t p_cmp = svacgt (pg, n, d->scale_thres); |
72 | |
73 | svfloat64_t r1 = svmul_x (pg, s1, s1); |
74 | svfloat64_t r2 = svmla_x (pg, s2, s2, y); |
75 | svfloat64_t r0 = svmul_x (pg, r2, s1); |
76 | |
77 | return svsel (p_cmp, r1, r0); |
78 | } |
79 | |
80 | /* Fast vector implementation of exp10 using FEXPA instruction. |
81 | Maximum measured error is 1.02 ulp. |
82 | SV_NAME_D1 (exp10)(-0x1.2862fec805e58p+2) got 0x1.885a89551d782p-16 |
83 | want 0x1.885a89551d781p-16. */ |
84 | svfloat64_t SV_NAME_D1 (exp10) (svfloat64_t x, svbool_t pg) |
85 | { |
86 | const struct data *d = ptr_barrier (&data); |
87 | svbool_t no_big_scale = svacle (pg, x, d->special_bound); |
88 | svbool_t special = svnot_z (pg, no_big_scale); |
89 | |
90 | /* n = round(x/(log10(2)/N)). */ |
91 | svfloat64_t shift = sv_f64 (x: d->shift); |
92 | svfloat64_t z = svmla_x (pg, shift, x, d->log10_2); |
93 | svfloat64_t n = svsub_x (pg, z, shift); |
94 | |
95 | /* r = x - n*log10(2)/N. */ |
96 | svfloat64_t log2_10 = svld1rq (svptrue_b64 (), &d->log2_10_hi); |
97 | svfloat64_t r = x; |
98 | r = svmls_lane (r, n, log2_10, 0); |
99 | r = svmls_lane (r, n, log2_10, 1); |
100 | |
101 | /* scale = 2^(n/N), computed using FEXPA. FEXPA does not propagate NaNs, so |
102 | for consistent NaN handling we have to manually propagate them. This |
103 | comes at significant performance cost. */ |
104 | svuint64_t u = svreinterpret_u64 (z); |
105 | svfloat64_t scale = svexpa (u); |
106 | |
107 | /* Approximate exp10(r) using polynomial. */ |
108 | svfloat64_t r2 = svmul_x (pg, r, r); |
109 | svfloat64_t y = svmla_x (pg, svmul_x (pg, r, d->poly[0]), r2, |
110 | sv_pairwise_poly_3_f64_x (pg, x: r, x2: r2, poly: d->poly + 1)); |
111 | |
112 | /* Assemble result as exp10(x) = 2^n * exp10(r). If |x| > SpecialBound |
113 | multiplication may overflow, so use special case routine. */ |
114 | if (__glibc_unlikely (svptest_any (pg, special))) |
115 | { |
116 | /* FEXPA zeroes the sign bit, however the sign is meaningful to the |
117 | special case function so needs to be copied. |
118 | e = sign bit of u << 46. */ |
119 | svuint64_t e = svand_x (pg, svlsl_x (pg, u, 46), 0x8000000000000000); |
120 | /* Copy sign to scale. */ |
121 | scale = svreinterpret_f64 (svadd_x (pg, e, svreinterpret_u64 (scale))); |
122 | return special_case (pg, s: scale, y, n, d); |
123 | } |
124 | |
125 | /* No special case. */ |
126 | return svmla_x (pg, scale, scale, y); |
127 | } |
128 | |