Int64.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package haxe;
  26. using haxe.Int64;
  27. private typedef NativeInt64 = Int;
  28. @:coreApi
  29. @:nativegen class Int64
  30. {
  31. @:extern private static inline function asNative(i:Int64):NativeInt64 return untyped i
  32. @:extern private static inline function ofNative(i:NativeInt64):Int64 return untyped i
  33. @:extern private static inline function mkNative(i:Dynamic):NativeInt64 return i
  34. #if haxe3
  35. public static inline function make( high : Int, low : Int ) : Int64
  36. {
  37. return ((cast(high, NativeInt64) << 32 ) | (cast(low, NativeInt64))).ofNative();
  38. }
  39. public static inline function getLow( x : Int64 ) : Int
  40. {
  41. return cast (x.asNative() & 0xFFFFFFFF.mkNative());
  42. }
  43. public static inline function getHigh( x : Int64 ) : Int
  44. {
  45. return cast(x,NativeInt64) >>> 32;
  46. }
  47. #else
  48. public static inline function make( high : Int32, low : Int32 ) : Int64
  49. {
  50. return ((cast(high, NativeInt64) << 32 ) | (cast(low, NativeInt64))).ofNative();
  51. }
  52. public static inline function ofInt32( x : Int32 ) : Int64 {
  53. return cast x;
  54. }
  55. public static inline function getLow( x : Int64 ) : Int32
  56. {
  57. return cast (x.asNative() & 0xFFFFFFFF.mkNative());
  58. }
  59. public static inline function getHigh( x : Int64 ) : Int32
  60. {
  61. return cast(cast(x,NativeInt64) >>> 32, Int32);
  62. }
  63. #end
  64. public static inline function ofInt( x : Int ) : Int64 {
  65. return cast x;
  66. }
  67. public static inline function toInt( x : Int64 ) : Int
  68. {
  69. return cast x;
  70. }
  71. public static inline function add( a : Int64, b : Int64 ) : Int64
  72. {
  73. return (a.asNative() + b.asNative()).ofNative();
  74. }
  75. public static inline function sub( a : Int64, b : Int64 ) : Int64
  76. {
  77. return (a.asNative() - b.asNative()).ofNative();
  78. }
  79. public static inline function mul( a : Int64, b : Int64 ) : Int64 {
  80. return (a.asNative() * b.asNative()).ofNative();
  81. }
  82. static function divMod( modulus : Int64, divisor : Int64 ) : { quotient : Int64, modulus : Int64 }
  83. {
  84. var q:Int64 = (modulus.asNative() / divisor.asNative()).mkNative().ofNative();
  85. var m:Int64 = (modulus.asNative() % divisor.asNative()).mkNative().ofNative();
  86. return { quotient : q, modulus : m };
  87. }
  88. public static inline function div( a : Int64, b : Int64 ) : Int64 {
  89. return (a.asNative() / b.asNative()).mkNative().ofNative();
  90. }
  91. public static inline function mod( a : Int64, b : Int64 ) : Int64 {
  92. return (a.asNative() % b.asNative()).mkNative().ofNative();
  93. }
  94. public static inline function shl( a : Int64, b : Int ) : Int64 {
  95. return (a.asNative() << b).ofNative();
  96. }
  97. public static inline function shr( a : Int64, b : Int ) : Int64 {
  98. return (a.asNative() >> b).ofNative();
  99. }
  100. public static inline function ushr( a : Int64, b : Int ) : Int64 {
  101. return (a.asNative() >>> b).ofNative();
  102. }
  103. public static inline function and( a : Int64, b : Int64 ) : Int64
  104. {
  105. return (a.asNative() & b.asNative()).ofNative();
  106. }
  107. public static inline function or( a : Int64, b : Int64 ) : Int64
  108. {
  109. return (a.asNative() | b.asNative()).ofNative();
  110. }
  111. public static inline function xor( a : Int64, b : Int64 ) : Int64
  112. {
  113. return (a.asNative() ^ b.asNative()).ofNative();
  114. }
  115. public static inline function neg( a : Int64 ) : Int64
  116. {
  117. return (~a.asNative()).ofNative();
  118. }
  119. public static inline function isNeg( a : Int64 ) : Bool
  120. {
  121. return (a.asNative() < 0.mkNative());
  122. }
  123. public static inline function isZero( a : Int64 ) : Bool
  124. {
  125. return (a.asNative() == 0.mkNative());
  126. }
  127. public static inline function compare( a : Int64, b : Int64 ) : Int
  128. {
  129. return cast (a.asNative() - b.asNative());
  130. }
  131. /**
  132. Compare two Int64 in unsigned mode.
  133. **/
  134. public static function ucompare( a : Int64, b : Int64 ) : Int
  135. {
  136. if (a.asNative() < 0.mkNative())
  137. return (b.asNative() < 0.mkNative()) ? compare( (~a.asNative()).ofNative(), (~b.asNative()).ofNative()) : 1;
  138. return (b.asNative() < 0.mkNative()) ? -1 : compare(a, b);
  139. }
  140. public static inline function toStr( a : Int64 ) : String {
  141. return a + "";
  142. }
  143. }