Int64.hx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C)2005-2019 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. import haxe.Int64Helper;
  25. private typedef __Int64 = cs.StdTypes.Int64;
  26. @:coreApi
  27. abstract Int64(__Int64) from __Int64 to __Int64 {
  28. public static inline function make(high:Int32, low:Int32):Int64
  29. return new Int64((cast(high, __Int64) << 32) | (cast(low, __Int64) & (untyped __cs__('0xffffffffL') : Int64)));
  30. private inline function new(x:__Int64)
  31. this = x;
  32. private var val(get, set):__Int64;
  33. inline function get_val():__Int64
  34. return this;
  35. inline function set_val(x:__Int64):__Int64
  36. return this = x;
  37. public var high(get, never):Int32;
  38. inline function get_high():Int32
  39. return cast(this >> 32);
  40. public var low(get, never):Int32;
  41. inline function get_low():Int32
  42. return cast this;
  43. public inline function copy():Int64
  44. return new Int64(this);
  45. @:from public static inline function ofInt(x:Int):Int64
  46. return cast x;
  47. public static inline function toInt(x:Int64):Int {
  48. if (x.val < 0x80000000 || x.val > 0x7FFFFFFF)
  49. throw "Overflow";
  50. return cast x.val;
  51. }
  52. @:deprecated('haxe.Int64.is() is deprecated. Use haxe.Int64.isInt64() instead')
  53. inline public static function is(val:Dynamic):Bool
  54. return Std.isOfType(val, cs.system.Int64);
  55. inline public static function isInt64(val:Dynamic):Bool
  56. return Std.isOfType(val, cs.system.Int64);
  57. public static inline function getHigh(x:Int64):Int32
  58. return cast(x.val >> 32);
  59. public static inline function getLow(x:Int64):Int32
  60. return cast(x.val);
  61. public static inline function isNeg(x:Int64):Bool
  62. return x.val < 0;
  63. public static inline function isZero(x:Int64):Bool
  64. return x.val == 0;
  65. public static inline function compare(a:Int64, b:Int64):Int {
  66. if (a.val < b.val)
  67. return -1;
  68. if (a.val > b.val)
  69. return 1;
  70. return 0;
  71. }
  72. public static inline function ucompare(a:Int64, b:Int64):Int {
  73. if (a.val < 0)
  74. return (b.val < 0) ? compare(a, b) : 1;
  75. return (b.val < 0) ? -1 : compare(a, b);
  76. }
  77. public static inline function toStr(x:Int64):String
  78. return '${x.val}';
  79. public static inline function divMod(dividend:Int64, divisor:Int64):{quotient:Int64, modulus:Int64}
  80. return {quotient: dividend / divisor, modulus: dividend % divisor};
  81. private inline function toString():String
  82. return '$this';
  83. public static function parseString(sParam:String):Int64 {
  84. return Int64Helper.parseString(sParam);
  85. }
  86. public static function fromFloat(f:Float):Int64 {
  87. return Int64Helper.fromFloat(f);
  88. }
  89. @:op(-A) public static function neg(x:Int64):Int64
  90. return -x.val;
  91. @:op(++A) private inline function preIncrement():Int64
  92. return ++this;
  93. @:op(A++) private inline function postIncrement():Int64
  94. return this++;
  95. @:op(--A) private inline function preDecrement():Int64
  96. return --this;
  97. @:op(A--) private inline function postDecrement():Int64
  98. return this--;
  99. @:op(A + B) public static inline function add(a:Int64, b:Int64):Int64
  100. return a.val + b.val;
  101. @:op(A + B) @:commutative private static inline function addInt(a:Int64, b:Int):Int64
  102. return a.val + b;
  103. @:op(A - B) public static inline function sub(a:Int64, b:Int64):Int64
  104. return a.val - b.val;
  105. @:op(A - B) private static inline function subInt(a:Int64, b:Int):Int64
  106. return a.val - b;
  107. @:op(A - B) private static inline function intSub(a:Int, b:Int64):Int64
  108. return a - b.val;
  109. @:op(A * B) public static inline function mul(a:Int64, b:Int64):Int64
  110. return a.val * b.val;
  111. @:op(A * B) @:commutative private static inline function mulInt(a:Int64, b:Int):Int64
  112. return a.val * b;
  113. @:op(A / B) public static inline function div(a:Int64, b:Int64):Int64
  114. return a.val / b.val;
  115. @:op(A / B) private static inline function divInt(a:Int64, b:Int):Int64
  116. return a.val / b;
  117. @:op(A / B) private static inline function intDiv(a:Int, b:Int64):Int64
  118. return a / b.val;
  119. @:op(A % B) public static inline function mod(a:Int64, b:Int64):Int64
  120. return a.val % b.val;
  121. @:op(A % B) private static inline function modInt(a:Int64, b:Int):Int64
  122. return a.val % b;
  123. @:op(A % B) private static inline function intMod(a:Int, b:Int64):Int64
  124. return a % b.val;
  125. @:op(A == B) public static inline function eq(a:Int64, b:Int64):Bool
  126. return a.val == b.val;
  127. @:op(A == B) @:commutative private static inline function eqInt(a:Int64, b:Int):Bool
  128. return a.val == b;
  129. @:op(A != B) public static inline function neq(a:Int64, b:Int64):Bool
  130. return a.val != b.val;
  131. @:op(A != B) @:commutative private static inline function neqInt(a:Int64, b:Int):Bool
  132. return a.val != b;
  133. @:op(A < B) private static inline function lt(a:Int64, b:Int64):Bool
  134. return a.val < b.val;
  135. @:op(A < B) private static inline function ltInt(a:Int64, b:Int):Bool
  136. return a.val < b;
  137. @:op(A < B) private static inline function intLt(a:Int, b:Int64):Bool
  138. return a < b.val;
  139. @:op(A <= B) private static inline function lte(a:Int64, b:Int64):Bool
  140. return a.val <= b.val;
  141. @:op(A <= B) private static inline function lteInt(a:Int64, b:Int):Bool
  142. return a.val <= b;
  143. @:op(A <= B) private static inline function intLte(a:Int, b:Int64):Bool
  144. return a <= b.val;
  145. @:op(A > B) private static inline function gt(a:Int64, b:Int64):Bool
  146. return a.val > b.val;
  147. @:op(A > B) private static inline function gtInt(a:Int64, b:Int):Bool
  148. return a.val > b;
  149. @:op(A > B) private static inline function intGt(a:Int, b:Int64):Bool
  150. return a > b.val;
  151. @:op(A >= B) private static inline function gte(a:Int64, b:Int64):Bool
  152. return a.val >= b.val;
  153. @:op(A >= B) private static inline function gteInt(a:Int64, b:Int):Bool
  154. return a.val >= b;
  155. @:op(A >= B) private static inline function intGte(a:Int, b:Int64):Bool
  156. return a >= b.val;
  157. @:op(~A) private static inline function complement(x:Int64):Int64
  158. return ~x.val;
  159. @:op(A & B) public static inline function and(a:Int64, b:Int64):Int64
  160. return a.val & b.val;
  161. @:op(A | B) public static inline function or(a:Int64, b:Int64):Int64
  162. return a.val | b.val;
  163. @:op(A ^ B) public static inline function xor(a:Int64, b:Int64):Int64
  164. return a.val ^ b.val;
  165. @:op(A << B) public static inline function shl(a:Int64, b:Int):Int64
  166. return a.val << b;
  167. @:op(A >> B) public static inline function shr(a:Int64, b:Int):Int64
  168. return a.val >> b;
  169. @:op(A >>> B) public static inline function ushr(a:Int64, b:Int):Int64
  170. return cast((a.val : cs.StdTypes.UInt64) >> b);
  171. }