Debian Codegraph @

libgfortran/intrinsics/random.c

raw
1 /* Implementation of the RANDOM intrinsics
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 Contributed by Lars Segerlund <[email protected]>,
4 Steve Kargl and Janne Blomqvist.
5
6 This file is part of the GNU Fortran runtime library (libgfortran).
7
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
12
13 Ligbfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27 /* For rand_s. */
28 #define _CRT_RAND_S
29
30 #include "libgfortran.h"
31 #include <gthr.h>
32 #include <string.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include "time_1.h"
40 #ifdef HAVE_SYS_RANDOM_H
41 #include <sys/random.h>
42 #endif
43
44 #ifdef __MINGW32__
45 #define HAVE_GETPID 1
46 #include <process.h>
47 #include <_mingw.h> /* For __MINGW64_VERSION_MAJOR */
48 #endif
49
50 extern void random_r4 (GFC_REAL_4 *);
51 iexport_proto(random_r4);
52
53 extern void random_r8 (GFC_REAL_8 *);
54 iexport_proto(random_r8);
55
56 extern void arandom_r4 (gfc_array_r4 *);
57 export_proto(arandom_r4);
58
59 extern void arandom_r8 (gfc_array_r8 *);
60 export_proto(arandom_r8);
61
62 #ifdef HAVE_GFC_REAL_10
63
64 extern void random_r10 (GFC_REAL_10 *);
65 iexport_proto(random_r10);
66
67 extern void arandom_r10 (gfc_array_r10 *);
68 export_proto(arandom_r10);
69
70 #endif
71
72 #ifdef HAVE_GFC_REAL_16
73
74 extern void random_r16 (GFC_REAL_16 *);
75 iexport_proto(random_r16);
76
77 extern void arandom_r16 (gfc_array_r16 *);
78 export_proto(arandom_r16);
79
80 #endif
81
82 #ifdef HAVE_GFC_REAL_17
83
84 extern void random_r17 (GFC_REAL_17 *);
85 iexport_proto(random_r17);
86
87 extern void arandom_r17 (gfc_array_r17 *);
88 export_proto(arandom_r17);
89
90 #endif
91
92 extern void random_m1 (GFC_UINTEGER_1 *);
93 export_proto (random_m1);
94
95 extern void random_m2 (GFC_UINTEGER_2 *);
96 export_proto (random_m2);
97
98 extern void random_m4 (GFC_UINTEGER_4 *);
99 export_proto (random_m4);
100
101 extern void random_m8 (GFC_UINTEGER_8 *);
102 export_proto (random_m8);
103
104 #ifdef HAVE_GFC_UINTEGER_16
105 extern void random_m16 (GFC_UINTEGER_16 *);
106 export_proto (random_m16);
107
108 #endif
109
110 extern void arandom_m1 (gfc_array_m1 *);
111 export_proto (arandom_m1);
112
113 extern void arandom_m2 (gfc_array_m2 *);
114 export_proto (arandom_m2);
115
116 extern void arandom_m4 (gfc_array_m4 *);
117 export_proto (arandom_m4);
118
119 extern void arandom_m8 (gfc_array_m8 *);
120 export_proto (arandom_m8);
121
122 #ifdef HAVE_GFC_UINTEGER_16
123
124 extern void arandom_m16 (gfc_array_m16 *);
125 export_proto (arandom_m16);
126
127 #endif
128
129 #ifdef __GTHREAD_MUTEX_INIT
130 static __gthread_mutex_t random_lock = __GTHREAD_MUTEX_INIT;
131 #else
132 static __gthread_mutex_t random_lock;
133 #endif
134
135 /* Helper routines to map a GFC_UINTEGER_* to the corresponding
136 GFC_REAL_* types in the range of [0,1). If GFC_REAL_*_RADIX are 2
137 or 16, respectively, we mask off the bits that don't fit into the
138 correct GFC_REAL_*, convert to the real type, then multiply by the
139 correct offset. */
140
141
142 static void
143 rnumber_4 (GFC_REAL_4 *f, GFC_UINTEGER_4 v)
144 {
145 GFC_UINTEGER_4 mask;
146 #if GFC_REAL_4_RADIX == 2
147 mask = ~ (GFC_UINTEGER_4) 0u << (32 - GFC_REAL_4_DIGITS);
148 #elif GFC_REAL_4_RADIX == 16
149 mask = ~ (GFC_UINTEGER_4) 0u << ((8 - GFC_REAL_4_DIGITS) * 4);
150 #else
151 #error "GFC_REAL_4_RADIX has unknown value"
152 #endif
153 v = v & mask;
154 *f = (GFC_REAL_4) v * GFC_REAL_4_LITERAL(0x1.p-32);
155 }
156
157 static void
158 rnumber_8 (GFC_REAL_8 *f, GFC_UINTEGER_8 v)
159 {
160 GFC_UINTEGER_8 mask;
161 #if GFC_REAL_8_RADIX == 2
162 mask = ~ (GFC_UINTEGER_8) 0u << (64 - GFC_REAL_8_DIGITS);
163 #elif GFC_REAL_8_RADIX == 16
164 mask = ~ (GFC_UINTEGER_8) 0u << (16 - GFC_REAL_8_DIGITS) * 4);
165 #else
166 #error "GFC_REAL_8_RADIX has unknown value"
167 #endif
168 v = v & mask;
169 *f = (GFC_REAL_8) v * GFC_REAL_8_LITERAL(0x1.p-64);
170 }
171
172 #ifdef HAVE_GFC_REAL_10
173
174 static void
175 rnumber_10 (GFC_REAL_10 *f, GFC_UINTEGER_8 v)
176 {
177 GFC_UINTEGER_8 mask;
178 #if GFC_REAL_10_RADIX == 2
179 mask = ~ (GFC_UINTEGER_8) 0u << (64 - GFC_REAL_10_DIGITS);
180 #elif GFC_REAL_10_RADIX == 16
181 mask = ~ (GFC_UINTEGER_10) 0u << ((16 - GFC_REAL_10_DIGITS) * 4);
182 #else
183 #error "GFC_REAL_10_RADIX has unknown value"
184 #endif
185 v = v & mask;
186 *f = (GFC_REAL_10) v * GFC_REAL_10_LITERAL(0x1.p-64);
187 }
188 #endif
189
190 #ifdef HAVE_GFC_REAL_16
191
192 /* For REAL(KIND=16), we only need to mask off the lower bits. */
193
194 static void
195 rnumber_16 (GFC_REAL_16 *f, GFC_UINTEGER_8 v1, GFC_UINTEGER_8 v2)
196 {
197 GFC_UINTEGER_8 mask;
198 #if GFC_REAL_16_RADIX == 2
199 mask = ~ (GFC_UINTEGER_8) 0u << (128 - GFC_REAL_16_DIGITS);
200 #elif GFC_REAL_16_RADIX == 16
201 mask = ~ (GFC_UINTEGER_8) 0u << ((32 - GFC_REAL_16_DIGITS) * 4);
202 #else
203 #error "GFC_REAL_16_RADIX has unknown value"
204 #endif
205 v2 = v2 & mask;
206 *f = (GFC_REAL_16) v1 * GFC_REAL_16_LITERAL(0x1.p-64)
207 + (GFC_REAL_16) v2 * GFC_REAL_16_LITERAL(0x1.p-128);
208 }
209 #endif
210
211 #ifdef HAVE_GFC_REAL_17
212
213 /* For REAL(KIND=16), we only need to mask off the lower bits. */
214
215 static void
216 rnumber_17 (GFC_REAL_17 *f, GFC_UINTEGER_8 v1, GFC_UINTEGER_8 v2)
217 {
218 GFC_UINTEGER_8 mask;
219 #if GFC_REAL_17_RADIX == 2
220 mask = ~ (GFC_UINTEGER_8) 0u << (128 - GFC_REAL_17_DIGITS);
221 #elif GFC_REAL_17_RADIX == 16
222 mask = ~ (GFC_UINTEGER_8) 0u << ((32 - GFC_REAL_17_DIGITS) * 4);
223 #else
224 #error "GFC_REAL_17_RADIX has unknown value"
225 #endif
226 v2 = v2 & mask;
227 *f = (GFC_REAL_17) v1 * GFC_REAL_17_LITERAL(0x1.p-64)
228 + (GFC_REAL_17) v2 * GFC_REAL_17_LITERAL(0x1.p-128);
229 }
230 #endif
231
232
233 /*
234
235 We use the xoshiro256** generator, a fast high-quality generator
236 that:
237
238 - passes TestU1 without any failures
239
240 - provides a "jump" function making it easy to provide many
241 independent parallel streams.
242
243 - Long period of 2**256 - 1
244
245 A description can be found at
246
247 http://prng.di.unimi.it/
248
249 or
250
251 https://arxiv.org/abs/1805.01407
252
253 The paper includes public domain source code which is the basis for
254 the implementation below.
255
256 */
257 typedef struct
258 {
259 bool init;
260 uint64_t s[4];
261 }
262 prng_state;
263
264
265 /* master_state is the only variable protected by random_lock. */
266 static prng_state master_state = { .init = false, .s = {
267 0xad63fa1ed3b55f36ULL, 0xd94473e78978b497ULL, 0xbc60592a98172477ULL,
268 0xa3de7c6e81265301ULL }
269 };
270
271
272 static __gthread_key_t rand_state_key;
273
274 static prng_state*
275 get_rand_state (void)
276 {
277 /* For single threaded apps. */
278 static prng_state rand_state;
279
280 if (__gthread_active_p ())
281 {
282 void* p = __gthread_getspecific (rand_state_key);
283 if (!p)
284 {
285 p = xcalloc (1, sizeof (prng_state));
286 __gthread_setspecific (rand_state_key, p);
287 }
288 return p;
289 }
290 else
291 return &rand_state;
292 }
293
294 static inline uint64_t
295 rotl (const uint64_t x, int k)
296 {
297 return (x << k) | (x >> (64 - k));
298 }
299
300
301 static uint64_t
302 prng_next (prng_state* rs)
303 {
304 const uint64_t result = rotl(rs->s[1] * 5, 7) * 9;
305
306 const uint64_t t = rs->s[1] << 17;
307
308 rs->s[2] ^= rs->s[0];
309 rs->s[3] ^= rs->s[1];
310 rs->s[1] ^= rs->s[2];
311 rs->s[0] ^= rs->s[3];
312
313 rs->s[2] ^= t;
314
315 rs->s[3] = rotl(rs->s[3], 45);
316
317 return result;
318 }
319
320
321 /* This is the jump function for the generator. It is equivalent to
322 2^128 calls to prng_next(); it can be used to generate 2^128
323 non-overlapping subsequences for parallel computations. */
324
325 static void
326 jump (prng_state* rs)
327 {
328 static const uint64_t JUMP[] = { 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c };
329
330 uint64_t s0 = 0;
331 uint64_t s1 = 0;
332 uint64_t s2 = 0;
333 uint64_t s3 = 0;
334 for(size_t i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
335 for(int b = 0; b < 64; b++) {
336 if (JUMP[i] & UINT64_C(1) << b) {
337 s0 ^= rs->s[0];
338 s1 ^= rs->s[1];
339 s2 ^= rs->s[2];
340 s3 ^= rs->s[3];
341 }
342 prng_next (rs);
343 }
344
345 rs->s[0] = s0;
346 rs->s[1] = s1;
347 rs->s[2] = s2;
348 rs->s[3] = s3;
349 }
350
351
352 /* Splitmix64 recommended by xoshiro author for initializing. After
353 getting one uint64_t value from the OS, this is used to fill in the
354 rest of the xoshiro state. */
355
356 static uint64_t
357 splitmix64 (uint64_t x)
358 {
359 uint64_t z = (x += 0x9e3779b97f4a7c15);
360 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
361 z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
362 return z ^ (z >> 31);
363 }
364
365
366 /* Get some bytes from the operating system in order to seed
367 the PRNG. */
368
369 static int
370 getosrandom (void *buf, size_t buflen)
371 {
372 /* rand_s is available in MinGW-w64 but not plain MinGW. */
373 #if defined(__MINGW64_VERSION_MAJOR)
374 unsigned int* b = buf;
375 for (size_t i = 0; i < buflen / sizeof (unsigned int); i++)
376 rand_s (&b[i]);
377 return buflen;
378 #else
379 #ifdef HAVE_GETENTROPY
380 if (getentropy (buf, buflen) == 0)
381 return buflen;
382 #endif
383 int flags = O_RDONLY;
384 #ifdef O_CLOEXEC
385 flags |= O_CLOEXEC;
386 #endif
387 int fd = open("/dev/urandom", flags);
388 if (fd != -1)
389 {
390 int res = read(fd, buf, buflen);
391 close (fd);
392 return res;
393 }
394 uint64_t seed = 0x047f7684e9fc949dULL;
395 time_t secs;
396 long usecs;
397 if (gf_gettime (&secs, &usecs) == 0)
398 {
399 seed ^= secs;
400 seed ^= usecs;
401 }
402 #ifdef HAVE_GETPID
403 pid_t pid = getpid();
404 seed ^= pid;
405 #endif
406 size_t size = buflen < sizeof (uint64_t) ? buflen : sizeof (uint64_t);
407 memcpy (buf, &seed, size);
408 return size;
409 #endif /* __MINGW64_VERSION_MAJOR */
410 }
411
412
413 /* Initialize the random number generator for the current thread,
414 using the master state and the number of times we must jump. */
415
416 static void
417 init_rand_state (prng_state* rs, const bool locked)
418 {
419 if (!locked)
420 __gthread_mutex_lock (&random_lock);
421 if (!master_state.init)
422 {
423 uint64_t os_seed;
424 getosrandom (&os_seed, sizeof (os_seed));
425 for (uint64_t i = 0; i < sizeof (master_state.s) / sizeof (uint64_t); i++)
426 {
427 os_seed = splitmix64 (os_seed);
428 master_state.s[i] = os_seed;
429 }
430 master_state.init = true;
431 }
432 memcpy (&rs->s, master_state.s, sizeof (master_state.s));
433 jump (&master_state);
434 if (!locked)
435 __gthread_mutex_unlock (&random_lock);
436 rs->init = true;
437 }
438
439
440 /* This function produces a REAL(4) value from the uniform distribution
441 with range [0,1). */
442
443 void
444 random_r4 (GFC_REAL_4 *x)
445 {
446 prng_state* rs = get_rand_state();
447
448 if (unlikely (!rs->init))
449 init_rand_state (rs, false);
450 uint64_t r = prng_next (rs);
451 /* Take the higher bits, ensuring that a stream of real(4), real(8),
452 and real(10) will be identical (except for precision). */
453 uint32_t high = (uint32_t) (r >> 32);
454 rnumber_4 (x, high);
455 }
456 iexport(random_r4);
457
458 /* This function produces a REAL(8) value from the uniform distribution
459 with range [0,1). */
460
461 void
462 random_r8 (GFC_REAL_8 *x)
463 {
464 GFC_UINTEGER_8 r;
465 prng_state* rs = get_rand_state();
466
467 if (unlikely (!rs->init))
468 init_rand_state (rs, false);
469 r = prng_next (rs);
470 rnumber_8 (x, r);
471 }
472 iexport(random_r8);
473
474 #ifdef HAVE_GFC_REAL_10
475
476 /* This function produces a REAL(10) value from the uniform distribution
477 with range [0,1). */
478
479 void
480 random_r10 (GFC_REAL_10 *x)
481 {
482 GFC_UINTEGER_8 r;
483 prng_state* rs = get_rand_state();
484
485 if (unlikely (!rs->init))
486 init_rand_state (rs, false);
487 r = prng_next (rs);
488 rnumber_10 (x, r);
489 }
490 iexport(random_r10);
491
492 #endif
493
494 /* This function produces a REAL(16) value from the uniform distribution
495 with range [0,1). */
496
497 #ifdef HAVE_GFC_REAL_16
498
499 void
500 random_r16 (GFC_REAL_16 *x)
501 {
502 GFC_UINTEGER_8 r1, r2;
503 prng_state* rs = get_rand_state();
504
505 if (unlikely (!rs->init))
506 init_rand_state (rs, false);
507 r1 = prng_next (rs);
508 r2 = prng_next (rs);
509 rnumber_16 (x, r1, r2);
510 }
511 iexport(random_r16);
512
513
514 #endif
515
516 /* This function produces a REAL(16) value from the uniform distribution
517 with range [0,1). */
518
519 #ifdef HAVE_GFC_REAL_17
520
521 void
522 random_r17 (GFC_REAL_17 *x)
523 {
524 GFC_UINTEGER_8 r1, r2;
525 prng_state* rs = get_rand_state();
526
527 if (unlikely (!rs->init))
528 init_rand_state (rs, false);
529 r1 = prng_next (rs);
530 r2 = prng_next (rs);
531 rnumber_17 (x, r1, r2);
532 }
533 iexport(random_r17);
534
535
536 #endif
537
538 /* Versions for unsigned numbers. */
539
540 /* Returns a random byte. */
541
542 void
543 random_m1 (GFC_UINTEGER_1 *x)
544 {
545 prng_state* rs = get_rand_state();
546
547 if (unlikely (!rs->init))
548 init_rand_state (rs, false);
549 GFC_UINTEGER_8 r = prng_next (rs);
550
551 *x = r >> 56;
552 }
553
554 /* A random 16-bit number. */
555
556 void
557 random_m2 (GFC_UINTEGER_2 *x)
558 {
559 prng_state* rs = get_rand_state();
560
561 if (unlikely (!rs->init))
562 init_rand_state (rs, false);
563 GFC_UINTEGER_8 r = prng_next (rs);
564
565 *x = r >> 48;
566 }
567
568 /* A random 32-bit number. */
569
570 void
571 random_m4 (GFC_UINTEGER_4 *x)
572 {
573 prng_state* rs = get_rand_state();
574
575 if (unlikely (!rs->init))
576 init_rand_state (rs, false);
577 GFC_UINTEGER_8 r = prng_next (rs);
578
579 *x = r >> 32;
580 }
581
582 /* A random 64-bit number. */
583
584 void
585 random_m8 (GFC_UINTEGER_8 *x)
586 {
587 prng_state* rs = get_rand_state();
588
589 if (unlikely (!rs->init))
590 init_rand_state (rs, false);
591 GFC_UINTEGER_8 r = prng_next (rs);
592
593 *x = r;
594 }
595
596 /* ... and a random 128-bit number, if we have the type. */
597
598 #ifdef HAVE_GFC_UINTEGER_16
599 void
600 random_m16 (GFC_UINTEGER_16 *x)
601 {
602 prng_state* rs = get_rand_state();
603
604 if (unlikely (!rs->init))
605 init_rand_state (rs, false);
606 GFC_UINTEGER_8 r1 = prng_next (rs);
607 GFC_UINTEGER_8 r2 = prng_next (rs);
608
609 *x = (((GFC_UINTEGER_16) r1) << 64) | (GFC_UINTEGER_16) r2;
610 }
611 #endif
612
613 /* This function fills a REAL(4) array with values from the uniform
614 distribution with range [0,1). */
615
616 void
617 arandom_r4 (gfc_array_r4 *x)
618 {
619 index_type count[GFC_MAX_DIMENSIONS];
620 index_type extent[GFC_MAX_DIMENSIONS];
621 index_type stride[GFC_MAX_DIMENSIONS];
622 index_type stride0;
623 index_type dim;
624 GFC_REAL_4 *dest;
625 prng_state* rs = get_rand_state();
626
627 dest = x->base_addr;
628
629 dim = GFC_DESCRIPTOR_RANK (x);
630
631 for (index_type n = 0; n < dim; n++)
632 {
633 count[n] = 0;
634 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
635 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
636 if (extent[n] <= 0)
637 return;
638 }
639
640 stride0 = stride[0];
641
642 if (unlikely (!rs->init))
643 init_rand_state (rs, false);
644
645 while (dest)
646 {
647 /* random_r4 (dest); */
648 uint64_t r = prng_next (rs);
649 uint32_t high = (uint32_t) (r >> 32);
650 rnumber_4 (dest, high);
651
652 /* Advance to the next element. */
653 dest += stride0;
654 count[0]++;
655 /* Advance to the next source element. */
656 index_type n = 0;
657 while (count[n] == extent[n])
658 {
659 /* When we get to the end of a dimension, reset it and increment
660 the next dimension. */
661 count[n] = 0;
662 /* We could precalculate these products, but this is a less
663 frequently used path so probably not worth it. */
664 dest -= stride[n] * extent[n];
665 n++;
666 if (n == dim)
667 {
668 dest = NULL;
669 break;
670 }
671 else
672 {
673 count[n]++;
674 dest += stride[n];
675 }
676 }
677 }
678 }
679
680 /* This function fills a REAL(8) array with values from the uniform
681 distribution with range [0,1). */
682
683 void
684 arandom_r8 (gfc_array_r8 *x)
685 {
686 index_type count[GFC_MAX_DIMENSIONS];
687 index_type extent[GFC_MAX_DIMENSIONS];
688 index_type stride[GFC_MAX_DIMENSIONS];
689 index_type stride0;
690 index_type dim;
691 GFC_REAL_8 *dest;
692 prng_state* rs = get_rand_state();
693
694 dest = x->base_addr;
695
696 dim = GFC_DESCRIPTOR_RANK (x);
697
698 for (index_type n = 0; n < dim; n++)
699 {
700 count[n] = 0;
701 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
702 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
703 if (extent[n] <= 0)
704 return;
705 }
706
707 stride0 = stride[0];
708
709 if (unlikely (!rs->init))
710 init_rand_state (rs, false);
711
712 while (dest)
713 {
714 /* random_r8 (dest); */
715 uint64_t r = prng_next (rs);
716 rnumber_8 (dest, r);
717
718 /* Advance to the next element. */
719 dest += stride0;
720 count[0]++;
721 /* Advance to the next source element. */
722 index_type n = 0;
723 while (count[n] == extent[n])
724 {
725 /* When we get to the end of a dimension, reset it and increment
726 the next dimension. */
727 count[n] = 0;
728 /* We could precalculate these products, but this is a less
729 frequently used path so probably not worth it. */
730 dest -= stride[n] * extent[n];
731 n++;
732 if (n == dim)
733 {
734 dest = NULL;
735 break;
736 }
737 else
738 {
739 count[n]++;
740 dest += stride[n];
741 }
742 }
743 }
744 }
745
746 #ifdef HAVE_GFC_REAL_10
747
748 /* This function fills a REAL(10) array with values from the uniform
749 distribution with range [0,1). */
750
751 void
752 arandom_r10 (gfc_array_r10 *x)
753 {
754 index_type count[GFC_MAX_DIMENSIONS];
755 index_type extent[GFC_MAX_DIMENSIONS];
756 index_type stride[GFC_MAX_DIMENSIONS];
757 index_type stride0;
758 index_type dim;
759 GFC_REAL_10 *dest;
760 prng_state* rs = get_rand_state();
761
762 dest = x->base_addr;
763
764 dim = GFC_DESCRIPTOR_RANK (x);
765
766 for (index_type n = 0; n < dim; n++)
767 {
768 count[n] = 0;
769 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
770 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
771 if (extent[n] <= 0)
772 return;
773 }
774
775 stride0 = stride[0];
776
777 if (unlikely (!rs->init))
778 init_rand_state (rs, false);
779
780 while (dest)
781 {
782 /* random_r10 (dest); */
783 uint64_t r = prng_next (rs);
784 rnumber_10 (dest, r);
785
786 /* Advance to the next element. */
787 dest += stride0;
788 count[0]++;
789 /* Advance to the next source element. */
790 index_type n = 0;
791 while (count[n] == extent[n])
792 {
793 /* When we get to the end of a dimension, reset it and increment
794 the next dimension. */
795 count[n] = 0;
796 /* We could precalculate these products, but this is a less
797 frequently used path so probably not worth it. */
798 dest -= stride[n] * extent[n];
799 n++;
800 if (n == dim)
801 {
802 dest = NULL;
803 break;
804 }
805 else
806 {
807 count[n]++;
808 dest += stride[n];
809 }
810 }
811 }
812 }
813
814 #endif
815
816 #ifdef HAVE_GFC_REAL_16
817
818 /* This function fills a REAL(16) array with values from the uniform
819 distribution with range [0,1). */
820
821 void
822 arandom_r16 (gfc_array_r16 *x)
823 {
824 index_type count[GFC_MAX_DIMENSIONS];
825 index_type extent[GFC_MAX_DIMENSIONS];
826 index_type stride[GFC_MAX_DIMENSIONS];
827 index_type stride0;
828 index_type dim;
829 GFC_REAL_16 *dest;
830 prng_state* rs = get_rand_state();
831
832 dest = x->base_addr;
833
834 dim = GFC_DESCRIPTOR_RANK (x);
835
836 for (index_type n = 0; n < dim; n++)
837 {
838 count[n] = 0;
839 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
840 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
841 if (extent[n] <= 0)
842 return;
843 }
844
845 stride0 = stride[0];
846
847 if (unlikely (!rs->init))
848 init_rand_state (rs, false);
849
850 while (dest)
851 {
852 /* random_r16 (dest); */
853 uint64_t r1 = prng_next (rs);
854 uint64_t r2 = prng_next (rs);
855 rnumber_16 (dest, r1, r2);
856
857 /* Advance to the next element. */
858 dest += stride0;
859 count[0]++;
860 /* Advance to the next source element. */
861 index_type n = 0;
862 while (count[n] == extent[n])
863 {
864 /* When we get to the end of a dimension, reset it and increment
865 the next dimension. */
866 count[n] = 0;
867 /* We could precalculate these products, but this is a less
868 frequently used path so probably not worth it. */
869 dest -= stride[n] * extent[n];
870 n++;
871 if (n == dim)
872 {
873 dest = NULL;
874 break;
875 }
876 else
877 {
878 count[n]++;
879 dest += stride[n];
880 }
881 }
882 }
883 }
884
885 #endif
886
887 #ifdef HAVE_GFC_REAL_17
888
889 /* This function fills a REAL(16) array with values from the uniform
890 distribution with range [0,1). */
891
892 void
893 arandom_r17 (gfc_array_r17 *x)
894 {
895 index_type count[GFC_MAX_DIMENSIONS];
896 index_type extent[GFC_MAX_DIMENSIONS];
897 index_type stride[GFC_MAX_DIMENSIONS];
898 index_type stride0;
899 index_type dim;
900 GFC_REAL_17 *dest;
901 prng_state* rs = get_rand_state();
902
903 dest = x->base_addr;
904
905 dim = GFC_DESCRIPTOR_RANK (x);
906
907 for (index_type n = 0; n < dim; n++)
908 {
909 count[n] = 0;
910 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
911 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
912 if (extent[n] <= 0)
913 return;
914 }
915
916 stride0 = stride[0];
917
918 if (unlikely (!rs->init))
919 init_rand_state (rs, false);
920
921 while (dest)
922 {
923 /* random_r17 (dest); */
924 uint64_t r1 = prng_next (rs);
925 uint64_t r2 = prng_next (rs);
926 rnumber_17 (dest, r1, r2);
927
928 /* Advance to the next element. */
929 dest += stride0;
930 count[0]++;
931 /* Advance to the next source element. */
932 index_type n = 0;
933 while (count[n] == extent[n])
934 {
935 /* When we get to the end of a dimension, reset it and increment
936 the next dimension. */
937 count[n] = 0;
938 /* We could precalculate these products, but this is a less
939 frequently used path so probably not worth it. */
940 dest -= stride[n] * extent[n];
941 n++;
942 if (n == dim)
943 {
944 dest = NULL;
945 break;
946 }
947 else
948 {
949 count[n]++;
950 dest += stride[n];
951 }
952 }
953 }
954 }
955
956 #endif
957
958 /* Fill an unsigned array with random bytes. */
959
960 void
961 arandom_m1 (gfc_array_m1 *x)
962 {
963 index_type count[GFC_MAX_DIMENSIONS];
964 index_type extent[GFC_MAX_DIMENSIONS];
965 index_type stride[GFC_MAX_DIMENSIONS];
966 index_type stride0;
967 index_type dim;
968 GFC_UINTEGER_1 *dest;
969 prng_state* rs = get_rand_state();
970
971 dest = x->base_addr;
972
973 dim = GFC_DESCRIPTOR_RANK (x);
974
975 for (index_type n = 0; n < dim; n++)
976 {
977 count[n] = 0;
978 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
979 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
980 if (extent[n] <= 0)
981 return;
982 }
983
984 stride0 = stride[0];
985
986 if (unlikely (!rs->init))
987 init_rand_state (rs, false);
988
989 while (dest)
990 {
991 /* random_m1 (dest); */
992 uint64_t r = prng_next (rs);
993 *dest = r >> 56;
994
995 /* Advance to the next element. */
996 dest += stride0;
997 count[0]++;
998 /* Advance to the next source element. */
999 index_type n = 0;
1000 while (count[n] == extent[n])
1001 {
1002 /* When we get to the end of a dimension, reset it and increment
1003 the next dimension. */
1004 count[n] = 0;
1005 /* We could precalculate these products, but this is a less
1006 frequently used path so probably not worth it. */
1007 dest -= stride[n] * extent[n];
1008 n++;
1009 if (n == dim)
1010 {
1011 dest = NULL;
1012 break;
1013 }
1014 else
1015 {
1016 count[n]++;
1017 dest += stride[n];
1018 }
1019 }
1020 }
1021 }
1022
1023 /* Fill an unsigned array with random 16-bit unsigneds. */
1024
1025 void
1026 arandom_m2 (gfc_array_m2 *x)
1027 {
1028 index_type count[GFC_MAX_DIMENSIONS];
1029 index_type extent[GFC_MAX_DIMENSIONS];
1030 index_type stride[GFC_MAX_DIMENSIONS];
1031 index_type stride0;
1032 index_type dim;
1033 GFC_UINTEGER_2 *dest;
1034 prng_state* rs = get_rand_state();
1035
1036 dest = x->base_addr;
1037
1038 dim = GFC_DESCRIPTOR_RANK (x);
1039
1040 for (index_type n = 0; n < dim; n++)
1041 {
1042 count[n] = 0;
1043 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
1044 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
1045 if (extent[n] <= 0)
1046 return;
1047 }
1048
1049 stride0 = stride[0];
1050
1051 if (unlikely (!rs->init))
1052 init_rand_state (rs, false);
1053
1054 while (dest)
1055 {
1056 /* random_m1 (dest); */
1057 uint64_t r = prng_next (rs);
1058 *dest = r >> 48;
1059
1060 /* Advance to the next element. */
1061 dest += stride0;
1062 count[0]++;
1063 /* Advance to the next source element. */
1064 index_type n = 0;
1065 while (count[n] == extent[n])
1066 {
1067 /* When we get to the end of a dimension, reset it and increment
1068 the next dimension. */
1069 count[n] = 0;
1070 /* We could precalculate these products, but this is a less
1071 frequently used path so probably not worth it. */
1072 dest -= stride[n] * extent[n];
1073 n++;
1074 if (n == dim)
1075 {
1076 dest = NULL;
1077 break;
1078 }
1079 else
1080 {
1081 count[n]++;
1082 dest += stride[n];
1083 }
1084 }
1085 }
1086 }
1087
1088 /* Fill an array with random 32-bit unsigneds. */
1089
1090 void
1091 arandom_m4 (gfc_array_m4 *x)
1092 {
1093 index_type count[GFC_MAX_DIMENSIONS];
1094 index_type extent[GFC_MAX_DIMENSIONS];
1095 index_type stride[GFC_MAX_DIMENSIONS];
1096 index_type stride0;
1097 index_type dim;
1098 GFC_UINTEGER_4 *dest;
1099 prng_state* rs = get_rand_state();
1100
1101 dest = x->base_addr;
1102
1103 dim = GFC_DESCRIPTOR_RANK (x);
1104
1105 for (index_type n = 0; n < dim; n++)
1106 {
1107 count[n] = 0;
1108 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
1109 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
1110 if (extent[n] <= 0)
1111 return;
1112 }
1113
1114 stride0 = stride[0];
1115
1116 if (unlikely (!rs->init))
1117 init_rand_state (rs, false);
1118
1119 while (dest)
1120 {
1121 /* random_m4 (dest); */
1122 uint64_t r = prng_next (rs);
1123 *dest = r >> 32;
1124
1125 /* Advance to the next element. */
1126 dest += stride0;
1127 count[0]++;
1128 /* Advance to the next source element. */
1129 index_type n = 0;
1130 while (count[n] == extent[n])
1131 {
1132 /* When we get to the end of a dimension, reset it and increment
1133 the next dimension. */
1134 count[n] = 0;
1135 /* We could precalculate these products, but this is a less
1136 frequently used path so probably not worth it. */
1137 dest -= stride[n] * extent[n];
1138 n++;
1139 if (n == dim)
1140 {
1141 dest = NULL;
1142 break;
1143 }
1144 else
1145 {
1146 count[n]++;
1147 dest += stride[n];
1148 }
1149 }
1150 }
1151 }
1152
1153 /* Fill an array with random 64-bit unsigneds. */
1154
1155 void
1156 arandom_m8 (gfc_array_m8 *x)
1157 {
1158 index_type count[GFC_MAX_DIMENSIONS];
1159 index_type extent[GFC_MAX_DIMENSIONS];
1160 index_type stride[GFC_MAX_DIMENSIONS];
1161 index_type stride0;
1162 index_type dim;
1163 GFC_UINTEGER_8 *dest;
1164 prng_state* rs = get_rand_state();
1165
1166 dest = x->base_addr;
1167
1168 dim = GFC_DESCRIPTOR_RANK (x);
1169
1170 for (index_type n = 0; n < dim; n++)
1171 {
1172 count[n] = 0;
1173 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
1174 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
1175 if (extent[n] <= 0)
1176 return;
1177 }
1178
1179 stride0 = stride[0];
1180
1181 if (unlikely (!rs->init))
1182 init_rand_state (rs, false);
1183
1184 while (dest)
1185 {
1186 /* random_m8 (dest); */
1187 uint64_t r = prng_next (rs);
1188 *dest = r;
1189
1190 /* Advance to the next element. */
1191 dest += stride0;
1192 count[0]++;
1193 /* Advance to the next source element. */
1194 index_type n = 0;
1195 while (count[n] == extent[n])
1196 {
1197 /* When we get to the end of a dimension, reset it and increment
1198 the next dimension. */
1199 count[n] = 0;
1200 /* We could precalculate these products, but this is a less
1201 frequently used path so probably not worth it. */
1202 dest -= stride[n] * extent[n];
1203 n++;
1204 if (n == dim)
1205 {
1206 dest = NULL;
1207 break;
1208 }
1209 else
1210 {
1211 count[n]++;
1212 dest += stride[n];
1213 }
1214 }
1215 }
1216 }
1217
1218 #ifdef HAVE_GFC_UINTEGER_16
1219
1220 /* Fill an unsigned array with random bytes. */
1221
1222 void
1223 arandom_m16 (gfc_array_m16 *x)
1224 {
1225 index_type count[GFC_MAX_DIMENSIONS];
1226 index_type extent[GFC_MAX_DIMENSIONS];
1227 index_type stride[GFC_MAX_DIMENSIONS];
1228 index_type stride0;
1229 index_type dim;
1230 GFC_UINTEGER_16 *dest;
1231 prng_state* rs = get_rand_state();
1232
1233 dest = x->base_addr;
1234
1235 dim = GFC_DESCRIPTOR_RANK (x);
1236
1237 for (index_type n = 0; n < dim; n++)
1238 {
1239 count[n] = 0;
1240 stride[n] = GFC_DESCRIPTOR_STRIDE(x,n);
1241 extent[n] = GFC_DESCRIPTOR_EXTENT(x,n);
1242 if (extent[n] <= 0)
1243 return;
1244 }
1245
1246 stride0 = stride[0];
1247
1248 if (unlikely (!rs->init))
1249 init_rand_state (rs, false);
1250
1251 while (dest)
1252 {
1253 /* random_m16 (dest); */
1254 uint64_t r1 = prng_next (rs), r2 = prng_next (rs);
1255 *dest = (((GFC_UINTEGER_16) r1) << 64) | (GFC_UINTEGER_16) r2;
1256
1257 /* Advance to the next element. */
1258 dest += stride0;
1259 count[0]++;
1260 /* Advance to the next source element. */
1261 index_type n = 0;
1262 while (count[n] == extent[n])
1263 {
1264 /* When we get to the end of a dimension, reset it and increment
1265 the next dimension. */
1266 count[n] = 0;
1267 /* We could precalculate these products, but this is a less
1268 frequently used path so probably not worth it. */
1269 dest -= stride[n] * extent[n];
1270 n++;
1271 if (n == dim)
1272 {
1273 dest = NULL;
1274 break;
1275 }
1276 else
1277 {
1278 count[n]++;
1279 dest += stride[n];
1280 }
1281 }
1282 }
1283 }
1284
1285 #endif
1286
1287 /* Number of elements in master_state array. */
1288 #define SZU64 (sizeof (master_state.s) / sizeof (uint64_t))
1289
1290 /* Equivalent number of elements in an array of GFC_INTEGER_{4,8}. */
1291 #define SZ_IN_INT_4 (SZU64 * (sizeof (uint64_t) / sizeof (GFC_INTEGER_4)))
1292 #define SZ_IN_INT_8 (SZU64 * (sizeof (uint64_t) / sizeof (GFC_INTEGER_8)))
1293
1294 /* Keys for scrambling the seed in order to avoid poor seeds. */
1295
1296 static const uint64_t xor_keys[] = {
1297 0xbd0c5b6e50c2df49ULL, 0xd46061cd46e1df38ULL, 0xbb4f4d4ed6103544ULL,
1298 0x114a583d0756ad39ULL
1299 };
1300
1301
1302 /* Since a XOR cipher is symmetric, we need only one routine, and we
1303 can use it both for encryption and decryption. */
1304
1305 static void
1306 scramble_seed (uint64_t *dest, const uint64_t *src)
1307 {
1308 for (size_t i = 0; i < SZU64; i++)
1309 dest[i] = src[i] ^ xor_keys[i];
1310 }
1311
1312
1313 /* random_seed is used to seed the PRNG with either a default
1314 set of seeds or user specified set of seeds. random_seed
1315 must be called with no argument or exactly one argument. */
1316
1317 void
1318 random_seed_i4 (GFC_INTEGER_4 *size, gfc_array_i4 *put, gfc_array_i4 *get)
1319 {
1320 uint64_t seed[SZU64];
1321
1322 /* Check that we only have one argument present. */
1323 if ((size ? 1 : 0) + (put ? 1 : 0) + (get ? 1 : 0) > 1)
1324 runtime_error ("RANDOM_SEED should have at most one argument present.");
1325
1326 if (size != NULL)
1327 *size = SZ_IN_INT_4;
1328
1329 prng_state* rs = get_rand_state();
1330
1331 /* Return the seed to GET data. */
1332 if (get != NULL)
1333 {
1334 /* If the rank of the array is not 1, abort. */
1335 if (GFC_DESCRIPTOR_RANK (get) != 1)
1336 runtime_error ("Array rank of GET is not 1.");
1337
1338 /* If the array is too small, abort. */
1339 if (GFC_DESCRIPTOR_EXTENT(get,0) < (index_type) SZ_IN_INT_4)
1340 runtime_error ("Array size of GET is too small.");
1341
1342 if (!rs->init)
1343 init_rand_state (rs, false);
1344
1345 /* Unscramble the seed. */
1346 scramble_seed (seed, rs->s);
1347
1348 /* Then copy it back to the user variable. */
1349 for (size_t i = 0; i < SZ_IN_INT_4 ; i++)
1350 memcpy (&(get->base_addr[(SZ_IN_INT_4 - 1 - i) *
1351 GFC_DESCRIPTOR_STRIDE(get,0)]),
1352 (unsigned char*) seed + i * sizeof(GFC_UINTEGER_4),
1353 sizeof(GFC_UINTEGER_4));
1354 }
1355
1356 else
1357 {
1358 __gthread_mutex_lock (&random_lock);
1359
1360 /* From the standard: "If no argument is present, the processor assigns
1361 a processor-dependent value to the seed." */
1362 if (size == NULL && put == NULL && get == NULL)
1363 {
1364 master_state.init = false;
1365 init_rand_state (rs, true);
1366 }
1367
1368 if (put != NULL)
1369 {
1370 /* If the rank of the array is not 1, abort. */
1371 if (GFC_DESCRIPTOR_RANK (put) != 1)
1372 runtime_error ("Array rank of PUT is not 1.");
1373
1374 /* If the array is too small, abort. */
1375 if (GFC_DESCRIPTOR_EXTENT(put,0) < (index_type) SZ_IN_INT_4)
1376 runtime_error ("Array size of PUT is too small.");
1377
1378 /* We copy the seed given by the user. */
1379 for (size_t i = 0; i < SZ_IN_INT_4; i++)
1380 memcpy ((unsigned char*) seed + i * sizeof(GFC_UINTEGER_4),
1381 &(put->base_addr[(SZ_IN_INT_4 - 1 - i) *
1382 GFC_DESCRIPTOR_STRIDE(put,0)]),
1383 sizeof(GFC_UINTEGER_4));
1384
1385 /* We put it after scrambling the bytes, to paper around users who
1386 provide seeds with quality only in the lower or upper part. */
1387 scramble_seed (master_state.s, seed);
1388 master_state.init = true;
1389 init_rand_state (rs, true);
1390 }
1391
1392 __gthread_mutex_unlock (&random_lock);
1393 }
1394 }
1395 iexport(random_seed_i4);
1396
1397
1398 void
1399 random_seed_i8 (GFC_INTEGER_8 *size, gfc_array_i8 *put, gfc_array_i8 *get)
1400 {
1401 uint64_t seed[SZU64];
1402
1403 /* Check that we only have one argument present. */
1404 if ((size ? 1 : 0) + (put ? 1 : 0) + (get ? 1 : 0) > 1)
1405 runtime_error ("RANDOM_SEED should have at most one argument present.");
1406
1407 if (size != NULL)
1408 *size = SZ_IN_INT_8;
1409
1410 prng_state* rs = get_rand_state();
1411
1412 /* Return the seed to GET data. */
1413 if (get != NULL)
1414 {
1415 /* If the rank of the array is not 1, abort. */
1416 if (GFC_DESCRIPTOR_RANK (get) != 1)
1417 runtime_error ("Array rank of GET is not 1.");
1418
1419 /* If the array is too small, abort. */
1420 if (GFC_DESCRIPTOR_EXTENT(get,0) < (index_type) SZ_IN_INT_8)
1421 runtime_error ("Array size of GET is too small.");
1422
1423 if (!rs->init)
1424 init_rand_state (rs, false);
1425
1426 /* Unscramble the seed. */
1427 scramble_seed (seed, rs->s);
1428
1429 /* This code now should do correct strides. */
1430 for (size_t i = 0; i < SZ_IN_INT_8; i++)
1431 memcpy (&(get->base_addr[i * GFC_DESCRIPTOR_STRIDE(get,0)]), &seed[i],
1432 sizeof (GFC_UINTEGER_8));
1433 }
1434
1435 else
1436 {
1437 __gthread_mutex_lock (&random_lock);
1438
1439 /* From the standard: "If no argument is present, the processor assigns
1440 a processor-dependent value to the seed." */
1441 if (size == NULL && put == NULL && get == NULL)
1442 {
1443 master_state.init = false;
1444 init_rand_state (rs, true);
1445 }
1446
1447 if (put != NULL)
1448 {
1449 /* If the rank of the array is not 1, abort. */
1450 if (GFC_DESCRIPTOR_RANK (put) != 1)
1451 runtime_error ("Array rank of PUT is not 1.");
1452
1453 /* If the array is too small, abort. */
1454 if (GFC_DESCRIPTOR_EXTENT(put,0) < (index_type) SZ_IN_INT_8)
1455 runtime_error ("Array size of PUT is too small.");
1456
1457 /* This code now should do correct strides. */
1458 for (size_t i = 0; i < SZ_IN_INT_8; i++)
1459 memcpy (&seed[i], &(put->base_addr[i * GFC_DESCRIPTOR_STRIDE(put,0)]),
1460 sizeof (GFC_UINTEGER_8));
1461
1462 scramble_seed (master_state.s, seed);
1463 master_state.init = true;
1464 init_rand_state (rs, true);
1465 }
1466
1467
1468 __gthread_mutex_unlock (&random_lock);
1469 }
1470 }
1471 iexport(random_seed_i8);
1472
1473
1474 #if !defined __GTHREAD_MUTEX_INIT || defined __GTHREADS
1475 static void __attribute__((constructor))
1476 constructor_random (void)
1477 {
1478 #ifndef __GTHREAD_MUTEX_INIT
1479 __GTHREAD_MUTEX_INIT_FUNCTION (&random_lock);
1480 #endif
1481 if (__gthread_active_p ())
1482 __gthread_key_create (&rand_state_key, &free);
1483 }
1484 #endif
1485
1486 #ifdef __GTHREADS
1487 static void __attribute__((destructor))
1488 destructor_random (void)
1489 {
1490 if (__gthread_active_p ())
1491 __gthread_key_delete (rand_state_key);
1492 }
1493 #endif