1 | /* Double-precision vector (AdvSIMD) exp2 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_LOG2_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 invln2; |
31 | uint64x2_t sign_exp_mask; |
32 | } data = { |
33 | /* Each coefficient was generated to approximate log(r) for |r| < 0x1.fp-9 |
34 | and N = 128, then scaled by log2(e) in extended precision and rounded back |
35 | to double precision. */ |
36 | .poly = { V2 (-0x1.71547652b83p-1), V2 (0x1.ec709dc340953p-2), |
37 | V2 (-0x1.71547651c8f35p-2), V2 (0x1.2777ebe12dda5p-2), |
38 | V2 (-0x1.ec738d616fe26p-3) }, |
39 | .invln2 = V2 (0x1.71547652b82fep0), |
40 | .min_norm = V2 (0x0010000000000000), /* asuint64(0x1p-1022). */ |
41 | .special_bound = V4 (0x7fe00000), /* asuint64(inf) - min_norm. */ |
42 | .sign_exp_mask = V2 (0xfff0000000000000), |
43 | }; |
44 | |
45 | #define Off v_u64 (0x3fe6900900000000) |
46 | #define IndexMask (N - 1) |
47 | |
48 | struct entry |
49 | { |
50 | float64x2_t invc; |
51 | float64x2_t log2c; |
52 | }; |
53 | |
54 | static inline struct entry |
55 | lookup (uint64x2_t i) |
56 | { |
57 | struct entry e; |
58 | uint64_t i0 = (i[0] >> (52 - V_LOG2_TABLE_BITS)) & IndexMask; |
59 | uint64_t i1 = (i[1] >> (52 - V_LOG2_TABLE_BITS)) & IndexMask; |
60 | float64x2_t e0 = vld1q_f64 (&__v_log2_data.table[i0].invc); |
61 | float64x2_t e1 = vld1q_f64 (&__v_log2_data.table[i1].invc); |
62 | e.invc = vuzp1q_f64 (e0, e1); |
63 | e.log2c = vuzp2q_f64 (e0, e1); |
64 | return e; |
65 | } |
66 | |
67 | static float64x2_t VPCS_ATTR NOINLINE |
68 | special_case (float64x2_t x, float64x2_t y, float64x2_t w, float64x2_t r2, |
69 | uint32x2_t special) |
70 | { |
71 | return v_call_f64 (log2, x, vfmaq_f64 (w, r2, y), vmovl_u32 (special)); |
72 | } |
73 | |
74 | /* Double-precision vector log2 routine. Implements the same algorithm as |
75 | vector log10, with coefficients and table entries scaled in extended |
76 | precision. The maximum observed error is 2.58 ULP: |
77 | _ZGVnN2v_log2(0x1.0b556b093869bp+0) got 0x1.fffb34198d9dap-5 |
78 | want 0x1.fffb34198d9ddp-5. */ |
79 | float64x2_t VPCS_ATTR V_NAME_D1 (log2) (float64x2_t x) |
80 | { |
81 | const struct data *d = ptr_barrier (&data); |
82 | uint64x2_t ix = vreinterpretq_u64_f64 (x); |
83 | uint32x2_t special = vcge_u32 (vsubhn_u64 (ix, d->min_norm), |
84 | vget_low_u32 (d->special_bound)); |
85 | |
86 | /* x = 2^k z; where z is in range [Off,2*Off) and exact. |
87 | The range is split into N subintervals. |
88 | The ith subinterval contains z and c is near its center. */ |
89 | uint64x2_t tmp = vsubq_u64 (ix, Off); |
90 | int64x2_t k = vshrq_n_s64 (vreinterpretq_s64_u64 (tmp), 52); |
91 | uint64x2_t iz = vsubq_u64 (ix, vandq_u64 (tmp, d->sign_exp_mask)); |
92 | float64x2_t z = vreinterpretq_f64_u64 (iz); |
93 | |
94 | struct entry e = lookup (tmp); |
95 | |
96 | /* log2(x) = log1p(z/c-1)/log(2) + log2(c) + k. */ |
97 | |
98 | float64x2_t r = vfmaq_f64 (v_f64 (-1.0), z, e.invc); |
99 | float64x2_t kd = vcvtq_f64_s64 (k); |
100 | float64x2_t w = vfmaq_f64 (e.log2c, r, d->invln2); |
101 | |
102 | float64x2_t r2 = vmulq_f64 (r, r); |
103 | float64x2_t y = v_pw_horner_4_f64 (r, r2, d->poly); |
104 | w = vaddq_f64 (kd, w); |
105 | |
106 | if (__glibc_unlikely (v_any_u32h (special))) |
107 | return special_case (x, y, w, r2, special); |
108 | return vfmaq_f64 (w, r2, y); |
109 | } |
110 | |