integrate isaac64 into nethack
Also removed the float code from isaac64 as they are not used in NetHack.
This commit is contained in:
@@ -884,26 +884,30 @@ get_random_seed()
|
||||
static void
|
||||
set_random(unsigned long seed)
|
||||
{
|
||||
#ifdef USE_ISAAC64
|
||||
init_isaac64(seed);
|
||||
#else
|
||||
/* the types are different enough here that sweeping the different
|
||||
* routine names into one via #defines is even more confusing
|
||||
*/
|
||||
#ifdef RANDOM /* srandom() from sys/share/random.c */
|
||||
# ifdef RANDOM /* srandom() from sys/share/random.c */
|
||||
srandom((unsigned int) seed);
|
||||
#else
|
||||
# if defined(__APPLE__) || defined(BSD) || defined(LINUX) || defined(ULTRIX) \
|
||||
|| defined(CYGWIN32) /* system srandom() */
|
||||
# if defined(BSD) && !defined(POSIX_TYPES) && defined(SUNOS4)
|
||||
(void)
|
||||
# endif
|
||||
srandom((int) seed);
|
||||
# else
|
||||
# ifdef UNIX /* system srand48() */
|
||||
# if defined(__APPLE__) || defined(BSD) || defined(LINUX) || defined(ULTRIX) \
|
||||
|| defined(CYGWIN32) /* system srandom() */
|
||||
# if defined(BSD) && !defined(POSIX_TYPES) && defined(SUNOS4)
|
||||
(void)
|
||||
# endif
|
||||
srandom((int) seed);
|
||||
# else
|
||||
# ifdef UNIX /* system srand48() */
|
||||
srand48((long) seed);
|
||||
# else /* poor quality system routine */
|
||||
# else /* poor quality system routine */
|
||||
srand((int) seed);
|
||||
# endif
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
110
src/isaac64.c
110
src/isaac64.c
@@ -1,13 +1,10 @@
|
||||
/*Written by Timothy B. Terriberry (tterribe@xiph.org) 1999-2009
|
||||
CC0 (Public domain) - see LICENSE file for details
|
||||
CC0 (Public domain) - see http://creativecommons.org/publicdomain/zero/1.0/ for details
|
||||
Based on the public domain ISAAC implementation by Robert J. Jenkins Jr.*/
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <ccan/ilog/ilog.h>
|
||||
#include "isaac64.h"
|
||||
|
||||
|
||||
#define ISAAC64_MASK ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
|
||||
|
||||
/* Extract ISAAC64_SZ_LOG bits (starting at bit 3). */
|
||||
@@ -17,7 +14,7 @@ static inline uint32_t lower_bits(uint64_t x)
|
||||
}
|
||||
|
||||
/* Extract next ISAAC64_SZ_LOG bits (starting at bit ISAAC64_SZ_LOG+2). */
|
||||
static inline uint32_t upper_bits(uint32_t y)
|
||||
static inline uint32_t upper_bits(uint64_t y)
|
||||
{
|
||||
return (y >> (ISAAC64_SZ_LOG+3)) & (ISAAC64_SZ-1);
|
||||
}
|
||||
@@ -150,106 +147,3 @@ uint64_t isaac64_next_uint(isaac64_ctx *_ctx,uint64_t _n){
|
||||
while(((d+_n-1)&ISAAC64_MASK)<d);
|
||||
return v;
|
||||
}
|
||||
|
||||
/*Returns a uniform random float.
|
||||
The expected value is within FLT_MIN (e.g., 1E-37) of 0.5.
|
||||
_bits: An initial set of random bits.
|
||||
_base: This should be -(the number of bits in _bits), up to -64.
|
||||
Return: A float uniformly distributed between 0 (inclusive) and 1
|
||||
(exclusive).
|
||||
The average value was measured over 2**32 samples to be
|
||||
0.499991407275206357.*/
|
||||
static float isaac64_float_bits(isaac64_ctx *_ctx,uint64_t _bits,int _base){
|
||||
float ret;
|
||||
int nbits_needed;
|
||||
while(!_bits){
|
||||
if(_base+FLT_MANT_DIG<FLT_MIN_EXP)return 0;
|
||||
_base-=64;
|
||||
_bits=isaac64_next_uint64(_ctx);
|
||||
}
|
||||
nbits_needed=FLT_MANT_DIG-ilog64_nz(_bits);
|
||||
#if FLT_MANT_DIG>64
|
||||
ret=ldexpf((float)_bits,_base);
|
||||
# if FLT_MANT_DIG>129
|
||||
while(64-nbits_needed<0){
|
||||
# else
|
||||
if(64-nbits_needed<0){
|
||||
# endif
|
||||
_base-=64;
|
||||
nbits_needed-=64;
|
||||
ret+=ldexpf((float)isaac64_next_uint64(_ctx),_base);
|
||||
}
|
||||
_bits=isaac64_next_uint64(_ctx)>>(64-nbits_needed);
|
||||
ret+=ldexpf((float)_bits,_base-nbits_needed);
|
||||
#else
|
||||
if(nbits_needed>0){
|
||||
_bits=_bits<<nbits_needed|isaac64_next_uint64(_ctx)>>(64-nbits_needed);
|
||||
}
|
||||
# if FLT_MANT_DIG<64
|
||||
else _bits>>=-nbits_needed;
|
||||
# endif
|
||||
ret=ldexpf((float)_bits,_base-nbits_needed);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
float isaac64_next_float(isaac64_ctx *_ctx){
|
||||
return isaac64_float_bits(_ctx,0,0);
|
||||
}
|
||||
|
||||
float isaac64_next_signed_float(isaac64_ctx *_ctx){
|
||||
uint64_t bits;
|
||||
bits=isaac64_next_uint64(_ctx);
|
||||
return (1|-((int)bits&1))*isaac64_float_bits(_ctx,bits>>1,-63);
|
||||
}
|
||||
|
||||
/*Returns a uniform random double.
|
||||
_bits: An initial set of random bits.
|
||||
_base: This should be -(the number of bits in _bits), up to -64.
|
||||
Return: A double uniformly distributed between 0 (inclusive) and 1
|
||||
(exclusive).
|
||||
The average value was measured over 2**32 samples to be
|
||||
0.499990992392019273.*/
|
||||
static double isaac64_double_bits(isaac64_ctx *_ctx,uint64_t _bits,int _base){
|
||||
double ret;
|
||||
int nbits_needed;
|
||||
while(!_bits){
|
||||
if(_base+DBL_MANT_DIG<DBL_MIN_EXP)return 0;
|
||||
_base-=64;
|
||||
_bits=isaac64_next_uint64(_ctx);
|
||||
}
|
||||
nbits_needed=DBL_MANT_DIG-ilog64_nz(_bits);
|
||||
#if DBL_MANT_DIG>64
|
||||
ret=ldexp((double)_bits,_base);
|
||||
# if DBL_MANT_DIG>129
|
||||
while(64-nbits_needed<0){
|
||||
# else
|
||||
if(64-nbits_needed<0){
|
||||
# endif
|
||||
_base-=64;
|
||||
nbits_needed-=64;
|
||||
ret+=ldexp((double)isaac64_next_uint64(_ctx),_base);
|
||||
}
|
||||
_bits=isaac64_next_uint64(_ctx)>>(64-nbits_needed);
|
||||
ret+=ldexp((double)_bits,_base-nbits_needed);
|
||||
#else
|
||||
if(nbits_needed>0){
|
||||
_bits=_bits<<nbits_needed|isaac64_next_uint64(_ctx)>>(64-nbits_needed);
|
||||
}
|
||||
# if DBL_MANT_DIG<64
|
||||
else _bits>>=-nbits_needed;
|
||||
# endif
|
||||
ret=ldexp((double)_bits,_base-nbits_needed);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
double isaac64_next_double(isaac64_ctx *_ctx){
|
||||
return isaac64_double_bits(_ctx,0,0);
|
||||
}
|
||||
|
||||
double isaac64_next_signed_double(isaac64_ctx *_ctx){
|
||||
uint64_t bits;
|
||||
bits=isaac64_next_uint64(_ctx);
|
||||
return (1|-((int)bits&1))*isaac64_double_bits(_ctx,bits>>1,-63);
|
||||
}
|
||||
|
||||
25
src/rnd.c
25
src/rnd.c
@@ -4,6 +4,30 @@
|
||||
|
||||
#include "hack.h"
|
||||
|
||||
#ifdef USE_ISAAC64
|
||||
#include "isaac64.h"
|
||||
|
||||
static isaac64_ctx rng_state;
|
||||
|
||||
void
|
||||
init_isaac64(unsigned long seed)
|
||||
{
|
||||
unsigned char new_rng_state[sizeof(seed)];
|
||||
int i;
|
||||
for (i=0; i<sizeof(seed); i++) {
|
||||
new_rng_state[i]= (unsigned char)(seed & 0xFF);
|
||||
seed >>= 8;
|
||||
}
|
||||
|
||||
isaac64_init(&rng_state, new_rng_state, sizeof(seed));
|
||||
}
|
||||
|
||||
static int
|
||||
RND(int x)
|
||||
{
|
||||
return (isaac64_next_uint64(&rng_state) % x);
|
||||
}
|
||||
#else
|
||||
/* "Rand()"s definition is determined by [OS]conf.h */
|
||||
#if defined(LINT) && defined(UNIX) /* rand() is long... */
|
||||
extern int NDECL(rand);
|
||||
@@ -16,6 +40,7 @@ extern int NDECL(rand);
|
||||
#define RND(x) ((int) ((Rand() >> 3) % (x)))
|
||||
#endif
|
||||
#endif /* LINT */
|
||||
#endif
|
||||
|
||||
/* 0 <= rn2(x) < x */
|
||||
int
|
||||
|
||||
Reference in New Issue
Block a user