Skip to content

Instantly share code, notes, and snippets.

@msg7086
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msg7086/b5de7cb128d8ef109808 to your computer and use it in GitHub Desktop.
Save msg7086/b5de7cb128d8ef109808 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/time.h>
long long sum[4] __attribute__((aligned(32))) = {1LL, 2LL, 3LL, 4LL};
long long four[4] __attribute__((aligned(32))) = {4LL, 4LL, 4LL, 4LL};
int main() {
struct timeval start, end;
gettimeofday(&start, NULL);
__asm__ (
"vpxor %%ymm0, %%ymm0, %%ymm0;"
"vmovdqa %0, %%ymm1;"
"vmovdqa %1, %%ymm2;"
"movl $250000000, %%ecx;"
"LOOP:"
"vpaddq %%ymm0, %%ymm1, %%ymm0;"
"vpaddq %%ymm1, %%ymm2, %%ymm1;"
"dec %%ecx;"
"jnz LOOP;"
"vmovdqa %%ymm0, %0;"
"vzeroall;"
:: "m"(sum), "m"(four) : "memory"
);
gettimeofday(&end, NULL);
printf("sum: %lld, time used: %f 's",
sum[0] + sum[1] + sum[2] + sum[3],
end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec) / 1000000.0);
}
// clang++ -o sum.exe sum.cpp -mavx2
// sum: 500000000500000000, time used: 0.069361 's
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment