diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-25 12:32:36 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-25 12:32:36 -0200 |
| commit | 9e8e60dd5f2223aaabd8239b9152167a8b630b4a (patch) | |
| tree | cc174d15101f342572befeadb0a3cf7aa1f66dbd /lbitlib.c | |
| parent | d72ec210c73c9143288db06c06d35ec0077a8097 (diff) | |
| download | lua-9e8e60dd5f2223aaabd8239b9152167a8b630b4a.tar.gz lua-9e8e60dd5f2223aaabd8239b9152167a8b630b4a.tar.bz2 lua-9e8e60dd5f2223aaabd8239b9152167a8b630b4a.zip | |
bitlib renamed to 'bit32' + new function for arithmetic shift
Diffstat (limited to 'lbitlib.c')
| -rw-r--r-- | lbitlib.c | 27 |
1 files changed, 21 insertions, 6 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbitlib.c,v 1.5 2010/07/02 11:38:13 roberto Exp roberto $ | 2 | ** $Id: lbitlib.c,v 1.6 2010/07/02 12:01:53 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 | */ |
| @@ -81,8 +81,7 @@ static int b_not (lua_State *L) { | |||
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | 83 | ||
| 84 | static int b_shift (lua_State *L, int i) { | 84 | static int b_shift (lua_State *L, b_uint r, int i) { |
| 85 | b_uint r = getuintarg(L, 1); | ||
| 86 | if (i < 0) { /* shift right? */ | 85 | if (i < 0) { /* shift right? */ |
| 87 | i = -i; | 86 | i = -i; |
| 88 | if (i >= NBITS) r = 0; | 87 | if (i >= NBITS) r = 0; |
| @@ -98,12 +97,27 @@ static int b_shift (lua_State *L, int i) { | |||
| 98 | 97 | ||
| 99 | 98 | ||
| 100 | static int b_lshift (lua_State *L) { | 99 | static int b_lshift (lua_State *L) { |
| 101 | return b_shift(L, luaL_checkint(L, 2)); | 100 | return b_shift(L, getuintarg(L, 1), luaL_checkint(L, 2)); |
| 102 | } | 101 | } |
| 103 | 102 | ||
| 104 | 103 | ||
| 105 | static int b_rshift (lua_State *L) { | 104 | static int b_rshift (lua_State *L) { |
| 106 | return b_shift(L, -luaL_checkint(L, 2)); | 105 | return b_shift(L, getuintarg(L, 1), -luaL_checkint(L, 2)); |
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | static int b_arshift (lua_State *L) { | ||
| 110 | b_uint r = getuintarg(L, 1); | ||
| 111 | int i = luaL_checkint(L, 2); | ||
| 112 | if (i < 0 || !(r & 0x80000000)) | ||
| 113 | return b_shift(L, r, -i); | ||
| 114 | else { /* arithmetic shift for 'negative' number */ | ||
| 115 | if (i >= NBITS) r = 0xffffffff; | ||
| 116 | else | ||
| 117 | r = (r >> i) | ~(~(b_uint)0 >> i); /* add signal bit */ | ||
| 118 | lua_pushnumber(L, lua_uint2number(r)); | ||
| 119 | return 1; | ||
| 120 | } | ||
| 107 | } | 121 | } |
| 108 | 122 | ||
| 109 | 123 | ||
| @@ -133,6 +147,7 @@ static const luaL_Reg bitlib[] = { | |||
| 133 | {"bxor", b_xor}, | 147 | {"bxor", b_xor}, |
| 134 | {"bnot", b_not}, | 148 | {"bnot", b_not}, |
| 135 | {"lshift", b_lshift}, | 149 | {"lshift", b_lshift}, |
| 150 | {"arshift", b_arshift}, | ||
| 136 | {"rshift", b_rshift}, | 151 | {"rshift", b_rshift}, |
| 137 | {"rol", b_rol}, | 152 | {"rol", b_rol}, |
| 138 | {"ror", b_ror}, | 153 | {"ror", b_ror}, |
| @@ -141,7 +156,7 @@ static const luaL_Reg bitlib[] = { | |||
| 141 | 156 | ||
| 142 | 157 | ||
| 143 | 158 | ||
| 144 | LUAMOD_API int luaopen_bit (lua_State *L) { | 159 | LUAMOD_API int luaopen_bit32 (lua_State *L) { |
| 145 | luaL_newlib(L, bitlib); | 160 | luaL_newlib(L, bitlib); |
| 146 | return 1; | 161 | return 1; |
| 147 | } | 162 | } |
