1 | /* Single-precision AdvSIMD log1p |
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_f32.h" |
22 | |
23 | const static struct data |
24 | { |
25 | float32x4_t poly[8], ln2; |
26 | uint32x4_t tiny_bound, minus_one, four, thresh; |
27 | int32x4_t three_quarters; |
28 | } data = { |
29 | .poly = { /* Generated using FPMinimax in [-0.25, 0.5]. First two coefficients |
30 | (1, -0.5) are not stored as they can be generated more |
31 | efficiently. */ |
32 | V4 (0x1.5555aap-2f), V4 (-0x1.000038p-2f), V4 (0x1.99675cp-3f), |
33 | V4 (-0x1.54ef78p-3f), V4 (0x1.28a1f4p-3f), V4 (-0x1.0da91p-3f), |
34 | V4 (0x1.abcb6p-4f), V4 (-0x1.6f0d5ep-5f) }, |
35 | .ln2 = V4 (0x1.62e43p-1f), |
36 | .tiny_bound = V4 (0x34000000), /* asuint32(0x1p-23). ulp=0.5 at 0x1p-23. */ |
37 | .thresh = V4 (0x4b800000), /* asuint32(INFINITY) - tiny_bound. */ |
38 | .minus_one = V4 (0xbf800000), |
39 | .four = V4 (0x40800000), |
40 | .three_quarters = V4 (0x3f400000) |
41 | }; |
42 | |
43 | static inline float32x4_t |
44 | eval_poly (float32x4_t m, const float32x4_t *p) |
45 | { |
46 | /* Approximate log(1+m) on [-0.25, 0.5] using split Estrin scheme. */ |
47 | float32x4_t p_12 = vfmaq_f32 (v_f32 (-0.5), m, p[0]); |
48 | float32x4_t p_34 = vfmaq_f32 (p[1], m, p[2]); |
49 | float32x4_t p_56 = vfmaq_f32 (p[3], m, p[4]); |
50 | float32x4_t p_78 = vfmaq_f32 (p[5], m, p[6]); |
51 | |
52 | float32x4_t m2 = vmulq_f32 (m, m); |
53 | float32x4_t p_02 = vfmaq_f32 (m, m2, p_12); |
54 | float32x4_t p_36 = vfmaq_f32 (p_34, m2, p_56); |
55 | float32x4_t p_79 = vfmaq_f32 (p_78, m2, p[7]); |
56 | |
57 | float32x4_t m4 = vmulq_f32 (m2, m2); |
58 | float32x4_t p_06 = vfmaq_f32 (p_02, m4, p_36); |
59 | return vfmaq_f32 (p_06, m4, vmulq_f32 (m4, p_79)); |
60 | } |
61 | |
62 | static float32x4_t NOINLINE VPCS_ATTR |
63 | special_case (float32x4_t x, float32x4_t y, uint32x4_t special) |
64 | { |
65 | return v_call_f32 (log1pf, x, y, special); |
66 | } |
67 | |
68 | /* Vector log1pf approximation using polynomial on reduced interval. Accuracy |
69 | is roughly 2.02 ULP: |
70 | log1pf(0x1.21e13ap-2) got 0x1.fe8028p-3 want 0x1.fe802cp-3. */ |
71 | VPCS_ATTR float32x4_t V_NAME_F1 (log1p) (float32x4_t x) |
72 | { |
73 | const struct data *d = ptr_barrier (&data); |
74 | |
75 | uint32x4_t ix = vreinterpretq_u32_f32 (x); |
76 | uint32x4_t ia = vreinterpretq_u32_f32 (vabsq_f32 (x)); |
77 | uint32x4_t special_cases |
78 | = vorrq_u32 (vcgeq_u32 (vsubq_u32 (ia, d->tiny_bound), d->thresh), |
79 | vcgeq_u32 (ix, d->minus_one)); |
80 | float32x4_t special_arg = x; |
81 | |
82 | #if WANT_SIMD_EXCEPT |
83 | if (__glibc_unlikely (v_any_u32 (special_cases))) |
84 | /* Side-step special lanes so fenv exceptions are not triggered |
85 | inadvertently. */ |
86 | x = v_zerofy_f32 (x, special_cases); |
87 | #endif |
88 | |
89 | /* With x + 1 = t * 2^k (where t = m + 1 and k is chosen such that m |
90 | is in [-0.25, 0.5]): |
91 | log1p(x) = log(t) + log(2^k) = log1p(m) + k*log(2). |
92 | |
93 | We approximate log1p(m) with a polynomial, then scale by |
94 | k*log(2). Instead of doing this directly, we use an intermediate |
95 | scale factor s = 4*k*log(2) to ensure the scale is representable |
96 | as a normalised fp32 number. */ |
97 | |
98 | float32x4_t m = vaddq_f32 (x, v_f32 (1.0f)); |
99 | |
100 | /* Choose k to scale x to the range [-1/4, 1/2]. */ |
101 | int32x4_t k |
102 | = vandq_s32 (vsubq_s32 (vreinterpretq_s32_f32 (m), d->three_quarters), |
103 | v_s32 (0xff800000)); |
104 | uint32x4_t ku = vreinterpretq_u32_s32 (k); |
105 | |
106 | /* Scale x by exponent manipulation. */ |
107 | float32x4_t m_scale |
108 | = vreinterpretq_f32_u32 (vsubq_u32 (vreinterpretq_u32_f32 (x), ku)); |
109 | |
110 | /* Scale up to ensure that the scale factor is representable as normalised |
111 | fp32 number, and scale m down accordingly. */ |
112 | float32x4_t s = vreinterpretq_f32_u32 (vsubq_u32 (d->four, ku)); |
113 | m_scale = vaddq_f32 (m_scale, vfmaq_f32 (v_f32 (-1.0f), v_f32 (0.25f), s)); |
114 | |
115 | /* Evaluate polynomial on the reduced interval. */ |
116 | float32x4_t p = eval_poly (m_scale, d->poly); |
117 | |
118 | /* The scale factor to be applied back at the end - by multiplying float(k) |
119 | by 2^-23 we get the unbiased exponent of k. */ |
120 | float32x4_t scale_back = vcvtq_f32_s32 (vshrq_n_s32 (k, 23)); |
121 | |
122 | /* Apply the scaling back. */ |
123 | float32x4_t y = vfmaq_f32 (p, scale_back, d->ln2); |
124 | |
125 | if (__glibc_unlikely (v_any_u32 (special_cases))) |
126 | return special_case (special_arg, y, special_cases); |
127 | return y; |
128 | } |
129 | libmvec_hidden_def (V_NAME_F1 (log1p)) |
130 | HALF_WIDTH_ALIAS_F1 (log1p) |
131 | |