Debian Codegraph @

gcc/testsuite/g++.target/riscv/pr122692-run-1.C

raw
1 /* { dg-do run { target { rv32 || rv64 } } } */
2 /* { dg-options "-O2" } */
3
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include <array>
10 #include <limits>
11 #include <memory>
12 #include <new>
13 #include <type_traits>
14
15 #define HWY_INLINE inline __attribute__((__always_inline__))
16 #define HWY_MIN(a, b) ((a) < (b) ? (a) : (b))
17 #define HWY_MAX(a, b) ((a) > (b) ? (a) : (b))
18
19 #if defined(__GNUC__) && !defined(__clang__)
20 #define NOIPA_ATTR __attribute__((__noipa__))
21 #else
22 #define NOIPA_ATTR
23 #endif
24
25 namespace test {
26
27 static __attribute__((__noinline__)) NOIPA_ATTR int Unpredictable1() {
28 int result = 1;
29 __asm__("" : "+r"(result)::);
30 return result;
31 }
32
33 class RandomState {
34 public:
35 explicit RandomState(
36 const uint64_t seed = uint64_t{0x123456789} *
37 static_cast<uint64_t>(test::Unpredictable1())) {
38 s0_ = SplitMix64(seed + 0x9E3779B97F4A7C15ull);
39 s1_ = SplitMix64(s0_);
40 }
41
42 HWY_INLINE uint64_t operator()() {
43 uint64_t s1 = s0_;
44 const uint64_t s0 = s1_;
45 const uint64_t bits = s1 + s0;
46 s0_ = s0;
47 s1 ^= s1 << 23;
48 s1 ^= s0 ^ (s1 >> 18) ^ (s0 >> 5);
49 s1_ = s1;
50 return bits;
51 }
52
53 private:
54 static uint64_t SplitMix64(uint64_t z) {
55 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
56 z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
57 return z ^ (z >> 31);
58 }
59
60 uint64_t s0_;
61 uint64_t s1_;
62 };
63
64 static __attribute__((__noinline__)) NOIPA_ATTR void GenerateRandomU16x16Vals(
65 RandomState& rng, uint16_t* const from, uint8_t* const expected) {
66 using T = uint16_t;
67 using TN = uint8_t;
68
69 const T min = 0;
70 const T max = static_cast<T>(std::numeric_limits<TN>::max());
71
72 for (size_t i = 0; i < 16; ++i) {
73 const uint64_t bits = rng();
74 __builtin_memcpy(&from[i], &bits, sizeof(T)); // not same size
75 expected[i] = static_cast<TN>(HWY_MIN(HWY_MAX(min, from[i]), max));
76 }
77 }
78
79 static __attribute__((__noinline__)) NOIPA_ATTR void DoVerifyU16x16Demote(
80 const uint16_t* const from, const uint8_t* const expected) {
81 for (int i = 0; i < 16; ++i) {
82 const uint8_t actual =
83 static_cast<uint8_t>((from[i] < 0xFF) ? from[i] : 0xFF);
84 if (expected[i] != actual) {
85 fprintf(stderr,
86 "Mismatch between expected result and actual result\nfrom=%u, "
87 "expected=%u, actual=%u\n",
88 static_cast<unsigned>(from[i]),
89 static_cast<unsigned>(expected[i]),
90 static_cast<unsigned>(actual));
91 __builtin_abort();
92 }
93 }
94 }
95
96 static void DoDemoteU16x16ToU8x16Test() {
97 using T = uint16_t;
98 using TN = uint8_t;
99 std::array<T, 16> from;
100 std::array<TN, 16> expected;
101
102 RandomState rng;
103 for (size_t rep = 0; rep < 1000; ++rep) {
104 GenerateRandomU16x16Vals(rng, from.data(), expected.data());
105 DoVerifyU16x16Demote(from.data(), expected.data());
106 }
107 }
108
109 } // namespace test
110
111 int main(int /*argc*/, char** /*argv*/) {
112 printf("Doing DoDemoteU16x16ToU8x16Test\n");
113 test::DoDemoteU16x16ToU8x16Test();
114 printf("Test completed successfully\n");
115 return 0;
116 }