X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=int64.c;h=8a1cda1a5fddc274816a133ba7aabf7ce1e0dba3;hb=49d2cf19accb059b3b68d1fc2b78e606a578c3e8;hp=b43feea1c746daa75ab27fbcb7473f964741299b;hpb=af83203852f38313327107a6074ed3217dffc264;p=PuTTY.git diff --git a/int64.c b/int64.c index b43feea1..8a1cda1a 100644 --- a/int64.c +++ b/int64.c @@ -5,12 +5,12 @@ */ #include +#include -typedef struct { - unsigned long hi, lo; -} uint64, int64; +#include "int64.h" -uint64 uint64_div10(uint64 x, int *remainder) { +uint64 uint64_div10(uint64 x, int *remainder) +{ uint64 y; int rem, r2; y.hi = x.hi / 10; @@ -30,37 +30,50 @@ uint64 uint64_div10(uint64 x, int *remainder) { return y; } -void uint64_decimal(uint64 x, char *buffer) { +void uint64_decimal(uint64 x, char *buffer) +{ char buf[20]; int start = 20; int d; - while (x.hi || x.lo) { + do { x = uint64_div10(x, &d); assert(start > 0); buf[--start] = d + '0'; - } + } while (x.hi || x.lo); - memcpy(buffer, buf+start, sizeof(buf)-start); - buffer[sizeof(buf)-start] = '\0'; + memcpy(buffer, buf + start, sizeof(buf) - start); + buffer[sizeof(buf) - start] = '\0'; } -uint64 uint64_make(unsigned long hi, unsigned long lo) { +uint64 uint64_make(unsigned long hi, unsigned long lo) +{ uint64 y; y.hi = hi; y.lo = lo; return y; } -uint64 uint64_add(uint64 x, uint64 y) { +uint64 uint64_add(uint64 x, uint64 y) +{ x.lo += y.lo; x.hi += y.hi + (x.lo < y.lo ? 1 : 0); return x; } -uint64 uint64_add32(uint64 x, unsigned long y) { +uint64 uint64_add32(uint64 x, unsigned long y) +{ uint64 yy; yy.hi = 0; yy.lo = y; return uint64_add(x, yy); } + +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; +}