def Mag.divmod
import bend-kit-bignum@0.1.0.0/bigint.bend as Bigintimport Base
243# a / b and a % b for b above 0. Both scale by f so that b's top limb is at
244# least 10^4 / 2 (Knuth D1), and the remainder scales back.
245def Mag.divmod(a: List<&2, U32>, +b: List<&2, U32>) ->
246 List<&2, U32> & List<&2, U32>:
247 +f = (10000 / (Mag.top(b) + 1) : U32)
248 +bn = Mag.mul1(b, f, 0)
249 +n = Mag.len(bn)
250 Mag.divmod.fin(f, Mag.long(List.reverse(&2, U32, Mag.mul1(a, f, 0)), bn, n,
251 Nat.sub(n, 1n), Mag.top(bn),
252 {(Nil{}, Nil{}) : List<&2, U32> & List<&2, U32>}))