lbitlib.c 4.0 KB

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