]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.3/src/lbitlib.c
2 ** $Id: lbitlib.c,v 1.30 2015/11/11 19:08:09 roberto Exp $
3 ** Standard library for bitwise operations
4 ** See Copyright Notice in lua.h
19 #if defined(LUA_COMPAT_BITLIB) /* { */
22 #define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
23 #define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i))
26 /* number of bits to consider in a number */
27 #if !defined(LUA_NBITS)
33 ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must
34 ** be made in two parts to avoid problems when LUA_NBITS is equal to the
35 ** number of bits in a lua_Unsigned.)
37 #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
40 /* macro to trim extra bits */
41 #define trim(x) ((x) & ALLONES)
44 /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
45 #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
49 static lua_Unsigned
andaux (lua_State
*L
) {
50 int i
, n
= lua_gettop(L
);
51 lua_Unsigned r
= ~(lua_Unsigned
)0;
52 for (i
= 1; i
<= n
; i
++)
53 r
&= checkunsigned(L
, i
);
58 static int b_and (lua_State
*L
) {
59 lua_Unsigned r
= andaux(L
);
65 static int b_test (lua_State
*L
) {
66 lua_Unsigned r
= andaux(L
);
67 lua_pushboolean(L
, r
!= 0);
72 static int b_or (lua_State
*L
) {
73 int i
, n
= lua_gettop(L
);
75 for (i
= 1; i
<= n
; i
++)
76 r
|= checkunsigned(L
, i
);
77 pushunsigned(L
, trim(r
));
82 static int b_xor (lua_State
*L
) {
83 int i
, n
= lua_gettop(L
);
85 for (i
= 1; i
<= n
; i
++)
86 r
^= checkunsigned(L
, i
);
87 pushunsigned(L
, trim(r
));
92 static int b_not (lua_State
*L
) {
93 lua_Unsigned r
= ~checkunsigned(L
, 1);
94 pushunsigned(L
, trim(r
));
99 static int b_shift (lua_State
*L
, lua_Unsigned r
, lua_Integer i
) {
100 if (i
< 0) { /* shift right? */
103 if (i
>= LUA_NBITS
) r
= 0;
106 else { /* shift left */
107 if (i
>= LUA_NBITS
) r
= 0;
116 static int b_lshift (lua_State
*L
) {
117 return b_shift(L
, checkunsigned(L
, 1), luaL_checkinteger(L
, 2));
121 static int b_rshift (lua_State
*L
) {
122 return b_shift(L
, checkunsigned(L
, 1), -luaL_checkinteger(L
, 2));
126 static int b_arshift (lua_State
*L
) {
127 lua_Unsigned r
= checkunsigned(L
, 1);
128 lua_Integer i
= luaL_checkinteger(L
, 2);
129 if (i
< 0 || !(r
& ((lua_Unsigned
)1 << (LUA_NBITS
- 1))))
130 return b_shift(L
, r
, -i
);
131 else { /* arithmetic shift for 'negative' number */
132 if (i
>= LUA_NBITS
) r
= ALLONES
;
134 r
= trim((r
>> i
) | ~(trim(~(lua_Unsigned
)0) >> i
)); /* add signal bit */
141 static int b_rot (lua_State
*L
, lua_Integer d
) {
142 lua_Unsigned r
= checkunsigned(L
, 1);
143 int i
= d
& (LUA_NBITS
- 1); /* i = d % NBITS */
145 if (i
!= 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
146 r
= (r
<< i
) | (r
>> (LUA_NBITS
- i
));
147 pushunsigned(L
, trim(r
));
152 static int b_lrot (lua_State
*L
) {
153 return b_rot(L
, luaL_checkinteger(L
, 2));
157 static int b_rrot (lua_State
*L
) {
158 return b_rot(L
, -luaL_checkinteger(L
, 2));
163 ** get field and width arguments for field-manipulation functions,
164 ** checking whether they are valid.
165 ** ('luaL_error' called without 'return' to avoid later warnings about
166 ** 'width' being used uninitialized.)
168 static int fieldargs (lua_State
*L
, int farg
, int *width
) {
169 lua_Integer f
= luaL_checkinteger(L
, farg
);
170 lua_Integer w
= luaL_optinteger(L
, farg
+ 1, 1);
171 luaL_argcheck(L
, 0 <= f
, farg
, "field cannot be negative");
172 luaL_argcheck(L
, 0 < w
, farg
+ 1, "width must be positive");
173 if (f
+ w
> LUA_NBITS
)
174 luaL_error(L
, "trying to access non-existent bits");
180 static int b_extract (lua_State
*L
) {
182 lua_Unsigned r
= trim(checkunsigned(L
, 1));
183 int f
= fieldargs(L
, 2, &w
);
184 r
= (r
>> f
) & mask(w
);
190 static int b_replace (lua_State
*L
) {
192 lua_Unsigned r
= trim(checkunsigned(L
, 1));
193 lua_Unsigned v
= trim(checkunsigned(L
, 2));
194 int f
= fieldargs(L
, 3, &w
);
195 lua_Unsigned m
= mask(w
);
196 r
= (r
& ~(m
<< f
)) | ((v
& m
) << f
);
202 static const luaL_Reg bitlib
[] = {
203 {"arshift", b_arshift
},
209 {"extract", b_extract
},
211 {"lshift", b_lshift
},
212 {"replace", b_replace
},
214 {"rshift", b_rshift
},
220 LUAMOD_API
int luaopen_bit32 (lua_State
*L
) {
221 luaL_newlib(L
, bitlib
);
229 LUAMOD_API
int luaopen_bit32 (lua_State
*L
) {
230 return luaL_error(L
, "library 'bit32' has been deprecated");