core.checkedint

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 24 20:47:50 PDT 2016


On 06/24/2016 11:19 PM, Walter Bright wrote:
> On 6/24/2016 6:42 PM, Andrei Alexandrescu wrote:
>> With your suggestion, this would also be an overflow:
>>
>> long x = -1;
>> auto y = array.length + x;
>>
>> I would be hard pressed to acknowledge that as an overflow that needs
>> to be
>> dynamically signaled. And the beauty of two's complement is that
>> indeed it just
>> works.
>
> That's a seductive test case. But I worry that mixed signed/unsigned
> arithmetic is not so simple.
>
> What about:
>
>      x + array.length
>      commutativity in general
>      associativity
>
> Does this become a morass of special cases?

Doesn't seem that way (with some simplifying rules, associativity is 
left to right so not necessarily optimal), but commutativity works 
nicely, please take a close look at 
https://gist.github.com/andralex/a0c0ad32704e6ba66e458ac48add4a99 and 
destroy what you find unfit. And indeed UBSAN is a good baseline to keep 
an eye on. -- Andrei



More information about the Digitalmars-d mailing list