2 ms·
Is that it really an overflow bug? Or a counter wraparound bug? For example, incorrectly using a X > Y comparison on values that are congruential (and do not o
by kazinator 12y ago
Is that it really an overflow bug? Or a counter wraparound bug?
For example, incorrectly using a X > Y comparison on values that are congruential (and do not overflow) isn't an "overflow bug". You can only locally compare values that are close together on the wheel, using subtraction.
The simple thing to do with tick counts is to start them at some high value that is only minutes away from rolling around. Then the situation reproduces soon after startup, rather than days or months later, and its effects are more likely to get caught in testing.
- kazinator 12y agoAnother thing you can do is reduce the range of tick counts. Say you have a 32 bit tick count which increments a hundred times a second, but the longest period (biggest delta between any two live time values) you care about in your module (driver or whatever) is well within 30 seconds. That's only 3000 ticks. Then, whenever you sample the counter, you can mask it down into, say, the 13 bit range [0,8192): effectively a tick counter that rolls over every 81.9 seconds (which you treat correctly as a 13 bit value in your calculations like is_before(t0, t1) or add_time(t0, delta)).
- ambrop7 12y agoThere's no need to reduce the range, you can just treat correctly the full counter range (see my other comment).
- kazinator 12y agoWell, it's a tautology that if you treat correctly anything, you don't need any defensive tricks. Treat all the ones and zeros correctly and everything else takes care of itself.
- ambrop7 12y agoYou mentioned correct treatment first :) I'm just saying that masking the clock is unnecessary and doesn't make correct treatment any easier.
- revelation 12y agoOr you just use a 64 bit variable and don't ever bother with this again in mankinds existence in this galaxy.
- kazinator 12y agoAssuming the frequency stays in the same ballpark. :)