ffts/
complex.rs

1use libc::c_void;
2
3///32-bit aligned complex number (see
4///[ffts.h](https://github.com/sevagh/ffts/blob/master/include/ffts.h#L68))
5#[derive(Debug, Clone, Copy, PartialEq)]
6#[repr(align(4))] //32-bit alignment for ffts
7pub struct FFTSComplex {
8    pub re: f32,
9    pub im: f32,
10}
11
12impl FFTSComplex {
13    ///Convert a real [f32] to a [FFTSComplex] with the imaginary values set to 0
14    pub fn vec_from_real(real: &[f32]) -> Vec<Self> {
15        let mut ret: Vec<FFTSComplex> = Vec::with_capacity(real.len());
16
17        for x in real.iter() {
18            ret.push(FFTSComplex { re: *x, im: 0.0 });
19        }
20
21        ret
22    }
23
24    ///Get a real [f32] from the real values of a [FFTSComplex]
25    pub fn vec_to_real(complex: &[Self]) -> Vec<f32> {
26        let mut ret: Vec<f32> = Vec::with_capacity(complex.len());
27
28        for x in complex.iter() {
29            ret.push(x.re);
30        }
31
32        ret
33    }
34
35    pub(in crate) fn to_aligned_c_ptr(complex: &mut [Self]) -> *mut c_void {
36        complex.as_mut_ptr() as *mut c_void
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use std::mem;
44
45    #[test]
46    fn test_mem_alignment() {
47        assert_eq!(mem::align_of::<FFTSComplex>(), 4);
48        println!("alignof FFTSComplex: {:#?}", mem::align_of::<FFTSComplex>());
49        println!("sizeof FFTSComplex: {:#?}", mem::size_of::<FFTSComplex>());
50        println!("sizeof f32: {:#?}", mem::size_of::<f32>());
51    }
52}