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
18 /* http://lua-users.org/lists/lua-l/2011-01/msg01039.html */
19 typedef unsigned int lua_Unsigned
;
20 #define LUA_UNSIGNED unsigned LUA_INT32
23 #if !defined(lua_unsigned2number)
24 /* on several machines, coercion from unsigned to double is slow,
25 so it may be worth to avoid */
26 #define lua_unsigned2number(u) \
27 (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)( unsigned int )(u))
30 #if !defined(lua_lock)
31 #define lua_lock(L) ((void) 0)
32 #define lua_unlock(L) ((void) 0)
36 LUA_API
void lua_pushunsigned (lua_State
*L
, lua_Unsigned u
) {
39 /* printf("u=%d,%u,%x\n",u,u,u);
40 printf("INT_MAX=%d %u %x, (lua_Unsigned)INT_MAX=%d %u %x (unsigned int)INT_MAX=%d %u %x\n",
41 INT_MAX,INT_MAX,INT_MAX,
42 (lua_Unsigned)INT_MAX,(lua_Unsigned)INT_MAX,(lua_Unsigned)INT_MAX,
43 (unsigned int)INT_MAX,(unsigned int)INT_MAX,(unsigned int)INT_MAX);
44 printf("((u) <= (lua_Unsigned)INT_MAX) :%d\n",(((unsigned int)u) <= (lua_Unsigned)INT_MAX));
45 printf("(lua_Number)(int)(u) =%d,%u,%x\n",(lua_Number)(int)(u),(lua_Number)(int)(u),(lua_Number)(int)(u));
46 printf("(lua_Number)(u) =%f,%u,%x\n",(lua_Number)(u),(lua_Number)(u),(lua_Number)(u));
47 printf("u=%d,%u,%x\n",u,u,u);
49 /*printf("n=%d,%u,%x u=%d,%u,%x\n",n,n,n,u,u,u); */
50 n
= lua_unsigned2number(u
);
51 /* printf("n=%f \n",n);
52 printf("u=%d %u %x n=%d %u %x\n",u,u,u,n,n,n);
53 printf("n=%d %u %x\n",n,n,n);
54 printf("u=%d,%u,%x\n",u,u,u);
56 /* setintptrV(L->top, n);
61 if (LJ_UNLIKELY(tvisnan(L
->top
)))
62 setnanV(L
->top
); /* Canonicalize injected NaNs. */
64 /*setnvalue(L->top, n);
75 #define luaL_checkunsigned (lua_Unsigned) luaL_checkinteger
76 /*#define lua_pushunsigned lua_pushinteger*/
80 /* number of bits to consider in a number */
81 #if !defined(LUA_NBITS)
86 #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
88 /* macro to trim extra bits */
89 #define trim(x) ((x) & ALLONES)
92 /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
93 #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
96 typedef lua_Unsigned b_uint
;
100 static b_uint
andaux (lua_State
*L
) {
101 int i
, n
= lua_gettop(L
);
102 b_uint r
= ~(b_uint
)0;
103 /* printf("1 r=%d,%u,%x\n",r,r,r);*/
104 for (i
= 1; i
<= n
; i
++)
105 r
&= luaL_checkunsigned(L
, i
);
106 /* printf("2 r=%d,%u,%x\n",r,r,r);
107 printf("3 r=%d,%u,%x\n",trim(r),trim(r),trim(r));
113 static int b_and (lua_State
*L
) {
114 b_uint r
= andaux(L
);
115 /* printf("4 r=%d,%u,%x\n",r,r,r);*/
116 lua_pushunsigned(L
, r
);
121 static int b_test (lua_State
*L
) {
122 b_uint r
= andaux(L
);
123 lua_pushboolean(L
, r
!= 0);
128 static int b_or (lua_State
*L
) {
129 int i
, n
= lua_gettop(L
);
131 for (i
= 1; i
<= n
; i
++)
132 r
|= luaL_checkunsigned(L
, i
);
133 lua_pushunsigned(L
, trim(r
));
138 static int b_xor (lua_State
*L
) {
139 int i
, n
= lua_gettop(L
);
141 for (i
= 1; i
<= n
; i
++)
142 r
^= luaL_checkunsigned(L
, i
);
143 lua_pushunsigned(L
, trim(r
));
148 static int b_not (lua_State
*L
) {
149 b_uint r
= ~luaL_checkunsigned(L
, 1);
150 lua_pushunsigned(L
, trim(r
));
155 static int b_shift (lua_State
*L
, b_uint r
, int i
) {
156 if (i
< 0) { /* shift right? */
159 if (i
>= LUA_NBITS
) r
= 0;
162 else { /* shift left */
163 if (i
>= LUA_NBITS
) r
= 0;
167 lua_pushunsigned(L
, r
);
172 static int b_lshift (lua_State
*L
) {
173 return b_shift(L
, luaL_checkunsigned(L
, 1), luaL_checkint(L
, 2));
177 static int b_rshift (lua_State
*L
) {
178 return b_shift(L
, luaL_checkunsigned(L
, 1), -luaL_checkint(L
, 2));
182 static int b_arshift (lua_State
*L
) {
183 b_uint r
= luaL_checkunsigned(L
, 1);
184 int i
= luaL_checkint(L
, 2);
185 if (i
< 0 || !(r
& ((b_uint
)1 << (LUA_NBITS
- 1))))
186 return b_shift(L
, r
, -i
);
187 else { /* arithmetic shift for 'negative' number */
188 if (i
>= LUA_NBITS
) r
= ALLONES
;
190 r
= trim((r
>> i
) | ~(~(b_uint
)0 >> i
)); /* add signal bit */
191 lua_pushunsigned(L
, r
);
197 static int b_rot (lua_State
*L
, int i
) {
198 b_uint r
= luaL_checkunsigned(L
, 1);
199 i
&= (LUA_NBITS
- 1); /* i = i % NBITS */
201 r
= (r
<< i
) | (r
>> (LUA_NBITS
- i
));
202 lua_pushunsigned(L
, trim(r
));
207 static int b_lrot (lua_State
*L
) {
208 return b_rot(L
, luaL_checkint(L
, 2));
212 static int b_rrot (lua_State
*L
) {
213 return b_rot(L
, -luaL_checkint(L
, 2));
218 ** get field and width arguments for field-manipulation functions,
219 ** checking whether they are valid
221 static int fieldargs (lua_State
*L
, int farg
, int *width
) {
222 int f
= luaL_checkint(L
, farg
);
223 int w
= luaL_optint(L
, farg
+ 1, 1);
224 luaL_argcheck(L
, 0 <= f
, farg
, "field cannot be negative");
225 luaL_argcheck(L
, 0 < w
, farg
+ 1, "width must be positive");
226 if (f
+ w
> LUA_NBITS
)
227 luaL_error(L
, "trying to access non-existent bits");
233 static int b_extract (lua_State
*L
) {
235 b_uint r
= luaL_checkunsigned(L
, 1);
236 int f
= fieldargs(L
, 2, &w
);
237 r
= (r
>> f
) & mask(w
);
238 lua_pushunsigned(L
, r
);
243 static int b_replace (lua_State
*L
) {
245 b_uint r
= luaL_checkunsigned(L
, 1);
246 b_uint v
= luaL_checkunsigned(L
, 2);
247 int f
= fieldargs(L
, 3, &w
);
249 v
&= m
; /* erase bits outside given width */
250 r
= (r
& ~(m
<< f
)) | (v
<< f
);
251 lua_pushunsigned(L
, r
);
256 static const luaL_Reg bitlib
[] = {
257 {"arshift", b_arshift
},
263 {"extract", b_extract
},
265 {"lshift", b_lshift
},
266 {"replace", b_replace
},
268 {"rshift", b_rshift
},
274 /* http://lua-users.org/lists/lua-l/2011-01/msg01039.html */
276 /*LUAMOD_API int luaopen_bit32 (lua_State *L) {*/
277 /* luaL_newlib(L, bitlib);*/
281 int luaopen_bit32 (lua_State
*L
) {
282 luaL_register(L
, "bit32", bitlib
);