This source file includes following definitions.
- main
- hibit
- test_hibit
- cube_dim
- test_cube_dim
- next_poweroftwo
- test_next_poweroftwo
- next_poweroftwo_inclusive
- test_next_poweroftwo_inclusive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include "opal_config.h"
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "support.h"
27 #include "opal/util/bit_ops.h"
28 #include "opal/util/output.h"
29 #include "opal/util/printf.h"
30
31
32
33
34
35 static int test_hibit(int value, int start);
36 static int test_cube_dim(int value);
37 static int test_next_poweroftwo(int value);
38 static int test_next_poweroftwo_inclusive(int value);
39
40 int main(int argc, char* argv[])
41 {
42 int i;
43 int vals[] = {0, 1, 2, 3, 4, 5, 127, 128, 129, (1 << 29) -1, (1 << 29), (1 << 29) +1, (1 << 30) -1, (1 << 30) };
44 test_init("opal_bit_ops()");
45
46 #ifdef DEBUG
47 printf ("Test usage: ./opal_bit_ops [VALUES]\n");
48 #endif
49
50 if (1 < argc) {
51 for (i = 1; i < argc; i++) {
52 int value;
53 value = atoi (argv[i]);
54 printf ("Testing %d. argument test_next_poweroftwo(%d): %s\n",
55 i, value, test_next_poweroftwo(value) ? "correct" : "wrong");
56 }
57 }
58
59 for (i = 0; i < (int)(sizeof(vals)/sizeof(vals[0])); i++) {
60 test_hibit (vals[i], 8 * sizeof(int) -2);
61 test_hibit (vals[i], 3);
62 test_cube_dim (vals[i]);
63 test_next_poweroftwo (vals[i]);
64 test_next_poweroftwo_inclusive (vals[i]);
65 }
66
67
68 return test_finalize();
69 }
70
71
72
73 static int hibit(int value, int start)
74 {
75 unsigned int mask;
76
77 --start;
78 mask = 1 << start;
79
80 for (; start >= 0; --start, mask >>= 1) {
81 if (value & mask) {
82 break;
83 }
84 }
85
86 return start;
87 }
88
89 static int test_hibit(int value, int start)
90 {
91 int out;
92 int bit = hibit (value, start);
93
94 #ifdef DEBUG
95 printf ("test_hibit(): value:%d expect:%d\n",
96 value, bit);
97 #endif
98
99 if (bit == (out = opal_hibit (value, start))) {
100 test_success();
101 return 1;
102 } else {
103 char * msg;
104 opal_asprintf(&msg, "Mismatch for hibit (w/ start:%d): value:%d, expected:%d got:%d\n",
105 start, value, bit, out);
106 test_failure(msg);
107 free(msg);
108 }
109 return 0;
110 }
111
112
113
114 static int cube_dim(int value)
115 {
116 int dim, size;
117
118 for (dim = 0, size = 1; size < value; ++dim, size <<= 1);
119
120 return dim;
121 }
122
123 static int test_cube_dim(int value)
124 {
125 int out;
126 int dim = cube_dim (value);
127
128 #ifdef DEBUG
129 printf ("test_cube_dim(): value:%d expect:%d\n",
130 value, dim);
131 #endif
132
133 if (dim == (out = opal_cube_dim (value))) {
134 test_success();
135 return 1;
136 } else {
137 char * msg;
138 opal_asprintf(&msg, "Mismatch for cube_dim: value:%d, expected:%d got:%d\n",
139 value, dim, out);
140 test_failure(msg);
141 free(msg);
142 }
143 return 0;
144 }
145
146
147
148 static int next_poweroftwo(int value)
149 {
150 int power2;
151
152 for (power2 = 1; value; value >>=1, power2 <<=1) ;
153
154 return power2;
155 }
156
157
158 static int test_next_poweroftwo(int value)
159 {
160 int out;
161 int power2 = next_poweroftwo (value);
162
163 #ifdef DEBUG
164 printf ("test_next_poweroftwo(): value:%d expect:%d\n",
165 value, power2);
166 #endif
167
168 if (power2 == (out = opal_next_poweroftwo (value))) {
169 test_success();
170 return 1;
171 } else {
172 char * msg;
173 opal_asprintf(&msg, "Mismatch for power-of-two: value:%d, expected:%d got:%d\n",
174 value, power2, out);
175 test_failure(msg);
176 free(msg);
177 }
178 return 0;
179 }
180
181
182
183
184 static int next_poweroftwo_inclusive(int value)
185 {
186 int power2 = 1;
187
188 while ( power2 < value )
189 power2 <<= 1;
190
191 return power2;
192 }
193
194 static int test_next_poweroftwo_inclusive(int value)
195 {
196 int out;
197 int power2 = next_poweroftwo_inclusive (value);
198
199 #ifdef DEBUG
200 printf ("test_next_poweroftwo(): value:%d expect:%d\n",
201 value, power2);
202 #endif
203
204 if (power2 == (out = opal_next_poweroftwo_inclusive (value))) {
205 test_success();
206 return 1;
207 } else {
208 char * msg;
209 opal_asprintf(&msg, "Mismatch for power-of-two-inclusive: value:%d, expected:%d got:%d\n",
210 value, power2, out);
211 test_failure(msg);
212 free(msg);
213 }
214
215 return 0;
216 }
217
218
219
220