bitlib renamed to 'bit32' + new function for arithmetic shift

This commit is contained in:
Roberto Ierusalimschy 2010-10-25 12:32:36 -02:00
parent d72ec210c7
commit 9e8e60dd5f
3 changed files with 26 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: lbitlib.c,v 1.5 2010/07/02 11:38:13 roberto Exp roberto $
** $Id: lbitlib.c,v 1.6 2010/07/02 12:01:53 roberto Exp roberto $
** Standard library for bitwise operations
** See Copyright Notice in lua.h
*/
@ -81,8 +81,7 @@ static int b_not (lua_State *L) {
}
static int b_shift (lua_State *L, int i) {
b_uint r = getuintarg(L, 1);
static int b_shift (lua_State *L, b_uint r, int i) {
if (i < 0) { /* shift right? */
i = -i;
if (i >= NBITS) r = 0;
@ -98,12 +97,27 @@ static int b_shift (lua_State *L, int i) {
static int b_lshift (lua_State *L) {
return b_shift(L, luaL_checkint(L, 2));
return b_shift(L, getuintarg(L, 1), luaL_checkint(L, 2));
}
static int b_rshift (lua_State *L) {
return b_shift(L, -luaL_checkint(L, 2));
return b_shift(L, getuintarg(L, 1), -luaL_checkint(L, 2));
}
static int b_arshift (lua_State *L) {
b_uint r = getuintarg(L, 1);
int i = luaL_checkint(L, 2);
if (i < 0 || !(r & 0x80000000))
return b_shift(L, r, -i);
else { /* arithmetic shift for 'negative' number */
if (i >= NBITS) r = 0xffffffff;
else
r = (r >> i) | ~(~(b_uint)0 >> i); /* add signal bit */
lua_pushnumber(L, lua_uint2number(r));
return 1;
}
}
@ -133,6 +147,7 @@ static const luaL_Reg bitlib[] = {
{"bxor", b_xor},
{"bnot", b_not},
{"lshift", b_lshift},
{"arshift", b_arshift},
{"rshift", b_rshift},
{"rol", b_rol},
{"ror", b_ror},
@ -141,7 +156,7 @@ static const luaL_Reg bitlib[] = {
LUAMOD_API int luaopen_bit (lua_State *L) {
LUAMOD_API int luaopen_bit32 (lua_State *L) {
luaL_newlib(L, bitlib);
return 1;
}

View File

@ -1,5 +1,5 @@
/*
** $Id: linit.c,v 1.27 2010/06/30 17:40:27 roberto Exp roberto $
** $Id: linit.c,v 1.28 2010/07/02 11:38:13 roberto Exp roberto $
** Initialization of libraries for lua.c and other clients
** See Copyright Notice in lua.h
*/
@ -34,7 +34,7 @@ static const luaL_Reg loadedlibs[] = {
{LUA_IOLIBNAME, luaopen_io},
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_BITLIBNAME, luaopen_bit},
{LUA_BITLIBNAME, luaopen_bit32},
{LUA_MATHLIBNAME, luaopen_math},
#if defined(LUA_COMPAT_DEBUGLIB)
{LUA_DBLIBNAME, luaopen_debug},

View File

@ -1,5 +1,5 @@
/*
** $Id: lualib.h,v 1.39 2009/11/24 12:05:44 roberto Exp roberto $
** $Id: lualib.h,v 1.40 2010/06/10 21:29:47 roberto Exp roberto $
** Lua standard libraries
** See Copyright Notice in lua.h
*/
@ -32,8 +32,8 @@ LUAMOD_API int (luaopen_os) (lua_State *L);
#define LUA_STRLIBNAME "string"
LUAMOD_API int (luaopen_string) (lua_State *L);
#define LUA_BITLIBNAME "bit"
LUAMOD_API int (luaopen_bit) (lua_State *L);
#define LUA_BITLIBNAME "bit32"
LUAMOD_API int (luaopen_bit32) (lua_State *L);
#define LUA_MATHLIBNAME "math"
LUAMOD_API int (luaopen_math) (lua_State *L);