vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation


//! WGSL lowering source for `stats.std_dev`.

/// Dispatchable WGSL kernel for population standard deviation over packed byte input.
pub const WGSL: &str = concat!(
    include_str!("../wgsl_byte_primitives/bytes.wgsl"),
    "\n",
    include_str!("wgsl/std_dev.wgsl"),
);

/// Compute the population standard deviation of byte values.
///
/// Empty input returns `0.0`.
#[must_use]
pub fn std_dev(input: &[u8]) -> f32 {
    if input.is_empty() {
        return 0.0;
    }
    let mean = input.iter().map(|&byte| f64::from(byte)).sum::<f64>() / input.len() as f64;
    let squared = input
        .iter()
        .map(|&byte| {
            let delta = f64::from(byte) - mean;
            delta * delta
        })
        .sum::<f64>();
    (squared / input.len() as f64).sqrt() as f32
}

/// Compute the standard deviation and return its IEEE-754 binary32 bit pattern.
#[must_use]
pub fn std_dev_bits(input: &[u8]) -> u32 {
    std_dev(input).to_bits()
}