3 * dumpscan - routines for scanning and manipulating AFS volume dumps
5 * Copyright (c) 1998 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software_Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie Mellon
26 * the rights to redistribute these changes.
29 /* int64.c - Support for 64-bit integers */
36 hexify_int64(dt_uint64 * X, char *buf)
38 static char mybuf[17];
48 while (x && p >= buf) {
50 c += ((c < 10) ? '0' : 'a' - 10);
60 sprintf(buf, "%08lx%08lx", (unsigned long)X->hi, (unsigned long)X->lo);
69 decimate_int64(dt_uint64 * X, char *buf)
71 static char mybuf[21];
79 while (x && p > buf) {
80 *--p = ((x % 10) + '0');
89 static char bitvals[64][21] = {
90 /* 1 */ "00000000000000000001",
91 /* 2 */ "00000000000000000002",
92 /* 4 */ "00000000000000000004",
93 /* 8 */ "00000000000000000008",
94 /* 10 */ "00000000000000000016",
95 /* 20 */ "00000000000000000032",
96 /* 40 */ "00000000000000000064",
97 /* 80 */ "00000000000000000128",
98 /* 100 */ "00000000000000000256",
99 /* 200 */ "00000000000000000512",
100 /* 400 */ "00000000000000001024",
101 /* 800 */ "00000000000000002048",
102 /* 1000 */ "00000000000000004096",
103 /* 2000 */ "00000000000000008192",
104 /* 4000 */ "00000000000000016384",
105 /* 8000 */ "00000000000000032768",
106 /* 10000 */ "00000000000000065536",
107 /* 20000 */ "00000000000000131072",
108 /* 40000 */ "00000000000000262144",
109 /* 80000 */ "00000000000000524288",
110 /* 100000 */ "00000000000001048576",
111 /* 200000 */ "00000000000002097152",
112 /* 400000 */ "00000000000004194304",
113 /* 800000 */ "00000000000008388608",
114 /* 1000000 */ "00000000000016777216",
115 /* 2000000 */ "00000000000033554432",
116 /* 4000000 */ "00000000000067108864",
117 /* 8000000 */ "00000000000134217728",
118 /* 10000000 */ "00000000000268435456",
119 /* 20000000 */ "00000000000536870912",
120 /* 40000000 */ "00000000001073741824",
121 /* 80000000 */ "00000000002147483648",
122 /* 100000000 */ "00000000004294967296",
123 /* 200000000 */ "00000000008589934592",
124 /* 400000000 */ "00000000017179869184",
125 /* 800000000 */ "00000000034359738368",
126 /* 1000000000 */ "00000000068719476736",
127 /* 2000000000 */ "00000000137438953472",
128 /* 4000000000 */ "00000000274877906944",
129 /* 8000000000 */ "00000000549755813888",
130 /* 10000000000 */ "00000001099511627776",
131 /* 20000000000 */ "00000002199023255552",
132 /* 40000000000 */ "00000004398046511104",
133 /* 80000000000 */ "00000008796093022208",
134 /* 100000000000 */ "00000017592186044416",
135 /* 200000000000 */ "00000035184372088832",
136 /* 400000000000 */ "00000070368744177664",
137 /* 800000000000 */ "00000140737488355328",
138 /* 1000000000000 */ "00000281474976710656",
139 /* 2000000000000 */ "00000562949953421312",
140 /* 4000000000000 */ "00001125899906842624",
141 /* 8000000000000 */ "00002251799813685248",
142 /* 10000000000000 */ "00004503599627370496",
143 /* 20000000000000 */ "00009007199254740992",
144 /* 40000000000000 */ "00018014398509481984",
145 /* 80000000000000 */ "00036028797018963968",
146 /* 100000000000000 */ "00072057594037927936",
147 /* 200000000000000 */ "00144115188075855872",
148 /* 400000000000000 */ "00288230376151711744",
149 /* 800000000000000 */ "00576460752303423488",
150 /* 1000000000000000 */ "01152921504606846976",
151 /* 2000000000000000 */ "02305843009213693952",
152 /* 4000000000000000 */ "04611686018427387904",
153 /* 8000000000000000 */ "09223372036854775808"
162 if (bitvals[0][0] < '0')
164 for (bit = 0; bit < 64; bit++)
165 for (digit = 0; digit < 20; digit++)
166 bitvals[bit][digit] -= '0';
171 add_bit(int bit, char *answer)
175 for (digit = 19; digit >= 0; digit--) {
176 answer[digit] += bitvals[bit][digit];
179 while (answer[digit] > 9) {
188 decimate(unsigned long hi, unsigned long lo, char *answer)
193 memset(answer, 0, 21);
194 for (bit = 0, mask = 1; bit < 32; bit++, mask <<= 1)
196 add_bit(bit, answer);
197 for (bit = 0, mask = 1; bit < 32; bit++, mask <<= 1)
199 add_bit(bit + 32, answer);
201 for (digit = 0; digit < 20; digit++)
202 answer[digit] += '0';
206 decimate_int64(dt_uint64 * X, char *buf)
208 static char mybuf[21];
214 decimate(X->hi, X->lo, buf);
215 for (p = buf; *p == '0'; p++);
216 return (*p) ? p : p - 1;
219 #endif /* NATIVE_INT64 */
223 shift_int64(dt_uint64 * X, int bits)
234 X->lo = ((X->hi & 0xffffffffL) >> (bits - 32));
237 X->lo = ((X->lo & 0xffffffffL) >> bits)
238 | ((X->hi & ((1 << (32 - bits)) - 1)) << (32 - bits));
239 X->hi = ((X->hi & 0xffffffffL) >> bits);
243 X->hi = ((X->lo & 0xffffffffL) << (bits - 32));
246 X->hi = ((X->hi & 0xffffffffL) << bits)
247 | ((X->lo & (((1 << bits) - 1) << (32 - bits))) >>
249 X->lo = ((X->lo & 0xffffffffL) << bits);
258 /** the rest of this is for testing the int64 suite **/
263 #define stringize(x) xize(x)
264 #define INT64_NAME stringize(unsigned NATIVE_INT64)
267 #endif /* NATIVE_INT64 */
274 signed char testchar = -1;
275 unsigned int testint = (unsigned char)testchar;
277 printf("We think '%s' is a native 64-bit type\n", INT64_NAME);
279 if (testint != 0xff) {
280 printf("testint = 0x%x; should be 0xff\n", testint);
281 fprintf(stderr, "Hmm... char's are not 8 bits. That sucks!\n");
284 printf("Looks like a char is 8 bits...\n");
286 if (sizeof(unsigned NATIVE_INT64) != 8) {
287 printf("sizeof(%s) = %d; should be 8\n", INT64_NAME,
288 sizeof(unsigned NATIVE_INT64));
289 fprintf(stderr, "Hey! You said a %s was 64-bits wide!\n",
293 printf("Yippee! We have a native 64-bit type (%s)\n\n", INT64_NAME);
295 #else /* !NATIVE_INT64 */
297 printf("Using fake 64-bit integers...\n\n");
298 #endif /* NATIVE_INT64 */
303 test_int64_constructs(void)
307 int failures = 0, pass;
310 printf("Constructor/accessor tests:\n");
311 printf("Setting x := %s\n", INT64_TEST_STR);
312 mk64(x, INT64_TEST_HI, INT64_TEST_LO);
315 pass = (x == INT64_TEST_CONST);
316 hexify_int64(&x, buf);
317 printf("NATIVE mk64: x = 0x%16s %s\n", buf,
318 pass ? "PASSED" : "FAILED");
322 pass = (x.hi == INT64_TEST_HI && x.lo == INT64_TEST_LO);
323 printf("FAKE mk64: x.hi = 0x%08lx x.lo = 0x%08lx %s\n", x.hi,
324 x.lo, pass ? "PASSED" : "FAILED");
329 pass = (hi64(x) == INT64_TEST_HI && lo64(x) == INT64_TEST_LO);
330 printf("hi64/lo64: hi64(x) = 0x%08lx lo64(x) = 0x%08lx %s\n", hi64(x),
331 lo64(x), pass ? "PASSED" : "FAILED");
336 pass = (hi == INT64_TEST_HI && lo == INT64_TEST_LO);
337 printf("ex64: hi = 0x%08lx lo = 0x%08lx %s\n", hi, lo,
338 pass ? "PASSED" : "FAILED");
343 pass = (hi64(y) == INT64_TEST_HI && lo64(y) == INT64_TEST_LO);
344 printf("cp64: hi64(y) = 0x%08lx lo64(y) = 0x%08lx %s\n", hi64(y),
345 lo64(y), pass ? "PASSED" : "FAILED");
350 printf("%d/4 tests FAILED\n\n", failures);
352 printf("All 4 tests PASSED\n\n");
357 test_int64_compares()
360 dt_uint64 control, test[NCOMPARE];
361 char cbuf[17], tbuf[17];
362 int i, r, result[NCOMPARE];
363 int pass, failures, FAILURES = 0;
365 printf("Comparison tests:\n");
367 mk64(control, 0x12345678, 0xabcdabcd);
368 mk64(test[0], 0x12340000, 0xabcd0000);
370 mk64(test[1], 0x12340000, 0xabcdabcd);
372 mk64(test[2], 0x12340000, 0xabcdffff);
374 mk64(test[3], 0x12345678, 0xabcd0000);
376 mk64(test[4], 0x12345678, 0xabcdabcd);
378 mk64(test[5], 0x12345678, 0xabcdffff);
380 mk64(test[6], 0x1234ffff, 0xabcd0000);
382 mk64(test[7], 0x1234ffff, 0xabcdabcd);
384 mk64(test[8], 0x1234ffff, 0xabcdffff);
387 for (i = 0; i < NCOMPARE; i++) {
389 hexify_int64(&control, cbuf);
390 hexify_int64(&test[i], tbuf);
392 r = eq64(control, test[i]);
393 pass = (r == (result[i] == 0));
396 printf("0x%s == 0x%s %s\n", cbuf, tbuf, pass ? "PASSED" : "FAILED");
398 r = ne64(control, test[i]);
399 pass = (r == (result[i] != 0));
402 printf("0x%s != 0x%s %s\n", cbuf, tbuf, pass ? "PASSED" : "FAILED");
404 r = lt64(control, test[i]);
405 pass = (r == (result[i] < 0));
408 printf("0x%s < 0x%s %s\n", cbuf, tbuf, pass ? "PASSED" : "FAILED");
410 r = le64(control, test[i]);
411 pass = (r == (result[i] <= 0));
414 printf("0x%s <= 0x%s %s\n", cbuf, tbuf, pass ? "PASSED" : "FAILED");
416 r = gt64(control, test[i]);
417 pass = (r == (result[i] > 0));
420 printf("0x%s > 0x%s %s\n", cbuf, tbuf, pass ? "PASSED" : "FAILED");
422 r = ge64(control, test[i]);
423 pass = (r == (result[i] >= 0));
426 printf("0x%s >= 0x%s %s\n", cbuf, tbuf, pass ? "PASSED" : "FAILED");
432 printf("0x%s is nonzero %s\n", tbuf,
433 pass ? "PASSED" : "FAILED");
436 printf("%d/7 tests on this pair FAILED\n\n", failures);
438 printf("All 7 tests on this pair PASSED\n\n");
442 pass = zero64(control);
445 printf("0x0000000000000000 is zero %s\n",
446 pass ? "PASSED" : "FAILED");
449 printf("%d/%d comparison tests FAILED\n\n", FAILURES,
452 printf("All %d comparison tests PASSED\n\n", 7 * NCOMPARE + 1);
459 printf("No arithmetic tests yet!!!\n");
467 test_int64_constructs();
468 test_int64_compares();