gsl_rng__ranlxs.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* rng/ranlxs.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include "gsl_rng.h"
  22. /* This is an implementation of M. Luescher's second generation
  23. version of the RANLUX generator.
  24. Thanks to Martin Luescher for providing information on this
  25. generator.
  26. */
  27. static unsigned long int ranlxs_get (void *vstate);
  28. static inline double ranlxs_get_double (void *vstate);
  29. static void ranlxs_set_lux (void *state, unsigned long int s, unsigned int luxury);
  30. static void ranlxs0_set (void *state, unsigned long int s);
  31. static void ranlxs1_set (void *state, unsigned long int s);
  32. static void ranlxs2_set (void *state, unsigned long int s);
  33. static const int next[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0};
  34. static const int snext[24] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  35. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0};
  36. static const double sbase = 16777216.0; /* 2^24 */
  37. static const double sone_bit = 1.0 / 16777216.0; /* 1/2^24 */
  38. static const double one_bit = 1.0 / 281474976710656.0; /* 1/2^48 */
  39. static const double shift = 268435456.0; /* 2^28 */
  40. #define RANLUX_STEP(x1,x2,i1,i2,i3) \
  41. x1=xdbl[i1] - xdbl[i2]; \
  42. if (x2 < 0) \
  43. { \
  44. x1-=one_bit; \
  45. x2+=1; \
  46. } \
  47. xdbl[i3]=x2
  48. typedef struct
  49. {
  50. double xdbl[12], ydbl[12]; /* doubles first so they are 8-byte aligned */
  51. double carry;
  52. float xflt[24];
  53. unsigned int ir;
  54. unsigned int jr;
  55. unsigned int is;
  56. unsigned int is_old;
  57. unsigned int pr;
  58. }
  59. ranlxs_state_t;
  60. static void increment_state (ranlxs_state_t * state);
  61. static void
  62. increment_state (ranlxs_state_t * state)
  63. {
  64. int k, kmax, m;
  65. double x, y1, y2, y3;
  66. float *xflt = state->xflt;
  67. double *xdbl = state->xdbl;
  68. double *ydbl = state->ydbl;
  69. double carry = state->carry;
  70. unsigned int ir = state->ir;
  71. unsigned int jr = state->jr;
  72. for (k = 0; ir > 0; ++k)
  73. {
  74. y1 = xdbl[jr] - xdbl[ir];
  75. y2 = y1 - carry;
  76. if (y2 < 0)
  77. {
  78. carry = one_bit;
  79. y2 += 1;
  80. }
  81. else
  82. {
  83. carry = 0;
  84. }
  85. xdbl[ir] = y2;
  86. ir = next[ir];
  87. jr = next[jr];
  88. }
  89. kmax = state->pr - 12;
  90. for (; k <= kmax; k += 12)
  91. {
  92. y1 = xdbl[7] - xdbl[0];
  93. y1 -= carry;
  94. RANLUX_STEP (y2, y1, 8, 1, 0);
  95. RANLUX_STEP (y3, y2, 9, 2, 1);
  96. RANLUX_STEP (y1, y3, 10, 3, 2);
  97. RANLUX_STEP (y2, y1, 11, 4, 3);
  98. RANLUX_STEP (y3, y2, 0, 5, 4);
  99. RANLUX_STEP (y1, y3, 1, 6, 5);
  100. RANLUX_STEP (y2, y1, 2, 7, 6);
  101. RANLUX_STEP (y3, y2, 3, 8, 7);
  102. RANLUX_STEP (y1, y3, 4, 9, 8);
  103. RANLUX_STEP (y2, y1, 5, 10, 9);
  104. RANLUX_STEP (y3, y2, 6, 11, 10);
  105. if (y3 < 0)
  106. {
  107. carry = one_bit;
  108. y3 += 1;
  109. }
  110. else
  111. {
  112. carry = 0;
  113. }
  114. xdbl[11] = y3;
  115. }
  116. kmax = state->pr;
  117. for (; k < kmax; ++k)
  118. {
  119. y1 = xdbl[jr] - xdbl[ir];
  120. y2 = y1 - carry;
  121. if (y2 < 0)
  122. {
  123. carry = one_bit;
  124. y2 += 1;
  125. }
  126. else
  127. {
  128. carry = 0;
  129. }
  130. xdbl[ir] = y2;
  131. ydbl[ir] = y2 + shift;
  132. ir = next[ir];
  133. jr = next[jr];
  134. }
  135. ydbl[ir] = xdbl[ir] + shift;
  136. for (k = next[ir]; k > 0;)
  137. {
  138. ydbl[k] = xdbl[k] + shift;
  139. k = next[k];
  140. }
  141. for (k = 0, m = 0; k < 12; ++k)
  142. {
  143. x = xdbl[k];
  144. y2 = ydbl[k] - shift;
  145. if (y2 > x)
  146. y2 -= sone_bit;
  147. y1 = (x - y2) * sbase;
  148. xflt[m++] = (float) y1;
  149. xflt[m++] = (float) y2;
  150. }
  151. state->ir = ir;
  152. state->is = 2 * ir;
  153. state->is_old = 2 * ir;
  154. state->jr = jr;
  155. state->carry = carry;
  156. }
  157. static inline double
  158. ranlxs_get_double (void *vstate)
  159. {
  160. ranlxs_state_t *state = (ranlxs_state_t *) vstate;
  161. const unsigned int is = snext[state->is];
  162. state->is = is;
  163. if (is == state->is_old)
  164. increment_state (state);
  165. return state->xflt[state->is];
  166. }
  167. static unsigned long int
  168. ranlxs_get (void *vstate)
  169. {
  170. return ranlxs_get_double (vstate) * 16777216.0; /* 2^24 */
  171. }
  172. static void
  173. ranlxs_set_lux (void *vstate, unsigned long int s, unsigned int luxury)
  174. {
  175. ranlxs_state_t *state = (ranlxs_state_t *) vstate;
  176. int ibit, jbit, i, k, m, xbit[31];
  177. double x, y;
  178. long int seed;
  179. if (s == 0)
  180. s = 1; /* default seed is 1 */
  181. seed = s;
  182. i = seed & 0xFFFFFFFFUL;
  183. for (k = 0; k < 31; ++k)
  184. {
  185. xbit[k] = i % 2;
  186. i /= 2;
  187. }
  188. ibit = 0;
  189. jbit = 18;
  190. for (k = 0; k < 12; ++k)
  191. {
  192. x = 0;
  193. for (m = 1; m <= 48; ++m)
  194. {
  195. y = (double) xbit[ibit];
  196. x += x + y;
  197. xbit[ibit] = (xbit[ibit] + xbit[jbit]) % 2;
  198. ibit = (ibit + 1) % 31;
  199. jbit = (jbit + 1) % 31;
  200. }
  201. state->xdbl[k] = one_bit * x;
  202. }
  203. state->carry = 0;
  204. state->ir = 0;
  205. state->jr = 7;
  206. state->is = 23;
  207. state->is_old = 0;
  208. state->pr = luxury;
  209. }
  210. static void
  211. ranlxs0_set (void *vstate, unsigned long int s)
  212. {
  213. ranlxs_set_lux (vstate, s, 109);
  214. }
  215. void
  216. ranlxs1_set (void *vstate, unsigned long int s)
  217. {
  218. ranlxs_set_lux (vstate, s, 202);
  219. }
  220. static void
  221. ranlxs2_set (void *vstate, unsigned long int s)
  222. {
  223. ranlxs_set_lux (vstate, s, 397);
  224. }
  225. static const gsl_rng_type ranlxs0_type =
  226. {"ranlxs0", /* name */
  227. 0x00ffffffUL, /* RAND_MAX */
  228. 0, /* RAND_MIN */
  229. sizeof (ranlxs_state_t),
  230. &ranlxs0_set,
  231. &ranlxs_get,
  232. &ranlxs_get_double};
  233. static const gsl_rng_type ranlxs1_type =
  234. {"ranlxs1", /* name */
  235. 0x00ffffffUL, /* RAND_MAX */
  236. 0, /* RAND_MIN */
  237. sizeof (ranlxs_state_t),
  238. &ranlxs1_set,
  239. &ranlxs_get,
  240. &ranlxs_get_double};
  241. static const gsl_rng_type ranlxs2_type =
  242. {"ranlxs2", /* name */
  243. 0x00ffffffUL, /* RAND_MAX */
  244. 0, /* RAND_MIN */
  245. sizeof (ranlxs_state_t),
  246. &ranlxs2_set,
  247. &ranlxs_get,
  248. &ranlxs_get_double};
  249. const gsl_rng_type *gsl_rng_ranlxs0 = &ranlxs0_type;
  250. const gsl_rng_type *gsl_rng_ranlxs1 = &ranlxs1_type;
  251. const gsl_rng_type *gsl_rng_ranlxs2 = &ranlxs2_type;