diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-01-10 16:17:39 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-01-10 16:17:39 -0200 |
| commit | 8ddfe3df29dfff3ac9f75972150bd263cc4e3487 (patch) | |
| tree | 14d79f1a1653878ef235040935716566acd80c93 | |
| parent | 6eb68ba57a58679cc69837b03490b12ba0cba8d4 (diff) | |
| download | lua-8ddfe3df29dfff3ac9f75972150bd263cc4e3487.tar.gz lua-8ddfe3df29dfff3ac9f75972150bd263cc4e3487.tar.bz2 lua-8ddfe3df29dfff3ac9f75972150bd263cc4e3487.zip | |
macros for all arithmetic operations over lua_Numbers
| -rw-r--r-- | lcode.c | 4 | ||||
| -rw-r--r-- | lobject.c | 4 | ||||
| -rw-r--r-- | ltable.c | 10 | ||||
| -rw-r--r-- | luaconf.h | 14 | ||||
| -rw-r--r-- | lvm.c | 36 |
5 files changed, 38 insertions, 30 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lcode.c,v 2.7 2004/10/04 19:01:53 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 2.8 2004/12/03 20:35:33 roberto Exp roberto $ |
| 3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -606,7 +606,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { | |||
| 606 | if (op == OPR_MINUS) { | 606 | if (op == OPR_MINUS) { |
| 607 | luaK_exp2val(fs, e); | 607 | luaK_exp2val(fs, e); |
| 608 | if (e->k == VK && ttisnumber(&fs->f->k[e->info])) | 608 | if (e->k == VK && ttisnumber(&fs->f->k[e->info])) |
| 609 | e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info])); | 609 | e->info = luaK_numberK(fs, num_unm(nvalue(&fs->f->k[e->info]))); |
| 610 | else { | 610 | else { |
| 611 | luaK_exp2anyreg(fs, e); | 611 | luaK_exp2anyreg(fs, e); |
| 612 | freeexp(fs, e); | 612 | freeexp(fs, e); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 2.6 2004/11/01 15:06:50 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.7 2004/11/24 19:16:03 roberto Exp roberto $ |
| 3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -73,7 +73,7 @@ int luaO_rawequalObj (const TValue *t1, const TValue *t2) { | |||
| 73 | case LUA_TNIL: | 73 | case LUA_TNIL: |
| 74 | return 1; | 74 | return 1; |
| 75 | case LUA_TNUMBER: | 75 | case LUA_TNUMBER: |
| 76 | return nvalue(t1) == nvalue(t2); | 76 | return num_eq(nvalue(t1), nvalue(t2)); |
| 77 | case LUA_TBOOLEAN: | 77 | case LUA_TBOOLEAN: |
| 78 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ | 78 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ |
| 79 | case LUA_TLIGHTUSERDATA: | 79 | case LUA_TLIGHTUSERDATA: |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 2.13 2005/01/04 15:55:12 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.14 2005/01/05 18:20:51 roberto Exp roberto $ |
| 3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -119,7 +119,7 @@ static int arrayindex (const TValue *key) { | |||
| 119 | lua_Number n = nvalue(key); | 119 | lua_Number n = nvalue(key); |
| 120 | int k; | 120 | int k; |
| 121 | lua_number2int(k, n); | 121 | lua_number2int(k, n); |
| 122 | if (cast(lua_Number, k) == nvalue(key)) | 122 | if (num_eq(cast(lua_Number, k), nvalue(key))) |
| 123 | return k; | 123 | return k; |
| 124 | } | 124 | } |
| 125 | return -1; /* `key' did not match some condition */ | 125 | return -1; /* `key' did not match some condition */ |
| @@ -436,7 +436,7 @@ const TValue *luaH_getnum (Table *t, int key) { | |||
| 436 | lua_Number nk = cast(lua_Number, key); | 436 | lua_Number nk = cast(lua_Number, key); |
| 437 | Node *n = hashnum(t, nk); | 437 | Node *n = hashnum(t, nk); |
| 438 | do { /* check whether `key' is somewhere in the chain */ | 438 | do { /* check whether `key' is somewhere in the chain */ |
| 439 | if (ttisnumber(gkey(n)) && nvalue(gkey(n)) == nk) | 439 | if (ttisnumber(gkey(n)) && num_eq(nvalue(gkey(n)), nk)) |
| 440 | return gval(n); /* that's it */ | 440 | return gval(n); /* that's it */ |
| 441 | else n = gnext(n); | 441 | else n = gnext(n); |
| 442 | } while (n); | 442 | } while (n); |
| @@ -469,7 +469,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
| 469 | case LUA_TNUMBER: { | 469 | case LUA_TNUMBER: { |
| 470 | int k; | 470 | int k; |
| 471 | lua_number2int(k, (nvalue(key))); | 471 | lua_number2int(k, (nvalue(key))); |
| 472 | if (cast(lua_Number, k) == nvalue(key)) /* is an integer index? */ | 472 | if (num_eq(cast(lua_Number, k), nvalue(key))) /* is an integer index? */ |
| 473 | return luaH_getnum(t, k); /* use specialized version */ | 473 | return luaH_getnum(t, k); /* use specialized version */ |
| 474 | /* else go through */ | 474 | /* else go through */ |
| 475 | } | 475 | } |
| @@ -493,7 +493,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { | |||
| 493 | return cast(TValue *, p); | 493 | return cast(TValue *, p); |
| 494 | else { | 494 | else { |
| 495 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); | 495 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); |
| 496 | else if (ttisnumber(key) && nvalue(key) != nvalue(key)) | 496 | else if (ttisnumber(key) && !num_eq(nvalue(key), nvalue(key))) |
| 497 | luaG_runerror(L, "table index is NaN"); | 497 | luaG_runerror(L, "table index is NaN"); |
| 498 | return newkey(L, t, key); | 498 | return newkey(L, t, key); |
| 499 | } | 499 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: luaconf.h,v 1.24 2005/01/07 20:00:33 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.25 2005/01/10 16:31:30 roberto Exp roberto $ |
| 3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -247,9 +247,17 @@ __inline int l_lrint (double flt) | |||
| 247 | #define LUA_UACNUMBER double | 247 | #define LUA_UACNUMBER double |
| 248 | 248 | ||
| 249 | 249 | ||
| 250 | /* primitive `^' operator for numbers */ | 250 | /* primitive operators for numbers */ |
| 251 | #define num_add(a,b) ((a)+(b)) | ||
| 252 | #define num_sub(a,b) ((a)-(b)) | ||
| 253 | #define num_mul(a,b) ((a)*(b)) | ||
| 254 | #define num_div(a,b) ((a)/(b)) | ||
| 255 | #define num_unm(a) (-(a)) | ||
| 256 | #define num_eq(a,b) ((a)==(b)) | ||
| 257 | #define num_lt(a,b) ((a)<(b)) | ||
| 258 | #define num_le(a,b) ((a)<=(b)) | ||
| 251 | #include <math.h> | 259 | #include <math.h> |
| 252 | #define lua_pow(a,b) pow(a,b) | 260 | #define num_pow(a,b) pow(a,b) |
| 253 | 261 | ||
| 254 | 262 | ||
| 255 | 263 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 2.20 2005/01/05 18:20:51 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.21 2005/01/07 20:00:33 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -241,7 +241,7 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { | |||
| 241 | if (ttype(l) != ttype(r)) | 241 | if (ttype(l) != ttype(r)) |
| 242 | return luaG_ordererror(L, l, r); | 242 | return luaG_ordererror(L, l, r); |
| 243 | else if (ttisnumber(l)) | 243 | else if (ttisnumber(l)) |
| 244 | return nvalue(l) < nvalue(r); | 244 | return num_lt(nvalue(l), nvalue(r)); |
| 245 | else if (ttisstring(l)) | 245 | else if (ttisstring(l)) |
| 246 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; | 246 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; |
| 247 | else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) | 247 | 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) { | |||
| 255 | if (ttype(l) != ttype(r)) | 255 | if (ttype(l) != ttype(r)) |
| 256 | return luaG_ordererror(L, l, r); | 256 | return luaG_ordererror(L, l, r); |
| 257 | else if (ttisnumber(l)) | 257 | else if (ttisnumber(l)) |
| 258 | return nvalue(l) <= nvalue(r); | 258 | return num_le(nvalue(l), nvalue(r)); |
| 259 | else if (ttisstring(l)) | 259 | else if (ttisstring(l)) |
| 260 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; | 260 | return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; |
| 261 | else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ | 261 | 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) { | |||
| 271 | lua_assert(ttype(t1) == ttype(t2)); | 271 | lua_assert(ttype(t1) == ttype(t2)); |
| 272 | switch (ttype(t1)) { | 272 | switch (ttype(t1)) { |
| 273 | case LUA_TNIL: return 1; | 273 | case LUA_TNIL: return 1; |
| 274 | case LUA_TNUMBER: return nvalue(t1) == nvalue(t2); | 274 | case LUA_TNUMBER: return num_eq(nvalue(t1), nvalue(t2)); |
| 275 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ | 275 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ |
| 276 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); | 276 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); |
| 277 | case LUA_TUSERDATA: { | 277 | case LUA_TUSERDATA: { |
| @@ -335,11 +335,11 @@ static StkId Arith (lua_State *L, StkId ra, const TValue *rb, | |||
| 335 | if ((b = luaV_tonumber(rb, &tempb)) != NULL && | 335 | if ((b = luaV_tonumber(rb, &tempb)) != NULL && |
| 336 | (c = luaV_tonumber(rc, &tempc)) != NULL) { | 336 | (c = luaV_tonumber(rc, &tempc)) != NULL) { |
| 337 | switch (op) { | 337 | switch (op) { |
| 338 | case TM_ADD: setnvalue(ra, nvalue(b) + nvalue(c)); break; | 338 | case TM_ADD: setnvalue(ra, num_add(nvalue(b), nvalue(c))); break; |
| 339 | case TM_SUB: setnvalue(ra, nvalue(b) - nvalue(c)); break; | 339 | case TM_SUB: setnvalue(ra, num_sub(nvalue(b), nvalue(c))); break; |
| 340 | case TM_MUL: setnvalue(ra, nvalue(b) * nvalue(c)); break; | 340 | case TM_MUL: setnvalue(ra, num_mul(nvalue(b), nvalue(c))); break; |
| 341 | case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break; | 341 | case TM_DIV: setnvalue(ra, num_div(nvalue(b), nvalue(c))); break; |
| 342 | case TM_POW: setnvalue(ra, lua_pow(nvalue(b), nvalue(c))); break; | 342 | case TM_POW: setnvalue(ra, num_pow(nvalue(b), nvalue(c))); break; |
| 343 | default: lua_assert(0); break; | 343 | default: lua_assert(0); break; |
| 344 | } | 344 | } |
| 345 | } | 345 | } |
| @@ -471,7 +471,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
| 471 | TValue *rb = RKB(i); | 471 | TValue *rb = RKB(i); |
| 472 | TValue *rc = RKC(i); | 472 | TValue *rc = RKC(i); |
| 473 | if (ttisnumber(rb) && ttisnumber(rc)) { | 473 | if (ttisnumber(rb) && ttisnumber(rc)) { |
| 474 | setnvalue(ra, nvalue(rb) + nvalue(rc)); | 474 | setnvalue(ra, num_add(nvalue(rb), nvalue(rc))); |
| 475 | } | 475 | } |
| 476 | else | 476 | else |
| 477 | base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/ | 477 | base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/ |
| @@ -481,7 +481,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
| 481 | TValue *rb = RKB(i); | 481 | TValue *rb = RKB(i); |
| 482 | TValue *rc = RKC(i); | 482 | TValue *rc = RKC(i); |
| 483 | if (ttisnumber(rb) && ttisnumber(rc)) { | 483 | if (ttisnumber(rb) && ttisnumber(rc)) { |
| 484 | setnvalue(ra, nvalue(rb) - nvalue(rc)); | 484 | setnvalue(ra, num_sub(nvalue(rb), nvalue(rc))); |
| 485 | } | 485 | } |
| 486 | else | 486 | else |
| 487 | base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/ | 487 | base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/ |
| @@ -491,7 +491,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
| 491 | TValue *rb = RKB(i); | 491 | TValue *rb = RKB(i); |
| 492 | TValue *rc = RKC(i); | 492 | TValue *rc = RKC(i); |
| 493 | if (ttisnumber(rb) && ttisnumber(rc)) { | 493 | if (ttisnumber(rb) && ttisnumber(rc)) { |
| 494 | setnvalue(ra, nvalue(rb) * nvalue(rc)); | 494 | setnvalue(ra, num_mul(nvalue(rb), nvalue(rc))); |
| 495 | } | 495 | } |
| 496 | else | 496 | else |
| 497 | base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/ | 497 | base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/ |
| @@ -501,7 +501,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
| 501 | TValue *rb = RKB(i); | 501 | TValue *rb = RKB(i); |
| 502 | TValue *rc = RKC(i); | 502 | TValue *rc = RKC(i); |
| 503 | if (ttisnumber(rb) && ttisnumber(rc)) { | 503 | if (ttisnumber(rb) && ttisnumber(rc)) { |
| 504 | setnvalue(ra, nvalue(rb) / nvalue(rc)); | 504 | setnvalue(ra, num_div(nvalue(rb), nvalue(rc))); |
| 505 | } | 505 | } |
| 506 | else | 506 | else |
| 507 | base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/ | 507 | base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/ |
| @@ -511,7 +511,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
| 511 | TValue *rb = RKB(i); | 511 | TValue *rb = RKB(i); |
| 512 | TValue *rc = RKC(i); | 512 | TValue *rc = RKC(i); |
| 513 | if (ttisnumber(rb) && ttisnumber(rc)) { | 513 | if (ttisnumber(rb) && ttisnumber(rc)) { |
| 514 | setnvalue(ra, lua_pow(nvalue(rb), nvalue(rc))); | 514 | setnvalue(ra, num_pow(nvalue(rb), nvalue(rc))); |
| 515 | } | 515 | } |
| 516 | else | 516 | else |
| 517 | base = Arith(L, ra, rb, rc, TM_POW, pc); /***/ | 517 | base = Arith(L, ra, rb, rc, TM_POW, pc); /***/ |
| @@ -521,7 +521,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
| 521 | const TValue *rb = RB(i); | 521 | const TValue *rb = RB(i); |
| 522 | TValue temp; | 522 | TValue temp; |
| 523 | if (tonumber(rb, &temp)) { | 523 | if (tonumber(rb, &temp)) { |
| 524 | setnvalue(ra, -nvalue(rb)); | 524 | setnvalue(ra, num_unm(nvalue(rb))); |
| 525 | } | 525 | } |
| 526 | else { | 526 | else { |
| 527 | setnilvalue(&temp); | 527 | setnilvalue(&temp); |
| @@ -657,9 +657,9 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
| 657 | } | 657 | } |
| 658 | case OP_FORLOOP: { | 658 | case OP_FORLOOP: { |
| 659 | lua_Number step = nvalue(ra+2); | 659 | lua_Number step = nvalue(ra+2); |
| 660 | lua_Number idx = nvalue(ra) + step; /* increment index */ | 660 | lua_Number idx = num_add(nvalue(ra), step); /* increment index */ |
| 661 | lua_Number limit = nvalue(ra+1); | 661 | lua_Number limit = nvalue(ra+1); |
| 662 | if (step > 0 ? idx <= limit : idx >= limit) { | 662 | if (step > 0 ? num_le(idx, limit) : num_le(limit, idx)) { |
| 663 | dojump(L, pc, GETARG_sBx(i)); /* jump back */ | 663 | dojump(L, pc, GETARG_sBx(i)); /* jump back */ |
| 664 | setnvalue(ra, idx); /* update internal index... */ | 664 | setnvalue(ra, idx); /* update internal index... */ |
| 665 | setnvalue(ra+3, idx); /* ...and external index */ | 665 | setnvalue(ra+3, idx); /* ...and external index */ |
| @@ -677,7 +677,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
| 677 | luaG_runerror(L, "`for' limit must be a number"); | 677 | luaG_runerror(L, "`for' limit must be a number"); |
| 678 | else if (!tonumber(pstep, ra+2)) | 678 | else if (!tonumber(pstep, ra+2)) |
| 679 | luaG_runerror(L, "`for' step must be a number"); | 679 | luaG_runerror(L, "`for' step must be a number"); |
| 680 | setnvalue(ra, nvalue(ra) - nvalue(pstep)); | 680 | setnvalue(ra, num_sub(nvalue(ra), nvalue(pstep))); |
| 681 | dojump(L, pc, GETARG_sBx(i)); | 681 | dojump(L, pc, GETARG_sBx(i)); |
| 682 | continue; | 682 | continue; |
| 683 | } | 683 | } |
