Debian Codegraph @

third-party/simde/arm/neon/hadd.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 */
26
27 /* TODO: the 128-bit versions only require AVX-512 because of the final
28 * conversions from larger types down to smaller ones. We could get
29 * the same results from AVX/AVX2 instructions with some shuffling
30 * to extract the low half of each input element to the low half
31 * of a 256-bit vector, then cast that to a 128-bit vector. */
32
33 #if !defined(SIMDE_ARM_NEON_HADD_H)
34 #define SIMDE_ARM_NEON_HADD_H
35
36 #include "addl.h"
37 #include "shr_n.h"
38 #include "movn.h"
39
40 HEDLEY_DIAGNOSTIC_PUSH
41 SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
42 SIMDE_BEGIN_DECLS_
43
44 SIMDE_FUNCTION_ATTRIBUTES
45 simde_int8x8_t
46 simde_vhadd_s8(simde_int8x8_t a, simde_int8x8_t b) {
47 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
48 return vhadd_s8(a, b);
49 #else
50 return simde_vmovn_s16(simde_vshrq_n_s16(simde_vaddl_s8(a, b), 1));
51 #endif
52 }
53 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
54 #undef vhadd_s8
55 #define vhadd_s8(a, b) simde_vhadd_s8((a), (b))
56 #endif
57
58 SIMDE_FUNCTION_ATTRIBUTES
59 simde_int16x4_t
60 simde_vhadd_s16(simde_int16x4_t a, simde_int16x4_t b) {
61 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
62 return vhadd_s16(a, b);
63 #else
64 return simde_vmovn_s32(simde_vshrq_n_s32(simde_vaddl_s16(a, b), 1));
65 #endif
66 }
67 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
68 #undef vhadd_s16
69 #define vhadd_s16(a, b) simde_vhadd_s16((a), (b))
70 #endif
71
72 SIMDE_FUNCTION_ATTRIBUTES
73 simde_int32x2_t
74 simde_vhadd_s32(simde_int32x2_t a, simde_int32x2_t b) {
75 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
76 return vhadd_s32(a, b);
77 #else
78 return simde_vmovn_s64(simde_vshrq_n_s64(simde_vaddl_s32(a, b), 1));
79 #endif
80 }
81 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
82 #undef vhadd_s32
83 #define vhadd_s32(a, b) simde_vhadd_s32((a), (b))
84 #endif
85
86 SIMDE_FUNCTION_ATTRIBUTES
87 simde_uint8x8_t
88 simde_vhadd_u8(simde_uint8x8_t a, simde_uint8x8_t b) {
89 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
90 return vhadd_u8(a, b);
91 #else
92 return simde_vmovn_u16(simde_vshrq_n_u16(simde_vaddl_u8(a, b), 1));
93 #endif
94 }
95 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
96 #undef vhadd_u8
97 #define vhadd_u8(a, b) simde_vhadd_u8((a), (b))
98 #endif
99
100 SIMDE_FUNCTION_ATTRIBUTES
101 simde_uint16x4_t
102 simde_vhadd_u16(simde_uint16x4_t a, simde_uint16x4_t b) {
103 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
104 return vhadd_u16(a, b);
105 #else
106 return simde_vmovn_u32(simde_vshrq_n_u32(simde_vaddl_u16(a, b), 1));
107 #endif
108 }
109 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
110 #undef vhadd_u16
111 #define vhadd_u16(a, b) simde_vhadd_u16((a), (b))
112 #endif
113
114 SIMDE_FUNCTION_ATTRIBUTES
115 simde_uint32x2_t
116 simde_vhadd_u32(simde_uint32x2_t a, simde_uint32x2_t b) {
117 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
118 return vhadd_u32(a, b);
119 #else
120 return simde_vmovn_u64(simde_vshrq_n_u64(simde_vaddl_u32(a, b), 1));
121 #endif
122 }
123 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
124 #undef vhadd_u32
125 #define vhadd_u32(a, b) simde_vhadd_u32((a), (b))
126 #endif
127
128 SIMDE_FUNCTION_ATTRIBUTES
129 simde_int8x16_t
130 simde_vhaddq_s8(simde_int8x16_t a, simde_int8x16_t b) {
131 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
132 return vhaddq_s8(a, b);
133 #else
134 simde_int8x16_private
135 r_,
136 a_ = simde_int8x16_to_private(a),
137 b_ = simde_int8x16_to_private(b);
138
139 #if defined(SIMDE_X86_AVX512VL_NATIVE) && defined(SIMDE_X86_AVX512BW_NATIVE)
140 r_.m128i = _mm256_cvtepi16_epi8(_mm256_srai_epi16(_mm256_add_epi16(_mm256_cvtepi8_epi16(a_.m128i), _mm256_cvtepi8_epi16(b_.m128i)), 1));
141 #else
142 SIMDE_VECTORIZE
143 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
144 r_.values[i] = HEDLEY_STATIC_CAST(int8_t, (HEDLEY_STATIC_CAST(int16_t, a_.values[i]) + HEDLEY_STATIC_CAST(int16_t, b_.values[i])) >> 1);
145 }
146 #endif
147
148 return simde_int8x16_from_private(r_);
149 #endif
150 }
151 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
152 #undef vhaddq_s8
153 #define vhaddq_s8(a, b) simde_vhaddq_s8((a), (b))
154 #endif
155
156 SIMDE_FUNCTION_ATTRIBUTES
157 simde_int16x8_t
158 simde_vhaddq_s16(simde_int16x8_t a, simde_int16x8_t b) {
159 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
160 return vhaddq_s16(a, b);
161 #else
162 simde_int16x8_private
163 r_,
164 a_ = simde_int16x8_to_private(a),
165 b_ = simde_int16x8_to_private(b);
166
167 #if defined(SIMDE_X86_AVX512VL_NATIVE)
168 r_.m128i = _mm256_cvtepi32_epi16(_mm256_srai_epi32(_mm256_add_epi32(_mm256_cvtepi16_epi32(a_.m128i), _mm256_cvtepi16_epi32(b_.m128i)), 1));
169 #else
170 SIMDE_VECTORIZE
171 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
172 r_.values[i] = HEDLEY_STATIC_CAST(int16_t, (HEDLEY_STATIC_CAST(int32_t, a_.values[i]) + HEDLEY_STATIC_CAST(int32_t, b_.values[i])) >> 1);
173 }
174 #endif
175
176 return simde_int16x8_from_private(r_);
177 #endif
178 }
179 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
180 #undef vhaddq_s16
181 #define vhaddq_s16(a, b) simde_vhaddq_s16((a), (b))
182 #endif
183
184 SIMDE_FUNCTION_ATTRIBUTES
185 simde_int32x4_t
186 simde_vhaddq_s32(simde_int32x4_t a, simde_int32x4_t b) {
187 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
188 return vhaddq_s32(a, b);
189 #else
190 simde_int32x4_private
191 r_,
192 a_ = simde_int32x4_to_private(a),
193 b_ = simde_int32x4_to_private(b);
194
195 #if defined(SIMDE_X86_AVX512VL_NATIVE)
196 r_.m128i = _mm256_cvtepi64_epi32(_mm256_srai_epi64(_mm256_add_epi64(_mm256_cvtepi32_epi64(a_.m128i), _mm256_cvtepi32_epi64(b_.m128i)), 1));
197 #else
198 SIMDE_VECTORIZE
199 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
200 r_.values[i] = HEDLEY_STATIC_CAST(int32_t, (HEDLEY_STATIC_CAST(int64_t, a_.values[i]) + HEDLEY_STATIC_CAST(int64_t, b_.values[i])) >> 1);
201 }
202 #endif
203
204 return simde_int32x4_from_private(r_);
205 #endif
206 }
207 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
208 #undef vhaddq_s32
209 #define vhaddq_s32(a, b) simde_vhaddq_s32((a), (b))
210 #endif
211
212 SIMDE_FUNCTION_ATTRIBUTES
213 simde_uint8x16_t
214 simde_vhaddq_u8(simde_uint8x16_t a, simde_uint8x16_t b) {
215 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
216 return vhaddq_u8(a, b);
217 #else
218 simde_uint8x16_private
219 r_,
220 a_ = simde_uint8x16_to_private(a),
221 b_ = simde_uint8x16_to_private(b);
222
223 #if defined(SIMDE_X86_AVX512VL_NATIVE) && defined(SIMDE_X86_AVX512BW_NATIVE)
224 r_.m128i = _mm256_cvtepi16_epi8(_mm256_srli_epi16(_mm256_add_epi16(_mm256_cvtepu8_epi16(a_.m128i), _mm256_cvtepu8_epi16(b_.m128i)), 1));
225 #elif defined(SIMDE_WASM_SIMD128_NATIVE)
226 v128_t lo =
227 wasm_u16x8_shr(wasm_i16x8_add(wasm_u16x8_extend_low_u8x16(a_.v128),
228 wasm_u16x8_extend_low_u8x16(b_.v128)),
229 1);
230 v128_t hi =
231 wasm_u16x8_shr(wasm_i16x8_add(wasm_u16x8_extend_high_u8x16(a_.v128),
232 wasm_u16x8_extend_high_u8x16(b_.v128)),
233 1);
234 r_.v128 = wasm_i8x16_shuffle(lo, hi, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
235 22, 24, 26, 28, 30);
236 #else
237 SIMDE_VECTORIZE
238 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
239 r_.values[i] = HEDLEY_STATIC_CAST(uint8_t, (HEDLEY_STATIC_CAST(uint16_t, a_.values[i]) + HEDLEY_STATIC_CAST(uint16_t, b_.values[i])) >> 1);
240 }
241 #endif
242
243 return simde_uint8x16_from_private(r_);
244 #endif
245 }
246 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
247 #undef vhaddq_u8
248 #define vhaddq_u8(a, b) simde_vhaddq_u8((a), (b))
249 #endif
250
251 SIMDE_FUNCTION_ATTRIBUTES
252 simde_uint16x8_t
253 simde_vhaddq_u16(simde_uint16x8_t a, simde_uint16x8_t b) {
254 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
255 return vhaddq_u16(a, b);
256 #else
257 simde_uint16x8_private
258 r_,
259 a_ = simde_uint16x8_to_private(a),
260 b_ = simde_uint16x8_to_private(b);
261
262 #if defined(SIMDE_X86_AVX512VL_NATIVE)
263 r_.m128i = _mm256_cvtepi32_epi16(_mm256_srli_epi32(_mm256_add_epi32(_mm256_cvtepu16_epi32(a_.m128i), _mm256_cvtepu16_epi32(b_.m128i)), 1));
264 #else
265 SIMDE_VECTORIZE
266 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
267 r_.values[i] = HEDLEY_STATIC_CAST(uint16_t, (HEDLEY_STATIC_CAST(uint32_t, a_.values[i]) + HEDLEY_STATIC_CAST(uint32_t, b_.values[i])) >> 1);
268 }
269 #endif
270
271 return simde_uint16x8_from_private(r_);
272 #endif
273 }
274 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
275 #undef vhaddq_u16
276 #define vhaddq_u16(a, b) simde_vhaddq_u16((a), (b))
277 #endif
278
279 SIMDE_FUNCTION_ATTRIBUTES
280 simde_uint32x4_t
281 simde_vhaddq_u32(simde_uint32x4_t a, simde_uint32x4_t b) {
282 #if defined(SIMDE_ARM_NEON_A32V7_NATIVE)
283 return vhaddq_u32(a, b);
284 #else
285 simde_uint32x4_private
286 r_,
287 a_ = simde_uint32x4_to_private(a),
288 b_ = simde_uint32x4_to_private(b);
289
290 #if defined(SIMDE_X86_AVX512VL_NATIVE)
291 r_.m128i = _mm256_cvtepi64_epi32(_mm256_srli_epi64(_mm256_add_epi64(_mm256_cvtepu32_epi64(a_.m128i), _mm256_cvtepu32_epi64(b_.m128i)), 1));
292 #else
293 SIMDE_VECTORIZE
294 for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) {
295 r_.values[i] = HEDLEY_STATIC_CAST(uint32_t, (HEDLEY_STATIC_CAST(uint64_t, a_.values[i]) + HEDLEY_STATIC_CAST(uint64_t, b_.values[i])) >> 1);
296 }
297 #endif
298
299 return simde_uint32x4_from_private(r_);
300 #endif
301 }
302 #if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES)
303 #undef vhaddq_u32
304 #define vhaddq_u32(a, b) simde_vhaddq_u32((a), (b))
305 #endif
306
307 SIMDE_END_DECLS_
308 HEDLEY_DIAGNOSTIC_POP
309
310 #endif /* !defined(SIMDE_ARM_NEON_HADD_H) */