Int32.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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
  21. * , STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  24. * DAMAGE.
  25. */
  26. package haxe;
  27. #if !haxe3
  28. @:nativegen
  29. class Int32
  30. {
  31. public static inline function make( a : Int, b : Int ) : Int32
  32. {
  33. return cast ((a << 16) | b);
  34. }
  35. public static inline function ofInt( x : Int ) : Int32
  36. {
  37. return cast x;
  38. }
  39. public static function toInt( x : Int32 ) : Int
  40. {
  41. if ( (((cast x) >> 30) & 1) != ((cast x) >>> 31) ) throw "Overflow " + x;
  42. return cast x;
  43. }
  44. public static inline function add( a : Int32, b : Int32 ) : Int32
  45. {
  46. return cast ((cast a) + cast b);
  47. }
  48. public static inline function sub( a : Int32, b : Int32 ) : Int32
  49. {
  50. return cast ((cast a) - cast b);
  51. }
  52. public static inline function mul( a : Int32, b : Int32 ) : Int32
  53. {
  54. return cast ((cast a) * cast b);
  55. }
  56. public static inline function div( a : Int32, b : Int32 ) : Int32
  57. {
  58. return cast ((cast a) / cast b);
  59. }
  60. public static inline function mod( a : Int32, b : Int32 ) : Int32
  61. {
  62. return cast ((cast a) % cast b);
  63. }
  64. public static inline function shl( a : Int32, b : Int ) : Int32
  65. {
  66. return cast ((cast a) << b);
  67. }
  68. public static inline function shr( a : Int32, b : Int ) : Int32
  69. {
  70. return cast ((cast a) >> b);
  71. }
  72. public static inline function ushr( a : Int32, b : Int ) : Int32
  73. {
  74. return cast ((cast a) >>> b);
  75. }
  76. public static inline function and( a : Int32, b : Int32 ) : Int32
  77. {
  78. return cast ((cast a) & cast b);
  79. }
  80. public static inline function or( a : Int32, b : Int32 ) : Int32
  81. {
  82. return cast ((cast a) | cast b);
  83. }
  84. public static inline function xor( a : Int32, b : Int32 ) : Int32
  85. {
  86. return cast ((cast a) ^ cast b);
  87. }
  88. public static inline function neg( a : Int32 ) : Int32
  89. {
  90. return cast -(cast a);
  91. }
  92. public static inline function complement( a : Int32 ) : Int32
  93. {
  94. return cast ~(cast a);
  95. }
  96. public static inline function compare( a : Int32, b : Int32 ) : Int
  97. {
  98. return (cast a) - cast b;
  99. }
  100. public static inline function isNeg( a : Int32 ) : Bool
  101. {
  102. return (cast a) < 0;
  103. }
  104. public static inline function isZero( a : Int32 ) : Bool
  105. {
  106. return (cast a) == 0;
  107. }
  108. public static function ucompare( a : Int32, b : Int32 ) : Int
  109. {
  110. if( isNeg(a) )
  111. return isNeg(b) ? compare(complement(b),complement(a)) : 1;
  112. return isNeg(b) ? -1 : compare(a,b);
  113. }
  114. public static inline function toNativeInt(a:Int32) : Int
  115. {
  116. return cast a;
  117. }
  118. }
  119. #end