| 1 |
use std::num::Wrapping; |
| 2 |
|
| 3 |
use wide::*; |
| 4 |
|
| 5 |
#[test] |
| 6 |
fn size_align() { |
| 7 |
assert_eq!(core::mem::size_of::<u16x16>(), 32); |
| 8 |
assert_eq!(core::mem::align_of::<u16x16>(), 32); |
| 9 |
} |
| 10 |
|
| 11 |
#[test] |
| 12 |
fn basic_traits() { |
| 13 |
crate::test_basic_traits::<u16x16, _, 16>(); |
| 14 |
} |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn impl_add_for_u16x16() { |
| 18 |
let a = u16x16::from([ |
| 19 |
1, |
| 20 |
2, |
| 21 |
i16::MAX as u16 - 1, |
| 22 |
i16::MAX as u16 - 1, |
| 23 |
15, |
| 24 |
20, |
| 25 |
5000, |
| 26 |
2990, |
| 27 |
1, |
| 28 |
2, |
| 29 |
i16::MAX as u16 - 1, |
| 30 |
i16::MAX as u16 - 1, |
| 31 |
15, |
| 32 |
20, |
| 33 |
5000, |
| 34 |
2990, |
| 35 |
]); |
| 36 |
let b = u16x16::from([ |
| 37 |
17, 18, 1, 2, 20, 5, 900, 900, 17, 18, 1, 2, 20, 5, 900, 900, |
| 38 |
]); |
| 39 |
let expected = u16x16::from([ |
| 40 |
18, |
| 41 |
20, |
| 42 |
i16::MAX as u16, |
| 43 |
i16::MIN as u16, |
| 44 |
35, |
| 45 |
25, |
| 46 |
5900, |
| 47 |
3890, |
| 48 |
18, |
| 49 |
20, |
| 50 |
i16::MAX as u16, |
| 51 |
i16::MIN as u16, |
| 52 |
35, |
| 53 |
25, |
| 54 |
5900, |
| 55 |
3890, |
| 56 |
]); |
| 57 |
let actual = a + b; |
| 58 |
assert_eq!(expected, actual); |
| 59 |
|
| 60 |
crate::test_random_vector_vs_scalar( |
| 61 |
|a: u16x16, b| a + b, |
| 62 |
|a, b| a.wrapping_add(b), |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
#[test] |
| 67 |
fn impl_sub_for_u16x16() { |
| 68 |
let a = u16x16::from([ |
| 69 |
1, |
| 70 |
2, |
| 71 |
1, |
| 72 |
2, |
| 73 |
15, |
| 74 |
20, |
| 75 |
5000, |
| 76 |
2990, |
| 77 |
1, |
| 78 |
2, |
| 79 |
u16::MAX, |
| 80 |
u16::MAX - 1, |
| 81 |
15, |
| 82 |
20, |
| 83 |
5000, |
| 84 |
2990, |
| 85 |
]); |
| 86 |
let b = u16x16::from([ |
| 87 |
17, 18, 1, 1, 20, 5, 900, 900, 17, 18, 1, 1, 20, 5, 900, 900, |
| 88 |
]); |
| 89 |
let expected = u16x16::from([ |
| 90 |
65520, 65520, 0, 1, 65531, 15, 4100, 2090, 65520, 65520, 65534, 65533, |
| 91 |
65531, 15, 4100, 2090, |
| 92 |
]); |
| 93 |
let actual = a - b; |
| 94 |
assert_eq!(expected, actual); |
| 95 |
|
| 96 |
crate::test_random_vector_vs_scalar( |
| 97 |
|a: u16x16, b| a - b, |
| 98 |
|a, b| a.wrapping_sub(b), |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn impl_saturating_add_for_u16x16() { |
| 104 |
let a = u16x16::from([ |
| 105 |
1, |
| 106 |
2, |
| 107 |
u16::MAX - 1, |
| 108 |
u16::MAX - 1, |
| 109 |
15, |
| 110 |
20, |
| 111 |
5000, |
| 112 |
2990, |
| 113 |
1, |
| 114 |
2, |
| 115 |
u16::MAX - 1, |
| 116 |
u16::MAX - 1, |
| 117 |
15, |
| 118 |
20, |
| 119 |
5000, |
| 120 |
2990, |
| 121 |
]); |
| 122 |
let b = u16x16::from([ |
| 123 |
17, 18, 1, 2, 20, 5, 900, 900, 17, 18, 1, 2, 20, 5, 900, 900, |
| 124 |
]); |
| 125 |
let expected = u16x16::from([ |
| 126 |
18, |
| 127 |
20, |
| 128 |
u16::MAX, |
| 129 |
u16::MAX, |
| 130 |
35, |
| 131 |
25, |
| 132 |
5900, |
| 133 |
3890, |
| 134 |
18, |
| 135 |
20, |
| 136 |
u16::MAX, |
| 137 |
u16::MAX, |
| 138 |
35, |
| 139 |
25, |
| 140 |
5900, |
| 141 |
3890, |
| 142 |
]); |
| 143 |
let actual = a.saturating_add(b); |
| 144 |
assert_eq!(expected, actual); |
| 145 |
|
| 146 |
crate::test_random_vector_vs_scalar( |
| 147 |
|a: u16x16, b| a.saturating_add(b), |
| 148 |
|a, b| a.saturating_add(b), |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn impl_saturating_sub_for_u16x16() { |
| 154 |
let a = u16x16::from([ |
| 155 |
1, 2, 1, 0, 15, 20, 5000, 2990, 1, 2, 1, 0, 15, 20, 5000, 2990, |
| 156 |
]); |
| 157 |
let b = u16x16::from([ |
| 158 |
17, 18, 1, 1, 20, 5, 900, 900, 17, 18, 1, 1, 20, 5, 900, 900, |
| 159 |
]); |
| 160 |
let expected = u16x16::from([ |
| 161 |
0, 0, 0, 0, 0, 15, 4100, 2090, 0, 0, 0, 0, 0, 15, 4100, 2090, |
| 162 |
]); |
| 163 |
let actual = a.saturating_sub(b); |
| 164 |
assert_eq!(expected, actual); |
| 165 |
|
| 166 |
crate::test_random_vector_vs_scalar( |
| 167 |
|a: u16x16, b| a.saturating_sub(b), |
| 168 |
|a, b| a.saturating_sub(b), |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn impl_bitand_for_u16x16() { |
| 174 |
let a = u16x16::from([0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1]); |
| 175 |
let b = u16x16::from([0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1]); |
| 176 |
let expected = u16x16::from([0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1]); |
| 177 |
let actual = a & b; |
| 178 |
assert_eq!(expected, actual); |
| 179 |
|
| 180 |
crate::test_random_vector_vs_scalar(|a: u16x16, b| a & b, |a, b| a & b); |
| 181 |
} |
| 182 |
|
| 183 |
#[test] |
| 184 |
fn impl_bitor_for_u16x16() { |
| 185 |
let a = u16x16::from([0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1]); |
| 186 |
let b = u16x16::from([0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1]); |
| 187 |
let expected = u16x16::from([0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]); |
| 188 |
let actual = a | b; |
| 189 |
assert_eq!(expected, actual); |
| 190 |
|
| 191 |
crate::test_random_vector_vs_scalar(|a: u16x16, b| a | b, |a, b| a | b); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn impl_bitxor_for_u16x16() { |
| 196 |
let a = u16x16::from([0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1]); |
| 197 |
let b = u16x16::from([0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1]); |
| 198 |
let expected = u16x16::from([0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0]); |
| 199 |
let actual = a ^ b; |
| 200 |
assert_eq!(expected, actual); |
| 201 |
|
| 202 |
crate::test_random_vector_vs_scalar(|a: u16x16, b| a ^ b, |a, b| a ^ b); |
| 203 |
} |
| 204 |
|
| 205 |
#[test] |
| 206 |
fn impl_shl_for_u16x16() { |
| 207 |
let a = u16x16::from([ |
| 208 |
1, |
| 209 |
2, |
| 210 |
u16::MAX - 1, |
| 211 |
u16::MAX - 1, |
| 212 |
128, |
| 213 |
255, |
| 214 |
590, |
| 215 |
5667, |
| 216 |
1, |
| 217 |
2, |
| 218 |
u16::MAX - 1, |
| 219 |
u16::MAX - 1, |
| 220 |
128, |
| 221 |
255, |
| 222 |
590, |
| 223 |
5667, |
| 224 |
]); |
| 225 |
let b = 2; |
| 226 |
let expected = u16x16::from([ |
| 227 |
1 << 2, |
| 228 |
2 << 2, |
| 229 |
(u16::MAX - 1) << 2, |
| 230 |
(u16::MAX - 1) << 2, |
| 231 |
128 << 2, |
| 232 |
255 << 2, |
| 233 |
590 << 2, |
| 234 |
5667 << 2, |
| 235 |
1 << 2, |
| 236 |
2 << 2, |
| 237 |
(u16::MAX - 1) << 2, |
| 238 |
(u16::MAX - 1) << 2, |
| 239 |
128 << 2, |
| 240 |
255 << 2, |
| 241 |
590 << 2, |
| 242 |
5667 << 2, |
| 243 |
]); |
| 244 |
let actual = a << b; |
| 245 |
assert_eq!(expected, actual); |
| 246 |
|
| 247 |
crate::test_random_vector_vs_scalar(|a: u16x16, _b| a << 3, |a, _b| a << 3); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn impl_shr_for_u16x16() { |
| 252 |
let a = u16x16::from([ |
| 253 |
1, |
| 254 |
2, |
| 255 |
u16::MAX - 1, |
| 256 |
u16::MAX - 1, |
| 257 |
128, |
| 258 |
255, |
| 259 |
590, |
| 260 |
5667, |
| 261 |
1, |
| 262 |
2, |
| 263 |
u16::MAX - 1, |
| 264 |
u16::MAX - 1, |
| 265 |
128, |
| 266 |
255, |
| 267 |
590, |
| 268 |
5667, |
| 269 |
]); |
| 270 |
let b = 2; |
| 271 |
let expected = u16x16::from([ |
| 272 |
1 >> 2, |
| 273 |
2 >> 2, |
| 274 |
(u16::MAX - 1) >> 2, |
| 275 |
(u16::MAX - 1) >> 2, |
| 276 |
128 >> 2, |
| 277 |
255 >> 2, |
| 278 |
590 >> 2, |
| 279 |
5667 >> 2, |
| 280 |
1 >> 2, |
| 281 |
2 >> 2, |
| 282 |
(u16::MAX - 1) >> 2, |
| 283 |
(u16::MAX - 1) >> 2, |
| 284 |
128 >> 2, |
| 285 |
255 >> 2, |
| 286 |
590 >> 2, |
| 287 |
5667 >> 2, |
| 288 |
]); |
| 289 |
let actual = a >> b; |
| 290 |
assert_eq!(expected, actual); |
| 291 |
|
| 292 |
crate::test_random_vector_vs_scalar(|a: u16x16, _b| a >> 3, |a, _b| a >> 3); |
| 293 |
} |
| 294 |
|
| 295 |
#[test] |
| 296 |
fn impl_u16x16_cmp_eq() { |
| 297 |
let a = u16x16::from([1, 2, 3, 4, 2, 1, 8, 2, 1, 2, 3, 4, 2, 1, 8, 2]); |
| 298 |
let b = u16x16::from([2_u16; 16]); |
| 299 |
let expected = u16x16::from([ |
| 300 |
0, |
| 301 |
u16::MAX, |
| 302 |
0, |
| 303 |
0, |
| 304 |
u16::MAX, |
| 305 |
0, |
| 306 |
0, |
| 307 |
u16::MAX, |
| 308 |
0, |
| 309 |
u16::MAX, |
| 310 |
0, |
| 311 |
0, |
| 312 |
u16::MAX, |
| 313 |
0, |
| 314 |
0, |
| 315 |
u16::MAX, |
| 316 |
]); |
| 317 |
let actual = a.cmp_eq(b); |
| 318 |
assert_eq!(expected, actual); |
| 319 |
} |
| 320 |
|
| 321 |
#[test] |
| 322 |
fn impl_u16x16_blend() { |
| 323 |
let use_t: u16 = u16::MAX; |
| 324 |
let t = u16x16::from([1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]); |
| 325 |
let f = u16x16::from([ |
| 326 |
17, 18, 19, 20, 25, 30, 50, 90, 17, 18, 19, 20, 25, 30, 50, 90, |
| 327 |
]); |
| 328 |
let mask = u16x16::from([ |
| 329 |
use_t, 0, use_t, 0, 0, 0, 0, use_t, use_t, 0, use_t, 0, 0, 0, 0, use_t, |
| 330 |
]); |
| 331 |
let expected = |
| 332 |
u16x16::from([1, 18, 3, 20, 25, 30, 50, 8, 1, 18, 3, 20, 25, 30, 50, 8]); |
| 333 |
let actual = mask.blend(t, f); |
| 334 |
assert_eq!(expected, actual); |
| 335 |
} |
| 336 |
|
| 337 |
#[test] |
| 338 |
fn impl_u16x16_max() { |
| 339 |
let a = |
| 340 |
u16x16::from([u16::MAX, 2, 1, 0, 6, 8, 12, 9, 1, 2, 1, 0, 6, 8, 12, 9]); |
| 341 |
let b = u16x16::from([17, 0, 1, 1, 19, 0, 0, 0, 17, 0, 1, 1, 19, 0, 0, 0]); |
| 342 |
let expected = |
| 343 |
u16x16::from([u16::MAX, 2, 1, 1, 19, 8, 12, 9, 17, 2, 1, 1, 19, 8, 12, 9]); |
| 344 |
let actual = a.max(b); |
| 345 |
assert_eq!(expected, actual); |
| 346 |
|
| 347 |
crate::test_random_vector_vs_scalar(|a: u16x16, b| a.max(b), |a, b| a.max(b)); |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn impl_u16x16_from_u8x16() { |
| 352 |
let v = [10u8, 2, 3, 4, 5, 6, 7, 8, 9, 7, 127, 12, 13, 6, 55, 255]; |
| 353 |
|
| 354 |
assert_eq!( |
| 355 |
u16x16::from(v.map(|a| u16::from(a))), |
| 356 |
u16x16::from(u8x16::from(v)) |
| 357 |
); |
| 358 |
|
| 359 |
crate::test_random_vector_vs_scalar( |
| 360 |
|a: u8x16, _b| u16x16::from(a), |
| 361 |
|a, _b| u16::from(a), |
| 362 |
); |
| 363 |
} |
| 364 |
|
| 365 |
#[test] |
| 366 |
fn impl_u16x16_min() { |
| 367 |
let a = u16x16::from([1, 2, 1, 0, 6, 8, 12, 9, 1, 2, 1, 0, 6, 8, 12, 9]); |
| 368 |
let b = |
| 369 |
u16x16::from([u16::MAX, 0, 1, 1, 19, 0, 0, 0, 17, 0, 1, 1, 19, 0, 0, 0]); |
| 370 |
let expected = u16x16::from([1, 0, 1, 0, 6, 0, 0, 0, 1, 0, 1, 0, 6, 0, 0, 0]); |
| 371 |
let actual = a.min(b); |
| 372 |
assert_eq!(expected, actual); |
| 373 |
|
| 374 |
crate::test_random_vector_vs_scalar(|a: u16x16, b| a.min(b), |a, b| a.min(b)); |
| 375 |
} |
| 376 |
|
| 377 |
#[test] |
| 378 |
fn impl_mul_for_u16x16() { |
| 379 |
let a = u16x16::from([ |
| 380 |
2, |
| 381 |
2, |
| 382 |
i16::MAX as u16, |
| 383 |
4, |
| 384 |
5, |
| 385 |
6, |
| 386 |
u16::MIN + 1, |
| 387 |
u16::MIN, |
| 388 |
1, |
| 389 |
2, |
| 390 |
i16::MAX as u16, |
| 391 |
4, |
| 392 |
5, |
| 393 |
6, |
| 394 |
u16::MIN + 1, |
| 395 |
u16::MIN, |
| 396 |
]); |
| 397 |
let b = u16x16::from([ |
| 398 |
17, 18, 190, 20, 21, 22, 1, 1, 17, 18, 190, 20, 21, 22, 1, 1, |
| 399 |
]); |
| 400 |
let expected = u16x16::from([ |
| 401 |
2 * 17, |
| 402 |
36, |
| 403 |
(Wrapping(i16::MAX as u16) * Wrapping(190)).0, |
| 404 |
80, |
| 405 |
105, |
| 406 |
132, |
| 407 |
u16::MIN + 1, |
| 408 |
u16::MIN, |
| 409 |
17, |
| 410 |
36, |
| 411 |
(Wrapping(i16::MAX as u16) * Wrapping(190)).0, |
| 412 |
80, |
| 413 |
105, |
| 414 |
132, |
| 415 |
u16::MIN + 1, |
| 416 |
u16::MIN, |
| 417 |
]); |
| 418 |
let actual = a * b; |
| 419 |
assert_eq!(expected, actual); |
| 420 |
|
| 421 |
crate::test_random_vector_vs_scalar( |
| 422 |
|a: u16x16, b| a * b, |
| 423 |
|a, b| a.wrapping_mul(b), |
| 424 |
); |
| 425 |
} |
| 426 |
|
| 427 |
#[cfg(feature = "serde")] |
| 428 |
#[test] |
| 429 |
fn impl_u16x16_ser_de_roundtrip() { |
| 430 |
let serialized = |
| 431 |
bincode::serialize(&u16x16::ZERO).expect("serialization failed"); |
| 432 |
let deserialized = |
| 433 |
bincode::deserialize(&serialized).expect("deserializaion failed"); |
| 434 |
assert_eq!(u16x16::ZERO, deserialized); |
| 435 |
} |