Blame rng/ranf.c

Packit 67cb25
/* rng/ranf.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <math.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <gsl/gsl_rng.h>
Packit 67cb25
#include <gsl/gsl_sys.h>
Packit 67cb25
Packit 67cb25
/* This is the CRAY RANF generator. The generator returns the
Packit 67cb25
   upper 32 bits from each term of the sequence,
Packit 67cb25
Packit 67cb25
   x_{n+1} = (a x_n) mod m 
Packit 67cb25
Packit 67cb25
   using 48-bit unsigned arithmetic, with a = 0x2875A2E7B175 and m =
Packit 67cb25
   2^48. The seed specifies the lower 32 bits of the initial value,
Packit 67cb25
   x_1, with the lowest bit set (to prevent the seed taking an even
Packit 67cb25
   value), and the upper 16 bits set to 0.
Packit 67cb25
Packit 67cb25
   There is a subtlety in the implementation of the seed. The initial
Packit 67cb25
   state is put one step back by multiplying by the modular inverse of
Packit 67cb25
   a mod m. This is done for compatibility with the original CRAY
Packit 67cb25
   implementation.
Packit 67cb25
Packit 67cb25
   Note, you can only seed the generator with integers up to 2^32,
Packit 67cb25
   while the CRAY uses wide integers which can cover all 2^48 states
Packit 67cb25
   of the generator.
Packit 67cb25
Packit 67cb25
   The theoretical value of x_{10001} is 141091827447341.
Packit 67cb25
Packit 67cb25
   The period of this generator is 2^{46}. */
Packit 67cb25
Packit 67cb25
static inline void ranf_advance (void *vstate);
Packit 67cb25
static unsigned long int ranf_get (void *vstate);
Packit 67cb25
static double ranf_get_double (void *vstate);
Packit 67cb25
static void ranf_set (void *state, unsigned long int s);
Packit 67cb25
Packit 67cb25
static const unsigned short int a0 = 0xB175 ;
Packit 67cb25
static const unsigned short int a1 = 0xA2E7 ;
Packit 67cb25
static const unsigned short int a2 = 0x2875 ;
Packit 67cb25
Packit 67cb25
typedef struct
Packit 67cb25
  {
Packit 67cb25
    unsigned short int x0, x1, x2;
Packit 67cb25
  }
Packit 67cb25
ranf_state_t;
Packit 67cb25
Packit 67cb25
static inline void
Packit 67cb25
ranf_advance (void *vstate)
Packit 67cb25
{
Packit 67cb25
  ranf_state_t *state = (ranf_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  const unsigned long int x0 = (unsigned long int) state->x0 ;
Packit 67cb25
  const unsigned long int x1 = (unsigned long int) state->x1 ;
Packit 67cb25
  const unsigned long int x2 = (unsigned long int) state->x2 ;
Packit 67cb25
Packit 67cb25
  unsigned long int r ;
Packit 67cb25
  
Packit 67cb25
  r = a0 * x0 ;
Packit 67cb25
  state->x0 = (r & 0xFFFF) ;
Packit 67cb25
 
Packit 67cb25
  r >>= 16 ;
Packit 67cb25
  r += a0 * x1 + a1 * x0 ;
Packit 67cb25
  state->x1 = (r & 0xFFFF) ;
Packit 67cb25
  
Packit 67cb25
  r >>= 16 ;
Packit 67cb25
  r += a0 * x2 + a1 * x1 + a2 * x0 ;
Packit 67cb25
  state->x2 = (r & 0xFFFF) ;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static unsigned long int 
Packit 67cb25
ranf_get (void *vstate)
Packit 67cb25
{
Packit 67cb25
  unsigned long int x1, x2;
Packit 67cb25
Packit 67cb25
  ranf_state_t *state = (ranf_state_t *) vstate;
Packit 67cb25
  ranf_advance (state) ;  
Packit 67cb25
Packit 67cb25
  x1 = (unsigned long int) state->x1;
Packit 67cb25
  x2 = (unsigned long int) state->x2;
Packit 67cb25
  
Packit 67cb25
  return (x2 << 16) + x1;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static double
Packit 67cb25
ranf_get_double (void * vstate)
Packit 67cb25
{
Packit 67cb25
  ranf_state_t *state = (ranf_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  ranf_advance (state) ; 
Packit 67cb25
Packit 67cb25
  return (ldexp((double) state->x2, -16)
Packit 67cb25
          + ldexp((double) state->x1, -32) 
Packit 67cb25
          + ldexp((double) state->x0, -48)) ;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
ranf_set (void *vstate, unsigned long int s)
Packit 67cb25
{
Packit 67cb25
  ranf_state_t *state = (ranf_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  unsigned short int x0, x1, x2 ;
Packit 67cb25
  unsigned long int r ;
Packit 67cb25
Packit 67cb25
  unsigned long int b0 = 0xD6DD ;
Packit 67cb25
  unsigned long int b1 = 0xB894 ;
Packit 67cb25
  unsigned long int b2 = 0x5CEE ;
Packit 67cb25
Packit 67cb25
  if (s == 0)  /* default seed */
Packit 67cb25
    {
Packit 67cb25
      x0 = 0x9CD1 ;
Packit 67cb25
      x1 = 0x53FC ;
Packit 67cb25
      x2 = 0x9482 ;
Packit 67cb25
    }
Packit 67cb25
  else 
Packit 67cb25
    {
Packit 67cb25
      x0 = (s | 1) & 0xFFFF ;
Packit 67cb25
      x1 = s >> 16 & 0xFFFF ;
Packit 67cb25
      x2 = 0 ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  r = b0 * x0 ;
Packit 67cb25
  state->x0 = (r & 0xFFFF) ;
Packit 67cb25
 
Packit 67cb25
  r >>= 16 ;
Packit 67cb25
  r += b0 * x1 + b1 * x0 ;
Packit 67cb25
  state->x1 = (r & 0xFFFF) ;
Packit 67cb25
  
Packit 67cb25
  r >>= 16 ;
Packit 67cb25
  r += b0 * x2 + b1 * x1 + b2 * x0 ;
Packit 67cb25
  state->x2 = (r & 0xFFFF) ;
Packit 67cb25
Packit 67cb25
  return;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static const gsl_rng_type ranf_type =
Packit 67cb25
{"ranf",                        /* name */
Packit 67cb25
 0xffffffffUL,                  /* RAND_MAX */
Packit 67cb25
 0,                             /* RAND_MIN */
Packit 67cb25
 sizeof (ranf_state_t),
Packit 67cb25
 &ranf_set,
Packit 67cb25
 &ranf_get,
Packit 67cb25
 &ranf_get_double
Packit 67cb25
};
Packit 67cb25
Packit 67cb25
const gsl_rng_type *gsl_rng_ranf = &ranf_type;