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
6 ** Patched to provide a compatibility layer with Lua5.3+ where
7 ** bit32 library was removed.
10 #include "lua_bitlib.h"
14 /* number of bits to consider in a number */
15 #define LUA_BITLIB_NBITS 32
18 #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_BITLIB_NBITS - 1)) << 1))
20 /* macro to trim extra bits */
21 #define trim(x) ((x) & ALLONES)
24 /* builds a number with 'n' ones (1 <= n <= LUA_BITLIB_NBITS) */
25 #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
28 typedef lua_Unsigned b_uint
;
31 static b_uint
andaux(lua_State
*L
) {
32 int i
, n
= lua_gettop(L
);
33 b_uint r
= ~(b_uint
)0;
34 for (i
= 1; i
<= n
; i
++)
35 r
&= (b_uint
)luaL_checkinteger(L
, i
);
40 static int b_and(lua_State
*L
) {
42 lua_pushinteger(L
, r
);
47 static int b_test(lua_State
*L
) {
49 lua_pushboolean(L
, r
!= 0);
54 static int b_or(lua_State
*L
) {
55 int i
, n
= lua_gettop(L
);
57 for (i
= 1; i
<= n
; i
++)
58 r
|= (b_uint
) luaL_checkinteger(L
, i
);
59 lua_pushinteger(L
, trim(r
));
64 static int b_xor(lua_State
*L
) {
65 int i
, n
= lua_gettop(L
);
67 for (i
= 1; i
<= n
; i
++)
68 r
^= (b_uint
) luaL_checkinteger(L
, i
);
69 lua_pushinteger(L
, trim(r
));
74 static int b_not(lua_State
*L
) {
75 b_uint r
= ~((b_uint
)luaL_checkinteger(L
, 1));
76 lua_pushinteger(L
, trim(r
));
81 static int b_shift(lua_State
*L
, b_uint r
, int i
) {
82 if (i
< 0) { /* shift right? */
85 if (i
>= LUA_BITLIB_NBITS
) r
= 0;
87 } else { /* shift left */
88 if (i
>= LUA_BITLIB_NBITS
) r
= 0;
92 lua_pushinteger(L
, r
);
97 static int b_lshift(lua_State
*L
) {
98 return b_shift(L
, (b_uint
)luaL_checkinteger(L
, 1), luaL_checkinteger(L
, 2));
102 static int b_rshift(lua_State
*L
) {
103 return b_shift(L
, (b_uint
)luaL_checkinteger(L
, 1), -luaL_checkinteger(L
, 2));
107 static int b_arshift(lua_State
*L
) {
108 b_uint r
= (b_uint
)luaL_checkinteger(L
, 1);
109 int i
= luaL_checkinteger(L
, 2);
110 if (i
< 0 || !(r
& ((b_uint
)1 << (LUA_BITLIB_NBITS
- 1))))
111 return b_shift(L
, r
, -i
);
112 else { /* arithmetic shift for 'negative' number */
113 if (i
>= LUA_BITLIB_NBITS
) r
= ALLONES
;
115 r
= trim((r
>> i
) | ~(~(b_uint
)0 >> i
)); /* add signal bit */
116 lua_pushinteger(L
, r
);
122 static int b_rot(lua_State
*L
, int i
) {
123 b_uint r
= (b_uint
)luaL_checkinteger(L
, 1);
124 i
&= (LUA_BITLIB_NBITS
- 1); /* i = i % NBITS */
126 if (i
!= 0) /* avoid undefined shift of LUA_BITLIB_NBITS when i == 0 */
127 r
= (r
<< i
) | (r
>> (LUA_BITLIB_NBITS
- i
));
129 lua_pushinteger(L
, trim(r
));
134 static int b_lrot(lua_State
*L
) {
135 return b_rot(L
, luaL_checkinteger(L
, 2));
139 static int b_rrot(lua_State
*L
) {
140 return b_rot(L
, -luaL_checkinteger(L
, 2));
145 ** get field and width arguments for field-manipulation functions,
146 ** checking whether they are valid.
147 ** ('luaL_error' called without 'return' to avoid later warnings about
148 ** 'width' being used uninitialized.)
150 static int fieldargs(lua_State
*L
, int farg
, int *width
) {
151 int f
= luaL_checkinteger(L
, farg
);
152 int w
= luaL_optinteger(L
, farg
+ 1, 1);
153 luaL_argcheck(L
, 0 <= f
, farg
, "field cannot be negative");
154 luaL_argcheck(L
, 0 < w
, farg
+ 1, "width must be positive");
155 if (f
+ w
> LUA_BITLIB_NBITS
)
156 luaL_error(L
, "trying to access non-existent bits");
162 static int b_extract(lua_State
*L
) {
164 b_uint r
= (b_uint
)luaL_checkinteger(L
, 1);
165 int f
= fieldargs(L
, 2, &w
);
166 r
= (r
>> f
) & mask(w
);
167 lua_pushinteger(L
, r
);
172 static int b_replace(lua_State
*L
) {
174 b_uint r
= (b_uint
)luaL_checkinteger(L
, 1);
175 b_uint v
= (b_uint
)luaL_checkinteger(L
, 2);
176 int f
= fieldargs(L
, 3, &w
);
178 v
&= m
; /* erase bits outside given width */
179 r
= (r
& ~(m
<< f
)) | (v
<< f
);
180 lua_pushinteger(L
, r
);
185 void register_bit32_lib(lua_State
*L
) {
186 static const luaL_Reg bitlib
[] = {
187 {"arshift", b_arshift
},
193 {"extract", b_extract
},
195 {"lshift", b_lshift
},
196 {"replace", b_replace
},
198 {"rshift", b_rshift
},
202 luaL_newlib(L
, bitlib
);
203 lua_setfield(L
, -2, "bit32");