gsl_rng__ranlxd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* rng/ranlxd.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 Martin Luescher's second generation
  23. double-precision (48-bit) version of the RANLUX generator.
  24. Thanks to Martin Luescher for providing information on this
  25. generator.
  26. */
  27. static inline unsigned long int ranlxd_get (void *vstate);
  28. static double ranlxd_get_double (void *vstate);
  29. static void ranlxd_set_lux (void *state, unsigned long int s, unsigned int luxury);
  30. static void ranlxd1_set (void *state, unsigned long int s);
  31. static void ranlxd2_set (void *state, unsigned long int s);
  32. static const int next[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0};
  33. static const double one_bit = 1.0 / 281474976710656.0; /* 1/2^48 */
  34. #define RANLUX_STEP(x1,x2,i1,i2,i3) \
  35. x1=xdbl[i1] - xdbl[i2]; \
  36. if (x2 < 0) \
  37. { \
  38. x1-=one_bit; \
  39. x2+=1; \
  40. } \
  41. xdbl[i3]=x2
  42. typedef struct
  43. {
  44. double xdbl[12];
  45. double carry;
  46. unsigned int ir;
  47. unsigned int jr;
  48. unsigned int ir_old;
  49. unsigned int pr;
  50. }
  51. ranlxd_state_t;
  52. static inline void increment_state (ranlxd_state_t * state);
  53. static inline void
  54. increment_state (ranlxd_state_t * state)
  55. {
  56. int k, kmax;
  57. double y1, y2, y3;
  58. double *xdbl = state->xdbl;
  59. double carry = state->carry;
  60. unsigned int ir = state->ir;
  61. unsigned int jr = state->jr;
  62. for (k = 0; ir > 0; ++k)
  63. {
  64. y1 = xdbl[jr] - xdbl[ir];
  65. y2 = y1 - carry;
  66. if (y2 < 0)
  67. {
  68. carry = one_bit;
  69. y2 += 1;
  70. }
  71. else
  72. {
  73. carry = 0;
  74. }
  75. xdbl[ir] = y2;
  76. ir = next[ir];
  77. jr = next[jr];
  78. }
  79. kmax = state->pr - 12;
  80. for (; k <= kmax; k += 12)
  81. {
  82. y1 = xdbl[7] - xdbl[0];
  83. y1 -= carry;
  84. RANLUX_STEP (y2, y1, 8, 1, 0);
  85. RANLUX_STEP (y3, y2, 9, 2, 1);
  86. RANLUX_STEP (y1, y3, 10, 3, 2);
  87. RANLUX_STEP (y2, y1, 11, 4, 3);
  88. RANLUX_STEP (y3, y2, 0, 5, 4);
  89. RANLUX_STEP (y1, y3, 1, 6, 5);
  90. RANLUX_STEP (y2, y1, 2, 7, 6);
  91. RANLUX_STEP (y3, y2, 3, 8, 7);
  92. RANLUX_STEP (y1, y3, 4, 9, 8);
  93. RANLUX_STEP (y2, y1, 5, 10, 9);
  94. RANLUX_STEP (y3, y2, 6, 11, 10);
  95. if (y3 < 0)
  96. {
  97. carry = one_bit;
  98. y3 += 1;
  99. }
  100. else
  101. {
  102. carry = 0;
  103. }
  104. xdbl[11] = y3;
  105. }
  106. kmax = state->pr;
  107. for (; k < kmax; ++k)
  108. {
  109. y1 = xdbl[jr] - xdbl[ir];
  110. y2 = y1 - carry;
  111. if (y2 < 0)
  112. {
  113. carry = one_bit;
  114. y2 += 1;
  115. }
  116. else
  117. {
  118. carry = 0;
  119. }
  120. xdbl[ir] = y2;
  121. ir = next[ir];
  122. jr = next[jr];
  123. }
  124. state->ir = ir;
  125. state->ir_old = ir;
  126. state->jr = jr;
  127. state->carry = carry;
  128. }
  129. static inline unsigned long int
  130. ranlxd_get (void *vstate)
  131. {
  132. return ranlxd_get_double (vstate) * 4294967296.0; /* 2^32 */
  133. }
  134. static double
  135. ranlxd_get_double (void *vstate)
  136. {
  137. ranlxd_state_t *state = (ranlxd_state_t *) vstate;
  138. int ir = state->ir;
  139. state->ir = next[ir];
  140. if (state->ir == state->ir_old)
  141. increment_state (state);
  142. return state->xdbl[state->ir];
  143. }
  144. static void
  145. ranlxd_set_lux (void *vstate, unsigned long int s, unsigned int luxury)
  146. {
  147. ranlxd_state_t *state = (ranlxd_state_t *) vstate;
  148. int ibit, jbit, i, k, l, xbit[31];
  149. double x, y;
  150. long int seed;
  151. if (s == 0)
  152. s = 1; /* default seed is 1 */
  153. seed = s;
  154. i = seed & 0xFFFFFFFFUL;
  155. for (k = 0; k < 31; ++k)
  156. {
  157. xbit[k] = i % 2;
  158. i /= 2;
  159. }
  160. ibit = 0;
  161. jbit = 18;
  162. for (k = 0; k < 12; ++k)
  163. {
  164. x = 0;
  165. for (l = 1; l <= 48; ++l)
  166. {
  167. y = (double) ((xbit[ibit] + 1) % 2);
  168. x += x + y;
  169. xbit[ibit] = (xbit[ibit] + xbit[jbit]) % 2;
  170. ibit = (ibit + 1) % 31;
  171. jbit = (jbit + 1) % 31;
  172. }
  173. state->xdbl[k] = one_bit * x;
  174. }
  175. state->carry = 0;
  176. state->ir = 11;
  177. state->jr = 7;
  178. state->ir_old = 0;
  179. state->pr = luxury;
  180. }
  181. static void
  182. ranlxd1_set (void *vstate, unsigned long int s)
  183. {
  184. ranlxd_set_lux (vstate, s, 202);
  185. }
  186. static void
  187. ranlxd2_set (void *vstate, unsigned long int s)
  188. {
  189. ranlxd_set_lux (vstate, s, 397);
  190. }
  191. static const gsl_rng_type ranlxd1_type =
  192. {"ranlxd1", /* name */
  193. 0xffffffffUL, /* RAND_MAX */
  194. 0, /* RAND_MIN */
  195. sizeof (ranlxd_state_t),
  196. &ranlxd1_set,
  197. &ranlxd_get,
  198. &ranlxd_get_double};
  199. static const gsl_rng_type ranlxd2_type =
  200. {"ranlxd2", /* name */
  201. 0xffffffffUL, /* RAND_MAX */
  202. 0, /* RAND_MIN */
  203. sizeof (ranlxd_state_t),
  204. &ranlxd2_set,
  205. &ranlxd_get,
  206. &ranlxd_get_double};
  207. const gsl_rng_type *gsl_rng_ranlxd1 = &ranlxd1_type;
  208. const gsl_rng_type *gsl_rng_ranlxd2 = &ranlxd2_type;