[libc-commits] [libc] [libc][stdfix] Fix idiv for MIN / -EPS (PR #241529)

via libc-commits libc-commits at lists.llvm.org
Thu Nov 12 11:55:42 PST 2026


================
@@ -220,7 +222,12 @@ idiv(T x, T y) {
 
   // If an integer result of one of these functions overflows, the behavior is
   // undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16
-  CompType result = x_comp / y_comp;
+  using WideType = cpp::conditional_t<
----------------
PiJoules wrote:

Hmm, using this WideType scares me a bit since this will likely bloat up the function for the 32-bit context. Regarding https://github.com/llvm/llvm-project/issues/241528, do we only see this bug specifically for `idivfx(MIN, -EPS)`? That is, we don't see any incorrect outputs for slightly different inputs like `idivfx(MIN+EPS, -EPS-EPS)`? My line of thinking is if the only bug is specifically for the `MIN, -EPS` inputs but a bunch of other edge case combinations are fine, then it might be cheaper to just literally catch this case early instead of doing the wide promotion. That is, we literally have something like

```
if constexpr (FXRep::SIGN_LEN > 0 && sizeof(XType) > sizeof(CompType)) {
  if (LIBC_UNLIKELY(x == FXRep::MIN() && y == -FXRep::EPS()))
    return static_cast<XType>(1) << FXRep::VALUE_LEN;
}
```

early on in the function.

https://github.com/llvm/llvm-project/pull/241529


More information about the libc-commits mailing list