diff options
Diffstat (limited to 'llimits.h')
-rw-r--r-- | llimits.h | 91 |
1 files changed, 90 insertions, 1 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llimits.h,v 1.81 2010/05/24 19:29:46 roberto Exp roberto $ | 2 | ** $Id: llimits.h,v 1.82 2010/05/31 16:08:55 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 | */ |
@@ -168,6 +168,95 @@ typedef lu_int32 Instruction; | |||
168 | #define luai_userstateyield(L,n) ((void)L) | 168 | #define luai_userstateyield(L,n) ((void)L) |
169 | #endif | 169 | #endif |
170 | 170 | ||
171 | /* | ||
172 | ** lua_number2int is a macro to convert lua_Number to int. | ||
173 | ** lua_number2integer is a macro to convert lua_Number to LUA_INTEGER. | ||
174 | ** lua_number2unsigned is a macro to convert a lua_Number to a LUA_UNSIGNED. | ||
175 | ** lua_unsigned2number is a macro to convert a LUA_UNSIGNED to a lua_Number. | ||
176 | */ | ||
177 | |||
178 | #if defined(MS_ASMTRICK) /* { */ | ||
179 | /* trick with Microsoft assembler for X86 */ | ||
180 | |||
181 | #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} | ||
182 | #define lua_number2integer(i,n) lua_number2int(i, n) | ||
183 | #define lua_number2unsigned(i,n) \ | ||
184 | {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} | ||
185 | |||
186 | |||
187 | #elif defined(LUA_IEEE754TRICK) /* }{ */ | ||
188 | /* the next trick should work on any machine using IEEE754 with | ||
189 | a 32-bit integer type */ | ||
190 | |||
191 | union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; | ||
192 | |||
193 | #if !defined(LUA_IEEEENDIAN) /* { */ | ||
194 | #define LUAI_EXTRAIEEE \ | ||
195 | static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; | ||
196 | #define LUA_IEEEENDIAN (ieeeendian.l_p[1] == 33) | ||
197 | #else | ||
198 | #define LUAI_EXTRAIEEE /* empty */ | ||
199 | #endif /* } */ | ||
200 | |||
201 | #define lua_number2int32(i,n,t) \ | ||
202 | { LUAI_EXTRAIEEE \ | ||
203 | volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ | ||
204 | (i) = (t)u.l_p[LUA_IEEEENDIAN]; } | ||
205 | |||
206 | #define lua_number2int(i,n) lua_number2int32(i, n, int) | ||
207 | #define lua_number2integer(i,n) lua_number2int32(i, n, LUA_INTEGER) | ||
208 | #define lua_number2unsigned(i,n) lua_number2int32(i, n, LUA_UNSIGNED) | ||
209 | |||
210 | #endif /* } */ | ||
211 | |||
212 | |||
213 | /* the following definitions always work, but may be slow */ | ||
214 | |||
215 | #if !defined(lua_number2int) | ||
216 | #define lua_number2int(i,n) ((i)=(int)(n)) | ||
217 | #endif | ||
218 | |||
219 | #if !defined(lua_number2integer) | ||
220 | #define lua_number2integer(i,n) ((i)=(LUA_INTEGER)(n)) | ||
221 | #endif | ||
222 | |||
223 | #if !defined(lua_number2unsigned) /* { */ | ||
224 | /* the following definition assures proper modulo behavior */ | ||
225 | #if defined(LUA_NUMBER_DOUBLE) | ||
226 | #include <math.h> | ||
227 | #define lua_number2unsigned(i,n) \ | ||
228 | ((i)=(LUA_UNSIGNED)((n) - floor((n)/4294967296.0)*4294967296.0)) | ||
229 | #else | ||
230 | #define lua_number2unsigned(i,n) ((i)=(LUA_UNSIGNED)(n)) | ||
231 | #endif | ||
232 | #endif /* } */ | ||
233 | |||
234 | |||
235 | #if !defined(lua_unsigned2number) | ||
236 | /* on several machines, coercion from unsigned to double is slow, | ||
237 | so it may be worth to avoid */ | ||
238 | #define lua_unsigned2number(u) \ | ||
239 | ((LUA_INT32)(u) < 0 ? (lua_Number)(u) : (lua_Number)(LUA_INT32)(u)) | ||
240 | #endif | ||
241 | |||
242 | |||
243 | /* | ||
244 | ** luai_hashnum is a macro do hash a lua_Number value into an integer. | ||
245 | ** The hash must be deterministic and give reasonable values for | ||
246 | ** both small and large values (outside the range of integers). | ||
247 | ** It is used only in ltable.c. | ||
248 | */ | ||
249 | |||
250 | #if !defined(luai_hashnum) /* { */ | ||
251 | |||
252 | #include <float.h> | ||
253 | #include <math.h> | ||
254 | |||
255 | #define luai_hashnum(i,n) { int e; \ | ||
256 | n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ | ||
257 | lua_number2int(i, n); i += e; } | ||
258 | |||
259 | #endif /* } */ | ||
171 | 260 | ||
172 | 261 | ||
173 | 262 | ||