X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftally%2Ftally.c;h=936b5142561a9ec172fd86a96ddf88a529472842;hp=d9f30ba12f39cc87c94d2e0e12c4af23177b8add;hb=ce3198f06194b9ccd9a5d7653daa109175e1226b;hpb=4a4985d51c2272569534457388d6178072f36452 diff --git a/ccan/tally/tally.c b/ccan/tally/tally.c index d9f30ba1..936b5142 100644 --- a/ccan/tally/tally.c +++ b/ccan/tally/tally.c @@ -292,26 +292,28 @@ static int64_t divls64(int64_t u1, uint64_t u0, int64_t v) { int64_t q, uneg, vneg, diff, borrow; - uneg = u1 >> 63; // -1 if u < 0. - if (uneg) { // Compute the absolute - u0 = -u0; // value of the dividend u. + uneg = u1 >> 63; /* -1 if u < 0. */ + if (uneg) { /* Compute the absolute */ + u0 = -u0; /* value of the dividend u. */ borrow = (u0 != 0); u1 = -u1 - borrow; } - vneg = v >> 63; // -1 if v < 0. - v = (v ^ vneg) - vneg; // Absolute value of v. + vneg = v >> 63; /* -1 if v < 0. */ + v = (v ^ vneg) - vneg; /* Absolute value of v. */ - if ((uint64_t)u1 >= (uint64_t)v) + if ((uint64_t)u1 >= (uint64_t)v) { goto overflow; + } q = divlu64(u1, u0, v); - diff = uneg ^ vneg; // Negate q if signs of - q = (q ^ diff) - diff; // u and v differed. + diff = uneg ^ vneg; /* Negate q if signs of */ + q = (q ^ diff) - diff; /* u and v differed. */ - if ((diff ^ q) < 0 && q != 0) { // If overflow, return the largest - overflow: // possible neg. quotient. + if ((diff ^ q) < 0 && q != 0) { /* If overflow, return the + largest */ + overflow: /* possible neg. quotient. */ q = 0x8000000000000000ULL; } return q; @@ -320,8 +322,9 @@ static int64_t divls64(int64_t u1, uint64_t u0, int64_t v) ssize_t tally_mean(const struct tally *tally) { size_t count = tally_num(tally); - if (!count) + if (!count) { return 0; + } if (sizeof(tally->total[0]) == sizeof(uint32_t)) { /* Use standard 64-bit arithmetic. */ @@ -364,10 +367,11 @@ static ssize_t bucket_range(const struct tally *tally, unsigned b, size_t *err) ssize_t min, max; min = bucket_min(tally->min, tally->step_bits, b); - if (b == tally->buckets - 1) + if (b == tally->buckets - 1) { max = tally->max; - else + } else { max = bucket_min(tally->min, tally->step_bits, b+1) - 1; + } /* FIXME: Think harder about cumulative error; is this enough?. */ *err = (max - min + 1) / 2;