From bf4bc9a3e400d8e772032eeb06c0f6da70a90271 Mon Sep 17 00:00:00 2001 From: Andrea Cappuccio Date: Mon, 9 Nov 2026 11:12:14 +0100 Subject: [PATCH 1/4] perf(hex): decode 16-byte blocks with NEON and SSE2 --- include/nbytes.h | 9 ++++-- src/nbytes.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/basic.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 2 deletions(-) diff --git a/include/nbytes.h b/include/nbytes.h index e4f94c8..a207424 100644 --- a/include/nbytes.h +++ b/include/nbytes.h @@ -238,11 +238,16 @@ inline int8_t Unhex(TypeName c) { return unhex_table[static_cast(c)]; } +size_t HexDecodeBlocks(char *buf, size_t len, const char *src, size_t srcLen); + template static size_t HexDecode(char *buf, size_t len, const TypeName *src, const size_t srcLen) { - size_t i; - for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) { + size_t i = 0; + if constexpr (sizeof(TypeName) == 1) { + i = HexDecodeBlocks(buf, len, reinterpret_cast(src), srcLen); + } + for (; i < len && i * 2 + 1 < srcLen; ++i) { unsigned a = Unhex(src[i * 2 + 0]); unsigned b = Unhex(src[i * 2 + 1]); if (!~a || !~b) return i; diff --git a/src/nbytes.cpp b/src/nbytes.cpp index 09e2665..a48e6ea 100644 --- a/src/nbytes.cpp +++ b/src/nbytes.cpp @@ -3,6 +3,15 @@ #include #include +#if defined(__ARM_NEON) || defined(_M_ARM64) +#include +#define NBYTES_HEX_NEON 1 +#elif defined(__SSE2__) || defined(_M_X64) || \ + (defined(_M_IX86_FP) && _M_IX86_FP >= 2) +#include +#define NBYTES_HEX_SSE2 1 +#endif + namespace nbytes { // ============================================================================ @@ -157,6 +166,72 @@ const int8_t unhex_table[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; +#if defined(NBYTES_HEX_NEON) +namespace { +inline bool HexDecodeBlock(const char *src, char *dst) { + const uint8x16x2_t c = vld2q_u8(reinterpret_cast(src)); + uint8x16_t nib[2]; + uint8x16_t valid = vdupq_n_u8(0xFF); + for (int k = 0; k < 2; k++) { + const uint8x16_t d = vsubq_u8(c.val[k], vdupq_n_u8('0')); + const uint8x16_t l = + vsubq_u8(vorrq_u8(c.val[k], vdupq_n_u8(0x20)), vdupq_n_u8('a')); + const uint8x16_t is_digit = vcltq_u8(d, vdupq_n_u8(10)); + const uint8x16_t is_alpha = vcltq_u8(l, vdupq_n_u8(6)); + valid = vandq_u8(valid, vorrq_u8(is_digit, is_alpha)); + nib[k] = vbslq_u8(is_digit, d, vaddq_u8(l, vdupq_n_u8(10))); + } + if (vminvq_u8(valid) == 0) return false; + vst1q_u8(reinterpret_cast(dst), + vorrq_u8(vshlq_n_u8(nib[0], 4), nib[1])); + return true; +} +} // namespace +#elif defined(NBYTES_HEX_SSE2) +namespace { +inline __m128i HexNibbles(__m128i c, int *mask) { + const __m128i d = _mm_sub_epi8(c, _mm_set1_epi8('0')); + const __m128i l = + _mm_sub_epi8(_mm_or_si128(c, _mm_set1_epi8(0x20)), _mm_set1_epi8('a')); + const __m128i is_digit = _mm_cmpeq_epi8(_mm_min_epu8(d, _mm_set1_epi8(9)), d); + const __m128i is_alpha = _mm_cmpeq_epi8(_mm_min_epu8(l, _mm_set1_epi8(5)), l); + *mask = _mm_movemask_epi8(_mm_or_si128(is_digit, is_alpha)); + return _mm_or_si128( + _mm_and_si128(is_digit, d), + _mm_andnot_si128(is_digit, _mm_add_epi8(l, _mm_set1_epi8(10)))); +} + +inline __m128i HexPack(__m128i n) { + const __m128i hi = _mm_and_si128(n, _mm_set1_epi16(0x00FF)); + const __m128i lo = _mm_srli_epi16(n, 8); + return _mm_or_si128(_mm_slli_epi16(hi, 4), lo); +} + +inline bool HexDecodeBlock(const char *src, char *dst) { + int m0, m1; + const __m128i n0 = + HexNibbles(_mm_loadu_si128(reinterpret_cast(src)), &m0); + const __m128i n1 = HexNibbles( + _mm_loadu_si128(reinterpret_cast(src + 16)), &m1); + if ((m0 & m1) != 0xFFFF) return false; + _mm_storeu_si128(reinterpret_cast<__m128i *>(dst), + _mm_packus_epi16(HexPack(n0), HexPack(n1))); + return true; +} +} // namespace +#endif + +size_t HexDecodeBlocks(char *buf, size_t len, const char *src, size_t srcLen) { + size_t i = 0; +#if defined(NBYTES_HEX_NEON) || defined(NBYTES_HEX_SSE2) + const size_t n = std::min(len, srcLen / 2); + for (; i + 16 <= n; i += 16) { + if (!HexDecodeBlock(src + i * 2, buf + i)) break; + } +#endif + return i; +} + inline constexpr char nibble(uint8_t x) { uint8_t add = (x >= 10) ? ('a' - 10) : '0'; return x + add; diff --git a/tests/basic.cpp b/tests/basic.cpp index 7a23211..cd85ff5 100644 --- a/tests/basic.cpp +++ b/tests/basic.cpp @@ -60,3 +60,78 @@ TEST(hex, decode_two_byte_rejects_non_latin1) { size_t n = nbytes::HexDecode(out, sizeof(out), input, 2); EXPECT_EQ(n, 0u); } + +namespace { +int RefUnhex(unsigned char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + +std::string RefHexDecode(const std::string &src, size_t len) { + std::string out; + for (size_t i = 0; i < len && i * 2 + 1 < src.size(); ++i) { + int a = RefUnhex(src[i * 2]); + int b = RefUnhex(src[i * 2 + 1]); + if (a < 0 || b < 0) break; + out.push_back(static_cast((a << 4) | b)); + } + return out; +} + +std::string HexDecodeToString(const std::string &src, size_t len) { + std::vector out(len + 1); + size_t n = nbytes::HexDecode(out.data(), len, src.data(), src.size()); + return std::string(out.data(), n); +} + +std::string RandomHex(size_t bytes, uint32_t &seed, bool upper) { + static const char lower_digits[] = "0123456789abcdef"; + static const char upper_digits[] = "0123456789ABCDEF"; + const char *digits = upper ? upper_digits : lower_digits; + std::string s; + for (size_t i = 0; i < bytes * 2; ++i) { + seed = seed * 1103515245 + 12345; + s.push_back(digits[(seed >> 16) & 15]); + } + return s; +} +} // namespace + +TEST(hex, decode_matches_reference_for_all_lengths) { + uint32_t seed = 42; + for (size_t bytes = 0; bytes <= 160; ++bytes) { + for (bool upper : {false, true}) { + std::string src = RandomHex(bytes, seed, upper); + EXPECT_EQ(HexDecodeToString(src, bytes), RefHexDecode(src, bytes)) + << "bytes=" << bytes << " upper=" << upper; + } + } +} + +TEST(hex, decode_stops_at_every_invalid_byte_in_every_position) { + uint32_t seed = 7; + const std::string base = RandomHex(48, seed, false); + for (size_t pos = 0; pos < base.size(); ++pos) { + for (int v = 0; v < 256; ++v) { + std::string src = base; + src[pos] = static_cast(v); + ASSERT_EQ(HexDecodeToString(src, 48), RefHexDecode(src, 48)) + << "pos=" << pos << " byte=" << v; + } + } +} + +TEST(hex, decode_respects_output_length_and_odd_input) { + uint32_t seed = 3; + const std::string src = RandomHex(64, seed, false); + for (size_t len = 0; len <= 64; ++len) { + EXPECT_EQ(HexDecodeToString(src, len), RefHexDecode(src, len)); + } + for (size_t cut = 0; cut <= src.size(); ++cut) { + std::string odd = src.substr(0, cut); + EXPECT_EQ(HexDecodeToString(odd, 64), RefHexDecode(odd, 64)) + << "cut=" << cut; + } +} From d0915f8bc939fe12d34f6ce59be95f0b23f4e20e Mon Sep 17 00:00:00 2001 From: Andrea Cappuccio Date: Mon, 16 Nov 2026 10:36:25 +0100 Subject: [PATCH 2/4] fix(hex): restrict NEON path to AArch64 --- src/nbytes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nbytes.cpp b/src/nbytes.cpp index a48e6ea..1d5a92d 100644 --- a/src/nbytes.cpp +++ b/src/nbytes.cpp @@ -3,7 +3,7 @@ #include #include -#if defined(__ARM_NEON) || defined(_M_ARM64) +#if defined(__aarch64__) || defined(_M_ARM64) #include #define NBYTES_HEX_NEON 1 #elif defined(__SSE2__) || defined(_M_X64) || \ From 249e15d2f595ebfdd5cc614dc7f7392100000550 Mon Sep 17 00:00:00 2001 From: Andrea Cappuccio Date: Mon, 16 Nov 2026 10:36:35 +0100 Subject: [PATCH 3/4] fix(hex): include for std::min --- src/nbytes.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nbytes.cpp b/src/nbytes.cpp index 1d5a92d..6f5d03e 100644 --- a/src/nbytes.cpp +++ b/src/nbytes.cpp @@ -1,4 +1,5 @@ #include "nbytes.h" +#include #include #include #include From 6ffc50b5bc692e1f1ee5ee9876882576744fb6f8 Mon Sep 17 00:00:00 2001 From: Andrea Cappuccio Date: Mon, 16 Nov 2026 10:37:09 +0100 Subject: [PATCH 4/4] refactor(hex): hide HexDecodeBlocks, skip it on short input --- include/nbytes.h | 7 ++++++- src/nbytes.cpp | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/nbytes.h b/include/nbytes.h index a207424..b7228a6 100644 --- a/include/nbytes.h +++ b/include/nbytes.h @@ -238,14 +238,19 @@ inline int8_t Unhex(TypeName c) { return unhex_table[static_cast(c)]; } +namespace detail { size_t HexDecodeBlocks(char *buf, size_t len, const char *src, size_t srcLen); +} // namespace detail template static size_t HexDecode(char *buf, size_t len, const TypeName *src, const size_t srcLen) { size_t i = 0; if constexpr (sizeof(TypeName) == 1) { - i = HexDecodeBlocks(buf, len, reinterpret_cast(src), srcLen); + if (len >= 16 && srcLen >= 32) { + i = detail::HexDecodeBlocks(buf, len, reinterpret_cast(src), + srcLen); + } } for (; i < len && i * 2 + 1 < srcLen; ++i) { unsigned a = Unhex(src[i * 2 + 0]); diff --git a/src/nbytes.cpp b/src/nbytes.cpp index 6f5d03e..89640d5 100644 --- a/src/nbytes.cpp +++ b/src/nbytes.cpp @@ -222,6 +222,7 @@ inline bool HexDecodeBlock(const char *src, char *dst) { } // namespace #endif +namespace detail { size_t HexDecodeBlocks(char *buf, size_t len, const char *src, size_t srcLen) { size_t i = 0; #if defined(NBYTES_HEX_NEON) || defined(NBYTES_HEX_SSE2) @@ -232,6 +233,7 @@ size_t HexDecodeBlocks(char *buf, size_t len, const char *src, size_t srcLen) { #endif return i; } +} // namespace detail inline constexpr char nibble(uint8_t x) { uint8_t add = (x >= 10) ? ('a' - 10) : '0';