/src/Simd/src/Simd/SimdPow.h
Line | Count | Source |
1 | | /* |
2 | | * Simd Library (http://ermig1979.github.io/Simd). |
3 | | * |
4 | | * Copyright (c) 2011-2026 Yermalayeu Ihar. |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to deal |
8 | | * in the Software without restriction, including without limitation the rights |
9 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 | | * copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | | * SOFTWARE. |
23 | | */ |
24 | | #ifndef __SimdPow_h__ |
25 | | #define __SimdPow_h__ |
26 | | |
27 | | #include "Simd/SimdMath.h" |
28 | | |
29 | | namespace Simd |
30 | | { |
31 | | namespace Base |
32 | | { |
33 | | SIMD_INLINE float Pow(float basis, float exponent) |
34 | 0 | { |
35 | 0 | return ::expf(::logf(basis)*exponent); |
36 | 0 | } |
37 | | } |
38 | | |
39 | | #ifdef SIMD_SSE41_ENABLE |
40 | | namespace Sse41 |
41 | | { |
42 | | class Pow |
43 | | { |
44 | | __m128i _exponent, _mantissa; |
45 | | __m128 _one; |
46 | | |
47 | | SIMD_INLINE __m128 Poly5(__m128 x, float a, float b, float c, float d, float e, float f) const |
48 | 0 | { |
49 | 0 | __m128 p = _mm_set1_ps(f); |
50 | 0 | p = _mm_add_ps(_mm_mul_ps(x, p), _mm_set1_ps(e)); |
51 | 0 | p = _mm_add_ps(_mm_mul_ps(x, p), _mm_set1_ps(d)); |
52 | 0 | p = _mm_add_ps(_mm_mul_ps(x, p), _mm_set1_ps(c)); |
53 | 0 | p = _mm_add_ps(_mm_mul_ps(x, p), _mm_set1_ps(b)); |
54 | 0 | p = _mm_add_ps(_mm_mul_ps(x, p), _mm_set1_ps(a)); |
55 | 0 | return p; |
56 | 0 | } |
57 | | |
58 | | SIMD_INLINE __m128 Exp2(__m128 x) const |
59 | 0 | { |
60 | 0 | x = _mm_max_ps(_mm_min_ps(x, _mm_set1_ps(129.00000f)), _mm_set1_ps(-126.99999f)); |
61 | 0 | __m128i ipart = _mm_cvtps_epi32(_mm_sub_ps(x, _mm_set1_ps(0.5f))); |
62 | 0 | __m128 fpart = _mm_sub_ps(x, _mm_cvtepi32_ps(ipart)); |
63 | 0 | __m128 expipart = _mm_castsi128_ps(_mm_slli_epi32(_mm_add_epi32(ipart, _mm_set1_epi32(127)), 23)); |
64 | 0 | __m128 expfpart = Poly5(fpart, 9.9999994e-1f, 6.9315308e-1f, 2.4015361e-1f, 5.5826318e-2f, 8.9893397e-3f, 1.8775767e-3f); |
65 | 0 | return _mm_mul_ps(expipart, expfpart); |
66 | 0 | } |
67 | | |
68 | | SIMD_INLINE __m128 Log2(__m128 x) const |
69 | 0 | { |
70 | 0 | __m128i i = _mm_castps_si128(x); |
71 | 0 | __m128 e = _mm_cvtepi32_ps(_mm_sub_epi32(_mm_srli_epi32(_mm_and_si128(i, _exponent), 23), _mm_set1_epi32(127))); |
72 | 0 | __m128 m = _mm_or_ps(_mm_castsi128_ps(_mm_and_si128(i, _mantissa)), _one); |
73 | 0 | __m128 p = Poly5(m, 3.1157899f, -3.3241990f, 2.5988452f, -1.2315303f, 3.1821337e-1f, -3.4436006e-2f); |
74 | 0 | return _mm_add_ps(_mm_mul_ps(p, _mm_sub_ps(m, _one)), e); |
75 | 0 | } |
76 | | |
77 | | public: |
78 | | |
79 | | SIMD_INLINE Pow() |
80 | 0 | { |
81 | 0 | _exponent = _mm_set1_epi32(0x7F800000); |
82 | 0 | _mantissa = _mm_set1_epi32(0x007FFFFF); |
83 | 0 | _one = _mm_set1_ps(1.0f); |
84 | 0 | } |
85 | | |
86 | | SIMD_INLINE __m128 operator() (__m128 basis, __m128 exponent) const |
87 | 0 | { |
88 | 0 | return Exp2(_mm_mul_ps(Log2(basis), exponent)); |
89 | 0 | } |
90 | | }; |
91 | | } |
92 | | #endif //SIMD_SSE41_ENABLE |
93 | | |
94 | | #ifdef SIMD_AVX2_ENABLE |
95 | | namespace Avx2 |
96 | | { |
97 | | class Pow |
98 | | { |
99 | | __m256i _exponent, _mantissa; |
100 | | __m256 _one; |
101 | | |
102 | | SIMD_INLINE __m256 Poly5(__m256 x, float a, float b, float c, float d, float e, float f) const |
103 | 0 | { |
104 | 0 | __m256 p = _mm256_set1_ps(f); |
105 | 0 | p = _mm256_fmadd_ps(x, p, _mm256_set1_ps(e)); |
106 | 0 | p = _mm256_fmadd_ps(x, p, _mm256_set1_ps(d)); |
107 | 0 | p = _mm256_fmadd_ps(x, p, _mm256_set1_ps(c)); |
108 | 0 | p = _mm256_fmadd_ps(x, p, _mm256_set1_ps(b)); |
109 | 0 | p = _mm256_fmadd_ps(x, p, _mm256_set1_ps(a)); |
110 | 0 | return p; |
111 | 0 | } |
112 | | |
113 | | SIMD_INLINE __m256 Exp2(__m256 x) const |
114 | 0 | { |
115 | 0 | x = _mm256_max_ps(_mm256_min_ps(x, _mm256_set1_ps(129.00000f)), _mm256_set1_ps(-126.99999f)); |
116 | 0 | __m256i ipart = _mm256_cvtps_epi32(_mm256_sub_ps(x, _mm256_set1_ps(0.5f))); |
117 | 0 | __m256 fpart = _mm256_sub_ps(x, _mm256_cvtepi32_ps(ipart)); |
118 | 0 | __m256 expipart = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_add_epi32(ipart, _mm256_set1_epi32(127)), 23)); |
119 | 0 | __m256 expfpart = Poly5(fpart, 9.9999994e-1f, 6.9315308e-1f, 2.4015361e-1f, 5.5826318e-2f, 8.9893397e-3f, 1.8775767e-3f); |
120 | 0 | return _mm256_mul_ps(expipart, expfpart); |
121 | 0 | } |
122 | | |
123 | | SIMD_INLINE __m256 Log2(__m256 x) const |
124 | 0 | { |
125 | 0 | __m256i i = _mm256_castps_si256(x); |
126 | 0 | __m256 e = _mm256_cvtepi32_ps(_mm256_sub_epi32(_mm256_srli_epi32(_mm256_and_si256(i, _exponent), 23), _mm256_set1_epi32(127))); |
127 | 0 | __m256 m = _mm256_or_ps(_mm256_castsi256_ps(_mm256_and_si256(i, _mantissa)), _one); |
128 | 0 | __m256 p = Poly5(m, 3.1157899f, -3.3241990f, 2.5988452f, -1.2315303f, 3.1821337e-1f, -3.4436006e-2f); |
129 | 0 | return _mm256_fmadd_ps(p, _mm256_sub_ps(m, _one), e); |
130 | 0 | } |
131 | | |
132 | | public: |
133 | | |
134 | | SIMD_INLINE Pow() |
135 | 0 | { |
136 | 0 | _exponent = _mm256_set1_epi32(0x7F800000); |
137 | 0 | _mantissa = _mm256_set1_epi32(0x007FFFFF); |
138 | 0 | _one = _mm256_set1_ps(1.0f); |
139 | 0 | } |
140 | | |
141 | | SIMD_INLINE __m256 operator()(__m256 basis, __m256 exponent) const |
142 | 0 | { |
143 | 0 | return Exp2(_mm256_mul_ps(Log2(basis), exponent)); |
144 | 0 | } |
145 | | }; |
146 | | } |
147 | | #endif //SIMD_AVX2_ENABLE |
148 | | |
149 | | #ifdef SIMD_AVX512BW_ENABLE |
150 | | namespace Avx512bw |
151 | | { |
152 | | class Pow |
153 | | { |
154 | | __m512i _exponent, _mantissa; |
155 | | __m512 _one; |
156 | | |
157 | | SIMD_INLINE __m512 Poly5(__m512 x, float a, float b, float c, float d, float e, float f) const |
158 | 0 | { |
159 | 0 | __m512 p = _mm512_set1_ps(f); |
160 | 0 | p = _mm512_fmadd_ps(x, p, _mm512_set1_ps(e)); |
161 | 0 | p = _mm512_fmadd_ps(x, p, _mm512_set1_ps(d)); |
162 | 0 | p = _mm512_fmadd_ps(x, p, _mm512_set1_ps(c)); |
163 | 0 | p = _mm512_fmadd_ps(x, p, _mm512_set1_ps(b)); |
164 | 0 | p = _mm512_fmadd_ps(x, p, _mm512_set1_ps(a)); |
165 | 0 | return p; |
166 | 0 | } |
167 | | |
168 | | SIMD_INLINE __m512 Exp2(__m512 x) const |
169 | 0 | { |
170 | 0 | x = _mm512_max_ps(_mm512_min_ps(x, _mm512_set1_ps(129.00000f)), _mm512_set1_ps(-126.99999f)); |
171 | 0 | __m512i ipart = _mm512_cvtps_epi32(_mm512_sub_ps(x, _mm512_set1_ps(0.5f))); |
172 | 0 | __m512 fpart = _mm512_sub_ps(x, _mm512_cvtepi32_ps(ipart)); |
173 | 0 | __m512 expipart = _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_add_epi32(ipart, _mm512_set1_epi32(127)), 23)); |
174 | 0 | __m512 expfpart = Poly5(fpart, 9.9999994e-1f, 6.9315308e-1f, 2.4015361e-1f, 5.5826318e-2f, 8.9893397e-3f, 1.8775767e-3f); |
175 | 0 | return _mm512_mul_ps(expipart, expfpart); |
176 | 0 | } |
177 | | |
178 | | SIMD_INLINE __m512 Log2(__m512 x) const |
179 | 0 | { |
180 | 0 | __m512i i = _mm512_castps_si512(x); |
181 | 0 | __m512 e = _mm512_cvtepi32_ps(_mm512_sub_epi32(_mm512_srli_epi32(_mm512_and_si512(i, _exponent), 23), _mm512_set1_epi32(127))); |
182 | 0 | __m512 m = Or(_mm512_castsi512_ps(_mm512_and_si512(i, _mantissa)), _one); |
183 | 0 | __m512 p = Poly5(m, 3.1157899f, -3.3241990f, 2.5988452f, -1.2315303f, 3.1821337e-1f, -3.4436006e-2f); |
184 | 0 | return _mm512_fmadd_ps(p, _mm512_sub_ps(m, _one), e); |
185 | 0 | } |
186 | | |
187 | | public: |
188 | | |
189 | | SIMD_INLINE Pow() |
190 | 0 | { |
191 | 0 | _exponent = _mm512_set1_epi32(0x7F800000); |
192 | 0 | _mantissa = _mm512_set1_epi32(0x007FFFFF); |
193 | 0 | _one = _mm512_set1_ps(1.0f); |
194 | 0 | } |
195 | | |
196 | | SIMD_INLINE __m512 operator()(__m512 basis, __m512 exponent) const |
197 | 0 | { |
198 | 0 | return Exp2(_mm512_mul_ps(Log2(basis), exponent)); |
199 | 0 | } |
200 | | }; |
201 | | } |
202 | | #endif |
203 | | |
204 | | #ifdef SIMD_SVE2_ENABLE |
205 | | namespace Sve2 |
206 | | { |
207 | | class Pow |
208 | | { |
209 | | SIMD_INLINE svfloat32_t Poly5(const svbool_t& mask, const svfloat32_t& x, float a, float b, float c, float d, float e, float f) const |
210 | | { |
211 | | svfloat32_t p = svdup_n_f32(f); |
212 | | p = svmla_f32_x(mask, svdup_n_f32(e), x, p); |
213 | | p = svmla_f32_x(mask, svdup_n_f32(d), x, p); |
214 | | p = svmla_f32_x(mask, svdup_n_f32(c), x, p); |
215 | | p = svmla_f32_x(mask, svdup_n_f32(b), x, p); |
216 | | p = svmla_f32_x(mask, svdup_n_f32(a), x, p); |
217 | | return p; |
218 | | } |
219 | | |
220 | | SIMD_INLINE svfloat32_t Exp2(const svbool_t& mask, const svfloat32_t& x) const |
221 | | { |
222 | | svfloat32_t _x = svmax_n_f32_x(mask, svmin_n_f32_x(mask, x, 129.00000f), -126.99999f); |
223 | | svint32_t ipart = svcvt_s32_f32_x(mask, svsub_n_f32_x(mask, _x, 0.5f)); |
224 | | svfloat32_t fpart = svsub_f32_x(mask, _x, svcvt_f32_s32_x(mask, ipart)); |
225 | | svfloat32_t expipart = svreinterpret_f32_s32(svlsl_n_s32_x(mask, svadd_n_s32_x(mask, ipart, 127), 23)); |
226 | | svfloat32_t expfpart = Poly5(mask, fpart, 9.9999994e-1f, 6.9315308e-1f, 2.4015361e-1f, 5.5826318e-2f, 8.9893397e-3f, 1.8775767e-3f); |
227 | | return svmul_f32_x(mask, expipart, expfpart); |
228 | | } |
229 | | |
230 | | SIMD_INLINE svfloat32_t Log2(const svbool_t& mask, const svfloat32_t& x) const |
231 | | { |
232 | | svuint32_t i = svreinterpret_u32_f32(x); |
233 | | svint32_t e32 = svsub_n_s32_x(mask, svreinterpret_s32_u32(svlsr_n_u32_x(mask, svand_n_u32_x(mask, i, 0x7F800000), 23)), 127); |
234 | | svfloat32_t e = svcvt_f32_s32_x(mask, e32); |
235 | | svfloat32_t one = svdup_n_f32(1.0f); |
236 | | svfloat32_t m = svreinterpret_f32_u32(svorr_u32_x(mask, svand_n_u32_x(mask, i, 0x007FFFFF), svreinterpret_u32_f32(one))); |
237 | | svfloat32_t p = Poly5(mask, m, 3.1157899f, -3.3241990f, 2.5988452f, -1.2315303f, 3.1821337e-1f, -3.4436006e-2f); |
238 | | return svmla_f32_x(mask, e, p, svsub_f32_x(mask, m, one)); |
239 | | } |
240 | | |
241 | | public: |
242 | | |
243 | | SIMD_INLINE Pow() {} |
244 | | |
245 | | SIMD_INLINE svfloat32_t operator() (const svbool_t& mask, const svfloat32_t& basis, const svfloat32_t& exponent) const |
246 | | { |
247 | | return Exp2(mask, svmul_f32_x(mask, Log2(mask, basis), exponent)); |
248 | | } |
249 | | }; |
250 | | } |
251 | | #endif //SIMD_SVE2_ENABLE |
252 | | |
253 | | #ifdef SIMD_NEON_ENABLE |
254 | | namespace Neon |
255 | | { |
256 | | class Pow |
257 | | { |
258 | | int32x4_t _exponent, _mantissa; |
259 | | float32x4_t _one; |
260 | | |
261 | | SIMD_INLINE float32x4_t Poly5(float32x4_t x, float a, float b, float c, float d, float e, float f) const |
262 | | { |
263 | | float32x4_t p = vdupq_n_f32(f); |
264 | | p = vmlaq_f32(vdupq_n_f32(e), x, p); |
265 | | p = vmlaq_f32(vdupq_n_f32(d), x, p); |
266 | | p = vmlaq_f32(vdupq_n_f32(c), x, p); |
267 | | p = vmlaq_f32(vdupq_n_f32(b), x, p); |
268 | | p = vmlaq_f32(vdupq_n_f32(a), x, p); |
269 | | return p; |
270 | | } |
271 | | |
272 | | SIMD_INLINE float32x4_t Exp2(float32x4_t x) const |
273 | | { |
274 | | x = vmaxq_f32(vminq_f32(x, vdupq_n_f32(129.00000f)), vdupq_n_f32(-126.99999f)); |
275 | | int32x4_t ipart = vcvtq_s32_f32(vsubq_f32(x, vdupq_n_f32(0.5f))); |
276 | | float32x4_t fpart = vsubq_f32(x, vcvtq_f32_s32(ipart)); |
277 | | float32x4_t expipart = vreinterpretq_f32_s32(vshlq_n_s32(vaddq_s32(ipart, vdupq_n_s32(127)), 23)); |
278 | | float32x4_t expfpart = Poly5(fpart, 9.9999994e-1f, 6.9315308e-1f, 2.4015361e-1f, 5.5826318e-2f, 8.9893397e-3f, 1.8775767e-3f); |
279 | | return vmulq_f32(expipart, expfpart); |
280 | | } |
281 | | |
282 | | SIMD_INLINE float32x4_t Log2(float32x4_t x) const |
283 | | { |
284 | | int32x4_t i = vreinterpretq_s32_f32(x); |
285 | | float32x4_t e = vcvtq_f32_s32(vsubq_s32(vshrq_n_s32(vandq_s32(i, _exponent), 23), vdupq_n_s32(127))); |
286 | | float32x4_t m = Or(vreinterpretq_f32_s32(vandq_s32(i, _mantissa)), _one); |
287 | | float32x4_t p = Poly5(m, 3.1157899f, -3.3241990f, 2.5988452f, -1.2315303f, 3.1821337e-1f, -3.4436006e-2f); |
288 | | return vaddq_f32(vmulq_f32(p, vsubq_f32(m, _one)), e); |
289 | | } |
290 | | |
291 | | public: |
292 | | |
293 | | SIMD_INLINE Pow() |
294 | | { |
295 | | _exponent = vdupq_n_s32(0x7F800000); |
296 | | _mantissa = vdupq_n_s32(0x007FFFFF); |
297 | | _one = vdupq_n_f32(1.0f); |
298 | | } |
299 | | |
300 | | SIMD_INLINE float32x4_t operator() (float32x4_t basis, float32x4_t exponent) const |
301 | | { |
302 | | return Exp2(vmulq_f32(Log2(basis), exponent)); |
303 | | } |
304 | | }; |
305 | | } |
306 | | #endif //SIMD_NEON_ENABLE |
307 | | } |
308 | | |
309 | | #endif//__SimdPow_h__ |