tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: detecting integer over/underflow



On Sun, 4 Mar 2012 16:46:29 -0500
christos%zoulas.com@localhost (Christos Zoulas) wrote:

> On Mar 4,  9:14pm, cryintothebluesky%googlemail.com@localhost (Sad Clouds)
> wrote:
> -- Subject: Re: detecting integer over/underflow
> 
> | On Sun, 4 Mar 2012 14:37:31 -0500
> | christos%zoulas.com@localhost (Christos Zoulas) wrote:
> | 
> | > Hello,
> | > 
> | > Currently it is not easy to detect if a value will fit into a type
> | > so I wrote the following macros to simplify this.
> | > 
> | > Basically we have:
> | > 
> | >   __overflow(type, value)
> | > 
> | > That returns false if value fits in type, and true if not. If
> | > anyone knows of a simpler way of doing this, I am all ears :-)
> | 
> | What arithmetic operations are you trying to support - addition,
> | subtraction, multiplication?
> 
> There is no operation involved. I've also renamed __overflow to
> __type_fit to indicate better what this does. It checks if value fits
> in type.

OK so given

int32_t n32;
uint64_t n64;
int32_t n32_result;

n32_result = (uint64_t)n32 + n64;

Are you checking that

1) 64-bit sum will not be truncated and will fit into 32-bit variable,
or

2) 64-bit sum will not overflow 64-bits before it is assigned (and
possibly truncated) to 32-bit variable, or

3) check both


Home | Main Index | Thread Index | Old Index