| 1 | // Copyright 2019 Google LLC |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include <stddef.h> |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <limits> |
| 21 | |
| 22 | #undef HWY_TARGET_INCLUDE |
| 23 | #define HWY_TARGET_INCLUDE "tests/shift_test.cc" |
| 24 | #include "hwy/foreach_target.h" // IWYU pragma: keep |
| 25 | #include "hwy/highway.h" |
| 26 | #include "hwy/tests/test_util-inl.h" |
| 27 | |
| 28 | HWY_BEFORE_NAMESPACE(); |
| 29 | namespace hwy { |
| 30 | namespace HWY_NAMESPACE { |
| 31 | |
| 32 | template <bool kSigned> |
| 33 | struct TestLeftShifts { |
| 34 | template <typename T, class D> |
| 35 | HWY_NOINLINE void operator()(T t, D d) { |
| 36 | if (kSigned) { |
| 37 | // Also test positive values |
| 38 | TestLeftShifts</*kSigned=*/false>()(t, d); |
| 39 | } |
| 40 | |
| 41 | using TI = MakeSigned<T>; |
| 42 | using TU = MakeUnsigned<T>; |
| 43 | const size_t N = Lanes(d); |
| 44 | auto expected = AllocateAligned<T>(N); |
| 45 | |
| 46 | // Values to shift |
| 47 | const auto values = Iota(d, static_cast<T>(kSigned ? -TI(N) : TI(0))); |
| 48 | constexpr size_t kMaxShift = (sizeof(T) * 8) - 1; |
| 49 | |
| 50 | // 0 |
| 51 | HWY_ASSERT_VEC_EQ(d, values, ShiftLeft<0>(values)); |
| 52 | HWY_ASSERT_VEC_EQ(d, values, ShiftLeftSame(values, 0)); |
| 53 | |
| 54 | // 1 |
| 55 | for (size_t i = 0; i < N; ++i) { |
| 56 | const T value = kSigned ? T(T(i) - T(N)) : T(i); |
| 57 | expected[i] = T(TU(value) << 1); |
| 58 | } |
| 59 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftLeft<1>(values)); |
| 60 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftLeftSame(values, 1)); |
| 61 | |
| 62 | // max |
| 63 | for (size_t i = 0; i < N; ++i) { |
| 64 | const T value = kSigned ? T(T(i) - T(N)) : T(i); |
| 65 | expected[i] = T(TU(value) << kMaxShift); |
| 66 | } |
| 67 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftLeft<kMaxShift>(values)); |
| 68 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftLeftSame(values, kMaxShift)); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | template <bool kSigned> |
| 73 | struct TestVariableLeftShifts { |
| 74 | template <typename T, class D> |
| 75 | HWY_NOINLINE void operator()(T t, D d) { |
| 76 | if (kSigned) { |
| 77 | // Also test positive values |
| 78 | TestVariableLeftShifts</*kSigned=*/false>()(t, d); |
| 79 | } |
| 80 | |
| 81 | using TI = MakeSigned<T>; |
| 82 | using TU = MakeUnsigned<T>; |
| 83 | const size_t N = Lanes(d); |
| 84 | auto expected = AllocateAligned<T>(N); |
| 85 | |
| 86 | const auto v0 = Zero(d); |
| 87 | const auto v1 = Set(d, 1); |
| 88 | const auto values = Iota(d, kSigned ? -TI(N) : TI(0)); // value to shift |
| 89 | |
| 90 | constexpr size_t kMaxShift = (sizeof(T) * 8) - 1; |
| 91 | const auto max_shift = Set(d, kMaxShift); |
| 92 | const auto small_shifts = And(Iota(d, 0), max_shift); |
| 93 | const auto large_shifts = max_shift - small_shifts; |
| 94 | |
| 95 | // Same: 0 |
| 96 | HWY_ASSERT_VEC_EQ(d, values, Shl(values, v0)); |
| 97 | |
| 98 | // Same: 1 |
| 99 | for (size_t i = 0; i < N; ++i) { |
| 100 | const T value = kSigned ? T(i) - T(N) : T(i); |
| 101 | expected[i] = T(TU(value) << 1); |
| 102 | } |
| 103 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shl(values, v1)); |
| 104 | |
| 105 | // Same: max |
| 106 | for (size_t i = 0; i < N; ++i) { |
| 107 | const T value = kSigned ? T(i) - T(N) : T(i); |
| 108 | expected[i] = T(TU(value) << kMaxShift); |
| 109 | } |
| 110 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shl(values, max_shift)); |
| 111 | |
| 112 | // Variable: small |
| 113 | for (size_t i = 0; i < N; ++i) { |
| 114 | const T value = kSigned ? T(i) - T(N) : T(i); |
| 115 | expected[i] = T(TU(value) << (i & kMaxShift)); |
| 116 | } |
| 117 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shl(values, small_shifts)); |
| 118 | |
| 119 | // Variable: large |
| 120 | for (size_t i = 0; i < N; ++i) { |
| 121 | expected[i] = T(TU(1) << (kMaxShift - (i & kMaxShift))); |
| 122 | } |
| 123 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shl(v1, large_shifts)); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | struct TestUnsignedRightShifts { |
| 128 | template <typename T, class D> |
| 129 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 130 | const size_t N = Lanes(d); |
| 131 | auto expected = AllocateAligned<T>(N); |
| 132 | |
| 133 | const auto values = Iota(d, 0); |
| 134 | |
| 135 | const T kMax = LimitsMax<T>(); |
| 136 | constexpr size_t kMaxShift = (sizeof(T) * 8) - 1; |
| 137 | |
| 138 | // Shift by 0 |
| 139 | HWY_ASSERT_VEC_EQ(d, values, ShiftRight<0>(values)); |
| 140 | HWY_ASSERT_VEC_EQ(d, values, ShiftRightSame(values, 0)); |
| 141 | |
| 142 | // Shift by 1 |
| 143 | for (size_t i = 0; i < N; ++i) { |
| 144 | expected[i] = T(T(i & kMax) >> 1); |
| 145 | } |
| 146 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRight<1>(values)); |
| 147 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRightSame(values, 1)); |
| 148 | |
| 149 | // max |
| 150 | for (size_t i = 0; i < N; ++i) { |
| 151 | expected[i] = T(T(i & kMax) >> kMaxShift); |
| 152 | } |
| 153 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRight<kMaxShift>(values)); |
| 154 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRightSame(values, kMaxShift)); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | struct TestRotateRight { |
| 159 | template <typename T, class D> |
| 160 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 161 | const size_t N = Lanes(d); |
| 162 | auto expected = AllocateAligned<T>(N); |
| 163 | |
| 164 | constexpr size_t kBits = sizeof(T) * 8; |
| 165 | const auto mask_shift = Set(d, T{kBits}); |
| 166 | // Cover as many bit positions as possible to test shifting out |
| 167 | const auto values = Shl(Set(d, T{1}), And(Iota(d, 0), mask_shift)); |
| 168 | |
| 169 | // Rotate by 0 |
| 170 | HWY_ASSERT_VEC_EQ(d, values, RotateRight<0>(values)); |
| 171 | |
| 172 | // Rotate by 1 |
| 173 | Store(values, d, expected.get()); |
| 174 | for (size_t i = 0; i < N; ++i) { |
| 175 | expected[i] = (expected[i] >> 1) | (expected[i] << (kBits - 1)); |
| 176 | } |
| 177 | HWY_ASSERT_VEC_EQ(d, expected.get(), RotateRight<1>(values)); |
| 178 | |
| 179 | // Rotate by half |
| 180 | Store(values, d, expected.get()); |
| 181 | for (size_t i = 0; i < N; ++i) { |
| 182 | expected[i] = (expected[i] >> (kBits / 2)) | (expected[i] << (kBits / 2)); |
| 183 | } |
| 184 | HWY_ASSERT_VEC_EQ(d, expected.get(), RotateRight<kBits / 2>(values)); |
| 185 | |
| 186 | // Rotate by max |
| 187 | Store(values, d, expected.get()); |
| 188 | for (size_t i = 0; i < N; ++i) { |
| 189 | expected[i] = (expected[i] >> (kBits - 1)) | (expected[i] << 1); |
| 190 | } |
| 191 | HWY_ASSERT_VEC_EQ(d, expected.get(), RotateRight<kBits - 1>(values)); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | struct TestVariableUnsignedRightShifts { |
| 196 | template <typename T, class D> |
| 197 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 198 | const size_t N = Lanes(d); |
| 199 | auto expected = AllocateAligned<T>(N); |
| 200 | |
| 201 | const auto v0 = Zero(d); |
| 202 | const auto v1 = Set(d, 1); |
| 203 | const auto values = Iota(d, 0); |
| 204 | |
| 205 | const T kMax = LimitsMax<T>(); |
| 206 | const auto max = Set(d, kMax); |
| 207 | |
| 208 | constexpr size_t kMaxShift = (sizeof(T) * 8) - 1; |
| 209 | const auto max_shift = Set(d, kMaxShift); |
| 210 | const auto small_shifts = And(Iota(d, 0), max_shift); |
| 211 | const auto large_shifts = max_shift - small_shifts; |
| 212 | |
| 213 | // Same: 0 |
| 214 | HWY_ASSERT_VEC_EQ(d, values, Shr(values, v0)); |
| 215 | |
| 216 | // Same: 1 |
| 217 | for (size_t i = 0; i < N; ++i) { |
| 218 | expected[i] = T(T(i & kMax) >> 1); |
| 219 | } |
| 220 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shr(values, v1)); |
| 221 | |
| 222 | // Same: max |
| 223 | HWY_ASSERT_VEC_EQ(d, v0, Shr(values, max_shift)); |
| 224 | |
| 225 | // Variable: small |
| 226 | for (size_t i = 0; i < N; ++i) { |
| 227 | expected[i] = T(i) >> (i & kMaxShift); |
| 228 | } |
| 229 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shr(values, small_shifts)); |
| 230 | |
| 231 | // Variable: Large |
| 232 | for (size_t i = 0; i < N; ++i) { |
| 233 | expected[i] = kMax >> (kMaxShift - (i & kMaxShift)); |
| 234 | } |
| 235 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shr(max, large_shifts)); |
| 236 | } |
| 237 | }; |
| 238 | |
| 239 | template <int kAmount, typename T> |
| 240 | T RightShiftNegative(T val) { |
| 241 | // C++ shifts are implementation-defined for negative numbers, and we have |
| 242 | // seen divisions replaced with shifts, so resort to bit operations. |
| 243 | using TU = hwy::MakeUnsigned<T>; |
| 244 | TU bits; |
| 245 | CopySameSize(&val, &bits); |
| 246 | |
| 247 | const TU shifted = TU(bits >> kAmount); |
| 248 | |
| 249 | const TU all = TU(~TU(0)); |
| 250 | const size_t num_zero = sizeof(TU) * 8 - 1 - kAmount; |
| 251 | const TU sign_extended = static_cast<TU>((all << num_zero) & LimitsMax<TU>()); |
| 252 | |
| 253 | bits = shifted | sign_extended; |
| 254 | CopySameSize(&bits, &val); |
| 255 | return val; |
| 256 | } |
| 257 | |
| 258 | class TestSignedRightShifts { |
| 259 | public: |
| 260 | template <typename T, class D> |
| 261 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 262 | const size_t N = Lanes(d); |
| 263 | auto expected = AllocateAligned<T>(N); |
| 264 | constexpr T kMin = LimitsMin<T>(); |
| 265 | constexpr T kMax = LimitsMax<T>(); |
| 266 | constexpr size_t kMaxShift = (sizeof(T) * 8) - 1; |
| 267 | |
| 268 | // First test positive values, negative are checked below. |
| 269 | const auto v0 = Zero(d); |
| 270 | const auto values = And(Iota(d, 0), Set(d, kMax)); |
| 271 | |
| 272 | // Shift by 0 |
| 273 | HWY_ASSERT_VEC_EQ(d, values, ShiftRight<0>(values)); |
| 274 | HWY_ASSERT_VEC_EQ(d, values, ShiftRightSame(values, 0)); |
| 275 | |
| 276 | // Shift by 1 |
| 277 | for (size_t i = 0; i < N; ++i) { |
| 278 | expected[i] = T(T(i & kMax) >> 1); |
| 279 | } |
| 280 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRight<1>(values)); |
| 281 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRightSame(values, 1)); |
| 282 | |
| 283 | // max |
| 284 | HWY_ASSERT_VEC_EQ(d, v0, ShiftRight<kMaxShift>(values)); |
| 285 | HWY_ASSERT_VEC_EQ(d, v0, ShiftRightSame(values, kMaxShift)); |
| 286 | |
| 287 | // Even negative value |
| 288 | Test<0>(kMin, d, __LINE__); |
| 289 | Test<1>(kMin, d, __LINE__); |
| 290 | Test<2>(kMin, d, __LINE__); |
| 291 | Test<kMaxShift>(kMin, d, __LINE__); |
| 292 | |
| 293 | const T odd = static_cast<T>(kMin + 1); |
| 294 | Test<0>(odd, d, __LINE__); |
| 295 | Test<1>(odd, d, __LINE__); |
| 296 | Test<2>(odd, d, __LINE__); |
| 297 | Test<kMaxShift>(odd, d, __LINE__); |
| 298 | } |
| 299 | |
| 300 | private: |
| 301 | template <int kAmount, typename T, class D> |
| 302 | void Test(T val, D d, int line) { |
| 303 | const auto expected = Set(d, RightShiftNegative<kAmount>(val)); |
| 304 | const auto in = Set(d, val); |
| 305 | const char* file = __FILE__; |
| 306 | AssertVecEqual(d, expected, ShiftRight<kAmount>(in), file, line); |
| 307 | AssertVecEqual(d, expected, ShiftRightSame(in, kAmount), file, line); |
| 308 | } |
| 309 | }; |
| 310 | |
| 311 | struct TestVariableSignedRightShifts { |
| 312 | template <typename T, class D> |
| 313 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 314 | using TU = MakeUnsigned<T>; |
| 315 | const size_t N = Lanes(d); |
| 316 | auto expected = AllocateAligned<T>(N); |
| 317 | |
| 318 | constexpr T kMin = LimitsMin<T>(); |
| 319 | constexpr T kMax = LimitsMax<T>(); |
| 320 | |
| 321 | constexpr size_t kMaxShift = (sizeof(T) * 8) - 1; |
| 322 | |
| 323 | // First test positive values, negative are checked below. |
| 324 | const auto v0 = Zero(d); |
| 325 | const auto positive = Iota(d, 0) & Set(d, kMax); |
| 326 | |
| 327 | // Shift by 0 |
| 328 | HWY_ASSERT_VEC_EQ(d, positive, ShiftRight<0>(positive)); |
| 329 | HWY_ASSERT_VEC_EQ(d, positive, ShiftRightSame(positive, 0)); |
| 330 | |
| 331 | // Shift by 1 |
| 332 | for (size_t i = 0; i < N; ++i) { |
| 333 | expected[i] = T(T(i & kMax) >> 1); |
| 334 | } |
| 335 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRight<1>(positive)); |
| 336 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRightSame(positive, 1)); |
| 337 | |
| 338 | // max |
| 339 | HWY_ASSERT_VEC_EQ(d, v0, ShiftRight<kMaxShift>(positive)); |
| 340 | HWY_ASSERT_VEC_EQ(d, v0, ShiftRightSame(positive, kMaxShift)); |
| 341 | |
| 342 | const auto max_shift = Set(d, kMaxShift); |
| 343 | const auto small_shifts = And(Iota(d, 0), max_shift); |
| 344 | const auto large_shifts = max_shift - small_shifts; |
| 345 | |
| 346 | const auto negative = Iota(d, kMin); |
| 347 | |
| 348 | // Test varying negative to shift |
| 349 | for (size_t i = 0; i < N; ++i) { |
| 350 | expected[i] = RightShiftNegative<1>(static_cast<T>(kMin + i)); |
| 351 | } |
| 352 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shr(negative, Set(d, 1))); |
| 353 | |
| 354 | // Shift MSB right by small amounts |
| 355 | for (size_t i = 0; i < N; ++i) { |
| 356 | const size_t amount = i & kMaxShift; |
| 357 | const TU shifted = ~((1ull << (kMaxShift - amount)) - 1); |
| 358 | CopySameSize(&shifted, &expected[i]); |
| 359 | } |
| 360 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shr(Set(d, kMin), small_shifts)); |
| 361 | |
| 362 | // Shift MSB right by large amounts |
| 363 | for (size_t i = 0; i < N; ++i) { |
| 364 | const size_t amount = kMaxShift - (i & kMaxShift); |
| 365 | const TU shifted = ~((1ull << (kMaxShift - amount)) - 1); |
| 366 | CopySameSize(&shifted, &expected[i]); |
| 367 | } |
| 368 | HWY_ASSERT_VEC_EQ(d, expected.get(), Shr(Set(d, kMin), large_shifts)); |
| 369 | } |
| 370 | }; |
| 371 | |
| 372 | HWY_NOINLINE void TestAllShifts() { |
| 373 | ForUnsignedTypes(ForPartialVectors<TestLeftShifts</*kSigned=*/false>>()); |
| 374 | ForSignedTypes(ForPartialVectors<TestLeftShifts</*kSigned=*/true>>()); |
| 375 | ForUnsignedTypes(ForPartialVectors<TestUnsignedRightShifts>()); |
| 376 | ForSignedTypes(ForPartialVectors<TestSignedRightShifts>()); |
| 377 | } |
| 378 | |
| 379 | HWY_NOINLINE void TestAllVariableShifts() { |
| 380 | const ForPartialVectors<TestLeftShifts</*kSigned=*/false>> shl_u; |
| 381 | const ForPartialVectors<TestLeftShifts</*kSigned=*/true>> shl_s; |
| 382 | const ForPartialVectors<TestUnsignedRightShifts> shr_u; |
| 383 | const ForPartialVectors<TestSignedRightShifts> shr_s; |
| 384 | |
| 385 | shl_u(uint16_t()); |
| 386 | shr_u(uint16_t()); |
| 387 | |
| 388 | shl_u(uint32_t()); |
| 389 | shr_u(uint32_t()); |
| 390 | |
| 391 | shl_s(int16_t()); |
| 392 | shr_s(int16_t()); |
| 393 | |
| 394 | shl_s(int32_t()); |
| 395 | shr_s(int32_t()); |
| 396 | |
| 397 | #if HWY_HAVE_INTEGER64 |
| 398 | shl_u(uint64_t()); |
| 399 | shr_u(uint64_t()); |
| 400 | |
| 401 | shl_s(int64_t()); |
| 402 | shr_s(int64_t()); |
| 403 | #endif |
| 404 | } |
| 405 | |
| 406 | HWY_NOINLINE void TestAllRotateRight() { |
| 407 | const ForPartialVectors<TestRotateRight> test; |
| 408 | test(uint32_t()); |
| 409 | #if HWY_HAVE_INTEGER64 |
| 410 | test(uint64_t()); |
| 411 | #endif |
| 412 | } |
| 413 | |
| 414 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 415 | } // namespace HWY_NAMESPACE |
| 416 | } // namespace hwy |
| 417 | HWY_AFTER_NAMESPACE(); |
| 418 | |
| 419 | #if HWY_ONCE |
| 420 | |
| 421 | namespace hwy { |
| 422 | HWY_BEFORE_TEST(HwyShiftTest); |
| 423 | HWY_EXPORT_AND_TEST_P(HwyShiftTest, TestAllShifts); |
| 424 | HWY_EXPORT_AND_TEST_P(HwyShiftTest, TestAllVariableShifts); |
| 425 | HWY_EXPORT_AND_TEST_P(HwyShiftTest, TestAllRotateRight); |
| 426 | } // namespace hwy |
| 427 | |
| 428 | #endif |