From 60293d386785b83d01d3cbbba1c0363a43206129 Mon Sep 17 00:00:00 2001 From: DoctorNoobingstoneIPresume Date: Sat, 10 Jul 2021 15:59:24 +0300 Subject: [PATCH] core.checkedint.addu: Faster (like subu: only one compare-and-branch) (resubmit). https://github.com/dlang/druntime/pull/3511#issuecomment-875154147 --- src/core/checkedint.d | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) 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; }