aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c/examples-4/simd-3.c
blob: 9f33713d3569f7737704bdf5576b58dc8443fc7f (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
/* { dg-do run } */
/* { dg-additional-options "-msse2" { target sse2_runtime } } */
/* { dg-additional-options "-mavx" { target avx_runtime } } */

#define N 100
#define EPS 0.0000000000000001

#include <stdlib.h>

void init(double *a, double *a_ref, double *b, int n)
{
  int i, s = -1;
  for ( i = 0; i < n; i++ )
  {
    a[i] = i*i*s;
    a_ref[i] = a[i];
    b[i] = i+i;
    s = -s;
  }
}

double work( double *a, double *b, int n )
{
   int i;
   double tmp, sum;
   sum = 0.0;
   #pragma omp simd private(tmp) reduction(+:sum)
   for (i = 0; i < n; i++) {
      tmp = a[i] + b[i];
      sum += tmp;
   }
   return sum;
}

double work_ref( double *a, double *b, int n )
{
   int i;
   double tmp, sum;
   sum = 0.0;
   for (i = 0; i < n; i++) {
      tmp = a[i] + b[i];
      sum += tmp;
   }
   return sum;
}

int main ()
{
  double a[N], a_ref[N], b[N];
  int res, ref;

  init(a, a_ref, b, N);

  res = work(a, b, N);
  ref = work_ref(a_ref, b, N);

  if (res != ref)
    abort ();

  return 0;
}