1 | /* Double-precision vector (AdvSIMD) log10 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 "v_math.h" |
21 | #include "poly_advsimd_f64.h" |
22 | |
23 | #define N (1 << V_LOG10_TABLE_BITS) |
24 | |
25 | static const struct data |
26 | { |
27 | uint64x2_t min_norm; |
28 | uint32x4_t special_bound; |
29 | float64x2_t poly[5]; |
30 | float64x2_t invln10, log10_2, ln2; |
31 | uint64x2_t sign_exp_mask; |
32 | } data = { |
33 | /* Computed from log coefficients divided by log(10) then rounded to double |
34 | precision. */ |
35 | .poly = { V2 (-0x1.bcb7b1526e506p-3), V2 (0x1.287a7636be1d1p-3), |
36 | V2 (-0x1.bcb7b158af938p-4), V2 (0x1.63c78734e6d07p-4), |
37 | V2 (-0x1.287461742fee4p-4) }, |
38 | .ln2 = V2 (0x1.62e42fefa39efp-1), |
39 | .invln10 = V2 (0x1.bcb7b1526e50ep-2), |
40 | .log10_2 = V2 (0x1.34413509f79ffp-2), |
41 | .min_norm = V2 (0x0010000000000000), /* asuint64(0x1p-1022). */ |
42 | .special_bound = V4 (0x7fe00000), /* asuint64(inf) - min_norm. */ |
43 | .sign_exp_mask = V2 (0xfff0000000000000), |
44 | }; |
45 | |
46 | #define Off v_u64 (0x3fe6900900000000) |
47 | #define IndexMask (N - 1) |
48 | |
49 | #define T(s, i) __v_log10_data.s[i] |
50 | |
51 | struct entry |
52 | { |
53 | float64x2_t invc; |
54 | float64x2_t log10c; |
55 | }; |
56 | |
57 | static inline struct entry |
58 | lookup (uint64x2_t i) |
59 | { |
60 | struct entry e; |
61 | uint64_t i0 = (i[0] >> (52 - V_LOG10_TABLE_BITS)) & IndexMask; |
62 | uint64_t i1 = (i[1] >> (52 - V_LOG10_TABLE_BITS)) & IndexMask; |
63 | float64x2_t e0 = vld1q_f64 (&__v_log10_data.table[i0].invc); |
64 | float64x2_t e1 = vld1q_f64 (&__v_log10_data.table[i1].invc); |
65 | e.invc = vuzp1q_f64 (e0, e1); |
66 | e.log10c = vuzp2q_f64 (e0, e1); |
67 | return e; |
68 | } |
69 | |
70 | static float64x2_t VPCS_ATTR NOINLINE |
71 | special_case (float64x2_t x, float64x2_t y, float64x2_t hi, float64x2_t r2, |
72 | uint32x2_t special) |
73 | { |
74 | return v_call_f64 (log10, x, vfmaq_f64 (hi, r2, y), vmovl_u32 (special)); |
75 | } |
76 | |
77 | /* Fast implementation of double-precision vector log10 |
78 | is a slight modification of double-precision vector log. |
79 | Max ULP error: < 2.5 ulp (nearest rounding.) |
80 | Maximum measured at 2.46 ulp for x in [0.96, 0.97] |
81 | _ZGVnN2v_log10(0x1.13192407fcb46p+0) got 0x1.fff6be3cae4bbp-6 |
82 | want 0x1.fff6be3cae4b9p-6. */ |
83 | float64x2_t VPCS_ATTR V_NAME_D1 (log10) (float64x2_t x) |
84 | { |
85 | const struct data *d = ptr_barrier (&data); |
86 | uint64x2_t ix = vreinterpretq_u64_f64 (x); |
87 | uint32x2_t special = vcge_u32 (vsubhn_u64 (ix, d->min_norm), |
88 | vget_low_u32 (d->special_bound)); |
89 | |
90 | /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. |
91 | The range is split into N subintervals. |
92 | The ith subinterval contains z and c is near its center. */ |
93 | uint64x2_t tmp = vsubq_u64 (ix, Off); |
94 | int64x2_t k = vshrq_n_s64 (vreinterpretq_s64_u64 (tmp), 52); |
95 | uint64x2_t iz = vsubq_u64 (ix, vandq_u64 (tmp, d->sign_exp_mask)); |
96 | float64x2_t z = vreinterpretq_f64_u64 (iz); |
97 | |
98 | struct entry e = lookup (tmp); |
99 | |
100 | /* log10(x) = log1p(z/c-1)/log(10) + log10(c) + k*log10(2). */ |
101 | float64x2_t r = vfmaq_f64 (v_f64 (-1.0), z, e.invc); |
102 | float64x2_t kd = vcvtq_f64_s64 (k); |
103 | |
104 | /* hi = r / log(10) + log10(c) + k*log10(2). |
105 | Constants in v_log10_data.c are computed (in extended precision) as |
106 | e.log10c := e.logc * ivln10. */ |
107 | float64x2_t w = vfmaq_f64 (e.log10c, r, d->invln10); |
108 | |
109 | /* y = log10(1+r) + n * log10(2). */ |
110 | float64x2_t hi = vfmaq_f64 (w, kd, d->log10_2); |
111 | |
112 | /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi. */ |
113 | float64x2_t r2 = vmulq_f64 (r, r); |
114 | float64x2_t y = v_pw_horner_4_f64 (r, r2, d->poly); |
115 | |
116 | if (__glibc_unlikely (v_any_u32h (special))) |
117 | return special_case (x, y, hi, r2, special); |
118 | return vfmaq_f64 (hi, r2, y); |
119 | } |
120 | |