計算前導零
clz 指令,是 count leading zeros(計算前導零)的縮寫,用於計算數字二進位制表示開頭零的數量。
試一試
(module
(func (export "leading0") (param $num i32) (result i32)
;; load number onto the stack
local.get $num
;; check how many leading zeros and return the result
i32.clz
)
)
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console }).then(
(result) => {
const leading0 = result.instance.exports.leading0;
console.log(
`Leading zeros: ${leading0(0b00000000_10000000_00000000_00000000)}`,
);
// Expected output: "Leading zeros: 8"
},
);
語法
wat
;; load a number onto the stack
i32.const 8388608 ;; 00000000_10000000_00000000_00000000
;; count leading zeros
i32.clz
;; the top item on the stack will now be 8
| 指令 | 二進位制操作碼 |
|---|---|
i32.clz |
0x67 |
i64.clz |
0x79 |