lambdaworks_math/fft/
errors.rs

1use core::fmt::Display;
2
3use crate::field::errors::FieldError;
4
5#[cfg(feature = "metal")]
6use lambdaworks_gpu::metal::abstractions::errors::MetalError;
7
8#[cfg(feature = "cuda")]
9use lambdaworks_gpu::cuda::abstractions::errors::CudaError;
10
11#[derive(Debug)]
12pub enum FFTError {
13    RootOfUnityError(u64),
14    InputError(usize),
15    OrderError(u64),
16    #[cfg(feature = "metal")]
17    MetalError(MetalError),
18    #[cfg(feature = "cuda")]
19    CudaError(CudaError),
20}
21
22impl Display for FFTError {
23    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
24        match self {
25            FFTError::RootOfUnityError(_) => write!(f, "Could not calculate root of unity"),
26            FFTError::InputError(v) => {
27                write!(f, "Input length is {v}, which is not a power of two")
28            }
29            FFTError::OrderError(v) => {
30                write!(f, "Order should be less than or equal to 63, but is {v}")
31            }
32            #[cfg(feature = "metal")]
33            FFTError::MetalError(_) => {
34                write!(f, "A Metal related error has ocurred")
35            }
36            #[cfg(feature = "cuda")]
37            FFTError::CudaError(_) => {
38                write!(f, "A CUDA related error has ocurred")
39            }
40        }
41    }
42}
43
44#[cfg(feature = "std")]
45impl std::error::Error for FFTError {
46    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
47        match self {
48            #[cfg(feature = "metal")]
49            FFTError::MetalError(e) => Some(e),
50            #[cfg(feature = "cuda")]
51            FFTError::CudaError(_) => Some(e),
52            _ => None,
53        }
54    }
55}
56
57#[cfg(feature = "metal")]
58impl From<MetalError> for FFTError {
59    fn from(error: MetalError) -> Self {
60        Self::MetalError(error)
61    }
62}
63
64#[cfg(feature = "cuda")]
65impl From<CudaError> for FFTError {
66    fn from(error: CudaError) -> Self {
67        Self::CudaError(error)
68    }
69}
70
71impl From<FieldError> for FFTError {
72    fn from(error: FieldError) -> Self {
73        match error {
74            FieldError::DivisionByZero => {
75                panic!("Can't divide by zero during FFT");
76            }
77            FieldError::InvZeroError => {
78                panic!("Can't calculate inverse of zero during FFT");
79            }
80            FieldError::RootOfUnityError(order) => FFTError::RootOfUnityError(order),
81        }
82    }
83}