Debian Codegraph @

gcc/testsuite/gcc.dg/vect/vect-early-break_130.c

raw
1 /* { dg-require-effective-target mmap } */
2 /* { dg-add-options vect_early_break } */
3
4 #include <sys/mman.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <string.h>
9
10 /* This was reduced from gcc/fortran/scanner.cc:gfc_widechar_to_char.
11 The problem was that we omitted adding the vector skip guard when
12 versioning for aliasing. When invoked on a string that is 28 bytes
13 long, that caused us to enter the vector body after having peeled 15
14 iterations, leaving only 13 iterations to be performed as vector, but
15 the vector body performs 16 (thus overflowing the res buffer by three
16 bytes). */
17 __attribute__((noipa))
18 void f (const uint32_t *s, char *res, int length)
19 {
20 unsigned long i;
21
22 for (i = 0; i < length; i++)
23 {
24 if (s[i] > 255)
25 __builtin_abort ();
26 res[i] = (char)s[i];
27 }
28 }
29
30 int main(void)
31 {
32 long pgsz = sysconf (_SC_PAGESIZE);
33 if (pgsz == -1) {
34 fprintf (stderr, "sysconf failed: %m\n");
35 return 0;
36 }
37
38 void *p = mmap (NULL,
39 pgsz * 2,
40 PROT_READ | PROT_WRITE,
41 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
42 if (p == MAP_FAILED) {
43 fprintf (stderr, "mmap failed: %m\n");
44 return 0;
45 }
46
47 if (mprotect (p + pgsz, pgsz, PROT_NONE)) {
48 fprintf (stderr, "mprotect failed: %m\n");
49 return 0;
50 }
51
52 uint32_t in[128];
53 memset (in, 0, sizeof(in));
54
55 uintptr_t x = (uintptr_t)in;
56
57 /* We want to make our input pointer maximally misaligned (so we have
58 to peel the greatest possible number of iterations for alignment).
59 We need two bits of alignment for our uint32_t pointer to be
60 aligned. Assuming we process 16 chars per vector iteration, we
61 will need to load 16 uint32_ts, thus we need a further 4 bits of
62 alignment. */
63 const uintptr_t align_bits = 2 + 4;
64 const uintptr_t align_p2 = (1 << align_bits);
65 const uintptr_t align_p2m1 = align_p2 - 1;
66
67 if (x & align_p2m1 <= 4)
68 x &= -align_p2; /* Round down. */
69 else
70 x = (x + align_p2m1) & -align_p2; /* Round up. */
71
72 /* Add one uint32_t to get maximally misaligned. */
73 uint32_t *inp = (uint32_t *)x + 1;
74
75 const char *str = "dec-comparison-complex_1.f90";
76 long n;
77 #pragma GCC novector
78 for (n = 0; str[n]; n++)
79 inp[n] = str[n];
80
81 if (n > pgsz)
82 __builtin_abort ();
83
84 char *buf = p + pgsz - n;
85 f (inp, buf, n);
86
87 #pragma GCC novector
88 for (int i = 0; i < n; i++)
89 if (buf[i] != str[i])
90 __builtin_abort ();
91 }