diff options
-rw-r--r-- | ltablib.c | 19 |
1 files changed, 14 insertions, 5 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.87 2015/11/23 11:09:27 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.86 2015/11/20 12:30:20 roberto Exp roberto $ |
3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -12,6 +12,7 @@ | |||
12 | 12 | ||
13 | #include <limits.h> | 13 | #include <limits.h> |
14 | #include <stddef.h> | 14 | #include <stddef.h> |
15 | #include <string.h> | ||
15 | 16 | ||
16 | #include "lua.h" | 17 | #include "lua.h" |
17 | 18 | ||
@@ -298,14 +299,22 @@ static unsigned int partition (lua_State *L, unsigned int lo, | |||
298 | */ | 299 | */ |
299 | #if !defined(l_sortpivot) | 300 | #if !defined(l_sortpivot) |
300 | /* Use 'time' and 'clock' as sources of "randomness" */ | 301 | /* Use 'time' and 'clock' as sources of "randomness" */ |
301 | |||
302 | #include <time.h> | 302 | #include <time.h> |
303 | 303 | ||
304 | #define szi (sizeof(unsigned int)) | ||
305 | #define sof(e) (sizeof(e)/szi) | ||
306 | |||
304 | static unsigned int choosePivot (unsigned int lo, unsigned int up) { | 307 | static unsigned int choosePivot (unsigned int lo, unsigned int up) { |
305 | unsigned int t = (unsigned int)(unsigned long)time(NULL); /* time */ | 308 | clock_t c = clock(); |
306 | unsigned int c = (unsigned int)(unsigned long)clock(); /* clock */ | 309 | time_t t = time(NULL); |
310 | unsigned int buff[sof(c) + sof(t)]; | ||
307 | unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */ | 311 | unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */ |
308 | unsigned int p = (c + t) % (r4 * 2) + (lo + r4); | 312 | unsigned int p, i, h = 0; |
313 | memcpy(buff, &c, sof(c) * szi); | ||
314 | memcpy(buff + sof(c), &t, sof(t) * szi); | ||
315 | for (i = 0; i < sof(buff); i++) | ||
316 | h += buff[i]; | ||
317 | p = h % (r4 * 2) + (lo + r4); | ||
309 | lua_assert(lo + r4 <= p && p <= up - r4); | 318 | lua_assert(lo + r4 <= p && p <= up - r4); |
310 | return p; | 319 | return p; |
311 | } | 320 | } |