typestar

Parameters & SIMD

22 steps in 5 sets of Mojo.

Mojo splits every function into two halves: what the compiler can settle and what the machine has to do. Square brackets carry compile-time parameters, so a size or a type is nailed down before the program starts and the generated code has nothing left to check. SIMD is the other half — a vector type that maps onto a real hardware register, where one instruction adds four lanes at once.

Together they are the reason Mojo exists. This tour walks from `comptime` bindings and parametric structs through masks, casts, and reductions, then down to raw pointers and spans, and finishes with the mandelbrot render that made the language famous.

Start this tour

Compile-time Mojo

SIMD

  • SIMD vectorsSIMD is a fixed-width vector that maps straight onto a hardware register.
  • SIMD arithmeticArithmetic on SIMD hits every lane at once, in a single instruction.
  • SIMD reductionsReductions collapse a whole vector down to one scalar in log-depth steps.
  • SIMD masksComparisons yield a lanewise mask, and select uses it to blend two vectors.
  • Casting SIMD lanescast converts every lane to another DType without leaving the vector.
  • Splatting a scalarHanding a SIMD constructor one value broadcasts it across every lane.

Numbers & time

  • Math functionsstd.math covers the usual scalar functions and applies them lanewise to SIMD.
  • Random numbersSeeding first makes a random run reproducible, which is what benchmarks need.
  • Timing a loopperf_counter_ns brackets a block of work to measure how long it really took.
  • Bitwise operationsShifts and masks are how flags, hashes, and packed formats get handled.
  • Float32 against Float64Narrower floats are faster and smaller, but they carry fewer digits.

Memory

  • Unsafe pointersalloc hands back raw memory that you index directly and must free yourself.
  • Pointer arithmeticAdding to a pointer moves it by whole elements, never by bytes.
  • List capacityA List doubles its buffer as it grows, which is what makes appends cheap.
  • Span viewsA Span borrows a window into a list without copying any of it.

Encore

The other Mojo tours