Skip to content

Commit 7589a4f

Browse files
Add a 4-digit SWAR follow-up to loop_parse_if_eight_digits (clang)
After the 8-digit SWAR block loop, consume a remaining 4-7 digit run in one read4_to_u32 + parse_four_digits_unrolled step instead of byte-by-byte (reusing the existing 4-digit helpers). The parsed result is identical; this is purely a faster way to consume the same digits. Gated to clang: on gcc the extra 4-digit check regresses inputs whose remainder is < 4 digits (e.g. the 17-digit fraction of uniform [0,1] -> -3% on 'random'), because the check becomes pure overhead there; clang does not show that. m8g.metal-24xl (Graviton4), -O3 -march=native, simple_fastfloat_benchmark, from_chars->double, clang 18, base vs patch back-to-back (2 samples): canada.txt +11.7%, mesh.txt +7.4%, random ~flat. No regression.
1 parent 7790aa6 commit 7589a4f

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

include/fast_float/ascii_number.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,21 @@ loop_parse_if_eight_digits(char const *&p, char const *const pend,
266266
p)); // in rare cases, this will overflow, but that's ok
267267
p += 8;
268268
}
269+
// Consume a remaining 4-7 digit run in a single SWAR step instead of
270+
// byte-by-byte (reuses the existing 4-digit helpers). The parsed result is
271+
// identical either way. Gated to clang: on gcc the extra 4-digit check
272+
// regresses inputs whose remainder is shorter than 4 digits (it becomes pure
273+
// overhead there); clang does not show that.
274+
#if defined(__clang__)
275+
if ((pend - p) >= 4) {
276+
uint32_t const val4 = read4_to_u32(p);
277+
if (is_made_of_four_digits_fast(val4)) {
278+
i = i * 10000 +
279+
parse_four_digits_unrolled(val4); // may overflow, that's ok
280+
p += 4;
281+
}
282+
}
283+
#endif
269284
}
270285

271286
enum class parse_error {

0 commit comments

Comments
 (0)