Int32.hx 3.7 KB

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