summaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-01-10 16:17:39 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-01-10 16:17:39 -0200
commit8ddfe3df29dfff3ac9f75972150bd263cc4e3487 (patch)
tree14d79f1a1653878ef235040935716566acd80c93 /lvm.c
parent6eb68ba57a58679cc69837b03490b12ba0cba8d4 (diff)
downloadlua-8ddfe3df29dfff3ac9f75972150bd263cc4e3487.tar.gz
lua-8ddfe3df29dfff3ac9f75972150bd263cc4e3487.tar.bz2
lua-8ddfe3df29dfff3ac9f75972150bd263cc4e3487.zip
macros for all arithmetic operations over lua_Numbers
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c36
1 files changed, 18 insertions, 18 deletions
diff --git a/lvm.c b/lvm.c
index d8e47233..63e0e88d 100644
--- a/lvm.c
+++ b/lvm.c
@@ -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 }