2 ** $Id: lbitlib.c,v 1.2 2015/02/02 14:03:05 lneto Exp $
3 ** Standard library for bitwise operations
4 ** See Copyright Notice in lua.h
19 #if defined(LUA_COMPAT_BITLIB) /* { */
22 /* number of bits to consider in a number */
23 #if !defined(LUA_NBITS)
29 ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must
30 ** be made in two parts to avoid problems when LUA_NBITS is equal to the
31 ** number of bits in a lua_Unsigned.)
33 #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
36 /* macro to trim extra bits */
37 #define trim(x) ((x) & ALLONES)
40 /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
41 #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
45 static lua_Unsigned
andaux (lua_State
*L
) {
46 int i
, n
= lua_gettop(L
);
47 lua_Unsigned r
= ~(lua_Unsigned
)0;
48 for (i
= 1; i
<= n
; i
++)
49 r
&= luaL_checkunsigned(L
, i
);
54 static int b_and (lua_State
*L
) {
55 lua_Unsigned r
= andaux(L
);
56 lua_pushunsigned(L
, r
);
61 static int b_test (lua_State
*L
) {
62 lua_Unsigned r
= andaux(L
);
63 lua_pushboolean(L
, r
!= 0);
68 static int b_or (lua_State
*L
) {
69 int i
, n
= lua_gettop(L
);
71 for (i
= 1; i
<= n
; i
++)
72 r
|= luaL_checkunsigned(L
, i
);
73 lua_pushunsigned(L
, trim(r
));
78 static int b_xor (lua_State
*L
) {
79 int i
, n
= lua_gettop(L
);
81 for (i
= 1; i
<= n
; i
++)
82 r
^= luaL_checkunsigned(L
, i
);
83 lua_pushunsigned(L
, trim(r
));
88 static int b_not (lua_State
*L
) {
89 lua_Unsigned r
= ~luaL_checkunsigned(L
, 1);
90 lua_pushunsigned(L
, trim(r
));
95 static int b_shift (lua_State
*L
, lua_Unsigned r
, lua_Integer i
) {
96 if (i
< 0) { /* shift right? */
99 if (i
>= LUA_NBITS
) r
= 0;
102 else { /* shift left */
103 if (i
>= LUA_NBITS
) r
= 0;
107 lua_pushunsigned(L
, r
);
112 static int b_lshift (lua_State
*L
) {
113 return b_shift(L
, luaL_checkunsigned(L
, 1), luaL_checkinteger(L
, 2));
117 static int b_rshift (lua_State
*L
) {
118 return b_shift(L
, luaL_checkunsigned(L
, 1), -luaL_checkinteger(L
, 2));
122 static int b_arshift (lua_State
*L
) {
123 lua_Unsigned r
= luaL_checkunsigned(L
, 1);
124 lua_Integer i
= luaL_checkinteger(L
, 2);
125 if (i
< 0 || !(r
& ((lua_Unsigned
)1 << (LUA_NBITS
- 1))))
126 return b_shift(L
, r
, -i
);
127 else { /* arithmetic shift for 'negative' number */
128 if (i
>= LUA_NBITS
) r
= ALLONES
;
130 r
= trim((r
>> i
) | ~(trim(~(lua_Unsigned
)0) >> i
)); /* add signal bit */
131 lua_pushunsigned(L
, r
);
137 static int b_rot (lua_State
*L
, lua_Integer d
) {
138 lua_Unsigned r
= luaL_checkunsigned(L
, 1);
139 int i
= d
& (LUA_NBITS
- 1); /* i = d % NBITS */
141 if (i
!= 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
142 r
= (r
<< i
) | (r
>> (LUA_NBITS
- i
));
143 lua_pushunsigned(L
, trim(r
));
148 static int b_lrot (lua_State
*L
) {
149 return b_rot(L
, luaL_checkinteger(L
, 2));
153 static int b_rrot (lua_State
*L
) {
154 return b_rot(L
, -luaL_checkinteger(L
, 2));
159 ** get field and width arguments for field-manipulation functions,
160 ** checking whether they are valid.
161 ** ('luaL_error' called without 'return' to avoid later warnings about
162 ** 'width' being used uninitialized.)
164 static int fieldargs (lua_State
*L
, int farg
, int *width
) {
165 lua_Integer f
= luaL_checkinteger(L
, farg
);
166 lua_Integer w
= luaL_optinteger(L
, farg
+ 1, 1);
167 luaL_argcheck(L
, 0 <= f
, farg
, "field cannot be negative");
168 luaL_argcheck(L
, 0 < w
, farg
+ 1, "width must be positive");
169 if (f
+ w
> LUA_NBITS
)
170 luaL_error(L
, "trying to access non-existent bits");
176 static int b_extract (lua_State
*L
) {
178 lua_Unsigned r
= trim(luaL_checkunsigned(L
, 1));
179 int f
= fieldargs(L
, 2, &w
);
180 r
= (r
>> f
) & mask(w
);
181 lua_pushunsigned(L
, r
);
186 static int b_replace (lua_State
*L
) {
188 lua_Unsigned r
= trim(luaL_checkunsigned(L
, 1));
189 lua_Unsigned v
= luaL_checkunsigned(L
, 2);
190 int f
= fieldargs(L
, 3, &w
);
192 v
&= m
; /* erase bits outside given width */
193 r
= (r
& ~(m
<< f
)) | (v
<< f
);
194 lua_pushunsigned(L
, r
);
199 static const luaL_Reg bitlib
[] = {
200 {"arshift", b_arshift
},
206 {"extract", b_extract
},
208 {"lshift", b_lshift
},
209 {"replace", b_replace
},
211 {"rshift", b_rshift
},
217 LUAMOD_API
int luaopen_bit32 (lua_State
*L
) {
218 luaL_newlib(L
, bitlib
);
226 LUAMOD_API
int luaopen_bit32 (lua_State
*L
) {
227 return luaL_error(L
, "library 'bit32' has been deprecated");