| 1 |
use std::num::Wrapping; |
| 2 |
use wide::*; |
| 3 |
|
| 4 |
#[test] |
| 5 |
fn size_align() { |
| 6 |
assert_eq!(core::mem::size_of::<u16x8>(), 16); |
| 7 |
assert_eq!(core::mem::align_of::<u16x8>(), 16); |
| 8 |
} |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn basic_traits() { |
| 12 |
crate::test_basic_traits::<u16x16, _, 16>(); |
| 13 |
} |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn impl_add_for_u16x8() { |
| 17 |
let a = u16x8::from([1, 2, 3, 4, 5, 6, u16::MAX - 1, u16::MAX - 1]); |
| 18 |
let b = u16x8::from([17, 18, 19, 20, 21, 22, 1, 2]); |
| 19 |
let expected = u16x8::from([18, 20, 22, 24, 26, 28, u16::MAX, 0]); |
| 20 |
let actual = a + b; |
| 21 |
assert_eq!(expected, actual); |
| 22 |
|
| 23 |
crate::test_random_vector_vs_scalar( |
| 24 |
|a: u16x8, b| a + b, |
| 25 |
|a, b| a.wrapping_add(b), |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn impl_sub_for_u16x8() { |
| 31 |
let a = u16x8::from([1468, 220, 3, 4456, 5, 6897, 1, 0]); |
| 32 |
let b = u16x8::from([17, 180, 192, 200, 121, 22, 1, 1]); |
| 33 |
let expected = u16x8::from([1451, 40, 65347, 4256, 65420, 6875, 0, u16::MAX]); |
| 34 |
let actual = a - b; |
| 35 |
assert_eq!(expected, actual); |
| 36 |
|
| 37 |
crate::test_random_vector_vs_scalar( |
| 38 |
|a: u16x8, b| a - b, |
| 39 |
|a, b| a.wrapping_sub(b), |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn impl_saturating_add_for_u16x8() { |
| 45 |
let a = u16x8::from([1, 2, 3, 4, 5, 6, u16::MAX - 1, u16::MAX - 1]); |
| 46 |
let b = u16x8::from([17, 18, 19, 20, 21, 22, 1, 2]); |
| 47 |
let expected = u16x8::from([18, 20, 22, 24, 26, 28, u16::MAX, u16::MAX]); |
| 48 |
let actual = a.saturating_add(b); |
| 49 |
assert_eq!(expected, actual); |
| 50 |
|
| 51 |
crate::test_random_vector_vs_scalar( |
| 52 |
|a: u16x8, b| a.saturating_add(b), |
| 53 |
|a, b| a.saturating_add(b), |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn impl_saturating_sub_for_u16x8() { |
| 59 |
let a = u16x8::from([1468, 220, 3, 4456, 5, 6897, 1, 0]); |
| 60 |
let b = u16x8::from([17, 180, 192, 200, 121, 22, 1, 1]); |
| 61 |
let expected = u16x8::from([1451, 40, 0, 4256, 0, 6875, 0, 0]); |
| 62 |
let actual = a.saturating_sub(b); |
| 63 |
assert_eq!(expected, actual); |
| 64 |
|
| 65 |
crate::test_random_vector_vs_scalar( |
| 66 |
|a: u16x8, b| a.saturating_sub(b), |
| 67 |
|a, b| a.saturating_sub(b), |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn impl_mul_for_u16x8() { |
| 73 |
let a = u16x8::from([1, 2, u16::MAX, 4, 5, 6, u16::MIN + 1, u16::MIN]); |
| 74 |
let b = u16x8::from([17, 18, 190, 20, 21, 22, 1, 1]); |
| 75 |
let expected = u16x8::from([ |
| 76 |
17, |
| 77 |
36, |
| 78 |
(Wrapping(u16::MAX) * Wrapping(190)).0, |
| 79 |
80, |
| 80 |
105, |
| 81 |
132, |
| 82 |
u16::MIN + 1, |
| 83 |
u16::MIN, |
| 84 |
]); |
| 85 |
let actual = a * b; |
| 86 |
assert_eq!(expected, actual); |
| 87 |
|
| 88 |
crate::test_random_vector_vs_scalar( |
| 89 |
|a: u16x8, b| a * b, |
| 90 |
|a, b| a.wrapping_mul(b), |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn impl_bitand_for_u8x16() { |
| 96 |
let a = u8x16::from([0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]); |
| 97 |
let b = u8x16::from([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]); |
| 98 |
let expected = u8x16::from([0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1]); |
| 99 |
let actual = a & b; |
| 100 |
assert_eq!(expected, actual); |
| 101 |
|
| 102 |
crate::test_random_vector_vs_scalar(|a: u16x8, b| a & b, |a, b| a & b); |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn impl_bitor_for_u8x16() { |
| 107 |
let a = u8x16::from([0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]); |
| 108 |
let b = u8x16::from([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]); |
| 109 |
let expected = u8x16::from([0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1]); |
| 110 |
let actual = a | b; |
| 111 |
assert_eq!(expected, actual); |
| 112 |
|
| 113 |
crate::test_random_vector_vs_scalar(|a: u16x8, b| a | b, |a, b| a | b); |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn impl_bitxor_for_u8x16() { |
| 118 |
let a = u8x16::from([0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]); |
| 119 |
let b = u8x16::from([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]); |
| 120 |
let expected = u8x16::from([0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0]); |
| 121 |
let actual = a ^ b; |
| 122 |
assert_eq!(expected, actual); |
| 123 |
|
| 124 |
crate::test_random_vector_vs_scalar(|a: u16x8, b| a ^ b, |a, b| a ^ b); |
| 125 |
} |
| 126 |
|
| 127 |
#[test] |
| 128 |
fn impl_shl_for_u16x8() { |
| 129 |
let a = u16x8::from([1, 2, 3, 4, 5, 6, u16::MAX - 1, u16::MAX - 1]); |
| 130 |
let b = 2; |
| 131 |
let expected = u16x8::from([ |
| 132 |
1 << 2, |
| 133 |
2 << 2, |
| 134 |
3 << 2, |
| 135 |
4 << 2, |
| 136 |
5 << 2, |
| 137 |
6 << 2, |
| 138 |
(u16::MAX - 1) << 2, |
| 139 |
(u16::MAX - 1) << 2, |
| 140 |
]); |
| 141 |
let actual = a << b; |
| 142 |
assert_eq!(expected, actual); |
| 143 |
|
| 144 |
crate::test_random_vector_vs_scalar(|a: u16x8, _b| a << 3, |a, _b| a << 3); |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn impl_shr_for_u16x8() { |
| 149 |
let a = u16x8::from([1, 2, 3, 4, 5, 6, u16::MAX - 1, u16::MAX - 1]); |
| 150 |
let b = 2; |
| 151 |
let expected = u16x8::from([ |
| 152 |
1 >> 2, |
| 153 |
2 >> 2, |
| 154 |
3 >> 2, |
| 155 |
4 >> 2, |
| 156 |
5 >> 2, |
| 157 |
6 >> 2, |
| 158 |
(u16::MAX - 1) >> 2, |
| 159 |
(u16::MAX - 1) >> 2, |
| 160 |
]); |
| 161 |
let actual = a >> b; |
| 162 |
assert_eq!(expected, actual); |
| 163 |
|
| 164 |
crate::test_random_vector_vs_scalar(|a: u16x8, _b| a >> 3, |a, _b| a >> 3); |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn impl_u16x8_cmp_eq() { |
| 169 |
let a = u16x8::from([1, 2, 3, 4, 1, 2, 3, 4]); |
| 170 |
let b = u16x8::from([2_u16; 8]); |
| 171 |
let expected = u16x8::from([0, u16::MAX, 0, 0, 0, u16::MAX, 0, 0]); |
| 172 |
let actual = a.cmp_eq(b); |
| 173 |
assert_eq!(expected, actual); |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
fn impl_u16x8_blend() { |
| 178 |
let use_t: u16 = u16::MAX; |
| 179 |
let t = u16x8::from([1, 2, 3, 4, 5, 6, 7, 8]); |
| 180 |
let f = u16x8::from([17, 18, 19, 20, 21, 22, 23, 24]); |
| 181 |
let mask = u16x8::from([use_t, 0, use_t, 0, use_t, 0, use_t, 0]); |
| 182 |
let expected = u16x8::from([1, 18, 3, 20, 5, 22, 7, 24]); |
| 183 |
let actual = mask.blend(t, f); |
| 184 |
assert_eq!(expected, actual); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn impl_u16x8_max() { |
| 189 |
let a = u16x8::from([1, 37001, 3, 4, 5, 6, 7, 8]); |
| 190 |
let b = u16x8::from([37000, 37000, 19, 20, 2, 2, 2, 24]); |
| 191 |
let expected = u16x8::from([37000, 37001, 19, 20, 5, 6, 7, 24]); |
| 192 |
let actual = a.max(b); |
| 193 |
assert_eq!(expected, actual); |
| 194 |
|
| 195 |
crate::test_random_vector_vs_scalar(|a: u16x8, b| a.max(b), |a, b| a.max(b)); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn impl_u16x8_min() { |
| 200 |
let a = u16x8::from([1, 37001, 3, 4, 5, 6, 7, 8]); |
| 201 |
let b = u16x8::from([37000, 37000, 19, 20, 2, 2, 2, 24]); |
| 202 |
let expected = u16x8::from([1, 37000, 3, 4, 2, 2, 2, 8]); |
| 203 |
let actual = a.min(b); |
| 204 |
assert_eq!(expected, actual); |
| 205 |
|
| 206 |
crate::test_random_vector_vs_scalar(|a: u16x8, b| a.min(b), |a, b| a.min(b)); |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn impl_u16x8_from_u8x16_low() { |
| 211 |
let a = |
| 212 |
u8x16::from([255, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 255, 128]); |
| 213 |
let expected = u16x8::from([255, 2, 3, 4, 5, 6, 7, 8]); |
| 214 |
let actual = u16x8::from_u8x16_low(a); |
| 215 |
assert_eq!(expected, actual); |
| 216 |
} |
| 217 |
#[test] |
| 218 |
fn impl_u16x8_from_u8x16_high() { |
| 219 |
let a = |
| 220 |
u8x16::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 255, 128]); |
| 221 |
let expected = u16x8::from([9, 10, 11, 12, 13, 14, 255, 128]); |
| 222 |
let actual = u16x8::from_u8x16_high(a); |
| 223 |
assert_eq!(expected, actual); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn impl_u16x8_mul_keep_high() { |
| 228 |
let a = u16x8::from([u16::MAX, 200, 300, 4568, 1, 2, 3, 200]); |
| 229 |
let b = u16x8::from([u16::MAX, 600, 700, 8910, 15, 26, 37, 600]); |
| 230 |
let c: [u16; 8] = u16x8::mul_keep_high(a, b).into(); |
| 231 |
assert_eq!( |
| 232 |
c, |
| 233 |
[ |
| 234 |
(u32::from(u16::MAX) * u32::from(u16::MAX) >> 16) as u16, |
| 235 |
1, |
| 236 |
3, |
| 237 |
621, |
| 238 |
0, |
| 239 |
0, |
| 240 |
0, |
| 241 |
1 |
| 242 |
] |
| 243 |
); |
| 244 |
|
| 245 |
crate::test_random_vector_vs_scalar( |
| 246 |
|a: u16x8, b| u16x8::mul_keep_high(a, b), |
| 247 |
|a, b| ((u32::from(a) * u32::from(b)) >> 16) as u16, |
| 248 |
); |
| 249 |
} |
| 250 |
|
| 251 |
#[test] |
| 252 |
fn impl_u16x8_mul_widen() { |
| 253 |
let a = u16x8::from([1, 2, 3, 4, 5, 6, i16::MAX as u16, u16::MAX]); |
| 254 |
let b = u16x8::from([17, 18, 190, 20, 21, 22, i16::MAX as u16, u16::MAX]); |
| 255 |
let expected = u32x8::from([ |
| 256 |
17, |
| 257 |
36, |
| 258 |
570, |
| 259 |
80, |
| 260 |
105, |
| 261 |
132, |
| 262 |
(i16::MAX as u32) * (i16::MAX as u32), |
| 263 |
(u16::MAX as u32) * (u16::MAX as u32), |
| 264 |
]); |
| 265 |
let actual = a.mul_widen(b); |
| 266 |
assert_eq!(expected, actual); |
| 267 |
|
| 268 |
crate::test_random_vector_vs_scalar( |
| 269 |
|a: u16x8, b| a.mul_widen(b), |
| 270 |
|a, b| u32::from(a) * u32::from(b), |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
#[cfg(feature = "serde")] |
| 275 |
#[test] |
| 276 |
fn impl_u16x8_ser_de_roundtrip() { |
| 277 |
let serialized = |
| 278 |
bincode::serialize(&u16x8::ZERO).expect("serialization failed"); |
| 279 |
let deserialized = |
| 280 |
bincode::deserialize(&serialized).expect("deserializaion failed"); |
| 281 |
assert_eq!(u16x8::ZERO, deserialized); |
| 282 |
} |