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
F4(7.5)fills all four float lanes with the same number.U4(255)does the same for unsigned bytes.- Splats pair with a ramp to offset or scale a whole vector at once.
Keywords and builtins used here
DTypeSIMDdefmainprintvar
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.
Step 6 of 6 in SIMD, step 11 of 22 in Parameters & SIMD.