Skip to content

Instantly share code, notes, and snippets.

@AxGord
Last active December 11, 2019 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AxGord/0b6080f750aa8a5d7e3909d0c370c712 to your computer and use it in GitHub Desktop.
Save AxGord/0b6080f750aa8a5d7e3909d0c370c712 to your computer and use it in GitHub Desktop.
class Main {
private static function main():Void {
trace(-1 > (5: UInt));
}
}
abstract UInt(Int) from Int to Int {
@:op(A + B) private static inline function add(a:UInt, b:UInt):UInt {
return a.toInt() + b.toInt();
}
@:op(A / B) private static inline function div(a:UInt, b:UInt):Float {
return a.toFloat() / b.toFloat();
}
@:op(A * B) private static inline function mul(a:UInt, b:UInt):UInt {
return a.toInt() * b.toInt();
}
@:op(A - B) private static inline function sub(a:UInt, b:UInt):UInt {
return a.toInt() - b.toInt();
}
@:op(A > B)
private static #if !js inline #end function gtInt(a:Int, b:UInt):Bool {
return b < 2147483647 && a > b.toInt();
}
@:op(A < B)
private static #if !js inline #end function ltInt(a:Int, b:UInt):Bool {
return b > 2147483647 || a < b.toInt();
}
@:op(A > B)
private static #if !js inline #end function gt(a:UInt, b:UInt):Bool {
var aNeg = a.toInt() < 0;
var bNeg = b.toInt() < 0;
return if (aNeg != bNeg) aNeg; else a.toInt() > b.toInt();
}
@:op(A >= B)
private static #if !js inline #end function gte(a:UInt, b:UInt):Bool {
var aNeg = a.toInt() < 0;
var bNeg = b.toInt() < 0;
return if (aNeg != bNeg) aNeg; else a.toInt() >= b.toInt();
}
@:op(A < B) private static inline function lt(a:UInt, b:UInt):Bool {
return gt(b, a);
}
@:op(A <= B) private static inline function lte(a:UInt, b:UInt):Bool {
return gte(b, a);
}
@:op(A & B) private static inline function and(a:UInt, b:UInt):UInt {
return a.toInt() & b.toInt();
}
@:op(A | B) private static inline function or(a:UInt, b:UInt):UInt {
return a.toInt() | b.toInt();
}
@:op(A ^ B) private static inline function xor(a:UInt, b:UInt):UInt {
return a.toInt() ^ b.toInt();
}
@:op(A << B) private static inline function shl(a:UInt, b:Int):UInt {
return a.toInt() << b;
}
@:op(A >> B) private static inline function shr(a:UInt, b:Int):UInt {
return a.toInt() >>> b;
}
@:op(A >>> B) private static inline function ushr(a:UInt, b:Int):UInt {
return a.toInt() >>> b;
}
@:op(A % B) private static inline function mod(a:UInt, b:UInt):UInt {
return Std.int(a.toFloat() % b.toFloat());
}
@:commutative @:op(A + B) private static inline function addWithFloat(a:UInt, b:Float):Float {
return a.toFloat() + b;
}
@:commutative @:op(A * B) private static inline function mulWithFloat(a:UInt, b:Float):Float {
return a.toFloat() * b;
}
@:op(A / B) private static inline function divFloat(a:UInt, b:Float):Float {
return a.toFloat() / b;
}
@:op(A / B) private static inline function floatDiv(a:Float, b:UInt):Float {
return a / b.toFloat();
}
@:op(A - B) private static inline function subFloat(a:UInt, b:Float):Float {
return a.toFloat() - b;
}
@:op(A - B) private static inline function floatSub(a:Float, b:UInt):Float {
return a - b.toFloat();
}
@:op(A > B) private static inline function gtFloat(a:UInt, b:Float):Bool {
return a.toFloat() > b;
}
@:commutative @:op(A == B) private static inline function equalsInt<T:Int>(a:UInt, b:T):Bool {
return a.toInt() == b;
}
@:commutative @:op(A != B) private static inline function notEqualsInt<T:Int>(a:UInt, b:T):Bool {
return a.toInt() != b;
}
@:commutative @:op(A == B) private static inline function equalsFloat<T:Float>(a:UInt, b:T):Bool {
return a.toFloat() == b;
}
@:commutative @:op(A != B) private static inline function notEqualsFloat<T:Float>(a:UInt, b:T):Bool {
return a.toFloat() != b;
}
@:op(A >= B) private static inline function gteFloat(a:UInt, b:Float):Bool {
return a.toFloat() >= b;
}
@:op(A > B) private static inline function floatGt(a:Float, b:UInt):Bool {
return a > b.toFloat();
}
@:op(A >= B) private static inline function floatGte(a:Float, b:UInt):Bool {
return a >= b.toFloat();
}
@:op(A < B) private static inline function ltFloat(a:UInt, b:Float):Bool {
return a.toFloat() < b;
}
@:op(A <= B) private static inline function lteFloat(a:UInt, b:Float):Bool {
return a.toFloat() <= b;
}
@:op(A < B) private static inline function floatLt(a:Float, b:UInt):Bool {
return a < b.toFloat();
}
@:op(A <= B) private static inline function floatLte(a:Float, b:UInt):Bool {
return a <= b.toFloat();
}
@:op(A % B) private static inline function modFloat(a:UInt, b:Float):Float {
return a.toFloat() % b;
}
@:op(A % B) private static inline function floatMod(a:Float, b:UInt):Float {
return a % b.toFloat();
}
@:op(~A) private inline function negBits():UInt {
return ~this;
}
@:op(++A) private inline function prefixIncrement():UInt {
return ++this;
}
@:op(A++) private inline function postfixIncrement():UInt {
return this++;
}
@:op(--A) private inline function prefixDecrement():UInt {
return --this;
}
@:op(A--) private inline function postfixDecrement():UInt {
return this--;
}
// TODO: radix is just defined to deal with doc_gen issues
private inline function toString(?radix:Int):String {
#if static
return Std.string(toFloat());
#else
return Std.string(this == null ? null : toFloat());
#end
}
private inline function toInt():Int {
return this;
}
@:to private #if (!js || analyzer) inline #end function toFloat():Float {
var int = toInt();
if (int < 0) {
return 4294967296.0 + int;
} else {
// + 0.0 here to make sure we promote to Float on some platforms
// In particular, PHP was having issues when comparing to Int in the == op.
return int + 0.0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment