2
0

lbitlib.c 4.8 KB

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