-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpuid.c
49 lines (40 loc) · 1.23 KB
/
cpuid.c
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
/*
* Checksum benchmark
*
* Copyright (c) 2017 Alexander Graf
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include "tsc.h"
#include <immintrin.h>
#include <string.h>
extern int avxChecksum(const char * const target, size_t targetLength);
extern int avxChecksumV2(const char * const target, size_t targetLength);
extern int naiveChecksum(const char * const target, size_t targetLength);
static void benchmark(int (*func)(const char * const target, size_t targetLength),
size_t len, const char *test_data, const char *type)
{
uint64_t tsc_start;
uint64_t tsc_end;
uint64_t loops;
int csum;
loops = 0;
tsc_start = rdtsc();
tsc_end = tsc_start + tsc_per_sec;
while (rdtsc() < tsc_end) {
csum = func(test_data, len);
asm("" : : "r"(csum));
loops++;
}
printf("%s loops: %ld\n", type, loops);
}
int main(int argc, char **argv)
{
char test_data[2048 * 1024];
setup_tsc();
memset(test_data, 'd', sizeof(test_data));
benchmark(naiveChecksum, sizeof(test_data), test_data, "Generic");
benchmark(avxChecksum, sizeof(test_data), test_data, "AVX2");
benchmark(avxChecksumV2, sizeof(test_data), test_data, "AVX2 (optimized)");
return 0;
}