random.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /* Implementation of the RANDOM intrinsics
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Lars Segerlund <seger@linuxmail.org>
  4. and Steve Kargl.
  5. This file is part of the GNU Fortran runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public
  8. License as published by the Free Software Foundation; either
  9. version 3 of the License, or (at your option) any later version.
  10. Ligbfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "libgfortran.h"
  22. #include <gthr.h>
  23. #include <string.h>
  24. extern void random_r4 (GFC_REAL_4 *);
  25. iexport_proto(random_r4);
  26. extern void random_r8 (GFC_REAL_8 *);
  27. iexport_proto(random_r8);
  28. extern void arandom_r4 (gfc_array_r4 *);
  29. export_proto(arandom_r4);
  30. extern void arandom_r8 (gfc_array_r8 *);
  31. export_proto(arandom_r8);
  32. #ifdef HAVE_GFC_REAL_10
  33. extern void random_r10 (GFC_REAL_10 *);
  34. iexport_proto(random_r10);
  35. extern void arandom_r10 (gfc_array_r10 *);
  36. export_proto(arandom_r10);
  37. #endif
  38. #ifdef HAVE_GFC_REAL_16
  39. extern void random_r16 (GFC_REAL_16 *);
  40. iexport_proto(random_r16);
  41. extern void arandom_r16 (gfc_array_r16 *);
  42. export_proto(arandom_r16);
  43. #endif
  44. #ifdef __GTHREAD_MUTEX_INIT
  45. static __gthread_mutex_t random_lock = __GTHREAD_MUTEX_INIT;
  46. #else
  47. static __gthread_mutex_t random_lock;
  48. #endif
  49. /* Helper routines to map a GFC_UINTEGER_* to the corresponding
  50. GFC_REAL_* types in the range of [0,1). If GFC_REAL_*_RADIX are 2
  51. or 16, respectively, we mask off the bits that don't fit into the
  52. correct GFC_REAL_*, convert to the real type, then multiply by the
  53. correct offset. */
  54. static void
  55. rnumber_4 (GFC_REAL_4 *f, GFC_UINTEGER_4 v)
  56. {
  57. GFC_UINTEGER_4 mask;
  58. #if GFC_REAL_4_RADIX == 2
  59. mask = ~ (GFC_UINTEGER_4) 0u << (32 - GFC_REAL_4_DIGITS);
  60. #elif GFC_REAL_4_RADIX == 16
  61. mask = ~ (GFC_UINTEGER_4) 0u << ((8 - GFC_REAL_4_DIGITS) * 4);
  62. #else
  63. #error "GFC_REAL_4_RADIX has unknown value"
  64. #endif
  65. v = v & mask;
  66. *f = (GFC_REAL_4) v * GFC_REAL_4_LITERAL(0x1.p-32);
  67. }
  68. static void
  69. rnumber_8 (GFC_REAL_8 *f, GFC_UINTEGER_8 v)
  70. {
  71. GFC_UINTEGER_8 mask;
  72. #if GFC_REAL_8_RADIX == 2
  73. mask = ~ (GFC_UINTEGER_8) 0u << (64 - GFC_REAL_8_DIGITS);
  74. #elif GFC_REAL_8_RADIX == 16
  75. mask = ~ (GFC_UINTEGER_8) 0u << (16 - GFC_REAL_8_DIGITS) * 4);
  76. #else
  77. #error "GFC_REAL_8_RADIX has unknown value"
  78. #endif
  79. v = v & mask;
  80. *f = (GFC_REAL_8) v * GFC_REAL_8_LITERAL(0x1.p-64);
  81. }
  82. #ifdef HAVE_GFC_REAL_10
  83. static void
  84. rnumber_10 (GFC_REAL_10 *f, GFC_UINTEGER_8 v)
  85. {
  86. GFC_UINTEGER_8 mask;
  87. #if GFC_REAL_10_RADIX == 2
  88. mask = ~ (GFC_UINTEGER_8) 0u << (64 - GFC_REAL_10_DIGITS);
  89. #elif GFC_REAL_10_RADIX == 16
  90. mask = ~ (GFC_UINTEGER_10) 0u << ((16 - GFC_REAL_10_DIGITS) * 4);
  91. #else
  92. #error "GFC_REAL_10_RADIX has unknown value"
  93. #endif
  94. v = v & mask;
  95. *f = (GFC_REAL_10) v * GFC_REAL_10_LITERAL(0x1.p-64);
  96. }
  97. #endif
  98. #ifdef HAVE_GFC_REAL_16
  99. /* For REAL(KIND=16), we only need to mask off the lower bits. */
  100. static void
  101. rnumber_16 (GFC_REAL_16 *f, GFC_UINTEGER_8 v1, GFC_UINTEGER_8 v2)
  102. {
  103. GFC_UINTEGER_8 mask;
  104. #if GFC_REAL_16_RADIX == 2
  105. mask = ~ (GFC_UINTEGER_8) 0u << (128 - GFC_REAL_16_DIGITS);
  106. #elif GFC_REAL_16_RADIX == 16
  107. mask = ~ (GFC_UINTEGER_8) 0u << ((32 - GFC_REAL_16_DIGITS) * 4);
  108. #else
  109. #error "GFC_REAL_16_RADIX has unknown value"
  110. #endif
  111. v2 = v2 & mask;
  112. *f = (GFC_REAL_16) v1 * GFC_REAL_16_LITERAL(0x1.p-64)
  113. + (GFC_REAL_16) v2 * GFC_REAL_16_LITERAL(0x1.p-128);
  114. }
  115. #endif
  116. /* libgfortran previously had a Mersenne Twister, taken from the paper:
  117. Mersenne Twister: 623-dimensionally equidistributed
  118. uniform pseudorandom generator.
  119. by Makoto Matsumoto & Takuji Nishimura
  120. which appeared in the: ACM Transactions on Modelling and Computer
  121. Simulations: Special Issue on Uniform Random Number
  122. Generation. ( Early in 1998 ).
  123. The Mersenne Twister code was replaced due to
  124. (1) Simple user specified seeds lead to really bad sequences for
  125. nearly 100000 random numbers.
  126. (2) open(), read(), and close() were not properly declared via header
  127. files.
  128. (3) The global index i was abused and caused unexpected behavior with
  129. GET and PUT.
  130. (4) See PR 15619.
  131. libgfortran currently uses George Marsaglia's KISS (Keep It Simple Stupid)
  132. random number generator. This PRNG combines:
  133. (1) The congruential generator x(n)=69069*x(n-1)+1327217885 with a period
  134. of 2^32,
  135. (2) A 3-shift shift-register generator with a period of 2^32-1,
  136. (3) Two 16-bit multiply-with-carry generators with a period of
  137. 597273182964842497 > 2^59.
  138. The overall period exceeds 2^123.
  139. http://www.ciphersbyritter.com/NEWS4/RANDC.HTM#369F6FCA.74C7C041@stat.fsu.edu
  140. The above web site has an archive of a newsgroup posting from George
  141. Marsaglia with the statement:
  142. Subject: Random numbers for C: Improvements.
  143. Date: Fri, 15 Jan 1999 11:41:47 -0500
  144. From: George Marsaglia <geo@stat.fsu.edu>
  145. Message-ID: <369F6FCA.74C7C041@stat.fsu.edu>
  146. References: <369B5E30.65A55FD1@stat.fsu.edu>
  147. Newsgroups: sci.stat.math,sci.math,sci.math.numer-analysis
  148. Lines: 93
  149. As I hoped, several suggestions have led to
  150. improvements in the code for RNG's I proposed for
  151. use in C. (See the thread "Random numbers for C: Some
  152. suggestions" in previous postings.) The improved code
  153. is listed below.
  154. A question of copyright has also been raised. Unlike
  155. DIEHARD, there is no copyright on the code below. You
  156. are free to use it in any way you want, but you may
  157. wish to acknowledge the source, as a courtesy.
  158. "There is no copyright on the code below." included the original
  159. KISS algorithm. */
  160. /* We use three KISS random number generators, with different
  161. seeds.
  162. As a matter of Quality of Implementation, the random numbers
  163. we generate for different REAL kinds, starting from the same
  164. seed, are always the same up to the precision of these types.
  165. We do this by using three generators with different seeds, the
  166. first one always for the most significant bits, the second one
  167. for bits 33..64 (if present in the REAL kind), and the third one
  168. (called twice) for REAL(16). */
  169. #define GFC_SL(k, n) ((k)^((k)<<(n)))
  170. #define GFC_SR(k, n) ((k)^((k)>>(n)))
  171. /* Reference for the seed:
  172. From: "George Marsaglia" <g...@stat.fsu.edu>
  173. Newsgroups: sci.math
  174. Message-ID: <e7CcnWxczriWssCjXTWc3A@comcast.com>
  175. The KISS RNG uses four seeds, x, y, z, c,
  176. with 0<=x<2^32, 0<y<2^32, 0<=z<2^32, 0<=c<698769069
  177. except that the two pairs
  178. z=0,c=0 and z=2^32-1,c=698769068
  179. should be avoided. */
  180. /* Any modifications to the seeds that change KISS_SIZE below need to be
  181. reflected in check.c (gfc_check_random_seed) to enable correct
  182. compile-time checking of PUT size for the RANDOM_SEED intrinsic. */
  183. #define KISS_DEFAULT_SEED_1 123456789, 362436069, 521288629, 316191069
  184. #define KISS_DEFAULT_SEED_2 987654321, 458629013, 582859209, 438195021
  185. #ifdef HAVE_GFC_REAL_16
  186. #define KISS_DEFAULT_SEED_3 573658661, 185639104, 582619469, 296736107
  187. #endif
  188. static GFC_UINTEGER_4 kiss_seed[] = {
  189. KISS_DEFAULT_SEED_1,
  190. KISS_DEFAULT_SEED_2,
  191. #ifdef HAVE_GFC_REAL_16
  192. KISS_DEFAULT_SEED_3
  193. #endif
  194. };
  195. static GFC_UINTEGER_4 kiss_default_seed[] = {
  196. KISS_DEFAULT_SEED_1,
  197. KISS_DEFAULT_SEED_2,
  198. #ifdef HAVE_GFC_REAL_16
  199. KISS_DEFAULT_SEED_3
  200. #endif
  201. };
  202. #define KISS_SIZE (sizeof(kiss_seed)/sizeof(kiss_seed[0]))
  203. static GFC_UINTEGER_4 * const kiss_seed_1 = kiss_seed;
  204. static GFC_UINTEGER_4 * const kiss_seed_2 = kiss_seed + 4;
  205. #ifdef HAVE_GFC_REAL_16
  206. static GFC_UINTEGER_4 * const kiss_seed_3 = kiss_seed + 8;
  207. #endif
  208. /* kiss_random_kernel() returns an integer value in the range of
  209. (0, GFC_UINTEGER_4_HUGE]. The distribution of pseudorandom numbers
  210. should be uniform. */
  211. static GFC_UINTEGER_4
  212. kiss_random_kernel(GFC_UINTEGER_4 * seed)
  213. {
  214. GFC_UINTEGER_4 kiss;
  215. seed[0] = 69069 * seed[0] + 1327217885;
  216. seed[1] = GFC_SL(GFC_SR(GFC_SL(seed[1],13),17),5);
  217. seed[2] = 18000 * (seed[2] & 65535) + (seed[2] >> 16);
  218. seed[3] = 30903 * (seed[3] & 65535) + (seed[3] >> 16);
  219. kiss = seed[0] + seed[1] + (seed[2] << 16) + seed[3];
  220. return kiss;
  221. }
  222. /* This function produces a REAL(4) value from the uniform distribution
  223. with range [0,1). */
  224. void
  225. random_r4 (GFC_REAL_4 *x)
  226. {
  227. GFC_UINTEGER_4 kiss;
  228. __gthread_mutex_lock (&random_lock);
  229. kiss = kiss_random_kernel (kiss_seed_1);
  230. rnumber_4 (x, kiss);
  231. __gthread_mutex_unlock (&random_lock);
  232. }
  233. iexport(random_r4);
  234. /* This function produces a REAL(8) value from the uniform distribution
  235. with range [0,1). */
  236. void
  237. random_r8 (GFC_REAL_8 *x)
  238. {
  239. GFC_UINTEGER_8 kiss;
  240. __gthread_mutex_lock (&random_lock);
  241. kiss = ((GFC_UINTEGER_8) kiss_random_kernel (kiss_seed_1)) << 32;
  242. kiss += kiss_random_kernel (kiss_seed_2);
  243. rnumber_8 (x, kiss);
  244. __gthread_mutex_unlock (&random_lock);
  245. }
  246. iexport(random_r8);
  247. #ifdef HAVE_GFC_REAL_10
  248. /* This function produces a REAL(10) value from the uniform distribution
  249. with range [0,1). */
  250. void
  251. random_r10 (GFC_REAL_10 *x)
  252. {
  253. GFC_UINTEGER_8 kiss;
  254. __gthread_mutex_lock (&random_lock);
  255. kiss = ((GFC_UINTEGER_8) kiss_random_kernel (kiss_seed_1)) << 32;
  256. kiss += kiss_random_kernel (kiss_seed_2);
  257. rnumber_10 (x, kiss);
  258. __gthread_mutex_unlock (&random_lock);
  259. }
  260. iexport(random_r10);
  261. #endif
  262. /* This function produces a REAL(16) value from the uniform distribution
  263. with range [0,1). */
  264. #ifdef HAVE_GFC_REAL_16
  265. void
  266. random_r16 (GFC_REAL_16 *x)
  267. {
  268. GFC_UINTEGER_8 kiss1, kiss2;
  269. __gthread_mutex_lock (&random_lock);
  270. kiss1 = ((GFC_UINTEGER_8) kiss_random_kernel (kiss_seed_1)) << 32;
  271. kiss1 += kiss_random_kernel (kiss_seed_2);
  272. kiss2 = ((GFC_UINTEGER_8) kiss_random_kernel (kiss_seed_3)) << 32;
  273. kiss2 += kiss_random_kernel (kiss_seed_3);
  274. rnumber_16 (x, kiss1, kiss2);
  275. __gthread_mutex_unlock (&random_lock);
  276. }
  277. iexport(random_r16);
  278. #endif
  279. /* This function fills a REAL(4) array with values from the uniform
  280. distribution with range [0,1). */
  281. void
  282. arandom_r4 (gfc_array_r4 *x)
  283. {
  284. index_type count[GFC_MAX_DIMENSIONS];
  285. index_type extent[GFC_MAX_DIMENSIONS];
  286. index_type stride[GFC_MAX_DIMENSIONS];
  287. index_type stride0;
  288. index_type dim;
  289. GFC_REAL_4 *dest;
  290. GFC_UINTEGER_4 kiss;
  291. int n;
  292. dest = x->base_addr;
  293. dim = GFC_DESCRIPTOR_RANK (x);
  294. for (n = 0; n < dim; n++)
  295. {
  296. count[n] = 0;
  297. stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
  298. extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
  299. if (extent[n] <= 0)
  300. return;
  301. }
  302. stride0 = stride[0];
  303. __gthread_mutex_lock (&random_lock);
  304. while (dest)
  305. {
  306. /* random_r4 (dest); */
  307. kiss = kiss_random_kernel (kiss_seed_1);
  308. rnumber_4 (dest, kiss);
  309. /* Advance to the next element. */
  310. dest += stride0;
  311. count[0]++;
  312. /* Advance to the next source element. */
  313. n = 0;
  314. while (count[n] == extent[n])
  315. {
  316. /* When we get to the end of a dimension, reset it and increment
  317. the next dimension. */
  318. count[n] = 0;
  319. /* We could precalculate these products, but this is a less
  320. frequently used path so probably not worth it. */
  321. dest -= stride[n] * extent[n];
  322. n++;
  323. if (n == dim)
  324. {
  325. dest = NULL;
  326. break;
  327. }
  328. else
  329. {
  330. count[n]++;
  331. dest += stride[n];
  332. }
  333. }
  334. }
  335. __gthread_mutex_unlock (&random_lock);
  336. }
  337. /* This function fills a REAL(8) array with values from the uniform
  338. distribution with range [0,1). */
  339. void
  340. arandom_r8 (gfc_array_r8 *x)
  341. {
  342. index_type count[GFC_MAX_DIMENSIONS];
  343. index_type extent[GFC_MAX_DIMENSIONS];
  344. index_type stride[GFC_MAX_DIMENSIONS];
  345. index_type stride0;
  346. index_type dim;
  347. GFC_REAL_8 *dest;
  348. GFC_UINTEGER_8 kiss;
  349. int n;
  350. dest = x->base_addr;
  351. dim = GFC_DESCRIPTOR_RANK (x);
  352. for (n = 0; n < dim; n++)
  353. {
  354. count[n] = 0;
  355. stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
  356. extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
  357. if (extent[n] <= 0)
  358. return;
  359. }
  360. stride0 = stride[0];
  361. __gthread_mutex_lock (&random_lock);
  362. while (dest)
  363. {
  364. /* random_r8 (dest); */
  365. kiss = ((GFC_UINTEGER_8) kiss_random_kernel (kiss_seed_1)) << 32;
  366. kiss += kiss_random_kernel (kiss_seed_2);
  367. rnumber_8 (dest, kiss);
  368. /* Advance to the next element. */
  369. dest += stride0;
  370. count[0]++;
  371. /* Advance to the next source element. */
  372. n = 0;
  373. while (count[n] == extent[n])
  374. {
  375. /* When we get to the end of a dimension, reset it and increment
  376. the next dimension. */
  377. count[n] = 0;
  378. /* We could precalculate these products, but this is a less
  379. frequently used path so probably not worth it. */
  380. dest -= stride[n] * extent[n];
  381. n++;
  382. if (n == dim)
  383. {
  384. dest = NULL;
  385. break;
  386. }
  387. else
  388. {
  389. count[n]++;
  390. dest += stride[n];
  391. }
  392. }
  393. }
  394. __gthread_mutex_unlock (&random_lock);
  395. }
  396. #ifdef HAVE_GFC_REAL_10
  397. /* This function fills a REAL(10) array with values from the uniform
  398. distribution with range [0,1). */
  399. void
  400. arandom_r10 (gfc_array_r10 *x)
  401. {
  402. index_type count[GFC_MAX_DIMENSIONS];
  403. index_type extent[GFC_MAX_DIMENSIONS];
  404. index_type stride[GFC_MAX_DIMENSIONS];
  405. index_type stride0;
  406. index_type dim;
  407. GFC_REAL_10 *dest;
  408. GFC_UINTEGER_8 kiss;
  409. int n;
  410. dest = x->base_addr;
  411. dim = GFC_DESCRIPTOR_RANK (x);
  412. for (n = 0; n < dim; n++)
  413. {
  414. count[n] = 0;
  415. stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
  416. extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
  417. if (extent[n] <= 0)
  418. return;
  419. }
  420. stride0 = stride[0];
  421. __gthread_mutex_lock (&random_lock);
  422. while (dest)
  423. {
  424. /* random_r10 (dest); */
  425. kiss = ((GFC_UINTEGER_8) kiss_random_kernel (kiss_seed_1)) << 32;
  426. kiss += kiss_random_kernel (kiss_seed_2);
  427. rnumber_10 (dest, kiss);
  428. /* Advance to the next element. */
  429. dest += stride0;
  430. count[0]++;
  431. /* Advance to the next source element. */
  432. n = 0;
  433. while (count[n] == extent[n])
  434. {
  435. /* When we get to the end of a dimension, reset it and increment
  436. the next dimension. */
  437. count[n] = 0;
  438. /* We could precalculate these products, but this is a less
  439. frequently used path so probably not worth it. */
  440. dest -= stride[n] * extent[n];
  441. n++;
  442. if (n == dim)
  443. {
  444. dest = NULL;
  445. break;
  446. }
  447. else
  448. {
  449. count[n]++;
  450. dest += stride[n];
  451. }
  452. }
  453. }
  454. __gthread_mutex_unlock (&random_lock);
  455. }
  456. #endif
  457. #ifdef HAVE_GFC_REAL_16
  458. /* This function fills a REAL(16) array with values from the uniform
  459. distribution with range [0,1). */
  460. void
  461. arandom_r16 (gfc_array_r16 *x)
  462. {
  463. index_type count[GFC_MAX_DIMENSIONS];
  464. index_type extent[GFC_MAX_DIMENSIONS];
  465. index_type stride[GFC_MAX_DIMENSIONS];
  466. index_type stride0;
  467. index_type dim;
  468. GFC_REAL_16 *dest;
  469. GFC_UINTEGER_8 kiss1, kiss2;
  470. int n;
  471. dest = x->base_addr;
  472. dim = GFC_DESCRIPTOR_RANK (x);
  473. for (n = 0; n < dim; n++)
  474. {
  475. count[n] = 0;
  476. stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
  477. extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
  478. if (extent[n] <= 0)
  479. return;
  480. }
  481. stride0 = stride[0];
  482. __gthread_mutex_lock (&random_lock);
  483. while (dest)
  484. {
  485. /* random_r16 (dest); */
  486. kiss1 = ((GFC_UINTEGER_8) kiss_random_kernel (kiss_seed_1)) << 32;
  487. kiss1 += kiss_random_kernel (kiss_seed_2);
  488. kiss2 = ((GFC_UINTEGER_8) kiss_random_kernel (kiss_seed_3)) << 32;
  489. kiss2 += kiss_random_kernel (kiss_seed_3);
  490. rnumber_16 (dest, kiss1, kiss2);
  491. /* Advance to the next element. */
  492. dest += stride0;
  493. count[0]++;
  494. /* Advance to the next source element. */
  495. n = 0;
  496. while (count[n] == extent[n])
  497. {
  498. /* When we get to the end of a dimension, reset it and increment
  499. the next dimension. */
  500. count[n] = 0;
  501. /* We could precalculate these products, but this is a less
  502. frequently used path so probably not worth it. */
  503. dest -= stride[n] * extent[n];
  504. n++;
  505. if (n == dim)
  506. {
  507. dest = NULL;
  508. break;
  509. }
  510. else
  511. {
  512. count[n]++;
  513. dest += stride[n];
  514. }
  515. }
  516. }
  517. __gthread_mutex_unlock (&random_lock);
  518. }
  519. #endif
  520. static void
  521. scramble_seed (unsigned char *dest, unsigned char *src, int size)
  522. {
  523. int i;
  524. for (i = 0; i < size; i++)
  525. dest[(i % 2) * (size / 2) + i / 2] = src[i];
  526. }
  527. static void
  528. unscramble_seed (unsigned char *dest, unsigned char *src, int size)
  529. {
  530. int i;
  531. for (i = 0; i < size; i++)
  532. dest[i] = src[(i % 2) * (size / 2) + i / 2];
  533. }
  534. /* random_seed is used to seed the PRNG with either a default
  535. set of seeds or user specified set of seeds. random_seed
  536. must be called with no argument or exactly one argument. */
  537. void
  538. random_seed_i4 (GFC_INTEGER_4 *size, gfc_array_i4 *put, gfc_array_i4 *get)
  539. {
  540. unsigned char seed[4 * KISS_SIZE];
  541. __gthread_mutex_lock (&random_lock);
  542. /* Check that we only have one argument present. */
  543. if ((size ? 1 : 0) + (put ? 1 : 0) + (get ? 1 : 0) > 1)
  544. runtime_error ("RANDOM_SEED should have at most one argument present.");
  545. /* From the standard: "If no argument is present, the processor assigns
  546. a processor-dependent value to the seed." */
  547. if (size == NULL && put == NULL && get == NULL)
  548. for (size_t i = 0; i < KISS_SIZE; i++)
  549. kiss_seed[i] = kiss_default_seed[i];
  550. if (size != NULL)
  551. *size = KISS_SIZE;
  552. if (put != NULL)
  553. {
  554. /* If the rank of the array is not 1, abort. */
  555. if (GFC_DESCRIPTOR_RANK (put) != 1)
  556. runtime_error ("Array rank of PUT is not 1.");
  557. /* If the array is too small, abort. */
  558. if (GFC_DESCRIPTOR_EXTENT(put,0) < (index_type) KISS_SIZE)
  559. runtime_error ("Array size of PUT is too small.");
  560. /* We copy the seed given by the user. */
  561. for (size_t i = 0; i < KISS_SIZE; i++)
  562. memcpy (seed + i * sizeof(GFC_UINTEGER_4),
  563. &(put->base_addr[(KISS_SIZE - 1 - i) * GFC_DESCRIPTOR_STRIDE(put,0)]),
  564. sizeof(GFC_UINTEGER_4));
  565. /* We put it after scrambling the bytes, to paper around users who
  566. provide seeds with quality only in the lower or upper part. */
  567. scramble_seed ((unsigned char *) kiss_seed, seed, 4 * KISS_SIZE);
  568. }
  569. /* Return the seed to GET data. */
  570. if (get != NULL)
  571. {
  572. /* If the rank of the array is not 1, abort. */
  573. if (GFC_DESCRIPTOR_RANK (get) != 1)
  574. runtime_error ("Array rank of GET is not 1.");
  575. /* If the array is too small, abort. */
  576. if (GFC_DESCRIPTOR_EXTENT(get,0) < (index_type) KISS_SIZE)
  577. runtime_error ("Array size of GET is too small.");
  578. /* Unscramble the seed. */
  579. unscramble_seed (seed, (unsigned char *) kiss_seed, 4 * KISS_SIZE);
  580. /* Then copy it back to the user variable. */
  581. for (size_t i = 0; i < KISS_SIZE; i++)
  582. memcpy (&(get->base_addr[(KISS_SIZE - 1 - i) * GFC_DESCRIPTOR_STRIDE(get,0)]),
  583. seed + i * sizeof(GFC_UINTEGER_4),
  584. sizeof(GFC_UINTEGER_4));
  585. }
  586. __gthread_mutex_unlock (&random_lock);
  587. }
  588. iexport(random_seed_i4);
  589. void
  590. random_seed_i8 (GFC_INTEGER_8 *size, gfc_array_i8 *put, gfc_array_i8 *get)
  591. {
  592. __gthread_mutex_lock (&random_lock);
  593. /* Check that we only have one argument present. */
  594. if ((size ? 1 : 0) + (put ? 1 : 0) + (get ? 1 : 0) > 1)
  595. runtime_error ("RANDOM_SEED should have at most one argument present.");
  596. /* From the standard: "If no argument is present, the processor assigns
  597. a processor-dependent value to the seed." */
  598. if (size == NULL && put == NULL && get == NULL)
  599. for (size_t i = 0; i < KISS_SIZE; i++)
  600. kiss_seed[i] = kiss_default_seed[i];
  601. if (size != NULL)
  602. *size = KISS_SIZE / 2;
  603. if (put != NULL)
  604. {
  605. /* If the rank of the array is not 1, abort. */
  606. if (GFC_DESCRIPTOR_RANK (put) != 1)
  607. runtime_error ("Array rank of PUT is not 1.");
  608. /* If the array is too small, abort. */
  609. if (GFC_DESCRIPTOR_EXTENT(put,0) < (index_type) KISS_SIZE / 2)
  610. runtime_error ("Array size of PUT is too small.");
  611. /* This code now should do correct strides. */
  612. for (size_t i = 0; i < KISS_SIZE / 2; i++)
  613. memcpy (&kiss_seed[2*i], &(put->base_addr[i * GFC_DESCRIPTOR_STRIDE(put,0)]),
  614. sizeof (GFC_UINTEGER_8));
  615. }
  616. /* Return the seed to GET data. */
  617. if (get != NULL)
  618. {
  619. /* If the rank of the array is not 1, abort. */
  620. if (GFC_DESCRIPTOR_RANK (get) != 1)
  621. runtime_error ("Array rank of GET is not 1.");
  622. /* If the array is too small, abort. */
  623. if (GFC_DESCRIPTOR_EXTENT(get,0) < (index_type) KISS_SIZE / 2)
  624. runtime_error ("Array size of GET is too small.");
  625. /* This code now should do correct strides. */
  626. for (size_t i = 0; i < KISS_SIZE / 2; i++)
  627. memcpy (&(get->base_addr[i * GFC_DESCRIPTOR_STRIDE(get,0)]), &kiss_seed[2*i],
  628. sizeof (GFC_UINTEGER_8));
  629. }
  630. __gthread_mutex_unlock (&random_lock);
  631. }
  632. iexport(random_seed_i8);
  633. #ifndef __GTHREAD_MUTEX_INIT
  634. static void __attribute__((constructor))
  635. init (void)
  636. {
  637. __GTHREAD_MUTEX_INIT_FUNCTION (&random_lock);
  638. }
  639. #endif