| 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 | #include <string.h> // memcpy |
| 19 | |
| 20 | #include <algorithm> // std::fill |
| 21 | |
| 22 | #undef HWY_TARGET_INCLUDE |
| 23 | #define HWY_TARGET_INCLUDE "tests/blockwise_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 | struct TestShiftBytes { |
| 33 | template <class T, class D> |
| 34 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 35 | // Scalar does not define Shift*Bytes. |
| 36 | #if HWY_TARGET != HWY_SCALAR || HWY_IDE |
| 37 | const Repartition<uint8_t, D> du8; |
| 38 | const size_t N8 = Lanes(du8); |
| 39 | |
| 40 | // Zero remains zero |
| 41 | const auto v0 = Zero(d); |
| 42 | HWY_ASSERT_VEC_EQ(d, v0, ShiftLeftBytes<1>(v0)); |
| 43 | HWY_ASSERT_VEC_EQ(d, v0, ShiftLeftBytes<1>(d, v0)); |
| 44 | HWY_ASSERT_VEC_EQ(d, v0, ShiftRightBytes<1>(d, v0)); |
| 45 | |
| 46 | // Zero after shifting out the high/low byte |
| 47 | auto bytes = AllocateAligned<uint8_t>(N8); |
| 48 | std::fill(bytes.get(), bytes.get() + N8, 0); |
| 49 | bytes[N8 - 1] = 0x7F; |
| 50 | const auto vhi = BitCast(d, Load(du8, bytes.get())); |
| 51 | bytes[N8 - 1] = 0; |
| 52 | bytes[0] = 0x7F; |
| 53 | const auto vlo = BitCast(d, Load(du8, bytes.get())); |
| 54 | HWY_ASSERT_VEC_EQ(d, v0, ShiftLeftBytes<1>(vhi)); |
| 55 | HWY_ASSERT_VEC_EQ(d, v0, ShiftLeftBytes<1>(d, vhi)); |
| 56 | HWY_ASSERT_VEC_EQ(d, v0, ShiftRightBytes<1>(d, vlo)); |
| 57 | |
| 58 | // Check expected result with Iota |
| 59 | const size_t N = Lanes(d); |
| 60 | auto in = AllocateAligned<T>(N); |
| 61 | const uint8_t* in_bytes = reinterpret_cast<const uint8_t*>(in.get()); |
| 62 | const auto v = BitCast(d, Iota(du8, 1)); |
| 63 | Store(v, d, in.get()); |
| 64 | |
| 65 | auto expected = AllocateAligned<T>(N); |
| 66 | uint8_t* expected_bytes = reinterpret_cast<uint8_t*>(expected.get()); |
| 67 | |
| 68 | const size_t block_size = HWY_MIN(N8, 16); |
| 69 | for (size_t block = 0; block < N8; block += block_size) { |
| 70 | expected_bytes[block] = 0; |
| 71 | memcpy(expected_bytes + block + 1, in_bytes + block, block_size - 1); |
| 72 | } |
| 73 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftLeftBytes<1>(v)); |
| 74 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftLeftBytes<1>(d, v)); |
| 75 | |
| 76 | for (size_t block = 0; block < N8; block += block_size) { |
| 77 | memcpy(expected_bytes + block, in_bytes + block + 1, block_size - 1); |
| 78 | expected_bytes[block + block_size - 1] = 0; |
| 79 | } |
| 80 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRightBytes<1>(d, v)); |
| 81 | #else |
| 82 | (void)d; |
| 83 | #endif // #if HWY_TARGET != HWY_SCALAR |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | HWY_NOINLINE void TestAllShiftBytes() { |
| 88 | ForIntegerTypes(ForPartialVectors<TestShiftBytes>()); |
| 89 | } |
| 90 | |
| 91 | struct TestShiftLeftLanes { |
| 92 | template <class T, class D> |
| 93 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 94 | // Scalar does not define Shift*Lanes. |
| 95 | #if HWY_TARGET != HWY_SCALAR || HWY_IDE |
| 96 | const auto v = Iota(d, T(1)); |
| 97 | const size_t N = Lanes(d); |
| 98 | if (N == 1) return; |
| 99 | auto expected = AllocateAligned<T>(N); |
| 100 | |
| 101 | HWY_ASSERT_VEC_EQ(d, v, ShiftLeftLanes<0>(v)); |
| 102 | HWY_ASSERT_VEC_EQ(d, v, ShiftLeftLanes<0>(d, v)); |
| 103 | |
| 104 | constexpr size_t kLanesPerBlock = 16 / sizeof(T); |
| 105 | |
| 106 | for (size_t i = 0; i < N; ++i) { |
| 107 | expected[i] = (i % kLanesPerBlock) == 0 ? T(0) : T(i); |
| 108 | } |
| 109 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftLeftLanes<1>(v)); |
| 110 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftLeftLanes<1>(d, v)); |
| 111 | #else |
| 112 | (void)d; |
| 113 | #endif // #if HWY_TARGET != HWY_SCALAR |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | struct TestShiftRightLanes { |
| 118 | template <class T, class D> |
| 119 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 120 | // Scalar does not define Shift*Lanes. |
| 121 | #if HWY_TARGET != HWY_SCALAR || HWY_IDE |
| 122 | const auto v = Iota(d, T(1)); |
| 123 | const size_t N = Lanes(d); |
| 124 | if (N == 1) return; |
| 125 | auto expected = AllocateAligned<T>(N); |
| 126 | |
| 127 | HWY_ASSERT_VEC_EQ(d, v, ShiftRightLanes<0>(d, v)); |
| 128 | |
| 129 | constexpr size_t kLanesPerBlock = 16 / sizeof(T); |
| 130 | |
| 131 | for (size_t i = 0; i < N; ++i) { |
| 132 | const size_t mod = i % kLanesPerBlock; |
| 133 | expected[i] = mod == (kLanesPerBlock - 1) || i >= N - 1 ? T(0) : T(2 + i); |
| 134 | } |
| 135 | HWY_ASSERT_VEC_EQ(d, expected.get(), ShiftRightLanes<1>(d, v)); |
| 136 | #else |
| 137 | (void)d; |
| 138 | #endif // #if HWY_TARGET != HWY_SCALAR |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | HWY_NOINLINE void TestAllShiftLeftLanes() { |
| 143 | ForAllTypes(ForPartialVectors<TestShiftLeftLanes>()); |
| 144 | } |
| 145 | |
| 146 | HWY_NOINLINE void TestAllShiftRightLanes() { |
| 147 | ForAllTypes(ForPartialVectors<TestShiftRightLanes>()); |
| 148 | } |
| 149 | |
| 150 | // Scalar does not define CombineShiftRightBytes. |
| 151 | #if HWY_TARGET != HWY_SCALAR || HWY_IDE |
| 152 | |
| 153 | template <int kBytes> |
| 154 | struct TestCombineShiftRightBytes { |
| 155 | template <class T, class D> |
| 156 | HWY_NOINLINE void operator()(T, D d) { |
| 157 | constexpr size_t kBlockSize = 16; |
| 158 | static_assert(kBytes < kBlockSize, "Shift count is per block"); |
| 159 | const Repartition<uint8_t, D> d8; |
| 160 | const size_t N8 = Lanes(d8); |
| 161 | if (N8 < 16) return; |
| 162 | auto hi_bytes = AllocateAligned<uint8_t>(N8); |
| 163 | auto lo_bytes = AllocateAligned<uint8_t>(N8); |
| 164 | auto expected_bytes = AllocateAligned<uint8_t>(N8); |
| 165 | uint8_t combined[2 * kBlockSize]; |
| 166 | |
| 167 | // Random inputs in each lane |
| 168 | RandomState rng; |
| 169 | for (size_t rep = 0; rep < AdjustedReps(100); ++rep) { |
| 170 | for (size_t i = 0; i < N8; ++i) { |
| 171 | hi_bytes[i] = static_cast<uint8_t>(Random64(&rng) & 0xFF); |
| 172 | lo_bytes[i] = static_cast<uint8_t>(Random64(&rng) & 0xFF); |
| 173 | } |
| 174 | for (size_t i = 0; i < N8; i += kBlockSize) { |
| 175 | // Arguments are not the same size. |
| 176 | CopyBytes<kBlockSize>(&lo_bytes[i], combined); |
| 177 | CopyBytes<kBlockSize>(&hi_bytes[i], combined + kBlockSize); |
| 178 | CopyBytes<kBlockSize>(combined + kBytes, &expected_bytes[i]); |
| 179 | } |
| 180 | |
| 181 | const auto hi = BitCast(d, Load(d8, hi_bytes.get())); |
| 182 | const auto lo = BitCast(d, Load(d8, lo_bytes.get())); |
| 183 | const auto expected = BitCast(d, Load(d8, expected_bytes.get())); |
| 184 | HWY_ASSERT_VEC_EQ(d, expected, CombineShiftRightBytes<kBytes>(d, hi, lo)); |
| 185 | } |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | template <int kLanes> |
| 190 | struct TestCombineShiftRightLanes { |
| 191 | template <class T, class D> |
| 192 | HWY_NOINLINE void operator()(T, D d) { |
| 193 | const Repartition<uint8_t, D> d8; |
| 194 | const size_t N8 = Lanes(d8); |
| 195 | if (N8 < 16) return; |
| 196 | |
| 197 | auto hi_bytes = AllocateAligned<uint8_t>(N8); |
| 198 | auto lo_bytes = AllocateAligned<uint8_t>(N8); |
| 199 | auto expected_bytes = AllocateAligned<uint8_t>(N8); |
| 200 | constexpr size_t kBlockSize = 16; |
| 201 | uint8_t combined[2 * kBlockSize]; |
| 202 | |
| 203 | // Random inputs in each lane |
| 204 | RandomState rng; |
| 205 | for (size_t rep = 0; rep < AdjustedReps(100); ++rep) { |
| 206 | for (size_t i = 0; i < N8; ++i) { |
| 207 | hi_bytes[i] = static_cast<uint8_t>(Random64(&rng) & 0xFF); |
| 208 | lo_bytes[i] = static_cast<uint8_t>(Random64(&rng) & 0xFF); |
| 209 | } |
| 210 | for (size_t i = 0; i < N8; i += kBlockSize) { |
| 211 | // Arguments are not the same size. |
| 212 | CopyBytes<kBlockSize>(&lo_bytes[i], combined); |
| 213 | CopyBytes<kBlockSize>(&hi_bytes[i], combined + kBlockSize); |
| 214 | CopyBytes<kBlockSize>(combined + kLanes * sizeof(T), |
| 215 | &expected_bytes[i]); |
| 216 | } |
| 217 | |
| 218 | const auto hi = BitCast(d, Load(d8, hi_bytes.get())); |
| 219 | const auto lo = BitCast(d, Load(d8, lo_bytes.get())); |
| 220 | const auto expected = BitCast(d, Load(d8, expected_bytes.get())); |
| 221 | HWY_ASSERT_VEC_EQ(d, expected, CombineShiftRightLanes<kLanes>(d, hi, lo)); |
| 222 | } |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | #endif // #if HWY_TARGET != HWY_SCALAR |
| 227 | |
| 228 | struct TestCombineShiftRight { |
| 229 | template <class T, class D> |
| 230 | HWY_NOINLINE void operator()(T t, D d) { |
| 231 | // Scalar does not define CombineShiftRightBytes. |
| 232 | #if HWY_TARGET != HWY_SCALAR || HWY_IDE |
| 233 | constexpr int kMaxBytes = |
| 234 | HWY_MIN(16, static_cast<int>(MaxLanes(d) * sizeof(T))); |
| 235 | constexpr int kMaxLanes = kMaxBytes / static_cast<int>(sizeof(T)); |
| 236 | TestCombineShiftRightBytes<kMaxBytes - 1>()(t, d); |
| 237 | TestCombineShiftRightBytes<HWY_MAX(kMaxBytes / 2, 1)>()(t, d); |
| 238 | TestCombineShiftRightBytes<1>()(t, d); |
| 239 | |
| 240 | TestCombineShiftRightLanes<kMaxLanes - 1>()(t, d); |
| 241 | TestCombineShiftRightLanes<HWY_MAX(kMaxLanes / 2, -1)>()(t, d); |
| 242 | TestCombineShiftRightLanes<1>()(t, d); |
| 243 | #else |
| 244 | (void)t; |
| 245 | (void)d; |
| 246 | #endif |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | HWY_NOINLINE void TestAllCombineShiftRight() { |
| 251 | // Need at least 2 lanes. |
| 252 | ForAllTypes(ForShrinkableVectors<TestCombineShiftRight>()); |
| 253 | } |
| 254 | |
| 255 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 256 | } // namespace HWY_NAMESPACE |
| 257 | } // namespace hwy |
| 258 | HWY_AFTER_NAMESPACE(); |
| 259 | |
| 260 | #if HWY_ONCE |
| 261 | |
| 262 | namespace hwy { |
| 263 | HWY_BEFORE_TEST(HwyBlockwiseShiftTest); |
| 264 | HWY_EXPORT_AND_TEST_P(HwyBlockwiseShiftTest, TestAllShiftBytes); |
| 265 | HWY_EXPORT_AND_TEST_P(HwyBlockwiseShiftTest, TestAllShiftLeftLanes); |
| 266 | HWY_EXPORT_AND_TEST_P(HwyBlockwiseShiftTest, TestAllShiftRightLanes); |
| 267 | HWY_EXPORT_AND_TEST_P(HwyBlockwiseShiftTest, TestAllCombineShiftRight); |
| 268 | } // namespace hwy |
| 269 | |
| 270 | #endif |