This source file includes following definitions.
- naive
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19 #include "integer_length.h"
20
21 #include <limits.h>
22
23 #include "macros.h"
24
25 #define NBITS (sizeof (unsigned long long) * CHAR_BIT)
26
27 static int
28 naive (unsigned long long x)
29 {
30 int j;
31 for (j = NBITS - 1; j >= 0; j--)
32 if (x & (1ULL << j))
33 return j + 1;
34 return 0;
35 }
36
37 int
38 main (int argc, char *argv[])
39 {
40 unsigned long x;
41 int i;
42
43 for (x = 0; x <= 256; x++)
44 ASSERT (integer_length_ll (x) == naive (x));
45 for (i = 0; i < NBITS; i++)
46 {
47 ASSERT (integer_length_ll (1ULL << i) == naive (1ULL << i));
48 ASSERT (integer_length_ll (1ULL << i) == i + 1);
49 ASSERT (integer_length_ll (-1ULL << i) == NBITS);
50 }
51 for (i = 0; i < NBITS - 1; i++)
52 ASSERT (integer_length_ll (3ULL << i) == i + 2);
53 for (i = 0; i < NBITS - 2; i++)
54 ASSERT (integer_length_ll (-3ULL << i) == NBITS);
55 for (i = 0; i < NBITS - 2; i++)
56 {
57 ASSERT (integer_length_ll (5ULL << i) == i + 3);
58 ASSERT (integer_length_ll (7ULL << i) == i + 3);
59 }
60 for (i = 0; i < NBITS - 3; i++)
61 {
62 ASSERT (integer_length_ll (-5ULL << i) == NBITS);
63 ASSERT (integer_length_ll (-7ULL << i) == NBITS);
64 }
65 return 0;
66 }