isaac.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.*/
  3. #include <float.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <ccan/ilog/ilog.h>
  7. #include "isaac.h"
  8. #define ISAAC_MASK (0xFFFFFFFFU)
  9. /* Extract ISAAC_SZ_LOG bits (starting at bit 2). */
  10. static inline uint32_t lower_bits(uint32_t x)
  11. {
  12. return (x & ((ISAAC_SZ-1) << 2)) >> 2;
  13. }
  14. /* Extract next ISAAC_SZ_LOG bits (starting at bit ISAAC_SZ_LOG+2). */
  15. static inline uint32_t upper_bits(uint32_t y)
  16. {
  17. return (y >> (ISAAC_SZ_LOG+2)) & (ISAAC_SZ-1);
  18. }
  19. static void isaac_update(isaac_ctx *_ctx){
  20. uint32_t *m;
  21. uint32_t *r;
  22. uint32_t a;
  23. uint32_t b;
  24. uint32_t x;
  25. uint32_t y;
  26. int i;
  27. m=_ctx->m;
  28. r=_ctx->r;
  29. a=_ctx->a;
  30. b=_ctx->b+(++_ctx->c);
  31. for(i=0;i<ISAAC_SZ/2;i++){
  32. x=m[i];
  33. a=(a^a<<13)+m[i+ISAAC_SZ/2];
  34. m[i]=y=m[lower_bits(x)]+a+b;
  35. r[i]=b=m[upper_bits(y)]+x;
  36. x=m[++i];
  37. a=(a^a>>6)+m[i+ISAAC_SZ/2];
  38. m[i]=y=m[lower_bits(x)]+a+b;
  39. r[i]=b=m[upper_bits(y)]+x;
  40. x=m[++i];
  41. a=(a^a<<2)+m[i+ISAAC_SZ/2];
  42. m[i]=y=m[lower_bits(x)]+a+b;
  43. r[i]=b=m[upper_bits(y)]+x;
  44. x=m[++i];
  45. a=(a^a>>16)+m[i+ISAAC_SZ/2];
  46. m[i]=y=m[lower_bits(x)]+a+b;
  47. r[i]=b=m[upper_bits(y)]+x;
  48. }
  49. for(i=ISAAC_SZ/2;i<ISAAC_SZ;i++){
  50. x=m[i];
  51. a=(a^a<<13)+m[i-ISAAC_SZ/2];
  52. m[i]=y=m[lower_bits(x)]+a+b;
  53. r[i]=b=m[upper_bits(y)]+x;
  54. x=m[++i];
  55. a=(a^a>>6)+m[i-ISAAC_SZ/2];
  56. m[i]=y=m[lower_bits(x)]+a+b;
  57. r[i]=b=m[upper_bits(y)]+x;
  58. x=m[++i];
  59. a=(a^a<<2)+m[i-ISAAC_SZ/2];
  60. m[i]=y=m[lower_bits(x)]+a+b;
  61. r[i]=b=m[upper_bits(y)]+x;
  62. x=m[++i];
  63. a=(a^a>>16)+m[i-ISAAC_SZ/2];
  64. m[i]=y=m[lower_bits(x)]+a+b;
  65. r[i]=b=m[upper_bits(y)]+x;
  66. }
  67. _ctx->b=b;
  68. _ctx->a=a;
  69. _ctx->n=ISAAC_SZ;
  70. }
  71. static void isaac_mix(uint32_t _x[8]){
  72. static const unsigned char SHIFT[8]={11,2,8,16,10,4,8,9};
  73. int i;
  74. for(i=0;i<8;i++){
  75. _x[i]^=_x[(i+1)&7]<<SHIFT[i];
  76. _x[(i+3)&7]+=_x[i];
  77. _x[(i+1)&7]+=_x[(i+2)&7];
  78. i++;
  79. _x[i]^=_x[(i+1)&7]>>SHIFT[i];
  80. _x[(i+3)&7]+=_x[i];
  81. _x[(i+1)&7]+=_x[(i+2)&7];
  82. }
  83. }
  84. void isaac_init(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed){
  85. _ctx->a=_ctx->b=_ctx->c=0;
  86. memset(_ctx->r,0,sizeof(_ctx->r));
  87. isaac_reseed(_ctx,_seed,_nseed);
  88. }
  89. void isaac_reseed(isaac_ctx *_ctx,const unsigned char *_seed,int _nseed){
  90. uint32_t *m;
  91. uint32_t *r;
  92. uint32_t x[8];
  93. int i;
  94. int j;
  95. m=_ctx->m;
  96. r=_ctx->r;
  97. if(_nseed>ISAAC_SEED_SZ_MAX)_nseed=ISAAC_SEED_SZ_MAX;
  98. for(i=0;i<_nseed>>2;i++){
  99. r[i]^=(uint32_t)_seed[i<<2|3]<<24|(uint32_t)_seed[i<<2|2]<<16|
  100. (uint32_t)_seed[i<<2|1]<<8|_seed[i<<2];
  101. }
  102. _nseed-=i<<2;
  103. if(_nseed>0){
  104. uint32_t ri;
  105. ri=_seed[i<<2];
  106. for(j=1;j<_nseed;j++)ri|=(uint32_t)_seed[i<<2|j]<<(j<<3);
  107. r[i++]^=ri;
  108. }
  109. x[0]=x[1]=x[2]=x[3]=x[4]=x[5]=x[6]=x[7]=0x9E3779B9U;
  110. for(i=0;i<4;i++)isaac_mix(x);
  111. for(i=0;i<ISAAC_SZ;i+=8){
  112. for(j=0;j<8;j++)x[j]+=r[i+j];
  113. isaac_mix(x);
  114. memcpy(m+i,x,sizeof(x));
  115. }
  116. for(i=0;i<ISAAC_SZ;i+=8){
  117. for(j=0;j<8;j++)x[j]+=m[i+j];
  118. isaac_mix(x);
  119. memcpy(m+i,x,sizeof(x));
  120. }
  121. isaac_update(_ctx);
  122. }
  123. uint32_t isaac_next_uint32(isaac_ctx *_ctx){
  124. if(!_ctx->n)isaac_update(_ctx);
  125. return _ctx->r[--_ctx->n];
  126. }
  127. uint32_t isaac_next_uint(isaac_ctx *_ctx,uint32_t _n){
  128. uint32_t r;
  129. uint32_t v;
  130. uint32_t d;
  131. do{
  132. r=isaac_next_uint32(_ctx);
  133. v=r%_n;
  134. d=r-v;
  135. }
  136. while(((d+_n-1)&ISAAC_MASK)<d);
  137. return v;
  138. }
  139. /*Returns a uniform random float.
  140. The expected value is within FLT_MIN (e.g., 1E-37) of 0.5.
  141. _bits: An initial set of random bits.
  142. _base: This should be -(the number of bits in _bits), up to -32.
  143. Return: A float uniformly distributed between 0 (inclusive) and 1
  144. (exclusive).
  145. The average value was measured over 2**32 samples to be
  146. 0.50000037448772916.*/
  147. static float isaac_float_bits(isaac_ctx *_ctx,uint32_t _bits,int _base){
  148. float ret;
  149. int nbits_needed;
  150. while(!_bits){
  151. if(_base+FLT_MANT_DIG<FLT_MIN_EXP)return 0;
  152. _base-=32;
  153. _bits=isaac_next_uint32(_ctx);
  154. }
  155. /*Note: This could also be determined with frexp(), for a slightly more
  156. portable solution, but that takes twice as long, and one has to worry
  157. about rounding effects, which can over-estimate the exponent when given
  158. FLT_MANT_DIG+1 consecutive one bits.
  159. Even the fallback C implementation of ILOGNZ_32() yields an implementation
  160. 25% faster than the frexp() method.*/
  161. nbits_needed=FLT_MANT_DIG-ilog32_nz(_bits);
  162. #if FLT_MANT_DIG>32
  163. ret=ldexpf((float)_bits,_base);
  164. # if FLT_MANT_DIG>65
  165. while(32-nbits_needed<0){
  166. # else
  167. if(32-nbits_needed<0){
  168. # endif
  169. _base-=32;
  170. nbits_needed-=32;
  171. ret+=ldexpf((float)isaac_next_uint32(_ctx),_base);
  172. }
  173. _bits=isaac_next_uint32(_ctx)>>32-nbits_needed;
  174. ret+=ldexpf((float)_bits,_base-nbits_needed);
  175. #else
  176. if(nbits_needed>0){
  177. _bits=_bits<<nbits_needed|isaac_next_uint32(_ctx)>>(32-nbits_needed);
  178. }
  179. # if FLT_MANT_DIG<32
  180. else _bits>>=-nbits_needed;
  181. # endif
  182. ret=ldexpf((float)_bits,_base-nbits_needed);
  183. #endif
  184. return ret;
  185. }
  186. float isaac_next_float(isaac_ctx *_ctx){
  187. return isaac_float_bits(_ctx,0,0);
  188. }
  189. float isaac_next_signed_float(isaac_ctx *_ctx){
  190. uint32_t bits;
  191. bits=isaac_next_uint32(_ctx);
  192. return (1|-((int)bits&1))*isaac_float_bits(_ctx,bits>>1,-31);
  193. }
  194. /*Returns a uniform random double.
  195. _bits: An initial set of random bits.
  196. _base: This should be -(the number of bits in _bits), up to -32.
  197. Return: A double uniformly distributed between 0 (inclusive) and 1
  198. (exclusive).
  199. The average value was measured over 2**32 samples to be
  200. 0.500006289408060911*/
  201. static double isaac_double_bits(isaac_ctx *_ctx,uint32_t _bits,int _base){
  202. double ret;
  203. int nbits_needed;
  204. while(!_bits){
  205. if(_base+DBL_MANT_DIG<DBL_MIN_EXP)return 0;
  206. _base-=32;
  207. _bits=isaac_next_uint32(_ctx);
  208. }
  209. nbits_needed=DBL_MANT_DIG-ilog32_nz(_bits);
  210. #if DBL_MANT_DIG>32
  211. ret=ldexp((double)_bits,_base);
  212. # if DBL_MANT_DIG>65
  213. while(32-nbits_needed<0){
  214. # else
  215. if(32-nbits_needed<0){
  216. # endif
  217. _base-=32;
  218. nbits_needed-=32;
  219. ret+=ldexp((double)isaac_next_uint32(_ctx),_base);
  220. }
  221. _bits=isaac_next_uint32(_ctx)>>(32-nbits_needed);
  222. ret+=ldexp((double)_bits,_base-nbits_needed);
  223. #else
  224. if(nbits_needed>0){
  225. _bits=_bits<<nbits_needed|isaac_next_uint32(_ctx)>>32-nbits_needed;
  226. }
  227. # if DBL_MANT_DIG<32
  228. else _bits>>=-nbits_needed;
  229. # endif
  230. ret=ldexp((double)_bits,exp-DBL_MANT_DIG);
  231. #endif
  232. return ret;
  233. }
  234. double isaac_next_double(isaac_ctx *_ctx){
  235. return isaac_double_bits(_ctx,0,0);
  236. }
  237. double isaac_next_signed_double(isaac_ctx *_ctx){
  238. uint32_t bits;
  239. bits=isaac_next_uint32(_ctx);
  240. return (1|-((int)bits&1))*isaac_double_bits(_ctx,bits>>1,-31);
  241. }