Skip to content

Commit 7d924d2

Browse files
Use clz32 for counting trailing zeroes. (#9340)
1 parent c45c296 commit 7d924d2

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

packages/core/graph/src/BitSet.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
// @flow strict-local
22

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+
}
1611

1712
export class BitSet {
1813
bits: Uint32Array;
@@ -95,7 +90,7 @@ export class BitSet {
9590
while (v !== 0) {
9691
let t = (v & -v) >>> 0;
9792
// $FlowFixMe
98-
fn((k << 5) + trailing0(v));
93+
fn((k << 5) + ctz32(v));
9994
v ^= t;
10095
}
10196
}

0 commit comments

Comments
 (0)