lbitlib.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. ** $Id: lbitlib.c,v 1.16 2011/06/20 16:35:23 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. */
  111. static int fieldargs (lua_State *L, int farg, int *width) {
  112. int f = luaL_checkint(L, farg);
  113. int w = luaL_optint(L, farg + 1, 1);
  114. luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
  115. luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
  116. if (f + w > LUA_NBITS)
  117. luaL_error(L, "trying to access non-existent bits");
  118. *width = w;
  119. return f;
  120. }
  121. static int b_extract (lua_State *L) {
  122. int w;
  123. b_uint r = luaL_checkunsigned(L, 1);
  124. int f = fieldargs(L, 2, &w);
  125. r = (r >> f) & mask(w);
  126. lua_pushunsigned(L, r);
  127. return 1;
  128. }
  129. static int b_replace (lua_State *L) {
  130. int w;
  131. b_uint r = luaL_checkunsigned(L, 1);
  132. b_uint v = luaL_checkunsigned(L, 2);
  133. int f = fieldargs(L, 3, &w);
  134. int m = mask(w);
  135. v &= m; /* erase bits outside given width */
  136. r = (r & ~(m << f)) | (v << f);
  137. lua_pushunsigned(L, r);
  138. return 1;
  139. }
  140. static const luaL_Reg bitlib[] = {
  141. {"arshift", b_arshift},
  142. {"band", b_and},
  143. {"bnot", b_not},
  144. {"bor", b_or},
  145. {"bxor", b_xor},
  146. {"btest", b_test},
  147. {"extract", b_extract},
  148. {"lrotate", b_lrot},
  149. {"lshift", b_lshift},
  150. {"replace", b_replace},
  151. {"rrotate", b_rrot},
  152. {"rshift", b_rshift},
  153. {NULL, NULL}
  154. };
  155. LUAMOD_API int luaopen_bit32 (lua_State *L) {
  156. luaL_newlib(L, bitlib);
  157. return 1;
  158. }