From 0b3f4e254e52f7497b813098c79619a4aaa83c4d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Dec 2010 13:38:28 -0200 Subject: [PATCH] more efficient hash for numbers in IEEE754 machines --- llimits.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/llimits.h b/llimits.h index 9b7a2544..2c69ce53 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.84 2010/11/08 16:33:20 roberto Exp roberto $ +** $Id: llimits.h,v 1.85 2010/12/10 13:40:22 roberto Exp roberto $ ** Limits, basic types, and some other `installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -174,6 +174,9 @@ typedef lu_int32 Instruction; ** lua_number2integer is a macro to convert lua_Number to lua_Integer. ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. +** luai_hashnum is a macro to hash a lua_Number value into an integer. +** The hash must be deterministic and give reasonable values for +** both small and large values (outside the range of integers). */ #if defined(MS_ASMTRICK) /* { */ @@ -204,6 +207,10 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ (i) = (t)u.l_p[LUA_IEEEENDIAN]; } +#define luai_hashnum(i,n) \ + { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ + (i) = u.l_p[0] + u.l_p[1]; } /* add double bits for his hash */ + #define lua_number2int(i,n) lua_number2int32(i, n, int) #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) @@ -242,14 +249,8 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; #endif -/* -** luai_hashnum is a macro do hash a lua_Number value into an integer. -** The hash must be deterministic and give reasonable values for -** both small and large values (outside the range of integers). -** It is used only in ltable.c. -*/ -#if !defined(luai_hashnum) /* { */ +#if defined(ltable_c) && !defined(luai_hashnum) /* { */ #include #include @@ -258,7 +259,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ lua_number2int(i, n); i += e; } -#endif /* } */ +#endif /* } */