Back to index

kdeartwork  4.3.2
Classes | Defines | Functions | Variables
vm_random.c File Reference
#include <limits.h>
#include <stdlib.h>
#include "vm_random.h"

Go to the source code of this file.

Classes

struct  vm_random_poly_info

Defines

#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

int vm_srandom (unsigned int seed, struct vm_random_data *buf)
int vm_initstate (unsigned int seed, void *arg_state, size_t n, struct vm_random_data *buf)
int vm_setstate (void *arg_state, struct vm_random_data *buf)
int32_t vm_random (struct vm_random_data *buf)
void vm_default_initstate (int seed, struct vm_random_data *buf)

Variables

static struct vm_random_poly_info
static int32_t vm_randtbl [DEG_3+1]

Class Documentation

struct vm_random_poly_info

Definition at line 113 of file vm_random.c.

Class Members
int degrees
int seps

Define Documentation

#define BREAK_0   8

Definition at line 79 of file vm_random.c.

#define BREAK_1   32

Definition at line 85 of file vm_random.c.

#define BREAK_2   64

Definition at line 91 of file vm_random.c.

#define BREAK_3   128

Definition at line 97 of file vm_random.c.

#define BREAK_4   256

Definition at line 103 of file vm_random.c.

#define DEG_0   0

Definition at line 80 of file vm_random.c.

#define DEG_1   7

Definition at line 86 of file vm_random.c.

#define DEG_2   15

Definition at line 92 of file vm_random.c.

#define DEG_3   31

Definition at line 98 of file vm_random.c.

#define DEG_4   63

Definition at line 104 of file vm_random.c.

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

Definition at line 111 of file vm_random.c.

#define SEP_0   0

Definition at line 81 of file vm_random.c.

#define SEP_1   3

Definition at line 87 of file vm_random.c.

#define SEP_2   1

Definition at line 93 of file vm_random.c.

#define SEP_3   3

Definition at line 99 of file vm_random.c.

#define SEP_4   1

Definition at line 105 of file vm_random.c.

#define TYPE_0   0

Definition at line 78 of file vm_random.c.

#define TYPE_1   1

Definition at line 84 of file vm_random.c.

#define TYPE_2   2

Definition at line 90 of file vm_random.c.

#define TYPE_3   3

Definition at line 96 of file vm_random.c.

#define TYPE_4   4

Definition at line 102 of file vm_random.c.


Function Documentation

void vm_default_initstate ( int  seed,
struct vm_random_data buf 
)

Definition at line 373 of file vm_random.c.

                                                        {
 vm_initstate( seed,
               vm_randtbl,
               128,
               buf );
}

Here is the call graph for this function:

Here is the caller graph for this function:

int vm_initstate ( unsigned int  seed,
void *  arg_state,
size_t  n,
struct vm_random_data buf 
)

Definition at line 212 of file vm_random.c.

{
  int type;
  int degree;
  int separation;
  int32_t *state;

  if (buf == NULL)
    goto fail;

  if (n >= BREAK_3)
    type = n < BREAK_4 ? TYPE_3 : TYPE_4;
  else if (n < BREAK_1)
    {
      if (n < BREAK_0)
       {
         goto fail;
       }
      type = TYPE_0;
    }
  else
    type = n < BREAK_2 ? TYPE_1 : TYPE_2;

  degree = vm_random_poly_info.degrees[type];
  separation = vm_random_poly_info.seps[type];

  buf->vm_rand_type = type;
  buf->vm_rand_sep = separation;
  buf->vm_rand_deg = degree;
  state = &((int32_t *) arg_state)[1];    /* First location.  */
  /* Must set END_PTR before srandom.  */
  buf->end_ptr = &state[degree];

  buf->state = state;

  vm_srandom (seed, buf);

  state[-1] = TYPE_0;
  if (type != TYPE_0)
    state[-1] = (buf->rptr - state) * MAX_TYPES + type;

  return 0;

 fail:
  return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t vm_random ( struct vm_random_data buf)

Definition at line 325 of file vm_random.c.

{
  int32_t *state;
  int32_t result;

  if (buf == NULL)
    goto fail;

  state = buf->state;

  if (buf->vm_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 result;

 fail:
  return -1;
}

Here is the caller graph for this function:

int vm_setstate ( void *  arg_state,
struct vm_random_data buf 
)

Definition at line 270 of file vm_random.c.

{
  int32_t *new_state = (int32_t *) arg_state;
  int type;
  int old_type;
  int32_t *old_state;
  int degree;
  int separation;

  if (buf == NULL)
    goto fail;

  old_type = buf->vm_rand_type;
  old_state = buf->state;
  if (old_type == TYPE_0)
    old_state[-1] = TYPE_0;
  else
    old_state[-1] = (MAX_TYPES * (buf->rptr - old_state)) + old_type;

  type = new_state[0] % MAX_TYPES;
  if (type < TYPE_0 || type >= TYPE_4)
    goto fail;

  buf->vm_rand_deg = degree = vm_random_poly_info.degrees[type];
  buf->vm_rand_sep = separation = vm_random_poly_info.seps[type];
  buf->vm_rand_type = type;

  if (type != TYPE_0)
    {
      int rear = new_state[0] / MAX_TYPES;
      buf->rptr = &new_state[rear];
      buf->fptr = &new_state[(rear + separation) % degree];
    }
  buf->state = &new_state[1];
  /* Set end_ptr too.  */
  buf->end_ptr = &new_state[degree];

  return 0;

 fail:
  return -1;
}
int vm_srandom ( unsigned int  seed,
struct vm_random_data buf 
)

Definition at line 146 of file vm_random.c.

{
  int type;
  int32_t *state;
  long int i;
  long int word;
  int32_t *dst;
  int kc;

  if (buf == NULL)
    goto fail;
  type = buf->vm_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->vm_rand_deg;
  for (i = 1; i < kc; ++i)
    {
      /* 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->vm_rand_sep];
  buf->rptr = &state[0];
  kc *= 10;
  while (--kc >= 0)
    {
      vm_random (buf);
    }

 done:
  return 0;

 fail:
  return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

struct vm_random_poly_info [static]
Initial value:

Definition at line 119 of file vm_random.c.

int32_t vm_randtbl[DEG_3+1] [static]
Initial value:
  {
      TYPE_3,
      
      -1726662223, 379960547, 1735697613, 1040273694, 1313901226,
      1627687941, -179304937, -2073333483, 1780058412, -1989503057,
      -615974602, 344556628, 939512070, -1249116260, 1507946756,
      -812545463, 154635395, 1388815473, -1926676823, 525320961,
      -1009028674, 968117788, -123449607, 1284210865, 435012392,
      -2017506339, -911064859, -370259173, 1132637927, 1398500161,
      -205601318,
  }

Definition at line 125 of file vm_random.c.