Definition
Counter rollover §6.21, also called wraparound, is the step in which a counter with a fixed number of bits passes its largest value and continues from zero. A counter of bits has values, so an 8-bit counter steps from 255 to 0. Elapsed-time ambiguity is the consequence for timing: two readings of such a counter show how far apart the two positions are, but not how many complete rounds of counts took place between the readings.
Where it fits
Main entry: Counter wraparound (§6.21).
The entry on counter wraparound gives the rule. Reducing a number modulo , the rule of unsigned modular arithmetic §4.16, means adding or subtracting until the result lies between 0 and . The end reading minus the start reading, reduced modulo , equals the true elapsed count exactly when the true elapsed count is less than .
One round of a 32-bit counter at 64,000,000 counts per second lasts seconds, about 67.1 seconds. An interval of one round or longer is reported short by one or more whole rounds. The result is never negative, so a wrong result does not look wrong. A figure built from such a counter cannot show a time as long as one round, so ask what guarantees that every measured interval was shorter.
Questions answered
Why does casting the subtraction help, and when does it fail?
Casting the difference of two counter readings to an unsigned type with as many bits as the counter makes the result roll over as the counter does, so the elapsed count is right even when the counter passed zero between the readings, as long as less than one full round of the counter elapsed. Casting means converting a value to a stated type, and an unsigned type holds only whole numbers from zero upward. Take an 8-bit counter, which has 256 values, a start reading of 250 and an end reading of 5. Counting forward, six counts reach 0 and five more reach 5: the true elapsed count is 11. With the readings held in unsigned 8-bit variables start and end, the expression end - start in the language C gives , because C first converts operands narrower than its standard integer type int to int, a step called integer promotion §4.18. The cast (uint8_t)(end - start) converts to the unsigned 8-bit type, which holds the values 0 to 255. The C standard defines that conversion as adding 256 until the value fits: . For a 32-bit counter in unsigned 32-bit variables, on a machine whose int has 32 bits, the subtraction already rolls over and the cast changes nothing.
The cast fails in three cases.
One full round or more. After 267 counts the readings are also 250 and 5, and the cast gives 11, 256 too few. No arithmetic on two readings can tell 11 from 267. Only an outside guarantee that the interval is shorter than one round can.
Wrong width. A 24-bit counter stored in a 32-bit variable still rolls over after counts. With a start of
0xFFFFF0and an end of0x000010, which are the numbers 16,777,200 and 16 in hexadecimal (base 16) notation, the true count is 32, but 32-bit subtraction gives 4,278,190,112. Keeping only the low 24 bits,(end - start) & 0xFFFFFF, gives 32.Reset or stop. A counter that was reset, stopped or set to a new value between the readings did not advance steadily, and no cast repairs that.
Related entries: Unsigned modular arithmetic (§4.16), DWT cycle counter (§6.20), Integer promotion and conversion (§4.18), Serial-number arithmetic and the half-range ambiguity (§19.3.4), Clock conversion, resolution, and accuracy (§18.1.2). Further reading: [78].