Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 55be6b1

Browse files
WalterBrightdlang-bot
authored andcommitted
optimize int128.d
1 parent 1fb337e commit 55be6b1

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

src/core/int128.d

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ Cent com(Cent c)
6767
pure
6868
Cent neg(Cent c)
6969
{
70-
return inc(com(c)); // ~c + 1
70+
if (c.lo == 0)
71+
c.hi = -c.hi;
72+
else
73+
{
74+
c.lo = -c.lo;
75+
c.hi = ~c.hi;
76+
}
77+
return c;
7178
}
7279

7380
/*****************************
@@ -598,16 +605,9 @@ bool ule(Cent c1, Cent c2)
598605
pure
599606
bool gt(Cent c1, Cent c2)
600607
{
601-
if (cast(I)c1.hi >= 0)
602-
{
603-
if (cast(I)c2.hi >= 0)
604-
return ugt(c1, c2);
605-
return true;
606-
}
607-
if (cast(I)c2.hi >= 0)
608-
return false;
609-
return ugt(c1, c2);
610-
608+
return (c1.hi == c2.hi)
609+
? (c1.lo > c2.lo)
610+
: (cast(I)c1.hi > cast(I)c2.hi);
611611
}
612612

613613
/****************************
@@ -699,6 +699,10 @@ unittest
699699
const C20_0 = Cent(0,20);
700700
const C90_30 = Cent(30,90);
701701

702+
const Cm10_0 = inc(com(C10_0)); // Cent(0, -10);
703+
const Cm10_1 = inc(com(C10_1)); // Cent(-1, -11);
704+
const Cm10_3 = inc(com(C10_3)); // Cent(-3, -11);
705+
702706
enum Cs_3 = Cent(3, I.min);
703707

704708
/************************/
@@ -735,6 +739,10 @@ unittest
735739
assert( !le(C2, C1) );
736740
assert( ge(C2, C1) );
737741

742+
assert(neg(C10_0) == Cm10_0);
743+
assert(neg(C10_1) == Cm10_1);
744+
assert(neg(C10_3) == Cm10_3);
745+
738746
assert(add(C7_1,C3_2) == C10_3);
739747
assert(sub(C1,C2) == Cm1);
740748

0 commit comments

Comments
 (0)