Fortran: Fix PR 47485.
[gcc.git] / gcc / testsuite / g++.dg / torture / pr115916.C
blob3d788678eaa3731fac051776afdd5b0ea9cfc8ce
1 /* { dg-do run } */
3 #include <stddef.h>
4 #include <stdint.h>
6 struct ve {
7     ve() = default;
8     ve(const ve&) = default;
9     ve& operator=(const ve&) = default;
11     // note that the code usually uses the first half of this array
12     uint8_t raw[16] = {};
15 static ve First8_(void) {
16     ve m;
17     __builtin_memset(m.raw, 0xff, 8);
18     return m;
21 static ve And_(ve a, ve b) {
22     ve au;
23     __builtin_memcpy(au.raw, a.raw, 16);
24     for (size_t i = 0; i < 8; ++i) {
25         au.raw[i] &= b.raw[i];
26     }
27     return au;
30 __attribute__((noipa, optimize(0)))
31 static void vec_assert(ve a) {
32     if (a.raw[6] != 0x06 && a.raw[6] != 0x07)
33         __builtin_trap();
36 static ve Reverse4_(ve v) {
37     ve ret;
38     for (size_t i = 0; i < 8; i += 4) {
39         ret.raw[i + 0] = v.raw[i + 3];
40         ret.raw[i + 1] = v.raw[i + 2];
41         ret.raw[i + 2] = v.raw[i + 1];
42         ret.raw[i + 3] = v.raw[i + 0];
43     }
44     return ret;
47 static ve DupEven_(ve v) {
48     for (size_t i = 0; i < 8; i += 2) {
49         v.raw[i + 1] = v.raw[i];
50     }
51     return v;
54 template <bool b>
55 ve Per4LaneBlockShuffle_(ve v) {
56     if (b) {
57         return Reverse4_(v);
58     } else {
59         return DupEven_(v);
60     }
63 template <bool b>
64 static inline __attribute__((always_inline)) void DoTestPer4LaneBlkShuffle(const ve v) {
65     ve actual = Per4LaneBlockShuffle_<b>(v);
66     const auto valid_lanes_mask = First8_();
67     ve actual_masked = And_(valid_lanes_mask, actual);
68     vec_assert(actual_masked);
71 static void DoTestPer4LaneBlkShuffles(const ve v) {
72     alignas(128) uint8_t src_lanes[8];
73     __builtin_memcpy(src_lanes, v.raw, 8);
74     // need both, hm
75     DoTestPer4LaneBlkShuffle<true >(v);
76     DoTestPer4LaneBlkShuffle<false>(v);
79 __attribute__((noipa, optimize(0)))
80 static void bug(void) {
81    uint8_t iv[8] = {1,2,3,4,5,6,7,8};
82    ve v;
83    __builtin_memcpy(v.raw, iv, 8);
84    DoTestPer4LaneBlkShuffles(v);
87 int main(void) {
88     bug();