typestar

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

  1. readings.gt(limit) gives one boolean per lane; bare > gives a single Bool.
  2. hot.select(limit, readings) takes limit wherever the mask is true.
  3. reduce_or and reduce_and ask whether any or all lanes matched.

Keywords and builtins used here

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.

Type this snippet

Step 4 of 6 in SIMD, step 9 of 22 in Parameters & SIMD.

← Previous Next →