From 6920a7f2e3f8cbdb9c9d43fca720b9dd459be733 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 2 May 2013 09:31:26 -0300 Subject: [PATCH] new macro 'intop' (to perform integer arithmetic on unsigned types) --- lvm.c | 17 ++++++++--------- lvm.h | 4 +++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lvm.c b/lvm.c index 4f32a3e0..fe6f0a78 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.166 2013/04/26 19:51:17 roberto Exp roberto $ +** $Id: lvm.c,v 2.167 2013/04/29 17:12:50 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -327,7 +327,7 @@ lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) { lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) { if (cast_unsigned(y) + 1 <= 1U) { /* special cases: -1 or 0 */ if (y == 0) - luaG_runerror(L, "attempt to divide by zero (in '%%')"); + luaG_runerror(L, "attempt to perform 'n%%0'"); else /* -1 */ return 0; /* avoid overflow with 0x80000... */ } @@ -346,11 +346,10 @@ lua_Integer luaV_pow (lua_Integer x, lua_Integer y) { lua_assert(y >= 0); if (y == 0) return r; for (; y > 1; y >>= 1) { - if (y & 1) - r = cast_integer(cast_unsigned(r) * cast_unsigned(x)); - x = cast_integer(cast_unsigned(x) * cast_unsigned(x)); + if (y & 1) r = intop(*, r, x); + x = intop(*, x, x); } - r = cast_integer(cast_unsigned(r) * cast_unsigned(x)); + r = intop(*, r, x); return r; } @@ -594,7 +593,7 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, cast_integer(cast_unsigned(ib) + cast_unsigned(ic))); + setivalue(ra, intop(+, ib, ic)); } else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { setnvalue(ra, luai_numadd(L, nb, nc)); @@ -607,7 +606,7 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, cast_integer(cast_unsigned(ib) - cast_unsigned(ic))); + setivalue(ra, intop(-, ib, ic)); } else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { setnvalue(ra, luai_numsub(L, nb, nc)); @@ -620,7 +619,7 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, cast_integer(cast_unsigned(ib) * cast_unsigned(ic))); + setivalue(ra, intop(*, ib, ic)); } else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { setnvalue(ra, luai_nummul(L, nb, nc)); diff --git a/lvm.h b/lvm.h index 591c94ec..b352e8ed 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.21 2013/04/26 16:03:50 roberto Exp roberto $ +** $Id: lvm.h,v 2.22 2013/04/29 17:12:50 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -21,6 +21,8 @@ #define tointeger(o,i) \ (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) +#define intop(op,v1,v2) \ + cast_integer(cast_unsigned(v1) op cast_unsigned(v2)) #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)