SFMT19937: sfmt19937.c File Reference

- Main Page
- Data Structures
- Files

- File List
- Globals

# sfmt19937.c File Reference

SIMD oriented Fast Mersenne Twister(SFMT). More...

`#include <string.h>`
`#include <assert.h>`
`#include "sfmt19937.h"`

## Data Structures
struct | W128_T
| 128-bit data structure More...
## Defines
#define | MEXP 19937
| Mersenne Exponent.
#define | WORDSIZE 128
| the word size of the recursion of SFMT is 128-bit.
#define | N (MEXP / WORDSIZE + 1)
| SFMT generator has an internal state array of 128-bit integers, and N is its size.
#define | N32 (N * 4)
| N32 is the size of internal state array when regarded as an array of 32-bit integers.
#define | N64 (N * 2)
| N64 is the size of internal state array when regarded as an array of 64-bit integers.
#define | POS1 122
| the pick up position of the array.
#define | SL1 18
| the parameter of shift left as four 32-bit registers.
#define | SL2 1
| the parameter of shift left as one 128-bit register.
#define | SR1 11
| the parameter of shift right as four 32-bit registers.
#define | SR2 1
| the parameter of shift right as one 128-bit register.
#define | MSK1 0xdfffffefU
| A bitmask, used in the recursion.
#define | MSK2 0xddfecb7fU
| A bitmask, used in the recursion.
#define | MSK3 0xbffaffffU
| A bitmask, used in the recursion.
#define | MSK4 0xbffffff6U
| A bitmask, used in the recursion.
#define | PARITY1 0x00000001U
| This definitions is a part of a 128-bit period certification vector.
#define | PARITY2 0x00000000U
| This definitions is a part of a 128-bit period certification vector.
#define | PARITY3 0x00000000U
| This definitions is a part of a 128-bit period certification vector.
#define | PARITY4 0x13c9e684U
| This definitions is a part of a 128-bit period certification vector.
#define | do_recursion(r, a, b, c, d)
| This function represents the recursion formula.
## Typedefs
typedef W128_T | w128_t
| 128-bit data type
## Functions
static void | rshift128 (uint32_t out[4], const uint32_t in[4], int shift)
| This function simulates SIMD 128-bit right shift by the standard C.
static void | lshift128 (uint32_t out[4], const uint32_t in[4], int shift)
| This function simulates SIMD 128-bit left shift by the standard C.
static void | gen_rand_all (void)
| This function fills the internal state array with psedorandom integers.
static void | gen_rand_array (w128_t array[], int size)
| This function fills the user-specified array with psedorandom integers.
static uint32_t | func1 (uint32_t x)
| This function represents a function used in the initialization by init_by_array.
static uint32_t | func2 (uint32_t x)
| This function represents a function used in the initialization by init_by_array.
static void | endian_check (void)
| This function checks ENDIAN of CPU and set big_endian flag.
static void | period_certification (void)
| This function certificate the period of 2^19937-1.
uint32_t | gen_rand32 (void)
| This function generates and returns 32-bit pseudorandom number.
uint64_t | gen_rand64 (void)
| This function generates and returns 64-bit pseudorandom number.
void | fill_array32 (uint32_t array[], int size)
| This function generates pseudorandom 32-bit integers in the specified array[] by one call.
void | fill_array64 (uint64_t array[], int size)
| This function generates pseudorandom 64-bit integers in the specified array[] by one call.
void | init_gen_rand (uint32_t seed)
| This function initializes the internal state array with a 32-bit integer seed.
void | init_by_array (uint32_t init_key[], int key_length)
| This function initializes the internal state array, with an array of 32-bit integers used as the seeds.
## Variables
static uint32_t | sfmt [N][4]
| the 128-bit internal state array
static uint32_t * | psfmt32 = &sfmt[0][0]
| the 32bit interger pointer to the 128-bit internal state array
static uint64_t * | psfmt64 = (uint64_t *)&sfmt[0][0]
| the 64bit interger pointer to the 128-bit internal state array
static int | idx
| index counter to the 32-bit internal state array
static int | initialized = 0
| a flag: it is 0 if and only if the internal state is not yet initialized.
static int | big_endian
| a flag: it is 1 if CPU is BIG ENDIAN.
static uint32_t | parity [4] = {PARITY1, PARITY2, PARITY3, PARITY4}
| a parity check vector which certificate the period of 2^{MEXP}-1.

## Detailed Description

SIMD oriented Fast Mersenne Twister(SFMT).

Author:
  Mutsuo Saito (Hiroshima University)

  Makoto Matsumoto (Hiroshima University)

Date:
  2007-01-25

Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima University. All rights reserved.

The new BSD License is applied to this software, see LICENSE.txt

## Define Documentation

#define do_recursion | ( | r,
|  | a,
|  | b,
|  | c,
|  | d |  | ) |

Value:

    do { \
        uint32_t x[4];\
        uint32_t y[4];\
    \
        lshift128(x, a, SL2);\
        rshift128(y, c, SR2);\
        r[0] = a[0] ^ x[0] ^ ((b[0] >> SR1) & MSK1) ^ y[0] ^ (d[0] << SL1);\
        r[1] = a[1] ^ x[1] ^ ((b[1] >> SR1) & MSK2) ^ y[1] ^ (d[1] << SL1);\
        r[2] = a[2] ^ x[2] ^ ((b[2] >> SR1) & MSK3) ^ y[2] ^ (d[2] << SL1);\
        r[3] = a[3] ^ x[3] ^ ((b[3] >> SR1) & MSK4) ^ y[3] ^ (d[3] << SL1);\
        } while(0)

This function represents the recursion formula.

Parameters:

  | r | output
  | a | a 128-bit part of the interal state array
  | b | a 128-bit part of the interal state array
  | c | a 128-bit part of the interal state array
  | d | a 128-bit part of the interal state array

#define MEXP 19937

Mersenne Exponent.

The period of the sequence is a multiple of 2^MEXP-1.

#define MSK1 0xdfffffefU

A bitmask, used in the recursion.

These parameters are introduced to break symmetry of SIMD.

#define MSK2 0xddfecb7fU

A bitmask, used in the recursion.

These parameters are introduced to break symmetry of SIMD.

#define MSK3 0xbffaffffU

A bitmask, used in the recursion.

These parameters are introduced to break symmetry of SIMD.

#define MSK4 0xbffffff6U

A bitmask, used in the recursion.

These parameters are introduced to break symmetry of SIMD.

#define N (MEXP / WORDSIZE + 1)

SFMT generator has an internal state array of 128-bit integers, and N is its size.

#define N32 (N * 4)

N32 is the size of internal state array when regarded as an array of 32-bit integers.

#define N64 (N * 2)

N64 is the size of internal state array when regarded as an array of 64-bit integers.

#define PARITY1 0x00000001U

This definitions is a part of a 128-bit period certification vector.

#define PARITY2 0x00000000U

This definitions is a part of a 128-bit period certification vector.

#define PARITY3 0x00000000U

This definitions is a part of a 128-bit period certification vector.

#define PARITY4 0x13c9e684U

This definitions is a part of a 128-bit period certification vector.

#define POS1 122

the pick up position of the array.

#define SL1 18

the parameter of shift left as four 32-bit registers.

#define SL2 1

the parameter of shift left as one 128-bit register.

The 128-bit integer is shifted by (SL2 * 8) bits.

#define SR1 11

the parameter of shift right as four 32-bit registers.

#define SR2 1

the parameter of shift right as one 128-bit register.

The 128-bit integer is shifted by (SL2 * 8) bits.

#define WORDSIZE 128

the word size of the recursion of SFMT is 128-bit.

## Typedef Documentation

typedef struct W128_T w128_t

128-bit data type

## Function Documentation

static void endian_check | ( | void |  | ) | `[static]`

This function checks ENDIAN of CPU and set big_endian flag.

void fill_array32 | ( | uint32_t | array[],
|  | int | size |
| ) |  |  | `[inline]`

This function generates pseudorandom 32-bit integers in the specified array[] by one call.

The number of pseudorandom integers is specified by the argument size, which must be at least 624 and a multiple of four. The generation by this function is much faster than the following gen_rand function.

For initialization, init_gen_rand or init_by_array must be called before the first call of this function. This function can not be used after calling gen_rand function, without initialization.

Parameters:

  | array | an array where pseudorandom 32-bit integers are filled by this function. The pointer to the array must be "aligned" (namely, must be a multiple of 16) in the SIMD version, since it refers to the address of a 128-bit integer. In the standard C version, the pointer is arbitrary.
  | size | the number of 32-bit pseudorandom integers to be generated. size must be a multiple of 4, and greater than or equal to 624.

Note:
  memalign or posix_memalign is available to get aligned memory. Mac OSX doesn't have these functions, but malloc of OSX returns the pointer to a memory block aligned multiple of 16.

void fill_array64 | ( | uint64_t | array[],
|  | int | size |
| ) |  |  | `[inline]`

This function generates pseudorandom 64-bit integers in the specified array[] by one call.

The number of pseudorandom integers is specified by the argument size, which must be at least 312 and a multiple of two. The generation by this function is much faster than the following gen_rand function.

For initialization, init_gen_rand or init_by_array must be called before the first call of this function. This function can not be used after calling gen_rand function, without initialization.

Parameters:

  | array | an array where pseudorandom 64-bit integers are filled by this function. The pointer to the array must be "aligned" (namely, must be a multiple of 16) in the SIMD version, since it refers to the address of a 128-bit integer. In the standard C version, the pointer is arbitrary.
  | size | the number of 64-bit pseudorandom integers to be generated. size must be a multiple of 2, and greater than or equal to 312.

Note:
  memalign or posix_memalign is available to get aligned memory. Mac OSX doesn't have these functions, but malloc of OSX returns the pointer to memory block aligned multiple of 16.

static uint32_t func1 | ( | uint32_t | x | ) | `[inline, static]`

This function represents a function used in the initialization by init_by_array.

Parameters:

  | x | 32-bit integer

Returns:
  32-bit integer

static uint32_t func2 | ( | uint32_t | x | ) | `[inline, static]`

This function represents a function used in the initialization by init_by_array.

Parameters:

  | x | 32-bit integer

Returns:
  32-bit integer

uint32_t gen_rand32 | ( | void |  | ) | `[inline]`

This function generates and returns 32-bit pseudorandom number.

init_gen_rand or init_by_array must be called before this function.

Returns:
  32-bit pseudorandom number

uint64_t gen_rand64 | ( | void |  | ) | `[inline]`

This function generates and returns 64-bit pseudorandom number.

init_gen_rand or init_by_array must be called before this function. The function gen_rand64 should not be called after gen_rand32, unless an initialization is again executed.

Returns:
  64-bit pseudorandom number

static void gen_rand_all | ( | void |  | ) | `[inline, static]`

This function fills the internal state array with psedorandom integers.

static void gen_rand_array | ( | w128_t | array[],
|  | int | size |
| ) |  |  | `[inline, static]`

This function fills the user-specified array with psedorandom integers.

Parameters:

  | array | an 128-bit array to be filled by pseudorandom numbers.
  | size | number of 128-bit pesudorandom numbers to be generated.

void init_by_array | ( | uint32_t | init_key[],
|  | int | key_length |
| ) |  |  |

This function initializes the internal state array, with an array of 32-bit integers used as the seeds.

Parameters:

  | init_key | the array of 32-bit integers, used as a seed.
  | key_length | the length of init_key.

void init_gen_rand | ( | uint32_t | seed | ) |

This function initializes the internal state array with a 32-bit integer seed.

Parameters:

  | seed | a 32-bit integer used as the seed.

static void lshift128 | ( | uint32_t | out[4],
|  | const uint32_t | in[4],
|  | int | shift |
| ) |  |  | `[inline, static]`

This function simulates SIMD 128-bit left shift by the standard C.

The 128-bit integer given in in[4] is shifted by (shift * 8) bits. This function simulates the LITTLE ENDIAN SIMD.

Parameters:

  | out | the output of this function
  | in | the 128-bit data to be shifted
  | shift | the shift value

static void period_certification | ( | void |  | ) | `[static]`

This function certificate the period of 2^19937-1.

static void rshift128 | ( | uint32_t | out[4],
|  | const uint32_t | in[4],
|  | int | shift |
| ) |  |  | `[inline, static]`

This function simulates SIMD 128-bit right shift by the standard C.

The 128-bit integer given in in[4] is shifted by (shift * 8) bits. This function simulates the LITTLE ENDIAN SIMD.

Parameters:

  | out | the output of this function
  | in | the 128-bit data to be shifted
  | shift | the shift value

## Variable Documentation

int big_endian`[static]`

a flag: it is 1 if CPU is BIG ENDIAN.

int idx`[static]`

index counter to the 32-bit internal state array

int initialized = 0`[static]`

a flag: it is 0 if and only if the internal state is not yet initialized.

uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4}`[static]`

a parity check vector which certificate the period of 2^{MEXP}-1.

uint32_t* psfmt32 = &sfmt[0][0]`[static]`

the 32bit interger pointer to the 128-bit internal state array

uint64_t* psfmt64 = (uint64_t *)&sfmt[0][0]`[static]`

the 64bit interger pointer to the 128-bit internal state array

uint32_t sfmt[N][4]`[static]`

the 128-bit internal state array