]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - stdlib/random_r.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / stdlib / random_r.c
index d19fd1755b441a281e351ca28ecf629fcf591027..c4cd9a3b54a763e2e167c8ab56c2bf20aff98c68 100644 (file)
@@ -1,19 +1,48 @@
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- */
+   Copyright (C) 1995-2015 Free Software Foundation, Inc.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/*
+   Copyright (C) 1983 Regents of the University of California.
+   All rights reserved.
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   1. Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+   2. Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+   4. Neither the name of the University nor the names of its contributors
+      may be used to endorse or promote products derived from this software
+      without specific prior written permission.
+
+   THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+   ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+   SUCH DAMAGE.*/
 
 /*
  * This is derived from the Berkeley source:
 
 #define        MAX_TYPES       5       /* Max number of types above.  */
 
-static const int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
-static const int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
+struct random_poly_info
+{
+  int seps[MAX_TYPES];
+  int degrees[MAX_TYPES];
+};
+
+static const struct random_poly_info random_poly_info =
+{
+  { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 },
+  { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }
+};
 
 
 
@@ -120,37 +158,61 @@ static const int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
    introduced by the L.C.R.N.G.  Note that the initialization of randtbl[]
    for default usage relies on values produced by this routine.  */
 int
-__srandom_r (x, buf)
-     unsigned int x;
+__srandom_r (seed, buf)
+     unsigned int seed;
      struct random_data *buf;
 {
-  if (buf == NULL || buf->rand_type < TYPE_0 || buf->rand_type > TYPE_4)
-    return -1;
+  int type;
+  int32_t *state;
+  long int i;
+  int32_t word;
+  int32_t *dst;
+  int kc;
 
-  buf->state[0] = x;
-  if (buf->rand_type != TYPE_0)
+  if (buf == NULL)
+    goto fail;
+  type = buf->rand_type;
+  if ((unsigned int) type >= MAX_TYPES)
+    goto fail;
+
+  state = buf->state;
+  /* We must make sure the seed is not 0.  Take arbitrarily 1 in this case.  */
+  if (seed == 0)
+    seed = 1;
+  state[0] = seed;
+  if (type == TYPE_0)
+    goto done;
+
+  dst = state;
+  word = seed;
+  kc = buf->rand_deg;
+  for (i = 1; i < kc; ++i)
     {
-      long int i;
-      for (i = 1; i < buf->rand_deg; ++i)
-       {
-         /* This does:
-              state[i] = (16807 * state[i - 1]) % 2147483647;
-            but avoids overflowing 31 bits.  */
-         long int hi = buf->state[i - 1] / 127773;
-         long int lo = buf->state[i - 1] % 127773;
-         long int test = 16807 * lo - 2836 * hi;
-         buf->state[i] = test + (test < 0 ? 2147483647 : 0);
-       }
-      buf->fptr = &buf->state[buf->rand_sep];
-      buf->rptr = &buf->state[0];
-      for (i = 0; i < 10 * buf->rand_deg; ++i)
-       {
-         int32_t discard;
-         (void) __random_r (buf, &discard);
-       }
+      /* This does:
+          state[i] = (16807 * state[i - 1]) % 2147483647;
+        but avoids overflowing 31 bits.  */
+      long int hi = word / 127773;
+      long int lo = word % 127773;
+      word = 16807 * lo - 2836 * hi;
+      if (word < 0)
+       word += 2147483647;
+      *++dst = word;
     }
 
+  buf->fptr = &state[buf->rand_sep];
+  buf->rptr = &state[0];
+  kc *= 10;
+  while (--kc >= 0)
+    {
+      int32_t discard;
+      (void) __random_r (buf, &discard);
+    }
+
+ done:
   return 0;
+
+ fail:
+  return -1;
 }
 
 weak_alias (__srandom_r, srandom_r)
@@ -165,69 +227,63 @@ weak_alias (__srandom_r, srandom_r)
    lose this information and will be able to restart with setstate.
    Note: The first thing we do is save the current state, if any, just like
    setstate so that it doesn't matter when initstate is called.
-   Returns a pointer to the old state.  */
+   Returns 0 on success, non-zero on failure.  */
 int
 __initstate_r (seed, arg_state, n, buf)
      unsigned int seed;
-     void *arg_state;
+     char *arg_state;
      size_t n;
      struct random_data *buf;
 {
   if (buf == NULL)
-    return -1;
+    goto fail;
 
-  if (buf->rand_type == TYPE_0)
-    buf->state[-1] = buf->rand_type;
-  else
-    buf->state[-1] = (MAX_TYPES * (buf->rptr - buf->state)) + buf->rand_type;
-  if (n < BREAK_1)
-    {
-      if (n < BREAK_0)
-       {
-         __set_errno (EINVAL);
-         return -1;
-       }
-      buf->rand_type = TYPE_0;
-      buf->rand_deg = DEG_0;
-      buf->rand_sep = SEP_0;
-    }
-  else if (n < BREAK_2)
+  int32_t *old_state = buf->state;
+  if (old_state != NULL)
     {
-      buf->rand_type = TYPE_1;
-      buf->rand_deg = DEG_1;
-      buf->rand_sep = SEP_1;
-    }
-  else if (n < BREAK_3)
-    {
-      buf->rand_type = TYPE_2;
-      buf->rand_deg = DEG_2;
-      buf->rand_sep = SEP_2;
+      int old_type = buf->rand_type;
+      if (old_type == TYPE_0)
+       old_state[-1] = TYPE_0;
+      else
+       old_state[-1] = (MAX_TYPES * (buf->rptr - old_state)) + old_type;
     }
-  else if (n < BREAK_4)
+
+  int type;
+  if (n >= BREAK_3)
+    type = n < BREAK_4 ? TYPE_3 : TYPE_4;
+  else if (n < BREAK_1)
     {
-      buf->rand_type = TYPE_3;
-      buf->rand_deg = DEG_3;
-      buf->rand_sep = SEP_3;
+      if (n < BREAK_0)
+       goto fail;
+
+      type = TYPE_0;
     }
   else
-    {
-      buf->rand_type = TYPE_4;
-      buf->rand_deg = DEG_4;
-      buf->rand_sep = SEP_4;
-    }
+    type = n < BREAK_2 ? TYPE_1 : TYPE_2;
+
+  int degree = random_poly_info.degrees[type];
+  int separation = random_poly_info.seps[type];
 
-  buf->state = &((int32_t *) arg_state)[1];    /* First location.  */
+  buf->rand_type = type;
+  buf->rand_sep = separation;
+  buf->rand_deg = degree;
+  int32_t *state = &((int32_t *) arg_state)[1];        /* First location.  */
   /* Must set END_PTR before srandom.  */
-  buf->end_ptr = &buf->state[buf->rand_deg];
+  buf->end_ptr = &state[degree];
+
+  buf->state = state;
 
   __srandom_r (seed, buf);
 
-  if (buf->rand_type == TYPE_0)
-    buf->state[-1] = buf->rand_type;
-  else
-    buf->state[-1] = (MAX_TYPES * (buf->rptr - buf->state)) + buf->rand_type;
+  state[-1] = TYPE_0;
+  if (type != TYPE_0)
+    state[-1] = (buf->rptr - state) * MAX_TYPES + type;
 
   return 0;
+
+ fail:
+  __set_errno (EINVAL);
+  return -1;
 }
 
 weak_alias (__initstate_r, initstate_r)
@@ -239,51 +295,52 @@ weak_alias (__initstate_r, initstate_r)
    location into the zeroth word of the state information. Note that due
    to the order in which things are done, it is OK to call setstate with the
    same state as the current state
-   Returns a pointer to the old state information.  */
+   Returns 0 on success, non-zero on failure.  */
 int
 __setstate_r (arg_state, buf)
-     void *arg_state;
+     char *arg_state;
      struct random_data *buf;
 {
-  int32_t *new_state = (int32_t *) arg_state;
-  int type = new_state[0] % MAX_TYPES;
-  int rear = new_state[0] / MAX_TYPES;
-
-  if (buf == NULL)
-    return -1;
-
-  if (buf->rand_type == TYPE_0)
-    buf->state[-1] = buf->rand_type;
+  int32_t *new_state = 1 + (int32_t *) arg_state;
+  int type;
+  int old_type;
+  int32_t *old_state;
+  int degree;
+  int separation;
+
+  if (arg_state == NULL || buf == NULL)
+    goto fail;
+
+  old_type = buf->rand_type;
+  old_state = buf->state;
+  if (old_type == TYPE_0)
+    old_state[-1] = TYPE_0;
   else
-    buf->state[-1] = (MAX_TYPES * (buf->rptr - buf->state)) + buf->rand_type;
+    old_state[-1] = (MAX_TYPES * (buf->rptr - old_state)) + old_type;
 
-  switch (type)
-    {
-    case TYPE_0:
-    case TYPE_1:
-    case TYPE_2:
-    case TYPE_3:
-    case TYPE_4:
-      buf->rand_type = type;
-      buf->rand_deg = degrees[type];
-      buf->rand_sep = seps[type];
-      break;
-    default:
-      /* State info munged.  */
-      __set_errno (EINVAL);
-      return -1;
-    }
+  type = new_state[-1] % MAX_TYPES;
+  if (type < TYPE_0 || type > TYPE_4)
+    goto fail;
 
-  buf->state = &new_state[1];
-  if (buf->rand_type != TYPE_0)
+  buf->rand_deg = degree = random_poly_info.degrees[type];
+  buf->rand_sep = separation = random_poly_info.seps[type];
+  buf->rand_type = type;
+
+  if (type != TYPE_0)
     {
-      buf->rptr = &buf->state[rear];
-      buf->fptr = &buf->state[(rear + buf->rand_sep) % buf->rand_deg];
+      int rear = new_state[-1] / MAX_TYPES;
+      buf->rptr = &new_state[rear];
+      buf->fptr = &new_state[(rear + separation) % degree];
     }
+  buf->state = new_state;
   /* Set end_ptr too.  */
-  buf->end_ptr = &buf->state[buf->rand_deg];
+  buf->end_ptr = &new_state[degree];
 
   return 0;
+
+ fail:
+  __set_errno (EINVAL);
+  return -1;
 }
 
 weak_alias (__setstate_r, setstate_r)
@@ -304,33 +361,50 @@ __random_r (buf, result)
      struct random_data *buf;
      int32_t *result;
 {
+  int32_t *state;
+
   if (buf == NULL || result == NULL)
-    return -1;
+    goto fail;
+
+  state = buf->state;
 
   if (buf->rand_type == TYPE_0)
     {
-      buf->state[0] = ((buf->state[0] * 1103515245) + 12345) & 0x7fffffff;
-      *result = buf->state[0];
+      int32_t val = state[0];
+      val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
+      state[0] = val;
+      *result = val;
     }
   else
     {
-      *buf->fptr += *buf->rptr;
+      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 = (*buf->fptr >> 1) & 0x7fffffff;
-      ++buf->fptr;
-      if (buf->fptr >= buf->end_ptr)
+      *result = (val >> 1) & 0x7fffffff;
+      ++fptr;
+      if (fptr >= end_ptr)
        {
-         buf->fptr = buf->state;
-         ++buf->rptr;
+         fptr = state;
+         ++rptr;
        }
       else
        {
-         ++buf->rptr;
-         if (buf->rptr >= buf->end_ptr)
-           buf->rptr = buf->state;
+         ++rptr;
+         if (rptr >= end_ptr)
+           rptr = state;
        }
+      buf->fptr = fptr;
+      buf->rptr = rptr;
     }
   return 0;
+
+ fail:
+  __set_errno (EINVAL);
+  return -1;
 }
 
 weak_alias (__random_r, random_r)