31 std::array<uint8_t, 256> t{};
32 for (
size_t i = 0; i < 256; ++i) {
35 for (
size_t d = 0; d < 10; ++d) {
36 t[
size_t{
'0'} + d] =
static_cast<uint8_t
>(d);
38 for (
size_t d = 0; d < 6; ++d) {
39 t[
size_t{
'a'} + d] =
static_cast<uint8_t
>(10 + d);
40 t[
size_t{
'A'} + d] =
static_cast<uint8_t
>(10 + d);
48 bool& is_pure_decimal)
noexcept {
49 is_pure_decimal =
false;
50 if (p >= end) [[unlikely]] {
53 if (end - p >= 2 && p[0] ==
'0' && ((p[1] | 0x20) ==
'x')) {
55 if (p == end || *p ==
'.') {
61 while (p < end && *p !=
'.') {
62 const uint8_t nib =
hex_nibble[
static_cast<unsigned char>(*p)];
63 if (nib == 0xff) [[unlikely]] {
66 if (v > (0xFFFFFFFFULL >> 4)) [[unlikely]] {
73 if (digits == 0) [[unlikely]] {
79 if (end - p >= 2 && p[0] ==
'0' && p[1] >=
'0' && p[1] <=
'9') {
82 while (p < end && *p !=
'.') {
84 if (c <
'0' || c >
'7') [[unlikely]] {
87 if (v > (0xFFFFFFFFULL >> 3)) [[unlikely]] {
90 v = (v << 3) | static_cast<uint64_t>(c -
'0');
96 if (*p <
'0' || *p >
'9') [[unlikely]] {
99 is_pure_decimal =
true;
100 uint64_t v =
static_cast<uint64_t
>(*p -
'0');
102 while (p < end && *p !=
'.') {
104 if (c <
'0' || c >
'9') [[unlikely]] {
107 if (v > 429496729ULL) [[unlikely]] {
110 v = v * 10u +
static_cast<uint64_t
>(c -
'0');
111 if (v > 0xFFFFFFFFULL) [[unlikely]] {
122 uint16_t& value)
noexcept {
123 if (pointer == end) {
126 const uint8_t n0 =
hex_nibble[
static_cast<unsigned char>(*pointer)];
133 if (pointer != end) {
134 const uint8_t n1 =
hex_nibble[
static_cast<unsigned char>(*pointer)];
139 if (pointer != end) {
140 const uint8_t n2 =
hex_nibble[
static_cast<unsigned char>(*pointer)];
145 if (pointer != end) {
146 const uint8_t n3 =
hex_nibble[
static_cast<unsigned char>(*pointer)];
157 value =
static_cast<uint16_t
>(v);
161#if defined(ADA_AVX512_IPV6)
166 std::array<uint16_t, 8>& address)
noexcept {
168 _mm256_setr_epi8(0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
169 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7);
170 const __m256i off = _mm256_setr_epi8(
171 -4, -3, -2, -1, -4, -3, -2, -1, -4, -3, -2, -1, -4, -3, -2, -1, -4, -3,
172 -2, -1, -4, -3, -2, -1, -4, -3, -2, -1, -4, -3, -2, -1);
173 const __m256i idx = _mm256_max_epi8(
175 _mm256_permutexvar_epi8(grp, _mm256_castsi128_si256(ends)), off),
176 _mm256_permutexvar_epi8(grp, _mm256_castsi128_si256(prev)));
177 const __m256i gathered = _mm512_castsi512_si256(
178 _mm512_permutexvar_epi8(_mm512_castsi256_si512(idx), nib));
180 const __m256i hextets = _mm256_madd_epi16(
181 _mm256_maddubs_epi16(gathered, _mm256_set1_epi16(0x0110)),
182 _mm256_set1_epi32(0x00010100));
183 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(address.data()),
184 _mm256_cvtepi32_epi16(hextets));
194 std::array<uint16_t, 8>& address,
195 bool& valid)
noexcept {
196 if (len < 2 || len > 45) [[unlikely]] {
200 alignas(64)
static constexpr uint8_t hex_lo[64] = {
201 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
202 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
203 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
204 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
205 0x80, 0x80, 0x80, 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
206 0x07, 0x08, 0x09, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80};
207 alignas(64)
static constexpr uint8_t hex_hi[64] = {
208 0x80, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x80, 0x80, 0x80, 0x80,
209 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
210 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
211 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x80, 0x80, 0x80, 0x80, 0x80,
212 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
213 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80};
214 alignas(64)
static constexpr uint8_t iota[64] = {
215 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
216 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
217 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
218 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63};
220 const __mmask64 active =
221 static_cast<__mmask64
>(_bzhi_u64(~0ULL,
static_cast<uint32_t
>(len)));
222 const __m512i colon = _mm512_set1_epi8(
':');
225 const __m512i v = _mm512_mask_loadu_epi8(colon, active, data);
226 const __mmask64 mcolon = _mm512_cmpeq_epi8_mask(v, colon);
231 if (_mm512_cmpeq_epi8_mask(v, _mm512_set1_epi8(
'.'))) [[unlikely]] {
235 const __m512i nib = _mm512_permutex2var_epi8(_mm512_load_si512(hex_lo), v,
236 _mm512_load_si512(hex_hi));
237 const __m128i comp = _mm512_castsi512_si128(
238 _mm512_maskz_compress_epi8(mcolon, _mm512_load_si512(iota)));
239 const __m128i lane0 =
240 _mm_setr_epi8(-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
241 const __m128i prev = _mm_or_si128(_mm_bslli_si128(comp, 1), lane0);
242 const __m128i width = _mm_sub_epi8(comp, prev);
244 const uint64_t mc =
static_cast<uint64_t
>(mcolon);
245 const uint64_t act =
static_cast<uint64_t
>(active);
246 const uint64_t dc = mc & (mc >> 1) & (act >> 1);
249 ipv6_gather(comp, prev, nib, address);
252 const __m512i fused = _mm512_ternarylogic_epi32(
253 nib, v, _mm512_set1_epi8(
static_cast<char>(0x80)), 0xf8);
256 _mm_setr_epi8(3, 3, 3, 3, 3, 3, 3, 3, -1, -1, -1, -1, -1, -1, -1, -1);
257 __mmask64 err = _kandn_mask64(mcolon, _mm512_movepi8_mask(fused));
259 err,
static_cast<__mmask64
>(
static_cast<uint16_t
>(_mm_cmpgt_epu8_mask(
260 _mm_sub_epi8(width, _mm_set1_epi8(2)), wmax))));
261 valid = (_mm_popcnt_u64(mc & act) == 7) && !err;
267 const uint32_t head =
static_cast<uint32_t
>(_mm_popcnt_u64(mc & (dc - 1))) +
268 static_cast<uint32_t
>(dc > 1);
269 const __mmask16 keep = _mm_cmpgt_epi8_mask(width, _mm_set1_epi8(1));
270 const uint32_t kept =
static_cast<uint32_t
>(_mm_popcnt_u32(keep));
271 const __mmask16 slots =
static_cast<__mmask16
>(
272 ~(_bzhi_u32(0xFFFFFFFFu, 8 - kept) << head) & 0xFFu);
275 ipv6_gather(_mm_maskz_expand_epi8(slots, _mm_maskz_compress_epi8(keep, comp)),
276 _mm_mask_expand_epi8(_mm_set1_epi8(-1), slots,
277 _mm_maskz_compress_epi8(keep, prev)),
280 uint64_t err = _mm512_movepi8_mask(v);
281 err |= _mm512_movepi8_mask(nib) & ~mc;
282 err |= _blsr_u64(dc);
283 err |=
static_cast<uint64_t
>(kept >= 8);
284 err |= _mm_cmpgt_epu8_mask(width, _mm_set1_epi8(5)) & keep;
285 err |= (mc & 1ULL) & ~dc;
286 err |= ((mc >> (len - 1)) & 1ULL) & ~(dc >> (len - 2));