Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement INumber interface #38

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Implement INumber interface #38

wants to merge 3 commits into from

Conversation

rubo
Copy link
Contributor

@rubo rubo commented Jan 14, 2024

  • Implement INumber interface for Int256 and UInt256 types
  • Implement Int128 and UInt128 support

@rubo rubo requested a review from benaadams January 14, 2024 21:55
@@ -1650,6 +1654,16 @@ public static void Xor(in UInt256 a, in UInt256 b, out UInt256 res)
? throw new OverflowException("Cannot convert UInt256 value to ulong.")
: a.u0;

public static explicit operator Int128(in UInt256 a) =>
a.u2 > 0 || a.u3 > 0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also confirm a.u1's last bit does not overflow sign?

public static bool operator >=(UInt128 a, in UInt256 b) => !LessThan(a, in b);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool LessThan(in UInt256 a, int b) => b >= 0 && a.u3 == 0 && a.u2 == 0 && a.u1 == 0 && a.u0 < (ulong)b;
Copy link

@flcl42 flcl42 Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if b==0 it seems to be false, so can be just b>0 I guess

private static bool LessThan(in UInt256 a, int b) => b >= 0 && a.u3 == 0 && a.u2 == 0 && a.u1 == 0 && a.u0 < (ulong)b;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool LessThan(int a, in UInt256 b) => b.u3 != 0 || b.u2 != 0 || b.u1 != 0 || (ulong)a < b.u0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be useful to compare a<0, not sure

private static bool LessThan(int a, in UInt256 b) => b.u3 != 0 || b.u2 != 0 || b.u1 != 0 || (ulong)a < b.u0;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool LessThan(in UInt256 a, uint b) => b >= 0 && a.u3 == 0 && a.u2 == 0 && a.u1 == 0 && a.u0 < b;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b is always >=0

@LukaszRozmej
Copy link
Member

Needs tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants