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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#![cfg_attr(feature = "cargo-clippy", feature(tool_lints))]
#![cfg_attr(
feature = "cargo-clippy",
allow(clippy::new_without_default_derive)
)]
extern crate arrayvec;
#[cfg(any(feature = "doc", feature = "use_gpu"))]
extern crate clfft;
#[cfg(feature = "std")]
extern crate crossbeam;
#[cfg(feature = "std")]
#[macro_use]
extern crate lazy_static;
#[cfg(feature = "std")]
extern crate linreg;
extern crate num_complex;
#[cfg(feature = "std")]
extern crate num_cpus;
extern crate num_traits;
#[cfg(any(feature = "doc", feature = "use_gpu"))]
extern crate ocl;
extern crate rustfft;
#[cfg(any(feature = "doc", feature = "use_sse", feature = "use_avx"))]
extern crate simd;
#[cfg(feature = "std")]
extern crate time;
#[macro_use]
mod simd_extensions;
pub mod conv_types;
mod multicore_support;
mod vector_types;
pub mod window_functions;
pub use multicore_support::print_calibration;
pub use multicore_support::MultiCoreSettings;
pub use vector_types::*;
mod gpu_support;
mod inline_vector;
use numbers::*;
use std::ops::Range;
pub mod numbers {
use gpu_support::{Gpu32, Gpu64, GpuFloat, GpuRegTrait};
pub use num_complex::Complex;
use num_traits;
pub use num_traits::Float;
pub use num_traits::Num;
pub use num_traits::One;
use rustfft;
#[cfg(any(
feature = "use_sse",
feature = "use_avx",
feature = "use_avx512"
))]
use simd;
use simd_extensions;
use simd_extensions::*;
use std::fmt::Debug;
pub trait DspNumber:
Num
+ Copy
+ Clone
+ Send
+ Sync
+ ToSimd
+ Debug
+ num_traits::Signed
+ num_traits::FromPrimitive
+ rustfft::FFTnum
+ 'static
{
}
impl<T> DspNumber for T where
T: Num
+ Copy
+ Clone
+ Send
+ Sync
+ ToSimd
+ Debug
+ num_traits::Signed
+ num_traits::FromPrimitive
+ rustfft::FFTnum
+ 'static
{}
pub trait ToSimd: Sized + Sync + Send {
type RegFallback: SimdGeneric<Self>;
type RegSse: SimdGeneric<Self>;
type RegAvx: SimdGeneric<Self>;
type RegAvx512: SimdGeneric<Self>;
type GpuReg: GpuRegTrait;
}
impl ToSimd for f32 {
type RegFallback = simd_extensions::fallback::f32x4;
#[cfg(feature = "use_sse")]
type RegSse = simd::f32x4;
#[cfg(not(feature = "use_sse"))]
type RegSse = simd_extensions::fallback::f32x4;
#[cfg(feature = "use_avx")]
type RegAvx = simd::x86::avx::f32x8;
#[cfg(not(feature = "use_avx"))]
type RegAvx = simd_extensions::fallback::f32x4;
#[cfg(feature = "use_avx512")]
type RegAvx512 = stdsimd::simd::f32x16;
#[cfg(not(feature = "use_avx512"))]
type RegAvx512 = simd_extensions::fallback::f32x4;
type GpuReg = Gpu32;
}
impl ToSimd for f64 {
type RegFallback = simd_extensions::fallback::f64x2;
#[cfg(feature = "use_sse")]
type RegSse = simd::x86::sse2::f64x2;
#[cfg(not(feature = "use_sse"))]
type RegSse = simd_extensions::fallback::f64x2;
#[cfg(feature = "use_avx")]
type RegAvx = simd::x86::avx::f64x4;
#[cfg(not(feature = "use_avx"))]
type RegAvx = simd_extensions::fallback::f64x2;
#[cfg(feature = "use_avx512")]
type RegAvx512 = stdsimd::simd::f64x8;
#[cfg(not(feature = "use_avx512"))]
type RegAvx512 = simd_extensions::fallback::f64x2;
type GpuReg = Gpu64;
}
pub trait RealNumber: Float + DspNumber + GpuFloat + num_traits::FloatConst {}
impl<T> RealNumber for T where T: Float + DspNumber + GpuFloat + num_traits::FloatConst {}
pub trait Zero {
fn zero() -> Self;
}
impl<T> Zero for T
where
T: DspNumber,
{
fn zero() -> Self {
<Self as num_traits::Zero>::zero()
}
}
impl<T> Zero for Complex<T>
where
T: DspNumber,
{
fn zero() -> Self {
<Self as num_traits::Zero>::zero()
}
}
}
fn transmute_slice<S, D>(source: &[S]) -> &[D] {
let len = get_target_slice_len::<S, D>(source);
unsafe {
let trans: &[D] = &*(source as *const [S] as *const [D]);
std::slice::from_raw_parts(trans.as_ptr(), len)
}
}
fn transmute_slice_mut<S, D>(source: &mut [S]) -> &mut [D] {
let len = get_target_slice_len::<S, D>(source);
unsafe {
let trans: &mut [D] = &mut *(source as *mut [S] as *mut [D]);
std::slice::from_raw_parts_mut(trans.as_mut_ptr(), len)
}
}
fn get_target_slice_len<S, D>(source: &[S]) -> usize {
let to_larger_type = std::mem::size_of::<D>() >= std::mem::size_of::<S>();
if to_larger_type {
assert_eq!(std::mem::size_of::<D>() % std::mem::size_of::<S>(), 0);
let ratio = std::mem::size_of::<D>() / std::mem::size_of::<S>();
assert_eq!(source.len() % ratio, 0);
source.len() / ratio
} else {
assert_eq!(std::mem::size_of::<S>() % std::mem::size_of::<D>(), 0);
let ratio = std::mem::size_of::<S>() / std::mem::size_of::<D>();
source.len() * ratio
}
}
fn array_to_complex<T>(array: &[T]) -> &[Complex<T>] {
transmute_slice(array)
}
fn array_to_complex_mut<T>(array: &mut [T]) -> &mut [Complex<T>] {
transmute_slice_mut(array)
}
fn memcpy<T: Copy>(data: &mut [T], from: Range<usize>, to: usize) {
use std::ptr::copy;
assert!(from.start <= from.end);
assert!(from.end <= data.len());
assert!(to <= data.len() - (from.end - from.start));
unsafe {
let ptr = data.as_mut_ptr();
copy(ptr.add(from.start), ptr.add(to), from.end - from.start)
}
}
fn memzero<T: Copy>(data: &mut [T], range: Range<usize>) {
use std::ptr::write_bytes;
assert!(range.start <= range.end);
assert!(range.end <= data.len());
unsafe {
let ptr = data.as_mut_ptr();
write_bytes(ptr.add(range.start), 0, range.end - range.start);
}
}
#[cfg(test)]
mod tests {
use super::*;
use simd_extensions::Simd;
#[test]
fn to_simd_test() {
let reg = <f32 as ToSimd>::RegFallback::splat(1.0);
let sum = reg.sum_real();
assert!(sum > 0.0);
}
}