commit 3d0b5edfe4df7ec54d6885b6b6ce917faddf6661
parent 8d50a998e381a444829effd5437d52e8ae6c7ee5
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 2 Apr 2018 14:51:40 -0300
using unsigned comparison in 'l_intfitsf' (avoids one comparison)
Diffstat:
M | lvm.c | | | 22 | +++++++++++++--------- |
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/lvm.c b/lvm.c
@@ -1,5 +1,5 @@
/*
-** $Id: lvm.c,v 2.350 2018/03/07 15:55:38 roberto Exp roberto $
+** $Id: lvm.c,v 2.351 2018/03/16 14:21:20 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@@ -50,24 +50,28 @@
/*
-** 'l_intfitsf' checks whether a given integer can be converted to a
-** float without rounding. Used in comparisons.
+** 'l_intfitsf' checks whether a given integer is in the range that
+** can be converted to a float without rounding. Used in comparisons.
*/
+
/* number of bits in the mantissa of a float */
#define NBM (l_mathlim(MANT_DIG))
/*
-** Check whether some integers may not fit in a float, that is, whether
-** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger).
-** (The shifts are done in parts to avoid shifting by more than the size
+** Check whether some integers may not fit in a float, testing whether
+** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.)
+** (The shifts are done in parts, to avoid shifting by more than the size
** of an integer. In a worst case, NBM == 113 for long double and
-** sizeof(integer) == 32.)
+** sizeof(long) == 32.)
*/
#if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \
>> (NBM - (3 * (NBM / 4)))) > 0
-#define l_intfitsf(i) \
- (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM))
+/* limit for integers that fit in a float */
+#define MAXINTFITSF ((lua_Unsigned)1 << NBM)
+
+/* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */
+#define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF))
#else /* all integers fit in a float precisely */