Add rand.c, an implementation of G. Marsaglia KISS PRNG

This commit is contained in:
Yamagi Burmeister 2012-06-02 10:56:40 +02:00
parent 881b935b65
commit 97f7ba0610
3 changed files with 75 additions and 0 deletions

View file

@ -353,6 +353,7 @@ CLIENT_OBJS_ := \
src/common/misc.o \
src/common/netchan.o \
src/common/pmove.o \
src/common/rand.o \
src/common/szone.o \
src/common/zone.o \
src/common/command/cmd_execution.o \
@ -403,6 +404,7 @@ SERVER_OBJS_ := \
src/common/misc.o \
src/common/netchan.o \
src/common/pmove.o \
src/common/rand.o \
src/common/szone.o \
src/common/zone.o \
src/common/command/cmd_execution.o \

View file

@ -757,4 +757,8 @@ void SV_Init (void);
void SV_Shutdown (char *finalmsg, qboolean reconnect);
void SV_Frame (int msec);
/* Random number generator */
int randk(void);
void randk_seed(void);
#endif

69
src/common/rand.c Normal file
View file

@ -0,0 +1,69 @@
/*
* KISS PRNG (c) 2011 Shinobu
*
* This file was optained from zuttobenkyou.wordpress.com
* and modified by the Yamagi Quake II developers.
*
* LICENSE: Public domain
*
* =======================================================================
*
* KISS PRNG, as devised by Dr. George Marsaglia
*
* =======================================================================
*/
#include <stdint.h>
#define QSIZE 0x200000
#define CNG (cng = 6906969069ULL * cng + 13579)
#define XS (xs ^= (xs << 13), xs ^= (xs >> 17), xs ^= (xs << 43))
#define KISS (B64MWC() + CNG + XS)
static uint64_t QARY[QSIZE];
static int j;
static uint64_t carry;
static uint64_t xs;
static uint64_t cng;
uint64_t
B64MWC(void)
{
uint64_t t, x;
j = (j + 1) & (QSIZE - 1);
x = QARY[j];
t = (x << 28) + carry;
carry = (x >> 36) - (t < x);
return QARY[j] = t - x;
}
/*
* Generate a pseudorandom
* signed integer.
*/
int
randk(void)
{
return (int)KISS;
}
void
randk_seed(void)
{
uint64_t i;
/* Seed QARY[] with CNG+XS: */
for (i = 0; i < QSIZE; i++)
{
QARY[i] = CNG + XS;
}
/* Run through several rounds
to warm up the state */
for (i = 0; i < 256; i++)
{
randk();
}
}