summaryrefslogtreecommitdiff
path: root/src/llimits.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/llimits.h')
-rw-r--r--src/llimits.h92
1 files changed, 91 insertions, 1 deletions
diff --git a/src/llimits.h b/src/llimits.h
index 4b75a84e..f8ede143 100644
--- a/src/llimits.h
+++ b/src/llimits.h
@@ -1,5 +1,5 @@
/*
-** $Id: llimits.h,v 1.82 2010/05/31 16:08:55 roberto Exp $
+** $Id: llimits.h,v 1.84 2010/11/08 16:33:20 roberto Exp $
** Limits, basic types, and some other `installation-dependent' definitions
** See Copyright Notice in lua.h
*/
@@ -168,6 +168,96 @@ typedef lu_int32 Instruction;
#define luai_userstateyield(L,n) ((void)L)
#endif
+/*
+** lua_number2int is a macro to convert lua_Number to int.
+** lua_number2integer is a macro to convert lua_Number to lua_Integer.
+** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
+** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
+*/
+
+#if defined(MS_ASMTRICK) /* { */
+/* trick with Microsoft assembler for X86 */
+
+#define lua_number2int(i,n) __asm {__asm fld n __asm fistp i}
+#define lua_number2integer(i,n) lua_number2int(i, n)
+#define lua_number2unsigned(i,n) \
+ {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;}
+
+
+#elif defined(LUA_IEEE754TRICK) /* }{ */
+/* the next trick should work on any machine using IEEE754 with
+ a 32-bit integer 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)
+#else
+#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]; }
+
+#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)
+
+#endif /* } */
+
+
+/* the following definitions always work, but may be slow */
+
+#if !defined(lua_number2int)
+#define lua_number2int(i,n) ((i)=(int)(n))
+#endif
+
+#if !defined(lua_number2integer)
+#define lua_number2integer(i,n) ((i)=(lua_Integer)(n))
+#endif
+
+#if !defined(lua_number2unsigned) /* { */
+/* the following definition assures proper modulo behavior */
+#if defined(LUA_NUMBER_DOUBLE)
+#include <math.h>
+#define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1)
+#define lua_number2unsigned(i,n) \
+ ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED))
+#else
+#define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
+#endif
+#endif /* } */
+
+
+#if !defined(lua_unsigned2number)
+/* on several machines, coercion from unsigned to double is slow,
+ so it may be worth to avoid */
+#define lua_unsigned2number(u) \
+ (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
+#endif
+
+
+/*
+** luai_hashnum is a macro do hash a lua_Number value into an integer.
+** The hash must be deterministic and give reasonable values for
+** both small and large values (outside the range of integers).
+** It is used only in ltable.c.
+*/
+
+#if !defined(luai_hashnum) /* { */
+
+#include <float.h>
+#include <math.h>
+
+#define luai_hashnum(i,n) { int e; \
+ n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
+ lua_number2int(i, n); i += e; }
+
+#endif /* } */