[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua and numbers
- From: ramsdell@... (John D. Ramsdell)
- Date: 10 Apr 2006 08:53:52 -0400
> From: Gavin Wraith <gavin@wra1th.plus.com>
> Subject: Re: Lua and numbers
>
> > My use of "integer patch" name was probably misleading, it really is
> > either "non-FPU optimization
> > patch" or "integer optimization patch".
Yeah, all I want is to avoid using all floating point operations. I
have enclosed the patch I use. You must define LUA_NUMBER_INTEGRAL,
or you'll get the original behavior of LUA_NUMBER as a double.
John
diff -ur olua-5.1/src/lmathlib.c lua-5.1/src/lmathlib.c
--- olua-5.1/src/lmathlib.c 2005-08-26 13:36:32.000000000 -0400
+++ lua-5.1/src/lmathlib.c 2006-04-10 08:36:30.000000000 -0400
@@ -252,8 +252,10 @@
luaL_register(L, LUA_MATHLIBNAME, mathlib);
lua_pushnumber(L, PI);
lua_setfield(L, -2, "pi");
+#if !defined(LUA_NUMBER_INTEGRAL)
lua_pushnumber(L, HUGE_VAL);
lua_setfield(L, -2, "huge");
+#endif
#if defined(LUA_COMPAT_MOD)
lua_getfield(L, -1, "fmod");
lua_setfield(L, -2, "mod");
diff -ur olua-5.1/src/luaconf.h lua-5.1/src/luaconf.h
--- olua-5.1/src/luaconf.h 2006-02-10 12:44:06.000000000 -0500
+++ lua-5.1/src/luaconf.h 2006-04-10 08:43:24.000000000 -0400
@@ -488,14 +488,18 @@
** ===================================================================
*/
+#if defined LUA_NUMBER_INTEGRAL
+#define LUA_NUMBER long
+#else
#define LUA_NUMBER_DOUBLE
#define LUA_NUMBER double
+#endif
/*
@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'
@* over a number.
*/
-#define LUAI_UACNUMBER double
+#define LUAI_UACNUMBER LUA_NUMBER
/*
@@ -505,11 +509,20 @@
@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
@@ lua_str2number converts a string to a number.
*/
+#if defined LUA_NUMBER_INTEGRAL
+#define LUA_NUMBER_SCAN "%ld"
+#define LUA_NUMBER_FMT "%ld"
+#else
#define LUA_NUMBER_SCAN "%lf"
#define LUA_NUMBER_FMT "%.14g"
+#endif
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
+#if defined LUA_NUMBER_INTEGRAL
+#define lua_str2number(s,p) strtol((s), (p), 10)
+#else
#define lua_str2number(s,p) strtod((s), (p))
+#endif
/*
@@ -521,8 +534,14 @@
#define luai_numsub(a,b) ((a)-(b))
#define luai_nummul(a,b) ((a)*(b))
#define luai_numdiv(a,b) ((a)/(b))
+#if defined LUA_NUMBER_INTEGRAL
+#define luai_nummod(a,b) ((a)%(b))
+LUA_NUMBER luai_ipow(LUA_NUMBER, LUA_NUMBER);
+#define luai_numpow(a,b) (luai_ipow(a,b))
+#else
#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
#define luai_numpow(a,b) (pow(a,b))
+#endif
#define luai_numunm(a) (-(a))
#define luai_numeq(a,b) ((a)==(b))
#define luai_numlt(a,b) ((a)<(b))
diff -ur olua-5.1/src/lvm.c lua-5.1/src/lvm.c
--- olua-5.1/src/lvm.c 2006-01-23 14:51:43.000000000 -0500
+++ lua-5.1/src/lvm.c 2006-04-10 08:44:41.000000000 -0400
@@ -27,6 +27,25 @@
#include "lvm.h"
+#if defined LUA_NUMBER_INTEGRAL
+LUA_NUMBER luai_ipow(LUA_NUMBER a, LUA_NUMBER b) {
+ if (b < 0)
+ return 0;
+ else if (b == 0)
+ return 1;
+ else {
+ LUA_NUMBER c = 1;
+ for (;;) {
+ if (b & 1)
+ c *= a;
+ b = b >> 1;
+ if (b == 0)
+ return c;
+ a *= a;
+ }
+ }
+}
+#endif
/* limit for table tag-method chains (to avoid loops) */
#define MAXTAGLOOP 100