From fdc6c4ec5d6d0b2c94390de96f12a81e4ef46182 Mon Sep 17 00:00:00 2001 From: Chris Tate Date: Wed, 21 Oct 2026 09:11:25 -0500 Subject: [PATCH] Convert string index arguments without libm calls The runtime's ToIntegerOrInfinity for string index arguments (slice, substring, charAt, indexOf, startsWith, padStart, repeat and the others in scr_string.c) converts through int64 instead of calling trunc, which is a libm call on baseline x86-64. Each slice made two such calls. --- packages/runtime/src/scr_string.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/runtime/src/scr_string.c b/packages/runtime/src/scr_string.c index ddd984e72..41588f25a 100644 --- a/packages/runtime/src/scr_string.c +++ b/packages/runtime/src/scr_string.c @@ -1759,10 +1759,14 @@ static __attribute__((noinline)) size_t scr_byte_to_u16_walk(const ScrStr *s, Sc } /* ECMA-262 ToIntegerOrInfinity for a double already known to be a Number: - * NaN → +0, otherwise truncate toward zero, ±Infinity preserved. */ -static double scr_to_integer_or_infinity(double x) { - if (isnan(x)) return 0.0; - return trunc(x); + * NaN → +0, otherwise truncate toward zero, ±Infinity preserved. Inside + * (-2^63, 2^63) the integer conversion truncates, without the libm trunc + * call baseline x86-64 needs; a fraction above -1 gives +0, as the spec's + * mathematical result does. Every double outside that range is already an + * integer or infinite, and NaN fails both comparisons. */ +static inline double scr_to_integer_or_infinity(double x) { + if (x > -9223372036854775808.0 && x < 9223372036854775808.0) return (double)(int64_t)x; + return isnan(x) ? 0.0 : x; } /* Byte substring search (needle and hay are both canonical WTF-8,