From 590577f88042fd5bb281d9576324b6f709a07dc4 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Wed, 9 Apr 2025 12:43:09 +0200 Subject: speed up multiplication --- src/block.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/block.rs') diff --git a/src/block.rs b/src/block.rs index abd7ff7..e9ba527 100644 --- a/src/block.rs +++ b/src/block.rs @@ -134,19 +134,24 @@ impl IndexMut for Block { impl Mul for Block { type Output = Block; - fn mul(self, rhs: u32) -> Block { - match rhs { - 0 => Block::NULL, - 1 => self, - 2 => { - let mut result = self << 1; - if self[0] & 0x80 != 0 { - result[15] ^= 135; + fn mul(mut self, mut rhs: u32) -> Block { + loop { + match rhs { + 0 => break Block::NULL, + 1 => break self, + 2 => { + let mut result = self << 1; + if self[0] & 0x80 != 0 { + result[15] ^= 135; + } + break result; } - result + _ if rhs % 2 == 0 => { + self = self * 2; + rhs = rhs / 2; + } + _ => break self * (rhs - 1) ^ self, } - _ if rhs % 2 == 0 => self * 2 * (rhs / 2), - _ => self * (rhs - 1) ^ self, } } } -- cgit v1.2.3