tally: fixes for 64 bit machines.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 20 Oct 2010 02:21:50 +0000 (12:51 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 20 Oct 2010 02:21:50 +0000 (12:51 +1030)
ccan/tally/tally.c
ccan/tally/test/run-total.c

index b89b0600ac301d0f5d088cb5a31176c841e1c8c3..0d0190795557b60ccfe8d73138dfda2baf99477d 100644 (file)
@@ -322,7 +322,7 @@ ssize_t tally_total(const struct tally *tally, ssize_t *overflow)
        }
 
        /* If result is negative, make sure we can represent it. */
-       if (tally->total[1] & (1 << (SIZET_BITS-1))) {
+       if (tally->total[1] & ((size_t)1 << (SIZET_BITS-1))) {
                /* Must have only underflowed once, and must be able to
                 * represent result at ssize_t. */
                if ((~tally->total[1])+1 != 0
index 1114a6ee1e8c593a9902b23f6943d61af01fb5bf..d7d73e58a5dd290f9f6c6de3d7b57df405a6f367 100644 (file)
@@ -38,14 +38,17 @@ int main(void)
        tally_add(tally, max);
        total = tally_total(tally, &overflow);
        ok1(overflow == 0);
-       ok1((size_t)total == 0xFFFFFFFE);
+       ok1((size_t)total == (size_t)-2);
        ok1(tally_total(tally, NULL) == max);
 
        /* Overflow into upper size_t. */
        tally_add(tally, max);
        total = tally_total(tally, &overflow);
        ok1(overflow == 1);
-       ok1((size_t)total == 0x7FFFFFFD);
+       if (sizeof(size_t) == 4)
+               ok1((size_t)total == 0x7FFFFFFD);
+       else if (sizeof(size_t) == 8)
+               ok1((size_t)total == 0x7FFFFFFFFFFFFFFDULL);
        ok1(tally_total(tally, NULL) == max);
        free(tally);