diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-16 15:39:37 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-16 15:39:37 -0300 |
commit | 44358e0206c3ec034a6a06e7ad4dc5e254874201 (patch) | |
tree | b788ec40ad25d4a6722ff5b30812a383a5cd1721 | |
parent | 07f861385eaa0073af98261ae2919ad9bbce813c (diff) | |
download | lua-44358e0206c3ec034a6a06e7ad4dc5e254874201.tar.gz lua-44358e0206c3ec034a6a06e7ad4dc5e254874201.tar.bz2 lua-44358e0206c3ec034a6a06e7ad4dc5e254874201.zip |
extra trim applied to 'replace' argument (in case lua_Unsigned is
larger than 32 bits)
-rw-r--r-- | lbitlib.c | 14 |
1 files changed, 8 insertions, 6 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbitlib.c,v 1.17 2013/02/21 13:44:53 roberto Exp roberto $ | 2 | ** $Id: lbitlib.c,v 1.18 2013/03/19 13:19:12 roberto Exp roberto $ |
3 | ** Standard library for bitwise operations | 3 | ** Standard library for bitwise operations |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -19,7 +19,12 @@ | |||
19 | #endif | 19 | #endif |
20 | 20 | ||
21 | 21 | ||
22 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) | 22 | /* type with (at least) LUA_NBITS bits */ |
23 | typedef unsigned long b_uint; | ||
24 | |||
25 | |||
26 | #define ALLONES (~(((~(b_uint)0) << (LUA_NBITS - 1)) << 1)) | ||
27 | |||
23 | 28 | ||
24 | /* macro to trim extra bits */ | 29 | /* macro to trim extra bits */ |
25 | #define trim(x) ((x) & ALLONES) | 30 | #define trim(x) ((x) & ALLONES) |
@@ -29,9 +34,6 @@ | |||
29 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) | 34 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) |
30 | 35 | ||
31 | 36 | ||
32 | typedef lua_Unsigned b_uint; | ||
33 | |||
34 | |||
35 | 37 | ||
36 | static b_uint andaux (lua_State *L) { | 38 | static b_uint andaux (lua_State *L) { |
37 | int i, n = lua_gettop(L); | 39 | int i, n = lua_gettop(L); |
@@ -165,7 +167,7 @@ static int fieldargs (lua_State *L, int farg, int *width) { | |||
165 | 167 | ||
166 | static int b_extract (lua_State *L) { | 168 | static int b_extract (lua_State *L) { |
167 | int w; | 169 | int w; |
168 | b_uint r = luaL_checkunsigned(L, 1); | 170 | b_uint r = trim(luaL_checkunsigned(L, 1)); |
169 | int f = fieldargs(L, 2, &w); | 171 | int f = fieldargs(L, 2, &w); |
170 | r = (r >> f) & mask(w); | 172 | r = (r >> f) & mask(w); |
171 | lua_pushunsigned(L, r); | 173 | lua_pushunsigned(L, r); |