lbitlib.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. ** $Id: lbitlib.c,v 1.25 2014/03/20 19:22:16 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. #if defined(LUA_COMPAT_BITLIB) /* { */
  12. /* number of bits to consider in a number */
  13. #if !defined(LUA_NBITS)
  14. #define LUA_NBITS 32
  15. #endif
  16. /*
  17. ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must
  18. ** be made in two parts to avoid problems when LUA_NBITS is equal to the
  19. ** number of bits in a lua_Unsigned.)
  20. */
  21. #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
  22. /* macro to trim extra bits */
  23. #define trim(x) ((x) & ALLONES)
  24. /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
  25. #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
  26. static lua_Unsigned andaux (lua_State *L) {
  27. int i, n = lua_gettop(L);
  28. lua_Unsigned r = ~(lua_Unsigned)0;
  29. for (i = 1; i <= n; i++)
  30. r &= luaL_checkunsigned(L, i);
  31. return trim(r);
  32. }
  33. static int b_and (lua_State *L) {
  34. lua_Unsigned r = andaux(L);
  35. lua_pushunsigned(L, r);
  36. return 1;
  37. }
  38. static int b_test (lua_State *L) {
  39. lua_Unsigned r = andaux(L);
  40. lua_pushboolean(L, r != 0);
  41. return 1;
  42. }
  43. static int b_or (lua_State *L) {
  44. int i, n = lua_gettop(L);
  45. lua_Unsigned r = 0;
  46. for (i = 1; i <= n; i++)
  47. r |= luaL_checkunsigned(L, i);
  48. lua_pushunsigned(L, trim(r));
  49. return 1;
  50. }
  51. static int b_xor (lua_State *L) {
  52. int i, n = lua_gettop(L);
  53. lua_Unsigned r = 0;
  54. for (i = 1; i <= n; i++)
  55. r ^= luaL_checkunsigned(L, i);
  56. lua_pushunsigned(L, trim(r));
  57. return 1;
  58. }
  59. static int b_not (lua_State *L) {
  60. lua_Unsigned r = ~luaL_checkunsigned(L, 1);
  61. lua_pushunsigned(L, trim(r));
  62. return 1;
  63. }
  64. static int b_shift (lua_State *L, lua_Unsigned r, int i) {
  65. if (i < 0) { /* shift right? */
  66. i = -i;
  67. r = trim(r);
  68. if (i >= LUA_NBITS) r = 0;
  69. else r >>= i;
  70. }
  71. else { /* shift left */
  72. if (i >= LUA_NBITS) r = 0;
  73. else r <<= i;
  74. r = trim(r);
  75. }
  76. lua_pushunsigned(L, r);
  77. return 1;
  78. }
  79. static int b_lshift (lua_State *L) {
  80. return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
  81. }
  82. static int b_rshift (lua_State *L) {
  83. return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
  84. }
  85. static int b_arshift (lua_State *L) {
  86. lua_Unsigned r = luaL_checkunsigned(L, 1);
  87. int i = luaL_checkint(L, 2);
  88. if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
  89. return b_shift(L, r, -i);
  90. else { /* arithmetic shift for 'negative' number */
  91. if (i >= LUA_NBITS) r = ALLONES;
  92. else
  93. r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */
  94. lua_pushunsigned(L, r);
  95. return 1;
  96. }
  97. }
  98. static int b_rot (lua_State *L, int i) {
  99. lua_Unsigned r = luaL_checkunsigned(L, 1);
  100. i &= (LUA_NBITS - 1); /* i = i % NBITS */
  101. r = trim(r);
  102. if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
  103. r = (r << i) | (r >> (LUA_NBITS - i));
  104. lua_pushunsigned(L, trim(r));
  105. return 1;
  106. }
  107. static int b_lrot (lua_State *L) {
  108. return b_rot(L, luaL_checkint(L, 2));
  109. }
  110. static int b_rrot (lua_State *L) {
  111. return b_rot(L, -luaL_checkint(L, 2));
  112. }
  113. /*
  114. ** get field and width arguments for field-manipulation functions,
  115. ** checking whether they are valid.
  116. ** ('luaL_error' called without 'return' to avoid later warnings about
  117. ** 'width' being used uninitialized.)
  118. */
  119. static int fieldargs (lua_State *L, int farg, int *width) {
  120. int f = luaL_checkint(L, farg);
  121. int w = luaL_optint(L, farg + 1, 1);
  122. luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
  123. luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
  124. if (f + w > LUA_NBITS)
  125. luaL_error(L, "trying to access non-existent bits");
  126. *width = w;
  127. return f;
  128. }
  129. static int b_extract (lua_State *L) {
  130. int w;
  131. lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
  132. int f = fieldargs(L, 2, &w);
  133. r = (r >> f) & mask(w);
  134. lua_pushunsigned(L, r);
  135. return 1;
  136. }
  137. static int b_replace (lua_State *L) {
  138. int w;
  139. lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
  140. lua_Unsigned v = luaL_checkunsigned(L, 2);
  141. int f = fieldargs(L, 3, &w);
  142. int m = mask(w);
  143. v &= m; /* erase bits outside given width */
  144. r = (r & ~(m << f)) | (v << f);
  145. lua_pushunsigned(L, r);
  146. return 1;
  147. }
  148. static const luaL_Reg bitlib[] = {
  149. {"arshift", b_arshift},
  150. {"band", b_and},
  151. {"bnot", b_not},
  152. {"bor", b_or},
  153. {"bxor", b_xor},
  154. {"btest", b_test},
  155. {"extract", b_extract},
  156. {"lrotate", b_lrot},
  157. {"lshift", b_lshift},
  158. {"replace", b_replace},
  159. {"rrotate", b_rrot},
  160. {"rshift", b_rshift},
  161. {NULL, NULL}
  162. };
  163. LUAMOD_API int luaopen_bit32 (lua_State *L) {
  164. luaL_newlib(L, bitlib);
  165. return 1;
  166. }
  167. #else /* }{ */
  168. LUAMOD_API int luaopen_bit32 (lua_State *L) {
  169. return luaL_error(L, "library 'bit32' has been deprecated");
  170. }
  171. #endif /* } */