Debian Codegraph @

third-party/simde/arm/neon/recpe.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_RECPE_H)
29 #define SIMDE_ARM_NEON_RECPE_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_vrecpes_f32(simde_float32_t a) {
40 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
41 return vrecpes_f32(a);
42 #else
43 return SIMDE_FLOAT32_C(1.0) / a;
44 #endif
45 }
46 #if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES)
47 #undef vrecpes_f32
48 #define vrecpes_f32(a) simde_vrecpes_f32((a))
49 #endif
50
51 SIMDE_FUNCTION_ATTRIBUTES
52 simde_float64_t
53 simde_vrecped_f64(simde_float64_t a) {
54 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
55 return vrecped_f64(a);
56 #else
57 return SIMDE_FLOAT64_C(1.0) / a;
58 #endif
59 }
60 #if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES)
61 #undef vrecped_f64
62 #define vrecped_f64(a) simde_vrecped_f64((a))
63 #endif
64
65 SIMDE_FUNCTION_ATTRIBUTES
66 simde_float32x2_t
67 simde_vrecpe_f32(simde_float32x2_t a) {
68 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
69 return vrecpe_f32(a);
70 #else
71 simde_float32x2_private
72 r_,
73 a_ = simde_float32x2_to_private(a);
74
75 #if defined(SIMDE_IEEE754_STORAGE)
76 /* https://stackoverflow.com/questions/12227126/division-as-multiply-and-lut-fast-float-division-reciprocal/12228234#12228234 */
77 SIMDE_VECTORIZE
78 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
79 int32_t ix;
80 simde_float32 fx = a_.values[i];
81 simde_memcpy(&ix, &fx, sizeof(ix));
82 int32_t x = INT32_C(0x7EF311C3) - ix;
83 simde_float32 temp;
84 simde_memcpy(&temp, &x, sizeof(temp));
85 r_.values[i] = temp * (SIMDE_FLOAT32_C(2.0) - temp * fx);
86 }
87 #elif defined(SIMDE_VECTOR_SUBSCRIPT_SCALAR)
88 r_.f32 = 1.0f / a_.f32;
89 #else
90 SIMDE_VECTORIZE
91 for (size_t i = 0 ; i < (sizeof(r_.f32) / sizeof(r_.f32[0])) ; i++) {
92 r_.values[i] = simde_vrecpes_f32(a_.values[i]);
93 }
94 #endif
95
96 return simde_float32x2_from_private(r_);
97 #endif
98 }
99 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
100 #undef vrecpe_f32
101 #define vrecpe_f32(a) simde_vrecpe_f32((a))
102 #endif
103
104 SIMDE_FUNCTION_ATTRIBUTES
105 simde_float64x1_t
106 simde_vrecpe_f64(simde_float64x1_t a) {
107 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
108 return vrecpe_f64(a);
109 #else
110 simde_float64x1_private
111 r_,
112 a_ = simde_float64x1_to_private(a);
113
114 #if defined(SIMDE_VECTOR_SUBSCRIPT_SCALAR)
115 r_.values = 1.0 / a_.values;
116 #else
117 SIMDE_VECTORIZE
118 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
119 r_.values[i] = simde_vrecped_f64(a_.values[i]);
120 }
121 #endif
122
123 return simde_float64x1_from_private(r_);
124 #endif
125 }
126 #if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES)
127 #undef vrecpe_f64
128 #define vrecpe_f64(a) simde_vrecpe_f64((a))
129 #endif
130
131 SIMDE_FUNCTION_ATTRIBUTES
132 simde_float64x2_t
133 simde_vrecpeq_f64(simde_float64x2_t a) {
134 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
135 return vrecpeq_f64(a);
136 #else
137 simde_float64x2_private
138 r_,
139 a_ = simde_float64x2_to_private(a);
140
141 #if defined(SIMDE_VECTOR_SUBSCRIPT_SCALAR)
142 r_.values = 1.0 / a_.values;
143 #else
144 SIMDE_VECTORIZE
145 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
146 r_.values[i] = simde_vrecped_f64(a_.values[i]);
147 }
148 #endif
149
150 return simde_float64x2_from_private(r_);
151 #endif
152 }
153 #if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES)
154 #undef vrecpeq_f64
155 #define vrecpeq_f64(a) simde_vrecpeq_f64((a))
156 #endif
157
158 SIMDE_FUNCTION_ATTRIBUTES
159 simde_float32x4_t
160 simde_vrecpeq_f32(simde_float32x4_t a) {
161 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
162 return vrecpeq_f32(a);
163 #elif defined(SIMDE_POWER_ALTIVEC_P6_NATIVE)
164 return vec_re(a);
165 #else
166 simde_float32x4_private
167 r_,
168 a_ = simde_float32x4_to_private(a);
169
170 #if defined(SIMDE_X86_SSE_NATIVE)
171 r_.m128 = _mm_rcp_ps(a_.m128);
172 #elif defined(SIMDE_IEEE754_STORAGE)
173 /* https://stackoverflow.com/questions/12227126/division-as-multiply-and-lut-fast-float-division-reciprocal/12228234#12228234 */
174 SIMDE_VECTORIZE
175 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
176 int32_t ix;
177 simde_float32 fx = a_.values[i];
178 simde_memcpy(&ix, &fx, sizeof(ix));
179 int32_t x = INT32_C(0x7EF311C3) - ix;
180 simde_float32 temp;
181 simde_memcpy(&temp, &x, sizeof(temp));
182 r_.values[i] = temp * (SIMDE_FLOAT32_C(2.0) - temp * fx);
183 }
184 #elif defined(SIMDE_VECTOR_SUBSCRIPT_SCALAR)
185 r_.f32 = 1.0f / a_.f32;
186 #else
187 SIMDE_VECTORIZE
188 for (size_t i = 0 ; i < (sizeof(r_.f32) / sizeof(r_.f32[0])) ; i++) {
189 r_.values[i] = simde_vrecpes_f32(a_.values[i]);
190 }
191 #endif
192
193 return simde_float32x4_from_private(r_);
194 #endif
195 }
196 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
197 #undef vrecpeq_f32
198 #define vrecpeq_f32(a) simde_vrecpeq_f32((a))
199 #endif
200
201 SIMDE_FUNCTION_ATTRIBUTES
202 simde_uint32x2_t
203 simde_vrecpe_u32(simde_uint32x2_t a){
204 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
205 return vrecpe_u32(a);
206 #else
207 simde_uint32x2_private
208 a_ = simde_uint32x2_to_private(a),
209 r_;
210
211 SIMDE_VECTORIZE
212 for(size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
213 if(a_.values[i] <= 0x7FFFFFFF){
214 r_.values[i] = UINT32_MAX;
215 } else {
216 uint32_t a_temp = (a_.values[i] >> 23) & 511;
217 a_temp = a_temp * 2 + 1;
218 uint32_t b = (1 << 19) / a_temp;
219 r_.values[i] = (b+1) / 2;
220 r_.values[i] = r_.values[i] << 23;
221 }
222 }
223
224 return simde_uint32x2_from_private(r_);
225 #endif
226 }
227 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
228 #undef vrecpe_u32
229 #define vrecpe_u32(a) simde_vrecpe_u32((a))
230 #endif
231
232 SIMDE_FUNCTION_ATTRIBUTES
233 simde_uint32x4_t
234 simde_vrecpeq_u32(simde_uint32x4_t a){
235 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
236 return vrecpeq_u32(a);
237 #else
238 simde_uint32x4_private
239 a_ = simde_uint32x4_to_private(a),
240 r_;
241
242 SIMDE_VECTORIZE
243 for(size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
244 if(a_.values[i] <= 0x7FFFFFFF){
245 r_.values[i] = UINT32_MAX;
246 } else {
247 uint32_t a_temp = (a_.values[i] >> 23) & 511;
248 a_temp = a_temp * 2 + 1;
249 uint32_t b = (1 << 19) / a_temp;
250 r_.values[i] = (b+1) / 2;
251 r_.values[i] = r_.values[i] << 23;
252 }
253 }
254
255 return simde_uint32x4_from_private(r_);
256 #endif
257 }
258 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
259 #undef vrecpeq_u32
260 #define vrecpeq_u32(a) simde_vrecpeq_u32((a))
261 #endif
262
263 SIMDE_END_DECLS_
264 HEDLEY_DIAGNOSTIC_POP
265 #endif /* !defined(SIMDE_ARM_NEON_RECPE_H) */