diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-05-20 15:19:11 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-05-20 15:19:11 -0300 |
| commit | 6dc20ff293239efd59ec8be7d14a31af084e28d5 (patch) | |
| tree | e1d1da3973ca6c412b615e911819ed391ed81685 | |
| parent | 99391e24ea25641b0cf419334229452cef9e8581 (diff) | |
| download | lua-6dc20ff293239efd59ec8be7d14a31af084e28d5.tar.gz lua-6dc20ff293239efd59ec8be7d14a31af084e28d5.tar.bz2 lua-6dc20ff293239efd59ec8be7d14a31af084e28d5.zip | |
'l <= r' for numbers has its own function, instead of using
'not (r < l)' (seems to be slightly more efficient)
Diffstat (limited to '')
| -rw-r--r-- | lvm.c | 44 |
1 files changed, 30 insertions, 14 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 2.240 2015/04/29 18:27:16 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.241 2015/05/20 16:22:55 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 | */ |
| @@ -286,27 +286,43 @@ static int LEintfloat (lua_Integer i, lua_Number f, int neg) { | |||
| 286 | 286 | ||
| 287 | 287 | ||
| 288 | /* | 288 | /* |
| 289 | ** Return 'l < r', for numbers. 'neg' means result will be negated | 289 | ** Return 'l < r', for numbers. |
| 290 | ** (that is, comparison is based on r <= l <--> not (l < r)). | ||
| 291 | ** In that case, comparisons with NaN must result in false after | ||
| 292 | ** being negated (so negate again the comparison). | ||
| 293 | */ | 290 | */ |
| 294 | static int LTnum (const TValue *l, const TValue *r, int neg) { | 291 | static int LTnum (const TValue *l, const TValue *r) { |
| 295 | if (ttisinteger(l)) { | 292 | if (ttisinteger(l)) { |
| 296 | lua_Integer li = ivalue(l); | 293 | lua_Integer li = ivalue(l); |
| 297 | if (ttisinteger(r)) | 294 | if (ttisinteger(r)) |
| 298 | return li < ivalue(r); /* both are integers */ | 295 | return li < ivalue(r); /* both are integers */ |
| 299 | else /* 'l' is int and 'r' is float */ | 296 | else /* 'l' is int and 'r' is float */ |
| 300 | return LTintfloat(li, fltvalue(r), neg); /* l < r ? */ | 297 | return LTintfloat(li, fltvalue(r), 0); /* l < r ? */ |
| 301 | } | 298 | } |
| 302 | else { | 299 | else { |
| 303 | lua_Number lf = fltvalue(l); /* 'l' must be float */ | 300 | lua_Number lf = fltvalue(l); /* 'l' must be float */ |
| 304 | if (ttisfloat(r)) { /* both are float */ | 301 | if (ttisfloat(r)) |
| 305 | lua_Number rf = fltvalue(r); | 302 | return luai_numlt(lf, fltvalue(r)); /* both are float */ |
| 306 | return (neg ? !luai_numle(rf, lf) : luai_numlt(lf, rf)); | 303 | else /* 'r' is int and 'l' is float */ |
| 307 | } | 304 | return !LEintfloat(ivalue(r), lf, 1); /* not (r <= l) ? */ |
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | |||
| 309 | /* | ||
| 310 | ** Return 'l <= r', for numbers. | ||
| 311 | */ | ||
| 312 | static int LEnum (const TValue *l, const TValue *r) { | ||
| 313 | if (ttisinteger(l)) { | ||
| 314 | lua_Integer li = ivalue(l); | ||
| 315 | if (ttisinteger(r)) | ||
| 316 | return li <= ivalue(r); /* both are integers */ | ||
| 317 | else /* 'l' is int and 'r' is float */ | ||
| 318 | return LEintfloat(li, fltvalue(r), 0); /* l <= r ? */ | ||
| 319 | } | ||
| 320 | else { | ||
| 321 | lua_Number lf = fltvalue(l); /* 'l' must be float */ | ||
| 322 | if (ttisfloat(r)) | ||
| 323 | return luai_numle(lf, fltvalue(r)); /* both are float */ | ||
| 308 | else /* 'r' is int and 'l' is float */ | 324 | else /* 'r' is int and 'l' is float */ |
| 309 | return !LEintfloat(ivalue(r), lf, !neg); /* not (r <= l) ? */ | 325 | return !LTintfloat(ivalue(r), lf, 1); /* not (r < l) ? */ |
| 310 | } | 326 | } |
| 311 | } | 327 | } |
| 312 | 328 | ||
| @@ -317,7 +333,7 @@ static int LTnum (const TValue *l, const TValue *r, int neg) { | |||
| 317 | int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { | 333 | int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { |
| 318 | int res; | 334 | int res; |
| 319 | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ | 335 | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ |
| 320 | return LTnum(l, r, 0); | 336 | return LTnum(l, r); |
| 321 | else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ | 337 | else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ |
| 322 | return l_strcmp(tsvalue(l), tsvalue(r)) < 0; | 338 | return l_strcmp(tsvalue(l), tsvalue(r)) < 0; |
| 323 | else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ | 339 | else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ |
| @@ -337,7 +353,7 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { | |||
| 337 | int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { | 353 | int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { |
| 338 | int res; | 354 | int res; |
| 339 | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ | 355 | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ |
| 340 | return !LTnum(r, l, 1); | 356 | return LEnum(l, r); |
| 341 | else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ | 357 | else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ |
| 342 | return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; | 358 | return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; |
| 343 | else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* try 'le' */ | 359 | else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* try 'le' */ |
