bit.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. ** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
  3. ** http://bitop.luajit.org/
  4. **
  5. ** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
  6. **
  7. ** Permission is hereby granted, free of charge, to any person obtaining
  8. ** a copy of this software and associated documentation files (the
  9. ** "Software"), to deal in the Software without restriction, including
  10. ** without limitation the rights to use, copy, modify, merge, publish,
  11. ** distribute, sublicense, and/or sell copies of the Software, and to
  12. ** permit persons to whom the Software is furnished to do so, subject to
  13. ** the following conditions:
  14. **
  15. ** The above copyright notice and this permission notice shall be
  16. ** included in all copies or substantial portions of the Software.
  17. **
  18. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. **
  26. ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
  27. */
  28. extern "C" {
  29. #include "bit.h"
  30. }
  31. #define LUA_BITOP_VERSION "1.0.2"
  32. #define LUA_LIB
  33. extern "C" {
  34. #include "lauxlib.h"
  35. }
  36. #if defined(_MSC_VER) && (_MSC_VER < 1700)
  37. /* Old MSVC is stuck in the last century and doesn't have C99's stdint.h. */
  38. typedef __int32 int32_t;
  39. typedef unsigned __int32 uint32_t;
  40. typedef unsigned __int64 uint64_t;
  41. #else
  42. #include <stdint.h>
  43. #endif
  44. typedef int32_t SBits;
  45. typedef uint32_t UBits;
  46. typedef union {
  47. lua_Number n;
  48. #ifdef LUA_NUMBER_DOUBLE
  49. uint64_t b;
  50. #else
  51. UBits b;
  52. #endif
  53. } BitNum;
  54. /* Convert argument to bit type. */
  55. static UBits barg(lua_State *L, int idx)
  56. {
  57. BitNum bn;
  58. UBits b;
  59. #if LUA_VERSION_NUM < 502
  60. bn.n = lua_tonumber(L, idx);
  61. #else
  62. bn.n = luaL_checknumber(L, idx);
  63. #endif
  64. #if defined(LUA_NUMBER_DOUBLE)
  65. bn.n += 6755399441055744.0; /* 2^52+2^51 */
  66. #ifdef SWAPPED_DOUBLE
  67. b = (UBits)(bn.b >> 32);
  68. #else
  69. b = (UBits)bn.b;
  70. #endif
  71. #elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
  72. defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
  73. defined(LUA_NUMBER_LLONG)
  74. if (sizeof(UBits) == sizeof(lua_Number))
  75. b = bn.b;
  76. else
  77. b = (UBits)(SBits)bn.n;
  78. #elif defined(LUA_NUMBER_FLOAT)
  79. #error "A 'float' lua_Number type is incompatible with this library"
  80. #else
  81. #error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
  82. #endif
  83. #if LUA_VERSION_NUM < 502
  84. if (b == 0 && !lua_isnumber(L, idx)) {
  85. luaL_typerror(L, idx, "number");
  86. }
  87. #endif
  88. return b;
  89. }
  90. /* Return bit type. */
  91. #define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
  92. static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
  93. static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
  94. #define BIT_OP(func, opr) \
  95. static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
  96. for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
  97. BIT_OP(bit_band, &=)
  98. BIT_OP(bit_bor, |=)
  99. BIT_OP(bit_bxor, ^=)
  100. #define bshl(b, n) (b << n)
  101. #define bshr(b, n) (b >> n)
  102. #define bsar(b, n) ((SBits)b >> n)
  103. #define brol(b, n) ((b << n) | (b >> (32-n)))
  104. #define bror(b, n) ((b << (32-n)) | (b >> n))
  105. #define BIT_SH(func, fn) \
  106. static int func(lua_State *L) { \
  107. UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
  108. BIT_SH(bit_lshift, bshl)
  109. BIT_SH(bit_rshift, bshr)
  110. BIT_SH(bit_arshift, bsar)
  111. BIT_SH(bit_rol, brol)
  112. BIT_SH(bit_ror, bror)
  113. static int bit_bswap(lua_State *L)
  114. {
  115. UBits b = barg(L, 1);
  116. b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
  117. BRET(b)
  118. }
  119. static int bit_tohex(lua_State *L)
  120. {
  121. UBits b = barg(L, 1);
  122. SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
  123. const char *hexdigits = "0123456789abcdef";
  124. char buf[8];
  125. int i;
  126. if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
  127. if (n > 8) n = 8;
  128. for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
  129. lua_pushlstring(L, buf, (size_t)n);
  130. return 1;
  131. }
  132. static const struct luaL_Reg bit_funcs[] = {
  133. { "tobit", bit_tobit },
  134. { "bnot", bit_bnot },
  135. { "band", bit_band },
  136. { "bor", bit_bor },
  137. { "bxor", bit_bxor },
  138. { "lshift", bit_lshift },
  139. { "rshift", bit_rshift },
  140. { "arshift", bit_arshift },
  141. { "rol", bit_rol },
  142. { "ror", bit_ror },
  143. { "bswap", bit_bswap },
  144. { "tohex", bit_tohex },
  145. { NULL, NULL }
  146. };
  147. /* Signed right-shifts are implementation-defined per C89/C99.
  148. ** But the de facto standard are arithmetic right-shifts on two's
  149. ** complement CPUs. This behaviour is required here, so test for it.
  150. */
  151. #define BAD_SAR (bsar(-8, 2) != (SBits)-2)
  152. LUALIB_API int luaopen_bit(lua_State *L)
  153. {
  154. UBits b;
  155. lua_pushnumber(L, (lua_Number)1437217655L);
  156. b = barg(L, -1);
  157. if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
  158. const char *msg = "compiled with incompatible luaconf.h";
  159. #ifdef LUA_NUMBER_DOUBLE
  160. #ifdef _WIN32
  161. if (b == (UBits)1610612736L)
  162. msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
  163. #endif
  164. if (b == (UBits)1127743488L)
  165. msg = "not compiled with SWAPPED_DOUBLE";
  166. #endif
  167. if (BAD_SAR)
  168. msg = "arithmetic right-shift broken";
  169. luaL_error(L, "bit library self-test failed (%s)", msg);
  170. }
  171. #if LUA_VERSION_NUM < 502
  172. luaL_register(L, "bit", bit_funcs);
  173. #else
  174. luaL_newlib(L, bit_funcs);
  175. #endif
  176. return 1;
  177. }