pxdct 0.3.2

Discrete cosine transform
Documentation
/*
 * // Copyright (c) Radzivon Bartoshyk 1/2026. All rights reserved.
 * //
 * // Redistribution and use in source and binary forms, with or without modification,
 * // are permitted provided that the following conditions are met:
 * //
 * // 1.  Redistributions of source code must retain the above copyright notice, this
 * // list of conditions and the following disclaimer.
 * //
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
 * // this list of conditions and the following disclaimer in the documentation
 * // and/or other materials provided with the distribution.
 * //
 * // 3.  Neither the name of the copyright holder nor the names of its
 * // contributors may be used to endorse or promote products derived from
 * // this software without specific prior written permission.
 * //
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
use crate::bidirectional::BidirectionalStore;
use crate::dct2::MixedRadix5Sample;
use crate::mla::fmla;
use crate::neon::dct2::mixed_radix3d::{
    dct2_radix_n_cos_twiddles_neond, dct2_radix_n_rotation_twiddles_neond,
};
use crate::neon::store_d::NeonStoreD;
use crate::neon::util::boring_neon_mixed_radix;
use crate::util::{DctSample, try_vec, validate_scratch};
use crate::{PxdctError, PxdctExecutor};
use num_traits::One;
use std::sync::Arc;

/// Radix-5 DCT-II implementation using direct decomposition algorithm.
///
/// This implements a fast DCT-II for lengths divisible by 5, decomposing the transform
/// into smaller sub-transforms around a center pivot element. The algorithm exploits
/// the symmetry structure: C₀ - S₁ - A - S₃ - C₄, where A is the center buffer.
pub(crate) struct NeonDct2MixedRadix5d {
    /// Precomputed rotation twiddles: tan(π(q-1-2m)k/(2N)) for combining C and S buffers
    rotation_layer: Vec<NeonStoreD>,
    /// Precomputed cosine twiddles for even and odd components
    cos_twiddles: Vec<NeonStoreD>,
    inner_dct: Arc<dyn PxdctExecutor<f64> + Send + Sync>,
    inner_dct_scratch_size: usize,
    execution_length: usize,
    q_modules: usize,
}

impl NeonDct2MixedRadix5d {
    pub(crate) fn new(
        len: usize,
        inner_dct: Arc<dyn PxdctExecutor<f64> + Send + Sync>,
    ) -> Result<NeonDct2MixedRadix5d, PxdctError> {
        assert!(
            len.is_multiple_of(5),
            "Mixed radix 5 should not be called on sizes no divisible by 5"
        );

        let q_modules = len / 5;

        // always 2 inner groups in Radix-5

        let rotation_layer = dct2_radix_n_rotation_twiddles_neond(5, q_modules, len);

        let cos_twiddles = dct2_radix_n_cos_twiddles_neond(5, q_modules, len);

        let inner_dct_scratch_size = inner_dct.scratch_size();

        Ok(NeonDct2MixedRadix5d {
            rotation_layer,
            inner_dct,
            inner_dct_scratch_size,
            cos_twiddles,
            execution_length: len,
            q_modules: len / 5,
        })
    }
}

impl NeonDct2MixedRadix5d {
    #[inline(always)]
    fn exec_block<S: BidirectionalStore<f64>, const N: usize>(
        &self,
        data: &mut S,
        a_buffer: &[f64],
        s_buffer: &[f64],
        c_buffer: &[f64],
        uk: usize,
        k: usize,
    ) {
        let rotation_twiddle_re = unsafe { *self.rotation_layer.get_unchecked(uk) };
        let rotation_twiddle_im = unsafe { *self.rotation_layer.get_unchecked(uk + 1) };

        let c_forward = NeonStoreD::load_n::<N>(unsafe { c_buffer.get_unchecked(k..) });
        let s_forward = NeonStoreD::load_n::<N>(unsafe {
            s_buffer.get_unchecked(self.q_modules - k - (N - 1)..)
        })
        .reverse_n::<N>();

        let rotated_dc = fmla(s_forward, rotation_twiddle_re, c_forward);

        let twiddle_re = unsafe { *self.cos_twiddles.get_unchecked(uk) };
        let twiddle_im = unsafe { *self.cos_twiddles.get_unchecked(uk + 1) };

        let twiddled_dc = rotated_dc * twiddle_re;

        let mut dc0 = twiddled_dc;
        let mut dc2 = twiddled_dc * f64::R5_COS_EVEN2_M0;
        let mut dc4 = twiddled_dc * f64::R5_COS_EVEN4_M0;

        let rotated_ds = fmla(c_forward, rotation_twiddle_im, s_forward);

        let twiddled_ds = rotated_ds * twiddle_im;

        let mut ds1 = twiddled_ds * f64::R5_SIN_ODD_M0;
        let mut ds3 = twiddled_ds * f64::R5_SIN_ODD1_M0;

        {
            let c_forward =
                NeonStoreD::load_n::<N>(unsafe { c_buffer.get_unchecked(self.q_modules + k..) });
            let s_forward = NeonStoreD::load_n::<N>(unsafe {
                s_buffer.get_unchecked(self.q_modules * 2 - k - (N - 1)..)
            })
            .reverse_n::<N>();

            let rotation_twiddle_re = unsafe { *self.rotation_layer.get_unchecked(uk + 2) };
            let rotation_twiddle_im = unsafe { *self.rotation_layer.get_unchecked(uk + 3) };

            let twiddle_re = unsafe { *self.cos_twiddles.get_unchecked(uk + 2) };
            let twiddle_im = unsafe { *self.cos_twiddles.get_unchecked(uk + 3) };

            let rotated_dc1 = fmla(s_forward, rotation_twiddle_re, c_forward);
            let rotated_ds2 = fmla(c_forward, rotation_twiddle_im, s_forward);

            let twiddled_dc = twiddle_re * rotated_dc1;
            let twiddled_ds = twiddle_im * rotated_ds2;

            dc0 = twiddled_dc + dc0;
            dc2 = NeonStoreD::f64_mul_add(f64::R5_COS_EVEN4_M0, twiddled_dc, dc2);
            dc4 = NeonStoreD::f64_mul_add(f64::R5_COS_EVEN2_M0, twiddled_dc, dc4);

            ds1 = NeonStoreD::f64_mul_nadd(f64::R5_SIN_ODD1_M0, twiddled_ds, ds1);
            ds3 = NeonStoreD::f64_mul_add(f64::R5_SIN_ODD_M0, twiddled_ds, ds3);
        }

        let a0 = NeonStoreD::load_n::<N>(unsafe { a_buffer.get_unchecked(k..) });
        let dc = dc0 + a0;
        dc.write_n::<N>(data.slice_from_mut(k..));

        let dss1 = NeonStoreD::f64_mul_add(2., ds1, -dc);
        let idx = self.q_modules * 2 - k - (N - 1);
        dss1.reverse_n::<N>()
            .write_n::<N>(data.slice_from_mut(idx..));

        dc2 = -(dc2 + a0); // negated 2j
        dc2 = NeonStoreD::f64_mul_add(2., dc2, -dss1);
        let idx1 = self.q_modules * 2 + k;
        dc2.write_n::<N>(data.slice_from_mut(idx1..));

        let dss3 = NeonStoreD::f64_mul_add(2., -ds3, -dc2);
        let idx = self.q_modules * 4 - k - (N - 1);
        dss3.reverse_n::<N>()
            .write_n::<N>(data.slice_from_mut(idx..));

        dc4 += a0;

        let idx1 = self.q_modules * 4 + k;
        let uq = NeonStoreD::f64_mul_add(2., dc4, -dss3);
        uq.write_n::<N>(data.slice_from_mut(idx1..));
    }

    #[inline(always)]
    fn execute_with_store<S: BidirectionalStore<f64>>(
        &self,
        data: &mut S,
        scratch: &mut [f64],
    ) -> Result<(), PxdctError> {
        let (scratch, inner_scratch) = scratch.split_at_mut(self.execution_length);
        let q_modules = self.execution_length / 5;
        let (a_buffer, c_s_buffer) = scratch.split_at_mut(q_modules);
        let (c_buffer, s_buffer) = c_s_buffer.split_at_mut(q_modules * 2);

        // Step 1: Decompose input into A (center), C (even-symmetric), S (odd-symmetric) buffers
        for (n, dst) in a_buffer.iter_mut().enumerate() {
            *dst = data[n * 5 + 2];
        }

        // Extract and combine symmetric pairs with sign alternation for S buffer
        for (m, (c_buffer, s_buffer)) in c_buffer
            .chunks_exact_mut(q_modules)
            .zip(s_buffer.chunks_exact_mut(q_modules))
            .enumerate()
        {
            let mut sign = f64::one();
            for (n, (c_dst, s_dst)) in c_buffer.iter_mut().zip(s_buffer.iter_mut()).enumerate() {
                let u0 = data[5 * n + m];
                let u1 = data[5 * n + 5 - m - 1];

                *c_dst = u0 + u1;
                *s_dst = (u0 - u1).mulsign(sign);

                sign = -sign;
            }
        }

        // Step 2: Apply DCT-II to all buffers (A, C₀, C₁, S₀, S₁)
        self.inner_dct
            .execute_with_scratch(scratch, inner_scratch)?;

        let (a_buffer, c_s_buffer) = scratch.split_at_mut(q_modules);
        let (c_buffer, s_buffer) = c_s_buffer.split_at_mut(q_modules * 2);

        {
            // Step 3: Recombine transformed buffers with twiddle factors

            // Handle k=0 case (DC and low frequencies)
            let qc = c_buffer[0];
            let mut c0 = qc;
            let mut c1 = qc * f64::R5_COS_EVEN2_M0;
            let mut c2 = qc * f64::R5_COS_EVEN4_M0;

            let s0_twiddled = s_buffer[0];

            // Odd components: S₁ uses j=1 (abs), S₃ uses j=3 (negated)
            let mut s0 = s0_twiddled * f64::R5_SIN_ODD_M0; // S₁: abs(sin(3π/5))
            let mut s1 = s0_twiddled * f64::R5_SIN_ODD1_M0; // S₃: -sin(π/5)

            {
                let ci = unsafe { *c_buffer.get_unchecked(q_modules) };
                let si = unsafe { *s_buffer.get_unchecked(q_modules) };

                let twiddle_ci = ci;
                let twiddle_si = si;

                c0 += ci;
                c1 = fmla(twiddle_ci, f64::R5_COS_EVEN4_M0, c1);
                c2 = fmla(twiddle_ci, f64::R5_COS_EVEN2_M0, c2);
                s0 = fmla(twiddle_si, -f64::R5_SIN_ODD1_M0, s0);
                s1 = fmla(twiddle_si, f64::R5_SIN_ODD_M0, s1);
            }

            let a0 = a_buffer[0];
            let dc = c0 + a0;
            data[0] = dc;

            let dc2 = c2 + a0;
            data[q_modules * 4] = dc2;
            data[q_modules * 3] = -s1;
            data[q_modules] = s0;
            let qid2 = -(c1 + a0); // negated 2j
            data[q_modules * 2] = qid2;

            let mut k = 1usize;
            let mut uk = 0usize;

            while k + 2 <= q_modules {
                self.exec_block::<S, 2>(data, a_buffer, s_buffer, c_buffer, uk, k);
                k += 2;
                uk += 4;
            }

            let remainder = q_modules - k;
            if remainder == 1 {
                self.exec_block::<S, 1>(data, a_buffer, s_buffer, c_buffer, uk, k);
            }
        }
        Ok(())
    }
}

boring_neon_mixed_radix!(NeonDct2MixedRadix5d, f64);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Pxdct;
    use crate::tests::naive_dct2;

    #[test]
    fn test_radix5_dct() {
        let mut input = vec![0.; 45];
        for (i, z) in input.iter_mut().enumerate() {
            *z = i as f64 + rand::random::<f64>() * 10.0;
        }
        // let mut input = vec![
        //     7.6871257, 1.2637726, 11.096954, 4.113755, 6.3156953, 12.343984, 9.859292, 15.516256,
        //     12.010594, 18.957434, 11.183157, 16.510174, 13.310775, 21.062075, 19.775341, 20.445467,
        //     22.57258, 25.571342, 23.987795, 19.597996, 24.935028, 21.360756, 22.820232, 27.915956,
        //     31.28283, 24.935028, 21.360756, 22.820232, 27.915956, 31.28283,
        // ];
        let mut reference_input = input.clone();
        reference_input = naive_dct2(&reference_input);
        let bf =
            NeonDct2MixedRadix5d::new(input.len(), Pxdct::make_dct2_f64(input.len() / 5).unwrap())
                .unwrap();
        bf.execute(&mut input).unwrap();
        println!(
            "{:?}",
            input
                .iter()
                .enumerate()
                .map(|(i, x)| format!("({i}) {}", x))
                .collect::<Vec<_>>()
        );
        println!(
            "{:?}",
            reference_input
                .iter()
                .enumerate()
                .map(|(i, x)| format!("({i}) {}", x))
                .collect::<Vec<_>>()
        );
        input
            .iter()
            .zip(reference_input.iter())
            .enumerate()
            .for_each(|(i, (&src, &r0))| {
                if (src - r0).abs() > 1e-1 {
                    println!(
                        "Difference must be < {}, but it was {}, at position {i}",
                        1e-1,
                        (src - r0).abs()
                    )
                }
            });
    }
}