mirror of
https://github.com/lua/lua
synced 2025-01-06 01:22:06 +03:00
no need for type 'b_uint' (lua_Unsigned must have at least 32 bits)
This commit is contained in:
parent
48735da0d0
commit
5fa680d47f
37
lbitlib.c
37
lbitlib.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lbitlib.c,v 1.19 2013/04/16 18:39:37 roberto Exp roberto $
|
||||
** $Id: lbitlib.c,v 1.20 2013/06/21 17:27:24 roberto Exp roberto $
|
||||
** Standard library for bitwise operations
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -19,11 +19,8 @@
|
||||
#endif
|
||||
|
||||
|
||||
/* type with (at least) LUA_NBITS bits */
|
||||
typedef unsigned long b_uint;
|
||||
|
||||
|
||||
#define ALLONES (~(((~(b_uint)0) << (LUA_NBITS - 1)) << 1))
|
||||
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
|
||||
|
||||
|
||||
/* macro to trim extra bits */
|
||||
@ -35,9 +32,9 @@ typedef unsigned long b_uint;
|
||||
|
||||
|
||||
|
||||
static b_uint andaux (lua_State *L) {
|
||||
static lua_Unsigned andaux (lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = ~(b_uint)0;
|
||||
lua_Unsigned r = ~(lua_Unsigned)0;
|
||||
for (i = 1; i <= n; i++)
|
||||
r &= luaL_checkunsigned(L, i);
|
||||
return trim(r);
|
||||
@ -45,14 +42,14 @@ static b_uint andaux (lua_State *L) {
|
||||
|
||||
|
||||
static int b_and (lua_State *L) {
|
||||
b_uint r = andaux(L);
|
||||
lua_Unsigned r = andaux(L);
|
||||
lua_pushunsigned(L, r);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_test (lua_State *L) {
|
||||
b_uint r = andaux(L);
|
||||
lua_Unsigned r = andaux(L);
|
||||
lua_pushboolean(L, r != 0);
|
||||
return 1;
|
||||
}
|
||||
@ -60,7 +57,7 @@ static int b_test (lua_State *L) {
|
||||
|
||||
static int b_or (lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = 0;
|
||||
lua_Unsigned r = 0;
|
||||
for (i = 1; i <= n; i++)
|
||||
r |= luaL_checkunsigned(L, i);
|
||||
lua_pushunsigned(L, trim(r));
|
||||
@ -70,7 +67,7 @@ static int b_or (lua_State *L) {
|
||||
|
||||
static int b_xor (lua_State *L) {
|
||||
int i, n = lua_gettop(L);
|
||||
b_uint r = 0;
|
||||
lua_Unsigned r = 0;
|
||||
for (i = 1; i <= n; i++)
|
||||
r ^= luaL_checkunsigned(L, i);
|
||||
lua_pushunsigned(L, trim(r));
|
||||
@ -79,13 +76,13 @@ static int b_xor (lua_State *L) {
|
||||
|
||||
|
||||
static int b_not (lua_State *L) {
|
||||
b_uint r = ~luaL_checkunsigned(L, 1);
|
||||
lua_Unsigned r = ~luaL_checkunsigned(L, 1);
|
||||
lua_pushunsigned(L, trim(r));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int b_shift (lua_State *L, b_uint r, int i) {
|
||||
static int b_shift (lua_State *L, lua_Unsigned r, int i) {
|
||||
if (i < 0) { /* shift right? */
|
||||
i = -i;
|
||||
r = trim(r);
|
||||
@ -113,14 +110,14 @@ static int b_rshift (lua_State *L) {
|
||||
|
||||
|
||||
static int b_arshift (lua_State *L) {
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
lua_Unsigned r = luaL_checkunsigned(L, 1);
|
||||
int i = luaL_checkint(L, 2);
|
||||
if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
|
||||
if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
|
||||
return b_shift(L, r, -i);
|
||||
else { /* arithmetic shift for 'negative' number */
|
||||
if (i >= LUA_NBITS) r = ALLONES;
|
||||
else
|
||||
r = trim((r >> i) | ~(trim(~(b_uint)0) >> i)); /* add signal bit */
|
||||
r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */
|
||||
lua_pushunsigned(L, r);
|
||||
return 1;
|
||||
}
|
||||
@ -128,7 +125,7 @@ static int b_arshift (lua_State *L) {
|
||||
|
||||
|
||||
static int b_rot (lua_State *L, int i) {
|
||||
b_uint r = luaL_checkunsigned(L, 1);
|
||||
lua_Unsigned r = luaL_checkunsigned(L, 1);
|
||||
i &= (LUA_NBITS - 1); /* i = i % NBITS */
|
||||
r = trim(r);
|
||||
r = (r << i) | (r >> (LUA_NBITS - i));
|
||||
@ -167,7 +164,7 @@ static int fieldargs (lua_State *L, int farg, int *width) {
|
||||
|
||||
static int b_extract (lua_State *L) {
|
||||
int w;
|
||||
b_uint r = trim(luaL_checkunsigned(L, 1));
|
||||
lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
|
||||
int f = fieldargs(L, 2, &w);
|
||||
r = (r >> f) & mask(w);
|
||||
lua_pushunsigned(L, r);
|
||||
@ -177,8 +174,8 @@ static int b_extract (lua_State *L) {
|
||||
|
||||
static int b_replace (lua_State *L) {
|
||||
int w;
|
||||
b_uint r = trim(luaL_checkunsigned(L, 1));
|
||||
b_uint v = luaL_checkunsigned(L, 2);
|
||||
lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
|
||||
lua_Unsigned v = luaL_checkunsigned(L, 2);
|
||||
int f = fieldargs(L, 3, &w);
|
||||
int m = mask(w);
|
||||
v &= m; /* erase bits outside given width */
|
||||
|
Loading…
Reference in New Issue
Block a user