From 8ddfe3df29dfff3ac9f75972150bd263cc4e3487 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Jan 2005 16:17:39 -0200 Subject: [PATCH] macros for all arithmetic operations over lua_Numbers --- lcode.c | 4 ++-- lobject.c | 4 ++-- ltable.c | 10 +++++----- luaconf.h | 14 +++++++++++--- lvm.c | 36 ++++++++++++++++++------------------ 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/lcode.c b/lcode.c index d7ba78db..d2f05a8d 100644 --- a/lcode.c +++ b/lcode.c @@ -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 ** See Copyright Notice in lua.h */ @@ -606,7 +606,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { if (op == OPR_MINUS) { luaK_exp2val(fs, e); 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 { luaK_exp2anyreg(fs, e); freeexp(fs, e); diff --git a/lobject.c b/lobject.c index 3ad1cf98..65e3153d 100644 --- a/lobject.c +++ b/lobject.c @@ -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 ** See Copyright Notice in lua.h */ @@ -73,7 +73,7 @@ int luaO_rawequalObj (const TValue *t1, const TValue *t2) { case LUA_TNIL: return 1; case LUA_TNUMBER: - return nvalue(t1) == nvalue(t2); + return num_eq(nvalue(t1), nvalue(t2)); case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ case LUA_TLIGHTUSERDATA: diff --git a/ltable.c b/ltable.c index a3a169b7..361facc8 100644 --- a/ltable.c +++ b/ltable.c @@ -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) ** See Copyright Notice in lua.h */ @@ -119,7 +119,7 @@ static int arrayindex (const TValue *key) { lua_Number n = nvalue(key); int k; lua_number2int(k, n); - if (cast(lua_Number, k) == nvalue(key)) + if (num_eq(cast(lua_Number, k), nvalue(key))) return k; } 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); Node *n = hashnum(t, nk); 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 */ else n = gnext(n); } while (n); @@ -469,7 +469,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { case LUA_TNUMBER: { int k; 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 */ /* else go through */ } @@ -493,7 +493,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { return cast(TValue *, p); else { 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"); return newkey(L, t, key); } diff --git a/luaconf.h b/luaconf.h index 2bccf671..64747364 100644 --- a/luaconf.h +++ b/luaconf.h @@ -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 ** See Copyright Notice in lua.h */ @@ -247,9 +247,17 @@ __inline int l_lrint (double flt) #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 -#define lua_pow(a,b) pow(a,b) +#define num_pow(a,b) pow(a,b) diff --git a/lvm.c b/lvm.c index d8e47233..63e0e88d 100644 --- a/lvm.c +++ b/lvm.c @@ -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 ** 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)) return luaG_ordererror(L, l, r); else if (ttisnumber(l)) - return nvalue(l) < nvalue(r); + return num_lt(nvalue(l), nvalue(r)); else if (ttisstring(l)) return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; 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)) return luaG_ordererror(L, l, r); else if (ttisnumber(l)) - return nvalue(l) <= nvalue(r); + return num_le(nvalue(l), nvalue(r)); else if (ttisstring(l)) return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; 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)); switch (ttype(t1)) { 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_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); 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 && (c = luaV_tonumber(rc, &tempc)) != NULL) { switch (op) { - case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break; - case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break; - case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break; - case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break; - case TM_POW: setnvalue(ra, lua_pow(nvalue(b), nvalue(c))); break; + case TM_ADD: setnvalue(ra, num_add(nvalue(b), nvalue(c))); break; + case TM_SUB: setnvalue(ra, num_sub(nvalue(b), nvalue(c))); break; + case TM_MUL: setnvalue(ra, num_mul(nvalue(b), nvalue(c))); break; + case TM_DIV: setnvalue(ra, num_div(nvalue(b), nvalue(c))); break; + case TM_POW: setnvalue(ra, num_pow(nvalue(b), nvalue(c))); break; default: lua_assert(0); break; } } @@ -471,7 +471,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { TValue *rb = RKB(i); TValue *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, nvalue(rb) + nvalue(rc)); + setnvalue(ra, num_add(nvalue(rb), nvalue(rc))); } else 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 *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, nvalue(rb) - nvalue(rc)); + setnvalue(ra, num_sub(nvalue(rb), nvalue(rc))); } else 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 *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, nvalue(rb) * nvalue(rc)); + setnvalue(ra, num_mul(nvalue(rb), nvalue(rc))); } else 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 *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, nvalue(rb) / nvalue(rc)); + setnvalue(ra, num_div(nvalue(rb), nvalue(rc))); } else 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 *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, lua_pow(nvalue(rb), nvalue(rc))); + setnvalue(ra, num_pow(nvalue(rb), nvalue(rc))); } else 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); TValue temp; if (tonumber(rb, &temp)) { - setnvalue(ra, -nvalue(rb)); + setnvalue(ra, num_unm(nvalue(rb))); } else { setnilvalue(&temp); @@ -657,9 +657,9 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { } case OP_FORLOOP: { 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); - 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 */ setnvalue(ra, idx); /* update internal 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"); else if (!tonumber(pstep, ra+2)) 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)); continue; }