Debian Codegraph @

third-party/simde/x86/avx512/lzcnt.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 #if !defined(SIMDE_X86_AVX512_LZCNT_H)
28 #define SIMDE_X86_AVX512_LZCNT_H
29
30 #include "types.h"
31 #include "mov.h"
32 #if HEDLEY_MSVC_VERSION_CHECK(14,0,0)
33 #include <intrin.h>
34 #pragma intrinsic(_BitScanReverse)
35 #if defined(_M_AMD64) || defined(_M_ARM64)
36 #pragma intrinsic(_BitScanReverse64)
37 #endif
38 #endif
39
40 HEDLEY_DIAGNOSTIC_PUSH
41 SIMDE_DISABLE_UNWANTED_DIAGNOSTICS
42 SIMDE_BEGIN_DECLS_
43
44 #if \
45 ( HEDLEY_HAS_BUILTIN(__builtin_clz) || \
46 HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
47 HEDLEY_ARM_VERSION_CHECK(4,1,0) ) && \
48 defined(__INT_MAX__) && defined(__LONG_MAX__) && defined(__LONG_LONG_MAX__) && \
49 defined(__INT32_MAX__) && defined(__INT64_MAX__)
50 #if __INT_MAX__ == __INT32_MAX__
51 #define simde_x_clz32(v) __builtin_clz(HEDLEY_STATIC_CAST(unsigned int, (v)))
52 #elif __LONG_MAX__ == __INT32_MAX__
53 #define simde_x_clz32(v) __builtin_clzl(HEDLEY_STATIC_CAST(unsigned long, (v)))
54 #elif __LONG_LONG_MAX__ == __INT32_MAX__
55 #define simde_x_clz32(v) __builtin_clzll(HEDLEY_STATIC_CAST(unsigned long long, (v)))
56 #endif
57
58 #if __INT_MAX__ == __INT64_MAX__
59 #define simde_x_clz64(v) __builtin_clz(HEDLEY_STATIC_CAST(unsigned int, (v)))
60 #elif __LONG_MAX__ == __INT64_MAX__
61 #define simde_x_clz64(v) __builtin_clzl(HEDLEY_STATIC_CAST(unsigned long, (v)))
62 #elif __LONG_LONG_MAX__ == __INT64_MAX__
63 #define simde_x_clz64(v) __builtin_clzll(HEDLEY_STATIC_CAST(unsigned long long, (v)))
64 #endif
65 #elif HEDLEY_MSVC_VERSION_CHECK(14,0,0)
66 static int simde_x_clz32(uint32_t x) {
67 unsigned long r;
68 _BitScanReverse(&r, x);
69 return 31 - HEDLEY_STATIC_CAST(int, r);
70 }
71 #define simde_x_clz32 simde_x_clz32
72
73 static int simde_x_clz64(uint64_t x) {
74 unsigned long r;
75
76 #if defined(_M_AMD64) || defined(_M_ARM64)
77 _BitScanReverse64(&r, x);
78 return 63 - HEDLEY_STATIC_CAST(int, r);
79 #else
80 uint32_t high = HEDLEY_STATIC_CAST(uint32_t, x >> 32);
81 if (high != 0)
82 return _BitScanReverse(&r, HEDLEY_STATIC_CAST(unsigned long, high));
83 else
84 return _BitScanReverse(&r, HEDLEY_STATIC_CAST(unsigned long, x & ~UINT32_C(0))) + 32;
85 #endif
86 }
87 #define simde_x_clz64 simde_x_clz64
88 #endif
89
90 #if !defined(simde_x_clz32) || !defined(simde_x_clz64)
91 static uint8_t simde_x_avx512cd_lz_lookup(const uint8_t value) {
92 static const uint8_t lut[256] = {
93 7, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
94 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
95 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
96 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
97 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
98 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
99 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
100 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
109 };
110 return lut[value];
111 };
112
113 #if !defined(simde_x_clz32)
114 static int simde_x_clz32(uint32_t x) {
115 size_t s = sizeof(x) * 8;
116 uint32_t r;
117
118 while ((s -= 8) != 0) {
119 r = x >> s;
120 if (r != 0)
121 return simde_x_avx512cd_lz_lookup(HEDLEY_STATIC_CAST(uint8_t, r)) +
122 (((sizeof(x) - 1) * 8) - s);
123 }
124
125 if (x == 0)
126 return (int) ((sizeof(x) * 8) - 1);
127 else
128 return simde_x_avx512cd_lz_lookup(HEDLEY_STATIC_CAST(uint8_t, x)) +
129 ((sizeof(x) - 1) * 8);
130 }
131 #endif
132
133 #if !defined(simde_x_clz64)
134 static int simde_x_clz64(uint64_t x) {
135 size_t s = sizeof(x) * 8;
136 uint64_t r;
137
138 while ((s -= 8) != 0) {
139 r = x >> s;
140 if (r != 0)
141 return simde_x_avx512cd_lz_lookup(HEDLEY_STATIC_CAST(uint8_t, r)) +
142 (((sizeof(x) - 1) * 8) - s);
143 }
144
145 if (x == 0)
146 return (int) ((sizeof(x) * 8) - 1);
147 else
148 return simde_x_avx512cd_lz_lookup(HEDLEY_STATIC_CAST(uint8_t, x)) +
149 ((sizeof(x) - 1) * 8);
150 }
151 #endif
152 #endif
153
154 SIMDE_FUNCTION_ATTRIBUTES
155 simde__m128i
156 simde_mm_lzcnt_epi32(simde__m128i a) {
157 #if defined(SIMDE_X86_AVX512VL_NATIVE) && defined(SIMDE_X86_AVX512CD_NATIVE)
158 return _mm_lzcnt_epi32(a);
159 #elif defined(SIMDE_X86_SSE2_NATIVE)
160 /* https://stackoverflow.com/a/58827596/501126 */
161 a = _mm_andnot_si128(_mm_srli_epi32(a, 8), a);
162 a = _mm_castps_si128(_mm_cvtepi32_ps(a));
163 a = _mm_srli_epi32(a, 23);
164 a = _mm_subs_epu16(_mm_set1_epi32(158), a);
165 a = _mm_min_epi16(a, _mm_set1_epi32(32));
166 return a;
167 #else
168 simde__m128i_private
169 r_,
170 a_ = simde__m128i_to_private(a);
171
172 #if defined(SIMDE_POWER_ALTIVEC_P8_NATIVE) || defined(SIMDE_ZARCH_ZVECTOR_13_NATIVE)
173 r_.altivec_u32 = vec_cntlz(a_.altivec_u32);
174 #else
175 SIMDE_VECTORIZE
176 for (size_t i = 0; i < (sizeof(r_.i32) / sizeof(r_.i32[0])); i++) {
177 r_.i32[i] = (HEDLEY_UNLIKELY(a_.i32[i] == 0) ? HEDLEY_STATIC_CAST(int32_t, sizeof(int32_t) * CHAR_BIT) : HEDLEY_STATIC_CAST(int32_t, simde_x_clz32(HEDLEY_STATIC_CAST(uint32_t, a_.i32[i]))));
178 }
179 #endif
180
181 return simde__m128i_from_private(r_);
182 #endif
183 }
184 #if defined(SIMDE_X86_AVX512VL_ENABLE_NATIVE_ALIASES) && defined(SIMDE_X86_AVX512CD_ENABLE_NATIVE_ALIASES)
185 #undef _mm_lzcnt_epi32
186 #define _mm_lzcnt_epi32(a) simde_mm_lzcnt_epi32(a)
187 #endif
188
189 SIMDE_FUNCTION_ATTRIBUTES
190 simde__m128i
191 simde_mm_mask_lzcnt_epi32(simde__m128i src, simde__mmask8 k, simde__m128i a) {
192 #if defined(SIMDE_X86_AVX512VL_NATIVE) && defined(SIMDE_X86_AVX512CD_NATIVE)
193 return _mm_mask_lzcnt_epi32(src, k, a);
194 #else
195 return simde_mm_mask_mov_epi32(src, k, simde_mm_lzcnt_epi32(a));
196 #endif
197 }
198 #if defined(SIMDE_X86_AVX512F_ENABLE_NATIVE_ALIASES)
199 #undef _mm_mask_lzcnt_epi32
200 #define _mm_mask_lzcnt_epi32(src, k, a) simde_mm_mask_lzcnt_epi32(src, k, a)
201 #endif
202
203 SIMDE_FUNCTION_ATTRIBUTES
204 simde__m128i
205 simde_mm_maskz_lzcnt_epi32(simde__mmask8 k, simde__m128i a) {
206 #if defined(SIMDE_X86_AVX512VL_NATIVE) && defined(SIMDE_X86_AVX512CD_NATIVE)
207 return _mm_maskz_lzcnt_epi32(k, a);
208 #else
209 return simde_mm_maskz_mov_epi32(k, simde_mm_lzcnt_epi32(a));
210 #endif
211 }
212 #if defined(SIMDE_X86_AVX512F_ENABLE_NATIVE_ALIASES)
213 #undef _mm_maskz_lzcnt_epi32
214 #define _mm_maskz_lzcnt_epi32(k, a) simde_mm_maskz_lzcnt_epi32(k, a)
215 #endif
216
217 SIMDE_END_DECLS_
218 HEDLEY_DIAGNOSTIC_POP
219
220 #endif /* !defined(SIMDE_X86_AVX512_LZCNT_H) */