lbitlib.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. ** $Id: lbitlib.c,v 1.7 2010/10/25 14:32:36 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 considered when shifting/rotating (must be a power of 2) */
  12. #define NBITS 32
  13. typedef LUA_INT32 b_int;
  14. typedef unsigned LUA_INT32 b_uint;
  15. #define getuintarg(L,arg) luaL_checkunsigned(L,arg)
  16. static b_uint andaux (lua_State *L) {
  17. int i, n = lua_gettop(L);
  18. b_uint r = ~(b_uint)0;
  19. for (i = 1; i <= n; i++)
  20. r &= getuintarg(L, i);
  21. return r;
  22. }
  23. static int b_and (lua_State *L) {
  24. b_uint r = andaux(L);
  25. lua_pushunsigned(L, r);
  26. return 1;
  27. }
  28. static int b_test (lua_State *L) {
  29. b_uint r = andaux(L);
  30. lua_pushboolean(L, r != 0);
  31. return 1;
  32. }
  33. static int b_or (lua_State *L) {
  34. int i, n = lua_gettop(L);
  35. b_uint r = 0;
  36. for (i = 1; i <= n; i++)
  37. r |= getuintarg(L, i);
  38. lua_pushunsigned(L, r);
  39. return 1;
  40. }
  41. static int b_xor (lua_State *L) {
  42. int i, n = lua_gettop(L);
  43. b_uint r = 0;
  44. for (i = 1; i <= n; i++)
  45. r ^= getuintarg(L, i);
  46. lua_pushunsigned(L, r);
  47. return 1;
  48. }
  49. static int b_not (lua_State *L) {
  50. b_uint r = ~getuintarg(L, 1);
  51. lua_pushunsigned(L, r);
  52. return 1;
  53. }
  54. static int b_shift (lua_State *L, b_uint r, int i) {
  55. if (i < 0) { /* shift right? */
  56. i = -i;
  57. if (i >= NBITS) r = 0;
  58. else r >>= i;
  59. }
  60. else { /* shift left */
  61. if (i >= NBITS) r = 0;
  62. else r <<= i;
  63. }
  64. lua_pushunsigned(L, r);
  65. return 1;
  66. }
  67. static int b_lshift (lua_State *L) {
  68. return b_shift(L, getuintarg(L, 1), luaL_checkint(L, 2));
  69. }
  70. static int b_rshift (lua_State *L) {
  71. return b_shift(L, getuintarg(L, 1), -luaL_checkint(L, 2));
  72. }
  73. static int b_arshift (lua_State *L) {
  74. b_uint r = getuintarg(L, 1);
  75. int i = luaL_checkint(L, 2);
  76. if (i < 0 || !(r & 0x80000000))
  77. return b_shift(L, r, -i);
  78. else { /* arithmetic shift for 'negative' number */
  79. if (i >= NBITS) r = 0xffffffff;
  80. else
  81. r = (r >> i) | ~(~(b_uint)0 >> i); /* add signal bit */
  82. lua_pushunsigned(L, r);
  83. return 1;
  84. }
  85. }
  86. static int b_rot (lua_State *L, int i) {
  87. b_uint r = getuintarg(L, 1);
  88. i &= (NBITS - 1); /* i = i % NBITS */
  89. r = (r << i) | (r >> (NBITS - i));
  90. lua_pushunsigned(L, r);
  91. return 1;
  92. }
  93. static int b_rol (lua_State *L) {
  94. return b_rot(L, luaL_checkint(L, 2));
  95. }
  96. static int b_ror (lua_State *L) {
  97. return b_rot(L, -luaL_checkint(L, 2));
  98. }
  99. static const luaL_Reg bitlib[] = {
  100. {"band", b_and},
  101. {"btest", b_test},
  102. {"bor", b_or},
  103. {"bxor", b_xor},
  104. {"bnot", b_not},
  105. {"lshift", b_lshift},
  106. {"arshift", b_arshift},
  107. {"rshift", b_rshift},
  108. {"rol", b_rol},
  109. {"ror", b_ror},
  110. {NULL, NULL}
  111. };
  112. LUAMOD_API int luaopen_bit32 (lua_State *L) {
  113. luaL_newlib(L, bitlib);
  114. return 1;
  115. }