[libc-commits] [libc] [libc][stdfix] Implement fxdivi functions (rdivi) (PR #154914)
Shreeyash Pandey via libc-commits
libc-commits at lists.llvm.org
Wed Oct 8 10:05:53 PDT 2025
================
@@ -224,6 +228,113 @@ idiv(T x, T y) {
return static_cast<XType>(result);
}
+LIBC_INLINE long accum nrstep(long accum d, long accum x0) {
+ auto v = x0 * (2.lk - (d * x0));
+ return v;
+}
+
+// Divide the two integers and return a fixed_point value
+//
+// For reference, see:
+// https://en.wikipedia.org/wiki/Division_algorithm#Newton%E2%80%93Raphson_division
+// https://stackoverflow.com/a/9231996
+
+template <typename XType> LIBC_INLINE constexpr XType divi(int n, int d) {
+ // If the value of the second operand of the / operator is zero, the
+ // behavior is undefined. Ref: ISO/IEC TR 18037:2008(E) p.g. 16
+ LIBC_CRASH_ON_VALUE(d, 0);
+
+ if (LIBC_UNLIKELY(n == 0)) {
+ return FXRep<XType>::ZERO();
+ }
+ auto is_power_of_two = [](int n) { return (n > 0) && ((n & (n - 1)) == 0); };
+ long accum max_val = static_cast<long accum>(FXRep<XType>::MAX());
+ long accum min_val = static_cast<long accum>(FXRep<XType>::MIN());
+
+ if (is_power_of_two(cpp::abs(d))) {
+ int k = cpp::countr_zero<uint32_t>(static_cast<uint32_t>(cpp::abs(d)));
+ constexpr int F = FXRep<XType>::FRACTION_LEN;
+ int64_t scaled_n = static_cast<int64_t>(n) << F;
+ int64_t res64 = scaled_n >> k;
+ constexpr int TotalBits = sizeof(XType) * 8;
----------------
bojle wrote:
done
https://github.com/llvm/llvm-project/pull/154914
More information about the libc-commits
mailing list