make code branchless (#2546)

This commit is contained in:
Liqiang TAO
2025-11-19 05:57:06 +08:00
committed by GitHub
parent b1c31b428d
commit 667d0ed3c7
+4 -10
View File
@@ -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<std::uint32_t>(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<char>('0' + k);
} else if (k < 100) {
*buf++ = static_cast<char>('0' + k / 10);
k %= 10;
*buf++ = static_cast<char>('0' + k);