mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-01-18 15:01:41 +00:00
Implement the Mersenne Twister PRNG.
This gives QF a consistent qualilty PRNG on all platforms. The implementation is slightly different from the standard, but gives the same results for the same speed (details in mersenne.c).
This commit is contained in:
parent
3de67589a3
commit
5188644e75
4 changed files with 153 additions and 3 deletions
|
@ -4,8 +4,8 @@ nobase_pkginclude_HEADERS = \
|
|||
alloc.h bspfile.h cbuf.h cdaudio.h checksum.h clip_hull.h cmd.h \
|
||||
console.h crc.h csqc.h cvar.h dstring.h draw.h gib.h hash.h hl.h \
|
||||
idparse.h image.h in_event.h info.h input.h iqm.h joystick.h keys.h \
|
||||
link.h llist.h locs.h mathlib.h mdfour.h model.h modelgen.h msg.h \
|
||||
object.h pak.h pakfile.h pcx.h png.h plugin.h pr_comp.h pr_debug.h \
|
||||
link.h llist.h locs.h mathlib.h mdfour.h mersenne.h model.h modelgen.h \
|
||||
msg.h object.h pak.h pakfile.h pcx.h png.h plugin.h pr_comp.h pr_debug.h \
|
||||
pr_obj.h progs.h qargs.h qdefs.h qendian.h qfplist.h qtypes.h quakefs.h \
|
||||
quakeio.h render.h riff.h ruamoko.h set.h screen.h script.h sizebuf.h \
|
||||
skin.h sound.h spritegn.h sys.h teamplay.h tga.h uint32.h va.h \
|
||||
|
|
46
include/QF/mersenne.h
Normal file
46
include/QF/mersenne.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
mersenne.h
|
||||
|
||||
Mersenne Twister PRNG
|
||||
|
||||
Copyright (C) 2013 Bill Currie <bill@taniwha.org>
|
||||
|
||||
Author: Bill Currie <bill@taniwha.org>
|
||||
Date: 2013/01/21
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program 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 a copy of the GNU General Public License
|
||||
along with this program; if not, write to:
|
||||
|
||||
Free Software Foundation, Inc.
|
||||
59 Temple Place - Suite 330
|
||||
Boston, MA 02111-1307, USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __QF_mersenne_h
|
||||
#define __QF_mersenne_h
|
||||
|
||||
#include "QF/qtypes.h"
|
||||
|
||||
#define MT_STATE_SIZE 624
|
||||
typedef struct {
|
||||
uint32_t state[MT_STATE_SIZE];
|
||||
int index;
|
||||
} mtstate_t;
|
||||
|
||||
|
||||
void mtwist_seed (mtstate_t *state, uint32_t seed);
|
||||
uint32_t mtwist_rand (mtstate_t *state);
|
||||
|
||||
#endif//__QF_mersenne_h
|
|
@ -51,7 +51,7 @@ libQFutil_la_DEPENDENCIES= $(asm)
|
|||
libQFutil_la_SOURCES= \
|
||||
bspfile.c buildnum.c cbuf.c checksum.c cmd.c crc.c cvar.c dstring.c \
|
||||
fendian.c hash.c idparse.c info.c link.c llist.c \
|
||||
mathlib.c mdfour.c msg.c pakfile.c plugin.c qargs.c qendian.c \
|
||||
mathlib.c mdfour.c mersenne.c msg.c pakfile.c plugin.c qargs.c qendian.c \
|
||||
qfplist.c quakefs.c quakeio.c riff.c script.c set.c sizebuf.c string.c \
|
||||
sys.c va.c ver_check.c vrect.c wad.c wadfile.c zone.c \
|
||||
$(dirent) $(fnmatch) $(getopt)
|
||||
|
|
104
libs/util/mersenne.c
Normal file
104
libs/util/mersenne.c
Normal file
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
mersenne.c
|
||||
|
||||
Mersenne Twister PRNG
|
||||
|
||||
Copyright (C) 2013 Bill Currie <bill@taniwha.org>
|
||||
|
||||
Author: Bill Currie <bill@taniwha.org>
|
||||
Date: 2013/01/21
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program 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 a copy of the GNU General Public License
|
||||
along with this program; if not, write to:
|
||||
|
||||
Free Software Foundation, Inc.
|
||||
59 Temple Place - Suite 330
|
||||
Boston, MA 02111-1307, USA
|
||||
|
||||
*/
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
|
||||
/* This implementation is based on the pseudo-code given on
|
||||
http://en.wikipedia.org/wiki/Mersenne_twister (as of the above date) with
|
||||
some references to the code available from
|
||||
http://www.cs.hmc.edu/~geoff/mtwist.html
|
||||
|
||||
The main difference is the actual generator has been modified such that
|
||||
rather than pre-generating the 624 untempered values and then
|
||||
regenerating new ones every 624th call, one single untempered value is
|
||||
generated every call. While the per-call time will be higher for this
|
||||
implementation, it should be constant and should also be very close to
|
||||
the averate call time of the standard implementation.
|
||||
|
||||
The code has been tested by generating 2048 values with both
|
||||
implementations using the same seed (0xdeadbeef) and comparing the output.
|
||||
There were no differences.
|
||||
*/
|
||||
|
||||
#include "QF/mersenne.h"
|
||||
|
||||
#define KNUTH_MULT 1812433253ul // 0x6c078965
|
||||
#define KNUTH_SHIFT 30
|
||||
#define HIGH_BITS 0x80000000
|
||||
#define LOW_BITS 0x7fffffff
|
||||
#define MT_RECURRENCE 0x9908b0df
|
||||
#define MT_TEMPER_U 11
|
||||
#define MT_TEMPER_S 7
|
||||
#define MT_TEMPER_T 15
|
||||
#define MT_TEMPER_L 18
|
||||
#define MT_TEMPER_B 0x9d2c5680
|
||||
#define MT_TEMPER_C 0xefc60000
|
||||
#define MT_FEEDBACK 397
|
||||
|
||||
#define MT state->state
|
||||
|
||||
void
|
||||
mtwist_seed (mtstate_t *state, uint32_t seed)
|
||||
{
|
||||
int i;
|
||||
|
||||
state->index = 0;
|
||||
MT[0] = seed;
|
||||
for (i = 1; i < MT_STATE_SIZE; i++) {
|
||||
MT[i] = KNUTH_MULT * (MT[i - 1] ^ (MT[i - 1] >> KNUTH_SHIFT)) + i;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void
|
||||
mtwist_generate (mtstate_t *state, int i)
|
||||
{
|
||||
uint32_t y;
|
||||
static const uint32_t recurrence[] = { 0, MT_RECURRENCE };
|
||||
|
||||
y = (MT[i] & HIGH_BITS) | (MT[(i + 1) % MT_STATE_SIZE] & LOW_BITS);
|
||||
MT[i] = MT[(i + MT_FEEDBACK) % MT_STATE_SIZE] ^ (y >> 1);
|
||||
MT[i] ^= recurrence[y & 1];
|
||||
}
|
||||
|
||||
uint32_t
|
||||
mtwist_rand (mtstate_t *state)
|
||||
{
|
||||
uint32_t y;
|
||||
|
||||
mtwist_generate (state, state->index);
|
||||
y = MT[state->index];
|
||||
y ^= (y >> MT_TEMPER_U);
|
||||
y ^= (y << MT_TEMPER_S) & MT_TEMPER_B;
|
||||
y ^= (y << MT_TEMPER_T) & MT_TEMPER_C;
|
||||
y ^= (y >> MT_TEMPER_L);
|
||||
state->index = (state->index + 1) % MT_STATE_SIZE;
|
||||
return y;
|
||||
}
|
Loading…
Reference in a new issue