use core::arch::x86_64::*;
use super::{COS_2PI_5, COS_4PI_5, SIN_2PI_5, SIN_4PI_5};
use crate::fft::{
Complex32,
butterflies::ops::{complex_mul_sse4_2, load_neg_imag_mask_sse4_2},
};
#[target_feature(enable = "sse4.2")]
pub(super) unsafe fn butterfly_radix5_stride1_sse4_2(
src: &[Complex32],
dst: &mut [Complex32],
stage_twiddles: &[Complex32],
) {
let samples = src.len();
let fifth_samples = samples / 5;
let simd_iters = (fifth_samples >> 1) << 1;
unsafe {
let cos_2pi_5 = _mm_set1_ps(COS_2PI_5);
let sin_2pi_5 = _mm_set1_ps(SIN_2PI_5);
let cos_4pi_5 = _mm_set1_ps(COS_4PI_5);
let sin_4pi_5 = _mm_set1_ps(SIN_4PI_5);
let negate_im = load_neg_imag_mask_sse4_2();
for i in (0..simd_iters).step_by(2) {
let z0_ptr = src.as_ptr().add(i) as *const f32;
let z0 = _mm_loadu_ps(z0_ptr);
let z1_ptr = src.as_ptr().add(i + fifth_samples) as *const f32;
let z1 = _mm_loadu_ps(z1_ptr);
let z2_ptr = src.as_ptr().add(i + fifth_samples * 2) as *const f32;
let z2 = _mm_loadu_ps(z2_ptr);
let z3_ptr = src.as_ptr().add(i + fifth_samples * 3) as *const f32;
let z3 = _mm_loadu_ps(z3_ptr);
let z4_ptr = src.as_ptr().add(i + fifth_samples * 4) as *const f32;
let z4 = _mm_loadu_ps(z4_ptr);
let t1 = z1;
let t2 = z2;
let t3 = z3;
let t4 = z4;
let sum_all = _mm_add_ps(_mm_add_ps(t1, t2), _mm_add_ps(t3, t4));
let a1 = _mm_add_ps(t1, t4);
let a2 = _mm_add_ps(t2, t3);
let t1_swap = _mm_shuffle_ps(t1, t1, 0b10_11_00_01);
let t4_swap = _mm_shuffle_ps(t4, t4, 0b10_11_00_01);
let b1_temp = _mm_sub_ps(t1_swap, t4_swap);
let b1 = _mm_xor_ps(b1_temp, negate_im);
let t2_swap = _mm_shuffle_ps(t2, t2, 0b10_11_00_01);
let t3_swap = _mm_shuffle_ps(t3, t3, 0b10_11_00_01);
let b2_temp = _mm_sub_ps(t2_swap, t3_swap);
let b2 = _mm_xor_ps(b2_temp, negate_im);
let c1 = _mm_add_ps(
z0,
_mm_add_ps(_mm_mul_ps(cos_2pi_5, a1), _mm_mul_ps(cos_4pi_5, a2)),
);
let c2 = _mm_add_ps(
z0,
_mm_add_ps(_mm_mul_ps(cos_4pi_5, a1), _mm_mul_ps(cos_2pi_5, a2)),
);
let d1 = _mm_add_ps(_mm_mul_ps(sin_2pi_5, b1), _mm_mul_ps(sin_4pi_5, b2));
let d2 = _mm_sub_ps(_mm_mul_ps(sin_4pi_5, b1), _mm_mul_ps(sin_2pi_5, b2));
let out0 = _mm_add_ps(z0, sum_all);
let out1 = _mm_add_ps(c1, d1);
let out4 = _mm_sub_ps(c1, d1);
let out2 = _mm_add_ps(c2, d2);
let out3 = _mm_sub_ps(c2, d2);
let j = 10 * i; let dst_ptr = dst.as_mut_ptr() as *mut f32;
let out0_pd = _mm_castps_pd(out0);
let out1_pd = _mm_castps_pd(out1);
let out2_pd = _mm_castps_pd(out2);
let out3_pd = _mm_castps_pd(out3);
let out4_pd = _mm_castps_pd(out4);
let out01_lo = _mm_castpd_ps(_mm_unpacklo_pd(out0_pd, out1_pd)); let out23_lo = _mm_castpd_ps(_mm_unpacklo_pd(out2_pd, out3_pd)); let out40_cross = _mm_castpd_ps(_mm_shuffle_pd(out4_pd, out0_pd, 0b10)); let out12_hi = _mm_castpd_ps(_mm_unpackhi_pd(out1_pd, out2_pd)); let out34_hi = _mm_castpd_ps(_mm_unpackhi_pd(out3_pd, out4_pd));
_mm_storeu_ps(dst_ptr.add(j), out01_lo);
_mm_storeu_ps(dst_ptr.add(j + 4), out23_lo);
_mm_storeu_ps(dst_ptr.add(j + 8), out40_cross);
_mm_storeu_ps(dst_ptr.add(j + 12), out12_hi);
_mm_storeu_ps(dst_ptr.add(j + 16), out34_hi);
}
}
super::butterfly_radix5_scalar::<2>(src, dst, stage_twiddles, 1, simd_iters);
}
#[target_feature(enable = "sse4.2")]
pub(super) unsafe fn butterfly_radix5_generic_sse4_2(
src: &[Complex32],
dst: &mut [Complex32],
stage_twiddles: &[Complex32],
stride: usize,
) {
if stride == 0 {
return;
}
let samples = src.len();
let fifth_samples = samples / 5;
let simd_iters = (fifth_samples >> 1) << 1;
unsafe {
let cos_2pi_5 = _mm_set1_ps(COS_2PI_5);
let sin_2pi_5 = _mm_set1_ps(SIN_2PI_5);
let cos_4pi_5 = _mm_set1_ps(COS_4PI_5);
let sin_4pi_5 = _mm_set1_ps(SIN_4PI_5);
let negate_im = load_neg_imag_mask_sse4_2();
for i in (0..simd_iters).step_by(2) {
let k = i % stride;
let k0 = k;
let k1 = k + 1 - ((k + 1 >= stride) as usize) * stride;
let z0_ptr = src.as_ptr().add(i) as *const f32;
let z0 = _mm_loadu_ps(z0_ptr);
let z1_ptr = src.as_ptr().add(i + fifth_samples) as *const f32;
let z1 = _mm_loadu_ps(z1_ptr);
let z2_ptr = src.as_ptr().add(i + fifth_samples * 2) as *const f32;
let z2 = _mm_loadu_ps(z2_ptr);
let z3_ptr = src.as_ptr().add(i + fifth_samples * 3) as *const f32;
let z3 = _mm_loadu_ps(z3_ptr);
let z4_ptr = src.as_ptr().add(i + fifth_samples * 4) as *const f32;
let z4 = _mm_loadu_ps(z4_ptr);
let tw_ptr = stage_twiddles.as_ptr().add(i * 4) as *const f32;
let w1 = _mm_loadu_ps(tw_ptr); let w2 = _mm_loadu_ps(tw_ptr.add(4)); let w3 = _mm_loadu_ps(tw_ptr.add(8)); let w4 = _mm_loadu_ps(tw_ptr.add(12));
let t1 = complex_mul_sse4_2(w1, z1);
let t2 = complex_mul_sse4_2(w2, z2);
let t3 = complex_mul_sse4_2(w3, z3);
let t4 = complex_mul_sse4_2(w4, z4);
let sum_all = _mm_add_ps(_mm_add_ps(t1, t2), _mm_add_ps(t3, t4));
let a1 = _mm_add_ps(t1, t4);
let a2 = _mm_add_ps(t2, t3);
let t1_swap = _mm_shuffle_ps(t1, t1, 0b10_11_00_01);
let t4_swap = _mm_shuffle_ps(t4, t4, 0b10_11_00_01);
let b1_temp = _mm_sub_ps(t1_swap, t4_swap);
let b1 = _mm_xor_ps(b1_temp, negate_im);
let t2_swap = _mm_shuffle_ps(t2, t2, 0b10_11_00_01);
let t3_swap = _mm_shuffle_ps(t3, t3, 0b10_11_00_01);
let b2_temp = _mm_sub_ps(t2_swap, t3_swap);
let b2 = _mm_xor_ps(b2_temp, negate_im);
let c1 = _mm_add_ps(
z0,
_mm_add_ps(_mm_mul_ps(cos_2pi_5, a1), _mm_mul_ps(cos_4pi_5, a2)),
);
let c2 = _mm_add_ps(
z0,
_mm_add_ps(_mm_mul_ps(cos_4pi_5, a1), _mm_mul_ps(cos_2pi_5, a2)),
);
let d1 = _mm_add_ps(_mm_mul_ps(sin_2pi_5, b1), _mm_mul_ps(sin_4pi_5, b2));
let d2 = _mm_sub_ps(_mm_mul_ps(sin_4pi_5, b1), _mm_mul_ps(sin_2pi_5, b2));
let out0 = _mm_add_ps(z0, sum_all);
let out1 = _mm_add_ps(c1, d1);
let out4 = _mm_sub_ps(c1, d1);
let out2 = _mm_add_ps(c2, d2);
let out3 = _mm_sub_ps(c2, d2);
let j0 = 5 * i - 4 * k0;
let j1 = 5 * (i + 1) - 4 * k1;
let out0_pd = _mm_castps_pd(out0);
let out1_pd = _mm_castps_pd(out1);
let out2_pd = _mm_castps_pd(out2);
let out3_pd = _mm_castps_pd(out3);
let out4_pd = _mm_castps_pd(out4);
let dst_ptr = dst.as_mut_ptr() as *mut f64;
_mm_storel_pd(dst_ptr.add(j0), out0_pd);
_mm_storel_pd(dst_ptr.add(j0 + stride), out1_pd);
_mm_storel_pd(dst_ptr.add(j0 + stride * 2), out2_pd);
_mm_storel_pd(dst_ptr.add(j0 + stride * 3), out3_pd);
_mm_storel_pd(dst_ptr.add(j0 + stride * 4), out4_pd);
_mm_storeh_pd(dst_ptr.add(j1), out0_pd);
_mm_storeh_pd(dst_ptr.add(j1 + stride), out1_pd);
_mm_storeh_pd(dst_ptr.add(j1 + stride * 2), out2_pd);
_mm_storeh_pd(dst_ptr.add(j1 + stride * 3), out3_pd);
_mm_storeh_pd(dst_ptr.add(j1 + stride * 4), out4_pd);
}
}
super::butterfly_radix5_scalar::<2>(src, dst, stage_twiddles, stride, simd_iters);
}