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):
1, which is 0 0 0 0 0 0 0 1 in binary.15, which is 0 0 0 0 1 1 1 1 in binary.6, which is 0 0 0 0 0 1 1 0 in binary.input1 AND mask is 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 0
input2 AND NOT mask is 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 1
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.
v128.bitselect
v128.bitselectThe v128.bitselect instruction.
[input1, input2, mask] -> [output]
input1The first input v128 value interpretation.
input2The second input v128 value interpretation.
maskThe mask v128 value interpretation.
outputThe output v128 value interpretation.
| Instruction | Binary format | Example text => binary |
|---|---|---|
v128.bitselect | 0xfd 82:u32 | v128.bitselect => 0xfd 0x52 |