From f34ba44bf88e830bdecebfd3948a7c7ffb4e2215 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Thu, 22 Jul 2021 23:00:36 +0200 Subject: big: Add `shl`, `shr` and `shrmod`. --- core/math/big/basic.odin | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'core/math/big/basic.odin') diff --git a/core/math/big/basic.odin b/core/math/big/basic.odin index 42db8b88e..cbabd073b 100644 --- a/core/math/big/basic.odin +++ b/core/math/big/basic.odin @@ -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, }; /* ========================== -- cgit v1.2.3