Change request for 5.2: luai_lnumdiv and luai_lnummod

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Change request for 5.2: luai_lnumdiv and luai_lnummod

John D. Ramsdell-2
The file src/luaconf.h greatly eases the task of adjusting the type of
numerical data used by Lua, however, if LUA_NUMBER is defined to be an
integral type, the interpreter must throw an exception on divide by
zero.  The constant propagation code need not worry about divide by
zero because it checks before it applies a reduction.  Therefore, the
context in which luai_numdiv(a,b) is expanded in the constant
propagation code does not have a lua_State available, while the
interpreter does, and access to that state is needed to signal an
exception.  Please provide a macro for use in each environment.

John

diff -ur lua-5.1.1/src/luaconf.h lua-5.1.1-lnum/src/luaconf.h
--- lua-5.1.1/src/luaconf.h	2006-04-10 14:27:23.000000000 -0400
+++ lua-5.1.1-lnum/src/luaconf.h	2006-07-27 09:37:48.000000000 -0400
@@ -534,6 +534,8 @@
 #define luai_nummul(a,b)	((a)*(b))
 #define luai_numdiv(a,b)	((a)/(b))
 #define luai_nummod(a,b)	((a) - floor((a)/(b))*(b))
+#define luai_lnumdiv(a,b)	(luai_numdiv(a,b))
+#define luai_lnummod(a,b)	(luai_nummod(a,b))
 #define luai_numpow(a,b)	(pow(a,b))
 #define luai_numunm(a)		(-(a))
 #define luai_numeq(a,b)		((a)==(b))
Only in lua-5.1.1-lnum/src: luaconf.h~
diff -ur lua-5.1.1/src/lvm.c lua-5.1.1-lnum/src/lvm.c
--- lua-5.1.1/src/lvm.c	2006-01-23 14:51:43.000000000 -0500
+++ lua-5.1.1-lnum/src/lvm.c	2006-07-27 09:36:27.000000000 -0400
@@ -321,8 +321,8 @@
       case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
       case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
       case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
-      case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
-      case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
+      case TM_DIV: setnvalue(ra, luai_lnumdiv(nb, nc)); break;
+      case TM_MOD: setnvalue(ra, luai_lnummod(nb, nc)); break;
       case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
       case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
       default: lua_assert(0); break;
@@ -479,11 +479,11 @@
         continue;
       }
       case OP_DIV: {
-        arith_op(luai_numdiv, TM_DIV);
+        arith_op(luai_lnumdiv, TM_DIV);
         continue;
       }
       case OP_MOD: {
-        arith_op(luai_nummod, TM_MOD);
+        arith_op(luai_lnummod, TM_MOD);
         continue;
       }
       case OP_POW: {

Reply | Threaded
Open this post in threaded view
|

Re: Change request for 5.2: luai_lnumdiv and luai_lnummod

David Jones-2

On 27 Jul 2006, at 14:49, John D. Ramsdell wrote:

The file src/luaconf.h greatly eases the task of adjusting the type of
numerical data used by Lua, however, if LUA_NUMBER is defined to be an
integral type, the interpreter must throw an exception on divide by
zero.

[to avoid invoking C's undefined behaviour]

  The constant propagation code need not worry about divide by
zero because it checks before it applies a reduction.  Therefore, the
context in which luai_numdiv(a,b) is expanded in the constant
propagation code does not have a lua_State available, while the
interpreter does, and access to that state is needed to signal an
exception.  Please provide a macro for use in each environment.


Seconded.

There are floating point environments where this is useful too.

drj