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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! Implements portable horizontal vector min/max reductions.

macro_rules! impl_reduction_min_max {
    ([$elem_ty:ident; $elem_count:expr]: $id:ident
     | $ielem_ty:ident | $test_tt:tt) => {
        impl $id {
            /// Largest vector element value.
            #[inline]
            pub fn max_element(self) -> $elem_ty {
                #[cfg(not(any(
                    target_arch = "aarch64",
                    target_arch = "arm",
                    target_arch = "powerpc64",
                    target_arch = "wasm32",
                )))]
                {
                    use crate::llvm::simd_reduce_max;
                    let v: $ielem_ty = unsafe { simd_reduce_max(self.0) };
                    v as $elem_ty
                }
                #[cfg(any(
                    target_arch = "aarch64",
                    target_arch = "arm",
                    target_arch = "powerpc64",
                    target_arch = "wasm32",
                ))]
                {
                    // FIXME: broken on AArch64
                    // https://github.com/rust-lang-nursery/packed_simd/issues/15
                    // FIXME: broken on WASM32
                    // https://github.com/rust-lang-nursery/packed_simd/issues/91
                    let mut x = self.extract(0);
                    for i in 1..$id::lanes() {
                        x = x.max(self.extract(i));
                    }
                    x
                }
            }

            /// Smallest vector element value.
            #[inline]
            pub fn min_element(self) -> $elem_ty {
                #[cfg(not(any(
                    target_arch = "aarch64",
                    target_arch = "arm",
                    all(target_arch = "x86", not(target_feature = "sse2")),
                    target_arch = "powerpc64",
                    target_arch = "wasm32",
                ),))]
                {
                    use crate::llvm::simd_reduce_min;
                    let v: $ielem_ty = unsafe { simd_reduce_min(self.0) };
                    v as $elem_ty
                }
                #[cfg(any(
                    target_arch = "aarch64",
                    target_arch = "arm",
                    all(target_arch = "x86", not(target_feature = "sse2")),
                    target_arch = "powerpc64",
                    target_arch = "wasm32",
                ))]
                {
                    // FIXME: broken on AArch64
                    // https://github.com/rust-lang-nursery/packed_simd/issues/15
                    // FIXME: broken on i586-unknown-linux-gnu
                    // https://github.com/rust-lang-nursery/packed_simd/issues/22
                    // FIXME: broken on WASM32
                    // https://github.com/rust-lang-nursery/packed_simd/issues/91
                    let mut x = self.extract(0);
                    for i in 1..$id::lanes() {
                        x = x.min(self.extract(i));
                    }
                    x
                }
            }
        }
        test_if! {$test_tt:
        paste::item! {
            pub mod [<$id _reduction_min_max>] {
                use super::*;
                #[cfg_attr(not(target_arch = "wasm32"), test)]
                #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                pub fn max_element() {
                    let v = $id::splat(0 as $elem_ty);
                    assert_eq!(v.max_element(), 0 as $elem_ty);
                    if $id::lanes() > 1 {
                        let v = v.replace(1, 1 as $elem_ty);
                        assert_eq!(v.max_element(), 1 as $elem_ty);
                    }
                    let v = v.replace(0, 2 as $elem_ty);
                    assert_eq!(v.max_element(), 2 as $elem_ty);
                }

                #[cfg_attr(not(target_arch = "wasm32"), test)]
                #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                pub fn min_element() {
                    let v = $id::splat(0 as $elem_ty);
                    assert_eq!(v.min_element(), 0 as $elem_ty);
                    if $id::lanes() > 1 {
                        let v = v.replace(1, 1 as $elem_ty);
                        assert_eq!(v.min_element(), 0 as $elem_ty);
                    }
                    let v = $id::splat(1 as $elem_ty);
                    let v = v.replace(0, 2 as $elem_ty);
                    if $id::lanes() > 1 {
                        assert_eq!(v.min_element(), 1 as $elem_ty);
                    } else {
                        assert_eq!(v.min_element(), 2 as $elem_ty);
                    }
                    if $id::lanes() > 1 {
                        let v = $id::splat(2 as $elem_ty);
                        let v = v.replace(1, 1 as $elem_ty);
                        assert_eq!(v.min_element(), 1 as $elem_ty);
                    }
                }
            }
        }
        }
    };
}

macro_rules! test_reduction_float_min_max {
    ([$elem_ty:ident; $elem_count:expr]: $id:ident | $test_tt:tt) => {
        test_if!{
            $test_tt:
            paste::item! {
                pub mod [<$id _reduction_min_max_nan>] {
                    use super::*;
                    #[cfg_attr(not(target_arch = "wasm32"), test)]
                    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn min_element_test() {
                        let n = crate::$elem_ty::NAN;

                        assert_eq!(n.min(-3.), -3.);
                        assert_eq!((-3. as $elem_ty).min(n), -3.);

                        let v0 = $id::splat(-3.);

                        let target_with_broken_last_lane_nan = !cfg!(any(
                            target_arch = "arm", target_arch = "aarch64",
                            all(target_arch = "x86",
                                not(target_feature = "sse2")
                            ),
                            target_arch = "powerpc64",
                            target_arch = "wasm32",
                        ));

                        // The vector is initialized to `-3.`s: [-3, -3, -3, -3]
                        for i in 0..$id::lanes() {
                            // We replace the i-th element of the vector with
                            // `NaN`: [-3, -3, -3, NaN]
                            let mut v = v0.replace(i, n);

                            // If the NaN is in the last place, the LLVM
                            // implementation of these methods is broken on some
                            // targets:
                            if i == $id::lanes() - 1 &&
                                target_with_broken_last_lane_nan {
                                // FIXME:
                                // https://github.com/rust-lang-nursery/packed_simd/issues/5
                                //
                                // If there is a NaN, the result should always
                                // the smallest element, but currently when the
                                // last element is NaN the current
                                // implementation incorrectly returns NaN.
                                //
                                // The targets mentioned above use different
                                // codegen that produces the correct result.
                                //
                                // These asserts detect if this behavior changes
                                    assert!(v.min_element().is_nan(),
                                            // FIXME: ^^^ should be -3.
                                            "[A]: nan at {} => {} | {:?}",
                                            i, v.min_element(), v);

                                // If we replace all the elements in the vector
                                // up-to the `i-th` lane with `NaN`s, the result
                                // is still always `-3.` unless all elements of
                                // the vector are `NaN`s:
                                //
                                // This is also broken:
                                for j in 0..i {
                                    v = v.replace(j, n);
                                    assert!(v.min_element().is_nan(),
                                            // FIXME: ^^^ should be -3.
                                            "[B]: nan at {} => {} | {:?}",
                                            i, v.min_element(), v);
                                }

                                // We are done here, since we were in the last
                                // lane which is the last iteration of the loop.
                                break
                            }

                            // We are not in the last lane, and there is only
                            // one `NaN` in the vector.

                            // If the vector has one lane, the result is `NaN`:
                            if $id::lanes() == 1 {
                                assert!(v.min_element().is_nan(),
                                        "[C]: all nans | v={:?} | min={} | \
                                         is_nan: {}",
                                        v, v.min_element(),
                                        v.min_element().is_nan()
                                );

                                // And we are done, since the vector only has
                                // one lane anyways.
                                break;
                            }

                            // The vector has more than one lane, since there is
                            // only one `NaN` in the vector, the result is
                            // always `-3`.
                            assert_eq!(v.min_element(), -3.,
                                       "[D]: nan at {} => {} | {:?}",
                                       i, v.min_element(), v);

                            // If we replace all the elements in the vector
                            // up-to the `i-th` lane with `NaN`s, the result is
                            // still always `-3.` unless all elements of the
                            // vector are `NaN`s:
                            for j in 0..i {
                                v = v.replace(j, n);

                                if i == $id::lanes() - 1 && j == i - 1 {
                                    // All elements of the vector are `NaN`s,
                                    // therefore the result is NaN as well.
                                    //
                                    // Note: the #lanes of the vector is > 1, so
                                    // "i - 1" does not overflow.
                                    assert!(v.min_element().is_nan(),
                                            "[E]: all nans | v={:?} | min={} | \
                                             is_nan: {}",
                                            v, v.min_element(),
                                            v.min_element().is_nan());
                                } else {
                                    // There are non-`NaN` elements in the
                                    // vector, therefore the result is `-3.`:
                                    assert_eq!(v.min_element(), -3.,
                                               "[F]: nan at {} => {} | {:?}",
                                               i, v.min_element(), v);
                                }
                            }
                        }

                        // If the vector contains all NaNs the result is NaN:
                        assert!($id::splat(n).min_element().is_nan(),
                                "all nans | v={:?} | min={} | is_nan: {}",
                                $id::splat(n), $id::splat(n).min_element(),
                                $id::splat(n).min_element().is_nan());
                    }
                    #[cfg_attr(not(target_arch = "wasm32"), test)]
                    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn max_element_test() {
                        let n = crate::$elem_ty::NAN;

                        assert_eq!(n.max(-3.), -3.);
                        assert_eq!((-3. as $elem_ty).max(n), -3.);

                        let v0 = $id::splat(-3.);

                        let target_with_broken_last_lane_nan = !cfg!(any(
                            target_arch = "arm", target_arch = "aarch64",
                            target_arch = "powerpc64", target_arch = "wasm32",
                        ));

                        // The vector is initialized to `-3.`s: [-3, -3, -3, -3]
                        for i in 0..$id::lanes() {
                            // We replace the i-th element of the vector with
                            // `NaN`: [-3, -3, -3, NaN]
                            let mut v = v0.replace(i, n);

                            // If the NaN is in the last place, the LLVM
                            // implementation of these methods is broken on some
                            // targets:
                            if i == $id::lanes() - 1 &&
                              target_with_broken_last_lane_nan {
                                // FIXME:
                                // https://github.com/rust-lang-nursery/packed_simd/issues/5
                                //
                                // If there is a NaN, the result should
                                // always the largest element, but currently
                                // when the last element is NaN the current
                                // implementation incorrectly returns NaN.
                                //
                                // The targets mentioned above use different
                                // codegen that produces the correct result.
                                //
                                // These asserts detect if this behavior
                                // changes
                                assert!(v.max_element().is_nan(),
                                        // FIXME: ^^^ should be -3.
                                        "[A]: nan at {} => {} | {:?}",
                                        i, v.max_element(), v);

                                // If we replace all the elements in the vector
                                // up-to the `i-th` lane with `NaN`s, the result
                                // is still always `-3.` unless all elements of
                                // the vector are `NaN`s:
                                //
                                // This is also broken:
                                for j in 0..i {
                                    v = v.replace(j, n);
                                    assert!(v.max_element().is_nan(),
                                            // FIXME: ^^^ should be -3.
                                            "[B]: nan at {} => {} | {:?}",
                                            i, v.max_element(), v);
                                }

                                // We are done here, since we were in the last
                                // lane which is the last iteration of the loop.
                                break
                            }

                            // We are not in the last lane, and there is only
                            // one `NaN` in the vector.

                            // If the vector has one lane, the result is `NaN`:
                            if $id::lanes() == 1 {
                                assert!(v.max_element().is_nan(),
                                        "[C]: all nans | v={:?} | min={} | \
                                         is_nan: {}",
                                        v, v.max_element(),
                                        v.max_element().is_nan());

                                // And we are done, since the vector only has
                                // one lane anyways.
                                break;
                            }

                            // The vector has more than one lane, since there is
                            // only one `NaN` in the vector, the result is
                            // always `-3`.
                            assert_eq!(v.max_element(), -3.,
                                       "[D]: nan at {} => {} | {:?}",
                                       i, v.max_element(), v);

                            // If we replace all the elements in the vector
                            // up-to the `i-th` lane with `NaN`s, the result is
                            // still always `-3.` unless all elements of the
                            // vector are `NaN`s:
                            for j in 0..i {
                                v = v.replace(j, n);

                                if i == $id::lanes() - 1 && j == i - 1 {
                                    // All elements of the vector are `NaN`s,
                                    // therefore the result is NaN as well.
                                    //
                                    // Note: the #lanes of the vector is > 1, so
                                    // "i - 1" does not overflow.
                                    assert!(v.max_element().is_nan(),
                                            "[E]: all nans | v={:?} | max={} | \
                                             is_nan: {}",
                                            v, v.max_element(),
                                            v.max_element().is_nan());
                                } else {
                                    // There are non-`NaN` elements in the
                                    // vector, therefore the result is `-3.`:
                                    assert_eq!(v.max_element(), -3.,
                                               "[F]: nan at {} => {} | {:?}",
                                               i, v.max_element(), v);
                                }
                            }
                        }

                        // If the vector contains all NaNs the result is NaN:
                        assert!($id::splat(n).max_element().is_nan(),
                                "all nans | v={:?} | max={} | is_nan: {}",
                                $id::splat(n), $id::splat(n).max_element(),
                                $id::splat(n).max_element().is_nan());
                    }
                }
            }
        }
    }
}