| 1 | use wide::{f32x4, f32x8, f64x2, f64x4}; |
| 2 | |
| 3 | use super::{BoolMask, HasBoolMask, LazySelect, Select}; |
| 4 | |
| 5 | macro_rules! impl_wide_bool_mask { |
| 6 | ($($ty: ident: ($scalar: ident, $uint: ident)),+) => { |
| 7 | $( |
| 8 | impl BoolMask for $ty { |
| 9 | #[inline] |
| 10 | fn from_bool(value: bool) -> Self { |
| 11 | $ty::splat(if value { $scalar::from_bits($uint::MAX) } else { 0.0 }) |
| 12 | } |
| 13 | |
| 14 | #[inline] |
| 15 | fn is_true(&self) -> bool { |
| 16 | self.all() |
| 17 | } |
| 18 | |
| 19 | #[inline] |
| 20 | fn is_false(&self) -> bool { |
| 21 | self.none() |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl HasBoolMask for $ty { |
| 26 | type Mask = Self; |
| 27 | } |
| 28 | |
| 29 | impl Select<Self> for $ty { |
| 30 | #[inline] |
| 31 | fn select(self, a: Self, b: Self) -> Self { |
| 32 | self.blend(a, b) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl LazySelect<Self> for $ty { |
| 37 | #[inline] |
| 38 | fn lazy_select<A, B>(self, a: A, b: B) -> Self |
| 39 | where |
| 40 | A: FnOnce() -> Self, |
| 41 | B: FnOnce() -> Self, |
| 42 | { |
| 43 | let a = a(); |
| 44 | let b = b(); |
| 45 | |
| 46 | self.select(a, b) |
| 47 | } |
| 48 | } |
| 49 | )+ |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | impl_wide_bool_mask!( |
| 54 | f32x4: (f32, u32), |
| 55 | f32x8: (f32, u32), |
| 56 | f64x2: (f64, u64), |
| 57 | f64x4: (f64, u64) |
| 58 | ); |
| 59 | |
| 60 | #[cfg(test)] |
| 61 | mod test { |
| 62 | use wide::{f32x4, f32x8, f64x2, f64x4}; |
| 63 | |
| 64 | use crate::bool_mask::BoolMask; |
| 65 | |
| 66 | #[test] |
| 67 | fn from_true() { |
| 68 | assert!(f32x4::from_bool(true).is_true()); |
| 69 | assert!(!f32x4::from_bool(true).is_false()); |
| 70 | |
| 71 | assert!(f32x8::from_bool(true).is_true()); |
| 72 | assert!(!f32x8::from_bool(true).is_false()); |
| 73 | |
| 74 | assert!(f64x2::from_bool(true).is_true()); |
| 75 | assert!(!f64x2::from_bool(true).is_false()); |
| 76 | |
| 77 | assert!(f64x4::from_bool(true).is_true()); |
| 78 | assert!(!f64x4::from_bool(true).is_false()); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn from_false() { |
| 83 | assert!(f32x4::from_bool(false).is_false()); |
| 84 | assert!(!f32x4::from_bool(false).is_true()); |
| 85 | |
| 86 | assert!(f32x8::from_bool(false).is_false()); |
| 87 | assert!(!f32x8::from_bool(false).is_true()); |
| 88 | |
| 89 | assert!(f64x2::from_bool(false).is_false()); |
| 90 | assert!(!f64x2::from_bool(false).is_true()); |
| 91 | |
| 92 | assert!(f64x4::from_bool(false).is_false()); |
| 93 | assert!(!f64x4::from_bool(false).is_true()); |
| 94 | } |
| 95 | } |