SIMD masks in Mojo
Comparisons yield a lanewise mask, and select uses it to blend two vectors.
comptime F4 = SIMD[DType.float64, 4]
def main():
var readings = F4(0.5, 2.5, 3.5, 1.0)
var limit = F4(2.0)
var hot = readings.gt(limit)
print(hot)
print(hot.select(limit, readings))
print(hot.reduce_or(), hot.reduce_and())
How it works
readings.gt(limit)gives one boolean per lane; bare>gives a singleBool.hot.select(limit, readings)takeslimitwherever the mask is true.reduce_orandreduce_andask whether any or all lanes matched.
Keywords and builtins used here
DTypeSIMDdefmainprintvar
The run, in numbers
- Lines
- 10
- Characters to type
- 224
- Tokens
- 67
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 62 seconds.
Step 4 of 6 in SIMD, step 9 of 22 in Parameters & SIMD.