typestar

Splatting a scalar in Mojo

Handing a SIMD constructor one value broadcasts it across every lane.

comptime F4 = SIMD[DType.float64, 4]
comptime U4 = SIMD[DType.uint8, 4]


def main():
    print(F4(7.5))
    print(U4(255))
    var ramp = F4(0.0, 1.0, 2.0, 3.0)
    print(ramp + F4(100.0))
    print(ramp * 3.0)

How it works

  1. F4(7.5) fills all four float lanes with the same number.
  2. U4(255) does the same for unsigned bytes.
  3. Splats pair with a ramp to offset or scale a whole vector at once.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
191
Tokens
64
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 55 seconds.

Type this snippet

Step 6 of 6 in SIMD, step 11 of 22 in Parameters & SIMD.

← Previous Next →