Back to Lo

Simd Clamp

docs/data/simd-clamp.md

1.53.0968 B
Original Source

Clamps each element in a collection between min and max values using SIMD instructions. The suffix (x2, x4, x8, x16, x32, x64) indicates the number of lanes processed simultaneously.

Note: Choose the variant matching your CPU's capabilities. Higher lane counts provide better performance but require newer CPU support.

go
// Using AVX2 variant (32 lanes at once) - Intel Haswell+ / AMD Excavator+
result := simd.ClampInt8x32([]int8{1, 5, 10, 15, 20}, 5, 15)
// []int8{5, 5, 10, 15, 15}
go
// Using AVX-512 variant (16 lanes at once) - Intel Skylake-X+
result := simd.ClampFloat32x16([]float32{0.5, 1.5, 2.5, 3.5}, 1.0, 3.0)
// []float32{1.0, 1.5, 2.5, 3.0}
go
// Using AVX variant (8 lanes at once) - works on all amd64
result := simd.ClampInt16x8([]int16{100, 150, 200, 250}, 120, 220)
// []int16{120, 150, 200, 220}
go
// Empty collection returns empty collection
result := simd.ClampUint32x4([]uint32{}, 10, 100)
// []uint32{}