| 1 | // Copyright 2022 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> // memset |
| 19 | |
| 20 | #include <array> // IWYU pragma: keep |
| 21 | |
| 22 | #include "hwy/base.h" |
| 23 | |
| 24 | #undef HWY_TARGET_INCLUDE |
| 25 | #define HWY_TARGET_INCLUDE "tests/compress_test.cc" |
| 26 | #include "hwy/foreach_target.h" // IWYU pragma: keep |
| 27 | #include "hwy/highway.h" |
| 28 | #include "hwy/tests/test_util-inl.h" |
| 29 | |
| 30 | HWY_BEFORE_NAMESPACE(); |
| 31 | namespace hwy { |
| 32 | namespace HWY_NAMESPACE { |
| 33 | |
| 34 | // Regenerate tables used in the implementation, instead of testing. |
| 35 | #define HWY_PRINT_TABLES 0 |
| 36 | |
| 37 | #if !HWY_PRINT_TABLES || HWY_IDE |
| 38 | |
| 39 | template <class D, class DI, typename T = TFromD<D>, typename TI = TFromD<DI>> |
| 40 | void CheckStored(D d, DI di, const char* op, size_t expected_pos, |
| 41 | size_t actual_pos, size_t num_to_check, |
| 42 | const AlignedFreeUniquePtr<T[]>& in, |
| 43 | const AlignedFreeUniquePtr<TI[]>& mask_lanes, |
| 44 | const AlignedFreeUniquePtr<T[]>& expected, const T* actual_u, |
| 45 | int line) { |
| 46 | if (expected_pos != actual_pos) { |
| 47 | hwy::Abort(__FILE__, line, |
| 48 | "%s: size mismatch for %s: expected %d, actual %d\n", op, |
| 49 | TypeName(T(), Lanes(d)).c_str(), static_cast<int>(expected_pos), |
| 50 | static_cast<int>(actual_pos)); |
| 51 | } |
| 52 | // Modified from AssertVecEqual - we may not be checking all lanes. |
| 53 | for (size_t i = 0; i < num_to_check; ++i) { |
| 54 | if (!IsEqual(expected[i], actual_u[i])) { |
| 55 | const size_t N = Lanes(d); |
| 56 | fprintf(stderr, "%s: mismatch at i=%d of %d, line %d:\n\n", op, |
| 57 | static_cast<int>(i), static_cast<int>(num_to_check), line); |
| 58 | Print(di, "mask", Load(di, mask_lanes.get()), 0, N); |
| 59 | Print(d, "in", Load(d, in.get()), 0, N); |
| 60 | Print(d, "expect", Load(d, expected.get()), 0, num_to_check); |
| 61 | Print(d, "actual", Load(d, actual_u), 0, num_to_check); |
| 62 | HWY_ASSERT(false); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | struct TestCompress { |
| 68 | template <class T, class D> |
| 69 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 70 | RandomState rng; |
| 71 | |
| 72 | using TI = MakeSigned<T>; // For mask > 0 comparison |
| 73 | using TU = MakeUnsigned<T>; |
| 74 | const Rebind<TI, D> di; |
| 75 | const size_t N = Lanes(d); |
| 76 | |
| 77 | for (int frac : {0, 2, 3}) { |
| 78 | // For CompressStore |
| 79 | const size_t misalign = static_cast<size_t>(frac) * N / 4; |
| 80 | |
| 81 | auto in_lanes = AllocateAligned<T>(N); |
| 82 | auto mask_lanes = AllocateAligned<TI>(N); |
| 83 | auto garbage = AllocateAligned<TU>(N); |
| 84 | auto expected = AllocateAligned<T>(N); |
| 85 | auto actual_a = AllocateAligned<T>(misalign + N); |
| 86 | T* actual_u = actual_a.get() + misalign; |
| 87 | |
| 88 | const size_t bits_size = RoundUpTo((N + 7) / 8, 8); |
| 89 | auto bits = AllocateAligned<uint8_t>(bits_size); |
| 90 | memset(bits.get(), 0, bits_size); // for MSAN |
| 91 | |
| 92 | // Each lane should have a chance of having mask=true. |
| 93 | for (size_t rep = 0; rep < AdjustedReps(200); ++rep) { |
| 94 | size_t expected_pos = 0; |
| 95 | for (size_t i = 0; i < N; ++i) { |
| 96 | const uint64_t r = Random32(&rng); |
| 97 | in_lanes[i] = T(); // cannot initialize float16_t directly. |
| 98 | CopyBytes<sizeof(T)>(&r, &in_lanes[i]); // not same size |
| 99 | mask_lanes[i] = (Random32(&rng) & 1024) ? TI(1) : TI(0); |
| 100 | if (mask_lanes[i] > 0) { |
| 101 | expected[expected_pos++] = in_lanes[i]; |
| 102 | } |
| 103 | garbage[i] = static_cast<TU>(Random64(&rng)); |
| 104 | } |
| 105 | size_t num_to_check; |
| 106 | if (CompressIsPartition<T>::value) { |
| 107 | // For non-native Compress, also check that mask=false lanes were |
| 108 | // moved to the back of the vector (highest indices). |
| 109 | size_t extra = expected_pos; |
| 110 | for (size_t i = 0; i < N; ++i) { |
| 111 | if (mask_lanes[i] == 0) { |
| 112 | expected[extra++] = in_lanes[i]; |
| 113 | } |
| 114 | } |
| 115 | HWY_ASSERT(extra == N); |
| 116 | num_to_check = N; |
| 117 | } else { |
| 118 | // For native Compress, only the mask=true lanes are defined. |
| 119 | num_to_check = expected_pos; |
| 120 | } |
| 121 | |
| 122 | const auto in = Load(d, in_lanes.get()); |
| 123 | const auto mask = |
| 124 | RebindMask(d, Gt(Load(di, mask_lanes.get()), Zero(di))); |
| 125 | StoreMaskBits(d, mask, bits.get()); |
| 126 | |
| 127 | // Compress |
| 128 | memset(actual_u, 0, N * sizeof(T)); |
| 129 | StoreU(Compress(in, mask), d, actual_u); |
| 130 | CheckStored(d, di, "Compress", expected_pos, expected_pos, num_to_check, |
| 131 | in_lanes, mask_lanes, expected, actual_u, __LINE__); |
| 132 | |
| 133 | // CompressNot |
| 134 | memset(actual_u, 0, N * sizeof(T)); |
| 135 | StoreU(CompressNot(in, Not(mask)), d, actual_u); |
| 136 | CheckStored(d, di, "CompressNot", expected_pos, expected_pos, |
| 137 | num_to_check, in_lanes, mask_lanes, expected, actual_u, |
| 138 | __LINE__); |
| 139 | |
| 140 | // CompressStore |
| 141 | memset(actual_u, 0, N * sizeof(T)); |
| 142 | const size_t size1 = CompressStore(in, mask, d, actual_u); |
| 143 | // expected_pos instead of num_to_check because this op is not |
| 144 | // affected by CompressIsPartition. |
| 145 | CheckStored(d, di, "CompressStore", expected_pos, size1, expected_pos, |
| 146 | in_lanes, mask_lanes, expected, actual_u, __LINE__); |
| 147 | |
| 148 | // CompressBlendedStore |
| 149 | memcpy(actual_u, garbage.get(), N * sizeof(T)); |
| 150 | const size_t size2 = CompressBlendedStore(in, mask, d, actual_u); |
| 151 | // expected_pos instead of num_to_check because this op only writes |
| 152 | // the mask=true lanes. |
| 153 | CheckStored(d, di, "CompressBlendedStore", expected_pos, size2, |
| 154 | expected_pos, in_lanes, mask_lanes, expected, actual_u, |
| 155 | __LINE__); |
| 156 | // Subsequent lanes are untouched. |
| 157 | for (size_t i = size2; i < N; ++i) { |
| 158 | #if HWY_COMPILER_MSVC && HWY_TARGET == HWY_AVX2 |
| 159 | // TODO(eustas): re-enable when compiler is fixed |
| 160 | #else |
| 161 | HWY_ASSERT_EQ(garbage[i], reinterpret_cast<TU*>(actual_u)[i]); |
| 162 | #endif |
| 163 | } |
| 164 | |
| 165 | // CompressBits |
| 166 | memset(actual_u, 0, N * sizeof(T)); |
| 167 | StoreU(CompressBits(in, bits.get()), d, actual_u); |
| 168 | CheckStored(d, di, "CompressBits", expected_pos, expected_pos, |
| 169 | num_to_check, in_lanes, mask_lanes, expected, actual_u, |
| 170 | __LINE__); |
| 171 | |
| 172 | // CompressBitsStore |
| 173 | memset(actual_u, 0, N * sizeof(T)); |
| 174 | const size_t size3 = CompressBitsStore(in, bits.get(), d, actual_u); |
| 175 | // expected_pos instead of num_to_check because this op is not |
| 176 | // affected by CompressIsPartition. |
| 177 | CheckStored(d, di, "CompressBitsStore", expected_pos, size3, |
| 178 | expected_pos, in_lanes, mask_lanes, expected, actual_u, |
| 179 | __LINE__); |
| 180 | } // rep |
| 181 | } // frac |
| 182 | } // operator() |
| 183 | }; |
| 184 | |
| 185 | HWY_NOINLINE void TestAllCompress() { |
| 186 | ForAllTypes(ForPartialVectors<TestCompress>()); |
| 187 | } |
| 188 | |
| 189 | struct TestCompressBlocks { |
| 190 | template <class T, class D> |
| 191 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 192 | #if HWY_TARGET == HWY_SCALAR |
| 193 | (void)d; |
| 194 | #else |
| 195 | static_assert(sizeof(T) == 8 && !IsSigned<T>(), "Should be u64"); |
| 196 | RandomState rng; |
| 197 | |
| 198 | using TI = MakeSigned<T>; // For mask > 0 comparison |
| 199 | const Rebind<TI, D> di; |
| 200 | const size_t N = Lanes(d); |
| 201 | |
| 202 | auto in_lanes = AllocateAligned<T>(N); |
| 203 | auto mask_lanes = AllocateAligned<TI>(N); |
| 204 | auto expected = AllocateAligned<T>(N); |
| 205 | auto actual = AllocateAligned<T>(N); |
| 206 | |
| 207 | // Each lane should have a chance of having mask=true. |
| 208 | for (size_t rep = 0; rep < AdjustedReps(200); ++rep) { |
| 209 | size_t expected_pos = 0; |
| 210 | for (size_t i = 0; i < N; i += 2) { |
| 211 | const uint64_t bits = Random32(&rng); |
| 212 | in_lanes[i + 1] = in_lanes[i] = T(); // cannot set float16_t directly. |
| 213 | CopyBytes<sizeof(T)>(&bits, &in_lanes[i]); // not same size |
| 214 | CopyBytes<sizeof(T)>(&bits, &in_lanes[i + 1]); // not same size |
| 215 | mask_lanes[i + 1] = mask_lanes[i] = TI{(Random32(&rng) & 8) ? 1 : 0}; |
| 216 | if (mask_lanes[i] > 0) { |
| 217 | expected[expected_pos++] = in_lanes[i]; |
| 218 | expected[expected_pos++] = in_lanes[i + 1]; |
| 219 | } |
| 220 | } |
| 221 | size_t num_to_check; |
| 222 | if (CompressIsPartition<T>::value) { |
| 223 | // For non-native Compress, also check that mask=false lanes were |
| 224 | // moved to the back of the vector (highest indices). |
| 225 | size_t extra = expected_pos; |
| 226 | for (size_t i = 0; i < N; ++i) { |
| 227 | if (mask_lanes[i] == 0) { |
| 228 | expected[extra++] = in_lanes[i]; |
| 229 | } |
| 230 | } |
| 231 | HWY_ASSERT(extra == N); |
| 232 | num_to_check = N; |
| 233 | } else { |
| 234 | // For native Compress, only the mask=true lanes are defined. |
| 235 | num_to_check = expected_pos; |
| 236 | } |
| 237 | |
| 238 | const auto in = Load(d, in_lanes.get()); |
| 239 | const auto mask = RebindMask(d, Gt(Load(di, mask_lanes.get()), Zero(di))); |
| 240 | |
| 241 | // CompressBlocksNot |
| 242 | memset(actual.get(), 0, N * sizeof(T)); |
| 243 | StoreU(CompressBlocksNot(in, Not(mask)), d, actual.get()); |
| 244 | CheckStored(d, di, "CompressBlocksNot", expected_pos, expected_pos, |
| 245 | num_to_check, in_lanes, mask_lanes, expected, actual.get(), |
| 246 | __LINE__); |
| 247 | } // rep |
| 248 | #endif // HWY_TARGET == HWY_SCALAR |
| 249 | } // operator() |
| 250 | }; |
| 251 | |
| 252 | HWY_NOINLINE void TestAllCompressBlocks() { |
| 253 | ForGE128Vectors<TestCompressBlocks>()(uint64_t()); |
| 254 | } |
| 255 | |
| 256 | #endif // !HWY_PRINT_TABLES |
| 257 | |
| 258 | #if HWY_PRINT_TABLES || HWY_IDE |
| 259 | namespace detail { // for code folding |
| 260 | |
| 261 | void PrintCompress8x8Tables() { |
| 262 | printf("======================================= 8x8\n"); |
| 263 | constexpr size_t N = 8; |
| 264 | for (uint64_t code = 0; code < (1ull << N); ++code) { |
| 265 | std::array<uint8_t, N> indices{0}; |
| 266 | size_t pos = 0; |
| 267 | // All lanes where mask = true |
| 268 | for (size_t i = 0; i < N; ++i) { |
| 269 | if (code & (1ull << i)) { |
| 270 | indices[pos++] = i; |
| 271 | } |
| 272 | } |
| 273 | // All lanes where mask = false |
| 274 | for (size_t i = 0; i < N; ++i) { |
| 275 | if (!(code & (1ull << i))) { |
| 276 | indices[pos++] = i; |
| 277 | } |
| 278 | } |
| 279 | HWY_ASSERT(pos == N); |
| 280 | |
| 281 | for (size_t i = 0; i < N; ++i) { |
| 282 | printf("%d,", indices[i]); |
| 283 | } |
| 284 | printf(code & 1 ? "//\n" : "/**/"); |
| 285 | } |
| 286 | printf("\n"); |
| 287 | } |
| 288 | |
| 289 | void PrintCompress16x8Tables() { |
| 290 | printf("======================================= 16x8\n"); |
| 291 | constexpr size_t N = 8; // 128-bit SIMD |
| 292 | for (uint64_t code = 0; code < (1ull << N); ++code) { |
| 293 | std::array<uint8_t, N> indices{0}; |
| 294 | size_t pos = 0; |
| 295 | // All lanes where mask = true |
| 296 | for (size_t i = 0; i < N; ++i) { |
| 297 | if (code & (1ull << i)) { |
| 298 | indices[pos++] = i; |
| 299 | } |
| 300 | } |
| 301 | // All lanes where mask = false |
| 302 | for (size_t i = 0; i < N; ++i) { |
| 303 | if (!(code & (1ull << i))) { |
| 304 | indices[pos++] = i; |
| 305 | } |
| 306 | } |
| 307 | HWY_ASSERT(pos == N); |
| 308 | |
| 309 | // Doubled (for converting lane to byte indices) |
| 310 | for (size_t i = 0; i < N; ++i) { |
| 311 | printf("%d,", 2 * indices[i]); |
| 312 | } |
| 313 | printf(code & 1 ? "//\n" : "/**/"); |
| 314 | } |
| 315 | printf("\n"); |
| 316 | } |
| 317 | |
| 318 | void PrintCompressNot16x8Tables() { |
| 319 | printf("======================================= Not 16x8\n"); |
| 320 | constexpr size_t N = 8; // 128-bit SIMD |
| 321 | for (uint64_t not_code = 0; not_code < (1ull << N); ++not_code) { |
| 322 | const uint64_t code = ~not_code; |
| 323 | std::array<uint8_t, N> indices{0}; |
| 324 | size_t pos = 0; |
| 325 | // All lanes where mask = true |
| 326 | for (size_t i = 0; i < N; ++i) { |
| 327 | if (code & (1ull << i)) { |
| 328 | indices[pos++] = i; |
| 329 | } |
| 330 | } |
| 331 | // All lanes where mask = false |
| 332 | for (size_t i = 0; i < N; ++i) { |
| 333 | if (!(code & (1ull << i))) { |
| 334 | indices[pos++] = i; |
| 335 | } |
| 336 | } |
| 337 | HWY_ASSERT(pos == N); |
| 338 | |
| 339 | // Doubled (for converting lane to byte indices) |
| 340 | for (size_t i = 0; i < N; ++i) { |
| 341 | printf("%d,", 2 * indices[i]); |
| 342 | } |
| 343 | printf(not_code & 1 ? "//\n" : "/**/"); |
| 344 | } |
| 345 | printf("\n"); |
| 346 | } |
| 347 | |
| 348 | // Compressed to nibbles, unpacked via variable right shift. Also includes |
| 349 | // FirstN bits in the nibble MSB. |
| 350 | void PrintCompress32x8Tables() { |
| 351 | printf("======================================= 32/64x8\n"); |
| 352 | constexpr size_t N = 8; // AVX2 or 64-bit AVX3 |
| 353 | for (uint64_t code = 0; code < (1ull << N); ++code) { |
| 354 | const size_t count = PopCount(code); |
| 355 | std::array<uint32_t, N> indices{0}; |
| 356 | size_t pos = 0; |
| 357 | // All lanes where mask = true |
| 358 | for (size_t i = 0; i < N; ++i) { |
| 359 | if (code & (1ull << i)) { |
| 360 | indices[pos++] = i; |
| 361 | } |
| 362 | } |
| 363 | // All lanes where mask = false |
| 364 | for (size_t i = 0; i < N; ++i) { |
| 365 | if (!(code & (1ull << i))) { |
| 366 | indices[pos++] = i; |
| 367 | } |
| 368 | } |
| 369 | HWY_ASSERT(pos == N); |
| 370 | |
| 371 | // Convert to nibbles |
| 372 | uint64_t packed = 0; |
| 373 | for (size_t i = 0; i < N; ++i) { |
| 374 | HWY_ASSERT(indices[i] < N); |
| 375 | if (i < count) { |
| 376 | indices[i] |= N; |
| 377 | HWY_ASSERT(indices[i] < 0x10); |
| 378 | } |
| 379 | packed += indices[i] << (i * 4); |
| 380 | } |
| 381 | |
| 382 | HWY_ASSERT(packed < (1ull << (N * 4))); |
| 383 | printf("0x%08x,", static_cast<uint32_t>(packed)); |
| 384 | } |
| 385 | printf("\n"); |
| 386 | } |
| 387 | |
| 388 | void PrintCompressNot32x8Tables() { |
| 389 | printf("======================================= Not 32/64x8\n"); |
| 390 | constexpr size_t N = 8; // AVX2 or 64-bit AVX3 |
| 391 | for (uint64_t not_code = 0; not_code < (1ull << N); ++not_code) { |
| 392 | const uint64_t code = ~not_code; |
| 393 | const size_t count = PopCount(code); |
| 394 | std::array<uint32_t, N> indices{0}; |
| 395 | size_t pos = 0; |
| 396 | // All lanes where mask = true |
| 397 | for (size_t i = 0; i < N; ++i) { |
| 398 | if (code & (1ull << i)) { |
| 399 | indices[pos++] = i; |
| 400 | } |
| 401 | } |
| 402 | // All lanes where mask = false |
| 403 | for (size_t i = 0; i < N; ++i) { |
| 404 | if (!(code & (1ull << i))) { |
| 405 | indices[pos++] = i; |
| 406 | } |
| 407 | } |
| 408 | HWY_ASSERT(pos == N); |
| 409 | |
| 410 | // Convert to nibbles |
| 411 | uint64_t packed = 0; |
| 412 | for (size_t i = 0; i < N; ++i) { |
| 413 | HWY_ASSERT(indices[i] < N); |
| 414 | if (i < count) { |
| 415 | indices[i] |= N; |
| 416 | HWY_ASSERT(indices[i] < 0x10); |
| 417 | } |
| 418 | packed += indices[i] << (i * 4); |
| 419 | } |
| 420 | |
| 421 | HWY_ASSERT(packed < (1ull << (N * 4))); |
| 422 | printf("0x%08x,", static_cast<uint32_t>(packed)); |
| 423 | } |
| 424 | printf("\n"); |
| 425 | } |
| 426 | |
| 427 | // Compressed to nibbles (for AVX3 64x4) |
| 428 | void PrintCompress64x4NibbleTables() { |
| 429 | printf("======================================= 64x4Nibble\n"); |
| 430 | constexpr size_t N = 4; // AVX2 |
| 431 | for (uint64_t code = 0; code < (1ull << N); ++code) { |
| 432 | std::array<uint32_t, N> indices{0}; |
| 433 | size_t pos = 0; |
| 434 | // All lanes where mask = true |
| 435 | for (size_t i = 0; i < N; ++i) { |
| 436 | if (code & (1ull << i)) { |
| 437 | indices[pos++] = i; |
| 438 | } |
| 439 | } |
| 440 | // All lanes where mask = false |
| 441 | for (size_t i = 0; i < N; ++i) { |
| 442 | if (!(code & (1ull << i))) { |
| 443 | indices[pos++] = i; |
| 444 | } |
| 445 | } |
| 446 | HWY_ASSERT(pos == N); |
| 447 | |
| 448 | // Convert to nibbles |
| 449 | uint64_t packed = 0; |
| 450 | for (size_t i = 0; i < N; ++i) { |
| 451 | HWY_ASSERT(indices[i] < N); |
| 452 | packed += indices[i] << (i * 4); |
| 453 | } |
| 454 | |
| 455 | HWY_ASSERT(packed < (1ull << (N * 4))); |
| 456 | printf("0x%08x,", static_cast<uint32_t>(packed)); |
| 457 | } |
| 458 | printf("\n"); |
| 459 | } |
| 460 | |
| 461 | void PrintCompressNot64x4NibbleTables() { |
| 462 | printf("======================================= Not 64x4Nibble\n"); |
| 463 | constexpr size_t N = 4; // AVX2 |
| 464 | for (uint64_t not_code = 0; not_code < (1ull << N); ++not_code) { |
| 465 | const uint64_t code = ~not_code; |
| 466 | std::array<uint32_t, N> indices{0}; |
| 467 | size_t pos = 0; |
| 468 | // All lanes where mask = true |
| 469 | for (size_t i = 0; i < N; ++i) { |
| 470 | if (code & (1ull << i)) { |
| 471 | indices[pos++] = i; |
| 472 | } |
| 473 | } |
| 474 | // All lanes where mask = false |
| 475 | for (size_t i = 0; i < N; ++i) { |
| 476 | if (!(code & (1ull << i))) { |
| 477 | indices[pos++] = i; |
| 478 | } |
| 479 | } |
| 480 | HWY_ASSERT(pos == N); |
| 481 | |
| 482 | // Convert to nibbles |
| 483 | uint64_t packed = 0; |
| 484 | for (size_t i = 0; i < N; ++i) { |
| 485 | HWY_ASSERT(indices[i] < N); |
| 486 | packed += indices[i] << (i * 4); |
| 487 | } |
| 488 | |
| 489 | HWY_ASSERT(packed < (1ull << (N * 4))); |
| 490 | printf("0x%08x,", static_cast<uint32_t>(packed)); |
| 491 | } |
| 492 | printf("\n"); |
| 493 | } |
| 494 | |
| 495 | void PrintCompressNot64x2NibbleTables() { |
| 496 | printf("======================================= Not 64x2Nibble\n"); |
| 497 | constexpr size_t N = 2; // 128-bit |
| 498 | for (uint64_t not_code = 0; not_code < (1ull << N); ++not_code) { |
| 499 | const uint64_t code = ~not_code; |
| 500 | std::array<uint32_t, N> indices{0}; |
| 501 | size_t pos = 0; |
| 502 | // All lanes where mask = true |
| 503 | for (size_t i = 0; i < N; ++i) { |
| 504 | if (code & (1ull << i)) { |
| 505 | indices[pos++] = i; |
| 506 | } |
| 507 | } |
| 508 | // All lanes where mask = false |
| 509 | for (size_t i = 0; i < N; ++i) { |
| 510 | if (!(code & (1ull << i))) { |
| 511 | indices[pos++] = i; |
| 512 | } |
| 513 | } |
| 514 | HWY_ASSERT(pos == N); |
| 515 | |
| 516 | // Convert to nibbles |
| 517 | uint64_t packed = 0; |
| 518 | for (size_t i = 0; i < N; ++i) { |
| 519 | HWY_ASSERT(indices[i] < N); |
| 520 | packed += indices[i] << (i * 4); |
| 521 | } |
| 522 | |
| 523 | HWY_ASSERT(packed < (1ull << (N * 4))); |
| 524 | printf("0x%08x,", static_cast<uint32_t>(packed)); |
| 525 | } |
| 526 | printf("\n"); |
| 527 | } |
| 528 | |
| 529 | void PrintCompress64x4Tables() { |
| 530 | printf("======================================= 64x4 uncompressed\n"); |
| 531 | constexpr size_t N = 4; // SVE_256 |
| 532 | for (uint64_t code = 0; code < (1ull << N); ++code) { |
| 533 | std::array<size_t, N> indices{0}; |
| 534 | size_t pos = 0; |
| 535 | // All lanes where mask = true |
| 536 | for (size_t i = 0; i < N; ++i) { |
| 537 | if (code & (1ull << i)) { |
| 538 | indices[pos++] = i; |
| 539 | } |
| 540 | } |
| 541 | // All lanes where mask = false |
| 542 | for (size_t i = 0; i < N; ++i) { |
| 543 | if (!(code & (1ull << i))) { |
| 544 | indices[pos++] = i; |
| 545 | } |
| 546 | } |
| 547 | HWY_ASSERT(pos == N); |
| 548 | |
| 549 | // Store uncompressed indices because SVE TBL returns 0 if an index is out |
| 550 | // of bounds. On AVX3 we simply variable-shift because permute indices are |
| 551 | // interpreted modulo N. Compression is not worth the extra shift+AND |
| 552 | // because the table is anyway only 512 bytes. |
| 553 | for (size_t i = 0; i < N; ++i) { |
| 554 | printf("%d,", static_cast<int>(indices[i])); |
| 555 | } |
| 556 | } |
| 557 | printf("\n"); |
| 558 | } |
| 559 | |
| 560 | void PrintCompressNot64x4Tables() { |
| 561 | printf("======================================= Not 64x4 uncompressed\n"); |
| 562 | constexpr size_t N = 4; // SVE_256 |
| 563 | for (uint64_t not_code = 0; not_code < (1ull << N); ++not_code) { |
| 564 | const uint64_t code = ~not_code; |
| 565 | std::array<size_t, N> indices{0}; |
| 566 | size_t pos = 0; |
| 567 | // All lanes where mask = true |
| 568 | for (size_t i = 0; i < N; ++i) { |
| 569 | if (code & (1ull << i)) { |
| 570 | indices[pos++] = i; |
| 571 | } |
| 572 | } |
| 573 | // All lanes where mask = false |
| 574 | for (size_t i = 0; i < N; ++i) { |
| 575 | if (!(code & (1ull << i))) { |
| 576 | indices[pos++] = i; |
| 577 | } |
| 578 | } |
| 579 | HWY_ASSERT(pos == N); |
| 580 | |
| 581 | // Store uncompressed indices because SVE TBL returns 0 if an index is out |
| 582 | // of bounds. On AVX3 we simply variable-shift because permute indices are |
| 583 | // interpreted modulo N. Compression is not worth the extra shift+AND |
| 584 | // because the table is anyway only 512 bytes. |
| 585 | for (size_t i = 0; i < N; ++i) { |
| 586 | printf("%d,", static_cast<int>(indices[i])); |
| 587 | } |
| 588 | } |
| 589 | printf("\n"); |
| 590 | } |
| 591 | |
| 592 | // Same as above, but prints pairs of u32 indices (for AVX2). Also includes |
| 593 | // FirstN bits in the nibble MSB. |
| 594 | void PrintCompress64x4PairTables() { |
| 595 | printf("======================================= 64x4 u32 index\n"); |
| 596 | constexpr size_t N = 4; // AVX2 |
| 597 | for (uint64_t code = 0; code < (1ull << N); ++code) { |
| 598 | const size_t count = PopCount(code); |
| 599 | std::array<size_t, N> indices{0}; |
| 600 | size_t pos = 0; |
| 601 | // All lanes where mask = true |
| 602 | for (size_t i = 0; i < N; ++i) { |
| 603 | if (code & (1ull << i)) { |
| 604 | indices[pos++] = i; |
| 605 | } |
| 606 | } |
| 607 | // All lanes where mask = false |
| 608 | for (size_t i = 0; i < N; ++i) { |
| 609 | if (!(code & (1ull << i))) { |
| 610 | indices[pos++] = i; |
| 611 | } |
| 612 | } |
| 613 | HWY_ASSERT(pos == N); |
| 614 | |
| 615 | // Store uncompressed indices because SVE TBL returns 0 if an index is out |
| 616 | // of bounds. On AVX3 we simply variable-shift because permute indices are |
| 617 | // interpreted modulo N. Compression is not worth the extra shift+AND |
| 618 | // because the table is anyway only 512 bytes. |
| 619 | for (size_t i = 0; i < N; ++i) { |
| 620 | const int first_n_bit = i < count ? 8 : 0; |
| 621 | const int low = static_cast<int>(2 * indices[i]) + first_n_bit; |
| 622 | HWY_ASSERT(low < 0x10); |
| 623 | printf("%d, %d, ", low, low + 1); |
| 624 | } |
| 625 | } |
| 626 | printf("\n"); |
| 627 | } |
| 628 | |
| 629 | void PrintCompressNot64x4PairTables() { |
| 630 | printf("======================================= Not 64x4 u32 index\n"); |
| 631 | constexpr size_t N = 4; // AVX2 |
| 632 | for (uint64_t not_code = 0; not_code < (1ull << N); ++not_code) { |
| 633 | const uint64_t code = ~not_code; |
| 634 | const size_t count = PopCount(code); |
| 635 | std::array<size_t, N> indices{0}; |
| 636 | size_t pos = 0; |
| 637 | // All lanes where mask = true |
| 638 | for (size_t i = 0; i < N; ++i) { |
| 639 | if (code & (1ull << i)) { |
| 640 | indices[pos++] = i; |
| 641 | } |
| 642 | } |
| 643 | // All lanes where mask = false |
| 644 | for (size_t i = 0; i < N; ++i) { |
| 645 | if (!(code & (1ull << i))) { |
| 646 | indices[pos++] = i; |
| 647 | } |
| 648 | } |
| 649 | HWY_ASSERT(pos == N); |
| 650 | |
| 651 | // Store uncompressed indices because SVE TBL returns 0 if an index is out |
| 652 | // of bounds. On AVX3 we simply variable-shift because permute indices are |
| 653 | // interpreted modulo N. Compression is not worth the extra shift+AND |
| 654 | // because the table is anyway only 512 bytes. |
| 655 | for (size_t i = 0; i < N; ++i) { |
| 656 | const int first_n_bit = i < count ? 8 : 0; |
| 657 | const int low = static_cast<int>(2 * indices[i]) + first_n_bit; |
| 658 | HWY_ASSERT(low < 0x10); |
| 659 | printf("%d, %d, ", low, low + 1); |
| 660 | } |
| 661 | } |
| 662 | printf("\n"); |
| 663 | } |
| 664 | |
| 665 | // 4-tuple of byte indices |
| 666 | void PrintCompress32x4Tables() { |
| 667 | printf("======================================= 32x4\n"); |
| 668 | using T = uint32_t; |
| 669 | constexpr size_t N = 4; // SSE4 |
| 670 | for (uint64_t code = 0; code < (1ull << N); ++code) { |
| 671 | std::array<uint32_t, N> indices{0}; |
| 672 | size_t pos = 0; |
| 673 | // All lanes where mask = true |
| 674 | for (size_t i = 0; i < N; ++i) { |
| 675 | if (code & (1ull << i)) { |
| 676 | indices[pos++] = i; |
| 677 | } |
| 678 | } |
| 679 | // All lanes where mask = false |
| 680 | for (size_t i = 0; i < N; ++i) { |
| 681 | if (!(code & (1ull << i))) { |
| 682 | indices[pos++] = i; |
| 683 | } |
| 684 | } |
| 685 | HWY_ASSERT(pos == N); |
| 686 | |
| 687 | for (size_t i = 0; i < N; ++i) { |
| 688 | for (size_t idx_byte = 0; idx_byte < sizeof(T); ++idx_byte) { |
| 689 | printf("%d,", static_cast<int>(sizeof(T) * indices[i] + idx_byte)); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | printf("\n"); |
| 694 | } |
| 695 | |
| 696 | void PrintCompressNot32x4Tables() { |
| 697 | printf("======================================= Not 32x4\n"); |
| 698 | using T = uint32_t; |
| 699 | constexpr size_t N = 4; // SSE4 |
| 700 | for (uint64_t not_code = 0; not_code < (1ull << N); ++not_code) { |
| 701 | const uint64_t code = ~not_code; |
| 702 | std::array<uint32_t, N> indices{0}; |
| 703 | size_t pos = 0; |
| 704 | // All lanes where mask = true |
| 705 | for (size_t i = 0; i < N; ++i) { |
| 706 | if (code & (1ull << i)) { |
| 707 | indices[pos++] = i; |
| 708 | } |
| 709 | } |
| 710 | // All lanes where mask = false |
| 711 | for (size_t i = 0; i < N; ++i) { |
| 712 | if (!(code & (1ull << i))) { |
| 713 | indices[pos++] = i; |
| 714 | } |
| 715 | } |
| 716 | HWY_ASSERT(pos == N); |
| 717 | |
| 718 | for (size_t i = 0; i < N; ++i) { |
| 719 | for (size_t idx_byte = 0; idx_byte < sizeof(T); ++idx_byte) { |
| 720 | printf("%d,", static_cast<int>(sizeof(T) * indices[i] + idx_byte)); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | printf("\n"); |
| 725 | } |
| 726 | |
| 727 | // 8-tuple of byte indices |
| 728 | void PrintCompress64x2Tables() { |
| 729 | printf("======================================= 64x2\n"); |
| 730 | using T = uint64_t; |
| 731 | constexpr size_t N = 2; // SSE4 |
| 732 | for (uint64_t code = 0; code < (1ull << N); ++code) { |
| 733 | std::array<uint32_t, N> indices{0}; |
| 734 | size_t pos = 0; |
| 735 | // All lanes where mask = true |
| 736 | for (size_t i = 0; i < N; ++i) { |
| 737 | if (code & (1ull << i)) { |
| 738 | indices[pos++] = i; |
| 739 | } |
| 740 | } |
| 741 | // All lanes where mask = false |
| 742 | for (size_t i = 0; i < N; ++i) { |
| 743 | if (!(code & (1ull << i))) { |
| 744 | indices[pos++] = i; |
| 745 | } |
| 746 | } |
| 747 | HWY_ASSERT(pos == N); |
| 748 | |
| 749 | for (size_t i = 0; i < N; ++i) { |
| 750 | for (size_t idx_byte = 0; idx_byte < sizeof(T); ++idx_byte) { |
| 751 | printf("%d,", static_cast<int>(sizeof(T) * indices[i] + idx_byte)); |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | printf("\n"); |
| 756 | } |
| 757 | |
| 758 | void PrintCompressNot64x2Tables() { |
| 759 | printf("======================================= Not 64x2\n"); |
| 760 | using T = uint64_t; |
| 761 | constexpr size_t N = 2; // SSE4 |
| 762 | for (uint64_t not_code = 0; not_code < (1ull << N); ++not_code) { |
| 763 | const uint64_t code = ~not_code; |
| 764 | std::array<uint32_t, N> indices{0}; |
| 765 | size_t pos = 0; |
| 766 | // All lanes where mask = true |
| 767 | for (size_t i = 0; i < N; ++i) { |
| 768 | if (code & (1ull << i)) { |
| 769 | indices[pos++] = i; |
| 770 | } |
| 771 | } |
| 772 | // All lanes where mask = false |
| 773 | for (size_t i = 0; i < N; ++i) { |
| 774 | if (!(code & (1ull << i))) { |
| 775 | indices[pos++] = i; |
| 776 | } |
| 777 | } |
| 778 | HWY_ASSERT(pos == N); |
| 779 | |
| 780 | for (size_t i = 0; i < N; ++i) { |
| 781 | for (size_t idx_byte = 0; idx_byte < sizeof(T); ++idx_byte) { |
| 782 | printf("%d,", static_cast<int>(sizeof(T) * indices[i] + idx_byte)); |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | printf("\n"); |
| 787 | } |
| 788 | |
| 789 | } // namespace detail |
| 790 | |
| 791 | HWY_NOINLINE void PrintTables() { |
| 792 | // Only print once. |
| 793 | #if HWY_TARGET == HWY_STATIC_TARGET |
| 794 | detail::PrintCompress32x8Tables(); |
| 795 | detail::PrintCompressNot32x8Tables(); |
| 796 | detail::PrintCompress64x4NibbleTables(); |
| 797 | detail::PrintCompressNot64x4NibbleTables(); |
| 798 | detail::PrintCompressNot64x2NibbleTables(); |
| 799 | detail::PrintCompress64x4Tables(); |
| 800 | detail::PrintCompressNot64x4Tables(); |
| 801 | detail::PrintCompress32x4Tables(); |
| 802 | detail::PrintCompressNot32x4Tables(); |
| 803 | detail::PrintCompress64x2Tables(); |
| 804 | detail::PrintCompressNot64x2Tables(); |
| 805 | detail::PrintCompress64x4PairTables(); |
| 806 | detail::PrintCompressNot64x4PairTables(); |
| 807 | detail::PrintCompress16x8Tables(); |
| 808 | detail::PrintCompress8x8Tables(); |
| 809 | detail::PrintCompressNot16x8Tables(); |
| 810 | #endif |
| 811 | } |
| 812 | |
| 813 | #endif // HWY_PRINT_TABLES |
| 814 | |
| 815 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 816 | } // namespace HWY_NAMESPACE |
| 817 | } // namespace hwy |
| 818 | HWY_AFTER_NAMESPACE(); |
| 819 | |
| 820 | #if HWY_ONCE |
| 821 | |
| 822 | namespace hwy { |
| 823 | HWY_BEFORE_TEST(HwyCompressTest); |
| 824 | #if HWY_PRINT_TABLES |
| 825 | // Only print instead of running tests; this will be visible in the log. |
| 826 | HWY_EXPORT_AND_TEST_P(HwyCompressTest, PrintTables); |
| 827 | #else |
| 828 | HWY_EXPORT_AND_TEST_P(HwyCompressTest, TestAllCompress); |
| 829 | HWY_EXPORT_AND_TEST_P(HwyCompressTest, TestAllCompressBlocks); |
| 830 | #endif |
| 831 | } // namespace hwy |
| 832 | |
| 833 | #endif |