PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/lua/lbitlib.c

https://bitbucket.org/cesbo/astra
C | 212 lines | 147 code | 51 blank | 14 comment | 17 complexity | db303cadee91312e2f365e0b8fe5998e MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. /*
  2. ** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 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. if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
  98. r = (r << i) | (r >> (LUA_NBITS - i));
  99. lua_pushunsigned(L, trim(r));
  100. return 1;
  101. }
  102. static int b_lrot (lua_State *L) {
  103. return b_rot(L, luaL_checkint(L, 2));
  104. }
  105. static int b_rrot (lua_State *L) {
  106. return b_rot(L, -luaL_checkint(L, 2));
  107. }
  108. /*
  109. ** get field and width arguments for field-manipulation functions,
  110. ** checking whether they are valid.
  111. ** ('luaL_error' called without 'return' to avoid later warnings about
  112. ** 'width' being used uninitialized.)
  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. }