macros for all arithmetic operations over lua_Numbers

This commit is contained in:
Roberto Ierusalimschy
2005-01-10 16:17:39 -02:00
parent 6eb68ba57a
commit 8ddfe3df29
5 changed files with 38 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.7 2004/10/04 19:01:53 roberto Exp roberto $ ** $Id: lcode.c,v 2.8 2004/12/03 20:35:33 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -606,7 +606,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
if (op == OPR_MINUS) { if (op == OPR_MINUS) {
luaK_exp2val(fs, e); luaK_exp2val(fs, e);
if (e->k == VK && ttisnumber(&fs->f->k[e->info])) if (e->k == VK && ttisnumber(&fs->f->k[e->info]))
e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info])); e->info = luaK_numberK(fs, num_unm(nvalue(&fs->f->k[e->info])));
else { else {
luaK_exp2anyreg(fs, e); luaK_exp2anyreg(fs, e);
freeexp(fs, e); freeexp(fs, e);

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.6 2004/11/01 15:06:50 roberto Exp roberto $ ** $Id: lobject.c,v 2.7 2004/11/24 19:16:03 roberto Exp roberto $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -73,7 +73,7 @@ int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
case LUA_TNIL: case LUA_TNIL:
return 1; return 1;
case LUA_TNUMBER: case LUA_TNUMBER:
return nvalue(t1) == nvalue(t2); return num_eq(nvalue(t1), nvalue(t2));
case LUA_TBOOLEAN: case LUA_TBOOLEAN:
return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
case LUA_TLIGHTUSERDATA: case LUA_TLIGHTUSERDATA:

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.13 2005/01/04 15:55:12 roberto Exp roberto $ ** $Id: ltable.c,v 2.14 2005/01/05 18:20:51 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -119,7 +119,7 @@ static int arrayindex (const TValue *key) {
lua_Number n = nvalue(key); lua_Number n = nvalue(key);
int k; int k;
lua_number2int(k, n); lua_number2int(k, n);
if (cast(lua_Number, k) == nvalue(key)) if (num_eq(cast(lua_Number, k), nvalue(key)))
return k; return k;
} }
return -1; /* `key' did not match some condition */ return -1; /* `key' did not match some condition */
@@ -436,7 +436,7 @@ const TValue *luaH_getnum (Table *t, int key) {
lua_Number nk = cast(lua_Number, key); lua_Number nk = cast(lua_Number, key);
Node *n = hashnum(t, nk); Node *n = hashnum(t, nk);
do { /* check whether `key' is somewhere in the chain */ do { /* check whether `key' is somewhere in the chain */
if (ttisnumber(gkey(n)) && nvalue(gkey(n)) == nk) if (ttisnumber(gkey(n)) && num_eq(nvalue(gkey(n)), nk))
return gval(n); /* that's it */ return gval(n); /* that's it */
else n = gnext(n); else n = gnext(n);
} while (n); } while (n);
@@ -469,7 +469,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
case LUA_TNUMBER: { case LUA_TNUMBER: {
int k; int k;
lua_number2int(k, (nvalue(key))); lua_number2int(k, (nvalue(key)));
if (cast(lua_Number, k) == nvalue(key)) /* is an integer index? */ if (num_eq(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 */
} }
@@ -493,7 +493,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
return cast(TValue *, p); return cast(TValue *, p);
else { else {
if (ttisnil(key)) luaG_runerror(L, "table index is nil"); if (ttisnil(key)) luaG_runerror(L, "table index is nil");
else if (ttisnumber(key) && nvalue(key) != nvalue(key)) else if (ttisnumber(key) && !num_eq(nvalue(key), nvalue(key)))
luaG_runerror(L, "table index is NaN"); luaG_runerror(L, "table index is NaN");
return newkey(L, t, key); return newkey(L, t, key);
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: luaconf.h,v 1.24 2005/01/07 20:00:33 roberto Exp roberto $ ** $Id: luaconf.h,v 1.25 2005/01/10 16:31:30 roberto Exp roberto $
** Configuration file for Lua ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -247,9 +247,17 @@ __inline int l_lrint (double flt)
#define LUA_UACNUMBER double #define LUA_UACNUMBER double
/* primitive `^' operator for numbers */ /* primitive operators for numbers */
#define num_add(a,b) ((a)+(b))
#define num_sub(a,b) ((a)-(b))
#define num_mul(a,b) ((a)*(b))
#define num_div(a,b) ((a)/(b))
#define num_unm(a) (-(a))
#define num_eq(a,b) ((a)==(b))
#define num_lt(a,b) ((a)<(b))
#define num_le(a,b) ((a)<=(b))
#include <math.h> #include <math.h>
#define lua_pow(a,b) pow(a,b) #define num_pow(a,b) pow(a,b)

36
lvm.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.20 2005/01/05 18:20:51 roberto Exp roberto $ ** $Id: lvm.c,v 2.21 2005/01/07 20:00:33 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -241,7 +241,7 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
if (ttype(l) != ttype(r)) if (ttype(l) != ttype(r))
return luaG_ordererror(L, l, r); return luaG_ordererror(L, l, r);
else if (ttisnumber(l)) else if (ttisnumber(l))
return nvalue(l) < nvalue(r); return num_lt(nvalue(l), nvalue(r));
else if (ttisstring(l)) else if (ttisstring(l))
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
@@ -255,7 +255,7 @@ static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
if (ttype(l) != ttype(r)) if (ttype(l) != ttype(r))
return luaG_ordererror(L, l, r); return luaG_ordererror(L, l, r);
else if (ttisnumber(l)) else if (ttisnumber(l))
return nvalue(l) <= nvalue(r); return num_le(nvalue(l), nvalue(r));
else if (ttisstring(l)) else if (ttisstring(l))
return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
@@ -271,7 +271,7 @@ int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
lua_assert(ttype(t1) == ttype(t2)); lua_assert(ttype(t1) == ttype(t2));
switch (ttype(t1)) { switch (ttype(t1)) {
case LUA_TNIL: return 1; case LUA_TNIL: return 1;
case LUA_TNUMBER: return nvalue(t1) == nvalue(t2); case LUA_TNUMBER: return num_eq(nvalue(t1), nvalue(t2));
case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
case LUA_TUSERDATA: { case LUA_TUSERDATA: {
@@ -335,11 +335,11 @@ static StkId Arith (lua_State *L, StkId ra, const TValue *rb,
if ((b = luaV_tonumber(rb, &tempb)) != NULL && if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
(c = luaV_tonumber(rc, &tempc)) != NULL) { (c = luaV_tonumber(rc, &tempc)) != NULL) {
switch (op) { switch (op) {
case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break; case TM_ADD: setnvalue(ra, num_add(nvalue(b), nvalue(c))); break;
case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break; case TM_SUB: setnvalue(ra, num_sub(nvalue(b), nvalue(c))); break;
case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break; case TM_MUL: setnvalue(ra, num_mul(nvalue(b), nvalue(c))); break;
case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break; case TM_DIV: setnvalue(ra, num_div(nvalue(b), nvalue(c))); break;
case TM_POW: setnvalue(ra, lua_pow(nvalue(b), nvalue(c))); break; case TM_POW: setnvalue(ra, num_pow(nvalue(b), nvalue(c))); break;
default: lua_assert(0); break; default: lua_assert(0); break;
} }
} }
@@ -471,7 +471,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RKB(i); TValue *rb = RKB(i);
TValue *rc = RKC(i); TValue *rc = RKC(i);
if (ttisnumber(rb) && ttisnumber(rc)) { if (ttisnumber(rb) && ttisnumber(rc)) {
setnvalue(ra, nvalue(rb) + nvalue(rc)); setnvalue(ra, num_add(nvalue(rb), nvalue(rc)));
} }
else else
base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/ base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/
@@ -481,7 +481,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RKB(i); TValue *rb = RKB(i);
TValue *rc = RKC(i); TValue *rc = RKC(i);
if (ttisnumber(rb) && ttisnumber(rc)) { if (ttisnumber(rb) && ttisnumber(rc)) {
setnvalue(ra, nvalue(rb) - nvalue(rc)); setnvalue(ra, num_sub(nvalue(rb), nvalue(rc)));
} }
else else
base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/ base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/
@@ -491,7 +491,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RKB(i); TValue *rb = RKB(i);
TValue *rc = RKC(i); TValue *rc = RKC(i);
if (ttisnumber(rb) && ttisnumber(rc)) { if (ttisnumber(rb) && ttisnumber(rc)) {
setnvalue(ra, nvalue(rb) * nvalue(rc)); setnvalue(ra, num_mul(nvalue(rb), nvalue(rc)));
} }
else else
base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/ base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/
@@ -501,7 +501,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RKB(i); TValue *rb = RKB(i);
TValue *rc = RKC(i); TValue *rc = RKC(i);
if (ttisnumber(rb) && ttisnumber(rc)) { if (ttisnumber(rb) && ttisnumber(rc)) {
setnvalue(ra, nvalue(rb) / nvalue(rc)); setnvalue(ra, num_div(nvalue(rb), nvalue(rc)));
} }
else else
base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/ base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/
@@ -511,7 +511,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
TValue *rb = RKB(i); TValue *rb = RKB(i);
TValue *rc = RKC(i); TValue *rc = RKC(i);
if (ttisnumber(rb) && ttisnumber(rc)) { if (ttisnumber(rb) && ttisnumber(rc)) {
setnvalue(ra, lua_pow(nvalue(rb), nvalue(rc))); setnvalue(ra, num_pow(nvalue(rb), nvalue(rc)));
} }
else else
base = Arith(L, ra, rb, rc, TM_POW, pc); /***/ base = Arith(L, ra, rb, rc, TM_POW, pc); /***/
@@ -521,7 +521,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
const TValue *rb = RB(i); const TValue *rb = RB(i);
TValue temp; TValue temp;
if (tonumber(rb, &temp)) { if (tonumber(rb, &temp)) {
setnvalue(ra, -nvalue(rb)); setnvalue(ra, num_unm(nvalue(rb)));
} }
else { else {
setnilvalue(&temp); setnilvalue(&temp);
@@ -657,9 +657,9 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
} }
case OP_FORLOOP: { case OP_FORLOOP: {
lua_Number step = nvalue(ra+2); lua_Number step = nvalue(ra+2);
lua_Number idx = nvalue(ra) + step; /* increment index */ lua_Number idx = num_add(nvalue(ra), step); /* increment index */
lua_Number limit = nvalue(ra+1); lua_Number limit = nvalue(ra+1);
if (step > 0 ? idx <= limit : idx >= limit) { if (step > 0 ? num_le(idx, limit) : num_le(limit, idx)) {
dojump(L, pc, GETARG_sBx(i)); /* jump back */ dojump(L, pc, GETARG_sBx(i)); /* jump back */
setnvalue(ra, idx); /* update internal index... */ setnvalue(ra, idx); /* update internal index... */
setnvalue(ra+3, idx); /* ...and external index */ setnvalue(ra+3, idx); /* ...and external index */
@@ -677,7 +677,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
luaG_runerror(L, "`for' limit must be a number"); luaG_runerror(L, "`for' limit must be a number");
else if (!tonumber(pstep, ra+2)) else if (!tonumber(pstep, ra+2))
luaG_runerror(L, "`for' step must be a number"); luaG_runerror(L, "`for' step must be a number");
setnvalue(ra, nvalue(ra) - nvalue(pstep)); setnvalue(ra, num_sub(nvalue(ra), nvalue(pstep)));
dojump(L, pc, GETARG_sBx(i)); dojump(L, pc, GETARG_sBx(i));
continue; continue;
} }