Skip to main content

poulpy_cpu_ref/reference/fft64/
module.rs

1use std::fmt::Debug;
2
3use rand_distr::num_traits::{Float, FloatConst};
4
5use crate::{
6    layouts::{Backend, Module},
7    reference::fft64::reim::{ReimFFTTable, ReimIFFTTable},
8};
9
10/// Access to the precomputed FFT/iFFT tables stored inside a `Module<B>` handle.
11///
12/// Backend crates implement [`FFTHandleProvider`] for their concrete handle type.
13/// `poulpy-hal` then provides this blanket trait on `Module<B>`, which lets family
14/// defaults share the same FFT64 handle contract across scalar and accelerated backends.
15pub trait FFTModuleHandle<F>
16where
17    F: Float + FloatConst + Debug,
18{
19    fn get_fft_table(&self) -> &ReimFFTTable<F>;
20    fn get_ifft_table(&self) -> &ReimIFFTTable<F>;
21}
22
23/// Implemented by FFT64 backend handle types that own precomputed FFT tables.
24///
25/// # Safety
26///
27/// Implementors must return references that stay valid for the lifetime of `&self`.
28/// The handle must be fully initialized before `Module::new()` returns.
29pub unsafe trait FFTHandleProvider<F>
30where
31    F: Float + FloatConst + Debug,
32{
33    fn get_fft_table(&self) -> &ReimFFTTable<F>;
34    fn get_ifft_table(&self) -> &ReimIFFTTable<F>;
35}
36
37/// Construct FFT64 backend handles for [`Module::new`](crate::api::ModuleNew::new).
38///
39/// # Safety
40///
41/// Implementors must return a fully initialized handle for the requested `n`.
42/// The handle is boxed and stored inside the `Module`, so it must be safe to
43/// drop via [`crate::layouts::Backend::destroy`].
44pub unsafe trait FFT64HandleFactory: Sized {
45    /// Builds a fully initialized handle for ring dimension `n`.
46    fn create_fft64_handle(n: usize) -> Self;
47
48    /// Optional runtime capability check (default: no-op).
49    fn assert_fft64_runtime_support() {}
50}
51
52impl<BE: Backend> FFTModuleHandle<BE::ScalarPrep> for Module<BE>
53where
54    BE::ScalarPrep: Float + FloatConst + Debug,
55    BE::Handle: FFTHandleProvider<BE::ScalarPrep>,
56{
57    fn get_fft_table(&self) -> &ReimFFTTable<BE::ScalarPrep> {
58        unsafe { (&*self.ptr()).get_fft_table() }
59    }
60
61    fn get_ifft_table(&self) -> &ReimIFFTTable<BE::ScalarPrep> {
62        unsafe { (&*self.ptr()).get_ifft_table() }
63    }
64}