[libc-commits] [libc] [libc][stdfix] Implement fxdivi functions (rdivi) (PR #154914)
via libc-commits
libc-commits at lists.llvm.org
Fri Sep 26 05:34:28 PDT 2025
================
@@ -246,13 +248,21 @@ template <typename XType> LIBC_INLINE constexpr XType divi(int n, int d) {
}
bool result_is_negative = ((n < 0) != (d < 0));
- unsigned int nv = static_cast<unsigned int>(n < 0 ? -n : n);
- unsigned int dv = static_cast<unsigned int>(d < 0 ? -d : d);
- unsigned int clz = cpp::countl_zero<unsigned int>(dv) - 1;
- unsigned long int scaled_val = dv << clz;
+ int64_t n64 = static_cast<int64_t>(n);
+ int64_t d64 = static_cast<int64_t>(d);
+
+ uint64_t nv = static_cast<uint64_t>(n64 < 0 ? -n64 : n64);
+ uint64_t dv = static_cast<uint64_t>(d64 < 0 ? -d64 : d64);
+
+ if (d == INT_MIN) {
+ dv = dv - 1; // Two's complement
----------------
lntue wrote:
`INT_MIN` is a power of 2. It might be better / more accurate to multiply `nv` by 2 and divide `dv` by 2 here:
```
// d = -2^31, dv = 2^31. Scale-by-2 to prevent clz overflowed.
nv <<= 1;
dv >>= 1;
```
https://github.com/llvm/llvm-project/pull/154914
More information about the libc-commits
mailing list