diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-26 14:10:22 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-26 14:10:22 -0300 |
commit | c98f195eb930422be2829f78696fb4bf79b93677 (patch) | |
tree | 119b0eca4b1b1435f6a73cecb50e368455081c14 | |
parent | 4d696c45b9710e4b3d2eb65a0ebef79766937a4a (diff) | |
download | lua-c98f195eb930422be2829f78696fb4bf79b93677.tar.gz lua-c98f195eb930422be2829f78696fb4bf79b93677.tar.bz2 lua-c98f195eb930422be2829f78696fb4bf79b93677.zip |
function 'luaV_numtointeger' changed to a global macro
'lua_numtointeger' (tricky, small, and useful in several places)
-rw-r--r-- | ltable.c | 21 | ||||
-rw-r--r-- | luaconf.h | 20 | ||||
-rw-r--r-- | lvm.c | 20 | ||||
-rw-r--r-- | lvm.h | 3 |
4 files changed, 35 insertions, 29 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.87 2014/04/15 14:28:20 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.88 2014/04/15 16:32:49 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -67,12 +67,6 @@ | |||
67 | #define hashpointer(t,p) hashmod(t, IntPoint(p)) | 67 | #define hashpointer(t,p) hashmod(t, IntPoint(p)) |
68 | 68 | ||
69 | 69 | ||
70 | /* checks whether a float has a value representable as a lua_Integer | ||
71 | (and does the conversion if so) */ | ||
72 | #define numisinteger(x,i) \ | ||
73 | (((x) == l_floor(x)) && luaV_numtointeger(x, i)) | ||
74 | |||
75 | |||
76 | #define dummynode (&dummynode_) | 70 | #define dummynode (&dummynode_) |
77 | 71 | ||
78 | #define isdummy(n) ((n) == dummynode) | 72 | #define isdummy(n) ((n) == dummynode) |
@@ -84,6 +78,17 @@ static const Node dummynode_ = { | |||
84 | 78 | ||
85 | 79 | ||
86 | /* | 80 | /* |
81 | ** Checks whether a float has a value representable as a lua_Integer | ||
82 | ** (and does the conversion if so) | ||
83 | */ | ||
84 | static int numisinteger (lua_Number x, lua_Integer *p) { | ||
85 | if ((x) == l_floor(x)) /* integral value? */ | ||
86 | return lua_numtointeger(x, p); /* try as an integer */ | ||
87 | else return 0; | ||
88 | } | ||
89 | |||
90 | |||
91 | /* | ||
87 | ** hash for floating-point numbers | 92 | ** hash for floating-point numbers |
88 | */ | 93 | */ |
89 | static Node *hashfloat (const Table *t, lua_Number n) { | 94 | static Node *hashfloat (const Table *t, lua_Number n) { |
@@ -424,7 +429,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | |||
424 | if (luai_numisnan(n)) | 429 | if (luai_numisnan(n)) |
425 | luaG_runerror(L, "table index is NaN"); | 430 | luaG_runerror(L, "table index is NaN"); |
426 | if (numisinteger(n, &k)) { /* index is int? */ | 431 | if (numisinteger(n, &k)) { /* index is int? */ |
427 | setivalue(&aux, k); | 432 | setivalue(&aux, k); |
428 | key = &aux; /* insert it as an integer */ | 433 | key = &aux; /* insert it as an integer */ |
429 | } | 434 | } |
430 | } | 435 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: luaconf.h,v 1.202 2014/05/15 15:24:32 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.203 2014/05/21 15:24:21 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 | */ |
@@ -506,6 +506,24 @@ | |||
506 | 506 | ||
507 | 507 | ||
508 | /* | 508 | /* |
509 | @@ lua_numtointeger converts a float number to an integer, or | ||
510 | ** return 0 if float is not within the range of a lua_Integer. | ||
511 | ** (The comparisons are tricky because of rounding, which can or | ||
512 | ** not occur depending on the relative sizes of floats and integers. | ||
513 | ** The tests here assume a two-complement representation, where | ||
514 | ** MININTEGER always has an exact representation as a float, | ||
515 | ** while if LUA_MAXINTEGER has an exact representation, so does | ||
516 | ** (LUA_MAXINTEGER + 1); otherwise, LUA_MAXINTEGER is equal to | ||
517 | ** (LUA_MAXINTEGER + 1) when converted to a float.) | ||
518 | ** This macro should be used only when 'n' has an integral value. | ||
519 | */ | ||
520 | #define lua_numtointeger(n,p) \ | ||
521 | (((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ | ||
522 | (n) < (LUA_NUMBER)(LUA_MAXINTEGER) + 1) && \ | ||
523 | (*p = (LUA_INTEGER)(n), 1)) | ||
524 | |||
525 | |||
526 | /* | ||
509 | @@ The luai_num* macros define the primitive operations over numbers. | 527 | @@ The luai_num* macros define the primitive operations over numbers. |
510 | ** They should work for any size of floating numbers. | 528 | ** They should work for any size of floating numbers. |
511 | */ | 529 | */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.212 2014/05/20 14:12:59 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.213 2014/05/23 18:32:21 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -80,22 +80,6 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | |||
80 | 80 | ||
81 | 81 | ||
82 | /* | 82 | /* |
83 | ** Check whether a float number is within the range of a lua_Integer. | ||
84 | ** (The comparisons are tricky because of rounding, which can or | ||
85 | ** not occur depending on the relative sizes of floats and integers.) | ||
86 | ** This function should be called only when 'n' has an integral value. | ||
87 | */ | ||
88 | int luaV_numtointeger (lua_Number n, lua_Integer *p) { | ||
89 | if (cast_num(LUA_MININTEGER) <= n && n < (LUA_MAXINTEGER + cast_num(1))) { | ||
90 | *p = cast(lua_Integer, n); | ||
91 | lua_assert(cast_num(*p) == n); | ||
92 | return 1; | ||
93 | } | ||
94 | return 0; /* number is outside integer limits */ | ||
95 | } | ||
96 | |||
97 | |||
98 | /* | ||
99 | ** try to convert a value to an integer, rounding up if 'up' is true | 83 | ** try to convert a value to an integer, rounding up if 'up' is true |
100 | */ | 84 | */ |
101 | static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { | 85 | static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { |
@@ -104,7 +88,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { | |||
104 | if (ttisfloat(obj)) { | 88 | if (ttisfloat(obj)) { |
105 | lua_Number n = fltvalue(obj); | 89 | lua_Number n = fltvalue(obj); |
106 | n = (up ? -l_floor(-n) : l_floor(n)); | 90 | n = (up ? -l_floor(-n) : l_floor(n)); |
107 | return luaV_numtointeger(n, p); | 91 | return lua_numtointeger(n, p); |
108 | } | 92 | } |
109 | else if (ttisinteger(obj)) { | 93 | else if (ttisinteger(obj)) { |
110 | *p = ivalue(obj); | 94 | *p = ivalue(obj); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.h,v 2.29 2014/04/27 14:41:11 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 2.30 2014/05/12 21:22:05 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -29,7 +29,6 @@ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); | |||
29 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); | 29 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); |
30 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); | 30 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); |
31 | LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); | 31 | LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); |
32 | LUAI_FUNC int luaV_numtointeger (lua_Number n, lua_Integer *p); | ||
33 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); | 32 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); |
34 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, | 33 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, |
35 | StkId val); | 34 | StkId val); |