SIMD arithmetic in Mojo
Arithmetic on SIMD hits every lane at once, in a single instruction.
comptime F4 = SIMD[DType.float64, 4]
def main():
var a = F4(1.0, 2.0, 4.0, 5.0)
var b = F4(10.0, 20.0, 40.0, 50.0)
print(a + b)
print(b - a)
print(a * b)
print(b / a)
How it works
a + badds lane 0 to lane 0, lane 1 to lane 1, and so on.- Subtraction, multiplication, and division behave the same way.
- No loop is written here, and none is generated.
Keywords and builtins used here
DTypeSIMDdefmainprintvar
The run, in numbers
- Lines
- 10
- Characters to type
- 168
- Tokens
- 64
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 51 seconds.
Step 2 of 6 in SIMD, step 7 of 22 in Parameters & SIMD.