aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2025-04-09 12:43:09 +0200
committerDaniel Schadt <kingdread@gmx.de>2025-04-09 12:46:24 +0200
commit590577f88042fd5bb281d9576324b6f709a07dc4 (patch)
treec3446c01133888153b3f945e6e5ff1554acc2add /src
parent9c5a943fdec6ecd9b547a851a6f20ac74d5884e4 (diff)
downloadzears-590577f88042fd5bb281d9576324b6f709a07dc4.tar.gz
zears-590577f88042fd5bb281d9576324b6f709a07dc4.tar.bz2
zears-590577f88042fd5bb281d9576324b6f709a07dc4.zip
speed up multiplication
Diffstat (limited to 'src')
-rw-r--r--src/block.rs27
1 files changed, 16 insertions, 11 deletions
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<usize> for Block {
impl Mul<u32> 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,
}
}
}