[libc-commits] [libc] [libc][stdfix] Implement fxdivi functions (rdivi) (PR #154914)
via libc-commits
libc-commits at lists.llvm.org
Sun Oct 5 19:23:53 PDT 2025
================
@@ -281,11 +281,18 @@ template <typename XType> LIBC_INLINE constexpr XType divi(int n, int d) {
// to quadratic convergence. So,
// E1 = (0.059)^2 = 0.0034
long accum val = nrstep(d_scaled_val, initial_approx);
- // E2 = 0.0000121
- val = nrstep(d_scaled_val, val);
- // E3 = 1.468e−10
- val = nrstep(d_scaled_val, val);
-
+ auto isPowerOfTwo = [](int n) { return (n > 0) && ((n & (n - 1)) == 0); };
+ // Division with a power of 2 would generally be expected to be
+ // exact, we handle this by specially treating po2 cases and having
+ // extra iterations for them.
+ if (FXRep<XType>::FRACTION_LEN > 8 || isPowerOfTwo(cpp::abs(d))) {
----------------
lntue wrote:
when the denominator is a power of two, it's better to just perform right shifts (be careful with signed-ness and overflow). Then the other conditions on the fraction lengths can be `constexpr`, i.e. no runtime cost.
https://github.com/llvm/llvm-project/pull/154914
More information about the libc-commits
mailing list