summaryrefslogtreecommitdiff
path: root/src/lbitlib.c
diff options
context:
space:
mode:
authorLua Team <team@lua.org>2013-07-06 12:00:00 +0000
committerrepogen <>2013-07-06 12:00:00 +0000
commit87cc247b6b22184fba47184c218a642ea7a49e96 (patch)
tree299ba8b72b95aa32336b5c810b7133f8efc4fb29 /src/lbitlib.c
parentdc27609467d2699ac9252e89d632432ac5f798f2 (diff)
downloadlua-github-5.3.0-work1.tar.gz
Lua 5.3.0-work15.3.0-work1
Diffstat (limited to 'src/lbitlib.c')
-rw-r--r--src/lbitlib.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/lbitlib.c b/src/lbitlib.c
index 9637532e..d6d436bc 100644
--- a/src/lbitlib.c
+++ b/src/lbitlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lbitlib.c,v 1.18 2013/03/19 13:19:12 roberto Exp $
+** $Id: lbitlib.c,v 1.20 2013/06/21 17:27:24 roberto Exp $
** Standard library for bitwise operations
** See Copyright Notice in lua.h
*/
@@ -19,7 +19,12 @@
#endif
-#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
+/* type with (at least) LUA_NBITS bits */
+typedef unsigned long b_uint;
+
+
+#define ALLONES (~(((~(b_uint)0) << (LUA_NBITS - 1)) << 1))
+
/* macro to trim extra bits */
#define trim(x) ((x) & ALLONES)
@@ -29,9 +34,6 @@
#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
-typedef lua_Unsigned b_uint;
-
-
static b_uint andaux (lua_State *L) {
int i, n = lua_gettop(L);
@@ -118,7 +120,7 @@ static int b_arshift (lua_State *L) {
else { /* arithmetic shift for 'negative' number */
if (i >= LUA_NBITS) r = ALLONES;
else
- r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
+ r = trim((r >> i) | ~(trim(~(b_uint)0) >> i)); /* add signal bit */
lua_pushunsigned(L, r);
return 1;
}
@@ -165,7 +167,7 @@ static int fieldargs (lua_State *L, int farg, int *width) {
static int b_extract (lua_State *L) {
int w;
- b_uint r = luaL_checkunsigned(L, 1);
+ b_uint r = trim(luaL_checkunsigned(L, 1));
int f = fieldargs(L, 2, &w);
r = (r >> f) & mask(w);
lua_pushunsigned(L, r);
@@ -175,7 +177,7 @@ static int b_extract (lua_State *L) {
static int b_replace (lua_State *L) {
int w;
- b_uint r = luaL_checkunsigned(L, 1);
+ b_uint r = trim(luaL_checkunsigned(L, 1));
b_uint v = luaL_checkunsigned(L, 2);
int f = fieldargs(L, 3, &w);
int m = mask(w);