new macro to convert double->int

This commit is contained in:
Roberto Ierusalimschy 2002-03-18 15:18:35 -03:00
parent b7ed502dea
commit 0b00e7f1a2
4 changed files with 20 additions and 12 deletions

6
lapi.c
View File

@ -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 ** Lua API
** See Copyright Notice in lua.h ** 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); api_check(L, ttype(t) == LUA_TTABLE);
value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */ value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */
if (ttype(value) == LUA_TNUMBER) if (ttype(value) == LUA_TNUMBER)
n = cast(int, nvalue(value)); lua_number2int(n, nvalue(value));
else { else {
Node *nd; Node *nd;
Table *a = hvalue(t); Table *a = hvalue(t);
@ -618,7 +618,7 @@ LUA_API int lua_getn (lua_State *L, int index) {
max = nvalue(key(nd)); max = nvalue(key(nd));
nd++; nd++;
} }
n = cast(int, max); lua_number2int(n, max);
} }
lua_unlock(L); lua_unlock(L);
return n; return n;

View File

@ -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) ** Lua tables (hash)
** See Copyright Notice in lua.h ** 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) { static int arrayindex (const TObject *key) {
if (ttype(key) == LUA_TNUMBER) { 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)) if (cast(lua_Number, k) == nvalue(key) && k >= 1 && !toobig(k))
return k; return k;
} }
@ -421,7 +422,8 @@ const TObject *luaH_get (Table *t, const TObject *key) {
switch (ttype(key)) { switch (ttype(key)) {
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key)); case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
case LUA_TNUMBER: { 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? */ if (cast(lua_Number, k) == nvalue(key)) /* is an integer index? */
return luaH_getnum(t, k); /* use specialized version */ return luaH_getnum(t, k); /* use specialized version */
/* else go through */ /* else go through */

7
lua.h
View File

@ -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 ** Lua - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: info@lua.org ** 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)) #define lua_str2number(s,p) strtod((s), (p))
#endif #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
/* }====================================================================== */ /* }====================================================================== */

View File

@ -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 ## Makefile
## See Copyright Notice in lua.h ## See Copyright Notice in lua.h
# #
@ -18,11 +18,12 @@
#EXTRA_H=-DLUA_USER_H='"ltests.h"' #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 # Compilation parameters
CC = g++ CC = gcc
CWARNS = -Wall -pedantic \ CWARNS = -Wall -pedantic \
-Waggregate-return \ -Waggregate-return \
-Wcast-align \ -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 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 \ ltm.o: ltm.c lua.h lobject.h llimits.h lstate.h ltm.h luadebug.h \
lstring.h ltable.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 \ 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 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 luadebug.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h
lzio.o: lzio.c lua.h lzio.h lzio.o: lzio.c lua.h lzio.h