summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorLoïc Guégan <loic.guegan@mailbox.org>2025-09-26 20:03:04 +0200
committerLoïc Guégan <loic.guegan@mailbox.org>2025-09-26 20:03:04 +0200
commit07b54cd51193883a1a4f8e1c1dd66d3d55a47bbe (patch)
tree856dcb8b60b3a84f194db61d4086941fe374e279 /main.c
Add codeHEADmaster
Diffstat (limited to 'main.c')
-rw-r--r--main.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..99a0eca
--- /dev/null
+++ b/main.c
@@ -0,0 +1,55 @@
+#ifdef USE_VECTOR
+#include <immintrin.h>
+#endif
+#include <stdio.h>
+#include <time.h>
+#include <stdlib.h>
+
+#define STRIDE (256/32) // How many integers fit in a 256 vector register
+#define VSIZE (STRIDE*200000)
+
+int main(int argc, char *argv[]) {
+
+ float start, end, duration;
+
+ int *v1;
+ posix_memalign((void**)&v1, 32, VSIZE * sizeof(int));
+ int *v2;
+ posix_memalign((void**)&v2, 32, VSIZE * sizeof(int));
+ int result[VSIZE];
+
+#ifndef USE_VECTOR
+ printf("Sequential..");
+ start = (float)clock()/CLOCKS_PER_SEC;
+ for(int i=0;i<VSIZE;i++){
+ result[i]=v1[i]+v2[i];
+ }
+ end = (float)clock()/CLOCKS_PER_SEC;
+ duration= end - start;
+ printf(" done! (duration=%.6fs)\n", duration);
+#else
+ printf("Vectorize...");
+ __m256i a,b;
+ // e == extended (for historical reasons just brandy name)
+ // p == packed (multiple packed elements)
+ // i == integer
+ // 32 == each integer is 32bits
+ __m256i c = _mm256_add_epi32(a, b);
+ start = (float)clock()/CLOCKS_PER_SEC;
+ for(int i=0;i<VSIZE;i+=STRIDE){
+ a = _mm256_load_si256((__m256i*)&v1[i]);
+ b = _mm256_load_si256((__m256i*)&v2[i]);
+ c = _mm256_add_epi32(a,b);
+
+ _mm256_store_si256((__m256i*)&result[i],c);
+ }
+ end = (float)clock()/CLOCKS_PER_SEC;
+ duration= end - start;
+ printf(" done! (duration=%.6fs)\n", duration);
+#endif
+
+ free(v1);
+ free(v2);
+
+ return 0;
+}