Debian Codegraph @

third-party/simde/x86/sse4.2.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 * 2017 Evan Nemerson <[email protected]>
25 * 2020 Hidayat Khan <[email protected]>
26 */
27
28 #if !defined(SIMDE_X86_SSE4_2_H)
29 #define SIMDE_X86_SSE4_2_H
30
31 #include "sse4.1.h"
32
33 #if defined(__ARM_ACLE) || (defined(__GNUC__) && defined(__ARM_FEATURE_CRC32))
34 #include <arm_acle.h>
35 #endif
36
37 HEDLEY_DIAGNOSTIC_PUSH
38 SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
39 SIMDE_BEGIN_DECLS_
40
41 #if defined(SIMDE_X86_SSE4_2_NATIVE)
42 #define SIMDE_SIDD_UBYTE_OPS _SIDD_UBYTE_OPS
43 #define SIMDE_SIDD_UWORD_OPS _SIDD_UWORD_OPS
44 #define SIMDE_SIDD_SBYTE_OPS _SIDD_SBYTE_OPS
45 #define SIMDE_SIDD_SWORD_OPS _SIDD_SWORD_OPS
46 #define SIMDE_SIDD_CMP_EQUAL_ANY _SIDD_CMP_EQUAL_ANY
47 #define SIMDE_SIDD_CMP_RANGES _SIDD_CMP_RANGES
48 #define SIMDE_SIDD_CMP_EQUAL_EACH _SIDD_CMP_EQUAL_EACH
49 #define SIMDE_SIDD_CMP_EQUAL_ORDERED _SIDD_CMP_EQUAL_ORDERED
50 #define SIMDE_SIDD_POSITIVE_POLARITY _SIDD_POSITIVE_POLARITY
51 #define SIMDE_SIDD_NEGATIVE_POLARITY _SIDD_NEGATIVE_POLARITY
52 #define SIMDE_SIDD_MASKED_POSITIVE_POLARITY _SIDD_MASKED_POSITIVE_POLARITY
53 #define SIMDE_SIDD_MASKED_NEGATIVE_POLARITY _SIDD_MASKED_NEGATIVE_POLARITY
54 #define SIMDE_SIDD_LEAST_SIGNIFICANT _SIDD_LEAST_SIGNIFICANT
55 #define SIMDE_SIDD_MOST_SIGNIFICANT _SIDD_MOST_SIGNIFICANT
56 #define SIMDE_SIDD_BIT_MASK _SIDD_BIT_MASK
57 #define SIMDE_SIDD_UNIT_MASK _SIDD_UNIT_MASK
58 #else
59 #define SIMDE_SIDD_UBYTE_OPS 0x00
60 #define SIMDE_SIDD_UWORD_OPS 0x01
61 #define SIMDE_SIDD_SBYTE_OPS 0x02
62 #define SIMDE_SIDD_SWORD_OPS 0x03
63 #define SIMDE_SIDD_CMP_EQUAL_ANY 0x00
64 #define SIMDE_SIDD_CMP_RANGES 0x04
65 #define SIMDE_SIDD_CMP_EQUAL_EACH 0x08
66 #define SIMDE_SIDD_CMP_EQUAL_ORDERED 0x0c
67 #define SIMDE_SIDD_POSITIVE_POLARITY 0x00
68 #define SIMDE_SIDD_NEGATIVE_POLARITY 0x10
69 #define SIMDE_SIDD_MASKED_POSITIVE_POLARITY 0x20
70 #define SIMDE_SIDD_MASKED_NEGATIVE_POLARITY 0x30
71 #define SIMDE_SIDD_LEAST_SIGNIFICANT 0x00
72 #define SIMDE_SIDD_MOST_SIGNIFICANT 0x40
73 #define SIMDE_SIDD_BIT_MASK 0x00
74 #define SIMDE_SIDD_UNIT_MASK 0x40
75 #endif
76
77 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES) && !defined(_SIDD_UBYTE_OPS)
78 #define _SIDD_UBYTE_OPS SIMDE_SIDD_UBYTE_OPS
79 #define _SIDD_UWORD_OPS SIMDE_SIDD_UWORD_OPS
80 #define _SIDD_SBYTE_OPS SIMDE_SIDD_SBYTE_OPS
81 #define _SIDD_SWORD_OPS SIMDE_SIDD_SWORD_OPS
82 #define _SIDD_CMP_EQUAL_ANY SIMDE_SIDD_CMP_EQUAL_ANY
83 #define _SIDD_CMP_RANGES SIMDE_SIDD_CMP_RANGES
84 #define _SIDD_CMP_EQUAL_EACH SIMDE_SIDD_CMP_EQUAL_EACH
85 #define _SIDD_CMP_EQUAL_ORDERED SIMDE_SIDD_CMP_EQUAL_ORDERED
86 #define _SIDD_POSITIVE_POLARITY SIMDE_SIDD_POSITIVE_POLARITY
87 #define _SIDD_NEGATIVE_POLARITY SIMDE_SIDD_NEGATIVE_POLARITY
88 #define _SIDD_MASKED_POSITIVE_POLARITY SIMDE_SIDD_MASKED_POSITIVE_POLARITY
89 #define _SIDD_MASKED_NEGATIVE_POLARITY SIMDE_SIDD_MASKED_NEGATIVE_POLARITY
90 #define _SIDD_LEAST_SIGNIFICANT SIMDE_SIDD_LEAST_SIGNIFICANT
91 #define _SIDD_MOST_SIGNIFICANT SIMDE_SIDD_MOST_SIGNIFICANT
92 #define _SIDD_BIT_MASK SIMDE_SIDD_BIT_MASK
93 #define _SIDD_UNIT_MASK SIMDE_SIDD_UNIT_MASK
94 #endif
95
96 SIMDE_FUNCTION_ATTRIBUTES
97 int simde_mm_cmpestrs (simde__m128i a, int la, simde__m128i b, int lb, const int imm8)
98 SIMDE_REQUIRE_CONSTANT_RANGE(imm8, 0, 255) {
99 #if !defined(HEDLEY_PGI_VERSION)
100 /* https://www.pgroup.com/userforum/viewtopic.php?f=4&p=27590&sid=cf89f8bf30be801831fe4a2ff0a2fa6c */
101 (void) a;
102 (void) b;
103 #endif
104 (void) la;
105 (void) lb;
106 return la <= ((128 / ((imm8 & SIMDE_SIDD_UWORD_OPS) ? 16 : 8)) - 1);
107 }
108 #if defined(SIMDE_X86_SSE4_2_NATIVE)
109 #if defined(__clang__) && !SIMDE_DETECT_CLANG_VERSION_CHECK(3,8,0)
110 #define simde_mm_cmpestrs(a, la, b, lb, imm8) \
111 _mm_cmpestrs( \
112 HEDLEY_REINTERPRET_CAST(__v16qi, a), la, \
113 HEDLEY_REINTERPRET_CAST(__v16qi, b), lb, \
114 imm8)
115 #else
116 #define simde_mm_cmpestrs(a, la, b, lb, imm8) _mm_cmpestrs(a, la, b, lb, imm8)
117 #endif
118 #endif
119 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES)
120 #undef _mm_cmpestrs
121 #define _mm_cmpestrs(a, la, b, lb, imm8) simde_mm_cmpestrs(a, la, b, lb, imm8)
122 #endif
123
124 SIMDE_FUNCTION_ATTRIBUTES
125 int simde_mm_cmpestrz (simde__m128i a, int la, simde__m128i b, int lb, const int imm8)
126 SIMDE_REQUIRE_CONSTANT_RANGE(imm8, 0, 255) {
127 #if !defined(HEDLEY_PGI_VERSION)
128 /* https://www.pgroup.com/userforum/viewtopic.php?f=4&p=27590&sid=cf89f8bf30be801831fe4a2ff0a2fa6c */
129 (void) a;
130 (void) b;
131 #endif
132 (void) la;
133 (void) lb;
134 return lb <= ((128 / ((imm8 & SIMDE_SIDD_UWORD_OPS) ? 16 : 8)) - 1);
135 }
136 #if defined(SIMDE_X86_SSE4_2_NATIVE)
137 #if defined(__clang__) && !SIMDE_DETECT_CLANG_VERSION_CHECK(3,8,0)
138 #define simde_mm_cmpestrz(a, la, b, lb, imm8) \
139 _mm_cmpestrz( \
140 HEDLEY_REINTERPRET_CAST(__v16qi, a), la, \
141 HEDLEY_REINTERPRET_CAST(__v16qi, b), lb, \
142 imm8)
143 #else
144 #define simde_mm_cmpestrz(a, la, b, lb, imm8) _mm_cmpestrz(a, la, b, lb, imm8)
145 #endif
146 #endif
147 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES)
148 #undef _mm_cmpestrz
149 #define _mm_cmpestrz(a, la, b, lb, imm8) simde_mm_cmpestrz(a, la, b, lb, imm8)
150 #endif
151
152 SIMDE_FUNCTION_ATTRIBUTES
153 simde__m128i
154 simde_mm_cmpgt_epi64 (simde__m128i a, simde__m128i b) {
155 #if defined(SIMDE_X86_SSE4_2_NATIVE)
156 return _mm_cmpgt_epi64(a, b);
157 #elif defined(SIMDE_X86_SSE2_NATIVE)
158 /* https://stackoverflow.com/a/65175746/501126 */
159 __m128i r = _mm_and_si128(_mm_cmpeq_epi32(a, b), _mm_sub_epi64(b, a));
160 r = _mm_or_si128(r, _mm_cmpgt_epi32(a, b));
161 return _mm_shuffle_epi32(r, _MM_SHUFFLE(3, 3, 1, 1));
162 #else
163 simde__m128i_private
164 r_,
165 a_ = simde__m128i_to_private(a),
166 b_ = simde__m128i_to_private(b);
167
168 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE)
169 r_.neon_u64 = vcgtq_s64(a_.neon_i64, b_.neon_i64);
170 #elif defined(SIMDE_ARM_NEON_A32V7_NATIVE)
171 /* https://stackoverflow.com/a/65223269/501126 */
172 r_.neon_i64 = vshrq_n_s64(vqsubq_s64(b_.neon_i64, a_.neon_i64), 63);
173 #elif defined(SIMDE_POWER_ALTIVEC_P8_NATIVE)
174 r_.altivec_u64 = HEDLEY_REINTERPRET_CAST(SIMDE_POWER_ALTIVEC_VECTOR(unsigned long long), vec_cmpgt(a_.altivec_i64, b_.altivec_i64));
175 #elif defined(SIMDE_VECTOR_SUBSCRIPT_OPS)
176 r_.i64 = HEDLEY_REINTERPRET_CAST(__typeof__(r_.i64), a_.i64 > b_.i64);
177 #else
178 SIMDE_VECTORIZE
179 for (size_t i = 0 ; i < (sizeof(r_.i64) / sizeof(r_.i64[0])) ; i++) {
180 r_.i64[i] = (a_.i64[i] > b_.i64[i]) ? ~INT64_C(0) : INT64_C(0);
181 }
182 #endif
183
184 return simde__m128i_from_private(r_);
185 #endif
186 }
187 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES)
188 #undef _mm_cmpgt_epi64
189 #define _mm_cmpgt_epi64(a, b) simde_mm_cmpgt_epi64(a, b)
190 #endif
191
192 SIMDE_FUNCTION_ATTRIBUTES
193 int
194 simde_mm_cmpistrs_8_(simde__m128i a) {
195 simde__m128i_private a_= simde__m128i_to_private(a);
196 const int upper_bound = (128 / 8) - 1;
197 int a_invalid = 0;
198 SIMDE_VECTORIZE
199 for (int i = 0 ; i <= upper_bound ; i++) {
200 if(!a_.i8[i])
201 a_invalid = 1;
202 }
203 return a_invalid;
204 }
205
206 SIMDE_FUNCTION_ATTRIBUTES
207 int
208 simde_mm_cmpistrs_16_(simde__m128i a) {
209 simde__m128i_private a_= simde__m128i_to_private(a);
210 const int upper_bound = (128 / 16) - 1;
211 int a_invalid = 0;
212 SIMDE_VECTORIZE
213 for (int i = 0 ; i <= upper_bound ; i++) {
214 if(!a_.i16[i])
215 a_invalid = 1;
216 }
217 return a_invalid;
218 }
219
220 #if defined(SIMDE_X86_SSE4_2_NATIVE)
221 #if defined(__clang__) && !SIMDE_DETECT_CLANG_VERSION_CHECK(3,8,0)
222 #define simde_mm_cmpistrs(a, b, imm8) \
223 _mm_cmpistrs( \
224 HEDLEY_REINTERPRET_CAST(__v16qi, a), \
225 HEDLEY_REINTERPRET_CAST(__v16qi, b), \
226 imm8)
227 #else
228 #define simde_mm_cmpistrs(a, b, imm8) _mm_cmpistrs(a, b, imm8)
229 #endif
230 #else
231 #define simde_mm_cmpistrs(a, b, imm8) \
232 (((imm8) & SIMDE_SIDD_UWORD_OPS) \
233 ? simde_mm_cmpistrs_16_((a)) \
234 : simde_mm_cmpistrs_8_((a)))
235 #endif
236 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES)
237 #undef _mm_cmpistrs
238 #define _mm_cmpistrs(a, b, imm8) simde_mm_cmpistrs(a, b, imm8)
239 #endif
240
241 SIMDE_FUNCTION_ATTRIBUTES
242 int
243 simde_mm_cmpistrz_8_(simde__m128i b) {
244 simde__m128i_private b_= simde__m128i_to_private(b);
245 const int upper_bound = (128 / 8) - 1;
246 int b_invalid = 0;
247 SIMDE_VECTORIZE
248 for (int i = 0 ; i <= upper_bound ; i++) {
249 if(!b_.i8[i])
250 b_invalid = 1;
251 }
252 return b_invalid;
253 }
254
255 SIMDE_FUNCTION_ATTRIBUTES
256 int
257 simde_mm_cmpistrz_16_(simde__m128i b) {
258 simde__m128i_private b_= simde__m128i_to_private(b);
259 const int upper_bound = (128 / 16) - 1;
260 int b_invalid = 0;
261 SIMDE_VECTORIZE
262 for (int i = 0 ; i <= upper_bound ; i++) {
263 if(!b_.i16[i])
264 b_invalid = 1;
265 }
266 return b_invalid;
267 }
268
269 #if defined(SIMDE_X86_SSE4_2_NATIVE)
270 #if defined(__clang__) && !SIMDE_DETECT_CLANG_VERSION_CHECK(3,8,0)
271 #define simde_mm_cmpistrz(a, b, imm8) \
272 _mm_cmpistrz( \
273 HEDLEY_REINTERPRET_CAST(__v16qi, a), \
274 HEDLEY_REINTERPRET_CAST(__v16qi, b), \
275 imm8)
276 #else
277 #define simde_mm_cmpistrz(a, b, imm8) _mm_cmpistrz(a, b, imm8)
278 #endif
279 #else
280 #define simde_mm_cmpistrz(a, b, imm8) \
281 (((imm8) & SIMDE_SIDD_UWORD_OPS) \
282 ? simde_mm_cmpistrz_16_((b)) \
283 : simde_mm_cmpistrz_8_((b)))
284 #endif
285 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES)
286 #undef _mm_cmpistrz
287 #define _mm_cmpistrz(a, b, imm8) simde_mm_cmpistrz(a, b, imm8)
288 #endif
289
290 SIMDE_FUNCTION_ATTRIBUTES
291 uint32_t
292 simde_mm_crc32_u8(uint32_t prevcrc, uint8_t v) {
293 #if defined(SIMDE_X86_SSE4_2_NATIVE)
294 return _mm_crc32_u8(prevcrc, v);
295 #else
296 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE) && defined(__ARM_FEATURE_CRC32)
297 return __crc32cb(prevcrc, v);
298 #else
299 uint32_t crc = prevcrc;
300 crc ^= v;
301 for(int bit = 0 ; bit < 8 ; bit++) {
302 if (crc & 1)
303 crc = (crc >> 1) ^ UINT32_C(0x82f63b78);
304 else
305 crc = (crc >> 1);
306 }
307 return crc;
308 #endif
309 #endif
310 }
311 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES)
312 #define _mm_crc32_u8(prevcrc, v) simde_mm_crc32_u8(prevcrc, v)
313 #endif
314
315 SIMDE_FUNCTION_ATTRIBUTES
316 uint32_t
317 simde_mm_crc32_u16(uint32_t prevcrc, uint16_t v) {
318 #if defined(SIMDE_X86_SSE4_2_NATIVE)
319 return _mm_crc32_u16(prevcrc, v);
320 #else
321 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE) && defined(__ARM_FEATURE_CRC32)
322 return __crc32ch(prevcrc, v);
323 #else
324 uint32_t crc = prevcrc;
325 crc = simde_mm_crc32_u8(crc, v & 0xff);
326 crc = simde_mm_crc32_u8(crc, (v >> 8) & 0xff);
327 return crc;
328 #endif
329 #endif
330 }
331 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES)
332 #define _mm_crc32_u16(prevcrc, v) simde_mm_crc32_u16(prevcrc, v)
333 #endif
334
335 SIMDE_FUNCTION_ATTRIBUTES
336 uint32_t
337 simde_mm_crc32_u32(uint32_t prevcrc, uint32_t v) {
338 #if defined(SIMDE_X86_SSE4_2_NATIVE)
339 return _mm_crc32_u32(prevcrc, v);
340 #else
341 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE) && defined(__ARM_FEATURE_CRC32)
342 return __crc32cw(prevcrc, v);
343 #else
344 uint32_t crc = prevcrc;
345 crc = simde_mm_crc32_u16(crc, v & 0xffff);
346 crc = simde_mm_crc32_u16(crc, (v >> 16) & 0xffff);
347 return crc;
348 #endif
349 #endif
350 }
351 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES)
352 #define _mm_crc32_u32(prevcrc, v) simde_mm_crc32_u32(prevcrc, v)
353 #endif
354
355 SIMDE_FUNCTION_ATTRIBUTES
356 uint64_t
357 simde_mm_crc32_u64(uint64_t prevcrc, uint64_t v) {
358 #if defined(SIMDE_X86_SSE4_2_NATIVE) && defined(SIMDE_ARCH_AMD64)
359 return _mm_crc32_u64(prevcrc, v);
360 #else
361 #if defined(SIMDE_ARM_NEON_A64V8_NATIVE) && defined(__ARM_FEATURE_CRC32)
362 return __crc32cd(HEDLEY_STATIC_CAST(uint32_t, prevcrc), v);
363 #else
364 uint64_t crc = prevcrc;
365 crc = simde_mm_crc32_u32(HEDLEY_STATIC_CAST(uint32_t, crc), v & 0xffffffff);
366 crc = simde_mm_crc32_u32(HEDLEY_STATIC_CAST(uint32_t, crc), (v >> 32) & 0xffffffff);
367 return crc;
368 #endif
369 #endif
370 }
371 #if defined(SIMDE_X86_SSE4_2_ENABLE_NATIVE_ALIASES) || (defined(SIMDE_ENABLE_NATIVE_ALIASES) && !defined(SIMDE_ARCH_AMD64))
372 #define _mm_crc32_u64(prevcrc, v) simde_mm_crc32_u64(prevcrc, v)
373 #endif
374
375 SIMDE_END_DECLS_
376
377 HEDLEY_DIAGNOSTIC_POP
378
379 #endif /* !defined(SIMDE_X86_SSE4_2_H) */