Grok 10.0.5
test_util.h
Go to the documentation of this file.
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
29#include "hwy/base.h"
30#include "hwy/highway.h"
31#include "hwy/highway_export.h"
32#include "hwy/print.h"
33
34namespace hwy {
35
36// The maximum vector size used in tests when defining test data. DEPRECATED.
37constexpr size_t kTestMaxVectorSize = 64;
38
39// 64-bit random generator (Xorshift128+). Much smaller state than std::mt19937,
40// which triggers a compiler bug.
42 public:
43 explicit RandomState(const uint64_t seed = 0x123456789ull) {
44 s0_ = SplitMix64(seed + 0x9E3779B97F4A7C15ull);
46 }
47
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
70static HWY_INLINE uint32_t Random32(RandomState* rng) {
71 return static_cast<uint32_t>((*rng)());
72}
73
74static 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.
80template <class T>
81inline 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
89HWY_TEST_DLLEXPORT bool BytesEqual(const void* p1, const void* p2,
90 const size_t size, size_t* pos = nullptr);
91
92void AssertStringEqual(const char* expected, const char* actual,
93 const char* target_name, const char* filename, int line);
94
95namespace detail {
96
97template <typename T, typename TU = MakeUnsigned<T>>
98TU 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
117HWY_TEST_DLLEXPORT bool IsEqual(const TypeInfo& info, const void* expected_ptr,
118 const void* actual_ptr);
119
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
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.
137template <typename T>
138std::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.
145template <typename T>
146HWY_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
151template <typename T>
152HWY_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
162template <typename T>
163HWY_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_
#define HWY_MAX(a, b)
Definition: base.h:135
#define HWY_NORETURN
Definition: base.h:74
#define HWY_MIN(a, b)
Definition: base.h:134
#define HWY_INLINE
Definition: base.h:70
Definition: test_util.h:41
static uint64_t SplitMix64(uint64_t z)
Definition: test_util.h:60
HWY_INLINE uint64_t operator()()
Definition: test_util.h:48
uint64_t s0_
Definition: test_util.h:66
uint64_t s1_
Definition: test_util.h:67
RandomState(const uint64_t seed=0x123456789ull)
Definition: test_util.h:43
#define HWY_TEST_DLLEXPORT
Definition: highway_export.h:15
N
Definition: rvv-inl.h:1998
HWY_DLLEXPORT void TypeName(const TypeInfo &info, size_t N, char *string100)
TU ComputeUlpDelta(const T expected, const T actual)
Definition: test_util.h:98
HWY_TEST_DLLEXPORT HWY_NORETURN void PrintMismatchAndAbort(const TypeInfo &info, const void *expected_ptr, const void *actual_ptr, const char *target_name, const char *filename, int line, size_t lane=0, size_t num_lanes=1)
HWY_TEST_DLLEXPORT bool IsEqual(const TypeInfo &info, const void *expected_ptr, const void *actual_ptr)
HWY_TEST_DLLEXPORT void AssertArrayEqual(const TypeInfo &info, const void *expected_void, const void *actual_void, size_t N, const char *target_name, const char *filename, int line)
Definition: aligned_allocator.h:27
void AssertStringEqual(const char *expected, const char *actual, const char *target_name, const char *filename, int line)
HWY_TEST_DLLEXPORT bool BytesEqual(const void *p1, const void *p2, const size_t size, size_t *pos=nullptr)
constexpr size_t kTestMaxVectorSize
Definition: test_util.h:37
HWY_INLINE bool IsEqual(const T expected, const T actual)
Definition: test_util.h:146
HWY_API void CopySameSize(const From *HWY_RESTRICT from, To *HWY_RESTRICT to)
Definition: base.h:961
HWY_INLINE void AssertArrayEqual(const T *expected, const T *actual, size_t count, const char *target_name, const char *filename, int line)
Definition: test_util.h:163
static HWY_INLINE uint64_t Random64(RandomState *rng)
Definition: test_util.h:74
std::string TypeName(T, size_t N)
Definition: test_util.h:138
HWY_INLINE void AssertEqual(const T expected, const T actual, const char *target_name, const char *filename, int line, size_t lane=0)
Definition: test_util.h:152
static HWY_INLINE uint32_t Random32(RandomState *rng)
Definition: test_util.h:70
void PreventElision(T &&output)
Definition: test_util.h:81
HWY_DLLEXPORT HWY_NORETURN void int line
Definition: base.h:992
Definition: print.h:33