| 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 | #ifndef __STDC_FORMAT_MACROS |
| 17 | #define __STDC_FORMAT_MACROS // before inttypes.h |
| 18 | #endif |
| 19 | #include <inttypes.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> // memcmp |
| 23 | |
| 24 | #undef HWY_TARGET_INCLUDE |
| 25 | #define HWY_TARGET_INCLUDE "tests/mask_mem_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 | struct TestMaskedLoad { |
| 35 | template <class T, class D> |
| 36 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 37 | RandomState rng; |
| 38 | |
| 39 | using TI = MakeSigned<T>; // For mask > 0 comparison |
| 40 | const Rebind<TI, D> di; |
| 41 | const size_t N = Lanes(d); |
| 42 | auto bool_lanes = AllocateAligned<TI>(N); |
| 43 | |
| 44 | auto lanes = AllocateAligned<T>(N); |
| 45 | Store(Iota(d, T{1}), d, lanes.get()); |
| 46 | |
| 47 | // Each lane should have a chance of having mask=true. |
| 48 | for (size_t rep = 0; rep < AdjustedReps(200); ++rep) { |
| 49 | for (size_t i = 0; i < N; ++i) { |
| 50 | bool_lanes[i] = (Random32(&rng) & 1024) ? TI(1) : TI(0); |
| 51 | } |
| 52 | |
| 53 | const auto mask = RebindMask(d, Gt(Load(di, bool_lanes.get()), Zero(di))); |
| 54 | const auto expected = IfThenElseZero(mask, Load(d, lanes.get())); |
| 55 | const auto actual = MaskedLoad(mask, d, lanes.get()); |
| 56 | HWY_ASSERT_VEC_EQ(d, expected, actual); |
| 57 | } |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | HWY_NOINLINE void TestAllMaskedLoad() { |
| 62 | ForAllTypes(ForPartialVectors<TestMaskedLoad>()); |
| 63 | } |
| 64 | |
| 65 | struct TestBlendedStore { |
| 66 | template <class T, class D> |
| 67 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 68 | RandomState rng; |
| 69 | |
| 70 | using TI = MakeSigned<T>; // For mask > 0 comparison |
| 71 | const Rebind<TI, D> di; |
| 72 | const size_t N = Lanes(d); |
| 73 | auto bool_lanes = AllocateAligned<TI>(N); |
| 74 | |
| 75 | const Vec<D> v = Iota(d, T{1}); |
| 76 | auto actual = AllocateAligned<T>(N); |
| 77 | auto expected = AllocateAligned<T>(N); |
| 78 | |
| 79 | // Each lane should have a chance of having mask=true. |
| 80 | for (size_t rep = 0; rep < AdjustedReps(200); ++rep) { |
| 81 | for (size_t i = 0; i < N; ++i) { |
| 82 | bool_lanes[i] = (Random32(&rng) & 1024) ? TI(1) : TI(0); |
| 83 | // Re-initialize to something distinct from v[i]. |
| 84 | actual[i] = static_cast<T>(127 - (i & 127)); |
| 85 | expected[i] = bool_lanes[i] ? static_cast<T>(i + 1) : actual[i]; |
| 86 | } |
| 87 | |
| 88 | const auto mask = RebindMask(d, Gt(Load(di, bool_lanes.get()), Zero(di))); |
| 89 | BlendedStore(v, mask, d, actual.get()); |
| 90 | HWY_ASSERT_VEC_EQ(d, expected.get(), Load(d, actual.get())); |
| 91 | } |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | HWY_NOINLINE void TestAllBlendedStore() { |
| 96 | ForAllTypes(ForPartialVectors<TestBlendedStore>()); |
| 97 | } |
| 98 | |
| 99 | class TestStoreMaskBits { |
| 100 | public: |
| 101 | template <class T, class D> |
| 102 | HWY_NOINLINE void operator()(T /*t*/, D /*d*/) { |
| 103 | RandomState rng; |
| 104 | using TI = MakeSigned<T>; // For mask > 0 comparison |
| 105 | const Rebind<TI, D> di; |
| 106 | const size_t N = Lanes(di); |
| 107 | auto bool_lanes = AllocateAligned<TI>(N); |
| 108 | |
| 109 | const ScalableTag<uint8_t, -3> d_bits; |
| 110 | const size_t expected_num_bytes = (N + 7) / 8; |
| 111 | auto expected = AllocateAligned<uint8_t>(expected_num_bytes); |
| 112 | auto actual = AllocateAligned<uint8_t>(HWY_MAX(8, expected_num_bytes)); |
| 113 | |
| 114 | for (size_t rep = 0; rep < AdjustedReps(200); ++rep) { |
| 115 | // Generate random mask pattern. |
| 116 | for (size_t i = 0; i < N; ++i) { |
| 117 | bool_lanes[i] = static_cast<TI>((rng() & 1024) ? 1 : 0); |
| 118 | } |
| 119 | const auto bools = Load(di, bool_lanes.get()); |
| 120 | const auto mask = Gt(bools, Zero(di)); |
| 121 | |
| 122 | // Requires at least 8 bytes, ensured above. |
| 123 | const size_t bytes_written = StoreMaskBits(di, mask, actual.get()); |
| 124 | if (bytes_written != expected_num_bytes) { |
| 125 | fprintf(stderr, "%s expected %" PRIu64 " bytes, actual %" PRIu64 "\n", |
| 126 | TypeName(T(), N).c_str(), |
| 127 | static_cast<uint64_t>(expected_num_bytes), |
| 128 | static_cast<uint64_t>(bytes_written)); |
| 129 | |
| 130 | HWY_ASSERT(false); |
| 131 | } |
| 132 | |
| 133 | // Requires at least 8 bytes, ensured above. |
| 134 | const auto mask2 = LoadMaskBits(di, actual.get()); |
| 135 | HWY_ASSERT_MASK_EQ(di, mask, mask2); |
| 136 | |
| 137 | memset(expected.get(), 0, expected_num_bytes); |
| 138 | for (size_t i = 0; i < N; ++i) { |
| 139 | expected[i / 8] = |
| 140 | static_cast<uint8_t>(expected[i / 8] | (bool_lanes[i] << (i % 8))); |
| 141 | } |
| 142 | |
| 143 | size_t i = 0; |
| 144 | // Stored bits must match original mask |
| 145 | for (; i < N; ++i) { |
| 146 | const TI is_set = (actual[i / 8] & (1 << (i % 8))) ? 1 : 0; |
| 147 | if (is_set != bool_lanes[i]) { |
| 148 | fprintf(stderr, "%s lane %" PRIu64 ": expected %d, actual %d\n", |
| 149 | TypeName(T(), N).c_str(), static_cast<uint64_t>(i), |
| 150 | static_cast<int>(bool_lanes[i]), static_cast<int>(is_set)); |
| 151 | Print(di, "bools", bools, 0, N); |
| 152 | Print(d_bits, "expected bytes", Load(d_bits, expected.get()), 0, |
| 153 | expected_num_bytes); |
| 154 | Print(d_bits, "actual bytes", Load(d_bits, actual.get()), 0, |
| 155 | expected_num_bytes); |
| 156 | |
| 157 | HWY_ASSERT(false); |
| 158 | } |
| 159 | } |
| 160 | // Any partial bits in the last byte must be zero |
| 161 | for (; i < 8 * bytes_written; ++i) { |
| 162 | const int bit = (actual[i / 8] & (1 << (i % 8))); |
| 163 | if (bit != 0) { |
| 164 | fprintf(stderr, "%s: bit #%" PRIu64 " should be zero\n", |
| 165 | TypeName(T(), N).c_str(), static_cast<uint64_t>(i)); |
| 166 | Print(di, "bools", bools, 0, N); |
| 167 | Print(d_bits, "expected bytes", Load(d_bits, expected.get()), 0, |
| 168 | expected_num_bytes); |
| 169 | Print(d_bits, "actual bytes", Load(d_bits, actual.get()), 0, |
| 170 | expected_num_bytes); |
| 171 | |
| 172 | HWY_ASSERT(false); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | HWY_NOINLINE void TestAllStoreMaskBits() { |
| 180 | ForAllTypes(ForPartialVectors<TestStoreMaskBits>()); |
| 181 | } |
| 182 | |
| 183 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 184 | } // namespace HWY_NAMESPACE |
| 185 | } // namespace hwy |
| 186 | HWY_AFTER_NAMESPACE(); |
| 187 | |
| 188 | #if HWY_ONCE |
| 189 | |
| 190 | namespace hwy { |
| 191 | HWY_BEFORE_TEST(HwyMaskTest); |
| 192 | HWY_EXPORT_AND_TEST_P(HwyMaskTest, TestAllMaskedLoad); |
| 193 | HWY_EXPORT_AND_TEST_P(HwyMaskTest, TestAllBlendedStore); |
| 194 | HWY_EXPORT_AND_TEST_P(HwyMaskTest, TestAllStoreMaskBits); |
| 195 | } // namespace hwy |
| 196 | |
| 197 | #endif |