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 | |
parent | d72ec210c73c9143288db06c06d35ec0077a8097 (diff) | |
download | lua-9e8e60dd5f2223aaabd8239b9152167a8b630b4a.tar.gz lua-9e8e60dd5f2223aaabd8239b9152167a8b630b4a.tar.bz2 lua-9e8e60dd5f2223aaabd8239b9152167a8b630b4a.zip |
bitlib renamed to 'bit32' + new function for arithmetic shift
-rw-r--r-- | lbitlib.c | 27 | ||||
-rw-r--r-- | linit.c | 4 | ||||
-rw-r--r-- | lualib.h | 6 |
3 files changed, 26 insertions, 11 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 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: linit.c,v 1.27 2010/06/30 17:40:27 roberto Exp roberto $ | 2 | ** $Id: linit.c,v 1.28 2010/07/02 11:38:13 roberto Exp roberto $ |
3 | ** Initialization of libraries for lua.c and other clients | 3 | ** Initialization of libraries for lua.c and other clients |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -34,7 +34,7 @@ static const luaL_Reg loadedlibs[] = { | |||
34 | {LUA_IOLIBNAME, luaopen_io}, | 34 | {LUA_IOLIBNAME, luaopen_io}, |
35 | {LUA_OSLIBNAME, luaopen_os}, | 35 | {LUA_OSLIBNAME, luaopen_os}, |
36 | {LUA_STRLIBNAME, luaopen_string}, | 36 | {LUA_STRLIBNAME, luaopen_string}, |
37 | {LUA_BITLIBNAME, luaopen_bit}, | 37 | {LUA_BITLIBNAME, luaopen_bit32}, |
38 | {LUA_MATHLIBNAME, luaopen_math}, | 38 | {LUA_MATHLIBNAME, luaopen_math}, |
39 | #if defined(LUA_COMPAT_DEBUGLIB) | 39 | #if defined(LUA_COMPAT_DEBUGLIB) |
40 | {LUA_DBLIBNAME, luaopen_debug}, | 40 | {LUA_DBLIBNAME, luaopen_debug}, |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lualib.h,v 1.39 2009/11/24 12:05:44 roberto Exp roberto $ | 2 | ** $Id: lualib.h,v 1.40 2010/06/10 21:29:47 roberto Exp roberto $ |
3 | ** Lua standard libraries | 3 | ** Lua standard libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -32,8 +32,8 @@ LUAMOD_API int (luaopen_os) (lua_State *L); | |||
32 | #define LUA_STRLIBNAME "string" | 32 | #define LUA_STRLIBNAME "string" |
33 | LUAMOD_API int (luaopen_string) (lua_State *L); | 33 | LUAMOD_API int (luaopen_string) (lua_State *L); |
34 | 34 | ||
35 | #define LUA_BITLIBNAME "bit" | 35 | #define LUA_BITLIBNAME "bit32" |
36 | LUAMOD_API int (luaopen_bit) (lua_State *L); | 36 | LUAMOD_API int (luaopen_bit32) (lua_State *L); |
37 | 37 | ||
38 | #define LUA_MATHLIBNAME "math" | 38 | #define LUA_MATHLIBNAME "math" |
39 | LUAMOD_API int (luaopen_math) (lua_State *L); | 39 | LUAMOD_API int (luaopen_math) (lua_State *L); |