| 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 | // Ensure incompabilities with Windows macros (e.g. #define StoreFence) are |
| 17 | // detected. Must come before Highway headers. |
| 18 | #include "hwy/base.h" |
| 19 | #if defined(_WIN32) || defined(_WIN64) |
| 20 | #include <windows.h> |
| 21 | #endif |
| 22 | |
| 23 | #include <stddef.h> |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | #include <algorithm> // std::fill |
| 27 | |
| 28 | #undef HWY_TARGET_INCLUDE |
| 29 | #define HWY_TARGET_INCLUDE "tests/memory_test.cc" |
| 30 | #include "hwy/cache_control.h" |
| 31 | #include "hwy/foreach_target.h" // IWYU pragma: keep |
| 32 | #include "hwy/highway.h" |
| 33 | #include "hwy/tests/test_util-inl.h" |
| 34 | |
| 35 | HWY_BEFORE_NAMESPACE(); |
| 36 | namespace hwy { |
| 37 | namespace HWY_NAMESPACE { |
| 38 | |
| 39 | struct TestLoadStore { |
| 40 | template <class T, class D> |
| 41 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 42 | const size_t N = Lanes(d); |
| 43 | const auto hi = Iota(d, static_cast<T>(1 + N)); |
| 44 | const auto lo = Iota(d, 1); |
| 45 | auto lanes = AllocateAligned<T>(2 * N); |
| 46 | Store(hi, d, &lanes[N]); |
| 47 | Store(lo, d, &lanes[0]); |
| 48 | |
| 49 | // Aligned load |
| 50 | const auto lo2 = Load(d, &lanes[0]); |
| 51 | HWY_ASSERT_VEC_EQ(d, lo2, lo); |
| 52 | |
| 53 | // Aligned store |
| 54 | auto lanes2 = AllocateAligned<T>(2 * N); |
| 55 | Store(lo2, d, &lanes2[0]); |
| 56 | Store(hi, d, &lanes2[N]); |
| 57 | for (size_t i = 0; i < 2 * N; ++i) { |
| 58 | HWY_ASSERT_EQ(lanes[i], lanes2[i]); |
| 59 | } |
| 60 | |
| 61 | // Unaligned load |
| 62 | const auto vu = LoadU(d, &lanes[1]); |
| 63 | auto lanes3 = AllocateAligned<T>(N); |
| 64 | Store(vu, d, lanes3.get()); |
| 65 | for (size_t i = 0; i < N; ++i) { |
| 66 | HWY_ASSERT_EQ(T(i + 2), lanes3[i]); |
| 67 | } |
| 68 | |
| 69 | // Unaligned store |
| 70 | StoreU(lo2, d, &lanes2[N / 2]); |
| 71 | size_t i = 0; |
| 72 | for (; i < N / 2; ++i) { |
| 73 | HWY_ASSERT_EQ(lanes[i], lanes2[i]); |
| 74 | } |
| 75 | for (; i < 3 * N / 2; ++i) { |
| 76 | HWY_ASSERT_EQ(T(i - N / 2 + 1), lanes2[i]); |
| 77 | } |
| 78 | // Subsequent values remain unchanged. |
| 79 | for (; i < 2 * N; ++i) { |
| 80 | HWY_ASSERT_EQ(T(i + 1), lanes2[i]); |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | HWY_NOINLINE void TestAllLoadStore() { |
| 86 | ForAllTypes(ForPartialVectors<TestLoadStore>()); |
| 87 | } |
| 88 | |
| 89 | struct TestSafeCopyN { |
| 90 | template <class T, class D> |
| 91 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 92 | const size_t N = Lanes(d); |
| 93 | const auto v = Iota(d, 1); |
| 94 | auto from = AllocateAligned<T>(N + 2); |
| 95 | auto to = AllocateAligned<T>(N + 2); |
| 96 | Store(v, d, from.get()); |
| 97 | |
| 98 | // 0: nothing changes |
| 99 | to[0] = T(); |
| 100 | SafeCopyN(0, d, from.get(), to.get()); |
| 101 | HWY_ASSERT_EQ(T(), to[0]); |
| 102 | |
| 103 | // 1: only first changes |
| 104 | to[1] = T(); |
| 105 | SafeCopyN(1, d, from.get(), to.get()); |
| 106 | HWY_ASSERT_EQ(static_cast<T>(1), to[0]); |
| 107 | HWY_ASSERT_EQ(T(), to[1]); |
| 108 | |
| 109 | // N-1: last does not change |
| 110 | to[N - 1] = T(); |
| 111 | SafeCopyN(N - 1, d, from.get(), to.get()); |
| 112 | HWY_ASSERT_EQ(T(), to[N - 1]); |
| 113 | // Also check preceding lanes |
| 114 | to[N - 1] = static_cast<T>(N); |
| 115 | HWY_ASSERT_VEC_EQ(d, to.get(), v); |
| 116 | |
| 117 | // N: all change |
| 118 | to[N] = T(); |
| 119 | SafeCopyN(N, d, from.get(), to.get()); |
| 120 | HWY_ASSERT_VEC_EQ(d, to.get(), v); |
| 121 | HWY_ASSERT_EQ(T(), to[N]); |
| 122 | |
| 123 | // N+1: subsequent lane does not change if using masked store |
| 124 | to[N + 1] = T(); |
| 125 | SafeCopyN(N + 1, d, from.get(), to.get()); |
| 126 | HWY_ASSERT_VEC_EQ(d, to.get(), v); |
| 127 | #if !HWY_MEM_OPS_MIGHT_FAULT |
| 128 | HWY_ASSERT_EQ(T(), to[N + 1]); |
| 129 | #endif |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | HWY_NOINLINE void TestAllSafeCopyN() { |
| 134 | ForAllTypes(ForPartialVectors<TestSafeCopyN>()); |
| 135 | } |
| 136 | |
| 137 | struct TestLoadDup128 { |
| 138 | template <class T, class D> |
| 139 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 140 | // Scalar does not define LoadDup128. |
| 141 | #if HWY_TARGET != HWY_SCALAR || HWY_IDE |
| 142 | constexpr size_t N128 = 16 / sizeof(T); |
| 143 | alignas(16) T lanes[N128]; |
| 144 | for (size_t i = 0; i < N128; ++i) { |
| 145 | lanes[i] = static_cast<T>(1 + i); |
| 146 | } |
| 147 | |
| 148 | const size_t N = Lanes(d); |
| 149 | auto expected = AllocateAligned<T>(N); |
| 150 | for (size_t i = 0; i < N; ++i) { |
| 151 | expected[i] = static_cast<T>(i % N128 + 1); |
| 152 | } |
| 153 | |
| 154 | HWY_ASSERT_VEC_EQ(d, expected.get(), LoadDup128(d, lanes)); |
| 155 | #else |
| 156 | (void)d; |
| 157 | #endif |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | HWY_NOINLINE void TestAllLoadDup128() { |
| 162 | ForAllTypes(ForGEVectors<128, TestLoadDup128>()); |
| 163 | } |
| 164 | |
| 165 | struct TestStream { |
| 166 | template <class T, class D> |
| 167 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 168 | const auto v = Iota(d, T(1)); |
| 169 | const size_t affected_bytes = |
| 170 | (Lanes(d) * sizeof(T) + HWY_STREAM_MULTIPLE - 1) & |
| 171 | ~size_t(HWY_STREAM_MULTIPLE - 1); |
| 172 | const size_t affected_lanes = affected_bytes / sizeof(T); |
| 173 | auto out = AllocateAligned<T>(2 * affected_lanes); |
| 174 | std::fill(out.get(), out.get() + 2 * affected_lanes, T(0)); |
| 175 | |
| 176 | Stream(v, d, out.get()); |
| 177 | FlushStream(); |
| 178 | const auto actual = Load(d, out.get()); |
| 179 | HWY_ASSERT_VEC_EQ(d, v, actual); |
| 180 | // Ensure Stream didn't modify more memory than expected |
| 181 | for (size_t i = affected_lanes; i < 2 * affected_lanes; ++i) { |
| 182 | HWY_ASSERT_EQ(T(0), out[i]); |
| 183 | } |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | HWY_NOINLINE void TestAllStream() { |
| 188 | const ForPartialVectors<TestStream> test; |
| 189 | // No u8,u16. |
| 190 | test(uint32_t()); |
| 191 | test(uint64_t()); |
| 192 | // No i8,i16. |
| 193 | test(int32_t()); |
| 194 | test(int64_t()); |
| 195 | ForFloatTypes(test); |
| 196 | } |
| 197 | |
| 198 | // Assumes little-endian byte order! |
| 199 | struct TestScatter { |
| 200 | template <class T, class D> |
| 201 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 202 | using Offset = MakeSigned<T>; |
| 203 | |
| 204 | const size_t N = Lanes(d); |
| 205 | const size_t range = 4 * N; // number of items to scatter |
| 206 | const size_t max_bytes = range * sizeof(T); // upper bound on offset |
| 207 | |
| 208 | RandomState rng; |
| 209 | |
| 210 | // Data to be scattered |
| 211 | auto bytes = AllocateAligned<uint8_t>(max_bytes); |
| 212 | for (size_t i = 0; i < max_bytes; ++i) { |
| 213 | bytes[i] = static_cast<uint8_t>(Random32(&rng) & 0xFF); |
| 214 | } |
| 215 | const auto data = Load(d, reinterpret_cast<const T*>(bytes.get())); |
| 216 | |
| 217 | // Scatter into these regions, ensure vector results match scalar |
| 218 | auto expected = AllocateAligned<T>(range); |
| 219 | auto actual = AllocateAligned<T>(range); |
| 220 | |
| 221 | const Rebind<Offset, D> d_offsets; |
| 222 | auto offsets = AllocateAligned<Offset>(N); // or indices |
| 223 | |
| 224 | for (size_t rep = 0; rep < 100; ++rep) { |
| 225 | // Byte offsets |
| 226 | std::fill(expected.get(), expected.get() + range, T(0)); |
| 227 | std::fill(actual.get(), actual.get() + range, T(0)); |
| 228 | for (size_t i = 0; i < N; ++i) { |
| 229 | // Must be aligned |
| 230 | offsets[i] = static_cast<Offset>((Random32(&rng) % range) * sizeof(T)); |
| 231 | CopyBytes<sizeof(T)>( |
| 232 | bytes.get() + i * sizeof(T), |
| 233 | reinterpret_cast<uint8_t*>(expected.get()) + offsets[i]); |
| 234 | } |
| 235 | const auto voffsets = Load(d_offsets, offsets.get()); |
| 236 | ScatterOffset(data, d, actual.get(), voffsets); |
| 237 | if (!BytesEqual(expected.get(), actual.get(), max_bytes)) { |
| 238 | Print(d, "Data", data); |
| 239 | Print(d_offsets, "Offsets", voffsets); |
| 240 | HWY_ASSERT(false); |
| 241 | } |
| 242 | |
| 243 | // Indices |
| 244 | std::fill(expected.get(), expected.get() + range, T(0)); |
| 245 | std::fill(actual.get(), actual.get() + range, T(0)); |
| 246 | for (size_t i = 0; i < N; ++i) { |
| 247 | offsets[i] = static_cast<Offset>(Random32(&rng) % range); |
| 248 | CopyBytes<sizeof(T)>(bytes.get() + i * sizeof(T), |
| 249 | &expected[size_t(offsets[i])]); |
| 250 | } |
| 251 | const auto vindices = Load(d_offsets, offsets.get()); |
| 252 | ScatterIndex(data, d, actual.get(), vindices); |
| 253 | if (!BytesEqual(expected.get(), actual.get(), max_bytes)) { |
| 254 | Print(d, "Data", data); |
| 255 | Print(d_offsets, "Indices", vindices); |
| 256 | HWY_ASSERT(false); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | HWY_NOINLINE void TestAllScatter() { |
| 263 | ForUIF3264(ForPartialVectors<TestScatter>()); |
| 264 | } |
| 265 | |
| 266 | struct TestGather { |
| 267 | template <class T, class D> |
| 268 | HWY_NOINLINE void operator()(T /*unused*/, D d) { |
| 269 | using Offset = MakeSigned<T>; |
| 270 | |
| 271 | const size_t N = Lanes(d); |
| 272 | const size_t range = 4 * N; // number of items to gather |
| 273 | const size_t max_bytes = range * sizeof(T); // upper bound on offset |
| 274 | |
| 275 | RandomState rng; |
| 276 | |
| 277 | // Data to be gathered from |
| 278 | auto bytes = AllocateAligned<uint8_t>(max_bytes); |
| 279 | for (size_t i = 0; i < max_bytes; ++i) { |
| 280 | bytes[i] = static_cast<uint8_t>(Random32(&rng) & 0xFF); |
| 281 | } |
| 282 | |
| 283 | auto expected = AllocateAligned<T>(N); |
| 284 | auto offsets = AllocateAligned<Offset>(N); |
| 285 | auto indices = AllocateAligned<Offset>(N); |
| 286 | |
| 287 | for (size_t rep = 0; rep < 100; ++rep) { |
| 288 | // Offsets |
| 289 | for (size_t i = 0; i < N; ++i) { |
| 290 | // Must be aligned |
| 291 | offsets[i] = static_cast<Offset>((Random32(&rng) % range) * sizeof(T)); |
| 292 | CopyBytes<sizeof(T)>(bytes.get() + offsets[i], &expected[i]); |
| 293 | } |
| 294 | |
| 295 | const Rebind<Offset, D> d_offset; |
| 296 | const T* base = reinterpret_cast<const T*>(bytes.get()); |
| 297 | auto actual = GatherOffset(d, base, Load(d_offset, offsets.get())); |
| 298 | HWY_ASSERT_VEC_EQ(d, expected.get(), actual); |
| 299 | |
| 300 | // Indices |
| 301 | for (size_t i = 0; i < N; ++i) { |
| 302 | indices[i] = |
| 303 | static_cast<Offset>(Random32(&rng) % (max_bytes / sizeof(T))); |
| 304 | CopyBytes<sizeof(T)>(base + indices[i], &expected[i]); |
| 305 | } |
| 306 | actual = GatherIndex(d, base, Load(d_offset, indices.get())); |
| 307 | HWY_ASSERT_VEC_EQ(d, expected.get(), actual); |
| 308 | } |
| 309 | } |
| 310 | }; |
| 311 | |
| 312 | HWY_NOINLINE void TestAllGather() { |
| 313 | ForUIF3264(ForPartialVectors<TestGather>()); |
| 314 | } |
| 315 | |
| 316 | HWY_NOINLINE void TestAllCache() { |
| 317 | LoadFence(); |
| 318 | FlushStream(); |
| 319 | int test = 0; |
| 320 | Prefetch(&test); |
| 321 | FlushCacheline(&test); |
| 322 | Pause(); |
| 323 | } |
| 324 | |
| 325 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 326 | } // namespace HWY_NAMESPACE |
| 327 | } // namespace hwy |
| 328 | HWY_AFTER_NAMESPACE(); |
| 329 | |
| 330 | #if HWY_ONCE |
| 331 | |
| 332 | namespace hwy { |
| 333 | HWY_BEFORE_TEST(HwyMemoryTest); |
| 334 | HWY_EXPORT_AND_TEST_P(HwyMemoryTest, TestAllLoadStore); |
| 335 | HWY_EXPORT_AND_TEST_P(HwyMemoryTest, TestAllSafeCopyN); |
| 336 | HWY_EXPORT_AND_TEST_P(HwyMemoryTest, TestAllLoadDup128); |
| 337 | HWY_EXPORT_AND_TEST_P(HwyMemoryTest, TestAllStream); |
| 338 | HWY_EXPORT_AND_TEST_P(HwyMemoryTest, TestAllScatter); |
| 339 | HWY_EXPORT_AND_TEST_P(HwyMemoryTest, TestAllGather); |
| 340 | HWY_EXPORT_AND_TEST_P(HwyMemoryTest, TestAllCache); |
| 341 | } // namespace hwy |
| 342 | |
| 343 | #endif |