Hardware Traps for Integer Overflow

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Fri May 30 02:08:53 PDT 2014


On Friday, 30 May 2014 at 07:00:58 UTC, Marco Leise wrote:
> Am Thu, 29 May 2014 20:10:13 +0000
> schrieb "John Colvin" <john.loughran.colvin at gmail.com>:
>
>> On Thursday, 29 May 2014 at 20:01:25 UTC, Tobias Pankrath 
>> wrote:
>> > On Thursday, 29 May 2014 at 15:32:54 UTC, Wanderer wrote:
>> >> I don't see any valid alternatives. What should ideally 
>> >> happen if you increment 0xFFFF..FFFF? Should the value 
>> >> remain the same?
>> >
>> > I know at least one firmware running in cars from several 
>> > manufacturers where
>> > this is the desired behavior in dozens of places. Saturated 
>> > arithmetic is common.
>> >
>> > (I'm not saying it should be the default)
>> 
>> There are dedicated instruction in more recent versions of SSE 
>> for saturated arithmetic.
>
> Actually  such instructions exist since MMX on Intel CPUs. The
> question is: Can these new SSE instructions replace integer
> math seemlessly?

As user-defined types in D it can definitely be done, although it 
probably wouldn't be performant for single small ints. I have a 
127bit SIMD integer type struct that I use occasionally, which 
could quite easily be made saturating without degrading 
performance much (if at all, I don't know how fast those 
saturating instructions are).

Obviously that doesn't change the language builtin types who will 
still happily wrap-around.


More information about the Digitalmars-d mailing list