bit.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include "bit.h"
  29. #define LUA_BITOP_VERSION "1.0.2"
  30. #define LUA_LIB
  31. #include "lauxlib.h"
  32. #ifdef _MSC_VER
  33. /* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
  34. typedef __int32 int32_t;
  35. typedef unsigned __int32 uint32_t;
  36. typedef unsigned __int64 uint64_t;
  37. #else
  38. #include <stdint.h>
  39. #endif
  40. typedef int32_t SBits;
  41. typedef uint32_t UBits;
  42. typedef union {
  43. lua_Number n;
  44. #ifdef LUA_NUMBER_DOUBLE
  45. uint64_t b;
  46. #else
  47. UBits b;
  48. #endif
  49. } BitNum;
  50. /* Convert argument to bit type. */
  51. static UBits barg(lua_State *L, int idx)
  52. {
  53. BitNum bn;
  54. UBits b;
  55. #if LUA_VERSION_NUM < 502
  56. bn.n = lua_tonumber(L, idx);
  57. #else
  58. bn.n = luaL_checknumber(L, idx);
  59. #endif
  60. #if defined(LUA_NUMBER_DOUBLE)
  61. bn.n += 6755399441055744.0; /* 2^52+2^51 */
  62. #ifdef SWAPPED_DOUBLE
  63. b = (UBits)(bn.b >> 32);
  64. #else
  65. b = (UBits)bn.b;
  66. #endif
  67. #elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
  68. defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
  69. defined(LUA_NUMBER_LLONG)
  70. if (sizeof(UBits) == sizeof(lua_Number))
  71. b = bn.b;
  72. else
  73. b = (UBits)(SBits)bn.n;
  74. #elif defined(LUA_NUMBER_FLOAT)
  75. #error "A 'float' lua_Number type is incompatible with this library"
  76. #else
  77. #error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
  78. #endif
  79. #if LUA_VERSION_NUM < 502
  80. if (b == 0 && !lua_isnumber(L, idx)) {
  81. luaL_typerror(L, idx, "number");
  82. }
  83. #endif
  84. return b;
  85. }
  86. /* Return bit type. */
  87. #define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
  88. static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
  89. static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
  90. #define BIT_OP(func, opr) \
  91. static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
  92. for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
  93. BIT_OP(bit_band, &=)
  94. BIT_OP(bit_bor, |=)
  95. BIT_OP(bit_bxor, ^=)
  96. #define bshl(b, n) (b << n)
  97. #define bshr(b, n) (b >> n)
  98. #define bsar(b, n) ((SBits)b >> n)
  99. #define brol(b, n) ((b << n) | (b >> (32-n)))
  100. #define bror(b, n) ((b << (32-n)) | (b >> n))
  101. #define BIT_SH(func, fn) \
  102. static int func(lua_State *L) { \
  103. UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
  104. BIT_SH(bit_lshift, bshl)
  105. BIT_SH(bit_rshift, bshr)
  106. BIT_SH(bit_arshift, bsar)
  107. BIT_SH(bit_rol, brol)
  108. BIT_SH(bit_ror, bror)
  109. static int bit_bswap(lua_State *L)
  110. {
  111. UBits b = barg(L, 1);
  112. b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
  113. BRET(b)
  114. }
  115. static int bit_tohex(lua_State *L)
  116. {
  117. UBits b = barg(L, 1);
  118. SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
  119. const char *hexdigits = "0123456789abcdef";
  120. char buf[8];
  121. int i;
  122. if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
  123. if (n > 8) n = 8;
  124. for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
  125. lua_pushlstring(L, buf, (size_t)n);
  126. return 1;
  127. }
  128. static const struct luaL_Reg bit_funcs[] = {
  129. { "tobit", bit_tobit },
  130. { "bnot", bit_bnot },
  131. { "band", bit_band },
  132. { "bor", bit_bor },
  133. { "bxor", bit_bxor },
  134. { "lshift", bit_lshift },
  135. { "rshift", bit_rshift },
  136. { "arshift", bit_arshift },
  137. { "rol", bit_rol },
  138. { "ror", bit_ror },
  139. { "bswap", bit_bswap },
  140. { "tohex", bit_tohex },
  141. { NULL, NULL }
  142. };
  143. /* Signed right-shifts are implementation-defined per C89/C99.
  144. ** But the de facto standard are arithmetic right-shifts on two's
  145. ** complement CPUs. This behaviour is required here, so test for it.
  146. */
  147. #define BAD_SAR (bsar(-8, 2) != (SBits)-2)
  148. LUALIB_API int luaopen_bit(lua_State *L)
  149. {
  150. UBits b;
  151. lua_pushnumber(L, (lua_Number)1437217655L);
  152. b = barg(L, -1);
  153. if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
  154. const char *msg = "compiled with incompatible luaconf.h";
  155. #ifdef LUA_NUMBER_DOUBLE
  156. #ifdef _WIN32
  157. if (b == (UBits)1610612736L)
  158. msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
  159. #endif
  160. if (b == (UBits)1127743488L)
  161. msg = "not compiled with SWAPPED_DOUBLE";
  162. #endif
  163. if (BAD_SAR)
  164. msg = "arithmetic right-shift broken";
  165. luaL_error(L, "bit library self-test failed (%s)", msg);
  166. }
  167. #if LUA_VERSION_NUM < 502
  168. luaL_register(L, "bit", bit_funcs);
  169. #else
  170. luaL_newlib(L, bit_funcs);
  171. #endif
  172. return 1;
  173. }