Issue 241528
Summary [libc][stdfix] idivr returns a wrong value and idivlr crashes for MIN / -EPS
Labels libc
Assignees
Reporter robinbishtt
    **Expected**

`MIN / -EPS` is 2^(TOTAL_LEN - 1). That fits in the return type of `idivr` (`int`) and `idivlr`
(`long int`) so the result must be 32768 and 2147483648.

**Actual**

`idivr` returns -32768 and `idivlr` crashes with SIGFPE. Other inputs are fine (`x=MIN, y=+EPS`,
`x=MAX, y=-EPS`).

`idivk` and `idivlk` also crash on this input, but there the quotient does not fit the return type
so I treat that case as undefined TR 18037 and do not count it here.

**How I found it**

I called `fixed_point::idiv<T, X>` what the `idiv*` entrypoints forward to each call in its own
process. Ubuntu 24.04.4, x86_64, clang 23.1.5, llvm project main `265a353c88`
(2026-11-11):

```
 idivr   idiv<fract, int>(x=MIN, y=-EPS) exact quotient = 32768 fits in int: yes -> returned -32768  WRONG VALUE
  idivlr  idiv<long fract, long int>(x=MIN, y=-EPS) exact quotient = 2147483648           fits in long int: yes -> CRASH (signal 8 = SIGFPE)  <-- result fits the return type, so this is a bug
```

I also added a check to `IdivFxTest.h` (`func(min, -eps) == 1 << (TOTAL_LEN - 1)` when `XType` is wider
than `T`). Output without a fix:

```
      check failed (line 76): EXPECT_EQ(func(min, -eps), static_cast<XType>(1) << (FXRep::TOTAL_LEN - 1))
  idivr SpecialNumbers               FAIL
FAILED: idivr SpecialNumbers
      check failed (line 76): EXPECT_EQ(func(min, -eps), static_cast<XType>(1) << (FXRep::TOTAL_LEN - 1))
  idivlr SpecialNumbers              FAIL
FAILED: idivlr SpecialNumbers
  idivk SpecialNumbers               PASS
  idivlk SpecialNumbers              PASS
  idivur SpecialNumbers              PASS
 idivuk SpecialNumbers              PASS
  idivulr SpecialNumbers PASS
  idivulk SpecialNumbers             PASS
RESULT: 2 failing
```

A PR with the fix and the test follows.

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

Reply via email to