initial
[fpgammix.git] / workloads / faster-lg.c
blobbaca9bffd7af17ad4f0fc62adcf171347a6ac9fa
1 #include <stdio.h>
2 #include <stdlib.h>
4 static int orig_lg(int n)
6 int j, l;
8 if (n <= 0) return -1;
10 for (j = n, l = 0; j; j >>= 1) l++;
11 return l - 1;
14 #define ARGS(a) a
16 static int lg ARGS((int n));
17 static int lg(n)
18 int n;
20 int j;
22 if (n <= 0) return -1;
24 j = 0;
25 #if 1
26 if (n & 0xFFFF0000) j = 16, n >>= 16;
27 if (n & 0x0000FF00) j += 8, n >>= 8;
28 if (n & 0x000000F0) j += 4, n >>= 4;
29 if (n & 0x0000000C) j += 2, n >>= 2;
30 if (n & 0x00000002) j += 1;
31 #else
32 if (n >> 16) j = 16, n >>= 16;
33 if (n >> 8) j += 8, n >>= 8;
34 if (n >> 4) j += 4, n >>= 4;
35 if (n >> 2) j += 2, n >>= 2;
36 if (n >> 1) j += 1;
37 #endif
39 return j;
42 int
43 main(int argc, char **argv)
45 int i = 0, k, l, f;
46 srandom(1729);
48 int sum = 0;
51 for (k = 0; k < 300000000; ++k) {
52 int n = k - 10 /*random() % 60*/;
54 #if 0
55 sum += l = orig_lg(n);
56 #else
57 sum += f = lg(n);
58 #endif
60 #if 0
61 if (l != f)
62 printf("Opps, bug at lg(%d) = %d, not %d\n", n, l, f);
63 #endif
66 printf("sum = %d\n", sum);