[PATCH] D120327: compiler-rt: Add udivmodei5 to builtins and add bitint library

Jacob Lifshay via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 29 04:31:59 PDT 2022


programmerjake added inline comments.


================
Comment at: compiler-rt/lib/builtins/int_lib.h:119
+COMPILER_RT_ABI void __udivmodei5(su_int *quo, su_int *rem, const su_int *a,
+                                  const su_int *b, unsigned int words);
+
----------------
mgehre-amd wrote:
> programmerjake wrote:
> > this should be changed to pass in a buffer big enough for knuth division...
> > that is `words * 2 + 1` (the buffer can just be `a` but zero-extended). also `b` needs to be non-const since knuth division needs to modify it.
> Maybe it's a stupid question, but does Knuth actually need extra scratch space when it's allowed to modify the inputs?
> 
> In APInt::divide, I see four temporary arrays being allocated (U, V, Q, R), each with a size of words (except for U which is words + 1).
> But then LHS and RHS are copied in to U and V. And after Knuth's algorithms, Q and R are copied into quo and rem.
> 
> Now if we allow to modify the arguments, we don't need to the copies and thus we can directly use the space of `a`, `b`, `quo` and `rem`, can't we?
hmm...i'd have to check the algorithm again...at the very least `a` has to have at least 1 extra word unless you want to branch every time you index `a` (or maybe peel off one iteration of the loop...sounds like a pain).

Knuth's algorithm (from Knuth's book):
inputs:
u = (u[1] u[2] ... u[m+n])
v = (v[1] v[2] ... v[n])

u is extended to (u[0] u[1] u[2] ... u[m+n]) by normalization step.

outputs:
floor(u/v) = (q[0] q[1] ... q[m])
u mod v = (r[1] r[2] ... r[n])


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D120327/new/

https://reviews.llvm.org/D120327



More information about the llvm-commits mailing list