aboutsummaryrefslogtreecommitdiff
path: root/examples/c++/Sum_over_array_(Optimized).cpp
diff options
context:
space:
mode:
authorRubén <rubrinbla@gmail.com>2017-10-12 02:12:28 +0200
committerRubén <rubrinbla@gmail.com>2017-10-12 02:15:31 +0200
commit6f7a4b8b4a815b2277b8e3152790f239dc21ce5d (patch)
tree2619eb2c038d36a23c6852909cad3f1437552674 /examples/c++/Sum_over_array_(Optimized).cpp
parentec6378db101f1fefcdea1f8256f1fc88911c3cc1 (diff)
downloadcompiler-explorer-6f7a4b8b4a815b2277b8e3152790f239dc21ce5d.tar.gz
compiler-explorer-6f7a4b8b4a815b2277b8e3152790f239dc21ce5d.zip
Add D examples and beautify all examples names
Diffstat (limited to 'examples/c++/Sum_over_array_(Optimized).cpp')
-rw-r--r--examples/c++/Sum_over_array_(Optimized).cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/examples/c++/Sum_over_array_(Optimized).cpp b/examples/c++/Sum_over_array_(Optimized).cpp
new file mode 100644
index 000000000..3304ff3e6
--- /dev/null
+++ b/examples/c++/Sum_over_array_(Optimized).cpp
@@ -0,0 +1,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;
+}