Forráskód Böngészése

new module 'lbitlib.c' for bitwise operations

Roberto Ierusalimschy 16 éve
szülő
commit
3abe3da9fb
4 módosított fájl, 38 hozzáadás és 23 törlés
  1. 2 1
      linit.c
  2. 30 20
      luaconf.h
  3. 4 1
      lualib.h
  4. 2 1
      makefile

+ 2 - 1
linit.c

@@ -1,5 +1,5 @@
 /*
-** $Id: linit.c,v 1.17 2009/05/01 13:37:11 roberto Exp roberto $
+** $Id: linit.c,v 1.18 2009/05/01 13:46:35 roberto Exp roberto $
 ** Initialization of libraries for lua.c
 ** See Copyright Notice in lua.h
 */
@@ -24,6 +24,7 @@ static const luaL_Reg loadedlibs[] = {
   {LUA_IOLIBNAME, luaopen_io},
   {LUA_OSLIBNAME, luaopen_os},
   {LUA_STRLIBNAME, luaopen_string},
+  {LUA_BITLIBNAME, luaopen_bit},
   {LUA_MATHLIBNAME, luaopen_math},
   {NULL, NULL}
 };

+ 30 - 20
luaconf.h

@@ -1,5 +1,5 @@
 /*
-** $Id: luaconf.h,v 1.104 2009/03/26 12:57:01 roberto Exp roberto $
+** $Id: luaconf.h,v 1.105 2009/06/18 18:19:36 roberto Exp roberto $
 ** Configuration file for Lua
 ** See Copyright Notice in lua.h
 */
@@ -141,14 +141,6 @@
 #define LUA_IGMARK	"-"
 
 
-/*
-@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
-** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
-** machines, ptrdiff_t gives a good choice between int or long.)
-*/
-#define LUA_INTEGER	ptrdiff_t
-
-
 /*
 @@ LUA_API is a mark for all core API functions.
 @@ LUALIB_API is a mark for all standard library functions.
@@ -403,24 +395,22 @@
 
 
 /*
-@@ LUAI_UINT32 is an unsigned integer with at least 32 bits.
-@@ LUAI_INT32 is an signed integer with at least 32 bits.
+@@ LUA_INT32 is an signed integer with exactly 32 bits.
 @@ LUAI_UMEM is an unsigned integer big enough to count the total
 @* memory used by Lua.
 @@ LUAI_MEM is a signed integer big enough to count the total memory
 @* used by Lua.
 ** CHANGE here if for some weird reason the default definitions are not
-** good enough for your machine. (The definitions in the 'else'
-** part always works, but may waste space on machines with 64-bit
-** longs.) Probably you do not need to change this.
+** good enough for your machine.  Probably you do not need to change
+** this.
 */
 #if LUAI_BITSINT >= 32
-#define LUAI_UINT32	unsigned int
+#define LUA_INT32	int
 #define LUAI_UMEM	size_t
 #define LUAI_MEM	ptrdiff_t
 #else
 /* 16-bit ints */
-#define LUAI_UINT32	unsigned long
+#define LUA_INT32	long
 #define LUAI_UMEM	unsigned long
 #define LUAI_MEM	long
 #endif
@@ -552,9 +542,21 @@
 #endif
 
 
+/*
+@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.
+** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
+** machines, ptrdiff_t gives a good choice between int or long.)
+*/
+#define LUA_INTEGER	ptrdiff_t
+
+
 /*
 @@ lua_number2int is a macro to convert lua_Number to int.
-@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
+@@ lua_number2integer is a macro to convert lua_Number to lUA_INTEGER.
+@@ lua_number2uint is a macro to convert a lua_Number to an unsigned
+@* LUA_INT32.
+@@ lua_uint2number is a macro to convert an unsigned LUA_INT32
+@* to a lua_Number.
 ** CHANGE them if you know a faster way to convert a lua_Number to
 ** int (with any rounding method and without throwing errors) in your
 ** system. In Pentium machines, a naive typecast from double to int
@@ -571,25 +573,33 @@
 #define lua_number2int(i,d)   __asm fld d   __asm fistp i
 #define lua_number2integer(i,n)		lua_number2int(i, n)
 
+#else
 /* the next trick should work on any Pentium, but sometimes clashes
    with a DirectX idiosyncrasy */
-#else
 
 union luai_Cast { double l_d; long l_l; };
 #define lua_number2int(i,d) \
   { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
 #define lua_number2integer(i,n)		lua_number2int(i, n)
+#define lua_number2uint(i,n)		lua_number2int(i, n)
 
 #endif
 
 
-/* this option always works, but may be slow */
 #else
+/* this option always works, but may be slow */
 #define lua_number2int(i,d)	((i)=(int)(d))
-#define lua_number2integer(i,d)	((i)=(lua_Integer)(d))
+#define lua_number2integer(i,d)	((i)=(LUA_INTEGER)(d))
+#define lua_number2uint(i,d)	((i)=(unsigned LUA_INT32)(d))
 
 #endif
 
+
+/* on several machines, coercion from unsigned to double is too slow,
+   so avoid that if possible */
+#define lua_uint2number(u)  \
+	((LUA_INT32)(u) < 0 ? (lua_Number)(u) : (lua_Number)(LUA_INT32)(u))
+
 /* }================================================================== */
 
 

+ 4 - 1
lualib.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lualib.h,v 1.36 2005/12/27 17:12:00 roberto Exp roberto $
+** $Id: lualib.h,v 1.37 2006/09/11 14:07:24 roberto Exp roberto $
 ** Lua standard libraries
 ** See Copyright Notice in lua.h
 */
@@ -30,6 +30,9 @@ LUALIB_API int (luaopen_os) (lua_State *L);
 #define LUA_STRLIBNAME	"string"
 LUALIB_API int (luaopen_string) (lua_State *L);
 
+#define LUA_BITLIBNAME	"bit"
+LUALIB_API int (luaopen_bit) (lua_State *L);
+
 #define LUA_MATHLIBNAME	"math"
 LUALIB_API int (luaopen_math) (lua_State *L);
 

+ 2 - 1
makefile

@@ -59,7 +59,7 @@ CORE_O=	lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \
 	ltm.o lundump.o lvm.o lzio.o ltests.o
 AUX_O=	lauxlib.o
 LIB_O=	lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \
-	loadlib.o linit.o
+	lbitlib.o loadlib.o linit.o
 
 LUA_T=	lua
 LUA_O=	lua.o
@@ -112,6 +112,7 @@ lapi.o: lapi.c lua.h luaconf.h lapi.h llimits.h lstate.h lobject.h ltm.h \
   lvm.h makefile
 lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h makefile
 lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h makefile
+lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h makefile
 lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
   lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \
   ltable.h makefile