use crate::PxdctError;
use crate::twiddles::FftTrigonometry;
use num_complex::Complex;
use num_traits::{AsPrimitive, Float, MulAdd};
use std::fmt::{Debug, Display};
use std::ops::{Add, AddAssign, BitXor, Mul, MulAssign, Neg};
use std::sync::Arc;
use zaft::{FftDirection, FftExecutor, R2CFftExecutor, Zaft};
#[inline]
pub(crate) fn mixed_radix3_twiddles<T: DctSample + FftTrigonometry + 'static, const N: usize>(
len: usize,
) -> [Complex<T>; N]
where
f64: AsPrimitive<T>,
{
let mut inner_layer = [Complex::<T>::default(); N];
for (i, layer) in inner_layer.chunks_exact_mut(2).enumerate() {
layer[0] = mixed_radix_inner_twiddle(2f64.as_() * (i as f64).as_() + 1f64.as_(), len);
layer[0].im *= T::SQRT_3;
layer[1] = mixed_radix_inner_twiddle(
2f64.as_() * (2f64.as_() * (i as f64).as_() + 1f64.as_()),
len,
);
layer[1].im = -layer[1].im * T::SQRT_3;
}
inner_layer
}
#[inline]
pub(crate) fn mixed_radix_inner_twiddle<T: Float + FftTrigonometry + 'static>(
angle: T,
fft_len: usize,
) -> Complex<T>
where
f64: AsPrimitive<T>,
{
let angle = angle / ((2 * fft_len) as f64).as_();
let (v_sin, v_cos) = angle.sincos_pi();
Complex {
re: v_cos,
im: v_sin,
}
}
pub(crate) trait DctSample:
FftTrigonometry
+ Float
+ Copy
+ 'static
+ Clone
+ Default
+ FftProvider<Self>
+ Debug
+ MulAdd<Self, Output = Self>
+ Add<Self, Output = Self>
+ Mul<Self, Output = Self>
+ FftSpectrumMulFactory<Self>
+ DctConstants
+ MixedRadix7Sample
+ MixedRadix5Sample
+ Neg<Output = Self>
+ TransposeFactory
+ AddAssign
+ Display
+ MixedRadix9Sample
+ MixedRadix17Sample
+ MixedRadix13Sample
+ MixedRadix19Sample
+ MixedRadix29Sample
+ MixedRadix23Sample
+ MulAssign
{
fn mulsigni(self, other: isize) -> Self;
fn mulsign(self, other: Self) -> Self;
}
trait PointerToSinglePrecisionSize {
fn to_single_precision_size(self) -> Self;
}
#[cfg(target_pointer_width = "64")]
impl PointerToSinglePrecisionSize for isize {
#[inline(always)]
fn to_single_precision_size(self) -> Self {
self >> 32
}
}
#[cfg(target_pointer_width = "32")]
impl PointerToSinglePrecisionSize for isize {
#[inline(always)]
fn to_single_precision_size(self) -> Self {
self
}
}
impl DctSample for f32 {
#[inline(always)]
fn mulsigni(self, other: isize) -> Self {
let s_prec_size = other.to_single_precision_size();
f32::from_bits(self.to_bits().bitxor((s_prec_size & (1isize << 31)) as u32))
}
#[inline(always)]
fn mulsign(self, other: Self) -> Self {
f32::from_bits(self.to_bits().bitxor(other.to_bits() & (1u32 << 31)))
}
}
impl DctSample for f64 {
#[inline(always)]
fn mulsigni(self, other: isize) -> Self {
let s_prec_size = other.to_single_precision_size();
f64::from_bits(
self.to_bits()
.bitxor(((s_prec_size as i64) & (1i64 << 63)) as u64),
)
}
#[inline(always)]
fn mulsign(self, other: Self) -> Self {
f64::from_bits(self.to_bits().bitxor(other.to_bits() & (1u64 << 63)))
}
}
pub(crate) trait DctConstants {
const HALF: Self;
const TWO: Self;
const SQRT_2: Self;
const SQRT_3: Self;
const FRAC_1_SQRT_2: Self;
const SQRT_3_OVER_2: Self;
}
impl DctConstants for f32 {
const HALF: Self = 0.5;
const TWO: Self = 2.;
const SQRT_2: Self = std::f32::consts::SQRT_2;
const SQRT_3: Self = f32::from_bits(0x3fddb3d7);
const FRAC_1_SQRT_2: Self = std::f32::consts::FRAC_1_SQRT_2;
const SQRT_3_OVER_2: Self = f32::from_bits(0x3f5db3d7);
}
impl DctConstants for f64 {
const HALF: Self = 0.5;
const TWO: Self = 2.;
const SQRT_2: Self = std::f64::consts::SQRT_2;
const SQRT_3: Self = f64::from_bits(0x3ffbb67ae8584caa);
const FRAC_1_SQRT_2: Self = std::f64::consts::FRAC_1_SQRT_2;
const SQRT_3_OVER_2: Self = f64::from_bits(0x3febb67ae8584caa);
}
pub(crate) trait FftProvider<T> {
fn make_fft(
n: usize,
direction: FftDirection,
) -> Result<Arc<dyn FftExecutor<T> + Send + Sync>, PxdctError>;
fn make_fft_r2c(n: usize) -> Result<Arc<dyn R2CFftExecutor<T> + Send + Sync>, PxdctError>;
}
impl FftProvider<f32> for f32 {
fn make_fft(
n: usize,
direction: FftDirection,
) -> Result<Arc<dyn FftExecutor<f32> + Send + Sync>, PxdctError> {
match direction {
FftDirection::Forward => Zaft::make_forward_fft_f32(n)
.map_err(|x| PxdctError::CantCreateUnderlyingFft(x.to_string())),
FftDirection::Inverse => Zaft::make_inverse_fft_f32(n)
.map_err(|x| PxdctError::CantCreateUnderlyingFft(x.to_string())),
}
}
fn make_fft_r2c(n: usize) -> Result<Arc<dyn R2CFftExecutor<f32> + Send + Sync>, PxdctError> {
Zaft::make_r2c_fft_f32(n).map_err(|x| PxdctError::CantCreateUnderlyingFft(x.to_string()))
}
}
impl FftProvider<f64> for f64 {
fn make_fft(
n: usize,
direction: FftDirection,
) -> Result<Arc<dyn FftExecutor<f64> + Send + Sync>, PxdctError> {
match direction {
FftDirection::Forward => Zaft::make_forward_fft_f64(n)
.map_err(|x| PxdctError::CantCreateUnderlyingFft(x.to_string())),
FftDirection::Inverse => Zaft::make_inverse_fft_f64(n)
.map_err(|x| PxdctError::CantCreateUnderlyingFft(x.to_string())),
}
}
fn make_fft_r2c(n: usize) -> Result<Arc<dyn R2CFftExecutor<f64> + Send + Sync>, PxdctError> {
Zaft::make_r2c_fft_f64(n).map_err(|x| PxdctError::CantCreateUnderlyingFft(x.to_string()))
}
}
macro_rules! try_vec {
() => {
Vec::new()
};
($elem:expr; $n:expr) => {{
let mut v = Vec::new();
v.try_reserve_exact($n)
.map_err(|_| crate::PxdctError::OutOfMemory($n))?;
v.resize($n, $elem);
v
}};
}
use crate::spectrum_mul::FftSpectrumMulFactory;
pub(crate) use try_vec;
#[inline]
#[cfg(all(target_arch = "x86_64", feature = "avx"))]
pub(crate) fn has_valid_avx() -> bool {
std::arch::is_x86_feature_detected!("avx2") && std::arch::is_x86_feature_detected!("fma")
}
macro_rules! create_dct2_3 {
($clazz: ident) => {
impl<T: DctSample> $clazz<T>
where
f64: AsPrimitive<T>,
{
pub(crate) fn new(len: usize) -> Result<$clazz<T>, PxdctError> {
let fft = T::make_fft(len, FftDirection::Forward)?;
use crate::twiddles::compute_twiddle;
let mut twiddles = try_vec![Complex::<T>::default(); len];
for (i, twiddle) in twiddles.iter_mut().enumerate() {
*twiddle = compute_twiddle::<T>(i, len * 4);
}
Ok($clazz {
twiddles,
fft_executor: fft,
execution_length: len,
spectrum_mul: T::create_mul_spectrum_to_real(),
})
}
}
};
}
macro_rules! create_dct2_3_real {
($clazz: ident) => {
impl<T: DctSample> $clazz<T>
where
f64: AsPrimitive<T>,
{
pub(crate) fn new(len: usize) -> Result<$clazz<T>, PxdctError> {
let fft = T::make_fft_r2c(len)?;
use crate::twiddles::compute_twiddle;
let mut twiddles = try_vec![Complex::<T>::default(); len];
for (i, twiddle) in twiddles.iter_mut().enumerate() {
*twiddle = compute_twiddle::<T>(i, len * 4);
}
Ok($clazz {
twiddles,
fft_executor: fft,
execution_length: len,
spectrum_mul: T::create_mul_spectrum_to_real(),
})
}
}
};
}
use crate::butterflies::MixedRadix9Sample;
use crate::dct2::prime_butterflies::{
MixedRadix13Sample, MixedRadix17Sample, MixedRadix19Sample, MixedRadix23Sample,
MixedRadix29Sample,
};
use crate::dct2::{MixedRadix5Sample, MixedRadix7Sample};
use crate::transpose::TransposeFactory;
pub(crate) use create_dct2_3;
pub(crate) use create_dct2_3_real;