lbitlib.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. ** $Id: lbitlib.c,v 1.18 2013/03/19 13:19:12 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 "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. typedef lua_Unsigned b_uint;
  21. static b_uint andaux (lua_State *L) {
  22. int i, n = lua_gettop(L);
  23. b_uint r = ~(b_uint)0;
  24. for (i = 1; i <= n; i++)
  25. r &= luaL_checkunsigned(L, i);
  26. return trim(r);
  27. }
  28. static int b_and (lua_State *L) {
  29. b_uint r = andaux(L);
  30. lua_pushunsigned(L, r);
  31. return 1;
  32. }
  33. static int b_test (lua_State *L) {
  34. b_uint r = andaux(L);
  35. lua_pushboolean(L, r != 0);
  36. return 1;
  37. }
  38. static int b_or (lua_State *L) {
  39. int i, n = lua_gettop(L);
  40. b_uint r = 0;
  41. for (i = 1; i <= n; i++)
  42. r |= luaL_checkunsigned(L, i);
  43. lua_pushunsigned(L, trim(r));
  44. return 1;
  45. }
  46. static int b_xor (lua_State *L) {
  47. int i, n = lua_gettop(L);
  48. b_uint r = 0;
  49. for (i = 1; i <= n; i++)
  50. r ^= luaL_checkunsigned(L, i);
  51. lua_pushunsigned(L, trim(r));
  52. return 1;
  53. }
  54. static int b_not (lua_State *L) {
  55. b_uint r = ~luaL_checkunsigned(L, 1);
  56. lua_pushunsigned(L, trim(r));
  57. return 1;
  58. }
  59. static int b_shift (lua_State *L, b_uint r, int i) {
  60. if (i < 0) { /* shift right? */
  61. i = -i;
  62. r = trim(r);
  63. if (i >= LUA_NBITS) r = 0;
  64. else r >>= i;
  65. }
  66. else { /* shift left */
  67. if (i >= LUA_NBITS) r = 0;
  68. else r <<= i;
  69. r = trim(r);
  70. }
  71. lua_pushunsigned(L, r);
  72. return 1;
  73. }
  74. static int b_lshift (lua_State *L) {
  75. return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
  76. }
  77. static int b_rshift (lua_State *L) {
  78. return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
  79. }
  80. static int b_arshift (lua_State *L) {
  81. b_uint r = luaL_checkunsigned(L, 1);
  82. int i = luaL_checkint(L, 2);
  83. if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
  84. return b_shift(L, r, -i);
  85. else { /* arithmetic shift for 'negative' number */
  86. if (i >= LUA_NBITS) r = ALLONES;
  87. else
  88. r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
  89. lua_pushunsigned(L, r);
  90. return 1;
  91. }
  92. }
  93. static int b_rot (lua_State *L, int i) {
  94. b_uint r = luaL_checkunsigned(L, 1);
  95. i &= (LUA_NBITS - 1); /* i = i % NBITS */
  96. r = trim(r);
  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. b_uint r = 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. b_uint r = luaL_checkunsigned(L, 1);
  134. b_uint 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. }