| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-options "-O1" } */ |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <limits.h> |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
#ifndef N |
| 9 |
#define N 65 |
| 10 |
#endif |
| 11 |
|
| 12 |
#ifndef TYPE |
| 13 |
#define TYPE int32_t |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifndef DEBUG |
| 17 |
#define DEBUG 1 |
| 18 |
#endif |
| 19 |
|
| 20 |
#define BASE ((TYPE) -1 < 0 ? -126 : 4) |
| 21 |
|
| 22 |
__attribute__ ((noinline, noipa)) |
| 23 |
void fun1(TYPE *x, int n) |
| 24 |
{ |
| 25 |
for (int i = 0; i < n; i++) |
| 26 |
x[i] = (-x[i]) >> 31; |
| 27 |
} |
| 28 |
|
| 29 |
__attribute__ ((noinline, noipa, optimize("O0"))) |
| 30 |
void fun2(TYPE *x, int n) |
| 31 |
{ |
| 32 |
for (int i = 0; i < n; i++) |
| 33 |
x[i] = (-x[i]) >> 31; |
| 34 |
} |
| 35 |
|
| 36 |
int main () |
| 37 |
{ |
| 38 |
TYPE a[N]; |
| 39 |
TYPE b[N]; |
| 40 |
|
| 41 |
/* This will invoke UB due to -INT32_MIN. The test is supposed to pass |
| 42 |
because GCC is supposed to handle this UB case in a predictable way. */ |
| 43 |
a[0] = INT32_MIN; |
| 44 |
b[0] = INT32_MIN; |
| 45 |
|
| 46 |
for (int i = 1; i < N; ++i) |
| 47 |
{ |
| 48 |
a[i] = BASE + i * 13; |
| 49 |
b[i] = BASE + i * 13; |
| 50 |
if (DEBUG) |
| 51 |
printf ("%d: 0x%x\n", i, a[i]); |
| 52 |
} |
| 53 |
|
| 54 |
fun1 (a, N); |
| 55 |
fun2 (b, N); |
| 56 |
|
| 57 |
if (DEBUG) |
| 58 |
printf ("%d = 0x%x == 0x%x\n", 0, a[0], b[0]); |
| 59 |
|
| 60 |
if (a[0] != 0x0 || b[0] != -1) |
| 61 |
__builtin_abort (); |
| 62 |
|
| 63 |
|
| 64 |
for (int i = 1; i < N; ++i) |
| 65 |
{ |
| 66 |
if (DEBUG) |
| 67 |
printf ("%d = 0x%x == 0x%x\n", i, a[i], b[i]); |
| 68 |
|
| 69 |
if (a[i] != b[i]) |
| 70 |
__builtin_abort (); |
| 71 |
} |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|