| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef HWY_TESTS_TEST_UTIL_H_ |
| 17 |
#define HWY_TESTS_TEST_UTIL_H_ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
#include <stdint.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <cmath> |
| 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 |
|
| 37 |
constexpr size_t kTestMaxVectorSize = 64; |
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
template <class T> |
| 81 |
inline void PreventElision(T&& output) { |
| 82 |
#if HWY_COMPILER_MSVC |
| 83 |
(void)output; |
| 84 |
#else |
| 85 |
asm volatile("" : "+r"(output) : : "memory"); |
| 86 |
#endif |
| 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 |
|
| 100 |
if (expected == actual) return 0; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if (std::isnan(expected) && std::isnan(actual)) return 0; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
TU ux, uy; |
| 109 |
CopySameSize(&expected, &ux); |
| 110 |
CopySameSize(&actual, &uy); |
| 111 |
|
| 112 |
|
| 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 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
template <typename T> |
| 138 |
std::string TypeName(T , size_t N) { |
| 139 |
char string100[100]; |
| 140 |
detail::TypeName(detail::MakeTypeInfo<T>(), N, string100); |
| 141 |
return string100; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 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 |
} |
| 172 |
|
| 173 |
#endif |