diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-12-23 13:38:28 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-12-23 13:38:28 -0200 |
commit | 0b3f4e254e52f7497b813098c79619a4aaa83c4d (patch) | |
tree | 6f63c4ec134e5406593e56b6f688d539690d2165 | |
parent | 551b076f1c7f9b66eecd8f6b7a12b1bd7a78b967 (diff) | |
download | lua-0b3f4e254e52f7497b813098c79619a4aaa83c4d.tar.gz lua-0b3f4e254e52f7497b813098c79619a4aaa83c4d.tar.bz2 lua-0b3f4e254e52f7497b813098c79619a4aaa83c4d.zip |
more efficient hash for numbers in IEEE754 machines
-rw-r--r-- | llimits.h | 19 |
1 files changed, 10 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llimits.h,v 1.84 2010/11/08 16:33:20 roberto Exp roberto $ | 2 | ** $Id: llimits.h,v 1.85 2010/12/10 13:40:22 roberto Exp roberto $ |
3 | ** Limits, basic types, and some other `installation-dependent' definitions | 3 | ** Limits, basic types, and some other `installation-dependent' definitions |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -174,6 +174,9 @@ typedef lu_int32 Instruction; | |||
174 | ** lua_number2integer is a macro to convert lua_Number to lua_Integer. | 174 | ** lua_number2integer is a macro to convert lua_Number to lua_Integer. |
175 | ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. | 175 | ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned. |
176 | ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. | 176 | ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number. |
177 | ** luai_hashnum is a macro to hash a lua_Number value into an integer. | ||
178 | ** The hash must be deterministic and give reasonable values for | ||
179 | ** both small and large values (outside the range of integers). | ||
177 | */ | 180 | */ |
178 | 181 | ||
179 | #if defined(MS_ASMTRICK) /* { */ | 182 | #if defined(MS_ASMTRICK) /* { */ |
@@ -204,6 +207,10 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; | |||
204 | volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ | 207 | volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ |
205 | (i) = (t)u.l_p[LUA_IEEEENDIAN]; } | 208 | (i) = (t)u.l_p[LUA_IEEEENDIAN]; } |
206 | 209 | ||
210 | #define luai_hashnum(i,n) \ | ||
211 | { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \ | ||
212 | (i) = u.l_p[0] + u.l_p[1]; } /* add double bits for his hash */ | ||
213 | |||
207 | #define lua_number2int(i,n) lua_number2int32(i, n, int) | 214 | #define lua_number2int(i,n) lua_number2int32(i, n, int) |
208 | #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) | 215 | #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer) |
209 | #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned) | 216 | #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]; }; | |||
242 | #endif | 249 | #endif |
243 | 250 | ||
244 | 251 | ||
245 | /* | ||
246 | ** luai_hashnum is a macro do hash a lua_Number value into an integer. | ||
247 | ** The hash must be deterministic and give reasonable values for | ||
248 | ** both small and large values (outside the range of integers). | ||
249 | ** It is used only in ltable.c. | ||
250 | */ | ||
251 | 252 | ||
252 | #if !defined(luai_hashnum) /* { */ | 253 | #if defined(ltable_c) && !defined(luai_hashnum) /* { */ |
253 | 254 | ||
254 | #include <float.h> | 255 | #include <float.h> |
255 | #include <math.h> | 256 | #include <math.h> |
@@ -258,7 +259,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; | |||
258 | n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ | 259 | n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ |
259 | lua_number2int(i, n); i += e; } | 260 | lua_number2int(i, n); i += e; } |
260 | 261 | ||
261 | #endif /* } */ | 262 | #endif /* } */ |
262 | 263 | ||
263 | 264 | ||
264 | 265 | ||