From 667d0ed3c77f55cbda2082b034168d69898d1f88 Mon Sep 17 00:00:00 2001 From: Liqiang TAO Date: Wed, 19 Nov 2025 05:57:06 +0800 Subject: [PATCH] make code branchless (#2546) --- src/to_chars.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/to_chars.cpp b/src/to_chars.cpp index ce71ff6cd..f140e1a19 100644 --- a/src/to_chars.cpp +++ b/src/to_chars.cpp @@ -814,20 +814,14 @@ void grisu2(char *buf, int &len, int &decimal_exponent, FloatType value) { */ inline char *append_exponent(char *buf, int e) { - if (e < 0) { - e = -e; - *buf++ = '-'; - } else { - *buf++ = '+'; - } + bool isNegative = e < 0; + e = isNegative ? -e : e; + *buf++ = isNegative ? '-' : '+'; auto k = static_cast(e); - if (k < 10) { + if (k < 100) { // Always print at least two digits in the exponent. // This is for compatibility with printf("%g"). - *buf++ = '0'; - *buf++ = static_cast('0' + k); - } else if (k < 100) { *buf++ = static_cast('0' + k / 10); k %= 10; *buf++ = static_cast('0' + k);