Search code examples
delphidelphi-5integer-overflow

Delphi: How to avoid EIntOverflow underflow when subtracting?


Microsoft already says, in the documentation for GetTickCount, that you could never compare tick counts to check if an interval has passed. e.g.:

Incorrect (pseudo-code):

DWORD endTime = GetTickCount + 10000; //10 s from now

...

if (GetTickCount > endTime)
   break;

The above code is bad because it is suceptable to rollover of the tick counter. For example, assume that the clock is near the end of it's range:

endTime = 0xfffffe00 + 10000
        = 0x00002510; //9,488 decimal

Then you perform your check:

if (GetTickCount > endTime)

Which is satisfied immediatly, since GetTickCount is larger than endTime:

if (0xfffffe01 > 0x00002510)

The solution

Instead you should always subtract the two time intervals:

DWORD startTime = GetTickCount;

...

if (GetTickCount - startTime) > 10000 //if it's been 10 seconds
   break;

Looking at the same math:

if (GetTickCount - startTime) > 10000

if (0xfffffe01 - 0xfffffe00) > 10000

if (1 > 10000)

Which is all well and good in C/C++, where the compiler behaves a certain way.

But what about Delphi?

But when i perform the same math in Delphi, with overflow checking on ({Q+}, {$OVERFLOWCHECKS ON}), the subtraction of the two tick counts generates an EIntOverflow exception when the TickCount rolls over:

if (0x00000100 - 0xffffff00) > 10000

0x00000100 - 0xffffff00 = 0x00000200

What is the intended solution for this problem?

Edit: i've tried to temporarily turn off OVERFLOWCHECKS:

{$OVERFLOWCHECKS OFF}]
   delta = GetTickCount - startTime;
{$OVERFLOWCHECKS ON}

But the subtraction still throws an EIntOverflow exception.

Is there a better solution, involving casts and larger intermediate variable types?


Update

Another SO question i asked explained why {$OVERFLOWCHECKS} doesn't work. It apparently only works at the function level, not the line level. So while the following doesn't work:

{$OVERFLOWCHECKS OFF}]
   delta = GetTickCount - startTime;
{$OVERFLOWCHECKS ON}

the following does work:

delta := Subtract(GetTickCount, startTime);

{$OVERFLOWCHECKS OFF}]
   function Subtract(const B, A: DWORD): DWORD;
   begin
      Result := (B - A);
   end;
{$OVERFLOWCHECKS ON}

Solution

  • How about a simple function like this one?

    function GetElapsedTime(LastTick : Cardinal) : Cardinal;
    var CurrentTick : Cardinal;
    begin
      CurrentTick := GetTickCount;
      if CurrentTick >= LastTick then
        Result := CurrentTick - LastTick
      else
        Result := (High(Cardinal) - LastTick) + CurrentTick;
    end;
    

    So you have

    StartTime := GetTickCount
    ...
    if GetElapsedTime(StartTime) > 10000 then
    ...
    

    It will work as long as the time between StartTime and the current GetTickCount is less than the infamous 49.7 days range of GetTickCount.