Browse the docs

silk.simd.simd.

Facts about SIMD vectors on the target CPU of the current compilation.

When to use#

Use preferredBytes with static if to pick a vector width for a kernel, and hardware to fall back to scalar code where vectors would only be emulated. Use the per-shape modules such as silk.simd.u8x16 for the vector operations themselves.

Details#

The answers come from the CPU model and features of the compilation profile, the same facts that Target.hasCpuFeature reads. They do not depend on the host machine and have no runtime cost.

Examples#

Pick a block width for a kernel#

silk
import silk.simd.simd

pub fn main() -> i32 {
  let width = simd.preferredBytes()
  if width != 16 && width != 32 && width != 64 {
    return 1
  }
  return 0
}

Import as simd with import silk.simd.simd as simd.

Public declarations: 2.

preferredBytes#

silk
pub static fn preferredBytes() -> usize

Returns the vector width, in bytes, that the target CPU handles best.

Details#

The width is 64 when AVX-512BW is enabled, 32 when AVX2 is enabled, and 16 otherwise, including on AArch64 and wasm32. Vectors of any admitted width still compile on every target.

hardware#

silk
pub static fn hardware() -> bool

Returns true if the target CPU executes 128-bit vectors in hardware.

Details#

This is always true on x86-64 and AArch64. On wasm32 it is true only with the simd128 feature; without it, vector operations compile to scalar code.

Category: SIMD.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.

View the source of this page