Debian Codegraph @

src/lib/core/highway/hwy/tests/test_util.h

raw symbols SCIP
1 // Copyright 2021 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 HWY_TESTS_TEST_UTIL_H_
17 #define HWY_TESTS_TEST_UTIL_H_
18
19 // Target-independent helper functions for use by *_test.cc.
20
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #include <cmath> // std::isnan
26 #include <string>
27
28 #include "hwy/aligned_allocator.h"
29 #include "hwy/base.h"
30 #include "hwy/highway.h"
31 #include "hwy/highway_export.h"
32 #include "hwy/print.h"
33
34 namespace hwy {
35
36 // The maximum vector size used in tests when defining test data. DEPRECATED.
37 constexpr size_t kTestMaxVectorSize = 64;
38
39 // 64-bit random generator (Xorshift128+). Much smaller state than std::mt19937,
40 // which triggers a compiler bug.
41 class RandomState {
42 public:
43 explicit RandomState(const uint64_t seed = 0x123456789ull) {
44 s0_ = SplitMix64(seed + 0x9E3779B97F4A7C15ull);
45 s1_ = SplitMix64(s0_);
46 }
47
48 HWY_INLINE uint64_t operator()() {
49 uint64_t s1 = s0_;
50 const uint64_t s0 = s1_;
51 const uint64_t bits = s1 + s0;
52 s0_ = s0;
53 s1 ^= s1 << 23;
54 s1 ^= s0 ^ (s1 >> 18) ^ (s0 >> 5);
55 s1_ = s1;
56 return bits;
57 }
58
59 private:
60 static uint64_t SplitMix64(uint64_t z) {
61 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
62 z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
63 return z ^ (z >> 31);
64 }
65
66 uint64_t s0_;
67 uint64_t s1_;
68 };
69
70 static HWY_INLINE uint32_t Random32(RandomState* rng) {
71 return static_cast<uint32_t>((*rng)());
72 }
73
74 static HWY_INLINE uint64_t Random64(RandomState* rng) { return (*rng)(); }
75
76 // Prevents the compiler from eliding the computations that led to "output".
77 // Works by indicating to the compiler that "output" is being read and modified.
78 // The +r constraint avoids unnecessary writes to memory, but only works for
79 // built-in types.
80 template <class T>
81 inline void PreventElision(T&& output) {
82 #if HWY_COMPILER_MSVC
83 (void)output;
84 #else // HWY_COMPILER_MSVC
85 asm volatile("" : "+r"(output) : : "memory");
86 #endif // HWY_COMPILER_MSVC
87 }
88
89 HWY_TEST_DLLEXPORT bool BytesEqual(const void* p1, const void* p2,
90 const size_t size, size_t* pos = nullptr);
91
92 void AssertStringEqual(const char* expected, const char* actual,
93 const char* target_name, const char* filename, int line);
94
95 namespace detail {
96
97 template <typename T, typename TU = MakeUnsigned<T>>
98 TU ComputeUlpDelta(const T expected, const T actual) {
99 // Handle -0 == 0 and infinities.
100 if (expected == actual) return 0;
101
102 // Consider "equal" if both are NaN, so we can verify an expected NaN.
103 // Needs a special case because there are many possible NaN representations.
104 if (std::isnan(expected) && std::isnan(actual)) return 0;
105
106 // Compute the difference in units of last place. We do not need to check for
107 // differing signs; they will result in large differences, which is fine.
108 TU ux, uy;
109 CopySameSize(&expected, &ux);
110 CopySameSize(&actual, &uy);
111
112 // Avoid unsigned->signed cast: 2's complement is only guaranteed by C++20.
113 const TU ulp = HWY_MAX(ux, uy) - HWY_MIN(ux, uy);
114 return ulp;
115 }
116
117 HWY_TEST_DLLEXPORT bool IsEqual(const TypeInfo& info, const void* expected_ptr,
118 const void* actual_ptr);
119
120 HWY_TEST_DLLEXPORT HWY_NORETURN void PrintMismatchAndAbort(
121 const TypeInfo& info, const void* expected_ptr, const void* actual_ptr,
122 const char* target_name, const char* filename, int line, size_t lane = 0,
123 size_t num_lanes = 1);
124
125 HWY_TEST_DLLEXPORT void AssertArrayEqual(const TypeInfo& info,
126 const void* expected_void,
127 const void* actual_void, size_t N,
128 const char* target_name,
129 const char* filename, int line);
130
131 } // namespace detail
132
133 // Returns a name for the vector/part/scalar. The type prefix is u/i/f for
134 // unsigned/signed/floating point, followed by the number of bits per lane;
135 // then 'x' followed by the number of lanes. Example: u8x16. This is useful for
136 // understanding which instantiation of a generic test failed.
137 template <typename T>
138 std::string TypeName(T /*unused*/, size_t N) {
139 char string100[100];
140 detail::TypeName(detail::MakeTypeInfo<T>(), N, string100);
141 return string100;
142 }
143
144 // Compare non-vector, non-string T.
145 template <typename T>
146 HWY_INLINE bool IsEqual(const T expected, const T actual) {
147 const auto info = detail::MakeTypeInfo<T>();
148 return detail::IsEqual(info, &expected, &actual);
149 }
150
151 template <typename T>
152 HWY_INLINE void AssertEqual(const T expected, const T actual,
153 const char* target_name, const char* filename,
154 int line, size_t lane = 0) {
155 const auto info = detail::MakeTypeInfo<T>();
156 if (!detail::IsEqual(info, &expected, &actual)) {
157 detail::PrintMismatchAndAbort(info, &expected, &actual, target_name,
158 filename, line, lane);
159 }
160 }
161
162 template <typename T>
163 HWY_INLINE void AssertArrayEqual(const T* expected, const T* actual,
164 size_t count, const char* target_name,
165 const char* filename, int line) {
166 const auto info = hwy::detail::MakeTypeInfo<T>();
167 detail::AssertArrayEqual(info, expected, actual, count, target_name, filename,
168 line);
169 }
170
171 } // namespace hwy
172
173 #endif // HWY_TESTS_TEST_UTIL_H_