lbitlib.c 4.5 KB

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