bitmask: Wasm SIMD bitwise instruction
The bitmask SIMD bitwise instruction inspects the most significant bit (MSB) — bit 7 — of each byte of a v128 value interpretation. This is the sign bit if the value is treated as signed. The instruction's output value is equal to all of those bits collected into a single i32, with byte 0's MSB in bit 0 of the result, byte 1's MSB in bit 1, and so on.
Note:
In a single byte, an MSB of 1 means the value is greater than or equal to 128 (this can be a negative value if signed), while an MSB of 0 means means the value is less than 128.
Try it
(module
(import "console" "log" (func $log (param i32)))
(func $main
v128.const i8x16 10 23 45 6 90 1 12 120 0 78 89 13 240 51 62 0
i8x16.bitmask
call $log ;; log the result
)
(start $main)
)
WebAssembly.instantiateStreaming(fetch("{%wasm-url%}"), { console });
In the above example, only lane 12 of the i8x16 value has a value of greater than or equal to 128 (240, to be exact), so its MSB is 1. All other byte's MSBs are set to 0.
The output i32 is therefore equal to:
0000 0001 0000 0000 0000
(only bit 12 is set to 1).
The example's output value is 4096, which is the decimal equivent of the above binary pattern.
Syntax
value_type.bitmask
value_type-
The type of value the instruction is being run on. The following types support
bitmask:i8x16i16x8i32x4i64x2
bitmask-
The
bitmaskinstruction. Must always be included after thevalue_typeand a period (.).
Type
[input] -> [output]
Binary encoding
| Instruction | Binary format | Example text => binary |
|---|---|---|
i8x16.bitmask |
0xfd 100:u32 |
i8x16.bitmask => 0xfd 0x64 |
i16x8.bitmask |
0xfd 132:u32 |
i16x8.bitmask => 0xfd 0x84 0x01 |
i32x4.bitmask |
0xfd 164:u32 |
i32x4.bitmask => 0xfd 0xa4 0x01 |
i64x2.bitmask |
0xfd 196:u32 |
i64x2.bitmask => 0xfd 0xc4 0x01 |