]> git.ozlabs.org Git - ccan/blobdiff - ccan/tally/tally.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / tally / tally.c
index d9f30ba12f39cc87c94d2e0e12c4af23177b8add..5cc3352a4a90df99362ac74ea0e9a6635ccafec4 100644 (file)
@@ -56,7 +56,7 @@ static unsigned bucket_of(ssize_t min, unsigned step_bits, ssize_t val)
                return 0;
        }
        assert(step_bits < SIZET_BITS);
-       return (size_t)(val - min) >> step_bits;
+       return ((size_t)val - (size_t)min) >> step_bits;
 }
 
 /* Return the min value in bucket b. */
@@ -67,7 +67,7 @@ static ssize_t bucket_min(ssize_t min, unsigned step_bits, unsigned b)
                return min;
        }
        assert(step_bits < SIZET_BITS);
-       return min + ((ssize_t)b << step_bits);
+       return min + ((size_t)b << step_bits);
 }
 
 /* Does shifting by this many bits truncate the number? */
@@ -76,6 +76,9 @@ static bool shift_overflows(size_t num, unsigned bits)
        if (bits == 0) {
                return false;
        }
+       if (bits >= SIZET_BITS) {
+               return true;
+       }
 
        return ((num << bits) >> 1) != (num << (bits - 1));
 }
@@ -94,7 +97,7 @@ static void renormalize(struct tally *tally,
 
        /* If we don't have sufficient range, increase step bits until
         * buckets cover entire range of ssize_t anyway. */
-       range = (new_max - new_min) + 1;
+       range = ((size_t)new_max - (size_t)new_min) + 1;
        while (!shift_overflows(tally->buckets, tally->step_bits)
               && range > ((size_t)tally->buckets << tally->step_bits)) {
                /* Collapse down. */
@@ -113,11 +116,13 @@ static void renormalize(struct tally *tally,
        memset(tally->counts, 0, sizeof(tally->counts[0]) * old_min);
 
        /* If we moved boundaries, adjust buckets to that ratio. */
-       spill = (tally->min - new_min) % (1 << tally->step_bits);
-       for (i = 0; i < tally->buckets-1; i++) {
-               size_t adjust = (tally->counts[i] >> tally->step_bits) * spill;
-               tally->counts[i] -= adjust;
-               tally->counts[i+1] += adjust;
+       if (tally->step_bits < SIZET_BITS) {
+               spill = (tally->min - new_min) % ((size_t)1 << tally->step_bits);
+               for (i = 0; i < tally->buckets-1; i++) {
+                       size_t adjust = (tally->counts[i] >> tally->step_bits) * spill;
+                       tally->counts[i] -= adjust;
+                       tally->counts[i+1] += adjust;
+               }
        }
 
 update:
@@ -292,26 +297,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 +327,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 +372,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;
@@ -382,8 +391,9 @@ ssize_t tally_approx_median(const struct tally *tally, size_t *err)
 
        for (i = 0; i < tally->buckets; i++) {
                total += tally->counts[i];
-               if (total * 2 >= count)
+               if (total * 2 >= count) {
                        break;
+               }
        }
        return bucket_range(tally, i, err);
 }
@@ -417,9 +427,11 @@ static unsigned get_max_bucket(const struct tally *tally)
 {
        unsigned int i;
 
-       for (i = tally->buckets; i > 0; i--)
-               if (tally->counts[i-1])
+       for (i = tally->buckets; i > 0; i--) {
+               if (tally->counts[i-1]) {
                        break;
+               }
+       }
        return i;
 }
 
@@ -444,15 +456,17 @@ char *tally_histogram(const struct tally *tally,
                /* We create a temporary then renormalize so < height. */
                /* FIXME: Antialias properly! */
                tmp = tally_new(tally->buckets);
-               if (!tmp)
+               if (!tmp) {
                        return NULL;
+               }
                tmp->min = tally->min;
                tmp->max = tally->max;
                tmp->step_bits = tally->step_bits;
                memcpy(tmp->counts, tally->counts,
                       sizeof(tally->counts[0]) * tmp->buckets);
-               while ((max_bucket = get_max_bucket(tmp)) >= height)
+               while ((max_bucket = get_max_bucket(tmp)) >= height) {
                        renormalize(tmp, tmp->min, tmp->max * 2);
+               }
                /* Restore max */
                tmp->max = tally->max;
                tally = tmp;
@@ -462,8 +476,9 @@ char *tally_histogram(const struct tally *tally,
        /* Figure out longest line, for scale. */
        largest_bucket = 0;
        for (i = 0; i < tally->buckets; i++) {
-               if (tally->counts[i] > largest_bucket)
+               if (tally->counts[i] > largest_bucket) {
                        largest_bucket = tally->counts[i];
+               }
        }
 
        p = graph = (char *)malloc(height * (width + 1) + 1);
@@ -479,25 +494,28 @@ char *tally_histogram(const struct tally *tally,
                row = height - i - 1;
                count = (double)tally->counts[row] / largest_bucket * (width-1)+1;
 
-               if (row == 0)
+               if (row == 0) {
                        covered = snprintf(p, width, "%zi", tally->min);
-               else if (row == height - 1)
+               } else if (row == height - 1) {
                        covered = snprintf(p, width, "%zi", tally->max);
-               else if (row == bucket_of(tally->min, tally->step_bits, 0))
+               } else if (row == bucket_of(tally->min, tally->step_bits, 0)) {
                        *p = '+';
-               else
+               } else {
                        *p = '|';
+               }
 
-               if (covered > width)
+               if (covered > width) {
                        covered = width;
+               }
                p += covered;
 
-               if (count > covered)
+               if (count > covered) {
                        count -= covered;
-               else
+                       memset(p, '*', count);
+               } else {
                        count = 0;
+               }
 
-               memset(p, '*', count);
                p += count;
                *p = '\n';
                p++;