aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-03-27 14:56:10 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-03-27 14:56:10 -0300
commitd12262068d689eacc452a459a021df0ad8f6d46c (patch)
treed3e839cd1ddf3daf5e12c8f472c10a980b7d9d8c /lapi.c
parent0443ad9e288825b6e4441eb11104bcdb4ff4593a (diff)
downloadlua-d12262068d689eacc452a459a021df0ad8f6d46c.tar.gz
lua-d12262068d689eacc452a459a021df0ad8f6d46c.tar.bz2
lua-d12262068d689eacc452a459a021df0ad8f6d46c.zip
Small optimizations in range checks
Checks of the form '1 <= x && x <= M' were rewritten in the form '(unsigned)x - 1 < (unsigned)M', which is usually more efficient. (Other similar checks have similar translations.) Although some compilers do these optimizations, that does not happen for all compilers or all cases.
Diffstat (limited to '')
-rw-r--r--lapi.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/lapi.c b/lapi.c
index 06396ad3..661fdb14 100644
--- a/lapi.c
+++ b/lapi.c
@@ -936,8 +936,8 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
936 api_checknelems(L, 1); 936 api_checknelems(L, 1);
937 o = index2value(L, idx); 937 o = index2value(L, idx);
938 api_check(L, ttisfulluserdata(o), "full userdata expected"); 938 api_check(L, ttisfulluserdata(o), "full userdata expected");
939 if (!(0 < n && n <= uvalue(o)->nuvalue)) 939 if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
940 res = 0; 940 res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
941 else { 941 else {
942 setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); 942 setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1));
943 luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); 943 luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
@@ -1313,7 +1313,8 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val,
1313 switch (ttypetag(fi)) { 1313 switch (ttypetag(fi)) {
1314 case LUA_TCCL: { /* C closure */ 1314 case LUA_TCCL: { /* C closure */
1315 CClosure *f = clCvalue(fi); 1315 CClosure *f = clCvalue(fi);
1316 if (!(1 <= n && n <= f->nupvalues)) return NULL; 1316 if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
1317 return NULL; /* 'n' not in [1, f->nupvalues] */
1317 *val = &f->upvalue[n-1]; 1318 *val = &f->upvalue[n-1];
1318 if (owner) *owner = obj2gco(f); 1319 if (owner) *owner = obj2gco(f);
1319 return ""; 1320 return "";
@@ -1322,7 +1323,8 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val,
1322 LClosure *f = clLvalue(fi); 1323 LClosure *f = clLvalue(fi);
1323 TString *name; 1324 TString *name;
1324 Proto *p = f->p; 1325 Proto *p = f->p;
1325 if (!(1 <= n && n <= p->sizeupvalues)) return NULL; 1326 if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
1327 return NULL; /* 'n' not in [1, p->sizeupvalues] */
1326 *val = f->upvals[n-1]->v; 1328 *val = f->upvals[n-1]->v;
1327 if (owner) *owner = obj2gco(f->upvals[n - 1]); 1329 if (owner) *owner = obj2gco(f->upvals[n - 1]);
1328 name = p->upvalues[n-1].name; 1330 name = p->upvalues[n-1].name;