1 /*Written by Timothy B. Terriberry (tterribe@xiph.org) 1999-2009 public domain.
2 Based on the public domain implementation by Robert J. Jenkins Jr.*/
6 #include <ccan/ilog/ilog.h>
10 #define ISAAC_MASK (0xFFFFFFFFU)
12 /* Extract ISAAC_SZ_LOG bits (starting at bit 2). */
13 static inline uint32_t lower_bits(uint32_t x)
15 return (x & ((ISAAC_SZ-1) << 2)) >> 2;
18 /* Extract next ISAAC_SZ_LOG bits (starting at bit ISAAC_SZ_LOG+2). */
19 static inline uint32_t upper_bits(uint32_t y)
21 return (y >> (ISAAC_SZ_LOG+2)) & (ISAAC_SZ-1);
24 static void isaac_update(isaac_ctx *_ctx){
35 b=_ctx->b+(++_ctx->c);
36 for(i=0;i<ISAAC_SZ/2;i++){
38 a=(a^a<<13)+m[i+ISAAC_SZ/2];
39 m[i]=y=m[lower_bits(x)]+a+b;
40 r[i]=b=m[upper_bits(y)]+x;
42 a=(a^a>>6)+m[i+ISAAC_SZ/2];
43 m[i]=y=m[lower_bits(x)]+a+b;
44 r[i]=b=m[upper_bits(y)]+x;
46 a=(a^a<<2)+m[i+ISAAC_SZ/2];
47 m[i]=y=m[lower_bits(x)]+a+b;
48 r[i]=b=m[upper_bits(y)]+x;
50 a=(a^a>>16)+m[i+ISAAC_SZ/2];
51 m[i]=y=m[lower_bits(x)]+a+b;
52 r[i]=b=m[upper_bits(y)]+x;
54 for(i=ISAAC_SZ/2;i<ISAAC_SZ;i++){
56 a=(a^a<<13)+m[i-ISAAC_SZ/2];
57 m[i]=y=m[lower_bits(x)]+a+b;
58 r[i]=b=m[upper_bits(y)]+x;
60 a=(a^a>>6)+m[i-ISAAC_SZ/2];
61 m[i]=y=m[lower_bits(x)]+a+b;
62 r[i]=b=m[upper_bits(y)]+x;
64 a=(a^a<<2)+m[i-ISAAC_SZ/2];
65 m[i]=y=m[lower_bits(x)]+a+b;
66 r[i]=b=m[upper_bits(y)]+x;
68 a=(a^a>>16)+m[i-ISAAC_SZ/2];
69 m[i]=y=m[lower_bits(x)]+a+b;
70 r[i]=b=m[upper_bits(y)]+x;
77 static void isaac_mix(uint32_t _x[8]){
78 static const unsigned char SHIFT[8]={11,2,8,16,10,4,8,9};
81 _x[i]^=_x[(i+1)&7]<<SHIFT[i];
83 _x[(i+1)&7]+=_x[(i+2)&7];
85 _x[i]^=_x[(i+1)&7]>>SHIFT[i];
87 _x[(i+1)&7]+=_x[(i+2)&7];
92 void isaac_init(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed){
93 _ctx->a=_ctx->b=_ctx->c=0;
94 memset(_ctx->r,0,sizeof(_ctx->r));
95 isaac_reseed(_ctx,_seed,_nseed);
98 void isaac_reseed(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed){
106 if(_nseed>ISAAC_SEED_SZ_MAX)_nseed=ISAAC_SEED_SZ_MAX;
107 for(i=0;i<_nseed>>2;i++){
108 r[i]^=(uint32_t)_seed[i<<2|3]<<24|(uint32_t)_seed[i<<2|2]<<16|
109 (uint32_t)_seed[i<<2|1]<<8|_seed[i<<2];
115 for(j=1;j<_nseed;j++)ri|=(uint32_t)_seed[i<<2|j]<<(j<<3);
118 x[0]=x[1]=x[2]=x[3]=x[4]=x[5]=x[6]=x[7]=0x9E3779B9U;
119 for(i=0;i<4;i++)isaac_mix(x);
120 for(i=0;i<ISAAC_SZ;i+=8){
121 for(j=0;j<8;j++)x[j]+=r[i+j];
123 memcpy(m+i,x,sizeof(x));
125 for(i=0;i<ISAAC_SZ;i+=8){
126 for(j=0;j<8;j++)x[j]+=m[i+j];
128 memcpy(m+i,x,sizeof(x));
133 uint32_t isaac_next_uint32(isaac_ctx *_ctx){
134 if(!_ctx->n)isaac_update(_ctx);
135 return _ctx->r[--_ctx->n];
138 uint32_t isaac_next_uint(isaac_ctx *_ctx,uint32_t _n){
143 r=isaac_next_uint32(_ctx);
147 while(((d+_n-1)&ISAAC_MASK)<d);
151 /*Returns a uniform random float.
152 The expected value is within FLT_MIN (e.g., 1E-37) of 0.5.
153 _bits: An initial set of random bits.
154 _base: This should be -(the number of bits in _bits), up to -32.
155 Return: A float uniformly distributed between 0 (inclusive) and 1
157 The average value was measured over 2**32 samples to be
158 0.50000037448772916.*/
159 static float isaac_float_bits(isaac_ctx *_ctx,uint32_t _bits,int _base){
163 if(_base+FLT_MANT_DIG<FLT_MIN_EXP)return 0;
165 _bits=isaac_next_uint32(_ctx);
167 /*Note: This could also be determined with frexp(), for a slightly more
168 portable solution, but that takes twice as long, and one has to worry
169 about rounding effects, which can over-estimate the exponent when given
170 FLT_MANT_DIG+1 consecutive one bits.
171 Even the fallback C implementation of ILOGNZ_32() yields an implementation
172 25% faster than the frexp() method.*/
173 nbits_needed=FLT_MANT_DIG-ilog32_nz(_bits);
175 ret=ldexpf((float)_bits,_base);
177 while(32-nbits_needed<0){
179 if(32-nbits_needed<0){
183 ret+=ldexpf((float)isaac_next_uint32(_ctx),_base);
185 _bits=isaac_next_uint32(_ctx)>>32-nbits_needed;
186 ret+=ldexpf((float)_bits,_base-nbits_needed);
189 _bits=_bits<<nbits_needed|isaac_next_uint32(_ctx)>>(32-nbits_needed);
192 else _bits>>=-nbits_needed;
194 ret=ldexpf((float)_bits,_base-nbits_needed);
199 float isaac_next_float(isaac_ctx *_ctx){
200 return isaac_float_bits(_ctx,0,0);
203 float isaac_next_signed_float(isaac_ctx *_ctx){
205 bits=isaac_next_uint32(_ctx);
206 return (1|-((int)bits&1))*isaac_float_bits(_ctx,bits>>1,-31);
209 /*Returns a uniform random double.
210 _bits: An initial set of random bits.
211 _base: This should be -(the number of bits in _bits), up to -32.
212 Return: A double uniformly distributed between 0 (inclusive) and 1
214 The average value was measured over 2**32 samples to be
215 0.500006289408060911*/
216 static double isaac_double_bits(isaac_ctx *_ctx,uint32_t _bits,int _base){
220 if(_base+DBL_MANT_DIG<DBL_MIN_EXP)return 0;
222 _bits=isaac_next_uint32(_ctx);
224 nbits_needed=DBL_MANT_DIG-ilog32_nz(_bits);
226 ret=ldexp((double)_bits,_base);
228 while(32-nbits_needed<0){
230 if(32-nbits_needed<0){
234 ret+=ldexp((double)isaac_next_uint32(_ctx),_base);
236 _bits=isaac_next_uint32(_ctx)>>(32-nbits_needed);
237 ret+=ldexp((double)_bits,_base-nbits_needed);
240 _bits=_bits<<nbits_needed|isaac_next_uint32(_ctx)>>32-nbits_needed;
243 else _bits>>=-nbits_needed;
245 ret=ldexp((double)_bits,exp-DBL_MANT_DIG);
250 double isaac_next_double(isaac_ctx *_ctx){
251 return isaac_double_bits(_ctx,0,0);
254 double isaac_next_signed_double(isaac_ctx *_ctx){
256 bits=isaac_next_uint32(_ctx);
257 return (1|-((int)bits&1))*isaac_double_bits(_ctx,bits>>1,-31);