Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce two new primitives {`shiftLeft`, `shiftRight`}, both with signature `(Nat, Nat32) -> Nat`. This is hopefully helpful for #3175. ------- - [x] `grep` for "TODO: use shift right instead" - [x] `grep` for "TODO: use shift left instead" - [ ] QuickChecks (but the tests are rather exhaustive already) - [x] make the prims into calls? — 1672aa0 ---------- ## Further optimisation opportunities - restructure the `lsh` code to reuse the 64-bit shift result to create the bignum ([see comment below](#3112 (comment))) - `clz >= 33` as a criterion for compact
- Loading branch information
Showing
6 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { debugPrint; shiftLeft; shiftRight; nat32ToNat } = "mo:⛔" | ||
|
||
func checkShiftLeft(base : Nat, amount : Nat32) = | ||
assert base * (2 ** nat32ToNat amount) == shiftLeft(base, amount); | ||
|
||
checkShiftLeft(42, 7); | ||
checkShiftLeft(42, 24); | ||
checkShiftLeft(42, 25); | ||
checkShiftLeft(42, 26); | ||
checkShiftLeft(42, 25 + 32); // 57 | ||
checkShiftLeft(42, 25 + 64); // 89 | ||
checkShiftLeft(42, 125); | ||
checkShiftLeft(0, 125); | ||
checkShiftLeft(10 ** 10, 25); | ||
|
||
class range(x : Nat32, y : Nat32) { | ||
var i = x; | ||
public func next() : ?Nat32 { if (i > y) null else {let j = i; i += 1; ?j} }; | ||
}; | ||
|
||
for (i in range(0, 200)) { checkShiftLeft(1, i) }; | ||
for (i in range(0, 200)) { checkShiftLeft(42, i) }; | ||
|
||
func checkShiftRight(base : Nat, amount : Nat32) = | ||
assert base / 2 ** nat32ToNat amount == shiftRight(base, amount); | ||
|
||
for (i in range(0, 40)) { checkShiftRight(1, i) }; | ||
for (i in range(0, 40)) { checkShiftRight(42, i) }; | ||
|
||
let huge = 2 ** 190; | ||
for (i in range(0, 200)) { checkShiftRight(huge, i) }; | ||
for (i in range(0, 200)) { checkShiftRight(huge - 1, i) }; | ||
|
||
// iterated | ||
|
||
assert 1 == shiftRight(shiftRight(huge, 189), 1); | ||
assert 0 == shiftRight(shiftRight(huge, 189), 33); | ||
|
||
// roundtrips | ||
for (i in range(0, 200)) { assert 1 == shiftRight(shiftLeft(1, i), i) }; | ||
for (i in range(0, 200)) { assert 42 == shiftRight(shiftLeft(42, i), i) }; | ||
for (i in range(0, 200)) { assert huge == shiftRight(shiftLeft(huge, i), i) }; | ||
for (i in range(0, 200)) { assert huge - 1 == shiftRight(shiftLeft(huge - 1, i), i) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
nat-shift.mo:43.35-43.43: warning [M0155], operator may trap for inferred type | ||
Nat |