diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-04-02 14:52:07 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-04-02 14:52:07 -0300 |
commit | 3d0b5edfe4df7ec54d6885b6b6ce917faddf6661 (patch) | |
tree | cb2036b809b69893bdf71b2d67b337df70565a31 | |
parent | 8d50a998e381a444829effd5437d52e8ae6c7ee5 (diff) | |
download | lua-3d0b5edfe4df7ec54d6885b6b6ce917faddf6661.tar.gz lua-3d0b5edfe4df7ec54d6885b6b6ce917faddf6661.tar.bz2 lua-3d0b5edfe4df7ec54d6885b6b6ce917faddf6661.zip |
using unsigned comparison in 'l_intfitsf' (avoids one comparison)
-rw-r--r-- | lvm.c | 22 |
1 files changed, 13 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.350 2018/03/07 15:55:38 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.351 2018/03/16 14:21:20 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 | */ |
@@ -50,24 +50,28 @@ | |||
50 | 50 | ||
51 | 51 | ||
52 | /* | 52 | /* |
53 | ** 'l_intfitsf' checks whether a given integer can be converted to a | 53 | ** 'l_intfitsf' checks whether a given integer is in the range that |
54 | ** float without rounding. Used in comparisons. | 54 | ** can be converted to a float without rounding. Used in comparisons. |
55 | */ | 55 | */ |
56 | |||
56 | /* number of bits in the mantissa of a float */ | 57 | /* number of bits in the mantissa of a float */ |
57 | #define NBM (l_mathlim(MANT_DIG)) | 58 | #define NBM (l_mathlim(MANT_DIG)) |
58 | 59 | ||
59 | /* | 60 | /* |
60 | ** Check whether some integers may not fit in a float, that is, whether | 61 | ** Check whether some integers may not fit in a float, testing whether |
61 | ** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger). | 62 | ** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.) |
62 | ** (The shifts are done in parts to avoid shifting by more than the size | 63 | ** (The shifts are done in parts, to avoid shifting by more than the size |
63 | ** of an integer. In a worst case, NBM == 113 for long double and | 64 | ** of an integer. In a worst case, NBM == 113 for long double and |
64 | ** sizeof(integer) == 32.) | 65 | ** sizeof(long) == 32.) |
65 | */ | 66 | */ |
66 | #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ | 67 | #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ |
67 | >> (NBM - (3 * (NBM / 4)))) > 0 | 68 | >> (NBM - (3 * (NBM / 4)))) > 0 |
68 | 69 | ||
69 | #define l_intfitsf(i) \ | 70 | /* limit for integers that fit in a float */ |
70 | (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM)) | 71 | #define MAXINTFITSF ((lua_Unsigned)1 << NBM) |
72 | |||
73 | /* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */ | ||
74 | #define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF)) | ||
71 | 75 | ||
72 | #else /* all integers fit in a float precisely */ | 76 | #else /* all integers fit in a float precisely */ |
73 | 77 | ||