lbitlib.c 4.8 KB

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