| 1 | /* |
| 2 | * Handling of the int64 and uint64 types. Done in 32-bit integers, |
| 3 | * for (pre-C99) portability. Hopefully once C99 becomes widespread |
| 4 | * we can kiss this lot goodbye... |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | |
| 9 | typedef struct { |
| 10 | unsigned long hi, lo; |
| 11 | } uint64, int64; |
| 12 | |
| 13 | uint64 uint64_div10(uint64 x, int *remainder) |
| 14 | { |
| 15 | uint64 y; |
| 16 | int rem, r2; |
| 17 | y.hi = x.hi / 10; |
| 18 | y.lo = x.lo / 10; |
| 19 | rem = x.lo % 10; |
| 20 | /* |
| 21 | * Now we have to add in the remainder left over from x.hi. |
| 22 | */ |
| 23 | r2 = x.hi % 10; |
| 24 | y.lo += r2 * 2 * (0x80000000 / 10); |
| 25 | rem += r2 * 2 * (0x80000000 % 10); |
| 26 | y.lo += rem / 10; |
| 27 | rem %= 10; |
| 28 | |
| 29 | if (remainder) |
| 30 | *remainder = rem; |
| 31 | return y; |
| 32 | } |
| 33 | |
| 34 | void uint64_decimal(uint64 x, char *buffer) |
| 35 | { |
| 36 | char buf[20]; |
| 37 | int start = 20; |
| 38 | int d; |
| 39 | |
| 40 | while (x.hi || x.lo) { |
| 41 | x = uint64_div10(x, &d); |
| 42 | assert(start > 0); |
| 43 | buf[--start] = d + '0'; |
| 44 | } |
| 45 | |
| 46 | memcpy(buffer, buf + start, sizeof(buf) - start); |
| 47 | buffer[sizeof(buf) - start] = '\0'; |
| 48 | } |
| 49 | |
| 50 | uint64 uint64_make(unsigned long hi, unsigned long lo) |
| 51 | { |
| 52 | uint64 y; |
| 53 | y.hi = hi; |
| 54 | y.lo = lo; |
| 55 | return y; |
| 56 | } |
| 57 | |
| 58 | uint64 uint64_add(uint64 x, uint64 y) |
| 59 | { |
| 60 | x.lo += y.lo; |
| 61 | x.hi += y.hi + (x.lo < y.lo ? 1 : 0); |
| 62 | return x; |
| 63 | } |
| 64 | |
| 65 | uint64 uint64_add32(uint64 x, unsigned long y) |
| 66 | { |
| 67 | uint64 yy; |
| 68 | yy.hi = 0; |
| 69 | yy.lo = y; |
| 70 | return uint64_add(x, yy); |
| 71 | } |