Some extra int64 functions.
authorowen <owen@cda61777-01e9-0310-a592-d414129be87e>
Sat, 5 Aug 2006 13:48:53 +0000 (13:48 +0000)
committerowen <owen@cda61777-01e9-0310-a592-d414129be87e>
Sat, 5 Aug 2006 13:48:53 +0000 (13:48 +0000)
git-svn-id: svn://svn.tartarus.org/sgt/putty@6778 cda61777-01e9-0310-a592-d414129be87e

int64.c
int64.h

diff --git a/int64.c b/int64.c
index 8a1cda1..a466bdd 100644 (file)
--- a/int64.c
+++ b/int64.c
@@ -77,3 +77,54 @@ int uint64_compare(uint64 x, uint64 y)
        return x.lo < y.lo ? -1 : +1;
     return 0;
 }
+
+uint64 uint64_subtract(uint64 x, uint64 y)
+{
+    x.lo -= y.lo;
+    x.hi -= y.hi + (x.lo > ~y.lo ? 1 : 0);
+    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;
+       x.lo |= (x.hi << (32-shift));
+       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) {
+       x.hi <<= shift;
+       x.hi |= (x.lo >> (32-shift));
+       x.lo <<= shift;
+    } else {
+       x.hi = x.lo << (shift-32);
+       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;
+}
diff --git a/int64.h b/int64.h
index 1b23f82..f62f8ef 100644 (file)
--- a/int64.h
+++ b/int64.h
@@ -15,5 +15,10 @@ uint64 uint64_make(unsigned long hi, unsigned long lo);
 uint64 uint64_add(uint64 x, uint64 y);
 uint64 uint64_add32(uint64 x, unsigned long y);
 int uint64_compare(uint64 x, uint64 y);
+uint64 uint64_subtract(uint64 x, uint64 y);
+double uint64_to_double(uint64 x);
+uint64 uint64_shift_right(uint64 x, int shift);
+uint64 uint64_shift_left(uint64 x, int shift);
+uint64 uint64_from_decimal(char *str);
 
 #endif