12 /* From wikipedia on RDTSC */
13 inline uint64_t rdtsc() {
15 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
16 return (uint64_t)hi << 32 | lo;
19 typedef int (*test_func)(const char *s1, const char *s2, int n);
21 void run_test(const char *buf1, const char *buf2,
22 const char *name, test_func func) {
27 for (i = 0; i < ITERS; i++) {
29 x = func(buf1, buf2, NUM);
37 printf("%s : %qu cycles\n", name, accum);
40 /* Build w/ -fno-builtin for this to be fast, this assumes that there
41 * is a difference at s1[n-1] */
42 int memcmp_fake(const char *s1, const char *s2, int n) {
43 int x = memcmp(s1, s2, n);
44 return x < 0 ? n - 1 : n + 1;
47 #define UNALIGNED_OK 1
49 test2(const char *s1c, const char *s2c, int n)
53 int nint = n / sizeof(int);
58 const int *s1 = (const int*)s1c;
59 const int *s2 = (const int*)s2c;
60 int nint_8 = nint - 8;
70 s1[i++] == s2[j++]) { }
72 i = (i - 1) * sizeof(int);
76 while (i < n && s1c[i] == s2c[i])
84 test1(const char *s1c, const char *s2c, int n) {
86 while (i < n && s1c[i] == s2c[i])
93 int main(/*int argc, char **argv*/) {
94 char *buf1 = malloc(NUM+1);
95 char *buf2 = malloc(NUM+1);
98 for (i = 0; i < NUM; i++) {
99 buf1[i] = buf2[i] = rand();
104 printf ("ALIGNED\n");
106 run_test(buf1, buf2, "memcmp", &memcmp_fake);
107 run_test(buf1, buf2, "test1", &test1);
108 run_test(buf1, buf2, "test2", &test2);
110 for (i = 0; i < NUM; i++) {
111 buf1[i] = buf2[i+1] = rand();
116 printf ("UNALIGNED\n");
118 run_test(buf1, buf2+1, "memcmp", &memcmp_fake);
119 run_test(buf1, buf2+1, "test1", &test1);
120 run_test(buf1, buf2+1, "test2", &test2);