Skip to content

Commit

Permalink
Implement comparison to 128-bit integers
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo committed Jan 14, 2024
1 parent 98fb0bd commit 6ffb894
Showing 1 changed file with 64 additions and 4 deletions.
68 changes: 64 additions & 4 deletions src/Nethermind.Int256/UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1789,16 +1789,76 @@ public static explicit operator UInt128(in UInt256 a) =>
private static bool LessThan(ulong a, in UInt256 b) => b.u3 != 0 || b.u2 != 0 || b.u1 != 0 || a < b.u0;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool LessThan(in UInt256 a, Int128 b) => a.u3 == 0 && a.u2 == 0 && a.u1 == 0 && a.u0 < b;
private static bool LessThan(in UInt256 a, Int128 b)
{
if (b < 0 || a.u3 != 0 || a.u2 != 0)
return false;

var bHigh = (ulong)(b >> 64);
var bLow = (ulong)(b & ulong.MaxValue);

if (a.u1 > bHigh)
return false;

if (a.u1 < bHigh)
return true;

return a.u0 < bLow;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool LessThan(Int128 a, in UInt256 b) => b.u3 != 0 || b.u2 != 0 || b.u1 != 0 || a < b.u0;
private static bool LessThan(Int128 a, in UInt256 b)
{
if (a < 0 || b.u3 != 0 || b.u2 != 0)
return true;

var aHigh = (ulong)(a >> 64);
var aLow = (ulong)(a & ulong.MaxValue);

if (aHigh > b.u1)
return false;

if (aHigh < b.u1)
return true;

return aLow < b.u0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool LessThan(in UInt256 a, UInt128 b) => a.u3 == 0 && a.u2 == 0 && a.u1 == 0 && a.u0 < b;
private static bool LessThan(in UInt256 a, UInt128 b)
{
if (a.u3 != 0 || a.u2 != 0)
return false;

var bHigh = (ulong)(b >> 64);
var bLow = (ulong)(b & ulong.MaxValue);

if (a.u1 > bHigh)
return false;

if (a.u1 < bHigh)
return true;

return a.u0 < bLow;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool LessThan(UInt128 a, in UInt256 b) => b.u3 != 0 || b.u2 != 0 || b.u1 != 0 || a < b.u0;
private static bool LessThan(UInt128 a, in UInt256 b)
{
if (b.u3 != 0 || b.u2 != 0)
return true;

var aHigh = (ulong)(a >> 64);
var aLow = (ulong)(a & ulong.MaxValue);

if (aHigh > b.u1)
return false;

if (aHigh < b.u1)
return true;

return aLow < b.u0;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool LessThan(in UInt256 a, in UInt256 b)
Expand Down

0 comments on commit 6ffb894

Please sign in to comment.