summaryrefslogtreecommitdiff
path: root/testsuite/ctlz.c
blob: 81023f997b02dc6b8cd6adb20ab4bedf80d8166a (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <string.h>

#include "../alpha_mmintrin.h"

#define ITERATIONS 10000
#define ARRAY_ELEMENTS 512
#define ARRAY_SIZE (ARRAY_ELEMENTS * sizeof(uint64_t))

int main(int argc, char ** argv) {
	uint64_t a[ARRAY_ELEMENTS], b[ARRAY_ELEMENTS], c[ARRAY_ELEMENTS];
	struct timespec cix_start, cix_end;
	double cix_t0, cix_t1, cix_total;
	struct timespec noncix_start, noncix_end;
	double noncix_t0, noncix_t1, noncix_total;
	int i, j;

	srand(time(NULL));

	for (i = 0; i < ARRAY_ELEMENTS; i++) {
		a[i] = rand();
	}
	
	clock_gettime(CLOCK_MONOTONIC, &cix_start);
	for (j = 0; j < ITERATIONS; j++) {
		for (i = 0; i < ARRAY_ELEMENTS; i++) {
			uint64_t l1 = a[i];
			uint64_t l2 = 1 << (64 - __ctlz(l1));

			b[i] = l2;
		}
	}
	clock_gettime(CLOCK_MONOTONIC, &cix_end);

	clock_gettime(CLOCK_MONOTONIC, &noncix_start);
	for (j = 0; j < ITERATIONS; j++) {
		for (i = 0; i < ARRAY_ELEMENTS; i++) {
			uint64_t l1 = a[i];
			uint64_t l2;
			int count = 0;
			while (l1 != 0) {
				l1 >>= 1;
				count += 1;
			}
			l2 = 1 << count;
			c[i] = l2;
		}
	}
	clock_gettime(CLOCK_MONOTONIC, &noncix_end);

	cix_total = (double)((cix_end.tv_sec - cix_start.tv_sec) + ((cix_end.tv_nsec - cix_start.tv_nsec) / 1.0e9)) / ITERATIONS;
	noncix_total = (double)((noncix_end.tv_sec - noncix_start.tv_sec) + ((noncix_end.tv_nsec - noncix_start.tv_nsec) / 1.0e9)) / ITERATIONS;

	if (memcmp(b, c, ARRAY_SIZE) == 0) {
		printf("%s:\n", argv[0]);
		printf("     CIX time: %.9lf seconds\n non-CIX time: %.9lf seconds\n", cix_total, noncix_total);
		printf("     Speed-up: %lf\n", noncix_total / cix_total);
	} else {
		puts("Final arrays are not equal. Something happened.");
	}
	
	return 0;
}