aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/torture/pr110838.c
blob: ae2874b6d0d9dee023861da245e0524eec645ae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* { dg-do run } */

typedef __UINT32_TYPE__ uint32_t;
typedef __UINT8_TYPE__ uint8_t;
typedef __INT8_TYPE__ int8_t;
typedef uint8_t pixel;

#define MSB (__CHAR_BIT__ * __SIZEOF_INT__ - 1)

/* get the sign of input variable (TODO: this is a dup, make common) */
static inline int8_t signOf(int x)
{
  return (x >> MSB) | ((int)((((uint32_t)-x)) >> MSB));
}

__attribute__((noipa))
static void calSign_bug(int8_t *dst, const pixel *src1, const pixel *src2, const int endX)
{
  for (int x = 0; x < endX; x++)
    dst[x] = signOf(src1[x] - src2[x]);
}

__attribute__((noipa, optimize(0)))
static void calSign_ok(int8_t *dst, const pixel *src1, const pixel *src2, const int endX)
{
  for (int x = 0; x < endX; x++)
    dst[x] = signOf(src1[x] - src2[x]);
}

__attribute__((noipa, optimize(0)))
int main()
{
  const pixel s1[9] = { 0xcd, 0x33, 0xd4, 0x3e, 0xb0, 0xfb, 0x95, 0x64, 0x70, };
  const pixel s2[9] = { 0xba, 0x9f, 0xab, 0xa1, 0x3b, 0x29, 0xb1, 0xbd, 0x64, };
  int endX = 9;
  int8_t dst[9];
  int8_t dst_ok[9];

  calSign_bug(dst, s1, s2, endX);
  calSign_ok(dst_ok, s1, s2, endX);

  if (__builtin_memcmp(dst, dst_ok, endX) != 0)
    __builtin_abort ();
  return 0;
}