Răsfoiți Sursa

better(?) handling of '#define's for IEEE-related tricks + avoid using
IEEE trick for 64-bit integer types (lua_Integer on 64-bit machines)

Roberto Ierusalimschy 13 ani în urmă
părinte
comite
77cbd817d1
3 a modificat fișierele cu 82 adăugiri și 66 ștergeri
  1. 10 5
      llimits.h
  2. 19 18
      lobject.h
  3. 53 43
      luaconf.h

+ 10 - 5
llimits.h

@@ -1,5 +1,5 @@
 /*
-** $Id: llimits.h,v 1.96 2012/01/25 21:05:40 roberto Exp roberto $
+** $Id: llimits.h,v 1.97 2012/03/28 18:27:25 roberto Exp roberto $
 ** Limits, basic types, and some other `installation-dependent' definitions
 ** See Copyright Notice in lua.h
 */
@@ -209,31 +209,36 @@ typedef lu_int32 Instruction;
 
 #elif defined(LUA_IEEE754TRICK)		/* }{ */
 /* the next trick should work on any machine using IEEE754 with
-   a 32-bit integer type */
+   a 32-bit int type */
 
 union luai_Cast { double l_d; LUA_INT32 l_p[2]; };
 
 #if !defined(LUA_IEEEENDIAN)	/* { */
 #define LUAI_EXTRAIEEE	\
   static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)};
-#define LUA_IEEEENDIAN		(ieeeendian.l_p[1] == 33)
+#define LUA_IEEEENDIANLOC	(ieeeendian.l_p[1] == 33)
 #else
+#define LUA_IEEEENDIANLOC	LUA_IEEEENDIAN
 #define LUAI_EXTRAIEEE		/* empty */
 #endif				/* } */
 
 #define lua_number2int32(i,n,t) \
   { LUAI_EXTRAIEEE \
     volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
-    (i) = (t)u.l_p[LUA_IEEEENDIAN]; }
+    (i) = (t)u.l_p[LUA_IEEEENDIANLOC]; }
 
 #define luai_hashnum(i,n)  \
   { volatile union luai_Cast u; u.l_d = (n) + 1.0;  /* avoid -0 */ \
     (i) = u.l_p[0]; (i) += u.l_p[1]; }  /* add double bits for his hash */
 
 #define lua_number2int(i,n)		lua_number2int32(i, n, int)
-#define lua_number2integer(i,n)		lua_number2int32(i, n, lua_Integer)
 #define lua_number2unsigned(i,n)	lua_number2int32(i, n, lua_Unsigned)
 
+/* the trick can be expanded to lua_Integer when it is a 32-bit value */
+#if defined(LUA_IEEELL)
+#define lua_number2integer(i,n)		lua_number2int32(i, n, lua_Integer)
+#endif
+
 #endif				/* } */
 
 

+ 19 - 18
lobject.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.h,v 2.68 2012/01/25 21:05:40 roberto Exp roberto $
+** $Id: lobject.h,v 2.69 2012/05/08 13:53:33 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -266,6 +266,8 @@ typedef struct lua_TValue TValue;
 #define setsvalue2n	setsvalue
 
 
+/* check whether a number is valid (useful only for NaN trick) */
+#define luai_checknum(L,o,c)	{ /* empty */ }
 
 
 /*
@@ -273,10 +275,7 @@ typedef struct lua_TValue TValue;
 ** NaN Trick
 ** =======================================================
 */
-
-#if defined(LUA_NANTRICK) \
- || defined(LUA_NANTRICK_LE) \
- || defined(LUA_NANTRICK_BE)
+#if defined(LUA_NANTRICK)
 
 /*
 ** numbers are represented in the 'd_' field. All other values have the
@@ -284,15 +283,23 @@ typedef struct lua_TValue TValue;
 ** a "signaled NaN", which is never generated by regular operations by
 ** the CPU (nor by 'strtod')
 */
-#if !defined(NNMARK)
+
+/* allows for external implementation for part of the trick */
+#if !defined(NNMARK)	/* { */
+
+
+#if !defined(LUA_IEEEENDIAN)
+#error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN'
+#endif
+
+
 #define NNMARK		0x7FF7A500
 #define NNMASK		0x7FFFFF00
-#endif
 
 #undef TValuefields
 #undef NILCONSTANT
 
-#if defined(LUA_NANTRICK_LE)
+#if (LUA_IEEEENDIAN == 0)	/* { */
 
 /* little endian */
 #define TValuefields  \
@@ -303,7 +310,7 @@ typedef struct lua_TValue TValue;
 #define d_(o)		((o)->u.d__)
 #define tt_(o)		((o)->u.i.tt__)
 
-#elif defined(LUA_NANTRICK_BE)
+#else				/* }{ */
 
 /* big endian */
 #define TValuefields  \
@@ -314,10 +321,9 @@ typedef struct lua_TValue TValue;
 #define d_(o)		((o)->u.d__)
 #define tt_(o)		((o)->u.i.tt__)
 
-#elif !defined(TValuefields)
-#error option 'LUA_NANTRICK' needs declaration for 'TValuefields'
+#endif				/* } */
 
-#endif
+#endif			/* } */
 
 
 /* correspondence with standard representation */
@@ -367,14 +373,9 @@ typedef struct lua_TValue TValue;
 	(ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
 
 
-
+#undef luai_checknum
 #define luai_checknum(L,o,c)	{ if (!ttisnumber(o)) c; }
 
-
-#else
-
-#define luai_checknum(L,o,c)	{ /* empty */ }
-
 #endif
 /* }====================================================== */
 

+ 53 - 43
luaconf.h

@@ -1,5 +1,5 @@
 /*
-** $Id: luaconf.h,v 1.169 2011/11/30 12:35:05 roberto Exp roberto $
+** $Id: luaconf.h,v 1.170 2011/12/06 16:58:36 roberto Exp roberto $
 ** Configuration file for Lua
 ** See Copyright Notice in lua.h
 */
@@ -453,66 +453,76 @@
 #define LUA_UNSIGNED	unsigned LUA_INT32
 
 
-#if defined(LUA_CORE)		/* { */
 
-#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI)	/* { */
+/*
+** Some tricks with doubles
+*/
+
+#if defined(LUA_CORE) && \
+    defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI)	/* { */
+/*
+** The next definitions activate some tricks to speed up the
+** conversion from doubles to integer types, mainly to LUA_UNSIGNED.
+**
+@@ MS_ASMTRICK uses Microsoft assembler to avoid clashes with a
+** DirectX idiosyncrasy.
+**
+@@ LUA_IEEE754TRICK uses a trick that should work on any machine
+** using IEEE754 with a 32-bit integer type.
+**
+@@ LUA_IEEELL extends the trick to LUA_INTEGER; should only be
+** defined when LUA_INTEGER is a 32-bit integer.
+**
+@@ LUA_IEEEENDIAN is the endianness of doubles in your machine
+** (0 for little endian, 1 for big endian); if not defined, Lua will
+** check it dynamically for LUA_IEEE754TRICK (but not for LUA_NANTRICK).
+**
+@@ LUA_NANTRICK controls the use of a trick to pack all types into
+** a single double value, using NaN values to represent non-number
+** values. The trick only works on 32-bit machines (ints and pointers
+** are 32-bit values) with numbers represented as IEEE 754-2008 doubles
+** with conventional endianess (12345678 or 87654321), in CPUs that do
+** not produce signaling NaN values (all NaNs are quiet).
+*/
 
-/* On a Microsoft compiler on a Pentium, use assembler to avoid clashes
-   with a DirectX idiosyncrasy */
+/* Microsoft compiler on a Pentium (32 bit) ? */
 #if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86)	/* { */
 
 #define MS_ASMTRICK
+#define LUA_IEEEENDIAN		0
+#define LUA_NANTRICK
 
-#else				/* }{ */
-/* the next definition uses a trick that should work on any machine
-   using IEEE754 with a 32-bit integer type */
-
-#define LUA_IEEE754TRICK
-
-/*
-@@ LUA_IEEEENDIAN is the endianness of doubles in your machine
-** (0 for little endian, 1 for big endian); if not defined, Lua will
-** check it dynamically.
-*/
-/* check for known architectures */
-#if defined(__i386__) || defined(__i386) || defined(__X86__) || \
-    defined (__x86_64)
-#define LUA_IEEEENDIAN	0
-#elif defined(__POWERPC__) || defined(__ppc__)
-#define LUA_IEEEENDIAN	1
-#endif
 
-#endif				/* } */
+/* pentium 32 bits? */
+#elif defined(__i386__) || defined(__i386) || defined(__X86__) /* }{ */
 
-#endif			/* } */
+#define LUA_IEEE754TRICK
+#define LUA_IEEELL
+#define LUA_IEEEENDIAN		0
+#define LUA_NANTRICK
 
-#endif			/* } */
+/* pentium 64 bits? */
+#elif defined(__x86_64)						/* }{ */
 
-/* }================================================================== */
+#define LUA_IEEE754TRICK
+#define LUA_IEEEENDIAN		0
 
+#elif defined(__POWERPC__) || defined(__ppc__)			/* }{ */
 
-/*
-@@ LUA_NANTRICK_LE/LUA_NANTRICK_BE controls the use of a trick to
-** pack all types into a single double value, using NaN values to
-** represent non-number values. The trick only works on 32-bit machines
-** (ints and pointers are 32-bit values) with numbers represented as
-** IEEE 754-2008 doubles with conventional endianess (12345678 or
-** 87654321), in CPUs that do not produce signaling NaN values (all NaNs
-** are quiet).
-*/
-#if defined(LUA_CORE) && \
-    defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI)	/* { */
+#define LUA_IEEE754TRICK
+#define LUA_IEEEENDIAN		1
 
-/* little-endian architectures that satisfy those conditions */
-#if defined(__i386__) || defined(__i386) || defined(__X86__) || \
-    defined(_M_IX86)
+#else								/* }{ */
 
-#define LUA_NANTRICK_LE
+/* assume IEEE754 and a 32-bit integer type */
+#define LUA_IEEE754TRICK
 
-#endif
+#endif								/* } */
 
 #endif							/* } */
 
+/* }================================================================== */
+