jixel 0.2.8

Tiny JPEG XL encoder
Documentation
use core::arch::wasm32::*;

#[inline]
#[target_feature(enable = "simd128")]
fn load(values: &[f32; 4]) -> v128 {
    unsafe { v128_load(values.as_ptr().cast()) }
}

#[inline]
#[target_feature(enable = "simd128")]
fn store(values: &mut [f32; 4], vector: v128) {
    unsafe { v128_store(values.as_mut_ptr().cast(), vector) }
}

#[target_feature(enable = "simd128")]
pub(crate) fn apply_cfl_wasm(x: &mut [f32], y: &[f32], b: &mut [f32], cmap_factor: [f32; 3]) {
    assert_eq!(x.len(), y.len());
    assert_eq!(x.len(), b.len());

    let cx = f32x4_splat(-cmap_factor[0]);
    let cb = f32x4_splat(-cmap_factor[2]);
    let (x_chunks, x_tail) = x.as_chunks_mut::<4>();
    let (y_chunks, y_tail) = y.as_chunks::<4>();
    let (b_chunks, b_tail) = b.as_chunks_mut::<4>();
    let (x_groups, x_chunks_tail) = x_chunks.as_chunks_mut::<4>();
    let (y_groups, y_chunks_tail) = y_chunks.as_chunks::<4>();
    let (b_groups, b_chunks_tail) = b_chunks.as_chunks_mut::<4>();

    for ((x, y), b) in x_groups.iter_mut().zip(y_groups).zip(b_groups) {
        let y0 = load(&y[0]);
        let y1 = load(&y[1]);
        let y2 = load(&y[2]);
        let y3 = load(&y[3]);
        let x0 = load(&x[0]);
        let x1 = load(&x[1]);
        let x2 = load(&x[2]);
        let x3 = load(&x[3]);
        let b0 = load(&b[0]);
        let b1 = load(&b[1]);
        let b2 = load(&b[2]);
        let b3 = load(&b[3]);

        let x0 = f32x4_add(x0, f32x4_mul(cx, y0));
        let x1 = f32x4_add(x1, f32x4_mul(cx, y1));
        let x2 = f32x4_add(x2, f32x4_mul(cx, y2));
        let x3 = f32x4_add(x3, f32x4_mul(cx, y3));
        let b0 = f32x4_add(b0, f32x4_mul(cb, y0));
        let b1 = f32x4_add(b1, f32x4_mul(cb, y1));
        let b2 = f32x4_add(b2, f32x4_mul(cb, y2));
        let b3 = f32x4_add(b3, f32x4_mul(cb, y3));

        store(&mut x[0], x0);
        store(&mut x[1], x1);
        store(&mut x[2], x2);
        store(&mut x[3], x3);
        store(&mut b[0], b0);
        store(&mut b[1], b1);
        store(&mut b[2], b2);
        store(&mut b[3], b3);
    }

    for ((x, y), b) in x_chunks_tail
        .iter_mut()
        .zip(y_chunks_tail)
        .zip(b_chunks_tail)
    {
        let yv = load(y);
        store(x, f32x4_add(load(x), f32x4_mul(cx, yv)));
        store(b, f32x4_add(load(b), f32x4_mul(cb, yv)));
    }

    for ((x, &y), b) in x_tail.iter_mut().zip(y_tail).zip(b_tail) {
        *x -= cmap_factor[0] * y;
        *b -= cmap_factor[2] * y;
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn apply_cfl_wasm_matches_scalar() {
        for size in [0, 1, 3, 4, 7, 16, 64, 1024, 4096] {
            let y: Vec<f32> = (0..size)
                .map(|i| ((i * 37 % 101) as f32 - 50.0) * 0.03125)
                .collect();
            let mut x: Vec<f32> = (0..size).map(|i| i as f32 * 0.015625 - 2.0).collect();
            let mut b: Vec<f32> = (0..size).map(|i| 3.0 - i as f32 * 0.0078125).collect();
            let mut want_x = x.clone();
            let mut want_b = b.clone();
            let factors: [f32; 3] = [0.3125, 0.0, -0.1875];
            for i in 0..size {
                want_x[i] = -factors[0] * y[i] + want_x[i];
                want_b[i] = -factors[2] * y[i] + want_b[i];
            }
            super::apply_cfl_wasm(&mut x, &y, &mut b, factors);
            assert_eq!(x, want_x, "X mismatch at size {size}");
            assert_eq!(b, want_b, "B mismatch at size {size}");
        }
    }
}