diff --git a/src/core/checkedint.d b/src/core/checkedint.d index 99096fa1c4..b474e141ce 100644 --- a/src/core/checkedint.d +++ b/src/core/checkedint.d @@ -168,7 +168,9 @@ pragma(inline, true) uint addu()(uint x, uint y, ref bool overflow) { immutable uint r = x + y; - if (r < x || r < y) + immutable bool o = r < x; + assert(o == (r < y)); + if (o) overflow = true; return r; } @@ -177,6 +179,14 @@ uint addu()(uint x, uint y, ref bool overflow) @betterC unittest { + for (uint i = 0; i < 10; ++i) + { + bool overflow; + immutable uint r = addu (uint.max - i, uint.max - i, overflow); + assert (r == 2 * (uint.max - i)); + assert (overflow); + } + bool overflow; assert(addu(2, 3, overflow) == 5); assert(!overflow); @@ -203,7 +213,9 @@ pragma(inline, true) ulong addu()(ulong x, ulong y, ref bool overflow) { immutable ulong r = x + y; - if (r < x || r < y) + immutable bool o = r < x; + assert(o == (r < y)); + if (o) overflow = true; return r; } @@ -240,7 +252,9 @@ pragma(inline, true) ucent addu()(ucent x, ucent y, ref bool overflow) { immutable ucent r = x + y; - if (r < x || r < y) + immutable bool o = r < x; + assert(o == (r < y)); + if (o) overflow = true; return r; }