Issue 241898
Summary [libc] strtod/strtof/strtold round incorrectly in directed rounding modes
Labels libc
Assignees
Reporter 0z5a
    On current main (b02e25dc58dc), strtod, strtof and strtold read the current rounding mode, but they return the wrong neighbor for some in-range inputs when it is not round-to-nearest. I found three separate code paths in `libc/src/__support/str_to_float.h` that are each affected.

Reproducer (x86_64 Linux, overlay build of libc linked first, clang 24):

```c
#include <fenv.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
  fesetround(FE_DOWNWARD);
 printf("%a\n", strtod("0x1.0000000000000cp0", NULL));
 printf("%a\n", (double)strtof("0xf9p-153", NULL));
 fesetround(FE_UPWARD);
  printf("%a\n", (double)strtof("94669824081.6", NULL));
  printf("%a\n", strtod("9007199254740993", NULL));
 printf("%La\n", strtold("77113036484543467188", NULL));
  return 0;
}
```

Results, with the expected values computed with exact rational arithmetic (glibc 2.39 agrees):

```
input mode         LLVM libc main expected
strtod("0x1.0000000000000cp0")     FE_DOWNWARD 0x1.0000000000001p+0      0x1p+0
strtof("0xf9p-153") FE_DOWNWARD  0x1p-145                  0x1.ep-146
strtof("94669824081.6") FE_UPWARD    0x1.60ac2ep+36 0x1.60ac3p+36
strtod("9007199254740993")         FE_UPWARD    0x1p+53 0x1.0000000000001p+53
strtold("77113036484543467188") FE_UPWARD    0x8.5c51143d99e69d6p+63   0x8.5c51143d99e69d7p+63
```

C23 7.24.1.5 says that for hexadecimal input, when FLT_RADIX is a power of 2, the result "is correctly rounded". Annex F.5 says these conversions honor the current rounding mode, and that decimal conversion with up to DECIMAL_DIG digits is correctly rounded. The libc code means to follow this: `strtofloatingpoint` maps `fputil::quick_get_round()` to `RoundDirection`, and `strtod_test.cpp` already has `ClingerFastPathDirectedRoundingNegativeExp`.

The three causes:

1. `binary_exp_to_float`, used for hexadecimal input. In the `else /* (round == RoundDirection::Down)*/` branch, the mantissa is incremented when both the round bit and the sticky bit are set. That is round-half-down, not round down, so any value more than half an ulp above a representable value is rounded up under FE_DOWNWARD and FE_TOWARDZERO (and the mirrored case for negatives).
2. `simple_decimal_conversion`. The normal-result path calls `hpd.round_to_integer_type<StorageType>()` without the `round` argument, so decimal inputs that reach this fallback are always rounded to nearest. The subnormal path just below it already passes `round`.
3. `eisel_lemire` (both the generic and the `long double` version). The `RoundDirection::Up` branch only checks the bits below the round bit (`low64(final_approx)` and the `halfway_constant` bits), not the round bit itself. That bit is the lowest bit of `final_mantissa` before the final shift. So a value exactly halfway between two neighbors is rounded down under FE_UPWARD.

These have been there since directed rounding was added. The same code is in llvmorg-17.0.1 through llvmorg-23.1.0. libc++ `from_chars` shares this header but always passes `RoundDirection::Nearest`, so it is not affected.

Two further checks, both on x86_64 Linux. First, for 300k random doubles and floats written out as their exact decimal expansions, strtod and strtof return the value exactly in all four modes, on main and with the fix, so exact inputs are not affected. Second, 300k random decimal and hexadecimal strings were run in all four modes and checked against exact rational arithmetic. On main this finds in-range misroundings in all three formats; with the three changes above it finds none.

One further difference is out of scope for this issue. Out-of-range results do not follow the rounding direction: overflow returns infinity instead of the largest finite value under FE_DOWNWARD/FE_TOWARDZERO (C23 only requires HUGE_VAL "when default rounding is in effect"), and underflow returns 0 instead of the smallest subnormal under FE_UPWARD. That one needs a design decision, so I did not touch it.

A fix with a regression test is ready. I can open a pull request for it.

Assisted-by: Claude (claude-opus-5-5). I reviewed the change and this text.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to