aboutsummaryrefslogtreecommitdiff
path: root/examples/c++/Sum_over_array_(Optimized).cpp
blob: 3304ff3e640eeac62ff257366def4cb4252d846e (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
// Compile with -O3 -march=native to see autovectorization
// assumes input is aligned on 64-byte boundary and that
// length is a multiple of 64.
int testFunction(int* input, int length) {
  // Alignment hints supported on GCC 4.7+ and any compiler
  // supporting the appropriate builtin (clang 3.6+).
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
#if __GNUC__ > 4 \
    || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) \
    || __has_builtin(__builtin_assume_aligned)
  input = static_cast<int*>(__builtin_assume_aligned(input, 64));
#endif
#if _MSC_VER
  __assume((length & 63) == 0);
#else
  if (length & 63) __builtin_unreachable();
#endif
  int sum = 0;
  for (int i = 0; i < length; ++i) {
    sum += input[i];
  }
  return sum;
}