Issue 241209
Summary [libc][stdfix] lrdivi saturates to the wrong sign for huge quotients
Labels libc
Assignees
Reporter robinbishtt
    Fix #

**Expected**

`long fract` cannot hold a quotient with |n/d| >= 1, so the result must saturate:
MAX if n/d > 0, MIN if n/d < 0.

**Actual**

For huge quotients the sign of the saturated result is flipped. I called
`fixed_point::fxdivi<long fract, long>` (what `lrdivi` forwards to) on main `0651368ba1`:

```
lrdivi(1L << 62, 3)        -> MIN expected MAX
lrdivi(-(1L << 62), 3)     -> MAX   expected MIN
lrdivi(LONG_MIN, -30221)   -> MIN   expected MAX
lrdivi(LONG_MIN, 30221)    -> MAX   expected MIN
```

`lrdivi(5, 3)` and `lrdivi(-5, 3)` are correct.

**How I found it**

I added saturation checks for a huge numerator to `FxDiviTest.h` (`func(huge, 3) == fx_max` and the
other three sign combinations) and a random check against exact integer arithmetic. Output without
the fix:

<!-- replace this block with your own Colab output -->
```
      check failed (line 229): EXPECT_EQ(func(huge, 3), fx_max)
      check failed (line 231): EXPECT_EQ(func(huge, -3), fx_min)
 check failed (line 232): EXPECT_EQ(func(-huge, 3), fx_min)
      check failed (line 233): EXPECT_EQ(func(-huge, -3), fx_max)
  lrdivi WideOperands FAIL
      first wrong: n=-206990482126251727 d=99
 saturation check: 194230 cases, 20228 wrong
  lrdivi sat_check FAIL
```

The other fxdivi types (`rdivi`, `kdivi`, `lkdivi`, `urdivi`, `ukdivi`, `ulrdivi`, `ulkdivi`) pass the
same checks. A PR with the fix and the tests follows.

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

Reply via email to