diff options
author | Jack Lloyd <[email protected]> | 2018-02-19 11:40:55 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-02-19 11:40:55 -0500 |
commit | 77506bcf956120826d6ecab52f310469bd9dceb0 (patch) | |
tree | 8a9398571a379a023a8e7113921c05ebad40da47 /src/lib/math | |
parent | 6baa9c20eb3f4d6ff81b357ff3d5760a7daaad41 (diff) |
Minor optimizations for BigInt operator/
Detect divisions by small powers of 2
Diffstat (limited to 'src/lib/math')
-rw-r--r-- | src/lib/math/bigint/big_ops3.cpp | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/src/lib/math/bigint/big_ops3.cpp b/src/lib/math/bigint/big_ops3.cpp index 48d84c8b4..eed8a29a0 100644 --- a/src/lib/math/bigint/big_ops3.cpp +++ b/src/lib/math/bigint/big_ops3.cpp @@ -107,6 +107,9 @@ BigInt operator*(const BigInt& x, const BigInt& y) */ BigInt operator/(const BigInt& x, const BigInt& y) { + if(y.sig_words() == 1 && is_power_of_2(y.word_at(0))) + return (x >> (y.bits() - 1)); + BigInt q, r; divide(x, y, q, r); return q; @@ -137,6 +140,9 @@ word operator%(const BigInt& n, word mod) if(mod == 0) throw BigInt::DivideByZero(); + if(mod == 1) + return 0; + if(is_power_of_2(mod)) return (n.word_at(0) & (mod - 1)); |