Permalink
Browse files

Some extra int64 functions.

git-svn-id: svn://svn.tartarus.org/sgt/putty@6778 cda61777-01e9-0310-a592-d414129be87e
  • Loading branch information...
1 parent 89c2674 commit c55cfdec64754f3953edcc866f7616793b43dd57 owen committed Aug 5, 2006
Showing with 56 additions and 0 deletions.
  1. +51 −0 int64.c
  2. +5 −0 int64.h
View
51 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;
+}
View
@@ -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

0 comments on commit c55cfde

Please sign in to comment.