2001-02-23 21:21:06 +03:00
|
|
|
/*
|
|
|
|
* Handling of the int64 and uint64 types. Done in 32-bit integers,
|
|
|
|
* for (pre-C99) portability. Hopefully once C99 becomes widespread
|
|
|
|
* we can kiss this lot goodbye...
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2001-08-26 15:35:11 +04:00
|
|
|
#include <string.h>
|
2001-02-23 21:21:06 +03:00
|
|
|
|
2001-08-26 15:35:11 +04:00
|
|
|
#include "int64.h"
|
2001-02-23 21:21:06 +03:00
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
uint64 uint64_div10(uint64 x, int *remainder)
|
|
|
|
{
|
2001-02-23 21:21:06 +03:00
|
|
|
uint64 y;
|
2008-09-16 22:21:33 +04:00
|
|
|
unsigned int rem, r2;
|
2001-02-23 21:21:06 +03:00
|
|
|
y.hi = x.hi / 10;
|
|
|
|
y.lo = x.lo / 10;
|
|
|
|
rem = x.lo % 10;
|
|
|
|
/*
|
|
|
|
* Now we have to add in the remainder left over from x.hi.
|
|
|
|
*/
|
|
|
|
r2 = x.hi % 10;
|
2008-09-16 22:21:33 +04:00
|
|
|
y.lo += r2 * 429496729;
|
|
|
|
rem += r2 * 6;
|
2001-02-23 21:21:06 +03:00
|
|
|
y.lo += rem / 10;
|
|
|
|
rem %= 10;
|
|
|
|
|
|
|
|
if (remainder)
|
|
|
|
*remainder = rem;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
void uint64_decimal(uint64 x, char *buffer)
|
|
|
|
{
|
2001-02-23 21:21:06 +03:00
|
|
|
char buf[20];
|
|
|
|
int start = 20;
|
|
|
|
int d;
|
|
|
|
|
2004-11-25 16:40:01 +03:00
|
|
|
do {
|
2001-02-23 21:21:06 +03:00
|
|
|
x = uint64_div10(x, &d);
|
|
|
|
assert(start > 0);
|
|
|
|
buf[--start] = d + '0';
|
2004-11-25 16:40:01 +03:00
|
|
|
} while (x.hi || x.lo);
|
2001-02-23 21:21:06 +03:00
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
memcpy(buffer, buf + start, sizeof(buf) - start);
|
|
|
|
buffer[sizeof(buf) - start] = '\0';
|
2001-02-23 21:21:06 +03:00
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
uint64 uint64_make(unsigned long hi, unsigned long lo)
|
|
|
|
{
|
2001-02-23 21:21:06 +03:00
|
|
|
uint64 y;
|
2008-09-17 02:56:08 +04:00
|
|
|
y.hi = hi & 0xFFFFFFFFU;
|
2008-09-16 22:21:33 +04:00
|
|
|
y.lo = lo & 0xFFFFFFFFU;
|
2001-02-23 21:21:06 +03:00
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
uint64 uint64_add(uint64 x, uint64 y)
|
|
|
|
{
|
2008-09-16 22:21:33 +04:00
|
|
|
x.lo = (x.lo + y.lo) & 0xFFFFFFFFU;
|
2001-02-23 21:21:06 +03:00
|
|
|
x.hi += y.hi + (x.lo < y.lo ? 1 : 0);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
uint64 uint64_add32(uint64 x, unsigned long y)
|
|
|
|
{
|
2001-02-23 21:21:06 +03:00
|
|
|
uint64 yy;
|
|
|
|
yy.hi = 0;
|
|
|
|
yy.lo = y;
|
|
|
|
return uint64_add(x, yy);
|
|
|
|
}
|
2001-08-26 15:35:11 +04:00
|
|
|
|
|
|
|
int uint64_compare(uint64 x, uint64 y)
|
|
|
|
{
|
|
|
|
if (x.hi != y.hi)
|
|
|
|
return x.hi < y.hi ? -1 : +1;
|
|
|
|
if (x.lo != y.lo)
|
|
|
|
return x.lo < y.lo ? -1 : +1;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-05 17:48:53 +04:00
|
|
|
|
|
|
|
uint64 uint64_subtract(uint64 x, uint64 y)
|
|
|
|
{
|
2008-09-16 22:21:33 +04:00
|
|
|
x.lo = (x.lo - y.lo) & 0xFFFFFFFFU;
|
|
|
|
x.hi = (x.hi - y.hi - (x.lo > (y.lo ^ 0xFFFFFFFFU) ? 1 : 0)) & 0xFFFFFFFFU;
|
2006-08-05 17:48:53 +04:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
double uint64_to_double(uint64 x)
|
|
|
|
{
|
|
|
|
return (4294967296.0 * x.hi) + (double)x.lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 uint64_shift_right(uint64 x, int shift)
|
|
|
|
{
|
|
|
|
if (shift < 32) {
|
|
|
|
x.lo >>= shift;
|
2008-09-16 22:21:33 +04:00
|
|
|
x.lo |= (x.hi << (32-shift)) & 0xFFFFFFFFU;
|
2006-08-05 17:48:53 +04:00
|
|
|
x.hi >>= shift;
|
|
|
|
} else {
|
|
|
|
x.lo = x.hi >> (shift-32);
|
|
|
|
x.hi = 0;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 uint64_shift_left(uint64 x, int shift)
|
|
|
|
{
|
|
|
|
if (shift < 32) {
|
2008-09-16 22:21:33 +04:00
|
|
|
x.hi = (x.hi << shift) & 0xFFFFFFFFU;
|
2006-08-05 17:48:53 +04:00
|
|
|
x.hi |= (x.lo >> (32-shift));
|
2008-09-16 22:21:33 +04:00
|
|
|
x.lo = (x.lo << shift) & 0xFFFFFFFFU;
|
2006-08-05 17:48:53 +04:00
|
|
|
} else {
|
2008-09-16 22:21:33 +04:00
|
|
|
x.hi = (x.lo << (shift-32)) & 0xFFFFFFFFU;
|
2006-08-05 17:48:53 +04:00
|
|
|
x.lo = 0;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 uint64_from_decimal(char *str)
|
|
|
|
{
|
|
|
|
uint64 ret;
|
|
|
|
ret.hi = ret.lo = 0;
|
|
|
|
while (*str >= '0' && *str <= '9') {
|
|
|
|
ret = uint64_add(uint64_shift_left(ret, 3),
|
|
|
|
uint64_shift_left(ret, 1));
|
|
|
|
ret = uint64_add32(ret, *str - '0');
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2008-09-16 22:21:33 +04:00
|
|
|
|
|
|
|
#ifdef TESTMODE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
uint64 x, y, z;
|
|
|
|
char buf[80];
|
|
|
|
|
|
|
|
x = uint64_make(0x3456789AUL, 0xDEF01234UL);
|
|
|
|
printf("%08lx.%08lx\n", x.hi, x.lo);
|
|
|
|
uint64_decimal(x, buf);
|
|
|
|
printf("%s\n", buf);
|
|
|
|
|
|
|
|
y = uint64_add32(x, 0xFFFFFFFFU);
|
|
|
|
printf("%08lx.%08lx\n", y.hi, y.lo);
|
|
|
|
uint64_decimal(y, buf);
|
|
|
|
printf("%s\n", buf);
|
|
|
|
|
|
|
|
z = uint64_subtract(y, x);
|
|
|
|
printf("%08lx.%08lx\n", z.hi, z.lo);
|
|
|
|
uint64_decimal(z, buf);
|
|
|
|
printf("%s\n", buf);
|
|
|
|
|
|
|
|
z = uint64_subtract(x, y);
|
|
|
|
printf("%08lx.%08lx\n", z.hi, z.lo);
|
|
|
|
uint64_decimal(z, buf);
|
|
|
|
printf("%s\n", buf);
|
|
|
|
|
|
|
|
y = uint64_shift_right(x, 4);
|
|
|
|
printf("%08lx.%08lx\n", y.hi, y.lo);
|
|
|
|
|
|
|
|
y = uint64_shift_right(x, 36);
|
|
|
|
printf("%08lx.%08lx\n", y.hi, y.lo);
|
|
|
|
|
|
|
|
y = uint64_shift_left(x, 4);
|
|
|
|
printf("%08lx.%08lx\n", x.hi, x.lo);
|
|
|
|
|
|
|
|
y = uint64_shift_left(x, 36);
|
|
|
|
printf("%08lx.%08lx\n", x.hi, x.lo);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|