MDN / bitselect: Wasm SIMD bitwise instruction
MDN / bitselect: Wasm SIMD bitwise instruction
The bitselect SIMD bitwise instruction takes three v128 values as inputs — two inputs and a mask value — and returns a new v128 value with each byte calculated using the formula output = (input1 AND mask) OR (input2 AND NOT mask).
(module
(import "console" "log" (func $log (param i32)))
(func $main
v128.const i8x16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
v128.const i8x16 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
v128.const i8x16 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
v128.bitselect
i8x16.extract_lane_u 15
call $log ;; log the result
)
(start $main)
)
WebAssembly.instantiateStreaming(fetch("{%wasm-url%}"), { console });
In the above example, we’ve set all input value lanes to the same value for simplicity. Walking through how the output value (9) is calculated, using the formula output = (input1 AND mask) OR (input2 AND NOT mask):
The first input is
1, which is0 0 0 0 0 0 0 1in binary.The second input is
15, which is0 0 0 0 1 1 1 1in binary.The mask is
6, which is0 0 0 0 0 1 1 0in binary.input1 AND maskis calculated as follows:input1 0 0 0 0 0 0 0 1 mask 0 0 0 0 0 1 1 0 input1 AND mask 0 0 0 0 0 0 0 0input2 AND NOT maskis calculated as follows:input2 0 0 0 0 1 1 1 1 NOT mask 1 1 1 1 1 0 0 1 input2 AND NOT mask 0 0 0 0 1 0 0 1We then OR the two results from steps 4. and 5.:
result1 0 0 0 0 0 0 0 0 result2 0 0 0 0 1 0 0 1 OR 0 0 0 0 1 0 0 1
0 0 0 0 1 0 0 1 is the binary equivalent of 9.
Syntax
v128.bitselect
v128.bitselect: The
v128.bitselectinstruction.
Type
[input1, input2, mask] -> [output]
input1: The first input
v128value interpretation.
input2: The second input
v128value interpretation.
mask: The mask
v128value interpretation.
output: The output
v128value interpretation.
Binary encoding
InstructionBinary formatExample text => binaryv128.bitselect0xfd 82:u32v128.bitselect => 0xfd 0x52