VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstBitOperations.cpp@ 4725

Last change on this file since 4725 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.1 KB
Line 
1/* $Id: tstBitOperations.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime Testcase - Inlined Bit Operations.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/asm.h>
23#include <iprt/stream.h>
24#include <iprt/string.h>
25
26
27int main()
28{
29 int rcRet = 0;
30 int i;
31 int j;
32 int k;
33 RTPrintf("tstBitOperations: TESTING\n");
34
35
36 /*
37 * Tests
38 */
39 uint32_t au32[4];
40 #define MAP_CLEAR(a) memset(&a, 0, sizeof(a));
41 #define MAP_SET(a) memset(&a, 0xff, sizeof(a));
42 #define DUMP() RTPrintf("au32={%08x,%08x,%08x,%08x}\n", au32[0], au32[1], au32[2], au32[3])
43 #define CHECK(expr) do { if (!(expr)) { RTPrintf("tstBitOperations: error line %d: %s\n", __LINE__, #expr); DUMP(); rcRet++; } } while (0)
44 #define CHECK_BIT(expr, b1) do { if (!(expr)) { RTPrintf("tstBitOperations: error line %d, b1=%d: %s\n", __LINE__, b1, #expr); rcRet++; } } while (0)
45 #define CHECK_BIT2(expr, b1, b2) do { if (!(expr)) { RTPrintf("tstBitOperations: error line %d, b1=%d b2=%d: %s\n", __LINE__, b1, b2, #expr); rcRet++; } } while (0)
46 #define CHECK_BIT3(expr, b1, b2, b3) do { if (!(expr)) { RTPrintf("tstBitOperations: error line %d, b1=%d b2=%d b3=%d: %s\n", __LINE__, b1, b2, b3, #expr); rcRet++; } } while (0)
47
48 /* set */
49 MAP_CLEAR(au32);
50 ASMBitSet(&au32[0], 0);
51 ASMBitSet(&au32[0], 31);
52 ASMBitSet(&au32[0], 65);
53 CHECK(au32[0] == 0x80000001U);
54 CHECK(au32[2] == 0x00000002U);
55 CHECK(ASMBitTestAndSet(&au32[0], 0) && au32[0] == 0x80000001U);
56 CHECK(!ASMBitTestAndSet(&au32[0], 16) && au32[0] == 0x80010001U);
57 CHECK(ASMBitTestAndSet(&au32[0], 16) && au32[0] == 0x80010001U);
58 CHECK(!ASMBitTestAndSet(&au32[0], 80) && au32[2] == 0x00010002U);
59
60 MAP_CLEAR(au32);
61 ASMAtomicBitSet(&au32[0], 0);
62 ASMAtomicBitSet(&au32[0], 30);
63 ASMAtomicBitSet(&au32[0], 64);
64 CHECK(au32[0] == 0x40000001U);
65 CHECK(au32[2] == 0x00000001U);
66 CHECK(ASMAtomicBitTestAndSet(&au32[0], 0) && au32[0] == 0x40000001U);
67 CHECK(!ASMAtomicBitTestAndSet(&au32[0], 16) && au32[0] == 0x40010001U);
68 CHECK(ASMAtomicBitTestAndSet(&au32[0], 16) && au32[0] == 0x40010001U);
69 CHECK(!ASMAtomicBitTestAndSet(&au32[0], 80) && au32[2] == 0x00010001U);
70
71 /* clear */
72 MAP_SET(au32);
73 ASMBitClear(&au32[0], 0);
74 ASMBitClear(&au32[0], 31);
75 ASMBitClear(&au32[0], 65);
76 CHECK(au32[0] == ~0x80000001U);
77 CHECK(au32[2] == ~0x00000002U);
78 CHECK(!ASMBitTestAndClear(&au32[0], 0) && au32[0] == ~0x80000001U);
79 CHECK(ASMBitTestAndClear(&au32[0], 16) && au32[0] == ~0x80010001U);
80 CHECK(!ASMBitTestAndClear(&au32[0], 16) && au32[0] == ~0x80010001U);
81 CHECK(ASMBitTestAndClear(&au32[0], 80) && au32[2] == ~0x00010002U);
82
83 MAP_SET(au32);
84 ASMAtomicBitClear(&au32[0], 0);
85 ASMAtomicBitClear(&au32[0], 30);
86 ASMAtomicBitClear(&au32[0], 64);
87 CHECK(au32[0] == ~0x40000001U);
88 CHECK(au32[2] == ~0x00000001U);
89 CHECK(!ASMAtomicBitTestAndClear(&au32[0], 0) && au32[0] == ~0x40000001U);
90 CHECK(ASMAtomicBitTestAndClear(&au32[0], 16) && au32[0] == ~0x40010001U);
91 CHECK(!ASMAtomicBitTestAndClear(&au32[0], 16) && au32[0] == ~0x40010001U);
92 CHECK(ASMAtomicBitTestAndClear(&au32[0], 80) && au32[2] == ~0x00010001U);
93
94 /* toggle */
95 MAP_SET(au32);
96 ASMBitToggle(&au32[0], 0);
97 ASMBitToggle(&au32[0], 31);
98 ASMBitToggle(&au32[0], 65);
99 ASMBitToggle(&au32[0], 47);
100 ASMBitToggle(&au32[0], 47);
101 CHECK(au32[0] == ~0x80000001U);
102 CHECK(au32[2] == ~0x00000002U);
103 CHECK(!ASMBitTestAndToggle(&au32[0], 0) && au32[0] == ~0x80000000U);
104 CHECK(ASMBitTestAndToggle(&au32[0], 0) && au32[0] == ~0x80000001U);
105 CHECK(ASMBitTestAndToggle(&au32[0], 16) && au32[0] == ~0x80010001U);
106 CHECK(!ASMBitTestAndToggle(&au32[0], 16) && au32[0] == ~0x80000001U);
107 CHECK(ASMBitTestAndToggle(&au32[0], 80) && au32[2] == ~0x00010002U);
108
109 MAP_SET(au32);
110 ASMAtomicBitToggle(&au32[0], 0);
111 ASMAtomicBitToggle(&au32[0], 30);
112 ASMAtomicBitToggle(&au32[0], 64);
113 ASMAtomicBitToggle(&au32[0], 47);
114 ASMAtomicBitToggle(&au32[0], 47);
115 CHECK(au32[0] == ~0x40000001U);
116 CHECK(au32[2] == ~0x00000001U);
117 CHECK(!ASMAtomicBitTestAndToggle(&au32[0], 0) && au32[0] == ~0x40000000U);
118 CHECK(ASMAtomicBitTestAndToggle(&au32[0], 0) && au32[0] == ~0x40000001U);
119 CHECK(ASMAtomicBitTestAndToggle(&au32[0], 16) && au32[0] == ~0x40010001U);
120 CHECK(!ASMAtomicBitTestAndToggle(&au32[0], 16) && au32[0] == ~0x40000001U);
121 CHECK(ASMAtomicBitTestAndToggle(&au32[0], 80) && au32[2] == ~0x00010001U);
122
123 /* test bit. */
124 for (i = 0; i < 128; i++)
125 {
126 MAP_SET(au32);
127 CHECK_BIT(ASMBitTest(&au32[0], i), i);
128 ASMBitToggle(&au32[0], i);
129 CHECK_BIT(!ASMBitTest(&au32[0], i), i);
130 CHECK_BIT(!ASMBitTestAndToggle(&au32[0], i), i);
131 CHECK_BIT(ASMBitTest(&au32[0], i), i);
132 CHECK_BIT(ASMBitTestAndToggle(&au32[0], i), i);
133 CHECK_BIT(!ASMBitTest(&au32[0], i), i);
134
135 MAP_SET(au32);
136 CHECK_BIT(ASMBitTest(&au32[0], i), i);
137 ASMAtomicBitToggle(&au32[0], i);
138 CHECK_BIT(!ASMBitTest(&au32[0], i), i);
139 CHECK_BIT(!ASMAtomicBitTestAndToggle(&au32[0], i), i);
140 CHECK_BIT(ASMBitTest(&au32[0], i), i);
141 CHECK_BIT(ASMAtomicBitTestAndToggle(&au32[0], i), i);
142 CHECK_BIT(!ASMBitTest(&au32[0], i), i);
143
144 }
145
146 /* bit searching */
147 MAP_SET(au32);
148 CHECK(ASMBitFirstClear(&au32[0], sizeof(au32) * 8) == -1);
149 CHECK(ASMBitFirstSet(&au32[0], sizeof(au32) * 8) == 0);
150
151 ASMBitClear(&au32[0], 1);
152 CHECK(ASMBitFirstClear(&au32[0], sizeof(au32) * 8) == 1);
153 CHECK(ASMBitFirstSet(&au32[0], sizeof(au32) * 8) == 0);
154
155 MAP_SET(au32);
156 ASMBitClear(&au32[0], 95);
157 CHECK(ASMBitFirstClear(&au32[0], sizeof(au32) * 8) == 95);
158 CHECK(ASMBitFirstSet(&au32[0], sizeof(au32) * 8) == 0);
159
160 MAP_SET(au32);
161 ASMBitClear(&au32[0], 127);
162 CHECK(ASMBitFirstClear(&au32[0], sizeof(au32) * 8) == 127);
163 CHECK(ASMBitFirstSet(&au32[0], sizeof(au32) * 8) == 0);
164 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, 0) == 1);
165 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, 1) == 2);
166 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, 2) == 3);
167
168
169 MAP_SET(au32);
170 CHECK(ASMBitNextClear(&au32[0], sizeof(au32) * 8, 0) == -1);
171 ASMBitClear(&au32[0], 32);
172 CHECK(ASMBitNextClear(&au32[0], sizeof(au32) * 8, 32) == -1);
173 ASMBitClear(&au32[0], 88);
174 CHECK(ASMBitNextClear(&au32[0], sizeof(au32) * 8, 57) == 88);
175
176 MAP_SET(au32);
177 ASMBitClear(&au32[0], 31);
178 ASMBitClear(&au32[0], 57);
179 ASMBitClear(&au32[0], 88);
180 ASMBitClear(&au32[0], 101);
181 ASMBitClear(&au32[0], 126);
182 ASMBitClear(&au32[0], 127);
183 CHECK(ASMBitFirstClear(&au32[0], sizeof(au32) * 8) == 31);
184 CHECK(ASMBitNextClear(&au32[0], sizeof(au32) * 8, 31) == 57);
185 CHECK(ASMBitNextClear(&au32[0], sizeof(au32) * 8, 57) == 88);
186 CHECK(ASMBitNextClear(&au32[0], sizeof(au32) * 8, 88) == 101);
187 CHECK(ASMBitNextClear(&au32[0], sizeof(au32) * 8, 101) == 126);
188 CHECK(ASMBitNextClear(&au32[0], sizeof(au32) * 8, 126) == 127);
189 CHECK(ASMBitNextClear(&au32[0], sizeof(au32) * 8, 127) == -1);
190
191 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, 29) == 30);
192 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, 30) == 32);
193
194 MAP_CLEAR(au32);
195 for (i = 1; i < 128; i++)
196 CHECK_BIT(ASMBitNextClear(&au32[0], sizeof(au32) * 8, i - 1) == i, i);
197 for (i = 0; i < 128; i++)
198 {
199 MAP_SET(au32);
200 ASMBitClear(&au32[0], i);
201 CHECK_BIT(ASMBitFirstClear(&au32[0], sizeof(au32) * 8) == i, i);
202 for (j = 0; j < i; j++)
203 CHECK_BIT(ASMBitNextClear(&au32[0], sizeof(au32) * 8, j) == i, i);
204 for (j = i; j < 128; j++)
205 CHECK_BIT(ASMBitNextClear(&au32[0], sizeof(au32) * 8, j) == -1, i);
206 }
207
208 /* clear range. */
209 MAP_SET(au32);
210 ASMBitClearRange(&au32, 0, 128);
211 CHECK(!au32[0] && !au32[1] && !au32[2] && !au32[3]);
212 for (i = 0; i < 128; i++)
213 {
214 for (j = i + 1; j <= 128; j++)
215 {
216 MAP_SET(au32);
217 ASMBitClearRange(&au32, i, j);
218 for (k = 0; k < i; k++)
219 CHECK_BIT3(ASMBitTest(&au32[0], k), i, j, k);
220 for (k = i; k < j; k++)
221 CHECK_BIT3(!ASMBitTest(&au32[0], k), i, j, k);
222 for (k = j; k < 128; k++)
223 CHECK_BIT3(ASMBitTest(&au32[0], k), i, j, k);
224 }
225 }
226
227 /* searching for set bits. */
228 MAP_CLEAR(au32);
229 CHECK(ASMBitFirstSet(&au32[0], sizeof(au32) * 8) == -1);
230
231 ASMBitSet(&au32[0], 65);
232 CHECK(ASMBitFirstSet(&au32[0], sizeof(au32) * 8) == 65);
233 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, 65) == -1);
234 for (i = 0; i < 65; i++)
235 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, i) == 65);
236 for (i = 65; i < 128; i++)
237 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, i) == -1);
238
239 ASMBitSet(&au32[0], 17);
240 CHECK(ASMBitFirstSet(&au32[0], sizeof(au32) * 8) == 17);
241 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, 17) == 65);
242 for (i = 0; i < 16; i++)
243 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, i) == 17);
244 for (i = 17; i < 65; i++)
245 CHECK(ASMBitNextSet(&au32[0], sizeof(au32) * 8, i) == 65);
246
247 MAP_SET(au32);
248 for (i = 1; i < 128; i++)
249 CHECK_BIT(ASMBitNextSet(&au32[0], sizeof(au32) * 8, i - 1) == i, i);
250 for (i = 0; i < 128; i++)
251 {
252 MAP_CLEAR(au32);
253 ASMBitSet(&au32[0], i);
254 CHECK_BIT(ASMBitFirstSet(&au32[0], sizeof(au32) * 8) == i, i);
255 for (j = 0; j < i; j++)
256 CHECK_BIT(ASMBitNextSet(&au32[0], sizeof(au32) * 8, j) == i, i);
257 for (j = i; j < 128; j++)
258 CHECK_BIT(ASMBitNextSet(&au32[0], sizeof(au32) * 8, j) == -1, i);
259 }
260
261
262 CHECK(ASMBitLastSetU32(0) == 0);
263 CHECK(ASMBitLastSetU32(1) == 1);
264 CHECK(ASMBitLastSetU32(0x80000000) == 32);
265 CHECK(ASMBitLastSetU32(0xffffffff) == 32);
266 CHECK(ASMBitLastSetU32(BIT(23) | BIT(11)) == 24);
267 for (i = 0; i < 32; i++)
268 CHECK(ASMBitLastSetU32(1 << i) == (unsigned)i + 1);
269
270 CHECK(ASMBitFirstSetU32(0) == 0);
271 CHECK(ASMBitFirstSetU32(1) == 1);
272 CHECK(ASMBitFirstSetU32(0x80000000) == 32);
273 CHECK(ASMBitFirstSetU32(0xffffffff) == 1);
274 CHECK(ASMBitFirstSetU32(BIT(23) | BIT(11)) == 12);
275 for (i = 0; i < 32; i++)
276 CHECK(ASMBitFirstSetU32(1 << i) == (unsigned)i + 1);
277
278 /*
279 * Summary
280 */
281 if (!rcRet)
282 RTPrintf("tstBitOperations: SUCCESS\n");
283 else
284 RTPrintf("tstBitOperations: FAILURE - %d errors\n", rcRet);
285 return rcRet;
286}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette