summaryrefslogtreecommitdiff
path: root/testsuite/ctlz.c
blob: 1fc0cf1654e7bcec1809b9d46b297281fca51b6d (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
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <string.h>

#include "../alpha_mmintrin.h"

#define ITERATIONS 10000

int main(int argc, char ** argv) {
	uint64_t a[512], b[512], c[512];
	uint64_t l1, l2;
	uint64_t start, end, overhead;
	uint64_t cix_total = 0, noncix_total = 0;
	int i, j, count = 0;

	start = __rpcc();
	end = __rpcc();
	overhead = end - start;

	srand(time(NULL));

	for (i = 0; i < 512; i++) {
		a[i] = rand();
	}
	
	for (j = 0; j < ITERATIONS; j++) {
		start = __rpcc();

		for (i = 0; i < 512; i++) {
			l1 = a[i];

			l2 = 1 << (64 - __ctlz(l1));

			b[i] = l2;
		}

		end = __rpcc();
		cix_total += end - start - overhead;
		memset(b, 4096, 0);
	}

	for (j = 0; j < ITERATIONS; j++) {
		start = __rpcc();
		
		for (i = 0; i < 512; i++) {
			l1 = a[i];
			while (l1 != 0) {
				l1 >>= 1;
				count += 1;
			}
			l2 = 1 << count;
			c[i] = l2;
	//		printf("nextpow2 of %lu is %lu\n", i, l2);
			count = 0;
		}
		
		end = __rpcc();
		noncix_total += end - start - overhead;
		memset(c, 4096, 0);
	}

	noncix_total /= ITERATIONS;
	cix_total /= ITERATIONS;

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