-
-
Notifications
You must be signed in to change notification settings - Fork 694
Implement UInt64 on top of Int64Native with unsigned ops routed through native layer #12834
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
Merged
+1,091
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,321 @@ | ||
| /* | ||
| * Copyright (C)2005-2019 Haxe Foundation | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||
| * copy of this software and associated documentation files (the "Software"), | ||
| * to deal in the Software without restriction, including without limitation | ||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| * and/or sell copies of the Software, and to permit persons to whom the | ||
| * Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| package haxe; | ||
|
|
||
| import haxe.numeric.Int64Native; | ||
|
|
||
| /** | ||
| A cross-platform unsigned 64-bit integer type. | ||
|
|
||
| Built on top of `haxe.numeric.Int64Native`, sharing the same backing | ||
| representation as `haxe.Int64`. All bit-identical operations (addition, | ||
| subtraction, multiplication, bitwise) delegate directly to `Int64Native`. | ||
| Operations that differ for unsigned interpretation (division, comparison, | ||
| right shift, toString) use unsigned-specific implementations provided | ||
| by `Int64Native` (udivMod, utoString, etc.), which native targets can | ||
| override for better performance. | ||
| **/ | ||
| abstract UInt64(Int64Native) from Int64Native to Int64Native { | ||
| private inline function new(x:Int64Native) | ||
| this = x; | ||
|
|
||
| /** | ||
| Makes a copy of `this` UInt64. | ||
| **/ | ||
| public inline function copy():UInt64 | ||
| return make(high, low); | ||
|
|
||
| /** | ||
| Construct a UInt64 from two 32-bit words `high` and `low`. | ||
| **/ | ||
| public static inline function make(high:Int32, low:Int32):UInt64 | ||
| return new UInt64(Int64Native.make(high, low)); | ||
|
|
||
| /** | ||
| Returns a UInt64 with the value of the Int `x`. | ||
| `x` is sign-extended to fill 64 bits (same bit pattern as `Int64.ofInt`). | ||
| **/ | ||
| @:from public static inline function ofInt(x:Int):UInt64 | ||
| return new UInt64(Int64Native.ofInt(x)); | ||
|
|
||
| /** | ||
| Returns the low 32 bits of `x` as an Int. | ||
| The top 32 bits are discarded. | ||
| **/ | ||
| public static inline function toInt(x:UInt64):Int | ||
| return x.low; | ||
|
|
||
| /** | ||
| Returns `true` if `x` is exactly zero. | ||
| **/ | ||
| public static inline function isZero(x:UInt64):Bool | ||
| return Int64Native.isZero(x); | ||
|
|
||
| /** | ||
| Compares `a` and `b` as unsigned 64-bit integers. | ||
| Returns a negative value if `a < b`, positive if `a > b`, | ||
| or 0 if `a == b`. | ||
| **/ | ||
| public static inline function compare(a:UInt64, b:UInt64):Int | ||
| return Int64Native.ucompare(a, b); | ||
|
|
||
| /** | ||
| Returns an unsigned decimal `String` representation of `x`. | ||
| **/ | ||
| public inline function toString():String | ||
| return Int64Native.utoString(this); | ||
|
|
||
| /** | ||
| Parses an unsigned decimal string into a UInt64. | ||
| Throws on invalid input, negative values, or overflow. | ||
| **/ | ||
| public static inline function parseString(sParam:String):UInt64 { | ||
| return Int64Native.uparseString(sParam); | ||
| } | ||
|
|
||
| /** | ||
| Converts a non-negative Float to UInt64. | ||
| Throws on negative, NaN, Infinite, or values exceeding `2^53-1`. | ||
| **/ | ||
| public static inline function fromFloat(f:Float):UInt64 { | ||
| return Int64Native.ufromFloat(f); | ||
| } | ||
|
|
||
| /** | ||
| Converts this unsigned 64-bit value to Float. | ||
| Values above `2^53` may lose precision. | ||
| **/ | ||
| public inline function toFloat():Float { | ||
| return Int64Native.utoFloat(this); | ||
| } | ||
Simn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| Performs unsigned integer division of `dividend` by `divisor`. | ||
| Returns `{ quotient : UInt64, modulus : UInt64 }`. | ||
| **/ | ||
| public static function divMod(dividend:UInt64, divisor:UInt64):{quotient:UInt64, modulus:UInt64} { | ||
| var r = Int64Native.udivMod(dividend, divisor); | ||
| return {quotient: r.quotient, modulus: r.modulus}; | ||
| } | ||
|
|
||
| /** | ||
| Reinterprets the bits of an `Int64` as a `UInt64`. | ||
| **/ | ||
| public static inline function fromInt64(x:Int64):UInt64 | ||
| return new UInt64((x : Int64Native)); | ||
|
|
||
| /** | ||
| Reinterprets the bits of this `UInt64` as an `Int64`. | ||
| **/ | ||
| public inline function toInt64():Int64 | ||
| return (this : Int64Native); | ||
|
|
||
| /** | ||
| Returns the two's complement negation of `x`. | ||
| **/ | ||
| @:op(-A) public static inline function neg(x:UInt64):UInt64 | ||
| return Int64Native.neg(x); | ||
|
|
||
| @:op(++A) private inline function preIncrement():UInt64 { | ||
| this = Int64Native.add(this, Int64Native.ofInt(1)); | ||
| return this; | ||
| } | ||
|
|
||
| @:op(A++) private inline function postIncrement():UInt64 { | ||
| var ret = this; | ||
| this = Int64Native.add(this, Int64Native.ofInt(1)); | ||
| return ret; | ||
| } | ||
|
|
||
| @:op(--A) private inline function preDecrement():UInt64 { | ||
| this = Int64Native.sub(this, Int64Native.ofInt(1)); | ||
| return this; | ||
| } | ||
|
|
||
| @:op(A--) private inline function postDecrement():UInt64 { | ||
| var ret = this; | ||
| this = Int64Native.sub(this, Int64Native.ofInt(1)); | ||
| return ret; | ||
| } | ||
|
|
||
| /** | ||
| Returns the sum of `a` and `b`. | ||
| **/ | ||
| @:op(A + B) public static inline function add(a:UInt64, b:UInt64):UInt64 | ||
| return Int64Native.add(a, b); | ||
|
|
||
| @:op(A + B) @:commutative private static inline function addInt(a:UInt64, b:Int):UInt64 | ||
| return add(a, b); | ||
|
|
||
| /** | ||
| Returns `a` minus `b`. | ||
| **/ | ||
| @:op(A - B) public static inline function sub(a:UInt64, b:UInt64):UInt64 | ||
| return Int64Native.sub(a, b); | ||
|
|
||
| @:op(A - B) private static inline function subInt(a:UInt64, b:Int):UInt64 | ||
| return sub(a, b); | ||
|
|
||
| @:op(A - B) private static inline function intSub(a:Int, b:UInt64):UInt64 | ||
| return sub(a, b); | ||
|
|
||
| /** | ||
| Returns the product of `a` and `b`. | ||
| **/ | ||
| @:op(A * B) public static inline function mul(a:UInt64, b:UInt64):UInt64 | ||
| return Int64Native.mul(a, b); | ||
|
|
||
| @:op(A * B) @:commutative private static inline function mulInt(a:UInt64, b:Int):UInt64 | ||
| return mul(a, b); | ||
|
|
||
| /** | ||
| Returns the unsigned quotient of `a` divided by `b`. | ||
| **/ | ||
| @:op(A / B) public static inline function div(a:UInt64, b:UInt64):UInt64 | ||
| return Int64Native.udivMod(a, b).quotient; | ||
|
|
||
Simn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @:op(A / B) private static inline function divInt(a:UInt64, b:Int):UInt64 | ||
| return div(a, b); | ||
|
|
||
| @:op(A / B) private static inline function intDiv(a:Int, b:UInt64):UInt64 | ||
| return div(a, b); | ||
|
|
||
| /** | ||
| Returns the unsigned modulus of `a` divided by `b`. | ||
| **/ | ||
| @:op(A % B) public static inline function mod(a:UInt64, b:UInt64):UInt64 | ||
| return Int64Native.udivMod(a, b).modulus; | ||
|
|
||
Simn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @:op(A % B) private static inline function modInt(a:UInt64, b:Int):UInt64 | ||
| return mod(a, b); | ||
|
|
||
| @:op(A % B) private static inline function intMod(a:Int, b:UInt64):UInt64 | ||
| return mod(a, b); | ||
|
|
||
| /** | ||
| Returns `true` if `a` is equal to `b`. | ||
| **/ | ||
| @:op(A == B) public static inline function eq(a:UInt64, b:UInt64):Bool | ||
| return Int64Native.eq(a, b); | ||
|
|
||
| @:op(A == B) @:commutative private static inline function eqInt(a:UInt64, b:Int):Bool | ||
| return eq(a, b); | ||
|
|
||
| /** | ||
| Returns `true` if `a` is not equal to `b`. | ||
| **/ | ||
| @:op(A != B) public static inline function neq(a:UInt64, b:UInt64):Bool | ||
| return Int64Native.neq(a, b); | ||
|
|
||
| @:op(A != B) @:commutative private static inline function neqInt(a:UInt64, b:Int):Bool | ||
| return neq(a, b); | ||
|
|
||
| @:op(A < B) private static inline function lt(a:UInt64, b:UInt64):Bool | ||
| return compare(a, b) < 0; | ||
|
|
||
| @:op(A < B) private static inline function ltInt(a:UInt64, b:Int):Bool | ||
| return lt(a, b); | ||
|
|
||
| @:op(A < B) private static inline function intLt(a:Int, b:UInt64):Bool | ||
| return lt(a, b); | ||
|
|
||
| @:op(A <= B) private static inline function lte(a:UInt64, b:UInt64):Bool | ||
| return compare(a, b) <= 0; | ||
|
|
||
| @:op(A <= B) private static inline function lteInt(a:UInt64, b:Int):Bool | ||
| return lte(a, b); | ||
|
|
||
| @:op(A <= B) private static inline function intLte(a:Int, b:UInt64):Bool | ||
| return lte(a, b); | ||
|
|
||
| @:op(A > B) private static inline function gt(a:UInt64, b:UInt64):Bool | ||
| return compare(a, b) > 0; | ||
|
|
||
| @:op(A > B) private static inline function gtInt(a:UInt64, b:Int):Bool | ||
| return gt(a, b); | ||
|
|
||
| @:op(A > B) private static inline function intGt(a:Int, b:UInt64):Bool | ||
| return gt(a, b); | ||
|
|
||
| @:op(A >= B) private static inline function gte(a:UInt64, b:UInt64):Bool | ||
| return compare(a, b) >= 0; | ||
|
|
||
| @:op(A >= B) private static inline function gteInt(a:UInt64, b:Int):Bool | ||
| return gte(a, b); | ||
|
|
||
| @:op(A >= B) private static inline function intGte(a:Int, b:UInt64):Bool | ||
| return gte(a, b); | ||
|
|
||
| /** | ||
| Returns the bitwise NOT of `a`. | ||
| **/ | ||
| @:op(~A) private static inline function complement(a:UInt64):UInt64 | ||
| return Int64Native.complement(a); | ||
|
|
||
| /** | ||
| Returns the bitwise AND of `a` and `b`. | ||
| **/ | ||
| @:op(A & B) public static inline function and(a:UInt64, b:UInt64):UInt64 | ||
| return Int64Native.and(a, b); | ||
|
|
||
| /** | ||
| Returns the bitwise OR of `a` and `b`. | ||
| **/ | ||
| @:op(A | B) public static inline function or(a:UInt64, b:UInt64):UInt64 | ||
| return Int64Native.or(a, b); | ||
|
|
||
| /** | ||
| Returns the bitwise XOR of `a` and `b`. | ||
| **/ | ||
| @:op(A ^ B) public static inline function xor(a:UInt64, b:UInt64):UInt64 | ||
| return Int64Native.xor(a, b); | ||
|
|
||
| /** | ||
| Returns `a` left-shifted by `b` bits. | ||
| **/ | ||
| @:op(A << B) public static inline function shl(a:UInt64, b:Int):UInt64 | ||
| return Int64Native.shl(a, b); | ||
|
|
||
| /** | ||
| Returns `a` right-shifted by `b` bits with zero-extension (logical shift). | ||
| For unsigned types, both `>>` and `>>>` perform logical (unsigned) right shift. | ||
| **/ | ||
| @:op(A >> B) public static inline function shr(a:UInt64, b:Int):UInt64 | ||
| return Int64Native.ushr(a, b); | ||
|
|
||
| /** | ||
| Returns `a` right-shifted by `b` bits with zero-extension (logical shift). | ||
| **/ | ||
| @:op(A >>> B) public static inline function ushr(a:UInt64, b:Int):UInt64 | ||
| return Int64Native.ushr(a, b); | ||
|
|
||
| public var high(get, never):Int32; | ||
|
|
||
| private inline function get_high() | ||
| return this.high; | ||
|
|
||
| public var low(get, never):Int32; | ||
|
|
||
| private inline function get_low() | ||
| return this.low; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.