Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect/prevent a counter overflow

I have a counter on a 16 bits field that is incremented/decremented over the time by a hardware peripheral.
I sample its value periodically to sum the difference into a 32bits field.

My problem is to detect the overflow/underflow of the 16 bits field when computing the difference.

Let's take an example:
At sample n-1, the counter value Vn-1 is 65530.
As sample n, the counter value Vn is 4.
The counter has been incremented by 10. But the difference (Vn - Vn-1), will be something like 65529 (not sure of the exact value).

The only way I found to detect this overflow is to compare the difference value to a fixed value greater than the max increment (I choose 10000).
Do you know a solution to manage this overflow without comparing to this subjective value?

Here is a code example:

static sint32 overallCount = 0;
sint32 diff;
static sint16 previousValue = 0;
sint16 currentValue;

currentValue = sampleValue();

diff = ((sint32) currentValue) - previousValue;
if(diff > 10000) {
    diff -= 65536;
} else if ((-diff) > 10000) {
    diff += 65536;
}

overallCount += diff;
like image 900
greydet Avatar asked Apr 01 '11 12:04

greydet


2 Answers

My previous answer had some mistakes so I've rewritten it, but the idea is the same, use unsigned types correctly.

Make currentValue and previousValue both unsigned integers of the chosen size (e.g. uint16_t). Then merely subtract them. Since the difference will be implicitly promoted to int if int is a larger type than uint16_t, you'll need to cast or implicitly convert the result back to uint16_t. So:

static uint16_t previousValue;
uint16_t currentValue = sampleValue();
uint16_t diff = currentValue - previousValue;

This makes use of the implicit conversion in the assignment, but you could cast if you like.

like image 182
R.. GitHub STOP HELPING ICE Avatar answered Oct 21 '22 03:10

R.. GitHub STOP HELPING ICE


Here are a few ideas for you:

  1. Do the addition in a 32-bit field, and verify that the result will fit into the 16-bit field afterwards.
  2. Test whether the old value's high bit changed as a result of the addition.
  3. Do the addition in assembly, and check the carry flag afterwards.
like image 45
Jonathan Avatar answered Oct 21 '22 03:10

Jonathan