diff options
| author | millert <> | 2003-02-28 21:27:44 +0000 |
|---|---|---|
| committer | millert <> | 2003-02-28 21:27:44 +0000 |
| commit | 13520626baf49aa5b220d51d3379c849d73c23fc (patch) | |
| tree | 070b79f0321d21a7714fe084451f11229495b8cf /src/lib/libc | |
| parent | 6739bef0b892a063bee07460073d25748d39c95c (diff) | |
| download | openbsd-13520626baf49aa5b220d51d3379c849d73c23fc.tar.gz openbsd-13520626baf49aa5b220d51d3379c849d73c23fc.tar.bz2 openbsd-13520626baf49aa5b220d51d3379c849d73c23fc.zip | |
Use int32_t, not long since this deals with 32bit quantities.
Inspired by a change in NetBSD and reported by Jan Johansson.
Diffstat (limited to 'src/lib/libc')
| -rw-r--r-- | src/lib/libc/stdlib/random.c | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c index 787581fe58..49a5ed1c3e 100644 --- a/src/lib/libc/stdlib/random.c +++ b/src/lib/libc/stdlib/random.c | |||
| @@ -32,7 +32,7 @@ | |||
| 32 | */ | 32 | */ |
| 33 | 33 | ||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
| 35 | static char *rcsid = "$OpenBSD: random.c,v 1.10 2002/12/06 17:43:34 millert Exp $"; | 35 | static char *rcsid = "$OpenBSD: random.c,v 1.11 2003/02/28 21:27:44 millert Exp $"; |
| 36 | #endif /* LIBC_SCCS and not lint */ | 36 | #endif /* LIBC_SCCS and not lint */ |
| 37 | 37 | ||
| 38 | #include <sys/param.h> | 38 | #include <sys/param.h> |
| @@ -59,10 +59,10 @@ static char *rcsid = "$OpenBSD: random.c,v 1.10 2002/12/06 17:43:34 millert Exp | |||
| 59 | * congruential generator. If the amount of state information is less than | 59 | * congruential generator. If the amount of state information is less than |
| 60 | * 32 bytes, a simple linear congruential R.N.G. is used. | 60 | * 32 bytes, a simple linear congruential R.N.G. is used. |
| 61 | * | 61 | * |
| 62 | * Internally, the state information is treated as an array of longs; the | 62 | * Internally, the state information is treated as an array of int32_t; the |
| 63 | * zeroeth element of the array is the type of R.N.G. being used (small | 63 | * zeroeth element of the array is the type of R.N.G. being used (small |
| 64 | * integer); the remainder of the array is the state information for the | 64 | * integer); the remainder of the array is the state information for the |
| 65 | * R.N.G. Thus, 32 bytes of state information will give 7 longs worth of | 65 | * R.N.G. Thus, 32 bytes of state information will give 7 int32_ts worth of |
| 66 | * state information, which will allow a degree seven polynomial. (Note: | 66 | * state information, which will allow a degree seven polynomial. (Note: |
| 67 | * the zeroeth word of state information also has some other information | 67 | * the zeroeth word of state information also has some other information |
| 68 | * stored in it -- see setstate() for details). | 68 | * stored in it -- see setstate() for details). |
| @@ -138,7 +138,7 @@ static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; | |||
| 138 | * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. | 138 | * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. |
| 139 | */ | 139 | */ |
| 140 | 140 | ||
| 141 | static long randtbl[DEG_3 + 1] = { | 141 | static int32_t randtbl[DEG_3 + 1] = { |
| 142 | TYPE_3, | 142 | TYPE_3, |
| 143 | 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05, | 143 | 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05, |
| 144 | 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454, | 144 | 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454, |
| @@ -162,8 +162,8 @@ static long randtbl[DEG_3 + 1] = { | |||
| 162 | * in the initialization of randtbl) because the state table pointer is set | 162 | * in the initialization of randtbl) because the state table pointer is set |
| 163 | * to point to randtbl[1] (as explained below). | 163 | * to point to randtbl[1] (as explained below). |
| 164 | */ | 164 | */ |
| 165 | static long *fptr = &randtbl[SEP_3 + 1]; | 165 | static int32_t *fptr = &randtbl[SEP_3 + 1]; |
| 166 | static long *rptr = &randtbl[1]; | 166 | static int32_t *rptr = &randtbl[1]; |
| 167 | 167 | ||
| 168 | /* | 168 | /* |
| 169 | * The following things are the pointer to the state information table, the | 169 | * The following things are the pointer to the state information table, the |
| @@ -175,11 +175,11 @@ static long *rptr = &randtbl[1]; | |||
| 175 | * this is more efficient than indexing every time to find the address of | 175 | * this is more efficient than indexing every time to find the address of |
| 176 | * the last element to see if the front and rear pointers have wrapped. | 176 | * the last element to see if the front and rear pointers have wrapped. |
| 177 | */ | 177 | */ |
| 178 | static long *state = &randtbl[1]; | 178 | static int32_t *state = &randtbl[1]; |
| 179 | static int32_t *end_ptr = &randtbl[DEG_3 + 1]; | ||
| 179 | static int rand_type = TYPE_3; | 180 | static int rand_type = TYPE_3; |
| 180 | static int rand_deg = DEG_3; | 181 | static int rand_deg = DEG_3; |
| 181 | static int rand_sep = SEP_3; | 182 | static int rand_sep = SEP_3; |
| 182 | static long *end_ptr = &randtbl[DEG_3 + 1]; | ||
| 183 | 183 | ||
| 184 | /* | 184 | /* |
| 185 | * srandom: | 185 | * srandom: |
| @@ -195,11 +195,11 @@ static long *end_ptr = &randtbl[DEG_3 + 1]; | |||
| 195 | */ | 195 | */ |
| 196 | void | 196 | void |
| 197 | srandom(x) | 197 | srandom(x) |
| 198 | u_int x; | 198 | unsigned int x; |
| 199 | { | 199 | { |
| 200 | register long int test; | 200 | int i; |
| 201 | register int i; | 201 | int32_t test; |
| 202 | ldiv_t val; | 202 | div_t val; |
| 203 | 203 | ||
| 204 | if (rand_type == TYPE_0) | 204 | if (rand_type == TYPE_0) |
| 205 | state[0] = x; | 205 | state[0] = x; |
| @@ -213,7 +213,7 @@ srandom(x) | |||
| 213 | * | 213 | * |
| 214 | * 2^31-1 (prime) = 2147483647 = 127773*16807+2836 | 214 | * 2^31-1 (prime) = 2147483647 = 127773*16807+2836 |
| 215 | */ | 215 | */ |
| 216 | val = ldiv(state[i-1], 127773); | 216 | val = div(state[i-1], 127773); |
| 217 | test = 16807 * val.rem - 2836 * val.quot; | 217 | test = 16807 * val.rem - 2836 * val.quot; |
| 218 | state[i] = test + (test < 0 ? 2147483647 : 0); | 218 | state[i] = test + (test < 0 ? 2147483647 : 0); |
| 219 | } | 219 | } |
| @@ -308,7 +308,7 @@ initstate(seed, arg_state, n) | |||
| 308 | char *arg_state; /* pointer to state array */ | 308 | char *arg_state; /* pointer to state array */ |
| 309 | size_t n; /* # bytes of state info */ | 309 | size_t n; /* # bytes of state info */ |
| 310 | { | 310 | { |
| 311 | register char *ostate = (char *)(&state[-1]); | 311 | char *ostate = (char *)(&state[-1]); |
| 312 | 312 | ||
| 313 | if (rand_type == TYPE_0) | 313 | if (rand_type == TYPE_0) |
| 314 | state[-1] = rand_type; | 314 | state[-1] = rand_type; |
| @@ -337,7 +337,7 @@ initstate(seed, arg_state, n) | |||
| 337 | rand_deg = DEG_4; | 337 | rand_deg = DEG_4; |
| 338 | rand_sep = SEP_4; | 338 | rand_sep = SEP_4; |
| 339 | } | 339 | } |
| 340 | state = &(((long *)arg_state)[1]); /* first location */ | 340 | state = &(((int32_t *)arg_state)[1]); /* first location */ |
| 341 | end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ | 341 | end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ |
| 342 | srandom(seed); | 342 | srandom(seed); |
| 343 | if (rand_type == TYPE_0) | 343 | if (rand_type == TYPE_0) |
| @@ -366,9 +366,9 @@ char * | |||
| 366 | setstate(arg_state) | 366 | setstate(arg_state) |
| 367 | const char *arg_state; | 367 | const char *arg_state; |
| 368 | { | 368 | { |
| 369 | register long *new_state = (long *)arg_state; | 369 | int32_t *new_state = (int32_t *)arg_state; |
| 370 | register int type = new_state[0] % MAX_TYPES; | 370 | int32_t type = new_state[0] % MAX_TYPES; |
| 371 | register int rear = new_state[0] / MAX_TYPES; | 371 | int32_t rear = new_state[0] / MAX_TYPES; |
| 372 | char *ostate = (char *)(&state[-1]); | 372 | char *ostate = (char *)(&state[-1]); |
| 373 | 373 | ||
| 374 | if (rand_type == TYPE_0) | 374 | if (rand_type == TYPE_0) |
| @@ -417,7 +417,7 @@ setstate(arg_state) | |||
| 417 | long | 417 | long |
| 418 | random() | 418 | random() |
| 419 | { | 419 | { |
| 420 | long i; | 420 | int32_t i; |
| 421 | 421 | ||
| 422 | if (rand_type == TYPE_0) | 422 | if (rand_type == TYPE_0) |
| 423 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; | 423 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; |
| @@ -430,5 +430,5 @@ random() | |||
| 430 | } else if (++rptr >= end_ptr) | 430 | } else if (++rptr >= end_ptr) |
| 431 | rptr = state; | 431 | rptr = state; |
| 432 | } | 432 | } |
| 433 | return(i); | 433 | return((long)i); |
| 434 | } | 434 | } |
