aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lbitlib.c27
-rw-r--r--linit.c4
-rw-r--r--lualib.h6
3 files changed, 26 insertions, 11 deletions
diff --git a/lbitlib.c b/lbitlib.c
index 21cf95b6..3bebccd5 100644
--- a/lbitlib.c
+++ b/lbitlib.c
@@ -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
84static int b_shift (lua_State *L, int i) { 84static 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
100static int b_lshift (lua_State *L) { 99static 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
105static int b_rshift (lua_State *L) { 104static 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
109static 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
144LUAMOD_API int luaopen_bit (lua_State *L) { 159LUAMOD_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}
diff --git a/linit.c b/linit.c
index 2779e9b1..de6e06ec 100644
--- a/linit.c
+++ b/linit.c
@@ -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},
diff --git a/lualib.h b/lualib.h
index 5c5d0a9c..b1815924 100644
--- a/lualib.h
+++ b/lualib.h
@@ -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"
33LUAMOD_API int (luaopen_string) (lua_State *L); 33LUAMOD_API int (luaopen_string) (lua_State *L);
34 34
35#define LUA_BITLIBNAME "bit" 35#define LUA_BITLIBNAME "bit32"
36LUAMOD_API int (luaopen_bit) (lua_State *L); 36LUAMOD_API int (luaopen_bit32) (lua_State *L);
37 37
38#define LUA_MATHLIBNAME "math" 38#define LUA_MATHLIBNAME "math"
39LUAMOD_API int (luaopen_math) (lua_State *L); 39LUAMOD_API int (luaopen_math) (lua_State *L);