Skip to content

Commit 190936f

Browse files
gpsheadclaude
andcommitted
pystrhex: Factor out scalar hexlify into shared inline function
Extract the scalar hexlify loop into _Py_hexlify_scalar() which is shared between the SIMD fallback path and the main non-SIMD path. Uses table lookup via Py_hexdigits for consistency. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 92c281d commit 190936f

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

Python/pystrhex.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
#include "pycore_strhex.h" // _Py_strhex_with_sep()
55
#include "pycore_unicodeobject.h" // _PyUnicode_CheckConsistency()
66

7+
/* Scalar hexlify: convert len bytes to 2*len hex characters.
8+
Uses table lookup via Py_hexdigits for the conversion. */
9+
static inline void
10+
_Py_hexlify_scalar(const unsigned char *src, Py_UCS1 *dst, Py_ssize_t len)
11+
{
12+
for (Py_ssize_t i = 0; i < len; i++) {
13+
unsigned char c = src[i];
14+
*dst++ = Py_hexdigits[c >> 4];
15+
*dst++ = Py_hexdigits[c & 0x0f];
16+
}
17+
}
18+
719
/* Portable SIMD optimization for hexlify using GCC/Clang vector extensions.
820
Uses __builtin_shufflevector for portable interleave that compiles to
921
native SIMD instructions (SSE2 punpcklbw/punpckhbw on x86-64,
@@ -87,13 +99,7 @@ _Py_hexlify_simd(const unsigned char *src, Py_UCS1 *dst, Py_ssize_t len)
8799
}
88100

89101
/* Scalar fallback for remaining 0-15 bytes */
90-
for (; i < len; i++, dst += 2) {
91-
unsigned int c = src[i];
92-
unsigned int h = c >> 4;
93-
unsigned int l = c & 0x0f;
94-
dst[0] = (Py_UCS1)(h + '0' + (h > 9) * ('a' - '0' - 10));
95-
dst[1] = (Py_UCS1)(l + '0' + (l > 9) * ('a' - '0' - 10));
96-
}
102+
_Py_hexlify_scalar(src + i, dst, len - i);
97103
}
98104

99105
#endif /* PY_HEXLIFY_CAN_COMPILE_SIMD */
@@ -184,11 +190,7 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
184190
else
185191
#endif
186192
{
187-
for (i = j = 0; i < arglen; ++i) {
188-
c = argbuf[i];
189-
retbuf[j++] = Py_hexdigits[c >> 4];
190-
retbuf[j++] = Py_hexdigits[c & 0x0f];
191-
}
193+
_Py_hexlify_scalar((const unsigned char *)argbuf, retbuf, arglen);
192194
}
193195
}
194196
else {

0 commit comments

Comments
 (0)