xref: /netbsd-src/external/gpl3/gcc.old/dist/libgfortran/intrinsics/rand.c (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1627f7eb2Smrg /* Implementation of the IRAND, RAND, and SRAND intrinsics.
2*4c3eb207Smrg    Copyright (C) 2004-2020 Free Software Foundation, Inc.
3627f7eb2Smrg    Contributed by Steven G. Kargl <kargls@comcast.net>.
4627f7eb2Smrg 
5627f7eb2Smrg This file is part of the GNU Fortran 95 runtime library (libgfortran).
6627f7eb2Smrg 
7627f7eb2Smrg Libgfortran is free software; you can redistribute it and/or
8627f7eb2Smrg modify it under the terms of the GNU General Public
9627f7eb2Smrg License as published by the Free Software Foundation; either
10627f7eb2Smrg version 3 of the License, or (at your option) any later version.
11627f7eb2Smrg 
12627f7eb2Smrg Libgfortran is distributed in the hope that it will be useful,
13627f7eb2Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14627f7eb2Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15627f7eb2Smrg GNU General Public License for more details.
16627f7eb2Smrg 
17627f7eb2Smrg Under Section 7 of GPL version 3, you are granted additional
18627f7eb2Smrg permissions described in the GCC Runtime Library Exception, version
19627f7eb2Smrg 3.1, as published by the Free Software Foundation.
20627f7eb2Smrg 
21627f7eb2Smrg You should have received a copy of the GNU General Public License and
22627f7eb2Smrg a copy of the GCC Runtime Library Exception along with this program;
23627f7eb2Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24627f7eb2Smrg <http://www.gnu.org/licenses/>.  */
25627f7eb2Smrg 
26627f7eb2Smrg /* Simple multiplicative congruent algorithm.
27627f7eb2Smrg    The period of this generator is approximately 2^31-1, which means that
28627f7eb2Smrg    it should not be used for anything serious.  The implementation here
29627f7eb2Smrg    is based of an algorithm from  S.K. Park and K.W. Miller, Comm. ACM,
30627f7eb2Smrg    31, 1192-1201 (1988).  It is also provided solely for compatibility
31627f7eb2Smrg    with G77.  */
32627f7eb2Smrg 
33627f7eb2Smrg #include "libgfortran.h"
34627f7eb2Smrg #include <gthr.h>
35627f7eb2Smrg 
36627f7eb2Smrg #define GFC_RAND_A	16807
37627f7eb2Smrg #define GFC_RAND_M	2147483647
38627f7eb2Smrg #define GFC_RAND_M1	(GFC_RAND_M - 1)
39627f7eb2Smrg 
40627f7eb2Smrg static GFC_UINTEGER_8 rand_seed = 1;
41627f7eb2Smrg #ifdef __GTHREAD_MUTEX_INIT
42627f7eb2Smrg static __gthread_mutex_t rand_seed_lock = __GTHREAD_MUTEX_INIT;
43627f7eb2Smrg #else
44627f7eb2Smrg static __gthread_mutex_t rand_seed_lock;
45627f7eb2Smrg #endif
46627f7eb2Smrg 
47627f7eb2Smrg 
48627f7eb2Smrg /* Set the seed of the irand generator.  Note 0 is a bad seed.  */
49627f7eb2Smrg 
50627f7eb2Smrg static void
srand_internal(GFC_INTEGER_8 i)51627f7eb2Smrg srand_internal (GFC_INTEGER_8 i)
52627f7eb2Smrg {
53627f7eb2Smrg   rand_seed = i ? i : 123459876;
54627f7eb2Smrg }
55627f7eb2Smrg 
56627f7eb2Smrg extern void PREFIX(srand) (GFC_INTEGER_4 *i);
57627f7eb2Smrg export_proto_np(PREFIX(srand));
58627f7eb2Smrg 
59627f7eb2Smrg void
PREFIX(srand)60627f7eb2Smrg PREFIX(srand) (GFC_INTEGER_4 *i)
61627f7eb2Smrg {
62627f7eb2Smrg   __gthread_mutex_lock (&rand_seed_lock);
63627f7eb2Smrg   srand_internal (*i);
64627f7eb2Smrg   __gthread_mutex_unlock (&rand_seed_lock);
65627f7eb2Smrg }
66627f7eb2Smrg 
67627f7eb2Smrg /* Return an INTEGER in the range [1,GFC_RAND_M-1].  */
68627f7eb2Smrg 
69627f7eb2Smrg extern GFC_INTEGER_4 irand (GFC_INTEGER_4 *);
70627f7eb2Smrg iexport_proto(irand);
71627f7eb2Smrg 
72627f7eb2Smrg GFC_INTEGER_4
irand(GFC_INTEGER_4 * i)73627f7eb2Smrg irand (GFC_INTEGER_4 *i)
74627f7eb2Smrg {
75627f7eb2Smrg   GFC_INTEGER_4 j;
76627f7eb2Smrg   if (i)
77627f7eb2Smrg     j = *i;
78627f7eb2Smrg   else
79627f7eb2Smrg     j = 0;
80627f7eb2Smrg 
81627f7eb2Smrg   __gthread_mutex_lock (&rand_seed_lock);
82627f7eb2Smrg 
83627f7eb2Smrg   switch (j)
84627f7eb2Smrg   {
85627f7eb2Smrg     /* Return the next RN. */
86627f7eb2Smrg     case 0:
87627f7eb2Smrg       break;
88627f7eb2Smrg 
89627f7eb2Smrg     /* Reset the RN sequence to system-dependent sequence and return the
90627f7eb2Smrg        first value.  */
91627f7eb2Smrg     case 1:
92627f7eb2Smrg       srand_internal (0);
93627f7eb2Smrg       break;
94627f7eb2Smrg 
95627f7eb2Smrg     /* Seed the RN sequence with j and return the first value.  */
96627f7eb2Smrg     default:
97627f7eb2Smrg       srand_internal (j);
98627f7eb2Smrg       break;
99627f7eb2Smrg    }
100627f7eb2Smrg 
101627f7eb2Smrg    rand_seed = GFC_RAND_A * rand_seed % GFC_RAND_M;
102627f7eb2Smrg    j = (GFC_INTEGER_4) rand_seed;
103627f7eb2Smrg 
104627f7eb2Smrg   __gthread_mutex_unlock (&rand_seed_lock);
105627f7eb2Smrg 
106627f7eb2Smrg    return j;
107627f7eb2Smrg }
108627f7eb2Smrg iexport(irand);
109627f7eb2Smrg 
110627f7eb2Smrg 
111627f7eb2Smrg /*  Return a random REAL in the range [0,1).  */
112627f7eb2Smrg 
113627f7eb2Smrg extern GFC_REAL_4 PREFIX(rand) (GFC_INTEGER_4 *i);
114627f7eb2Smrg export_proto_np(PREFIX(rand));
115627f7eb2Smrg 
116627f7eb2Smrg GFC_REAL_4
PREFIX(rand)117627f7eb2Smrg PREFIX(rand) (GFC_INTEGER_4 *i)
118627f7eb2Smrg {
119627f7eb2Smrg   GFC_UINTEGER_4 mask;
120627f7eb2Smrg #if GFC_REAL_4_RADIX == 2
121627f7eb2Smrg   mask = ~ (GFC_UINTEGER_4) 0u << (32 - GFC_REAL_4_DIGITS + 1);
122627f7eb2Smrg #elif GFC_REAL_4_RADIX == 16
123627f7eb2Smrg   mask = ~ (GFC_UINTEGER_4) 0u << ((8 - GFC_REAL_4_DIGITS) * 4 + 1);
124627f7eb2Smrg #else
125627f7eb2Smrg #error "GFC_REAL_4_RADIX has unknown value"
126627f7eb2Smrg #endif
127627f7eb2Smrg   return ((GFC_UINTEGER_4) (irand(i) -1) & mask) * (GFC_REAL_4) 0x1.p-31f;
128627f7eb2Smrg }
129627f7eb2Smrg 
130627f7eb2Smrg #ifndef __GTHREAD_MUTEX_INIT
131627f7eb2Smrg static void __attribute__((constructor))
init(void)132627f7eb2Smrg init (void)
133627f7eb2Smrg {
134627f7eb2Smrg   __GTHREAD_MUTEX_INIT_FUNCTION (&rand_seed_lock);
135627f7eb2Smrg }
136627f7eb2Smrg #endif
137