lbitlib.c 4.4 KB

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