renamed 'hf mfdes readdata, writedata' to 'read/write'
[RRG-proxmark3.git] / client / deps / liblua / lbitlib.c
blob090e52b8bea6706ab10e2d01cbf56f463bab87d5
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 */
7 #define lbitlib_c
8 #define LUA_LIB
10 #include "lua.h"
12 #include "lauxlib.h"
13 #include "lualib.h"
16 /* number of bits to consider in a number */
17 #if !defined(LUA_NBITS)
18 #define LUA_NBITS 32
19 #endif
22 #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
24 /* macro to trim extra bits */
25 #define trim(x) ((x) & ALLONES)
28 /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
29 #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
32 typedef lua_Unsigned b_uint;
36 static b_uint andaux(lua_State *L) {
37 int i, n = lua_gettop(L);
38 b_uint r = ~(b_uint)0;
39 for (i = 1; i <= n; i++)
40 r &= luaL_checkunsigned(L, i);
41 return trim(r);
45 static int b_and(lua_State *L) {
46 b_uint r = andaux(L);
47 lua_pushunsigned(L, r);
48 return 1;
52 static int b_test(lua_State *L) {
53 b_uint r = andaux(L);
54 lua_pushboolean(L, r != 0);
55 return 1;
59 static int b_or(lua_State *L) {
60 int i, n = lua_gettop(L);
61 b_uint r = 0;
62 for (i = 1; i <= n; i++)
63 r |= luaL_checkunsigned(L, i);
64 lua_pushunsigned(L, trim(r));
65 return 1;
69 static int b_xor(lua_State *L) {
70 int i, n = lua_gettop(L);
71 b_uint r = 0;
72 for (i = 1; i <= n; i++)
73 r ^= luaL_checkunsigned(L, i);
74 lua_pushunsigned(L, trim(r));
75 return 1;
79 static int b_not(lua_State *L) {
80 b_uint r = ~luaL_checkunsigned(L, 1);
81 lua_pushunsigned(L, trim(r));
82 return 1;
86 static int b_shift(lua_State *L, b_uint r, int i) {
87 if (i < 0) { /* shift right? */
88 i = -i;
89 r = trim(r);
90 if (i >= LUA_NBITS) r = 0;
91 else r >>= i;
92 } else { /* shift left */
93 if (i >= LUA_NBITS) r = 0;
94 else r <<= i;
95 r = trim(r);
97 lua_pushunsigned(L, r);
98 return 1;
102 static int b_lshift(lua_State *L) {
103 return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
107 static int b_rshift(lua_State *L) {
108 return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
112 static int b_arshift(lua_State *L) {
113 b_uint r = luaL_checkunsigned(L, 1);
114 int i = luaL_checkint(L, 2);
115 if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
116 return b_shift(L, r, -i);
117 else { /* arithmetic shift for 'negative' number */
118 if (i >= LUA_NBITS) r = ALLONES;
119 else
120 r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
121 lua_pushunsigned(L, r);
122 return 1;
127 static int b_rot(lua_State *L, int i) {
128 b_uint r = luaL_checkunsigned(L, 1);
129 i &= (LUA_NBITS - 1); /* i = i % NBITS */
130 r = trim(r);
131 r = (r << i) | (r >> (LUA_NBITS - i));
132 lua_pushunsigned(L, trim(r));
133 return 1;
137 static int b_lrot(lua_State *L) {
138 return b_rot(L, luaL_checkint(L, 2));
142 static int b_rrot(lua_State *L) {
143 return b_rot(L, -luaL_checkint(L, 2));
148 ** get field and width arguments for field-manipulation functions,
149 ** checking whether they are valid.
150 ** ('luaL_error' called without 'return' to avoid later warnings about
151 ** 'width' being used uninitialized.)
153 static int fieldargs(lua_State *L, int farg, int *width) {
154 int f = luaL_checkint(L, farg);
155 int w = luaL_optint(L, farg + 1, 1);
156 luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
157 luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
158 if (f + w > LUA_NBITS)
159 luaL_error(L, "trying to access non-existent bits");
160 *width = w;
161 return f;
165 static int b_extract(lua_State *L) {
166 int w;
167 b_uint r = luaL_checkunsigned(L, 1);
168 int f = fieldargs(L, 2, &w);
169 r = (r >> f) & mask(w);
170 lua_pushunsigned(L, r);
171 return 1;
175 static int b_replace(lua_State *L) {
176 int w;
177 b_uint r = luaL_checkunsigned(L, 1);
178 b_uint v = luaL_checkunsigned(L, 2);
179 int f = fieldargs(L, 3, &w);
180 int m = mask(w);
181 v &= m; /* erase bits outside given width */
182 r = (r & ~(m << f)) | (v << f);
183 lua_pushunsigned(L, r);
184 return 1;
188 static const luaL_Reg bitlib[] = {
189 {"arshift", b_arshift},
190 {"band", b_and},
191 {"bnot", b_not},
192 {"bor", b_or},
193 {"bxor", b_xor},
194 {"btest", b_test},
195 {"extract", b_extract},
196 {"lrotate", b_lrot},
197 {"lshift", b_lshift},
198 {"replace", b_replace},
199 {"rrotate", b_rrot},
200 {"rshift", b_rshift},
201 {NULL, NULL}
206 LUAMOD_API int luaopen_bit32(lua_State *L) {
207 luaL_newlib(L, bitlib);
208 return 1;