Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

214 řádky
4.2 KiB

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