diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-07-09 14:49:50 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-07-09 14:49:50 -0300 |
| commit | 5fa680d47f36d2f54d436d47beee2cb7dcf986cb (patch) | |
| tree | b0c4f8846eb7378140995e9855710d7ccf0d5369 | |
| parent | 48735da0d035d0a24f67b499ae888b824993c0fe (diff) | |
| download | lua-5fa680d47f36d2f54d436d47beee2cb7dcf986cb.tar.gz lua-5fa680d47f36d2f54d436d47beee2cb7dcf986cb.tar.bz2 lua-5fa680d47f36d2f54d436d47beee2cb7dcf986cb.zip | |
no need for type 'b_uint' (lua_Unsigned must have at least 32 bits)
| -rw-r--r-- | lbitlib.c | 37 |
1 files changed, 17 insertions, 20 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbitlib.c,v 1.19 2013/04/16 18:39:37 roberto Exp roberto $ | 2 | ** $Id: lbitlib.c,v 1.20 2013/06/21 17:27:24 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,11 +19,8 @@ | |||
| 19 | #endif | 19 | #endif |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | /* type with (at least) LUA_NBITS bits */ | ||
| 23 | typedef unsigned long b_uint; | ||
| 24 | 22 | ||
| 25 | 23 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) | |
| 26 | #define ALLONES (~(((~(b_uint)0) << (LUA_NBITS - 1)) << 1)) | ||
| 27 | 24 | ||
| 28 | 25 | ||
| 29 | /* macro to trim extra bits */ | 26 | /* macro to trim extra bits */ |
| @@ -35,9 +32,9 @@ typedef unsigned long b_uint; | |||
| 35 | 32 | ||
| 36 | 33 | ||
| 37 | 34 | ||
| 38 | static b_uint andaux (lua_State *L) { | 35 | static lua_Unsigned andaux (lua_State *L) { |
| 39 | int i, n = lua_gettop(L); | 36 | int i, n = lua_gettop(L); |
| 40 | b_uint r = ~(b_uint)0; | 37 | lua_Unsigned r = ~(lua_Unsigned)0; |
| 41 | for (i = 1; i <= n; i++) | 38 | for (i = 1; i <= n; i++) |
| 42 | r &= luaL_checkunsigned(L, i); | 39 | r &= luaL_checkunsigned(L, i); |
| 43 | return trim(r); | 40 | return trim(r); |
| @@ -45,14 +42,14 @@ static b_uint andaux (lua_State *L) { | |||
| 45 | 42 | ||
| 46 | 43 | ||
| 47 | static int b_and (lua_State *L) { | 44 | static int b_and (lua_State *L) { |
| 48 | b_uint r = andaux(L); | 45 | lua_Unsigned r = andaux(L); |
| 49 | lua_pushunsigned(L, r); | 46 | lua_pushunsigned(L, r); |
| 50 | return 1; | 47 | return 1; |
| 51 | } | 48 | } |
| 52 | 49 | ||
| 53 | 50 | ||
| 54 | static int b_test (lua_State *L) { | 51 | static int b_test (lua_State *L) { |
| 55 | b_uint r = andaux(L); | 52 | lua_Unsigned r = andaux(L); |
| 56 | lua_pushboolean(L, r != 0); | 53 | lua_pushboolean(L, r != 0); |
| 57 | return 1; | 54 | return 1; |
| 58 | } | 55 | } |
| @@ -60,7 +57,7 @@ static int b_test (lua_State *L) { | |||
| 60 | 57 | ||
| 61 | static int b_or (lua_State *L) { | 58 | static int b_or (lua_State *L) { |
| 62 | int i, n = lua_gettop(L); | 59 | int i, n = lua_gettop(L); |
| 63 | b_uint r = 0; | 60 | lua_Unsigned r = 0; |
| 64 | for (i = 1; i <= n; i++) | 61 | for (i = 1; i <= n; i++) |
| 65 | r |= luaL_checkunsigned(L, i); | 62 | r |= luaL_checkunsigned(L, i); |
| 66 | lua_pushunsigned(L, trim(r)); | 63 | lua_pushunsigned(L, trim(r)); |
| @@ -70,7 +67,7 @@ static int b_or (lua_State *L) { | |||
| 70 | 67 | ||
| 71 | static int b_xor (lua_State *L) { | 68 | static int b_xor (lua_State *L) { |
| 72 | int i, n = lua_gettop(L); | 69 | int i, n = lua_gettop(L); |
| 73 | b_uint r = 0; | 70 | lua_Unsigned r = 0; |
| 74 | for (i = 1; i <= n; i++) | 71 | for (i = 1; i <= n; i++) |
| 75 | r ^= luaL_checkunsigned(L, i); | 72 | r ^= luaL_checkunsigned(L, i); |
| 76 | lua_pushunsigned(L, trim(r)); | 73 | lua_pushunsigned(L, trim(r)); |
| @@ -79,13 +76,13 @@ static int b_xor (lua_State *L) { | |||
| 79 | 76 | ||
| 80 | 77 | ||
| 81 | static int b_not (lua_State *L) { | 78 | static int b_not (lua_State *L) { |
| 82 | b_uint r = ~luaL_checkunsigned(L, 1); | 79 | lua_Unsigned r = ~luaL_checkunsigned(L, 1); |
| 83 | lua_pushunsigned(L, trim(r)); | 80 | lua_pushunsigned(L, trim(r)); |
| 84 | return 1; | 81 | return 1; |
| 85 | } | 82 | } |
| 86 | 83 | ||
| 87 | 84 | ||
| 88 | static int b_shift (lua_State *L, b_uint r, int i) { | 85 | static int b_shift (lua_State *L, lua_Unsigned r, int i) { |
| 89 | if (i < 0) { /* shift right? */ | 86 | if (i < 0) { /* shift right? */ |
| 90 | i = -i; | 87 | i = -i; |
| 91 | r = trim(r); | 88 | r = trim(r); |
| @@ -113,14 +110,14 @@ static int b_rshift (lua_State *L) { | |||
| 113 | 110 | ||
| 114 | 111 | ||
| 115 | static int b_arshift (lua_State *L) { | 112 | static int b_arshift (lua_State *L) { |
| 116 | b_uint r = luaL_checkunsigned(L, 1); | 113 | lua_Unsigned r = luaL_checkunsigned(L, 1); |
| 117 | int i = luaL_checkint(L, 2); | 114 | int i = luaL_checkint(L, 2); |
| 118 | if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) | 115 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) |
| 119 | return b_shift(L, r, -i); | 116 | return b_shift(L, r, -i); |
| 120 | else { /* arithmetic shift for 'negative' number */ | 117 | else { /* arithmetic shift for 'negative' number */ |
| 121 | if (i >= LUA_NBITS) r = ALLONES; | 118 | if (i >= LUA_NBITS) r = ALLONES; |
| 122 | else | 119 | else |
| 123 | r = trim((r >> i) | ~(trim(~(b_uint)0) >> i)); /* add signal bit */ | 120 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ |
| 124 | lua_pushunsigned(L, r); | 121 | lua_pushunsigned(L, r); |
| 125 | return 1; | 122 | return 1; |
| 126 | } | 123 | } |
| @@ -128,7 +125,7 @@ static int b_arshift (lua_State *L) { | |||
| 128 | 125 | ||
| 129 | 126 | ||
| 130 | static int b_rot (lua_State *L, int i) { | 127 | static int b_rot (lua_State *L, int i) { |
| 131 | b_uint r = luaL_checkunsigned(L, 1); | 128 | lua_Unsigned r = luaL_checkunsigned(L, 1); |
| 132 | i &= (LUA_NBITS - 1); /* i = i % NBITS */ | 129 | i &= (LUA_NBITS - 1); /* i = i % NBITS */ |
| 133 | r = trim(r); | 130 | r = trim(r); |
| 134 | r = (r << i) | (r >> (LUA_NBITS - i)); | 131 | r = (r << i) | (r >> (LUA_NBITS - i)); |
| @@ -167,7 +164,7 @@ static int fieldargs (lua_State *L, int farg, int *width) { | |||
| 167 | 164 | ||
| 168 | static int b_extract (lua_State *L) { | 165 | static int b_extract (lua_State *L) { |
| 169 | int w; | 166 | int w; |
| 170 | b_uint r = trim(luaL_checkunsigned(L, 1)); | 167 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); |
| 171 | int f = fieldargs(L, 2, &w); | 168 | int f = fieldargs(L, 2, &w); |
| 172 | r = (r >> f) & mask(w); | 169 | r = (r >> f) & mask(w); |
| 173 | lua_pushunsigned(L, r); | 170 | lua_pushunsigned(L, r); |
| @@ -177,8 +174,8 @@ static int b_extract (lua_State *L) { | |||
| 177 | 174 | ||
| 178 | static int b_replace (lua_State *L) { | 175 | static int b_replace (lua_State *L) { |
| 179 | int w; | 176 | int w; |
| 180 | b_uint r = trim(luaL_checkunsigned(L, 1)); | 177 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); |
| 181 | b_uint v = luaL_checkunsigned(L, 2); | 178 | lua_Unsigned v = luaL_checkunsigned(L, 2); |
| 182 | int f = fieldargs(L, 3, &w); | 179 | int f = fieldargs(L, 3, &w); |
| 183 | int m = mask(w); | 180 | int m = mask(w); |
| 184 | v &= m; /* erase bits outside given width */ | 181 | v &= m; /* erase bits outside given width */ |
