In 1978, the ACM published an algorithm by Robert Morris for counting large numbers of events in small registers. It uses one byte to keep track of the approximate logarithm of the count, and randomly increments the logarithm with a probability that scales with the value of the count. You can tune the range and precision of the counter by adjusting the base of the logarithm. Morris suggests pre-calculating a table of probabilities so that no difficult calculations are needed at runtime.
Recently I learned from emsenn about a 2009 paper by Miklós Csűrös titled approximate counting with a floating-point counter. It changes Morris’s algorithm to use a binary log-linear counter instead of a pure logarithm. It reminded me of my hg64 log-linear histograms, so I thought it would be fun to implement, especially since the algorithm can be made shorter and faster.
representing a counter
We need to choose the number of bits to use for the exponent and
mantissa of each approximate floating point counter. Typically the
exponent will be 5 or 6 bits, corresponding to an unpacked exact
counter of 32 or 64 bits. The mantissa will typically be the rest of
an 8 or 16 bit integer (though languages such as Zig and C23 allow
more adventurous integer sizes). I’ll call the approximate counter
type usmol and the unpacked counter type ubig.
#define EXPBITS 5
#define SIGBITS 3
typedef unsigned _BitInt(EXPBITS+SIGBITS) usmol;
typedef uint32_t ubig;
static_assert(sizeof(ubig) * 8 == 1 << EXPBITS,
"ubig must match EXPBITS");
It’s straightforward to extract the exponent and mantissa from a
usmol counter. Unpacking a usmol into a ubig is slightly tricky.
Like hg64 and IEEE754, normal numbers (with a nonzero exponent) have
an implicit leading bit, and denormal numbers (exponent is zero) do
not. The effect is that small counts are represented exactly in a
usmol (up to 2*MANTISSA, covering denormal numbers and exponent ==
1), and larger counts are shifted by the exponent.
(I don’t need the inverse conversion here, but it is used by hg64 to map a sample to a histogram bucket, truncating the value by shifting right.)
#define MANTISSA (1 << SIGBITS)
static inline ubig
exponent(usmol approx) {
return(ubig)(approx / MANTISSA);
}
static inline ubig
mantissa(usmol approx) {
return(ubig)(approx % MANTISSA);
}
static inline ubig
unpack(usmol approx) {
ubig e = exponent(approx) - 1;
ubig m = mantissa(approx) | MANTISSA;
return(approx < MANTISSA ? approx : m << e);
}
bumping a counter
Because binary floating point uses logarithms in base 2, we need the probability of incrementing the counter to scale with 2^-e. Specifically, we want the probability of increasing the counter to be 100% when its exponent is 0 or 1, 50% when the exponent is 2, 25% when it is 3, etc.
Csűrös does this by getting a random bit e times in a loop and
incrementing the counter if they are all zero. A more efficient way to
do this on a computer is to get a ubig word full of random bits and
count the leading or trailing zeroes.
There are a variety of bit scan functions that differ in how
they number bits and what they do when the word is entirely zero. The
POSIX ffs() family has the advantage of no undefined behaviour, but
it weirdly counts from one. However this oddity turns out to match the
way denormal numbers give our exponent an off-by-one bias.
So for instance, when our exponent is 2 and we want a 50% probability,
we want ffs() to return 2 or more.
OK, that determines when to increment the mantissa given a particular exponent. But how does the exponent get incremented? Like Csűrös, we put the exponent immediately to the left of the mantissa; when the mantissa overflows, it rolls over to zero and the exponent ticks up. No special cases are needed.
The resulting code is short:
usmol
bump(rng_t *rng, usmol approx) {
ubig r = ubig_random(rng);
ubig z = (ubig)ffsll((long long)r);
ubig e = exponent(approx);
return(approx + (z >= e));
}
considerations
Morris allows you to smoothly tune the allowable range and precision
of a counter by adjusting the base of the logarithm. By contrast,
Csűrös has very lumpy 2^2^n choice of ranges; that isn’t necessarily
bad because it’s normal for programmers to have to choose between
uint32_t and uint64_t.
Morris uses a lookup table to determine the probability of incrementing, which is tolerable for an 8 bit counter, but it can get large for wider counters. Csűrös just uses a few instructions and bigger counters don’t need much more.
So I don’t think either algorithm is clearly better than the other, except that Csűrös has the winning fun factor of bithacking a bespoke floating point format.
Tony Finch – blog