aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/vect/vect-cond-reduc-in-order-2-signed-zero.c
blob: 155ce477103e00b99943de1ac052ca1f5c388b80 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Make sure a -0 stays -0 when we perform a conditional reduction.  */
/* { dg-require-effective-target vect_double } */
/* { dg-add-options ieee } */
/* { dg-additional-options "-std=gnu99 -fno-fast-math" } */

#include "tree-vect.h"

#include <math.h>

#define N (VECTOR_BITS * 17)

double __attribute__ ((noinline, noclone))
reduc_plus_double (double *restrict a, double init, int *cond, int n)
{
  double res = init;
  for (int i = 0; i < n; i++)
    if (cond[i])
      res += a[i];
  return res;
}

double __attribute__ ((noinline, noclone, optimize ("0")))
reduc_plus_double_ref (double *restrict a, double init, int *cond, int n)
{
  double res = init;
  for (int i = 0; i < n; i++)
    if (cond[i])
      res += a[i];
  return res;
}

double __attribute__ ((noinline, noclone))
reduc_minus_double (double *restrict a, double init, int *cond, int n)
{
  double res = init;
  for (int i = 0; i < n; i++)
    if (cond[i])
      res -= a[i];
  return res;
}

double __attribute__ ((noinline, noclone, optimize ("0")))
reduc_minus_double_ref (double *restrict a, double init, int *cond, int n)
{
  double res = init;
  for (int i = 0; i < n; i++)
    if (cond[i])
      res -= a[i];
  return res;
}

int __attribute__ ((optimize (1)))
main ()
{
  int n = 19;
  double a[N];
  int cond1[N], cond2[N];

  for (int i = 0; i < N; i++)
    {
      a[i] = (i * 0.1) * (i & 1 ? 1 : -1);
      cond1[i] = 0;
      cond2[i] = i & 4 ? 1 : 0;
      asm volatile ("" ::: "memory");
    }

  double res1 = reduc_plus_double (a, -0.0, cond1, n);
  double ref1 = reduc_plus_double_ref (a, -0.0, cond1, n);
  double res2 = reduc_minus_double (a, -0.0, cond1, n);
  double ref2 = reduc_minus_double_ref (a, -0.0, cond1, n);
  double res3 = reduc_plus_double (a, -0.0, cond1, n);
  double ref3 = reduc_plus_double_ref (a, -0.0, cond1, n);
  double res4 = reduc_minus_double (a, -0.0, cond1, n);
  double ref4 = reduc_minus_double_ref (a, -0.0, cond1, n);

  if (res1 != ref1 || signbit (res1) != signbit (ref1))
    __builtin_abort ();
  if (res2 != ref2 || signbit (res2) != signbit (ref2))
    __builtin_abort ();
  if (res3 != ref3 || signbit (res3) != signbit (ref3))
    __builtin_abort ();
  if (res4 != ref4 || signbit (res4) != signbit (ref4))
    __builtin_abort ();

  res1 = reduc_plus_double (a, 0.0, cond1, n);
  ref1 = reduc_plus_double_ref (a, 0.0, cond1, n);
  res2 = reduc_minus_double (a, 0.0, cond1, n);
  ref2 = reduc_minus_double_ref (a, 0.0, cond1, n);
  res3 = reduc_plus_double (a, 0.0, cond1, n);
  ref3 = reduc_plus_double_ref (a, 0.0, cond1, n);
  res4 = reduc_minus_double (a, 0.0, cond1, n);
  ref4 = reduc_minus_double_ref (a, 0.0, cond1, n);

  if (res1 != ref1 || signbit (res1) != signbit (ref1))
    __builtin_abort ();
  if (res2 != ref2 || signbit (res2) != signbit (ref2))
    __builtin_abort ();
  if (res3 != ref3 || signbit (res3) != signbit (ref3))
    __builtin_abort ();
  if (res4 != ref4 || signbit (res4) != signbit (ref4))
    __builtin_abort ();

  res1 = reduc_plus_double (a, -0.0, cond2, n);
  ref1 = reduc_plus_double_ref (a, -0.0, cond2, n);
  res2 = reduc_minus_double (a, -0.0, cond2, n);
  ref2 = reduc_minus_double_ref (a, -0.0, cond2, n);
  res3 = reduc_plus_double (a, -0.0, cond2, n);
  ref3 = reduc_plus_double_ref (a, -0.0, cond2, n);
  res4 = reduc_minus_double (a, -0.0, cond2, n);
  ref4 = reduc_minus_double_ref (a, -0.0, cond2, n);

  if (res1 != ref1 || signbit (res1) != signbit (ref1))
    __builtin_abort ();
  if (res2 != ref2 || signbit (res2) != signbit (ref2))
    __builtin_abort ();
  if (res3 != ref3 || signbit (res3) != signbit (ref3))
    __builtin_abort ();
  if (res4 != ref4 || signbit (res4) != signbit (ref4))
    __builtin_abort ();

  res1 = reduc_plus_double (a, 0.0, cond2, n);
  ref1 = reduc_plus_double_ref (a, 0.0, cond2, n);
  res2 = reduc_minus_double (a, 0.0, cond2, n);
  ref2 = reduc_minus_double_ref (a, 0.0, cond2, n);
  res3 = reduc_plus_double (a, 0.0, cond2, n);
  ref3 = reduc_plus_double_ref (a, 0.0, cond2, n);
  res4 = reduc_minus_double (a, 0.0, cond2, n);
  ref4 = reduc_minus_double_ref (a, 0.0, cond2, n);

  if (res1 != ref1 || signbit (res1) != signbit (ref1))
    __builtin_abort ();
  if (res2 != ref2 || signbit (res2) != signbit (ref2))
    __builtin_abort ();
  if (res3 != ref3 || signbit (res3) != signbit (ref3))
    __builtin_abort ();
  if (res4 != ref4 || signbit (res4) != signbit (ref4))
    __builtin_abort ();

  return 0;
}