diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-11-03 13:16:17 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-11-03 13:16:17 -0200 |
commit | f1629217f136c37b58c912692b4d78f4314b4795 (patch) | |
tree | 59ffb5310394c0001b77e3d5b8ce57f8d79e10d1 | |
parent | fdd5f2f7a32535eb8f2fbafbed2d84c0e69392c0 (diff) | |
download | lua-f1629217f136c37b58c912692b4d78f4314b4795.tar.gz lua-f1629217f136c37b58c912692b4d78f4314b4795.tar.bz2 lua-f1629217f136c37b58c912692b4d78f4314b4795.zip |
code for conversion macros moved from luaconf to llimits + 'uint'
renamed to 'unsigned' in those macros
-rw-r--r-- | lapi.c | 6 | ||||
-rw-r--r-- | llimits.h | 91 | ||||
-rw-r--r-- | luaconf.h | 107 |
3 files changed, 101 insertions, 103 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.138 2010/10/25 19:01:37 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.139 2010/10/25 20:31:11 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -353,7 +353,7 @@ LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) { | |||
353 | if (tonumber(o, &n)) { | 353 | if (tonumber(o, &n)) { |
354 | lua_Unsigned res; | 354 | lua_Unsigned res; |
355 | lua_Number num = nvalue(o); | 355 | lua_Number num = nvalue(o); |
356 | lua_number2uint(res, num); | 356 | lua_number2unsigned(res, num); |
357 | if (isnum) *isnum = 1; | 357 | if (isnum) *isnum = 1; |
358 | return res; | 358 | return res; |
359 | } | 359 | } |
@@ -472,7 +472,7 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { | |||
472 | LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { | 472 | LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { |
473 | lua_Number n; | 473 | lua_Number n; |
474 | lua_lock(L); | 474 | lua_lock(L); |
475 | n = lua_uint2number(u); | 475 | n = lua_unsigned2number(u); |
476 | setnvalue(L->top, n); | 476 | setnvalue(L->top, n); |
477 | api_incr_top(L); | 477 | api_incr_top(L); |
478 | lua_unlock(L); | 478 | lua_unlock(L); |
@@ -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 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: luaconf.h,v 1.147 2010/10/29 11:13:21 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.148 2010/10/29 17:52:46 roberto Exp roberto $ |
3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -348,27 +348,13 @@ | |||
348 | 348 | ||
349 | 349 | ||
350 | 350 | ||
351 | /* | ||
352 | ** {================================================================== | ||
353 | ** CHANGE (to smaller values) the following definitions if your system | ||
354 | ** has a small C stack. (Or you may want to change them to larger | ||
355 | ** values if your system has a large C stack and these limits are | ||
356 | ** too rigid for you.) Some of these constants control the size of | ||
357 | ** stack-allocated arrays used by the compiler or the interpreter, while | ||
358 | ** others limit the maximum number of recursive calls that the compiler | ||
359 | ** or the interpreter can perform. Values too large may cause a C stack | ||
360 | ** overflow for some forms of deep constructs. | ||
361 | ** =================================================================== | ||
362 | */ | ||
363 | |||
364 | 351 | ||
365 | /* | 352 | /* |
366 | @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. | 353 | @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. |
354 | ** CHANGE it if it uses too much C-stack space. | ||
367 | */ | 355 | */ |
368 | #define LUAL_BUFFERSIZE BUFSIZ | 356 | #define LUAL_BUFFERSIZE BUFSIZ |
369 | 357 | ||
370 | /* }================================================================== */ | ||
371 | |||
372 | 358 | ||
373 | 359 | ||
374 | 360 | ||
@@ -445,32 +431,21 @@ | |||
445 | #define LUA_UNSIGNED unsigned LUA_INT32 | 431 | #define LUA_UNSIGNED unsigned LUA_INT32 |
446 | 432 | ||
447 | 433 | ||
448 | /* | ||
449 | @@ lua_number2int is a macro to convert lua_Number to int. | ||
450 | @@ lua_number2integer is a macro to convert lua_Number to LUA_INTEGER. | ||
451 | @@ lua_number2uint is a macro to convert a lua_Number to a LUA_UNSIGNED. | ||
452 | @@ lua_uint2number is a macro to convert a LUA_UNSIGNED to a lua_Number. | ||
453 | */ | ||
454 | |||
455 | #if defined(LUA_CORE) /* { */ | 434 | #if defined(LUA_CORE) /* { */ |
456 | 435 | ||
457 | #if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && \ | 436 | #if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */ |
458 | !defined(LUA_NOIEEE754TRICK) /* { */ | ||
459 | 437 | ||
460 | /* On a Microsoft compiler on a Pentium, use assembler to avoid clashes | 438 | /* On a Microsoft compiler on a Pentium, use assembler to avoid clashes |
461 | with a DirectX idiosyncrasy */ | 439 | with a DirectX idiosyncrasy */ |
462 | #if defined(_MSC_VER) && defined(M_IX86) /* { */ | 440 | #if defined(_MSC_VER) && defined(M_IX86) /* { */ |
463 | 441 | ||
464 | #define lua_number2int(i,n) __asm {__asm fld n __asm fistp i} | 442 | #define MS_ASMTRICK |
465 | #define lua_number2integer(i,n) lua_number2int(i, n) | ||
466 | #define lua_number2uint(i,n) \ | ||
467 | {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;} | ||
468 | 443 | ||
469 | #else /* }{ */ | 444 | #else /* }{ */ |
470 | /* the next trick should work on any machine using IEEE754 with | 445 | /* the next definition uses a trick that should work on any machine |
471 | a 32-bit integer type */ | 446 | using IEEE754 with a 32-bit integer type */ |
472 | 447 | ||
473 | union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; | 448 | #define LUA_IEEE754TRICK |
474 | 449 | ||
475 | /* | 450 | /* |
476 | @@ LUA_IEEEENDIAN is the endianness of doubles in your machine | 451 | @@ LUA_IEEEENDIAN is the endianness of doubles in your machine |
@@ -485,77 +460,11 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; | |||
485 | #define LUA_IEEEENDIAN 1 | 460 | #define LUA_IEEEENDIAN 1 |
486 | #endif | 461 | #endif |
487 | 462 | ||
488 | #if !defined(LUA_IEEEENDIAN) /* { */ | ||
489 | #define LUAI_EXTRAIEEE \ | ||
490 | static const union luai_Cast ieeeendian = {-(33.0 + 6755399441055744.0)}; | ||
491 | #define LUA_IEEEENDIAN (ieeeendian.l_p[1] == 33) | ||
492 | #else | ||
493 | #define LUAI_EXTRAIEEE /* empty */ | ||
494 | #endif /* } */ | ||
495 | |||
496 | #define lua_number2int32(i,n,t) \ | ||
497 | { LUAI_EXTRAIEEE \ | ||
498 | volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \ | ||
499 | (i) = (t)u.l_p[LUA_IEEEENDIAN]; } | ||
500 | |||
501 | #define lua_number2int(i,n) lua_number2int32(i, n, int) | ||
502 | #define lua_number2integer(i,n) lua_number2int32(i, n, LUA_INTEGER) | ||
503 | #define lua_number2uint(i,n) lua_number2int32(i, n, LUA_UNSIGNED) | ||
504 | |||
505 | #endif /* } */ | 463 | #endif /* } */ |
506 | 464 | ||
507 | |||
508 | #endif /* } */ | 465 | #endif /* } */ |
509 | 466 | ||
510 | 467 | #endif /* } */ | |
511 | /* the following definitions always work, but may be slow */ | ||
512 | |||
513 | #if !defined(lua_number2int) | ||
514 | #define lua_number2int(i,n) ((i)=(int)(n)) | ||
515 | #endif | ||
516 | |||
517 | #if !defined(lua_number2integer) | ||
518 | #define lua_number2integer(i,n) ((i)=(LUA_INTEGER)(n)) | ||
519 | #endif | ||
520 | |||
521 | #if !defined(lua_number2uint) && (defined(lapi_c) || defined(luaall_c)) /* { */ | ||
522 | /* the following definition assures proper modulo behavior */ | ||
523 | #if defined(LUA_NUMBER_DOUBLE) | ||
524 | #include <math.h> | ||
525 | #define lua_number2uint(i,n) \ | ||
526 | ((i)=(LUA_UNSIGNED)((n) - floor((n)/4294967296.0)*4294967296.0)) | ||
527 | #else | ||
528 | #define lua_number2uint(i,n) ((i)=(LUA_UNSIGNED)(n)) | ||
529 | #endif | ||
530 | #endif /* } */ | ||
531 | |||
532 | #if !defined(lua_uint2number) | ||
533 | /* on several machines, coercion from unsigned to double is slow, | ||
534 | so it may be worth to avoid */ | ||
535 | #define lua_uint2number(u) \ | ||
536 | ((LUA_INT32)(u) < 0 ? (lua_Number)(u) : (lua_Number)(LUA_INT32)(u)) | ||
537 | #endif | ||
538 | |||
539 | #endif /* } */ | ||
540 | |||
541 | |||
542 | /* | ||
543 | @@ luai_hashnum is a macro do hash a lua_Number value into an integer. | ||
544 | @* The hash must be deterministic and give reasonable values for | ||
545 | @* both small and large values (outside the range of integers). | ||
546 | @* It is used only in ltable.c. | ||
547 | */ | ||
548 | |||
549 | #if defined(ltable_c) || defined(luaall_c) | ||
550 | |||
551 | #include <float.h> | ||
552 | #include <math.h> | ||
553 | |||
554 | #define luai_hashnum(i,n) { int e; \ | ||
555 | n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \ | ||
556 | lua_number2int(i, n); i += e; } | ||
557 | |||
558 | #endif /* ltable_c */ | ||
559 | 468 | ||
560 | /* }================================================================== */ | 469 | /* }================================================================== */ |
561 | 470 | ||