core.checkedint

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 24 09:56:36 PDT 2016


There are a few simple enhancements to add to core.checkedint:

1. No need for different names (addu/adds) etc., overloading should take 
care of it (simplifies caller code). Right now the API is an odd mix of 
overloading and distinct names.

2. The overflow flag should be an integral, not a bool, so you get to 
count how many overflows there were. This is minor but has no extra cost 
on the happy case.

3. There should be an exponentiation function.

So I'm thinking of the signatures:

pure nothrow @nogc @safe
{
   int add(int x, int y, ref uint overflows);
   uint add(uint x, uint y, ref uint overflows);
   long add(long x, int y, ref uint overflows);
   ulong add(ulong x, uint y, ref uint overflows);

   int mul(int x, int y, ref uint overflows);
   uint mul(uint x, uint y, ref uint overflows);
   long mul(long x, int y, ref uint overflows);
   ulong mul(ulong x, uint y, ref uint overflows);

   int sub(int x, int y, ref uint overflows);
   uint sub(uint x, uint y, ref uint overflows);
   long sub(long x, int y, ref uint overflows);
   ulong sub(ulong x, uint y, ref uint overflows);

   int neg(int x, ref uint overflows);
   long neg(long x, ref uint overflows);

   int pow(int x, int y, ref uint overflows);
   uint pow(uint x, uint y, ref uint overflows);
   long pow(long x, int y, ref uint overflows);
   ulong pow(ulong x, uint y, ref uint overflows);
}

Thoughts?


Andrei


More information about the Digitalmars-d mailing list