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
//! This module defines the API of portable vector types.
//!
//! # API
//!
//! ## Traits
//!
//! All portable vector types implement the following traits:
//!
//! * [x] `Copy`,
//! * [x] `Clone`,
//! * [x] `Debug`,
//! * [x] `Default`
//! * [x] `PartialEq`
//! * [x] `PartialOrd` (TODO: re-write in term of
//!        comparison operations and boolean reductions),
//!
//! Non-floating-point vector types also implement:
//!
//! * [x] `Hash`,
//! * [x] `Eq`, and
//! * [x] `Ord`.
//!
//! Integer vector types also implement:
//!
//! * [x] `fmt::LowerHex`.
//!
//! ## Conversions
//!
//! * [x]: `FromBits/IntoBits`: bitwise lossless transmutes between vectors of
//!        the same size (i.e., same `mem::size_of`).
//! * [x]: `From/Into`: casts between vectors with the same number of lanes
//!        (potentially lossy).
//!
//! ## Inherent methods
//!
//! * [x] minimal API: implemented by all vector types except for boolean
//!       vectors.
//! * [x] minimal boolean vector API: implemented by boolean vectors.
//! * [x] load/store API: aligned and unaligned memory loads and
//!       stores - implemented by all vectors.
//! * [x] comparison API: vector lane-wise comparison producing
//!       boolean vectors - implemented by all vectors.
//! * [x] arithmetic operations: implemented by all non-boolean vectors.
//! * [x] `std::ops::Neg`: implemented by signed-integer and floating-point
//!       vectors.
//! * [x] bitwise operations: implemented by integer and boolean
//!       vectors.
//! * [x] shift operations: implemented by integer vectors.
//! * [x] arithmetic reductions: implemented by integer and floating-point
//!       vectors.
//! * [x] bitwise reductions: implemented by integer and boolean
//!       vectors.
//! * [x] boolean reductions: implemented by boolean vectors.
//! * [ ] portable shuffles: `shufflevector`.
//! * [ ] portable `gather`/`scatter`:

/// Adds the vector type `$id`, with elements of types `$elem_tys`.
macro_rules! define_ty {
    ($id:ident, $($elem_tys:ident),+ | $(#[$doc:meta])*) => {
        $(#[$doc])*
            #[repr(simd)]
        #[derive(Copy, Debug, /*FIXME:*/ PartialOrd)]
        #[allow(non_camel_case_types)]
        pub struct $id($($elem_tys),*);
    }
}

#[macro_use]
mod arithmetic_ops;
#[macro_use]
mod arithmetic_reductions;
#[macro_use]
mod bitwise_ops;
#[macro_use]
mod bitwise_reductions;
#[macro_use]
mod boolean_reductions;
#[macro_use]
mod bool_vectors;
#[macro_use]
mod cmp;
#[macro_use]
mod default;
#[macro_use]
mod eq;
#[macro_use]
mod fmt;
#[macro_use]
mod from;
#[macro_use]
mod from_bits;
#[macro_use]
mod hash;
#[macro_use]
mod load_store;
#[macro_use]
mod minimal;
#[macro_use]
mod minmax_reductions;
#[macro_use]
mod neg;
#[macro_use]
mod partial_eq;
// TODO:
//#[macro_use]
//mod partial_ord;
// TODO:
//#[macro_use]
//mod shuffles;
// TODO:
//#[macro_use]
//mod gather_scatter;
#[macro_use]
mod shifts;

/// Imports required to implement vector types using the macros.

macro_rules! simd_api_imports {
    () => {
        use ::coresimd::simd_llvm::*;
        use fmt;
        use hash;
        use ops;
        #[allow(unused_imports)]
        use num;
        use cmp::{Eq, PartialEq};
        use ptr;
        use mem;
        #[allow(unused_imports)]
        use convert::{From, Into};
        use slice::SliceExt;
        #[allow(unused_imports)]
        use iter::Iterator;
        #[allow(unused_imports)]
        use default::Default;
        use clone::Clone;
        use super::codegen::sum::{ReduceAdd};
        use super::codegen::product::{ReduceMul};
        use super::codegen::and::{ReduceAnd};
        use super::codegen::or::{ReduceOr};
        use super::codegen::xor::{ReduceXor};
        use super::codegen::min::{ReduceMin};
        use super::codegen::max::{ReduceMax};
    }
}

/// Defines a portable packed SIMD floating-point vector type.
macro_rules! simd_f_ty {
    ($id:ident : $elem_count:expr, $elem_ty:ident, $bool_ty:ident, $test_mod:ident |
     $($elem_tys:ident),+ | $($elem_name:ident),+ | $(#[$doc:meta])*) => {
        define_ty!($id, $($elem_tys),+ | $(#[$doc])*);
        impl_minimal!($id, $elem_ty, $elem_count, $($elem_name),*);
        impl_load_store!($id, $elem_ty, $elem_count);
        impl_cmp!($id, $bool_ty);
        impl_arithmetic_ops!($id);
        impl_arithmetic_reductions!($id, $elem_ty);
        impl_minmax_reductions!($id, $elem_ty);
        impl_neg_op!($id, $elem_ty);
        impl_partial_eq!($id);
        impl_default!($id, $elem_ty);

        #[cfg(test)]
        mod $test_mod {
            test_minimal!($id, $elem_ty, $elem_count);
            test_load_store!($id, $elem_ty);
            test_cmp!($id, $elem_ty, $bool_ty, 1. as $elem_ty, 0. as $elem_ty);
            test_arithmetic_ops!($id, $elem_ty);
            test_arithmetic_reductions!($id, $elem_ty);
            test_minmax_reductions!($id, $elem_ty);
            test_neg_op!($id, $elem_ty);
            test_partial_eq!($id, 1. as $elem_ty, 0. as $elem_ty);
            test_default!($id, $elem_ty);
        }
    }
}

/// Defines a portable packed SIMD signed-integer vector type.
macro_rules! simd_i_ty {
    ($id:ident : $elem_count:expr, $elem_ty:ident, $bool_ty:ident, $test_mod:ident |
     $($elem_tys:ident),+ | $($elem_name:ident),+ | $(#[$doc:meta])*) => {
        define_ty!($id, $($elem_tys),+ | $(#[$doc])*);
        impl_minimal!($id, $elem_ty, $elem_count, $($elem_name),*);
        impl_load_store!($id, $elem_ty, $elem_count);
        impl_cmp!($id, $bool_ty);
        impl_hash!($id, $elem_ty);
        impl_arithmetic_ops!($id);
        impl_arithmetic_reductions!($id, $elem_ty);
        impl_minmax_reductions!($id, $elem_ty);
        impl_neg_op!($id, $elem_ty);
        impl_bitwise_ops!($id, !(0 as $elem_ty));
        impl_bitwise_reductions!($id, $elem_ty);
        impl_all_shifts!($id, $elem_ty);
        impl_hex_fmt!($id, $elem_ty);
        impl_eq!($id);
        impl_partial_eq!($id);
        impl_default!($id, $elem_ty);

        #[cfg(test)]
        mod $test_mod {
            test_minimal!($id, $elem_ty, $elem_count);
            test_load_store!($id, $elem_ty);
            test_cmp!($id, $elem_ty, $bool_ty, 1 as $elem_ty, 0 as $elem_ty);
            test_hash!($id, $elem_ty);
            test_arithmetic_ops!($id, $elem_ty);
            test_arithmetic_reductions!($id, $elem_ty);
            test_minmax_reductions!($id, $elem_ty);
            test_neg_op!($id, $elem_ty);
            test_int_bitwise_ops!($id, $elem_ty);
            test_bitwise_reductions!($id, !(0 as $elem_ty));
            test_all_shift_ops!($id, $elem_ty);
            test_hex_fmt!($id, $elem_ty);
            test_partial_eq!($id, 1 as $elem_ty, 0 as $elem_ty);
            test_default!($id, $elem_ty);
        }
    }
}

/// Defines a portable packed SIMD unsigned-integer vector type.
macro_rules! simd_u_ty {
    ($id:ident : $elem_count:expr, $elem_ty:ident, $bool_ty:ident, $test_mod:ident |
     $($elem_tys:ident),+ | $($elem_name:ident),+ | $(#[$doc:meta])*) => {
        define_ty!($id, $($elem_tys),+ | $(#[$doc])*);
        impl_minimal!($id, $elem_ty, $elem_count, $($elem_name),*);
        impl_load_store!($id, $elem_ty, $elem_count);
        impl_cmp!($id, $bool_ty);
        impl_hash!($id, $elem_ty);
        impl_arithmetic_ops!($id);
        impl_arithmetic_reductions!($id, $elem_ty);
        impl_minmax_reductions!($id, $elem_ty);
        impl_bitwise_ops!($id, !(0 as $elem_ty));
        impl_bitwise_reductions!($id, $elem_ty);
        impl_all_shifts!($id, $elem_ty);
        impl_hex_fmt!($id, $elem_ty);
        impl_eq!($id);
        impl_partial_eq!($id);
        impl_default!($id, $elem_ty);

        #[cfg(test)]
        mod $test_mod {
            test_minimal!($id, $elem_ty, $elem_count);
            test_load_store!($id, $elem_ty);
            test_cmp!($id, $elem_ty, $bool_ty, 1 as $elem_ty, 0 as $elem_ty);
            test_hash!($id, $elem_ty);
            test_arithmetic_ops!($id, $elem_ty);
            test_arithmetic_reductions!($id, $elem_ty);
            test_minmax_reductions!($id, $elem_ty);
            test_int_bitwise_ops!($id, $elem_ty);
            test_bitwise_reductions!($id, !(0 as $elem_ty));
            test_all_shift_ops!($id, $elem_ty);
            test_hex_fmt!($id, $elem_ty);
            test_partial_eq!($id, 1 as $elem_ty, 0 as $elem_ty);
            test_default!($id, $elem_ty);
        }
    }
}

/// Defines a portable packed SIMD boolean vector type.
macro_rules! simd_b_ty {
    ($id:ident : $elem_count:expr, $elem_ty:ident, $test_mod:ident |
     $($elem_tys:ident),+ | $($elem_name:ident),+ | $(#[$doc:meta])*) => {
        define_ty!($id, $($elem_tys),+ | $(#[$doc])*);
        impl_bool_minimal!($id, $elem_ty, $elem_count, $($elem_name),*);
        impl_bitwise_ops!($id, true);
        impl_bool_bitwise_reductions!($id, bool);
        impl_bool_reductions!($id);
        impl_bool_cmp!($id, $id);
        impl_eq!($id);
        impl_partial_eq!($id);
        impl_default!($id, bool);

        #[cfg(test)]
        mod $test_mod {
            test_bool_minimal!($id, $elem_count);
            test_bool_bitwise_ops!($id);
            test_bool_reductions!($id);
            test_bitwise_reductions!($id, true);
            test_cmp!($id, $elem_ty, $id, true, false);
            test_partial_eq!($id, true, false);
            test_default!($id, bool);
        }
    }
}