Back to index

cell-binutils  2.17cvs20070401
Defines | Functions | Variables
random.c File Reference
#include <errno.h>

Go to the source code of this file.

Defines

#define ULONG_MAX   ((unsigned long)(~0L)) /* 0xFFFFFFFF for 32-bits */
#define LONG_MAX   ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF for 32-bits*/
#define PTR   char *
#define NULL   (void *) 0
#define TYPE_0   0
#define BREAK_0   8
#define DEG_0   0
#define SEP_0   0
#define TYPE_1   1
#define BREAK_1   32
#define DEG_1   7
#define SEP_1   3
#define TYPE_2   2
#define BREAK_2   64
#define DEG_2   15
#define SEP_2   1
#define TYPE_3   3
#define BREAK_3   128
#define DEG_3   31
#define SEP_3   3
#define TYPE_4   4
#define BREAK_4   256
#define DEG_4   63
#define SEP_4   1
#define MAX_TYPES   5 /* Max number of types above. */

Functions

long int random (void)
void srandom (unsigned int x)
PTR initstate (unsigned int seed, PTR arg_state, unsigned long n)
PTR setstate (PTR arg_state)

Variables

static int degrees [MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }
static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }
static long int randtbl [DEG_3+1]
static long intfptr = &randtbl[SEP_3 + 1]
static long intrptr = &randtbl[1]
static long intstate = &randtbl[1]
static int rand_type = TYPE_3
static int rand_deg = DEG_3
static int rand_sep = SEP_3
static long intend_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])]

Define Documentation

#define BREAK_0   8

Definition at line 128 of file random.c.

#define BREAK_1   32

Definition at line 134 of file random.c.

#define BREAK_2   64

Definition at line 140 of file random.c.

#define BREAK_3   128

Definition at line 146 of file random.c.

#define BREAK_4   256

Definition at line 152 of file random.c.

#define DEG_0   0

Definition at line 129 of file random.c.

#define DEG_1   7

Definition at line 135 of file random.c.

#define DEG_2   15

Definition at line 141 of file random.c.

#define DEG_3   31

Definition at line 147 of file random.c.

#define DEG_4   63

Definition at line 153 of file random.c.

#define LONG_MAX   ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF for 32-bits*/

Definition at line 67 of file random.c.

#define MAX_TYPES   5 /* Max number of types above. */

Definition at line 160 of file random.c.

#define NULL   (void *) 0

Definition at line 77 of file random.c.

#define PTR   char *

Definition at line 75 of file random.c.

#define SEP_0   0

Definition at line 130 of file random.c.

#define SEP_1   3

Definition at line 136 of file random.c.

#define SEP_2   1

Definition at line 142 of file random.c.

#define SEP_3   3

Definition at line 148 of file random.c.

#define SEP_4   1

Definition at line 154 of file random.c.

#define TYPE_0   0

Definition at line 127 of file random.c.

#define TYPE_1   1

Definition at line 133 of file random.c.

#define TYPE_2   2

Definition at line 139 of file random.c.

#define TYPE_3   3

Definition at line 145 of file random.c.

#define TYPE_4   4

Definition at line 151 of file random.c.

#define ULONG_MAX   ((unsigned long)(~0L)) /* 0xFFFFFFFF for 32-bits */

Definition at line 66 of file random.c.


Function Documentation

PTR initstate ( unsigned int  seed,
PTR  arg_state,
unsigned long  n 
)

Definition at line 257 of file random.c.

{
  PTR ostate = (PTR) &state[-1];

  if (rand_type == TYPE_0)
    state[-1] = rand_type;
  else
    state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;
  if (n < BREAK_1)
    {
      if (n < BREAK_0)
       {
         errno = EINVAL;
         return NULL;
       }
      rand_type = TYPE_0;
      rand_deg = DEG_0;
      rand_sep = SEP_0;
    }
  else if (n < BREAK_2)
    {
      rand_type = TYPE_1;
      rand_deg = DEG_1;
      rand_sep = SEP_1;
    }
  else if (n < BREAK_3)
    {
      rand_type = TYPE_2;
      rand_deg = DEG_2;
      rand_sep = SEP_2;
    }
  else if (n < BREAK_4)
    {
      rand_type = TYPE_3;
      rand_deg = DEG_3;
      rand_sep = SEP_3;
    }
  else
    {
      rand_type = TYPE_4;
      rand_deg = DEG_4;
      rand_sep = SEP_4;
    }

  state = &((long int *) arg_state)[1];   /* First location.  */
  /* Must set END_PTR before srandom.  */
  end_ptr = &state[rand_deg];
  srandom(seed);
  if (rand_type == TYPE_0)
    state[-1] = rand_type;
  else
    state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;

  return ostate;
}

Here is the call graph for this function:

long int random ( void  )

Definition at line 376 of file random.c.

{
  if (rand_type == TYPE_0)
    {
      state[0] = ((state[0] * 1103515245) + 12345) & LONG_MAX;
      return state[0];
    }
  else
    {
      long int i;
      *fptr += *rptr;
      /* Chucking least random bit.  */
      i = (*fptr >> 1) & LONG_MAX;
      ++fptr;
      if (fptr >= end_ptr)
       {
         fptr = state;
         ++rptr;
       }
      else
       {
         ++rptr;
         if (rptr >= end_ptr)
           rptr = state;
       }
      return i;
    }
}

Here is the caller graph for this function:

PTR setstate ( PTR  arg_state)

Definition at line 323 of file random.c.

{
  register long int *new_state = (long int *) arg_state;
  register int type = new_state[0] % MAX_TYPES;
  register int rear = new_state[0] / MAX_TYPES;
  PTR ostate = (PTR) &state[-1];

  if (rand_type == TYPE_0)
    state[-1] = rand_type;
  else
    state[-1] = (MAX_TYPES * (rptr - state)) + rand_type;

  switch (type)
    {
    case TYPE_0:
    case TYPE_1:
    case TYPE_2:
    case TYPE_3:
    case TYPE_4:
      rand_type = type;
      rand_deg = degrees[type];
      rand_sep = seps[type];
      break;
    default:
      /* State info munged.  */
      errno = EINVAL;
      return NULL;
    }

  state = &new_state[1];
  if (rand_type != TYPE_0)
    {
      rptr = &state[rear];
      fptr = &state[(rear + rand_sep) % rand_deg];
    }
  /* Set end_ptr too.  */
  end_ptr = &state[rand_deg];

  return ostate;
}
void srandom ( unsigned int  x)

Definition at line 230 of file random.c.

{
  state[0] = x;
  if (rand_type != TYPE_0)
    {
      register long int i;
      for (i = 1; i < rand_deg; ++i)
       state[i] = (1103515145 * state[i - 1]) + 12345;
      fptr = &state[rand_sep];
      rptr = &state[0];
      for (i = 0; i < 10 * rand_deg; ++i)
       random();
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 162 of file random.c.

long int* end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])] [static]

Definition at line 219 of file random.c.

long int* fptr = &randtbl[SEP_3 + 1] [static]

Definition at line 198 of file random.c.

int rand_deg = DEG_3 [static]

Definition at line 216 of file random.c.

int rand_sep = SEP_3 [static]

Definition at line 217 of file random.c.

int rand_type = TYPE_3 [static]

Definition at line 215 of file random.c.

long int randtbl[DEG_3+1] [static]
Initial value:
  { TYPE_3,
      0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 
      0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 
      0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 
      0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 
      0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7, 
      0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 
      0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 
      0xf5ad9d0e, 0x8999220b, 0x27fb47b9
    }

Definition at line 176 of file random.c.

long int* rptr = &randtbl[1] [static]

Definition at line 199 of file random.c.

int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 } [static]

Definition at line 163 of file random.c.

long int* state = &randtbl[1] [static]

Definition at line 213 of file random.c.