Int64.hx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe;
  23. using haxe.Int64;
  24. private typedef __Int64 = java.StdTypes.Int64;
  25. @:coreApi
  26. abstract Int64(__Int64) from __Int64 to __Int64
  27. {
  28. public static inline function make( high : Int32, low : Int32 ) : Int64
  29. return new Int64( (cast(high, __Int64) << 32) | (low & untyped __java__('0xffffffffL')) );
  30. private inline function new(x : __Int64)
  31. this = x;
  32. private var val( get, set ) : __Int64;
  33. inline function get_val() : __Int64 return this;
  34. inline function set_val( x : __Int64 ) : __Int64 return this = x;
  35. public inline function copy():Int64
  36. return new Int64( this );
  37. @:from public static inline function ofInt( x : Int ) : Int64
  38. return cast x;
  39. public static inline function toInt( x : Int64 ) : Int {
  40. if( x.val < 0x80000000 || x.val > 0x7FFFFFFF )
  41. throw "Overflow";
  42. return cast x.val;
  43. }
  44. public static inline function getHigh( x : Int64 ) : Int32
  45. return cast( x.val >> 32 );
  46. public static inline function getLow( x : Int64 ) : Int32
  47. return cast( x.val );
  48. public static inline function isNeg( x : Int64 ) : Bool
  49. return x.val < 0;
  50. public static inline function isZero( x : Int64 ) : Bool
  51. return x.val == 0;
  52. public static inline function compare( a : Int64, b : Int64 ) : Int
  53. {
  54. if( a.val < b.val ) return -1;
  55. if( a.val > b.val ) return 1;
  56. return 0;
  57. }
  58. public static inline function ucompare( a : Int64, b : Int64 ) : Int {
  59. if( a.val < 0 )
  60. return ( b.val < 0 ) ? compare( a, b ) : 1;
  61. return ( b.val < 0 ) ? -1 : compare( a, b );
  62. }
  63. public static inline function toStr( x : Int64 ) : String
  64. return x.val + "";
  65. public static inline function divMod( dividend : Int64, divisor : Int64 ) : { quotient : Int64, modulus : Int64 }
  66. return {quotient:0,modulus:0};//{ quotient: dividend / divisor, modulus: dividend % divisor };
  67. private inline function toString() : String
  68. return this + "";
  69. @:op(-A) public static function neg( x : Int64 ) : Int64
  70. return -x.val;
  71. @:op(++A) private inline function preIncrement() : Int64
  72. return ++this;
  73. @:op(A++) private inline function postIncrement() : Int64
  74. return this++;
  75. @:op(--A) private inline function preDecrement() : Int64
  76. return --this;
  77. @:op(A--) private inline function postDecrement() : Int64
  78. return this--;
  79. @:op(A + B) public static inline function add( a : Int64, b : Int64 ) : Int64
  80. return a.val + b.val;
  81. @:op(A + B) @:commutative private static inline function addInt( a : Int64, b : Int ) : Int64
  82. return a.val + b;
  83. @:op(A - B) public static inline function sub( a : Int64, b : Int64 ) : Int64
  84. return a.val - b.val;
  85. @:op(A - B) private static inline function subInt( a : Int64, b : Int ) : Int64
  86. return a.val - b;
  87. @:op(A - B) private static inline function intSub( a : Int, b : Int64 ) : Int64
  88. return a - b.val;
  89. @:op(A * B) public static inline function mul( a : Int64, b : Int64 ) : Int64
  90. return a.val * b.val;
  91. @:op(A * B) @:commutative private static inline function mulInt( a : Int64, b : Int ) : Int64
  92. return a.val * b;
  93. @:op(A / B) public static inline function div( a : Int64, b : Int64 ) : Int64
  94. return a.val / b.val;
  95. @:op(A / B) private static inline function divInt( a : Int64, b : Int ) : Int64
  96. return a.val / b;
  97. @:op(A / B) private static inline function intDiv( a : Int, b : Int64 ) : Int64
  98. return a / b.val;
  99. @:op(A % B) public static inline function mod( a : Int64, b : Int64 ) : Int64
  100. return a.val % b.val;
  101. @:op(A % B) private static inline function modInt( a : Int64, b : Int ) : Int64
  102. return a.val % b;
  103. @:op(A % B) private static inline function intMod( a : Int, b : Int64 ) : Int64
  104. return a % b.val;
  105. @:op(A == B) public static inline function eq( a : Int64, b : Int64 ) : Bool
  106. return a.val == b.val;
  107. @:op(A == B) @:commutative private static inline function eqInt( a : Int64, b : Int ) : Bool
  108. return a.val == b;
  109. @:op(A != B) public static inline function neq( a : Int64, b : Int64 ) : Bool
  110. return a.val != b.val;
  111. @:op(A != B) @:commutative private static inline function neqInt( a : Int64, b : Int ) : Bool
  112. return a.val != b;
  113. @:op(A < B) private static inline function lt( a : Int64, b : Int64 ) : Bool
  114. return a.val < b.val;
  115. @:op(A < B) private static inline function ltInt( a : Int64, b : Int ) : Bool
  116. return a.val < b;
  117. @:op(A < B) private static inline function intLt( a : Int, b : Int64 ) : Bool
  118. return a < b.val;
  119. @:op(A <= B) private static inline function lte( a : Int64, b : Int64 ) : Bool
  120. return a.val <= b.val;
  121. @:op(A <= B) private static inline function lteInt( a : Int64, b : Int ) : Bool
  122. return a.val <= b;
  123. @:op(A <= B) private static inline function intLte( a : Int, b : Int64 ) : Bool
  124. return a <= b.val;
  125. @:op(A > B) private static inline function gt( a : Int64, b : Int64 ) : Bool
  126. return a.val > b.val;
  127. @:op(A > B) private static inline function gtInt( a : Int64, b : Int ) : Bool
  128. return a.val > b;
  129. @:op(A > B) private static inline function intGt( a : Int, b : Int64 ) : Bool
  130. return a > b.val;
  131. @:op(A >= B) private static inline function gte( a : Int64, b : Int64 ) : Bool
  132. return a.val >= b.val;
  133. @:op(A >= B) private static inline function gteInt( a : Int64, b : Int ) : Bool
  134. return a.val >= b;
  135. @:op(A >= B) private static inline function intGte( a : Int, b : Int64 ) : Bool
  136. return a >= b.val;
  137. @:op(~A) private static inline function complement( x : Int64 ) : Int64
  138. return ~x.val;
  139. @:op(A & B) public static inline function and( a : Int64, b : Int64 ) : Int64
  140. return a.val & b.val;
  141. @:op(A | B) public static inline function or( a : Int64, b : Int64 ) : Int64
  142. return a.val | b.val;
  143. @:op(A ^ B) public static inline function xor( a : Int64, b : Int64 ) : Int64
  144. return a.val ^ b.val;
  145. @:op(A << B) public static inline function shl( a : Int64, b : Int ) : Int64
  146. return a.val << b;
  147. @:op(A >> B) public static inline function shr( a : Int64, b : Int ) : Int64
  148. return a.val >> b;
  149. @:op(A >>> B) public static inline function ushr( a : Int64, b : Int ) : Int64
  150. return a.val >>> b;
  151. }