Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2005 Jul : Check if overflow WILL happen.

www.cryer.info
Managed Newsgroup Archive

Check if overflow WILL happen.

Subject:Check if overflow WILL happen.
Posted by:"Ian Boyd" (ian.borlandnews008@zunblvlda1.dyndns.org.spamsucks)
Date:Thu, 28 Jul 2005 19:36:43

i want to know if A + B will overflow.

i don't want to go ahead and add them, and try to catch an exception
(catching exceptions is much slower; and besides, it's not exceptional -
it's perfectly reasonable to excpect it to happen, and i am going to have to
react anyway).


Also, it is for a library unit. So i don't want to do the (pseudo-code):

procedure IncrementSomeNumber(var A: LongWord; const N: LongWord; var Carry:
LongWord);
begin
{$OVERFLOWCHECKS ON}
    try
        A := A + N;
        Carry := 0;
    except
        on EIntOverflow do
            Carry := 1;
    end;
{$OVERFLOWCHECKS OFF}
end;

because i don't know if Overflowchecks *were* off before i got there.


So i'm thinking that i might have to do something ugly like:

procedure IncrementSomeNumber(var A: LongWord; const N: LongWord; var Carry:
LongWord);
var
    Intermediate: Inte64;
begin
    Intermediate := A + N;
    A := Intermediate and $FFFFFFFF;
    C := Intermediate shr 32;
end;

promotion to an Int64, and then picking it apart?
Isn't there something inside Delphi itself i can use?
Some of the code that is generated by Delphi itself to look for an overflow?

Replies:

www.cryer.info
Managed Newsgroup Archive