Function pow [src]
q = a ^ b
r may not alias a.
Asserts that r has enough limbs to store the result. Upper bound is
calcPowLimbsBufferLen(a.bitCountAbs(), b).
limbs_buffer is used for temporary storage.
The amount required is given by calcPowLimbsBufferLen.
Prototype
pub fn pow(r: *Mutable, a: Const, b: u32, limbs_buffer: []Limb) void
Parameters
r: *Mutable
a: Const
b: u32
limbs_buffer: []Limb
Source
pub fn pow(r: *Mutable, a: Const, b: u32, limbs_buffer: []Limb) void {
assert(r.limbs.ptr != a.limbs.ptr); // illegal aliasing
// Handle all the trivial cases first
switch (b) {
0 => {
// a^0 = 1
return r.set(1);
},
1 => {
// a^1 = a
return r.copy(a);
},
else => {},
}
if (a.eqlZero()) {
// 0^b = 0
return r.set(0);
} else if (a.limbs.len == 1 and a.limbs[0] == 1) {
// 1^b = 1 and -1^b = ±1
r.set(1);
r.positive = a.positive or (b & 1) == 0;
return;
}
// Here a>1 and b>1
const needed_limbs = calcPowLimbsBufferLen(a.bitCountAbs(), b);
assert(r.limbs.len >= needed_limbs);
assert(limbs_buffer.len >= needed_limbs);
llpow(r.limbs, a.limbs, b, limbs_buffer);
r.normalize(needed_limbs);
r.positive = a.positive or (b & 1) == 0;
}