Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/core/checkedint.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down