| 1 |
/* { dg-do run { target { rv32 || rv64 } } } */ |
| 2 |
/* { dg-options "-O2" } */ |
| 3 |
|
| 4 |
#include <inttypes.h> |
| 5 |
#include <stddef.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
#include <array> |
| 11 |
#include <limits> |
| 12 |
#include <memory> |
| 13 |
#include <new> |
| 14 |
#include <type_traits> |
| 15 |
|
| 16 |
#define HWY_INLINE inline __attribute__((__always_inline__)) |
| 17 |
#define HWY_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 18 |
#define HWY_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 19 |
|
| 20 |
#if defined(__GNUC__) && !defined(__clang__) |
| 21 |
#define NOIPA_ATTR __attribute__((__noipa__)) |
| 22 |
#else |
| 23 |
#define NOIPA_ATTR |
| 24 |
#endif |
| 25 |
|
| 26 |
namespace test { |
| 27 |
|
| 28 |
static __attribute__((__noinline__)) NOIPA_ATTR int Unpredictable1() { |
| 29 |
int result = 1; |
| 30 |
__asm__("" : "+r"(result)::); |
| 31 |
return result; |
| 32 |
} |
| 33 |
|
| 34 |
class RandomState { |
| 35 |
public: |
| 36 |
explicit RandomState( |
| 37 |
const uint64_t seed = uint64_t{0x123456789} * |
| 38 |
static_cast<uint64_t>(test::Unpredictable1())) { |
| 39 |
s0_ = SplitMix64(seed + 0x9E3779B97F4A7C15ull); |
| 40 |
s1_ = SplitMix64(s0_); |
| 41 |
} |
| 42 |
|
| 43 |
HWY_INLINE uint64_t operator()() { |
| 44 |
uint64_t s1 = s0_; |
| 45 |
const uint64_t s0 = s1_; |
| 46 |
const uint64_t bits = s1 + s0; |
| 47 |
s0_ = s0; |
| 48 |
s1 ^= s1 << 23; |
| 49 |
s1 ^= s0 ^ (s1 >> 18) ^ (s0 >> 5); |
| 50 |
s1_ = s1; |
| 51 |
return bits; |
| 52 |
} |
| 53 |
|
| 54 |
private: |
| 55 |
static uint64_t SplitMix64(uint64_t z) { |
| 56 |
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; |
| 57 |
z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; |
| 58 |
return z ^ (z >> 31); |
| 59 |
} |
| 60 |
|
| 61 |
uint64_t s0_; |
| 62 |
uint64_t s1_; |
| 63 |
}; |
| 64 |
|
| 65 |
template <class T> |
| 66 |
static __attribute__((noinline)) NOIPA_ATTR T* MallocArray( |
| 67 |
size_t num_to_alloc) { |
| 68 |
static_assert(sizeof(T) > 0, "sizeof(T) > 0 must be true"); |
| 69 |
constexpr size_t kMaxNumToAlloc = |
| 70 |
std::numeric_limits<size_t>::max() / sizeof(T); |
| 71 |
if (num_to_alloc > kMaxNumToAlloc) { |
| 72 |
return nullptr; |
| 73 |
} |
| 74 |
|
| 75 |
return reinterpret_cast<T*>(::malloc(num_to_alloc * sizeof(T))); |
| 76 |
} |
| 77 |
|
| 78 |
struct CFreeDeleter { |
| 79 |
HWY_INLINE void operator()(const volatile void* ptr) const noexcept { |
| 80 |
if (ptr) { |
| 81 |
::free(const_cast<void*>(ptr)); |
| 82 |
} |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
#define HWY_ASSERT(cond) \ |
| 87 |
do { \ |
| 88 |
if (__builtin_expect(!(cond), false)) { \ |
| 89 |
fprintf(stderr, "Assertion failed at line %d of file %s: %s\n", \ |
| 90 |
static_cast<int>(__LINE__), __FILE__, "" #cond); \ |
| 91 |
fflush(stderr); \ |
| 92 |
__builtin_abort(); \ |
| 93 |
} \ |
| 94 |
} while (false) |
| 95 |
|
| 96 |
static __attribute__((__noinline__)) NOIPA_ATTR void AssertU8x16ArrayEquals( |
| 97 |
std::array<uint8_t, 16> expected, std::array<uint8_t, 16> actual, |
| 98 |
const int line, const char* filename) { |
| 99 |
for (size_t i = 0; i < 16; i++) { |
| 100 |
if (expected[i] != actual[i]) { |
| 101 |
fprintf(stderr, "Array mismatch at line %d of file %s:\n", line, |
| 102 |
filename); |
| 103 |
fprintf(stderr, |
| 104 |
"Expected: {%" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 |
| 105 |
", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 |
| 106 |
", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 |
| 107 |
", %" PRIu8 ", %" PRIu8 "}\n", |
| 108 |
expected[0], expected[1], expected[2], expected[3], expected[4], |
| 109 |
expected[5], expected[6], expected[7], expected[8], expected[9], |
| 110 |
expected[10], expected[11], expected[12], expected[13], |
| 111 |
expected[14], expected[15]); |
| 112 |
fprintf(stderr, |
| 113 |
"Actual: {%" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 |
| 114 |
", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 |
| 115 |
", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 ", %" PRIu8 |
| 116 |
", %" PRIu8 ", %" PRIu8 "}\n", |
| 117 |
actual[0], actual[1], actual[2], actual[3], actual[4], actual[5], |
| 118 |
actual[6], actual[7], actual[8], actual[9], actual[10], |
| 119 |
actual[11], actual[12], actual[13], actual[14], actual[15]); |
| 120 |
__builtin_abort(); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
#define ASSERT_U8X16_ARR_EQUALS(expected, actual) \ |
| 126 |
AssertU8x16ArrayEquals(expected, actual, __LINE__, __FILE__) |
| 127 |
|
| 128 |
static std::array<uint8_t, 16> LoadU8x16Vec(const uint8_t* __restrict ptr) { |
| 129 |
std::array<uint8_t, 16> result; |
| 130 |
__builtin_memcpy(&result, ptr, 16 * sizeof(uint8_t)); |
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
static std::array<uint16_t, 8> LoadU16x8Vec(const uint16_t* __restrict ptr) { |
| 135 |
std::array<uint16_t, 8> result; |
| 136 |
__builtin_memcpy(&result, ptr, 8 * sizeof(uint16_t)); |
| 137 |
return result; |
| 138 |
} |
| 139 |
|
| 140 |
static void DoOrderedDemote2U16x8ToU8x16Test() { |
| 141 |
using T = uint16_t; |
| 142 |
using TN = uint8_t; |
| 143 |
std::unique_ptr<T[], CFreeDeleter> from(MallocArray<T>(16)); |
| 144 |
std::unique_ptr<TN[], CFreeDeleter> expected(MallocArray<TN>(16)); |
| 145 |
HWY_ASSERT(from && expected); |
| 146 |
|
| 147 |
constexpr size_t N = 8; |
| 148 |
constexpr size_t twiceN = 16; |
| 149 |
|
| 150 |
// Narrower range in the wider type, for clamping before we cast |
| 151 |
const T min = static_cast<T>( |
| 152 |
std::is_signed_v<T> ? std::numeric_limits<TN>::lowest() : TN{0}); |
| 153 |
const T max = std::numeric_limits<TN>::max(); |
| 154 |
|
| 155 |
RandomState rng; |
| 156 |
for (size_t rep = 0; rep < 1000; ++rep) { |
| 157 |
for (size_t i = 0; i < twiceN; ++i) { |
| 158 |
const uint64_t bits = rng(); |
| 159 |
__builtin_memcpy(&from[i], &bits, sizeof(T)); // not same size |
| 160 |
expected[i] = static_cast<TN>(HWY_MIN(HWY_MAX(min, from[i]), max)); |
| 161 |
} |
| 162 |
|
| 163 |
std::array<uint8_t, 16> actual; |
| 164 |
for (size_t i = 0; i < 16; i++) { |
| 165 |
actual[i] = static_cast<uint8_t>(HWY_MIN(from[i], 0xFF)); |
| 166 |
} |
| 167 |
ASSERT_U8X16_ARR_EQUALS(LoadU8x16Vec(expected.get()), actual); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
} // namespace test |
| 172 |
|
| 173 |
int main(int /*argc*/, char** /*argv*/) { |
| 174 |
printf("Doing DoOrderedDemote2U16x8ToU8x16Test\n"); |
| 175 |
test::DoOrderedDemote2U16x8ToU8x16Test(); |
| 176 |
printf("Test completed successfully\n"); |
| 177 |
return 0; |
| 178 |
} |