mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-02 04:38:16 +00:00
big: Move _mul private functions.
This commit is contained in:
@@ -290,168 +290,6 @@ int_choose_digit :: proc(res: ^Int, n, k: int) -> (err: Error) {
|
||||
}
|
||||
choose :: proc { int_choose_digit, };
|
||||
|
||||
/*
|
||||
Multiplies |a| * |b| and only computes upto digs digits of result.
|
||||
HAC pp. 595, Algorithm 14.12 Modified so you can control how
|
||||
many digits of output are created.
|
||||
*/
|
||||
_int_mul :: proc(dest, a, b: ^Int, digits: int) -> (err: Error) {
|
||||
/*
|
||||
Can we use the fast multiplier?
|
||||
*/
|
||||
if digits < _WARRAY && min(a.used, b.used) < _MAX_COMBA {
|
||||
return _int_mul_comba(dest, a, b, digits);
|
||||
}
|
||||
|
||||
/*
|
||||
Set up temporary output `Int`, which we'll swap for `dest` when done.
|
||||
*/
|
||||
|
||||
t := &Int{};
|
||||
|
||||
if err = grow(t, max(digits, _DEFAULT_DIGIT_COUNT)); err != nil { return err; }
|
||||
t.used = digits;
|
||||
|
||||
/*
|
||||
Compute the digits of the product directly.
|
||||
*/
|
||||
pa := a.used;
|
||||
for ix := 0; ix < pa; ix += 1 {
|
||||
/*
|
||||
Limit ourselves to `digits` DIGITs of output.
|
||||
*/
|
||||
pb := min(b.used, digits - ix);
|
||||
carry := _WORD(0);
|
||||
iy := 0;
|
||||
|
||||
/*
|
||||
Compute the column of the output and propagate the carry.
|
||||
*/
|
||||
#no_bounds_check for iy = 0; iy < pb; iy += 1 {
|
||||
/*
|
||||
Compute the column as a _WORD.
|
||||
*/
|
||||
column := _WORD(t.digit[ix + iy]) + _WORD(a.digit[ix]) * _WORD(b.digit[iy]) + carry;
|
||||
|
||||
/*
|
||||
The new column is the lower part of the result.
|
||||
*/
|
||||
t.digit[ix + iy] = DIGIT(column & _WORD(_MASK));
|
||||
|
||||
/*
|
||||
Get the carry word from the result.
|
||||
*/
|
||||
carry = column >> _DIGIT_BITS;
|
||||
}
|
||||
/*
|
||||
Set carry if it is placed below digits
|
||||
*/
|
||||
if ix + iy < digits {
|
||||
t.digit[ix + pb] = DIGIT(carry);
|
||||
}
|
||||
}
|
||||
|
||||
swap(dest, t);
|
||||
destroy(t);
|
||||
return clamp(dest);
|
||||
}
|
||||
|
||||
/*
|
||||
Fast (comba) multiplier
|
||||
|
||||
This is the fast column-array [comba] multiplier. It is
|
||||
designed to compute the columns of the product first
|
||||
then handle the carries afterwards. This has the effect
|
||||
of making the nested loops that compute the columns very
|
||||
simple and schedulable on super-scalar processors.
|
||||
|
||||
This has been modified to produce a variable number of
|
||||
digits of output so if say only a half-product is required
|
||||
you don't have to compute the upper half (a feature
|
||||
required for fast Barrett reduction).
|
||||
|
||||
Based on Algorithm 14.12 on pp.595 of HAC.
|
||||
*/
|
||||
_int_mul_comba :: proc(dest, a, b: ^Int, digits: int) -> (err: Error) {
|
||||
/*
|
||||
Set up array.
|
||||
*/
|
||||
W: [_WARRAY]DIGIT = ---;
|
||||
|
||||
/*
|
||||
Grow the destination as required.
|
||||
*/
|
||||
if err = grow(dest, digits); err != nil { return err; }
|
||||
|
||||
/*
|
||||
Number of output digits to produce.
|
||||
*/
|
||||
pa := min(digits, a.used + b.used);
|
||||
|
||||
/*
|
||||
Clear the carry
|
||||
*/
|
||||
_W := _WORD(0);
|
||||
|
||||
ix: int;
|
||||
for ix = 0; ix < pa; ix += 1 {
|
||||
tx, ty, iy, iz: int;
|
||||
|
||||
/*
|
||||
Get offsets into the two bignums.
|
||||
*/
|
||||
ty = min(b.used - 1, ix);
|
||||
tx = ix - ty;
|
||||
|
||||
/*
|
||||
This is the number of times the loop will iterate, essentially.
|
||||
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||
*/
|
||||
|
||||
iy = min(a.used - tx, ty + 1);
|
||||
|
||||
/*
|
||||
Execute loop.
|
||||
*/
|
||||
#no_bounds_check for iz = 0; iz < iy; iz += 1 {
|
||||
_W += _WORD(a.digit[tx + iz]) * _WORD(b.digit[ty - iz]);
|
||||
}
|
||||
|
||||
/*
|
||||
Store term.
|
||||
*/
|
||||
W[ix] = DIGIT(_W) & _MASK;
|
||||
|
||||
/*
|
||||
Make next carry.
|
||||
*/
|
||||
_W = _W >> _WORD(_DIGIT_BITS);
|
||||
}
|
||||
|
||||
/*
|
||||
Setup dest.
|
||||
*/
|
||||
old_used := dest.used;
|
||||
dest.used = pa;
|
||||
|
||||
/*
|
||||
Now extract the previous digit [below the carry].
|
||||
*/
|
||||
// for ix = 0; ix < pa; ix += 1 { dest.digit[ix] = W[ix]; }
|
||||
|
||||
copy_slice(dest.digit[0:], W[:pa]);
|
||||
|
||||
/*
|
||||
Clear unused digits [that existed in the old copy of dest].
|
||||
*/
|
||||
zero_unused(dest, old_used);
|
||||
|
||||
/*
|
||||
Adjust dest.used based on leading zeroes.
|
||||
*/
|
||||
|
||||
return clamp(dest);
|
||||
}
|
||||
|
||||
/*
|
||||
Low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16
|
||||
|
||||
Reference in New Issue
Block a user