big: Add shl, shr and shrmod.

This commit is contained in:
Jeroen van Rijn
2021-07-22 23:00:36 +02:00
parent d4d863c4db
commit f34ba44bf8
4 changed files with 218 additions and 27 deletions
+14 -15
View File
@@ -463,48 +463,47 @@ int_double :: proc(dest, src: ^Int) -> (err: Error) {
double :: proc { int_double, };
shl1 :: double;
/*
dest = src % (2^power);
remainder = numerator % (1 << bits)
*/
int_mod_power_of_two :: proc(dest, src: ^Int, power: int) -> (err: Error) {
dest := dest; src := src;
if err = clear_if_uninitialized(dest); err != .None {
int_mod_bits :: proc(remainder, numerator: ^Int, bits: int) -> (err: Error) {
remainder := remainder; numerator := numerator;
if err = clear_if_uninitialized(remainder); err != .None {
return err;
}
if err = clear_if_uninitialized(src); err != .None {
if err = clear_if_uninitialized(numerator); err != .None {
return err;
}
if power < 0 { return .Invalid_Argument; }
if power == 0 { return zero(dest); }
if bits < 0 { return .Invalid_Argument; }
if bits == 0 { return zero(remainder); }
/*
If the modulus is larger than the value, return the value.
*/
err = copy(dest, src);
if power >= (src.used * _DIGIT_BITS) || err != .None {
err = copy(remainder, numerator);
if bits >= (numerator.used * _DIGIT_BITS) || err != .None {
return;
}
/*
Zero digits above the last digit of the modulus.
*/
zero_count := (power / _DIGIT_BITS) + 0 if (power % _DIGIT_BITS == 0) else 1;
zero_count := (bits / _DIGIT_BITS) + 0 if (bits % _DIGIT_BITS == 0) else 1;
/*
Zero remainder.
*/
if zero_count > 0 {
mem.zero_slice(dest.digit[zero_count:]);
mem.zero_slice(remainder.digit[zero_count:]);
}
/*
Clear the digit that is not completely outside/inside the modulus.
*/
dest.digit[power / _DIGIT_BITS] &= DIGIT(1 << DIGIT(power % _DIGIT_BITS)) - DIGIT(1);
return clamp(dest);
remainder.digit[bits / _DIGIT_BITS] &= DIGIT(1 << DIGIT(bits % _DIGIT_BITS)) - DIGIT(1);
return clamp(remainder);
}
mod_power_of_two :: proc { int_mod_power_of_two, };
mod_bits :: proc { int_mod_bits, };
/*
==========================