Back to index

kdeartwork  4.3.2
Classes | Defines | Functions
vm_random.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  vm_random_data

Defines

#define VM_RAND_MAX   2147483647
#define int32_t   int

Functions

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)
void vm_default_initstate (int seed, struct vm_random_data *buf)
int vm_srandom (unsigned int seed, struct vm_random_data *buf)
int32_t vm_random (struct vm_random_data *buf)

Class Documentation

struct vm_random_data

Definition at line 8 of file vm_random.h.

Class Members
int32_t * end_ptr
int32_t * fptr
int32_t * rptr
int32_t * state
int vm_rand_deg
int vm_rand_sep
int vm_rand_type

Define Documentation

#define int32_t   int

Definition at line 6 of file vm_random.h.

#define VM_RAND_MAX   2147483647

Definition at line 4 of file vm_random.h.


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: