1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use ffi;
use types::*;
use num_traits::Zero;
use std::ops::{Deref, DerefMut};
use std::os::raw::c_void;
use std::slice::{from_raw_parts, from_raw_parts_mut};
#[derive(Debug)]
pub struct AlignedVec<T> {
n: usize,
data: *mut T,
}
pub trait AlignedAllocable: Zero + Clone + Copy + Sized {
unsafe fn alloc(n: usize) -> *mut Self;
}
impl AlignedAllocable for f64 {
unsafe fn alloc(n: usize) -> *mut Self {
ffi::fftw_alloc_real(n)
}
}
impl AlignedAllocable for f32 {
unsafe fn alloc(n: usize) -> *mut Self {
ffi::fftwf_alloc_real(n)
}
}
impl AlignedAllocable for c64 {
unsafe fn alloc(n: usize) -> *mut Self {
ffi::fftw_alloc_complex(n)
}
}
impl AlignedAllocable for c32 {
unsafe fn alloc(n: usize) -> *mut Self {
ffi::fftwf_alloc_complex(n)
}
}
impl<T> AlignedVec<T> {
pub fn as_slice(&self) -> &[T] {
unsafe { from_raw_parts(self.data, self.n) }
}
pub fn as_slice_mut(&mut self) -> &mut [T] {
unsafe { from_raw_parts_mut(self.data, self.n) }
}
}
impl<T> Deref for AlignedVec<T> {
type Target = [T];
fn deref(&self) -> &[T] {
self.as_slice()
}
}
impl<T> DerefMut for AlignedVec<T> {
fn deref_mut(&mut self) -> &mut [T] {
self.as_slice_mut()
}
}
impl<T> AlignedVec<T>
where
T: AlignedAllocable,
{
pub fn new(n: usize) -> Self {
let ptr = excall! { T::alloc(n) };
let mut vec = AlignedVec { n: n, data: ptr };
for v in vec.iter_mut() {
*v = T::zero();
}
vec
}
}
impl<T> Drop for AlignedVec<T> {
fn drop(&mut self) {
excall! { ffi::fftw_free(self.data as *mut c_void) };
}
}
impl<T> Clone for AlignedVec<T>
where
T: AlignedAllocable,
{
fn clone(&self) -> Self {
let mut new_vec = Self::new(self.n);
new_vec.copy_from_slice(self);
new_vec
}
}
pub type Alignment = i32;
pub fn alignment_of<T>(a: &[T]) -> Alignment {
unsafe { ffi::fftw_alignment_of(a.as_ptr() as *mut _) }
}