[OT] The Usual Arithmetic Confusions

Walter Bright newshound2 at digitalmars.com
Sat Feb 5 08:59:22 UTC 2022


On 2/4/2022 6:35 PM, Siarhei Siamashka wrote:
> My suggestion:
> 
>   1. Implement `wrapping_add`, `wrapping_sub`, `wrapping_mul` intrinsics similar 
> to Rust, this is easy and costs nothing.
>   2. Implement an experimental `-ftrapv` option in one of the D compilers (most 
> likely GDC or LDC) to catch both signed and unsigned overflows at runtime. Or 
> maybe add function attributes to enable/disable this functionality with a more 
> fine grained control. Yes, I know that this violates the current D language 
> spec, which requires two's complement wraparound for everything, but it doesn't 
> matter for a fancy experimental option.
>   3. Run some tests with `-ftrapv` and check how many arithmetic overflows are 
> actually triggered in Phobos. Replace the affected arithmetic operators with 
> intrinsics if the wrapping behavior is actually intended.
>   4. In the long run consider updating the language spec.
> 
> Benefits: even if `-ftrapv` turns out to have a high overhead, this would still 
> become a useful tool for testing arithmetic overflows safety in applications. 
> Having something is better than having nothing.

I recommend creating a DIP for it.


More information about the Digitalmars-d mailing list