diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-23 16:01:16 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-23 16:01:16 -0300 |
| commit | 86e8039a72646cd9192fd08a6f1771c90b872ff6 (patch) | |
| tree | e0c68d303aba023fc44d8e9193e239cbf7ae97f9 | |
| parent | 5a04f1851e0d42b4bcbb0af103490bc964e985aa (diff) | |
| download | lua-86e8039a72646cd9192fd08a6f1771c90b872ff6.tar.gz lua-86e8039a72646cd9192fd08a6f1771c90b872ff6.tar.bz2 lua-86e8039a72646cd9192fd08a6f1771c90b872ff6.zip | |
Clock component removed from 'luaL_makeseed'
'clock' can be quite slow on some machines.
| -rw-r--r-- | lauxlib.c | 10 | ||||
| -rw-r--r-- | lmathlib.c | 4 | ||||
| -rw-r--r-- | ltablib.c | 2 | ||||
| -rw-r--r-- | manual/manual.of | 4 |
4 files changed, 9 insertions, 11 deletions
| @@ -1094,8 +1094,8 @@ static void warnfon (void *ud, const char *message, int tocont) { | |||
| 1094 | 1094 | ||
| 1095 | /* | 1095 | /* |
| 1096 | ** A function to compute an unsigned int with some level of | 1096 | ** A function to compute an unsigned int with some level of |
| 1097 | ** randomness. Rely on Address Space Layout Randomization (if present), | 1097 | ** randomness. Rely on Address Space Layout Randomization (if present) |
| 1098 | ** current time, and clock. | 1098 | ** and the current time. |
| 1099 | */ | 1099 | */ |
| 1100 | #if !defined(luai_makeseed) | 1100 | #if !defined(luai_makeseed) |
| 1101 | 1101 | ||
| @@ -1115,18 +1115,16 @@ static void warnfon (void *ud, const char *message, int tocont) { | |||
| 1115 | 1115 | ||
| 1116 | 1116 | ||
| 1117 | static unsigned int luai_makeseed (void) { | 1117 | static unsigned int luai_makeseed (void) { |
| 1118 | unsigned int buff[sof(void*) + sof(clock_t) + sof(time_t)]; | 1118 | unsigned int buff[sof(void*) + sof(time_t)]; |
| 1119 | unsigned int res; | 1119 | unsigned int res; |
| 1120 | unsigned int *b = buff; | 1120 | unsigned int *b = buff; |
| 1121 | clock_t c = clock(); | ||
| 1122 | time_t t = time(NULL); | 1121 | time_t t = time(NULL); |
| 1123 | void *h = buff; | 1122 | void *h = buff; |
| 1124 | addbuff(b, h); /* local variable's address */ | 1123 | addbuff(b, h); /* local variable's address */ |
| 1125 | addbuff(b, c); /* clock */ | ||
| 1126 | addbuff(b, t); /* time */ | 1124 | addbuff(b, t); /* time */ |
| 1127 | res = buff[0]; | 1125 | res = buff[0]; |
| 1128 | for (b = buff + 1; b < buff + sof(buff); b++) | 1126 | for (b = buff + 1; b < buff + sof(buff); b++) |
| 1129 | res += *b; | 1127 | res ^= (res >> 3) + (res << 7) + *b; |
| 1130 | return res; | 1128 | return res; |
| 1131 | } | 1129 | } |
| 1132 | 1130 | ||
| @@ -607,8 +607,8 @@ static int math_randomseed (lua_State *L) { | |||
| 607 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); | 607 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); |
| 608 | lua_Unsigned n1, n2; | 608 | lua_Unsigned n1, n2; |
| 609 | if (lua_isnone(L, 1)) { | 609 | if (lua_isnone(L, 1)) { |
| 610 | n1 = luaL_makeseed(L); | 610 | n1 = luaL_makeseed(L); /* "random" seed */ |
| 611 | n2 = I2UInt(state->s[0]); | 611 | n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */ |
| 612 | } | 612 | } |
| 613 | else { | 613 | else { |
| 614 | n1 = luaL_checkinteger(L, 1); | 614 | n1 = luaL_checkinteger(L, 1); |
| @@ -310,7 +310,7 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { | |||
| 310 | */ | 310 | */ |
| 311 | static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { | 311 | static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { |
| 312 | IdxT r4 = (up - lo) / 4; /* range/4 */ | 312 | IdxT r4 = (up - lo) / 4; /* range/4 */ |
| 313 | IdxT p = rnd % (r4 * 2) + (lo + r4); | 313 | IdxT p = (rnd ^ lo ^ up) % (r4 * 2) + (lo + r4); |
| 314 | lua_assert(lo + r4 <= p && p <= up - r4); | 314 | lua_assert(lo + r4 <= p && p <= up - r4); |
| 315 | return p; | 315 | return p; |
| 316 | } | 316 | } |
diff --git a/manual/manual.of b/manual/manual.of index fdae76f2..446517ec 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
| @@ -5728,8 +5728,8 @@ it does not run it. | |||
| 5728 | @apii{0,0,-} | 5728 | @apii{0,0,-} |
| 5729 | 5729 | ||
| 5730 | Returns a value with a weak attempt for randomness. | 5730 | Returns a value with a weak attempt for randomness. |
| 5731 | (It produces that value based on the current date and time, | 5731 | (It produces that value based on the current date and time |
| 5732 | the current processor time, and the address of an internal variable, | 5732 | and the address of an internal variable, |
| 5733 | in case the machine has Address Space Layout Randomization.) | 5733 | in case the machine has Address Space Layout Randomization.) |
| 5734 | 5734 | ||
| 5735 | } | 5735 | } |
