lbitlib.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. ** $Id: lbitlib.c,v 1.12 2010/11/22 16:39:20 roberto Exp roberto $
  3. ** Standard library for bitwise operations
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lbitlib_c
  7. #define LUA_LIB
  8. #include "lua.h"
  9. #include "lauxlib.h"
  10. #include "lualib.h"
  11. /* number of bits to consider in a number */
  12. #define NBITS 32
  13. #define ALLONES (~(((~(lua_Unsigned)0) << (NBITS - 1)) << 1))
  14. /* mask to trim extra bits */
  15. #define trim(x) ((x) & ALLONES)
  16. typedef lua_Unsigned b_uint;
  17. #define getuintarg(L,arg) luaL_checkunsigned(L,arg)
  18. static b_uint andaux (lua_State *L) {
  19. int i, n = lua_gettop(L);
  20. b_uint r = ~(b_uint)0;
  21. for (i = 1; i <= n; i++)
  22. r &= getuintarg(L, i);
  23. return trim(r);
  24. }
  25. static int b_and (lua_State *L) {
  26. b_uint r = andaux(L);
  27. lua_pushunsigned(L, r);
  28. return 1;
  29. }
  30. static int b_test (lua_State *L) {
  31. b_uint r = andaux(L);
  32. lua_pushboolean(L, r != 0);
  33. return 1;
  34. }
  35. static int b_or (lua_State *L) {
  36. int i, n = lua_gettop(L);
  37. b_uint r = 0;
  38. for (i = 1; i <= n; i++)
  39. r |= getuintarg(L, i);
  40. lua_pushunsigned(L, trim(r));
  41. return 1;
  42. }
  43. static int b_xor (lua_State *L) {
  44. int i, n = lua_gettop(L);
  45. b_uint r = 0;
  46. for (i = 1; i <= n; i++)
  47. r ^= getuintarg(L, i);
  48. lua_pushunsigned(L, trim(r));
  49. return 1;
  50. }
  51. static int b_not (lua_State *L) {
  52. b_uint r = ~getuintarg(L, 1);
  53. lua_pushunsigned(L, trim(r));
  54. return 1;
  55. }
  56. static int b_shift (lua_State *L, b_uint r, int i) {
  57. if (i < 0) { /* shift right? */
  58. i = -i;
  59. r = trim(r);
  60. if (i >= NBITS) r = 0;
  61. else r >>= i;
  62. }
  63. else { /* shift left */
  64. if (i >= NBITS) r = 0;
  65. else r <<= i;
  66. r = trim(r);
  67. }
  68. lua_pushunsigned(L, r);
  69. return 1;
  70. }
  71. static int b_lshift (lua_State *L) {
  72. return b_shift(L, getuintarg(L, 1), luaL_checkint(L, 2));
  73. }
  74. static int b_rshift (lua_State *L) {
  75. return b_shift(L, getuintarg(L, 1), -luaL_checkint(L, 2));
  76. }
  77. static int b_arshift (lua_State *L) {
  78. b_uint r = getuintarg(L, 1);
  79. int i = luaL_checkint(L, 2);
  80. if (i < 0 || !(r & ((b_uint)1 << (NBITS - 1))))
  81. return b_shift(L, r, -i);
  82. else { /* arithmetic shift for 'negative' number */
  83. if (i >= NBITS) r = ALLONES;
  84. else
  85. r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
  86. lua_pushunsigned(L, r);
  87. return 1;
  88. }
  89. }
  90. static int b_rot (lua_State *L, int i) {
  91. b_uint r = getuintarg(L, 1);
  92. i &= (NBITS - 1); /* i = i % NBITS */
  93. r = trim(r);
  94. r = (r << i) | (r >> (NBITS - i));
  95. lua_pushunsigned(L, trim(r));
  96. return 1;
  97. }
  98. static int b_lrot (lua_State *L) {
  99. return b_rot(L, luaL_checkint(L, 2));
  100. }
  101. static int b_rrot (lua_State *L) {
  102. return b_rot(L, -luaL_checkint(L, 2));
  103. }
  104. static const luaL_Reg bitlib[] = {
  105. {"arshift", b_arshift},
  106. {"band", b_and},
  107. {"bnot", b_not},
  108. {"bor", b_or},
  109. {"bxor", b_xor},
  110. {"lrotate", b_lrot},
  111. {"lshift", b_lshift},
  112. {"rrotate", b_rrot},
  113. {"rshift", b_rshift},
  114. {"btest", b_test},
  115. {NULL, NULL}
  116. };
  117. LUAMOD_API int luaopen_bit32 (lua_State *L) {
  118. luaL_newlib(L, bitlib);
  119. return 1;
  120. }