Clock component removed from 'luaL_makeseed'
'clock' can be quite slow on some machines.
This commit is contained in:
10
lauxlib.c
10
lauxlib.c
@@ -1094,8 +1094,8 @@ static void warnfon (void *ud, const char *message, int tocont) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** A function to compute an unsigned int with some level of
|
** A function to compute an unsigned int with some level of
|
||||||
** randomness. Rely on Address Space Layout Randomization (if present),
|
** randomness. Rely on Address Space Layout Randomization (if present)
|
||||||
** current time, and clock.
|
** and the current time.
|
||||||
*/
|
*/
|
||||||
#if !defined(luai_makeseed)
|
#if !defined(luai_makeseed)
|
||||||
|
|
||||||
@@ -1115,18 +1115,16 @@ static void warnfon (void *ud, const char *message, int tocont) {
|
|||||||
|
|
||||||
|
|
||||||
static unsigned int luai_makeseed (void) {
|
static unsigned int luai_makeseed (void) {
|
||||||
unsigned int buff[sof(void*) + sof(clock_t) + sof(time_t)];
|
unsigned int buff[sof(void*) + sof(time_t)];
|
||||||
unsigned int res;
|
unsigned int res;
|
||||||
unsigned int *b = buff;
|
unsigned int *b = buff;
|
||||||
clock_t c = clock();
|
|
||||||
time_t t = time(NULL);
|
time_t t = time(NULL);
|
||||||
void *h = buff;
|
void *h = buff;
|
||||||
addbuff(b, h); /* local variable's address */
|
addbuff(b, h); /* local variable's address */
|
||||||
addbuff(b, c); /* clock */
|
|
||||||
addbuff(b, t); /* time */
|
addbuff(b, t); /* time */
|
||||||
res = buff[0];
|
res = buff[0];
|
||||||
for (b = buff + 1; b < buff + sof(buff); b++)
|
for (b = buff + 1; b < buff + sof(buff); b++)
|
||||||
res += *b;
|
res ^= (res >> 3) + (res << 7) + *b;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -607,8 +607,8 @@ static int math_randomseed (lua_State *L) {
|
|||||||
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
|
RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
lua_Unsigned n1, n2;
|
lua_Unsigned n1, n2;
|
||||||
if (lua_isnone(L, 1)) {
|
if (lua_isnone(L, 1)) {
|
||||||
n1 = luaL_makeseed(L);
|
n1 = luaL_makeseed(L); /* "random" seed */
|
||||||
n2 = I2UInt(state->s[0]);
|
n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
n1 = luaL_checkinteger(L, 1);
|
n1 = luaL_checkinteger(L, 1);
|
||||||
|
|||||||
@@ -310,7 +310,7 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
|
|||||||
*/
|
*/
|
||||||
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
||||||
IdxT r4 = (up - lo) / 4; /* range/4 */
|
IdxT r4 = (up - lo) / 4; /* range/4 */
|
||||||
IdxT p = rnd % (r4 * 2) + (lo + r4);
|
IdxT p = (rnd ^ lo ^ up) % (r4 * 2) + (lo + r4);
|
||||||
lua_assert(lo + r4 <= p && p <= up - r4);
|
lua_assert(lo + r4 <= p && p <= up - r4);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5728,8 +5728,8 @@ it does not run it.
|
|||||||
@apii{0,0,-}
|
@apii{0,0,-}
|
||||||
|
|
||||||
Returns a value with a weak attempt for randomness.
|
Returns a value with a weak attempt for randomness.
|
||||||
(It produces that value based on the current date and time,
|
(It produces that value based on the current date and time
|
||||||
the current processor time, and the address of an internal variable,
|
and the address of an internal variable,
|
||||||
in case the machine has Address Space Layout Randomization.)
|
in case the machine has Address Space Layout Randomization.)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user