Blame src/lib/libast/uwin/random.c

Packit Service a8c26c
#include "FEATURE/uwin"
Packit Service a8c26c
Packit Service a8c26c
#if !_UWIN || _lib_random
Packit Service a8c26c
Packit Service a8c26c
void _STUB_random(){}
Packit Service a8c26c
Packit Service a8c26c
#else
Packit Service a8c26c
Packit Service a8c26c
/*
Packit Service a8c26c
 * Copyright (c) 1983
Packit Service a8c26c
 *	The Regents of the University of California.  All rights reserved.
Packit Service a8c26c
 *
Packit Service a8c26c
 * Redistribution and use in source and binary forms, with or without
Packit Service a8c26c
 * modification, are permitted provided that the following conditions
Packit Service a8c26c
 * are met:
Packit Service a8c26c
 * 1. Redistributions of source code must retain the above copyright
Packit Service a8c26c
 *    notice, this list of conditions and the following disclaimer.
Packit Service a8c26c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service a8c26c
 *    notice, this list of conditions and the following disclaimer in the
Packit Service a8c26c
 *    documentation and/or other materials provided with the distribution.
Packit Service a8c26c
 * 3. Neither the name of the University nor the names of its contributors
Packit Service a8c26c
 *    may be used to endorse or promote products derived from this software
Packit Service a8c26c
 *    without specific prior written permission.
Packit Service a8c26c
 *
Packit Service a8c26c
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit Service a8c26c
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service a8c26c
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service a8c26c
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit Service a8c26c
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service a8c26c
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service a8c26c
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service a8c26c
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service a8c26c
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service a8c26c
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service a8c26c
 * SUCH DAMAGE.
Packit Service a8c26c
 */
Packit Service a8c26c
Packit Service a8c26c
/*
Packit Service a8c26c
 * This is derived from the Berkeley source:
Packit Service a8c26c
 *	@(#)random.c	5.5 (Berkeley) 7/6/88
Packit Service a8c26c
 * It was reworked for the GNU C Library by Roland McGrath.
Packit Service a8c26c
 */
Packit Service a8c26c
Packit Service a8c26c
#define initstate	______initstate
Packit Service a8c26c
#define random		______random
Packit Service a8c26c
#define setstate	______setstate
Packit Service a8c26c
#define srandom		______srandom
Packit Service a8c26c
Packit Service a8c26c
#include <errno.h>
Packit Service a8c26c
#include <limits.h>
Packit Service a8c26c
#include <stddef.h>
Packit Service a8c26c
#include <stdlib.h>
Packit Service a8c26c
Packit Service a8c26c
#undef	initstate
Packit Service a8c26c
#undef	random
Packit Service a8c26c
#undef	setstate
Packit Service a8c26c
#undef	srandom
Packit Service a8c26c
Packit Service a8c26c
#if defined(__EXPORT__)
Packit Service a8c26c
#define extern		__EXPORT__
Packit Service a8c26c
#endif
Packit Service a8c26c
Packit Service a8c26c
extern long int	random();
Packit Service a8c26c
Packit Service a8c26c
#define PTR	char*
Packit Service a8c26c
Packit Service a8c26c
/* An improved random number generation package.  In addition to the standard
Packit Service a8c26c
   rand()/srand() like interface, this package also has a special state info
Packit Service a8c26c
   interface.  The initstate() routine is called with a seed, an array of
Packit Service a8c26c
   bytes, and a count of how many bytes are being passed in; this array is
Packit Service a8c26c
   then initialized to contain information for random number generation with
Packit Service a8c26c
   that much state information.  Good sizes for the amount of state
Packit Service a8c26c
   information are 32, 64, 128, and 256 bytes.  The state can be switched by
Packit Service a8c26c
   calling the setstate() function with the same array as was initiallized
Packit Service a8c26c
   with initstate().  By default, the package runs with 128 bytes of state
Packit Service a8c26c
   information and generates far better random numbers than a linear
Packit Service a8c26c
   congruential generator.  If the amount of state information is less than
Packit Service a8c26c
   32 bytes, a simple linear congruential R.N.G. is used.  Internally, the
Packit Service a8c26c
   state information is treated as an array of longs; the zeroeth element of
Packit Service a8c26c
   the array is the type of R.N.G. being used (small integer); the remainder
Packit Service a8c26c
   of the array is the state information for the R.N.G.  Thus, 32 bytes of
Packit Service a8c26c
   state information will give 7 longs worth of state information, which will
Packit Service a8c26c
   allow a degree seven polynomial.  (Note: The zeroeth word of state
Packit Service a8c26c
   information also has some other information stored in it; see setstate
Packit Service a8c26c
   for details).  The random number generation technique is a linear feedback
Packit Service a8c26c
   shift register approach, employing trinomials (since there are fewer terms
Packit Service a8c26c
   to sum up that way).  In this approach, the least significant bit of all
Packit Service a8c26c
   the numbers in the state table will act as a linear feedback shift register,
Packit Service a8c26c
   and will have period 2^deg - 1 (where deg is the degree of the polynomial
Packit Service a8c26c
   being used, assuming that the polynomial is irreducible and primitive).
Packit Service a8c26c
   The higher order bits will have longer periods, since their values are
Packit Service a8c26c
   also influenced by pseudo-random carries out of the lower bits.  The
Packit Service a8c26c
   total period of the generator is approximately deg*(2**deg - 1); thus
Packit Service a8c26c
   doubling the amount of state information has a vast influence on the
Packit Service a8c26c
   period of the generator.  Note: The deg*(2**deg - 1) is an approximation
Packit Service a8c26c
   only good for large deg, when the period of the shift register is the
Packit Service a8c26c
   dominant factor.  With deg equal to seven, the period is actually much
Packit Service a8c26c
   longer than the 7*(2**7 - 1) predicted by this formula.  */
Packit Service a8c26c
Packit Service a8c26c
Packit Service a8c26c
Packit Service a8c26c
/* For each of the currently supported random number generators, we have a
Packit Service a8c26c
   break value on the amount of state information (you need at least thi
Packit Service a8c26c
   bytes of state info to support this random number generator), a degree for
Packit Service a8c26c
   the polynomial (actually a trinomial) that the R.N.G. is based on, and
Packit Service a8c26c
   separation between the two lower order coefficients of the trinomial.  */
Packit Service a8c26c
Packit Service a8c26c
/* Linear congruential.  */
Packit Service a8c26c
#define	TYPE_0		0
Packit Service a8c26c
#define	BREAK_0		8
Packit Service a8c26c
#define	DEG_0		0
Packit Service a8c26c
#define	SEP_0		0
Packit Service a8c26c
Packit Service a8c26c
/* x**7 + x**3 + 1.  */
Packit Service a8c26c
#define	TYPE_1		1
Packit Service a8c26c
#define	BREAK_1		32
Packit Service a8c26c
#define	DEG_1		7
Packit Service a8c26c
#define	SEP_1		3
Packit Service a8c26c
Packit Service a8c26c
/* x**15 + x + 1.  */
Packit Service a8c26c
#define	TYPE_2		2
Packit Service a8c26c
#define	BREAK_2		64
Packit Service a8c26c
#define	DEG_2		15
Packit Service a8c26c
#define	SEP_2		1
Packit Service a8c26c
Packit Service a8c26c
/* x**31 + x**3 + 1.  */
Packit Service a8c26c
#define	TYPE_3		3
Packit Service a8c26c
#define	BREAK_3		128
Packit Service a8c26c
#define	DEG_3		31
Packit Service a8c26c
#define	SEP_3		3
Packit Service a8c26c
Packit Service a8c26c
/* x**63 + x + 1.  */
Packit Service a8c26c
#define	TYPE_4		4
Packit Service a8c26c
#define	BREAK_4		256
Packit Service a8c26c
#define	DEG_4		63
Packit Service a8c26c
#define	SEP_4		1
Packit Service a8c26c
Packit Service a8c26c
Packit Service a8c26c
/* Array versions of the above information to make code run faster.
Packit Service a8c26c
   Relies on fact that TYPE_i == i.  */
Packit Service a8c26c
Packit Service a8c26c
#define	MAX_TYPES	5	/* Max number of types above.  */
Packit Service a8c26c
Packit Service a8c26c
static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
Packit Service a8c26c
static int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
Packit Service a8c26c
Packit Service a8c26c
Packit Service a8c26c
Packit Service a8c26c
/* Initially, everything is set up as if from:
Packit Service a8c26c
	initstate(1, randtbl, 128);
Packit Service a8c26c
   Note that this initialization takes advantage of the fact that srandom
Packit Service a8c26c
   advances the front and rear pointers 10*rand_deg times, and hence the
Packit Service a8c26c
   rear pointer which starts at 0 will also end up at zero; thus the zeroeth
Packit Service a8c26c
   element of the state information, which contains info about the current
Packit Service a8c26c
   position of the rear pointer is just
Packit Service a8c26c
	(MAX_TYPES * (rptr - state)) + TYPE_3 == TYPE_3.  */
Packit Service a8c26c
Packit Service a8c26c
static long int randtbl[DEG_3 + 1] =
Packit Service a8c26c
  {
Packit Service a8c26c
    TYPE_3,
Packit Service a8c26c
    -851904987, -43806228, -2029755270, 1390239686, -1912102820,
Packit Service a8c26c
    -485608943, 1969813258, -1590463333, -1944053249, 455935928, 508023712,
Packit Service a8c26c
    -1714531963, 1800685987, -2015299881, 654595283, -1149023258,
Packit Service a8c26c
    -1470005550, -1143256056, -1325577603, -1568001885, 1275120390,
Packit Service a8c26c
    -607508183, -205999574, -1696891592, 1492211999, -1528267240,
Packit Service a8c26c
    -952028296, -189082757, 362343714, 1424981831, 2039449641,
Packit Service a8c26c
  };
Packit Service a8c26c
Packit Service a8c26c
/* FPTR and RPTR are two pointers into the state info, a front and a rear
Packit Service a8c26c
   pointer.  These two pointers are always rand_sep places aparts, as they
Packit Service a8c26c
   cycle through the state information.  (Yes, this does mean we could get
Packit Service a8c26c
   away with just one pointer, but the code for random is more efficient
Packit Service a8c26c
   this way).  The pointers are left positioned as they would be from the call:
Packit Service a8c26c
	initstate(1, randtbl, 128);
Packit Service a8c26c
   (The position of the rear pointer, rptr, is really 0 (as explained above
Packit Service a8c26c
   in the initialization of randtbl) because the state table pointer is set
Packit Service a8c26c
   to point to randtbl[1] (as explained below).)  */
Packit Service a8c26c
Packit Service a8c26c
static long int *fptr = &randtbl[SEP_3 + 1];
Packit Service a8c26c
static long int *rptr = &randtbl[1];
Packit Service a8c26c
Packit Service a8c26c
Packit Service a8c26c
Packit Service a8c26c
/* The following things are the pointer to the state information table,
Packit Service a8c26c
   the type of the current generator, the degree of the current polynomial
Packit Service a8c26c
   being used, and the separation between the two pointers.
Packit Service a8c26c
   Note that for efficiency of random, we remember the first location of
Packit Service a8c26c
   the state information, not the zeroeth.  Hence it is valid to access
Packit Service a8c26c
   state[-1], which is used to store the type of the R.N.G.
Packit Service a8c26c
   Also, we remember the last location, since this is more efficient than
Packit Service a8c26c
   indexing every time to find the address of the last element to see if
Packit Service a8c26c
   the front and rear pointers have wrapped.  */
Packit Service a8c26c
Packit Service a8c26c
static long int *state = &randtbl[1];
Packit Service a8c26c
Packit Service a8c26c
static int rand_type = TYPE_3;
Packit Service a8c26c
static int rand_deg = DEG_3;
Packit Service a8c26c
static int rand_sep = SEP_3;
Packit Service a8c26c
Packit Service a8c26c
static long int *end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])];
Packit Service a8c26c

Packit Service a8c26c
/* Initialize the random number generator based on the given seed.  If the
Packit Service a8c26c
   type is the trivial no-state-information type, just remember the seed.
Packit Service a8c26c
   Otherwise, initializes state[] based on the given "seed" via a linear
Packit Service a8c26c
   congruential generator.  Then, the pointers are set to known locations
Packit Service a8c26c
   that are exactly rand_sep places apart.  Lastly, it cycles the state
Packit Service a8c26c
   information a given number of times to get rid of any initial dependencies
Packit Service a8c26c
   introduced by the L.C.R.N.G.  Note that the initialization of randtbl[]
Packit Service a8c26c
   for default usage relies on values produced by this routine.  */
Packit Service a8c26c
extern void srandom(unsigned int x)
Packit Service a8c26c
{
Packit Service a8c26c
  state[0] = x;
Packit Service a8c26c
  if (rand_type != TYPE_0)
Packit Service a8c26c
    {
Packit Service a8c26c
      register long int i;
Packit Service a8c26c
      for (i = 1; i < rand_deg; ++i)
Packit Service a8c26c
	state[i] = (1103515145 * state[i - 1]) + 12345;
Packit Service a8c26c
      fptr = &state[rand_sep];
Packit Service a8c26c
      rptr = &state[0];
Packit Service a8c26c
      for (i = 0; i < 10 * rand_deg; ++i)
Packit Service a8c26c
	(void) random();
Packit Service a8c26c
    }
Packit Service a8c26c
}
Packit Service a8c26c

Packit Service a8c26c
/* Initialize the state information in the given array of N bytes for
Packit Service a8c26c
   future random number generation.  Based on the number of bytes we
Packit Service a8c26c
   are given, and the break values for the different R.N.G.'s, we choose
Packit Service a8c26c
   the best (largest) one we can and set things up for it.  srandom is
Packit Service a8c26c
   then called to initialize the state information.  Note that on return
Packit Service a8c26c
   from srandom, we set state[-1] to be the type multiplexed with the current
Packit Service a8c26c
   value of the rear pointer; this is so successive calls to initstate won't
Packit Service a8c26c
   lose this information and will be able to restart with setstate.
Packit Service a8c26c
   Note: The first thing we do is save the current state, if any, just like
Packit Service a8c26c
   setstate so that it doesn't matter when initstate is called.
Packit Service a8c26c
   Returns a pointer to the old state.  */
Packit Service a8c26c
extern char* initstate(unsigned int seed, char* arg_state, size_t n)
Packit Service a8c26c
{
Packit Service a8c26c
  PTR ostate = (PTR) &state[-1];
Packit Service a8c26c
Packit Service a8c26c
  if (rand_type == TYPE_0)
Packit Service a8c26c
    state[-1] = rand_type;
Packit Service a8c26c
  else
Packit Service a8c26c
    state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
Packit Service a8c26c
  if (n < BREAK_1)
Packit Service a8c26c
    {
Packit Service a8c26c
      if (n < BREAK_0)
Packit Service a8c26c
	{
Packit Service a8c26c
	  errno = EINVAL;
Packit Service a8c26c
	  return NULL;
Packit Service a8c26c
	}
Packit Service a8c26c
      rand_type = TYPE_0;
Packit Service a8c26c
      rand_deg = DEG_0;
Packit Service a8c26c
      rand_sep = SEP_0;
Packit Service a8c26c
    }
Packit Service a8c26c
  else if (n < BREAK_2)
Packit Service a8c26c
    {
Packit Service a8c26c
      rand_type = TYPE_1;
Packit Service a8c26c
      rand_deg = DEG_1;
Packit Service a8c26c
      rand_sep = SEP_1;
Packit Service a8c26c
    }
Packit Service a8c26c
  else if (n < BREAK_3)
Packit Service a8c26c
    {
Packit Service a8c26c
      rand_type = TYPE_2;
Packit Service a8c26c
      rand_deg = DEG_2;
Packit Service a8c26c
      rand_sep = SEP_2;
Packit Service a8c26c
    }
Packit Service a8c26c
  else if (n < BREAK_4)
Packit Service a8c26c
    {
Packit Service a8c26c
      rand_type = TYPE_3;
Packit Service a8c26c
      rand_deg = DEG_3;
Packit Service a8c26c
      rand_sep = SEP_3;
Packit Service a8c26c
    }
Packit Service a8c26c
  else
Packit Service a8c26c
    {
Packit Service a8c26c
      rand_type = TYPE_4;
Packit Service a8c26c
      rand_deg = DEG_4;
Packit Service a8c26c
      rand_sep = SEP_4;
Packit Service a8c26c
    }
Packit Service a8c26c
Packit Service a8c26c
  state = &((long int *) arg_state)[1];	/* First location.  */
Packit Service a8c26c
  /* Must set END_PTR before srandom.  */
Packit Service a8c26c
  end_ptr = &state[rand_deg];
Packit Service a8c26c
  srandom(seed);
Packit Service a8c26c
  if (rand_type == TYPE_0)
Packit Service a8c26c
    state[-1] = rand_type;
Packit Service a8c26c
  else
Packit Service a8c26c
    state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
Packit Service a8c26c
Packit Service a8c26c
  return ostate;
Packit Service a8c26c
}
Packit Service a8c26c

Packit Service a8c26c
/* Restore the state from the given state array.
Packit Service a8c26c
   Note: It is important that we also remember the locations of the pointers
Packit Service a8c26c
   in the current state information, and restore the locations of the pointers
Packit Service a8c26c
   from the old state information.  This is done by multiplexing the pointer
Packit Service a8c26c
   location into the zeroeth word of the state information. Note that due
Packit Service a8c26c
   to the order in which things are done, it is OK to call setstate with the
Packit Service a8c26c
   same state as the current state
Packit Service a8c26c
   Returns a pointer to the old state information.  */
Packit Service a8c26c
extern char *setstate(const char *arg_state)
Packit Service a8c26c
{
Packit Service a8c26c
  register long int *new_state = (long int *) arg_state;
Packit Service a8c26c
  register int type = new_state[0] % MAX_TYPES;
Packit Service a8c26c
  register int rear = new_state[0] / MAX_TYPES;
Packit Service a8c26c
  PTR ostate = (PTR) &state[-1];
Packit Service a8c26c
Packit Service a8c26c
  if (rand_type == TYPE_0)
Packit Service a8c26c
    state[-1] = rand_type;
Packit Service a8c26c
  else
Packit Service a8c26c
    state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
Packit Service a8c26c
Packit Service a8c26c
  switch (type)
Packit Service a8c26c
    {
Packit Service a8c26c
    case TYPE_0:
Packit Service a8c26c
    case TYPE_1:
Packit Service a8c26c
    case TYPE_2:
Packit Service a8c26c
    case TYPE_3:
Packit Service a8c26c
    case TYPE_4:
Packit Service a8c26c
      rand_type = type;
Packit Service a8c26c
      rand_deg = degrees[type];
Packit Service a8c26c
      rand_sep = seps[type];
Packit Service a8c26c
      break;
Packit Service a8c26c
    default:
Packit Service a8c26c
      /* State info munged.  */
Packit Service a8c26c
      errno = EINVAL;
Packit Service a8c26c
      return NULL;
Packit Service a8c26c
    }
Packit Service a8c26c
Packit Service a8c26c
  state = &new_state[1];
Packit Service a8c26c
  if (rand_type != TYPE_0)
Packit Service a8c26c
    {
Packit Service a8c26c
      rptr = &state[rear];
Packit Service a8c26c
      fptr = &state[(rear + rand_sep) % rand_deg];
Packit Service a8c26c
    }
Packit Service a8c26c
  /* Set end_ptr too.  */
Packit Service a8c26c
  end_ptr = &state[rand_deg];
Packit Service a8c26c
Packit Service a8c26c
  return ostate;
Packit Service a8c26c
}
Packit Service a8c26c

Packit Service a8c26c
/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
Packit Service a8c26c
   congruential bit.  Otherwise, we do our fancy trinomial stuff, which is the
Packit Service a8c26c
   same in all ther other cases due to all the global variables that have been
Packit Service a8c26c
   set up.  The basic operation is to add the number at the rear pointer into
Packit Service a8c26c
   the one at the front pointer.  Then both pointers are advanced to the next
Packit Service a8c26c
   location cyclically in the table.  The value returned is the sum generated,
Packit Service a8c26c
   reduced to 31 bits by throwing away the "least random" low bit.
Packit Service a8c26c
   Note: The code takes advantage of the fact that both the front and
Packit Service a8c26c
   rear pointers can't wrap on the same call by not testing the rear
Packit Service a8c26c
   pointer if the front one has wrapped.  Returns a 31-bit random number.  */
Packit Service a8c26c
Packit Service a8c26c
extern long int random()
Packit Service a8c26c
{
Packit Service a8c26c
  if (rand_type == TYPE_0)
Packit Service a8c26c
    {
Packit Service a8c26c
      state[0] = ((state[0] * 1103515245) + 12345) & LONG_MAX;
Packit Service a8c26c
      return state[0];
Packit Service a8c26c
    }
Packit Service a8c26c
  else
Packit Service a8c26c
    {
Packit Service a8c26c
      long int i;
Packit Service a8c26c
      *fptr += *rptr;
Packit Service a8c26c
      /* Chucking least random bit.  */
Packit Service a8c26c
      i = (*fptr >> 1) & LONG_MAX;
Packit Service a8c26c
      ++fptr;
Packit Service a8c26c
      if (fptr >= end_ptr)
Packit Service a8c26c
	{
Packit Service a8c26c
	  fptr = state;
Packit Service a8c26c
	  ++rptr;
Packit Service a8c26c
	}
Packit Service a8c26c
      else
Packit Service a8c26c
	{
Packit Service a8c26c
	  ++rptr;
Packit Service a8c26c
	  if (rptr >= end_ptr)
Packit Service a8c26c
	    rptr = state;
Packit Service a8c26c
	}
Packit Service a8c26c
      return i;
Packit Service a8c26c
    }
Packit Service a8c26c
}
Packit Service a8c26c
Packit Service a8c26c
#endif