diff options
| -rw-r--r-- | lbitlib.c | 46 |
1 files changed, 25 insertions, 21 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbitlib.c,v 1.28 2014/11/02 19:19:04 roberto Exp roberto $ | 2 | ** $Id: lbitlib.c,v 1.29 2015/10/08 15:55:35 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,6 +19,10 @@ | |||
| 19 | #if defined(LUA_COMPAT_BITLIB) /* { */ | 19 | #if defined(LUA_COMPAT_BITLIB) /* { */ |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | #define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) | ||
| 23 | #define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) | ||
| 24 | |||
| 25 | |||
| 22 | /* number of bits to consider in a number */ | 26 | /* number of bits to consider in a number */ |
| 23 | #if !defined(LUA_NBITS) | 27 | #if !defined(LUA_NBITS) |
| 24 | #define LUA_NBITS 32 | 28 | #define LUA_NBITS 32 |
| @@ -46,14 +50,14 @@ static lua_Unsigned andaux (lua_State *L) { | |||
| 46 | int i, n = lua_gettop(L); | 50 | int i, n = lua_gettop(L); |
| 47 | lua_Unsigned r = ~(lua_Unsigned)0; | 51 | lua_Unsigned r = ~(lua_Unsigned)0; |
| 48 | for (i = 1; i <= n; i++) | 52 | for (i = 1; i <= n; i++) |
| 49 | r &= luaL_checkunsigned(L, i); | 53 | r &= checkunsigned(L, i); |
| 50 | return trim(r); | 54 | return trim(r); |
| 51 | } | 55 | } |
| 52 | 56 | ||
| 53 | 57 | ||
| 54 | static int b_and (lua_State *L) { | 58 | static int b_and (lua_State *L) { |
| 55 | lua_Unsigned r = andaux(L); | 59 | lua_Unsigned r = andaux(L); |
| 56 | lua_pushunsigned(L, r); | 60 | pushunsigned(L, r); |
| 57 | return 1; | 61 | return 1; |
| 58 | } | 62 | } |
| 59 | 63 | ||
| @@ -69,8 +73,8 @@ static int b_or (lua_State *L) { | |||
| 69 | int i, n = lua_gettop(L); | 73 | int i, n = lua_gettop(L); |
| 70 | lua_Unsigned r = 0; | 74 | lua_Unsigned r = 0; |
| 71 | for (i = 1; i <= n; i++) | 75 | for (i = 1; i <= n; i++) |
| 72 | r |= luaL_checkunsigned(L, i); | 76 | r |= checkunsigned(L, i); |
| 73 | lua_pushunsigned(L, trim(r)); | 77 | pushunsigned(L, trim(r)); |
| 74 | return 1; | 78 | return 1; |
| 75 | } | 79 | } |
| 76 | 80 | ||
| @@ -79,15 +83,15 @@ static int b_xor (lua_State *L) { | |||
| 79 | int i, n = lua_gettop(L); | 83 | int i, n = lua_gettop(L); |
| 80 | lua_Unsigned r = 0; | 84 | lua_Unsigned r = 0; |
| 81 | for (i = 1; i <= n; i++) | 85 | for (i = 1; i <= n; i++) |
| 82 | r ^= luaL_checkunsigned(L, i); | 86 | r ^= checkunsigned(L, i); |
| 83 | lua_pushunsigned(L, trim(r)); | 87 | pushunsigned(L, trim(r)); |
| 84 | return 1; | 88 | return 1; |
| 85 | } | 89 | } |
| 86 | 90 | ||
| 87 | 91 | ||
| 88 | static int b_not (lua_State *L) { | 92 | static int b_not (lua_State *L) { |
| 89 | lua_Unsigned r = ~luaL_checkunsigned(L, 1); | 93 | lua_Unsigned r = ~checkunsigned(L, 1); |
| 90 | lua_pushunsigned(L, trim(r)); | 94 | pushunsigned(L, trim(r)); |
| 91 | return 1; | 95 | return 1; |
| 92 | } | 96 | } |
| 93 | 97 | ||
| @@ -104,23 +108,23 @@ static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { | |||
| 104 | else r <<= i; | 108 | else r <<= i; |
| 105 | r = trim(r); | 109 | r = trim(r); |
| 106 | } | 110 | } |
| 107 | lua_pushunsigned(L, r); | 111 | pushunsigned(L, r); |
| 108 | return 1; | 112 | return 1; |
| 109 | } | 113 | } |
| 110 | 114 | ||
| 111 | 115 | ||
| 112 | static int b_lshift (lua_State *L) { | 116 | static int b_lshift (lua_State *L) { |
| 113 | return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2)); | 117 | return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); |
| 114 | } | 118 | } |
| 115 | 119 | ||
| 116 | 120 | ||
| 117 | static int b_rshift (lua_State *L) { | 121 | static int b_rshift (lua_State *L) { |
| 118 | return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2)); | 122 | return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); |
| 119 | } | 123 | } |
| 120 | 124 | ||
| 121 | 125 | ||
| 122 | static int b_arshift (lua_State *L) { | 126 | static int b_arshift (lua_State *L) { |
| 123 | lua_Unsigned r = luaL_checkunsigned(L, 1); | 127 | lua_Unsigned r = checkunsigned(L, 1); |
| 124 | lua_Integer i = luaL_checkinteger(L, 2); | 128 | lua_Integer i = luaL_checkinteger(L, 2); |
| 125 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) | 129 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) |
| 126 | return b_shift(L, r, -i); | 130 | return b_shift(L, r, -i); |
| @@ -128,19 +132,19 @@ static int b_arshift (lua_State *L) { | |||
| 128 | if (i >= LUA_NBITS) r = ALLONES; | 132 | if (i >= LUA_NBITS) r = ALLONES; |
| 129 | else | 133 | else |
| 130 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ | 134 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ |
| 131 | lua_pushunsigned(L, r); | 135 | pushunsigned(L, r); |
| 132 | return 1; | 136 | return 1; |
| 133 | } | 137 | } |
| 134 | } | 138 | } |
| 135 | 139 | ||
| 136 | 140 | ||
| 137 | static int b_rot (lua_State *L, lua_Integer d) { | 141 | static int b_rot (lua_State *L, lua_Integer d) { |
| 138 | lua_Unsigned r = luaL_checkunsigned(L, 1); | 142 | lua_Unsigned r = checkunsigned(L, 1); |
| 139 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ | 143 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ |
| 140 | r = trim(r); | 144 | r = trim(r); |
| 141 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ | 145 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ |
| 142 | r = (r << i) | (r >> (LUA_NBITS - i)); | 146 | r = (r << i) | (r >> (LUA_NBITS - i)); |
| 143 | lua_pushunsigned(L, trim(r)); | 147 | pushunsigned(L, trim(r)); |
| 144 | return 1; | 148 | return 1; |
| 145 | } | 149 | } |
| 146 | 150 | ||
| @@ -175,22 +179,22 @@ static int fieldargs (lua_State *L, int farg, int *width) { | |||
| 175 | 179 | ||
| 176 | static int b_extract (lua_State *L) { | 180 | static int b_extract (lua_State *L) { |
| 177 | int w; | 181 | int w; |
| 178 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); | 182 | lua_Unsigned r = trim(checkunsigned(L, 1)); |
| 179 | int f = fieldargs(L, 2, &w); | 183 | int f = fieldargs(L, 2, &w); |
| 180 | r = (r >> f) & mask(w); | 184 | r = (r >> f) & mask(w); |
| 181 | lua_pushunsigned(L, r); | 185 | pushunsigned(L, r); |
| 182 | return 1; | 186 | return 1; |
| 183 | } | 187 | } |
| 184 | 188 | ||
| 185 | 189 | ||
| 186 | static int b_replace (lua_State *L) { | 190 | static int b_replace (lua_State *L) { |
| 187 | int w; | 191 | int w; |
| 188 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); | 192 | lua_Unsigned r = trim(checkunsigned(L, 1)); |
| 189 | lua_Unsigned v = trim(luaL_checkunsigned(L, 2)); | 193 | lua_Unsigned v = trim(checkunsigned(L, 2)); |
| 190 | int f = fieldargs(L, 3, &w); | 194 | int f = fieldargs(L, 3, &w); |
| 191 | lua_Unsigned m = mask(w); | 195 | lua_Unsigned m = mask(w); |
| 192 | r = (r & ~(m << f)) | ((v & m) << f); | 196 | r = (r & ~(m << f)) | ((v & m) << f); |
| 193 | lua_pushunsigned(L, r); | 197 | pushunsigned(L, r); |
| 194 | return 1; | 198 | return 1; |
| 195 | } | 199 | } |
| 196 | 200 | ||
