summaryrefslogtreecommitdiff
path: root/libc/stdlib/random_r.c
diff options
context:
space:
mode:
authorPeter S. Mazinger <ps.m@gmx.net>2005-11-29 09:06:27 +0000
committerPeter S. Mazinger <ps.m@gmx.net>2005-11-29 09:06:27 +0000
commit57f63db72a6cc9a2b75a6d9b87b715fff06ae1b1 (patch)
treefa89ca0616d0f4d3a1304b5602d06dcc495002ac /libc/stdlib/random_r.c
parentd896dc4caf53e60eb5a12784fedd1bdf1a47443f (diff)
Use __STDC__ syntax, no need for prototype if random_r is before srandom_r
Diffstat (limited to 'libc/stdlib/random_r.c')
-rw-r--r--libc/stdlib/random_r.c122
1 files changed, 60 insertions, 62 deletions
diff --git a/libc/stdlib/random_r.c b/libc/stdlib/random_r.c
index 6631d85a8..9e203f0d5 100644
--- a/libc/stdlib/random_r.c
+++ b/libc/stdlib/random_r.c
@@ -123,6 +123,66 @@ static const struct random_poly_info random_poly_info =
+/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
+ congruential bit. Otherwise, we do our fancy trinomial stuff, which is the
+ same in all the other cases due to all the global variables that have been
+ set up. The basic operation is to add the number at the rear pointer into
+ the one at the front pointer. Then both pointers are advanced to the next
+ location cyclically in the table. The value returned is the sum generated,
+ reduced to 31 bits by throwing away the "least random" low bit.
+ Note: The code takes advantage of the fact that both the front and
+ rear pointers can't wrap on the same call by not testing the rear
+ pointer if the front one has wrapped. Returns a 31-bit random number. */
+
+int attribute_hidden __random_r(struct random_data *buf, int32_t *result)
+{
+ int32_t *state;
+
+ if (buf == NULL || result == NULL)
+ goto fail;
+
+ state = buf->state;
+
+ if (buf->rand_type == TYPE_0)
+ {
+ int32_t val = state[0];
+ val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
+ state[0] = val;
+ *result = val;
+ }
+ else
+ {
+ int32_t *fptr = buf->fptr;
+ int32_t *rptr = buf->rptr;
+ int32_t *end_ptr = buf->end_ptr;
+ int32_t val;
+
+ val = *fptr += *rptr;
+ /* Chucking least random bit. */
+ *result = (val >> 1) & 0x7fffffff;
+ ++fptr;
+ if (fptr >= end_ptr)
+ {
+ fptr = state;
+ ++rptr;
+ }
+ else
+ {
+ ++rptr;
+ if (rptr >= end_ptr)
+ rptr = state;
+ }
+ buf->fptr = fptr;
+ buf->rptr = rptr;
+ }
+ return 0;
+
+fail:
+ __set_errno (EINVAL);
+ return -1;
+}
+strong_alias(__random_r,random_r)
+
/* Initialize the random number generator based on the given seed. If the
type is the trivial no-state-information type, just remember the seed.
Otherwise, initializes state[] based on the given "seed" via a linear
@@ -302,65 +362,3 @@ fail:
__set_errno (EINVAL);
return -1;
}
-
-/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
- congruential bit. Otherwise, we do our fancy trinomial stuff, which is the
- same in all the other cases due to all the global variables that have been
- set up. The basic operation is to add the number at the rear pointer into
- the one at the front pointer. Then both pointers are advanced to the next
- location cyclically in the table. The value returned is the sum generated,
- reduced to 31 bits by throwing away the "least random" low bit.
- Note: The code takes advantage of the fact that both the front and
- rear pointers can't wrap on the same call by not testing the rear
- pointer if the front one has wrapped. Returns a 31-bit random number. */
-
-int attribute_hidden __random_r (buf, result)
- struct random_data *buf;
- int32_t *result;
-{
- int32_t *state;
-
- if (buf == NULL || result == NULL)
- goto fail;
-
- state = buf->state;
-
- if (buf->rand_type == TYPE_0)
- {
- int32_t val = state[0];
- val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
- state[0] = val;
- *result = val;
- }
- else
- {
- int32_t *fptr = buf->fptr;
- int32_t *rptr = buf->rptr;
- int32_t *end_ptr = buf->end_ptr;
- int32_t val;
-
- val = *fptr += *rptr;
- /* Chucking least random bit. */
- *result = (val >> 1) & 0x7fffffff;
- ++fptr;
- if (fptr >= end_ptr)
- {
- fptr = state;
- ++rptr;
- }
- else
- {
- ++rptr;
- if (rptr >= end_ptr)
- rptr = state;
- }
- buf->fptr = fptr;
- buf->rptr = rptr;
- }
- return 0;
-
-fail:
- __set_errno (EINVAL);
- return -1;
-}
-strong_alias(__random_r,random_r)