Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Unsigned integer wrapping on overflow (http://www.velocityreviews.com/forums/t731946-unsigned-integer-wrapping-on-overflow.html)

joe 08-27-2010 07:28 PM

Unsigned integer wrapping on overflow
 
Wouldn't it be better if unsigned integers saturated upon overflow
instead of wrapping? I'm thinking from an overflow detection point of
view and using "overflow" to mean either overflow or underflow. Detection
could be done in a called function then if desired, if one is willing to
give up the maximum unsigned value for a given width integer in select
cases, which sounds very reasonable to me.



Öö Tiib 08-27-2010 07:45 PM

Re: Unsigned integer wrapping on overflow
 
On 27 aug, 22:28, "joe" <jc1...@att.net> wrote:
> Wouldn't it be better if unsigned integers saturated upon overflow
> instead of wrapping? I'm thinking from an overflow detection point of
> view and using "overflow" to mean either overflow or underflow. Detection
> could be done in a called function then if desired, if one is willing to
> give up the maximum unsigned value for a given width integer in select
> cases, which sounds very reasonable to me.


You are not forced to use arithmetic provided by language. You can
easily make your own numeric classes and their operations. Also it is
simple to throw together a static code analysis tool that warns on
usage of any language-provided operations outside of such wrappers. I
do not suggest it ... but C++ is easy to use as sort of language
construction kit from that viewpoint.

Then you can use (one or several) inbuilt values as "invalid value",
"missing value", "overflow value", "underflow value" etc.

Also you may add units to your values so 2 yards multiplied with 2
yards gives 4 square yards and 1 meter - 1 yard gives 0.0856 meters.
More likely than not there already is some library that does it.

Sprechen sie von C++ 08-27-2010 10:42 PM

Re: Unsigned integer wrapping on overflow
 
"joe" <jc1996@att.net> wrote in message
news:9lUdo.101194$4B7.3391@newsfe16.iad...
> Wouldn't it be better if unsigned integers saturated upon overflow instead
> of wrapping? I'm thinking from an overflow detection point of view and
> using "overflow" to mean either overflow or underflow. Detection could be
> done in a called function then if desired, if one is willing to give up
> the maximum unsigned value for a given width integer in select cases,
> which sounds very reasonable to me.


The basic types map directly to the CPU instructions. So given the CPU
supports wraparound to allow for multi-precision calculations that is why it
rolls over. When that happens the CPU asserts the overflow bit so that code
can increment the higher order word.



joe 08-27-2010 10:53 PM

Re: Unsigned integer wrapping on overflow
 
Sprechen sie von C++ wrote:
> "joe" <jc1996@att.net> wrote in message
> news:9lUdo.101194$4B7.3391@newsfe16.iad...
>> Wouldn't it be better if unsigned integers saturated upon overflow
>> instead of wrapping? I'm thinking from an overflow detection point
>> of view and using "overflow" to mean either overflow or underflow.
>> Detection could be done in a called function then if desired, if one
>> is willing to give up the maximum unsigned value for a given width
>> integer in select cases, which sounds very reasonable to me.

>
> The basic types map directly to the CPU instructions. So given the CPU
> supports wraparound to allow for multi-precision calculations that is
> why it rolls over.


But the compiler can interrogate the overflow bit and do what it wants,
yes? What the performance hit would be I don't know.

> When that happens the CPU asserts the overflow bit
> so that code can increment the higher order word.




joe 08-27-2010 10:59 PM

Re: Unsigned integer wrapping on overflow
 
Öö Tiib wrote:
> On 27 aug, 22:28, "joe" <jc1...@att.net> wrote:
>> Wouldn't it be better if unsigned integers saturated upon overflow
>> instead of wrapping? I'm thinking from an overflow detection point of
>> view and using "overflow" to mean either overflow or underflow.
>> Detection could be done in a called function then if desired, if one
>> is willing to give up the maximum unsigned value for a given width
>> integer in select cases, which sounds very reasonable to me.

>
> You are not forced to use arithmetic provided by language. You can
> easily make your own numeric classes and their operations.


Like MS's SafeInt, I know. But if it was built-in to the low level, there
wouldn't be a need for those kinds of things. If people are doing it in
programmer-land, then surely the compiler could do it more efficiently?
And it doesn't have to be one way or the other, a new set of safe
primitive integers could be added for those who want to use them.

> Also it is
> simple to throw together a static code analysis tool that warns on
> usage of any language-provided operations outside of such wrappers. I
> do not suggest it ... but C++ is easy to use as sort of language
> construction kit from that viewpoint.
>
> Then you can use (one or several) inbuilt values as "invalid value",
> "missing value", "overflow value", "underflow value" etc.


At a pretty big performance hit no doubt (considering that integers are
the most primitve things).

>
> Also you may add units to your values so 2 yards multiplied with 2
> yards gives 4 square yards and 1 meter - 1 yard gives 0.0856 meters.
> More likely than not there already is some library that does it.




Öö Tiib 08-27-2010 11:34 PM

Re: Unsigned integer wrapping on overflow
 
On 28 aug, 01:59, "joe" <jc1...@att.net> wrote:
> Öö Tiib wrote:
> > On 27 aug, 22:28, "joe" <jc1...@att.net> wrote:
> >> Wouldn't it be better if unsigned integers saturated upon overflow
> >> instead of wrapping? I'm thinking from an overflow detection point of
> >> view and using "overflow" to mean either overflow or underflow.
> >> Detection could be done in a called function then if desired, if one
> >> is willing to give up the maximum unsigned value for a given width
> >> integer in select cases, which sounds very reasonable to me.

>
> > You are not forced to use arithmetic provided by language. You can
> > easily make your own numeric classes and their operations.

>
> Like MS's SafeInt, I know. But if it was built-in to the low level, there
> wouldn't be a need for those kinds of things. If people are doing it in
> programmer-land, then surely the compiler could do it more efficiently?
> And it doesn't have to be one way or the other, a new set of safe
> primitive integers could be added for those who want to use them.
>
> > Also it is
> > simple to throw together a static code analysis tool that warns on
> > usage of any language-provided operations outside of such wrappers. I
> > do not suggest it ... but C++ is easy to use as sort of language
> > construction kit from that viewpoint.

>
> > Then you can use (one or several) inbuilt values as "invalid value",
> > "missing value", "overflow value", "underflow value" etc.

>
> At a pretty big performance hit no doubt (considering that integers are
> the most primitve things).


Integer arithmetics are cheap. Most int values in software can
consider itself overflown far below 2 billions. Some int variable can
consider itself overflowing as low as 300 other at 2000, depending
what it means. Red-black tree with depth 300 does not perhaps fit into
internet. ;)

joe 08-28-2010 12:47 AM

Re: Unsigned integer wrapping on overflow
 
Öö Tiib wrote:
> On 28 aug, 01:59, "joe" <jc1...@att.net> wrote:
>> Öö Tiib wrote:
>>> On 27 aug, 22:28, "joe" <jc1...@att.net> wrote:
>>>> Wouldn't it be better if unsigned integers saturated upon overflow
>>>> instead of wrapping? I'm thinking from an overflow detection point
>>>> of view and using "overflow" to mean either overflow or underflow.
>>>> Detection could be done in a called function then if desired, if
>>>> one is willing to give up the maximum unsigned value for a given
>>>> width integer in select cases, which sounds very reasonable to me.

>>
>>> You are not forced to use arithmetic provided by language. You can
>>> easily make your own numeric classes and their operations.

>>
>> Like MS's SafeInt, I know. But if it was built-in to the low level,
>> there wouldn't be a need for those kinds of things. If people are
>> doing it in programmer-land, then surely the compiler could do it
>> more efficiently? And it doesn't have to be one way or the other, a
>> new set of safe primitive integers could be added for those who want
>> to use them.
>>
>>> Also it is
>>> simple to throw together a static code analysis tool that warns on
>>> usage of any language-provided operations outside of such wrappers.
>>> I do not suggest it ... but C++ is easy to use as sort of language
>>> construction kit from that viewpoint.

>>
>>> Then you can use (one or several) inbuilt values as "invalid value",
>>> "missing value", "overflow value", "underflow value" etc.

>>
>> At a pretty big performance hit no doubt (considering that integers
>> are the most primitve things).

>
> Integer arithmetics are cheap.


Until you box them.

> Most int values in software can
> consider itself overflown far below 2 billions. Some int variable can
> consider itself overflowing as low as 300 other at 2000, depending
> what it means. Red-black tree with depth 300 does not perhaps fit into
> internet. ;)




tni 08-28-2010 06:28 AM

Re: Unsigned integer wrapping on overflow
 
On 2010-08-28 0:42, Sprechen sie von C++ wrote:
> "joe" <jc1996@att.net> wrote in message
> news:9lUdo.101194$4B7.3391@newsfe16.iad...
>> Wouldn't it be better if unsigned integers saturated upon overflow
>> instead of wrapping? I'm thinking from an overflow detection point of
>> view and using "overflow" to mean either overflow or underflow.
>> Detection could be done in a called function then if desired, if one
>> is willing to give up the maximum unsigned value for a given width
>> integer in select cases, which sounds very reasonable to me.


IMO, well-defined overflow (that you get with unsigned) is at least as
useful as saturation. But an additional mechanism that saturates or
throws on overflow would certainly be extremely useful.

> The basic types map directly to the CPU instructions. So given the CPU
> supports wraparound to allow for multi-precision calculations that is
> why it rolls over.


But the funny thing is, you can't even implement that efficiently in
that so-called systems programming language C (and C++).

> When that happens the CPU asserts the overflow bit so
> that code can increment the higher order word.


Given that pretty much every CPU out there does have an overflow flag,
you should really have some way to access it.

Öö Tiib 08-28-2010 11:41 AM

Re: Unsigned integer wrapping on overflow
 
On 28 aug, 03:47, "joe" <jc1...@att.net> wrote:
> Öö Tiib wrote:
> > On 28 aug, 01:59, "joe" <jc1...@att.net> wrote:
> >> Öö Tiib wrote:
> >>> On 27 aug, 22:28, "joe" <jc1...@att.net> wrote:
> >>>> Wouldn't it be better if unsigned integers saturated upon overflow
> >>>> instead of wrapping? I'm thinking from an overflow detection point
> >>>> of view and using "overflow" to mean either overflow or underflow.
> >>>> Detection could be done in a called function then if desired, if
> >>>> one is willing to give up the maximum unsigned value for a given
> >>>> width integer in select cases, which sounds very reasonable to me.

>
> >>> You are not forced to use arithmetic provided by language. You can
> >>> easily make your own numeric classes and their operations.

>
> >> Like MS's SafeInt, I know. But if it was built-in to the low level,
> >> there wouldn't be a need for those kinds of things. If people are
> >> doing it in programmer-land, then surely the compiler could do it
> >> more efficiently? And it doesn't have to be one way or the other, a
> >> new set of safe primitive integers could be added for those who want
> >> to use them.

>
> >>> Also it is
> >>> simple to throw together a static code analysis tool that warns on
> >>> usage of any language-provided operations outside of such wrappers.
> >>> I do not suggest it ... but C++ is easy to use as sort of language
> >>> construction kit from that viewpoint.

>
> >>> Then you can use (one or several) inbuilt values as "invalid value",
> >>> "missing value", "overflow value", "underflow value" etc.

>
> >> At a pretty big performance hit no doubt (considering that integers
> >> are the most primitve things).

>
> > Integer arithmetics are cheap.

>
> Until you box them.


Your overflow handling does not add less overhead on machine code
level. Additionally that is on common case unnecessary overhead,
because variables that are in danger to overflow their physical limits
are of too small type anyway. Usual int variable is within smaller
limits than int32_t or even int16_t has. Additionally the platforms
where size of int16_t and int is same are becoming more and more
exotic each year. Some people find that unsigned variable has
underflow limit too near to its actual lower limit so even advocate
against using unsigned types. Handling the real limits gives lot more
bang for the buck.

> > Most int values in software can
> > consider itself overflown far below 2 billions. Some int variable can
> > consider itself overflowing as low as 300 other at 2000, depending
> > what it means. Red-black tree with depth 300 does not perhaps fit into
> > internet. ;)



Juha Nieminen 08-28-2010 03:42 PM

Re: Unsigned integer wrapping on overflow
 
joe <jc1996@att.net> wrote:
> But the compiler can interrogate the overflow bit and do what it wants,
> yes? What the performance hit would be I don't know.


The performance hit would be quite drastic. Every single operation you
do with an integer would have to be followed by a conditional jump, which
the CPU might or might not guess correctly (if it doesn't, a really severe
penalty follows).


All times are GMT. The time now is 06:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.