|
1 | 1 | // @flow strict-local
|
2 | 2 |
|
3 |
| -// Small wasm program that exposes the `ctz` instruction. |
4 |
| -// https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/Count_trailing_zeros |
5 |
| -const wasmBuf = new Uint8Array([ |
6 |
| - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, |
7 |
| - 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01, 0x09, 0x74, 0x72, |
8 |
| - 0x61, 0x69, 0x6c, 0x69, 0x6e, 0x67, 0x30, 0x00, 0x00, 0x0a, 0x07, 0x01, 0x05, |
9 |
| - 0x00, 0x20, 0x00, 0x68, 0x0b, 0x00, 0x0f, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x02, |
10 |
| - 0x08, 0x01, 0x00, 0x01, 0x00, 0x03, 0x6e, 0x75, 0x6d, |
11 |
| -]); |
12 |
| - |
13 |
| -// eslint-disable-next-line |
14 |
| -const {trailing0} = new WebAssembly.Instance(new WebAssembly.Module(wasmBuf)) |
15 |
| - .exports; |
| 3 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/clz32#implementing_count_leading_ones_and_beyond |
| 4 | +function ctz32(n: number): number { |
| 5 | + if (n === 0) { |
| 6 | + return 32; |
| 7 | + } |
| 8 | + let reversed = n & -n; |
| 9 | + return 31 - Math.clz32(reversed); |
| 10 | +} |
16 | 11 |
|
17 | 12 | export class BitSet {
|
18 | 13 | bits: Uint32Array;
|
@@ -95,7 +90,7 @@ export class BitSet {
|
95 | 90 | while (v !== 0) {
|
96 | 91 | let t = (v & -v) >>> 0;
|
97 | 92 | // $FlowFixMe
|
98 |
| - fn((k << 5) + trailing0(v)); |
| 93 | + fn((k << 5) + ctz32(v)); |
99 | 94 | v ^= t;
|
100 | 95 | }
|
101 | 96 | }
|
|
0 commit comments