From 0b00e7f1a20b5c4c3f21aaf163818335d0d61928 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 18 Mar 2002 15:18:35 -0300
Subject: new macro to convert double->int

---
 lapi.c   |  6 +++---
 ltable.c |  8 +++++---
 lua.h    |  7 ++++++-
 makefile | 11 ++++++-----
 4 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/lapi.c b/lapi.c
index ec13bb74..faf0fa2c 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 1.175 2002/03/04 21:29:41 roberto Exp roberto $
+** $Id: lapi.c,v 1.176 2002/03/07 18:15:10 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -597,7 +597,7 @@ LUA_API int lua_getn (lua_State *L, int index) {
   api_check(L, ttype(t) == LUA_TTABLE);
   value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n"));  /* = t.n */
   if (ttype(value) == LUA_TNUMBER)
-    n = cast(int, nvalue(value));
+    lua_number2int(n, nvalue(value));
   else {
     Node *nd;
     Table *a = hvalue(t);
@@ -618,7 +618,7 @@ LUA_API int lua_getn (lua_State *L, int index) {
         max = nvalue(key(nd));
       nd++;
     }
-    n = cast(int, max);
+    lua_number2int(n, max);
   }
   lua_unlock(L);
   return n;
diff --git a/ltable.c b/ltable.c
index 098109ff..d0feafef 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ltable.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
+** $Id: ltable.c,v 1.101 2002/02/14 21:41:08 roberto Exp roberto $
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 */
@@ -85,7 +85,8 @@ Node *luaH_mainposition (const Table *t, const TObject *key) {
 */
 static int arrayindex (const TObject *key) {
   if (ttype(key) == LUA_TNUMBER) {
-    int k = cast(int, nvalue(key));
+    int k;
+    lua_number2int(k, (nvalue(key)));
     if (cast(lua_Number, k) == nvalue(key) && k >= 1 && !toobig(k))
       return k;
   }
@@ -421,7 +422,8 @@ const TObject *luaH_get (Table *t, const TObject *key) {
   switch (ttype(key)) {
     case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
     case LUA_TNUMBER: {
-      int k = cast(int, nvalue(key));
+      int k;
+      lua_number2int(k, (nvalue(key)));
       if (cast(lua_Number, k) == nvalue(key))  /* is an integer index? */
         return luaH_getnum(t, k);  /* use specialized version */
       /* else go through */
diff --git a/lua.h b/lua.h
index 2fd5ff0a..eb4f06db 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.121 2002/02/14 21:40:13 roberto Exp roberto $
+** $Id: lua.h,v 1.122 2002/03/07 18:15:10 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: info@lua.org
@@ -294,6 +294,11 @@ LUA_API int lua_pushupvalues (lua_State *L);
 #define lua_str2number(s,p)     strtod((s), (p))
 #endif
 
+/* function to convert a lua_Number to int (with any rounding method) */
+#ifndef lua_number2int
+#define lua_number2int(i,n)	((i)=(int)(n))
+#endif
+
 /* }====================================================================== */
 
 
diff --git a/makefile b/makefile
index 4b0423c0..b14b8e3f 100644
--- a/makefile
+++ b/makefile
@@ -1,5 +1,5 @@
 #
-## $Id: makefile,v 1.32 2001/07/24 22:40:08 roberto Exp $
+## $Id: makefile,v 1.34 2002/02/14 21:49:33 roberto Exp roberto $
 ## Makefile
 ## See Copyright Notice in lua.h
 #
@@ -18,11 +18,12 @@
 
 #EXTRA_H=-DLUA_USER_H='"ltests.h"'
 
-CONFIG = -D_POSIX_SOURCE  $(EXTRA_H)
+CONFIG = -D_POSIX_SOURCE  $(EXTRA_H) \
+  -D'lua_number2int(i,d)=__asm__("fldl %1\nfistpl %0":"=m"(i):"m"(d))'
 
 
 # Compilation parameters
-CC = g++
+CC = gcc
 CWARNS = -Wall -pedantic \
 	-Waggregate-return \
 	-Wcast-align \
@@ -146,9 +147,9 @@ ltests.o: ltests.c lua.h lapi.h lobject.h llimits.h lauxlib.h lcode.h \
  luadebug.h ldo.h lfunc.h lmem.h lstring.h lualib.h
 ltm.o: ltm.c lua.h lobject.h llimits.h lstate.h ltm.h luadebug.h \
  lstring.h ltable.h
-lua.o: lua.c lua.h luadebug.h lualib.h
+lua.o: lua.c lua.h lauxlib.h luadebug.h lualib.h
 lundump.o: lundump.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
  luadebug.h lfunc.h lmem.h lopcodes.h lstring.h lundump.h lzio.h
-lvm.o: lvm.c lua.h lapi.h lobject.h llimits.h ldebug.h lstate.h ltm.h \
+lvm.o: lvm.c lua.h ldebug.h lstate.h lobject.h llimits.h ltm.h \
  luadebug.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
 lzio.o: lzio.c lua.h lzio.h
-- 
cgit v1.2.3-55-g6feb