Skip to content

Commit

Permalink
Implement Int256
Browse files Browse the repository at this point in the history
All I did was copy the code over from Int128 like I did with Int128 and replace it with 256 bit integer logic.
  • Loading branch information
SomeGuyWhoLovesCoding committed Nov 9, 2024
1 parent 7bc84e1 commit a6dd4f4
Show file tree
Hide file tree
Showing 5 changed files with 637 additions and 132 deletions.
78 changes: 0 additions & 78 deletions std/haxe/Int128.hx
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,6 @@ abstract Int128(__Int128) from __Int128 to __Int128 {
return make(high, low);
}

@:op(A + B) private static inline function addInt(a:Int128, b:Int):Int128
return add(a, b);

@:op(A + B) private static inline function addInt64(a:Int128, b:Int64):Int128
return add(a, b);

/**
Returns `a` minus `b`.
**/
Expand All @@ -288,18 +282,6 @@ abstract Int128(__Int128) from __Int128 to __Int128 {
return make(high, low);
}

@:op(A - B) private static inline function subInt(a:Int128, b:Int):Int128
return sub(a, b);

@:op(A - B) private static inline function subInt64(a:Int128, b:Int64):Int128
return sub(a, b);

@:op(A - B) private static inline function intSub(a:Int, b:Int128):Int128
return sub(a, b);

@:op(A - B) private static inline function int64Sub(a:Int64, b:Int128):Int128
return sub(a, b);

/**
Returns the product of `a` and `b`.
**/
Expand All @@ -326,102 +308,42 @@ abstract Int128(__Int128) from __Int128 to __Int128 {
return make(high, low);
}

@:op(A * B) @:commutative private static inline function mulInt(a:Int128, b:Int):Int128
return mul(a, b);

@:op(A * B) @:commutative private static inline function mulInt64(a:Int128, b:Int64):Int128
return mul(a, b);

/**
Returns the quotient of `a` divided by `b`.
**/
@:op(A / B) public static inline function div(a:Int128, b:Int128):Int128
return divMod(a, b).quotient;

@:op(A / B) private static inline function divInt(a:Int128, b:Int):Int128
return div(a, b);

@:op(A / B) private static inline function divInt64(a:Int128, b:Int64):Int128
return div(a, b);

@:op(A / B) private static inline function intDiv(a:Int, b:Int128):Int128
return div(a, b).toInt();

@:op(A / B) private static inline function int64Div(a:Int64, b:Int128):Int128
return div(a, b).toInt();

/**
Returns the modulus of `a` divided by `b`.
**/
@:op(A % B) public static inline function mod(a:Int128, b:Int128):Int128
return divMod(a, b).modulus;

@:op(A % B) private static inline function modInt(a:Int128, b:Int):Int128
return mod(a, b).toInt();

@:op(A % B) private static inline function modInt64(a:Int128, b:Int64):Int128
return mod(a, b).toInt();

/**
Returns `true` if `a` is equal to `b`.
**/
@:op(A == B) public static inline function eq(a:Int128, b:Int128):Bool
return a.high == b.high && a.low == b.low;

@:op(A == B) @:commutative private static inline function eqInt(a:Int128, b:Int):Bool
return eq(a, b);

@:op(A == B) @:commutative private static inline function eqInt64(a:Int128, b:Int64):Bool
return eq(a, b);

/**
Returns `true` if `a` is not equal to `b`.
**/
@:op(A != B) public static inline function neq(a:Int128, b:Int128):Bool
return a.high != b.high || a.low != b.low;

@:op(A != B) @:commutative private static inline function neqInt(a:Int128, b:Int):Bool
return neq(a, b);

@:op(A != B) @:commutative private static inline function neqInt64(a:Int128, b:Int64):Bool
return neq(a, b);

@:op(A < B) private static inline function lt(a:Int128, b:Int128):Bool
return compare(a, b) < 0;

@:op(A < B) private static inline function ltInt(a:Int128, b:Int):Bool
return lt(a, b);

@:op(A < B) private static inline function intLt(a:Int, b:Int128):Bool
return lt(a, b);

@:op(A <= B) private static inline function lte(a:Int128, b:Int128):Bool
return compare(a, b) <= 0;

@:op(A <= B) private static inline function lteInt(a:Int128, b:Int):Bool
return lte(a, b);

@:op(A <= B) private static inline function intLte(a:Int, b:Int128):Bool
return lte(a, b);

@:op(A > B) private static inline function gt(a:Int128, b:Int128):Bool
return compare(a, b) > 0;

@:op(A > B) private static inline function gtInt(a:Int128, b:Int):Bool
return gt(a, b);

@:op(A > B) private static inline function intGt(a:Int, b:Int128):Bool
return gt(a, b);

@:op(A >= B) private static inline function gte(a:Int128, b:Int128):Bool
return compare(a, b) >= 0;

@:op(A >= B) private static inline function gteInt(a:Int128, b:Int):Bool
return gte(a, b);

@:op(A >= B) private static inline function intGte(a:Int, b:Int128):Bool
return gte(a, b);

/**
Returns the bitwise NOT of `a`.
**/
Expand Down
6 changes: 6 additions & 0 deletions std/haxe/Int128Helper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class Int128Helper {
*/
public static var minValue64:Int128 = ~maxValue64;

/**
The maximum unsigned `Int64` value with the type `Int128`.
This is handy for type comparison.
*/
public static var maxValue64U:Int128 = Int128.make(0, -1);

/**
The maximum `Int32` value with the type `Int128`.
This is handy for type comparison.
Expand Down
Loading

0 comments on commit a6dd4f4

Please sign in to comment.