def pow_go

bend-collections-laws-math@1.0.0.0 · src/math/generic.bend:170

import bend-collections-laws-math@1.0.0.0/src/math/generic.bend as Generic
import Base
import bend-collections-laws-math@1.0.0.0/src/math/num.bend as N

170# square-and-multiply, low bits first; an overflowing square means the
171# result overflows too (a later bit multiplies it in). Callers pass fuel
172# 1 + k: every step halves k, so the loop always ends on k == 0.
173def pow_go(~T: Data, ~op: N.Op<T> -> T, ~test: N.Test<T> -> Bool, fuel: Nat, +k: Nat, +b: T, +bok: Bool, st: T & Bool) -> Maybe<&2, T>:
174  match fuel k:
175    case 0n _:
176      st_fin(T, st)
177    case 1n+f 0n:
178      st_fin(T, st)
179    case 1n+f 1n+j:
180      pow_go(~T, ~op, ~test, f, Nat.div(1n+j, 2n), sq_val(~T, ~op, Nat.is_lt(1n, 1n+j), b), sq_ok(~T, ~test, Nat.is_lt(1n, 1n+j), b, bok), mul_st(~T, ~op, ~test, Nat.is_eq(Nat.mod(1n+j, 2n), 1n), b, bok, st))

Uses (14)

Used by (12)