Skip to main content

gain_generic/
gain_generic.rs

1// Copyright 2026 the Fearless_SIMD Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! Apply gain to floating-point vectors of different widths and element types.
5
6use fearless_simd::{Level, dispatch, f32x4, f32x16, f64x2, prelude::*};
7
8// V::Element makes the gain match the vector's scalar type: f32 or f64.
9#[inline(always)] // or #[simd], either works
10fn apply_gain<S: Simd, V: SimdFloat<S>>(samples: V, gain: V::Element) -> V {
11    samples * gain
12}
13
14fn main() {
15    let level = Level::new();
16    dispatch!(level, simd => {
17        // f32 vectors can be any length
18        let samples = f32x4::from_slice(simd, &[0.1, -0.2, 0.3, -0.4]);
19        let output = apply_gain(samples, 0.5);
20        println!("f32x4: {output:?}");
21
22        let samples = f32x16::from_slice(
23            simd,
24            &[
25                0.1, -0.2, 0.3, -0.4, 0.5, -0.6, 0.7, -0.8,
26                0.9, -1.0, 1.1, -1.2, 1.3, -1.4, 1.5, -1.6,
27            ],
28        );
29        let output = apply_gain(samples, 0.5);
30        println!("f32x16: {output:?}");
31
32        // f64 vectors work too through the same helper
33        let samples = f64x2::from_slice(simd, &[0.1, -0.2]);
34        let output = apply_gain(samples, 0.5);
35        println!("f64x2: {output:?}");
36    });
37}