| 1 |
use std::num::Wrapping; |
| 2 |
|
| 3 |
use wide::*; |
| 4 |
|
| 5 |
#[test] |
| 6 |
fn size_align() { |
| 7 |
assert_eq!(core::mem::size_of::<u32x4>(), 16); |
| 8 |
assert_eq!(core::mem::align_of::<u32x4>(), 16); |
| 9 |
} |
| 10 |
|
| 11 |
#[test] |
| 12 |
fn basic_traits() { |
| 13 |
crate::test_basic_traits::<u32x4, _, 4>(); |
| 14 |
} |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn impl_add_for_u32x4() { |
| 18 |
let a = u32x4::from([1, 2, u32::MAX - 1, u32::MAX - 1]); |
| 19 |
let b = u32x4::from([17, 18, 1, 2]); |
| 20 |
let expected = u32x4::from([18, 20, u32::MAX, u32::MIN]); |
| 21 |
let actual = a + b; |
| 22 |
assert_eq!(expected, actual); |
| 23 |
} |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn impl_sub_for_u32x4() { |
| 27 |
let a = u32x4::from([9001, 2, 1, 0]); |
| 28 |
let b = u32x4::from([17, 18, 1, 1]); |
| 29 |
let expected = u32x4::from([8984, 4294967280, 0, u32::MAX]); |
| 30 |
let actual = a - b; |
| 31 |
assert_eq!(expected, actual); |
| 32 |
} |
| 33 |
|
| 34 |
#[test] |
| 35 |
fn impl_mul_for_u32x4() { |
| 36 |
let a = u32x4::from([1, 2, u32::MIN + 1, u32::MAX]); |
| 37 |
let b = u32x4::from([17, 18, 1, 32]); |
| 38 |
let expected = |
| 39 |
u32x4::from([17, 36, 1, (Wrapping(u32::MAX) * Wrapping(32)).0]); |
| 40 |
let actual = a * b; |
| 41 |
assert_eq!(expected, actual); |
| 42 |
|
| 43 |
crate::test_random_vector_vs_scalar( |
| 44 |
|a: u32x4, b| a * b, |
| 45 |
|a, b| a.wrapping_mul(b), |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
#[test] |
| 50 |
fn impl_bitand_for_u32x4() { |
| 51 |
let a = u32x4::from([0, 0, 1, 1]); |
| 52 |
let b = u32x4::from([0, 1, 0, 1]); |
| 53 |
let expected = u32x4::from([0, 0, 0, 1]); |
| 54 |
let actual = a & b; |
| 55 |
assert_eq!(expected, actual); |
| 56 |
|
| 57 |
crate::test_random_vector_vs_scalar(|a: u32x4, b| a & b, |a, b| a & b); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn impl_bitor_for_u32x4() { |
| 62 |
let a = u32x4::from([0, 0, 1, 1]); |
| 63 |
let b = u32x4::from([0, 1, 0, 1]); |
| 64 |
let expected = u32x4::from([0, 1, 1, 1]); |
| 65 |
let actual = a | b; |
| 66 |
assert_eq!(expected, actual); |
| 67 |
|
| 68 |
crate::test_random_vector_vs_scalar(|a: u32x4, b| a | b, |a, b| a | b); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn impl_bitxor_for_u32x4() { |
| 73 |
let a = u32x4::from([0, 0, 1, 1]); |
| 74 |
let b = u32x4::from([0, 1, 0, 1]); |
| 75 |
let expected = u32x4::from([0, 1, 1, 0]); |
| 76 |
let actual = a ^ b; |
| 77 |
assert_eq!(expected, actual); |
| 78 |
|
| 79 |
crate::test_random_vector_vs_scalar(|a: u32x4, b| a ^ b, |a, b| a ^ b); |
| 80 |
} |
| 81 |
|
| 82 |
#[test] |
| 83 |
fn impl_shl_for_u32x4() { |
| 84 |
let a = u32x4::from([1, 2, u32::MAX - 1, u32::MAX - 1]); |
| 85 |
let b = 2; |
| 86 |
let expected = |
| 87 |
u32x4::from([1 << 2, 2 << 2, (u32::MAX - 1) << 2, (u32::MAX - 1) << 2]); |
| 88 |
let actual = a << b; |
| 89 |
assert_eq!(expected, actual); |
| 90 |
|
| 91 |
crate::test_random_vector_vs_scalar(|a: u32x4, _b| a << 3, |a, _b| a << 3); |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn impl_shr_for_u32x4() { |
| 96 |
let a = u32x4::from([1, 2, u32::MAX - 1, u32::MAX - 1]); |
| 97 |
let b = 2; |
| 98 |
let expected = |
| 99 |
u32x4::from([1 >> 2, 2 >> 2, (u32::MAX - 1) >> 2, (u32::MAX - 1) >> 2]); |
| 100 |
let actual = a >> b; |
| 101 |
assert_eq!(expected, actual); |
| 102 |
|
| 103 |
crate::test_random_vector_vs_scalar(|a: u32x4, _b| a >> 3, |a, _b| a >> 3); |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn impl_u32x4_cmp_eq() { |
| 108 |
let a = u32x4::from([1, 2, 3, 4]); |
| 109 |
let b = u32x4::from([2_u32; 4]); |
| 110 |
let expected = u32x4::from([0, u32::MAX, 0, 0]); |
| 111 |
let actual = a.cmp_eq(b); |
| 112 |
assert_eq!(expected, actual); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn impl_u32x4_cmp_gt() { |
| 117 |
let a = u32x4::from([1, 2, 3, u32::MAX]); |
| 118 |
let b = u32x4::from([u32::MAX, 2, 2, 2]); |
| 119 |
let expected = u32x4::from([0, 0, u32::MAX, u32::MAX]); |
| 120 |
let actual = a.cmp_gt(b); |
| 121 |
assert_eq!(expected, actual); |
| 122 |
|
| 123 |
crate::test_random_vector_vs_scalar( |
| 124 |
|a: u32x4, b| a.cmp_gt(b), |
| 125 |
|a, b| if a > b { u32::MAX } else { 0 }, |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
#[test] |
| 130 |
fn impl_u32x4_cmp_lt() { |
| 131 |
let a = u32x4::from([1, 2, 3, u32::MAX]); |
| 132 |
let b = u32x4::from([u32::MAX, 3, 3, 3]); |
| 133 |
let expected = u32x4::from([u32::MAX, u32::MAX, 0, 0]); |
| 134 |
let actual = a.cmp_lt(b); |
| 135 |
assert_eq!(expected, actual); |
| 136 |
|
| 137 |
let expected = u32x4::from([0, 0, 0, 0]); |
| 138 |
let actual = a.cmp_lt(a); |
| 139 |
assert_eq!(expected, actual); |
| 140 |
|
| 141 |
crate::test_random_vector_vs_scalar( |
| 142 |
|a: u32x4, b| a.cmp_lt(b), |
| 143 |
|a, b| if a < b { u32::MAX } else { 0 }, |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn impl_u32x4_blend() { |
| 149 |
let use_t: u32 = u32::MAX; |
| 150 |
let t = u32x4::from([1, 2, 3, 4]); |
| 151 |
let f = u32x4::from([17, 18, 19, 20]); |
| 152 |
let mask = u32x4::from([use_t, 0, use_t, 0]); |
| 153 |
let expected = u32x4::from([1, 18, 3, 20]); |
| 154 |
let actual = mask.blend(t, f); |
| 155 |
assert_eq!(expected, actual); |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn impl_u32x4_max() { |
| 160 |
let a = u32x4::from([0, 2, 3, 4]); |
| 161 |
let b = u32x4::from([17, 1, 0, 20]); |
| 162 |
let expected = u32x4::from([17, 2, 3, 20]); |
| 163 |
let actual = a.max(b); |
| 164 |
assert_eq!(expected, actual); |
| 165 |
|
| 166 |
crate::test_random_vector_vs_scalar(|a: u32x4, b| a.max(b), |a, b| a.max(b)); |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
fn impl_u32x4_min() { |
| 171 |
let a = u32x4::from([0, 2, 3, 4]); |
| 172 |
let b = u32x4::from([17, 1, 0, 20]); |
| 173 |
let expected = u32x4::from([0, 1, 0, 4]); |
| 174 |
let actual = a.min(b); |
| 175 |
assert_eq!(expected, actual); |
| 176 |
|
| 177 |
crate::test_random_vector_vs_scalar(|a: u32x4, b| a.min(b), |a, b| a.min(b)); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn impl_u32x4_not() { |
| 182 |
let a = u32x4::from([15313, 52322, u32::MAX, 4]); |
| 183 |
let expected = u32x4::from([4294951982, 4294914973, 0, 4294967291]); |
| 184 |
let actual = !a; |
| 185 |
assert_eq!(expected, actual); |
| 186 |
|
| 187 |
crate::test_random_vector_vs_scalar(|a: u32x4, _b| !a, |a, _b| !a); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn impl_u32x4_shr_each() { |
| 192 |
let a = u32x4::from([15313, 52322, u32::MAX, 4]); |
| 193 |
let shift = u32x4::from([1, 30, 8, 33 /* test masking behavior */]); |
| 194 |
let expected = u32x4::from([7656u32, 0, 16777215, 2]); |
| 195 |
let actual = a >> shift; |
| 196 |
assert_eq!(expected, actual); |
| 197 |
|
| 198 |
crate::test_random_vector_vs_scalar( |
| 199 |
|a: u32x4, b| a >> b, |
| 200 |
|a, b| a.wrapping_shr(b), |
| 201 |
); |
| 202 |
} |
| 203 |
#[test] |
| 204 |
fn impl_u32x4_shl_each() { |
| 205 |
let a = u32x4::from([15313, 52322, u32::MAX, 4]); |
| 206 |
let shift = u32x4::from([1, 30, 8, 33 /* test masking behavior */]); |
| 207 |
let expected = u32x4::from([30626, 2147483648, 4294967040, 8]); |
| 208 |
let actual = a << shift; |
| 209 |
assert_eq!(expected, actual); |
| 210 |
|
| 211 |
crate::test_random_vector_vs_scalar( |
| 212 |
|a: u32x4, b| a << b, |
| 213 |
|a, b| a.wrapping_shl(b), |
| 214 |
); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn test_u32x4_any() { |
| 219 |
let a = u32x4::from([0, 0, 0, u32::MAX]); |
| 220 |
assert!(a.any()); |
| 221 |
// |
| 222 |
let a = u32x4::from([0, 0, 0, 0]); |
| 223 |
assert!(!a.any()); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn test_u32x4_all() { |
| 228 |
let a = u32x4::from([0, 0, 0, u32::MAX]); |
| 229 |
assert!(!a.all()); |
| 230 |
// |
| 231 |
let a = u32x4::from([u32::MAX; 4]); |
| 232 |
assert!(a.all()); |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn test_u32x4_none() { |
| 237 |
let a = u32x4::from([0, 0, 0, u32::MAX]); |
| 238 |
assert!(!a.none()); |
| 239 |
// |
| 240 |
let a = u32x4::from([0; 4]); |
| 241 |
assert!(a.none()); |
| 242 |
} |
| 243 |
|
| 244 |
#[test] |
| 245 |
fn impl_u32x4_mul_widen() { |
| 246 |
let a = u32x4::from([1, 2, 3 * 1000000, u32::MAX]); |
| 247 |
let b = u32x4::from([5, 6, 7 * 1000000, u32::MAX]); |
| 248 |
let expected = u64x4::from([ |
| 249 |
1 * 5, |
| 250 |
2 * 6, |
| 251 |
3 * 7 * 1000000 * 1000000, |
| 252 |
u32::MAX as u64 * u32::MAX as u64, |
| 253 |
]); |
| 254 |
let actual = a.mul_widen(b); |
| 255 |
assert_eq!(expected, actual); |
| 256 |
|
| 257 |
crate::test_random_vector_vs_scalar( |
| 258 |
|a: u32x4, b| a.mul_widen(b), |
| 259 |
|a, b| u64::from(a) * u64::from(b), |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
#[test] |
| 264 |
fn impl_u32x4_mul_keep_high() { |
| 265 |
let mul_high = |a: u32, b: u32| ((u64::from(a) * u64::from(b)) >> 32) as u32; |
| 266 |
let a = u32x4::from([1, 2 * 10000000, 3 * 1000000, u32::MAX]); |
| 267 |
let b = u32x4::from([5, 6 * 100, 7 * 1000000, u32::MAX]); |
| 268 |
let expected = u32x4::from([ |
| 269 |
mul_high(1, 5), |
| 270 |
mul_high(2 * 10000000, 6 * 100), |
| 271 |
mul_high(3 * 1000000, 7 * 1000000), |
| 272 |
mul_high(u32::MAX, u32::MAX), |
| 273 |
]); |
| 274 |
let actual = a.mul_keep_high(b); |
| 275 |
assert_eq!(expected, actual); |
| 276 |
|
| 277 |
crate::test_random_vector_vs_scalar( |
| 278 |
|a: u32x4, b| a.mul_keep_high(b), |
| 279 |
|a, b| ((u64::from(a) * u64::from(b)) >> 32) as u32, |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
#[test] |
| 284 |
fn impl_u32x4_transpose() { |
| 285 |
let a = [ |
| 286 |
u32x4::from([1, 2, 3, 4]), |
| 287 |
u32x4::from([5, 6, 7, 8]), |
| 288 |
u32x4::from([9, 10, 11, 12]), |
| 289 |
u32x4::from([13, 14, 15, 16]), |
| 290 |
]; |
| 291 |
let a_t = [ |
| 292 |
u32x4::from([1, 5, 9, 13]), |
| 293 |
u32x4::from([2, 6, 10, 14]), |
| 294 |
u32x4::from([3, 7, 11, 15]), |
| 295 |
u32x4::from([4, 8, 12, 16]), |
| 296 |
]; |
| 297 |
assert_eq!(u32x4::transpose(a), a_t); |
| 298 |
} |
| 299 |
|
| 300 |
#[cfg(feature = "serde")] |
| 301 |
#[test] |
| 302 |
fn impl_u32x4_ser_de_roundtrip() { |
| 303 |
let serialized = |
| 304 |
bincode::serialize(&u32x4::ZERO).expect("serialization failed"); |
| 305 |
let deserialized = |
| 306 |
bincode::deserialize(&serialized).expect("deserializaion failed"); |
| 307 |
assert_eq!(u32x4::ZERO, deserialized); |
| 308 |
} |