Debian Codegraph @

third-party/simde/arm/neon/rsqrte.h

raw
1 /* SPDX-License-Identifier: MIT
2 *
3 * Permission is hereby granted, free of charge, to any person
4 * obtaining a copy of this software and associated documentation
5 * files (the "Software"), to deal in the Software without
6 * restriction, including without limitation the rights to use, copy,
7 * modify, merge, publish, distribute, sublicense, and/or sell copies
8 * of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Copyright:
24 * 2020 Evan Nemerson <[email protected]>
25 * 2021 Zhi An Ng <[email protected]> (Copyright owned by Google, LLC)
26 */
27
28 #if !defined(SIMDE_ARM_NEON_RSQRTE_H)
29 #define SIMDE_ARM_NEON_RSQRTE_H
30
31 #include "types.h"
32
33 HEDLEY_DIAGNOSTIC_PUSH
34 SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
35 SIMDE_BEGIN_DECLS_
36
37 SIMDE_FUNCTION_ATTRIBUTES
38 simde_float32_t
39 simde_vrsqrtes_f32(simde_float32_t a) {
40 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
41 return vrsqrtes_f32(a);
42 #else
43 #if defined(SIMDE_IEEE754_STORAGE)
44 /* https://basesandframes.files.wordpress.com/2020/04/even_faster_math_functions_green_2020.pdf
45 Pages 100 - 103 */
46 #if SIMDE_ACCURACY_PREFERENCE <= 0
47 return (INT32_C(0x5F37624F) - (a >> 1));
48 #else
49 simde_float32 x = a;
50 simde_float32 xhalf = SIMDE_FLOAT32_C(0.5) * x;
51 int32_t ix;
52
53 simde_memcpy(&ix, &x, sizeof(ix));
54
55 #if SIMDE_ACCURACY_PREFERENCE == 1
56 ix = INT32_C(0x5F375A82) - (ix >> 1);
57 #else
58 ix = INT32_C(0x5F37599E) - (ix >> 1);
59 #endif
60
61 simde_memcpy(&x, &ix, sizeof(x));
62
63 #if SIMDE_ACCURACY_PREFERENCE >= 2
64 x = x * (SIMDE_FLOAT32_C(1.5008909) - xhalf * x * x);
65 #endif
66 x = x * (SIMDE_FLOAT32_C(1.5008909) - xhalf * x * x);
67 return x;
68 #endif
69 #elif defined(simde_math_sqrtf)
70 return 1.0f / simde_math_sqrtf(a);
71 #else
72 HEDLEY_UNREACHABLE();
73 #endif
74 #endif
75 }
76 #if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES)
77 #undef vrsqrtes_f32
78 #define vrsqrtes_f32(a) simde_vrsqrtes_f32((a))
79 #endif
80
81 SIMDE_FUNCTION_ATTRIBUTES
82 simde_float64_t
83 simde_vrsqrted_f64(simde_float64_t a) {
84 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
85 return vrsqrted_f64(a);
86 #else
87 #if defined(SIMDE_IEEE754_STORAGE)
88 //https://www.mdpi.com/1099-4300/23/1/86/htm
89 simde_float64_t x = a;
90 simde_float64_t xhalf = SIMDE_FLOAT64_C(0.5) * x;
91 int64_t ix;
92
93 simde_memcpy(&ix, &x, sizeof(ix));
94 ix = INT64_C(0x5FE6ED2102DCBFDA) - (ix >> 1);
95 simde_memcpy(&x, &ix, sizeof(x));
96 x = x * (SIMDE_FLOAT64_C(1.50087895511633457) - xhalf * x * x);
97 x = x * (SIMDE_FLOAT64_C(1.50000057967625766) - xhalf * x * x);
98 return x;
99 #elif defined(simde_math_sqrtf)
100 return SIMDE_FLOAT64_C(1.0) / simde_math_sqrt(a_.values[i]);
101 #else
102 HEDLEY_UNREACHABLE();
103 #endif
104 #endif
105 }
106 #if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES)
107 #undef vrsqrted_f64
108 #define vrsqrted_f64(a) simde_vrsqrted_f64((a))
109 #endif
110
111 SIMDE_FUNCTION_ATTRIBUTES
112 simde_uint32x2_t
113 simde_vrsqrte_u32(simde_uint32x2_t a) {
114 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
115 return vrsqrte_u32(a);
116 #else
117 simde_uint32x2_private
118 a_ = simde_uint32x2_to_private(a),
119 r_;
120
121 for(size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[i])) ; i++) {
122 if(a_.values[i] < 0x3FFFFFFF) {
123 r_.values[i] = UINT32_MAX;
124 } else {
125 uint32_t a_temp = (a_.values[i] >> 23) & 511;
126 if(a_temp < 256) {
127 a_temp = a_temp * 2 + 1;
128 } else {
129 a_temp = (a_temp >> 1) << 1;
130 a_temp = (a_temp + 1) * 2;
131 }
132 uint32_t b = 512;
133 while((a_temp * (b + 1) * (b + 1)) < (1 << 28))
134 b = b + 1;
135 r_.values[i] = (b + 1) / 2;
136 r_.values[i] = r_.values[i] << 23;
137 }
138 }
139 return simde_uint32x2_from_private(r_);
140 #endif
141 }
142 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
143 #undef vrsqrte_u32
144 #define vrsqrte_u32(a) simde_vrsqrte_u32((a))
145 #endif
146
147 SIMDE_FUNCTION_ATTRIBUTES
148 simde_float32x2_t
149 simde_vrsqrte_f32(simde_float32x2_t a) {
150 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
151 return vrsqrte_f32(a);
152 #else
153 simde_float32x2_private
154 r_,
155 a_ = simde_float32x2_to_private(a);
156
157 #if defined(SIMDE_IEEE754_STORAGE)
158 /* https://basesandframes.files.wordpress.com/2020/04/even_faster_math_functions_green_2020.pdf
159 Pages 100 - 103 */
160 SIMDE_VECTORIZE
161 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
162 #if SIMDE_ACCURACY_PREFERENCE <= 0
163 r_.i32[i] = INT32_C(0x5F37624F) - (a_.i32[i] >> 1);
164 #else
165 simde_float32 x = a_.values[i];
166 simde_float32 xhalf = SIMDE_FLOAT32_C(0.5) * x;
167 int32_t ix;
168
169 simde_memcpy(&ix, &x, sizeof(ix));
170
171 #if SIMDE_ACCURACY_PREFERENCE == 1
172 ix = INT32_C(0x5F375A82) - (ix >> 1);
173 #else
174 ix = INT32_C(0x5F37599E) - (ix >> 1);
175 #endif
176
177 simde_memcpy(&x, &ix, sizeof(x));
178
179 #if SIMDE_ACCURACY_PREFERENCE >= 2
180 x = x * (SIMDE_FLOAT32_C(1.5008909) - xhalf * x * x);
181 #endif
182 x = x * (SIMDE_FLOAT32_C(1.5008909) - xhalf * x * x);
183
184 r_.values[i] = x;
185 #endif
186 }
187 #elif defined(simde_math_sqrtf)
188 SIMDE_VECTORIZE
189 for (size_t i = 0 ; i < (sizeof(r_.f32) / sizeof(r_.f32[0])) ; i++) {
190 r_.f32[i] = 1.0f / simde_math_sqrtf(a_.f32[i]);
191 }
192 #else
193 HEDLEY_UNREACHABLE();
194 #endif
195
196 return simde_float32x2_from_private(r_);
197 #endif
198 }
199 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
200 #undef vrsqrte_f32
201 #define vrsqrte_f32(a) simde_vrsqrte_f32((a))
202 #endif
203
204 SIMDE_FUNCTION_ATTRIBUTES
205 simde_float64x1_t
206 simde_vrsqrte_f64(simde_float64x1_t a) {
207 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
208 return vrsqrte_f64(a);
209 #else
210 simde_float64x1_private
211 r_,
212 a_ = simde_float64x1_to_private(a);
213
214 #if defined(SIMDE_IEEE754_STORAGE)
215 //https://www.mdpi.com/1099-4300/23/1/86/htm
216 SIMDE_VECTORIZE
217 for(size_t i = 0 ; i < (sizeof(r_.values)/sizeof(r_.values[0])) ; i++) {
218 simde_float64_t x = a_.values[i];
219 simde_float64_t xhalf = SIMDE_FLOAT64_C(0.5) * x;
220 int64_t ix;
221
222 simde_memcpy(&ix, &x, sizeof(ix));
223 ix = INT64_C(0x5FE6ED2102DCBFDA) - (ix >> 1);
224 simde_memcpy(&x, &ix, sizeof(x));
225 x = x * (SIMDE_FLOAT64_C(1.50087895511633457) - xhalf * x * x);
226 x = x * (SIMDE_FLOAT64_C(1.50000057967625766) - xhalf * x * x);
227 r_.values[i] = x;
228 }
229 #elif defined(simde_math_sqrtf)
230 SIMDE_VECTORIZE
231 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
232 r_.values[i] = SIMDE_FLOAT64_C(1.0) / simde_math_sqrt(a_.values[i]);
233 }
234 #else
235 HEDLEY_UNREACHABLE();
236 #endif
237
238 return simde_float64x1_from_private(r_);
239 #endif
240 }
241 #if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES)
242 #undef vrsqrte_f64
243 #define vrsqrte_f64(a) simde_vrsqrte_f64((a))
244 #endif
245
246 SIMDE_FUNCTION_ATTRIBUTES
247 simde_uint32x4_t
248 simde_vrsqrteq_u32(simde_uint32x4_t a) {
249 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
250 return vrsqrteq_u32(a);
251 #else
252 simde_uint32x4_private
253 a_ = simde_uint32x4_to_private(a),
254 r_;
255
256 for(size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[i])) ; i++) {
257 if(a_.values[i] < 0x3FFFFFFF) {
258 r_.values[i] = UINT32_MAX;
259 } else {
260 uint32_t a_temp = (a_.values[i] >> 23) & 511;
261 if(a_temp < 256) {
262 a_temp = a_temp * 2 + 1;
263 } else {
264 a_temp = (a_temp >> 1) << 1;
265 a_temp = (a_temp + 1) * 2;
266 }
267 uint32_t b = 512;
268 while((a_temp * (b + 1) * (b + 1)) < (1 << 28))
269 b = b + 1;
270 r_.values[i] = (b + 1) / 2;
271 r_.values[i] = r_.values[i] << 23;
272 }
273 }
274 return simde_uint32x4_from_private(r_);
275 #endif
276 }
277 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
278 #undef vrsqrteq_u32
279 #define vrsqrteq_u32(a) simde_vrsqrteq_u32((a))
280 #endif
281
282 SIMDE_FUNCTION_ATTRIBUTES
283 simde_float32x4_t
284 simde_vrsqrteq_f32(simde_float32x4_t a) {
285 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
286 return vrsqrteq_f32(a);
287 #elif defined(SIMDE_POWER_ALTIVEC_P6_NATIVE)
288 return vec_rsqrte(a);
289 #else
290 simde_float32x4_private
291 r_,
292 a_ = simde_float32x4_to_private(a);
293
294 #if defined(SIMDE_X86_SSE_NATIVE)
295 r_.m128 = _mm_rsqrt_ps(a_.m128);
296 #elif defined(SIMDE_IEEE754_STORAGE)
297 /* https://basesandframes.files.wordpress.com/2020/04/even_faster_math_functions_green_2020.pdf
298 Pages 100 - 103 */
299 SIMDE_VECTORIZE
300 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
301 #if SIMDE_ACCURACY_PREFERENCE <= 0
302 r_.i32[i] = INT32_C(0x5F37624F) - (a_.i32[i] >> 1);
303 #else
304 simde_float32 x = a_.values[i];
305 simde_float32 xhalf = SIMDE_FLOAT32_C(0.5) * x;
306 int32_t ix;
307
308 simde_memcpy(&ix, &x, sizeof(ix));
309
310 #if SIMDE_ACCURACY_PREFERENCE == 1
311 ix = INT32_C(0x5F375A82) - (ix >> 1);
312 #else
313 ix = INT32_C(0x5F37599E) - (ix >> 1);
314 #endif
315
316 simde_memcpy(&x, &ix, sizeof(x));
317
318 #if SIMDE_ACCURACY_PREFERENCE >= 2
319 x = x * (SIMDE_FLOAT32_C(1.5008909) - xhalf * x * x);
320 #endif
321 x = x * (SIMDE_FLOAT32_C(1.5008909) - xhalf * x * x);
322
323 r_.values[i] = x;
324 #endif
325 }
326 #elif defined(simde_math_sqrtf)
327 SIMDE_VECTORIZE
328 for (size_t i = 0 ; i < (sizeof(r_.f32) / sizeof(r_.f32[0])) ; i++) {
329 r_.f32[i] = 1.0f / simde_math_sqrtf(a_.f32[i]);
330 }
331 #else
332 HEDLEY_UNREACHABLE();
333 #endif
334
335 return simde_float32x4_from_private(r_);
336 #endif
337 }
338 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
339 #undef vrsqrteq_f32
340 #define vrsqrteq_f32(a) simde_vrsqrteq_f32((a))
341 #endif
342
343 SIMDE_FUNCTION_ATTRIBUTES
344 simde_float64x2_t
345 simde_vrsqrteq_f64(simde_float64x2_t a) {
346 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
347 return vrsqrteq_f64(a);
348 #else
349 simde_float64x2_private
350 r_,
351 a_ = simde_float64x2_to_private(a);
352
353 #if defined(SIMDE_IEEE754_STORAGE)
354 //https://www.mdpi.com/1099-4300/23/1/86/htm
355 SIMDE_VECTORIZE
356 for(size_t i = 0 ; i < (sizeof(r_.values)/sizeof(r_.values[0])) ; i++) {
357 simde_float64_t x = a_.values[i];
358 simde_float64_t xhalf = SIMDE_FLOAT64_C(0.5) * x;
359 int64_t ix;
360
361 simde_memcpy(&ix, &x, sizeof(ix));
362 ix = INT64_C(0x5FE6ED2102DCBFDA) - (ix >> 1);
363 simde_memcpy(&x, &ix, sizeof(x));
364 x = x * (SIMDE_FLOAT64_C(1.50087895511633457) - xhalf * x * x);
365 x = x * (SIMDE_FLOAT64_C(1.50000057967625766) - xhalf * x * x);
366 r_.values[i] = x;
367 }
368 #elif defined(simde_math_sqrtf)
369 SIMDE_VECTORIZE
370 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
371 r_.values[i] = SIMDE_FLOAT64_C(1.0) / simde_math_sqrt(a_.values[i]);
372 }
373 #else
374 HEDLEY_UNREACHABLE();
375 #endif
376
377 return simde_float64x2_from_private(r_);
378 #endif
379 }
380 #if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES)
381 #undef vrsqrteq_f64
382 #define vrsqrteq_f64(a) simde_vrsqrteq_f64((a))
383 #endif
384
385 SIMDE_END_DECLS_
386 HEDLEY_DIAGNOSTIC_POP
387 #endif /* !defined(SIMDE_ARM_NEON_RSQRTE_H) */