/* Mersenne Twister pseudo-random number generator functions.
THE FUNCTIONS IN THIS FILEAREFOR INTERNAL USE ONLY. THEY'RE ALMOST
CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
FUTURE GNU MP RELEASES.
Copyright 2002, 2003, 2006 Free Software Foundation, Inc.
This fileis part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2of the License, or (at your option) any
later version.
or both in parallel, as here.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. Ifnot,
see https://www.gnu.org/licenses/. */
#include <stdio.h> /* forNULL */
#include "gmp-impl.h"
#include "randmt.h"
/* This code implements the Mersenne Twister pseudorandom number generator by Takuji Nishimura and Makoto Matsumoto. The buffer initialization functionis different inorderto permit seeds greaterthan2^32-1.
This filecontains a special __gmp_randinit_mt_noseed which excludes the
seeding functionfrom the gmp_randfnptr_t routines. This isforuseby
mpn_random and mpn_random2 on the globalrandom generator. MT seeding
uses mpz functions, and we don't want mpn routines dragging mpz functions into the link. */
/* Default seed tousewhen the generator isnot initialized. */
#define DEFAULT_SEED 5489 /* was 4357 */
/* Get nbits bits ofoutputfrom the generator into dest.
Note that Mersenne Twister is designed to produce outputs in 32-bit words. */
void
__gmp_randget_mt (gmp_randstate_ptr rstate, mp_ptr dest, unsigned long int nbits)
{
gmp_uint_least32_t y;
int rbits;
mp_size_t i;
mp_size_t nlimbs; int *pmti;
gmp_uint_least32_t *mt;
#define NEXT_RANDOM \
do \
{ \ if (*pmti >= N) \
{ \
__gmp_mt_recalc_buffer (mt); \
*pmti = 0; \
} \
y = mt[(*pmti)++]; \
y ^= (y >> 11); \
y ^= (y << 7) & MASK_1; \
y ^= (y << 15) & MASK_2; \
y ^= (y >> 18); \
} \
while (0)
/* Handle the common cases of32- or64-bit limbs with fast,
optimized routines, and the rest of cases with a general
routine. Inall cases, no more than31 bits are rejected for the last limb so that every version of the codeis
consistent with the others. */
#if (GMP_NUMB_BITS == 32)
for (i = 0; i < nlimbs; i++)
{
NEXT_RANDOM;
dest[i] = (mp_limb_t) y;
} if (rbits)
{
NEXT_RANDOM;
dest[nlimbs] = (mp_limb_t) (y & ~(ULONG_MAX << rbits));
}
for (i = 0; i < nlimbs; i++)
{
NEXT_RANDOM;
dest[i] = (mp_limb_t) y;
NEXT_RANDOM;
dest[i] |= (mp_limb_t) y << 32;
} if (rbits)
{ if (rbits < 32)
{
NEXT_RANDOM;
dest[nlimbs] = (mp_limb_t) (y & ~(ULONG_MAX << rbits));
} else
{
NEXT_RANDOM;
dest[nlimbs] = (mp_limb_t) y; if (rbits > 32)
{
NEXT_RANDOM;
dest[nlimbs] |=
((mp_limb_t) (y & ~(ULONG_MAX << (rbits-32)))) << 32;
}
}
}
#else /* GMP_NUMB_BITS != 64 */
{
/* Fall back to a general algorithm. This algorithm works by
keeping a pool ofupto64 bits (2 outputs from MT) acting
as a shift register from which bits are consumed as needed.
Bits are consumed using the LSB bits of bitpool_l, and
inserted via bitpool_h and shifted to the right place. */
gmp_uint_least32_t bitpool_h = 0;
gmp_uint_least32_t bitpool_l = 0;
int bits_in_pool = 0; /* Holds number of valid bits in the pool. */
int bits_to_fill; /* Holds total number of bits to put in
destination. */
int bitidx; /* Holds the destination bit position. */
mp_size_t nlimbs2; /* Number of whole+partial limbs to fill. */
nlimbs2 = nlimbs + (rbits != 0);
for (i = 0; i < nlimbs2; i++)
{
bitidx = 0; if (i < nlimbs)
bits_to_fill = GMP_NUMB_BITS; else
bits_to_fill = rbits;
dest[i] = CNST_LIMB (0);
while (bits_to_fill >= 32) /* Process whole 32-bit blocks first. */
{ if (bits_in_pool < 32) /* Need more bits. */
{
/* 64-bit right shift. */
NEXT_RANDOM;
bitpool_h = y;
bitpool_l |= (bitpool_h << bits_in_pool) & 0xFFFFFFFF; if (bits_in_pool == 0)
bitpool_h = 0; else
bitpool_h >>= 32 - bits_in_pool;
bits_in_pool += 32; /* We've got 32 more bits. */
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.