std/simd

March 14, 2026 ยท View on GitHub

Zen-C provides native SIMD (Single Instruction, Multiple Data) vector types that compile directly to hardware-optimized vector instructions (SSE, AVX, NEON, etc.) supported by the target backend.

Overview

  • Native Performance: Leverages LLVM/GCC vector extensions for maximum efficiency.
  • Implicitly Portable: Types like f32x4 map to the best available 128-bit hardware specific to the architecture.
  • Element-wise arithmetic: Standard operators (+, -, *, /) apply to all lanes simultaneously.
  • Broadcasting: Initializing with a single value broadcasts it to all lanes.

Usage

import "std/simd.zc"

fn main() {
    // Initialization (Explicit lanes)
    let a = f32x4 { 1.0, 2.0, 3.0, 4.0 };
    
    // Broadcasting (Single value to all lanes)
    let b = f32x4 { v: 2.0 };
    
    // Element-wise addition
    let c = a + b;   // Result: { 3.0, 4.0, 5.0, 6.0 }
    
    // Lane Access
    let first = c[0];
}

Vector Types

The standard library defines several 128-bit and 256-bit vector types. You can also define custom ones using the @vector(N) attribute.

128-bit Vectors (SSE / NEON)

TypeElement TypeLanesByte Size
f32x4f32416
f64x2f64216
i32x4i32416
u32x4u32416
i64x2i64216
u64x2u64216
i16x8i16816
u16x8u16816
i8x16i81616
u8x16u81616

256-bit Vectors (AVX / AVX2)

TypeElement TypeLanesByte Size
f32x8f32832
f64x4f64432
i32x8i32832
u32x8u32832
i64x4i64432
u64x4u64432
i16x16i161632
u16x16u161632
i8x32i83232
u8x32u83232

Operations

CategoryOperatorDescription
Arithmetic+, -, *, /Standard element-wise addition, subtraction, multiplication, and division.
Bitwise&, |, ^, ~Bitwise AND, OR, XOR, and NOT across all lanes.
Indexing[i]Access or modify individual lanes by index.
Comparison==, !=, <, >Returns a boolean mask vector (results vary by backend).