OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libgfortran/] [intrinsics/] [rand.c] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* Implementation of the IRAND, RAND, and SRAND intrinsics.
2
   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3
   Contributed by Steven G. Kargl <kargls@comcast.net>.
4
 
5
This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
 
7
Libgfortran is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public
9
License as published by the Free Software Foundation; either
10
version 2 of the License, or (at your option) any later version.
11
 
12
In addition to the permissions in the GNU General Public License, the
13
Free Software Foundation gives you unlimited permission to link the
14
compiled version of this file into combinations with other programs,
15
and to distribute those combinations without any restriction coming
16
from the use of this file.  (The General Public License restrictions
17
do apply in other respects; for example, they cover modification of
18
the file, and distribution when not linked into a combine
19
executable.)
20
 
21
Libgfortran is distributed in the hope that it will be useful,
22
but WITHOUT ANY WARRANTY; without even the implied warranty of
23
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
GNU General Public License for more details.
25
 
26
You should have received a copy of the GNU General Public
27
License along with libgfortran; see the file COPYING.  If not,
28
write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29
Boston, MA 02110-1301, USA.  */
30
 
31
/* Simple multiplicative congruent algorithm.
32
   The period of this generator is approximately 2^31-1, which means that
33
   it should not be used for anything serious.  The implementation here
34
   is based of an algorithm from  S.K. Park and K.W. Miller, Comm. ACM,
35
   31, 1192-1201 (1988).  It is also provided solely for compatibility
36
   with G77.  */
37
 
38
#include "config.h"
39
#include "libgfortran.h"
40
#include "../io/io.h"
41
 
42
#define GFC_RAND_A      16807
43
#define GFC_RAND_M      2147483647
44
#define GFC_RAND_M1     (GFC_RAND_M - 1)
45
 
46
static GFC_UINTEGER_8 rand_seed = 1;
47
#ifdef __GTHREAD_MUTEX_INIT
48
static __gthread_mutex_t rand_seed_lock = __GTHREAD_MUTEX_INIT;
49
#else
50
static __gthread_mutex_t rand_seed_lock;
51
#endif
52
 
53
 
54
/* Set the seed of the irand generator.  Note 0 is a bad seed.  */
55
 
56
static void
57
srand_internal (GFC_INTEGER_8 i)
58
{
59
  rand_seed = i ? i : 123459876;
60
}
61
 
62
extern void PREFIX(srand) (GFC_INTEGER_4 *i);
63
export_proto_np(PREFIX(srand));
64
 
65
void
66
PREFIX(srand) (GFC_INTEGER_4 *i)
67
{
68
  __gthread_mutex_lock (&rand_seed_lock);
69
  srand_internal (*i);
70
  __gthread_mutex_unlock (&rand_seed_lock);
71
}
72
 
73
/* Return an INTEGER in the range [1,GFC_RAND_M-1].  */
74
 
75
extern GFC_INTEGER_4 irand (GFC_INTEGER_4 *);
76
iexport_proto(irand);
77
 
78
GFC_INTEGER_4
79
irand (GFC_INTEGER_4 *i)
80
{
81
  GFC_INTEGER_4 j;
82
  if (i)
83
    j = *i;
84
  else
85
    j = 0;
86
 
87
  __gthread_mutex_lock (&rand_seed_lock);
88
 
89
  switch (j)
90
  {
91
    /* Return the next RN. */
92
    case 0:
93
      break;
94
 
95
    /* Reset the RN sequence to system-dependent sequence and return the
96
       first value.  */
97
    case 1:
98
      srand_internal (0);
99
      break;
100
 
101
    /* Seed the RN sequence with j and return the first value.  */
102
    default:
103
      srand_internal (j);
104
      break;
105
   }
106
 
107
   rand_seed = GFC_RAND_A * rand_seed % GFC_RAND_M;
108
   j = (GFC_INTEGER_4) rand_seed;
109
 
110
  __gthread_mutex_unlock (&rand_seed_lock);
111
 
112
   return j;
113
}
114
iexport(irand);
115
 
116
 
117
/*  Return a random REAL in the range [0,1).  */
118
 
119
extern GFC_REAL_4 PREFIX(rand) (GFC_INTEGER_4 *i);
120
export_proto_np(PREFIX(rand));
121
 
122
GFC_REAL_4
123
PREFIX(rand) (GFC_INTEGER_4 *i)
124
{
125
  return normalize_r4_i4 (irand (i) - 1, GFC_RAND_M1 - 1);
126
}
127
 
128
#ifndef __GTHREAD_MUTEX_INIT
129
static void __attribute__((constructor))
130
init (void)
131
{
132
  __GTHREAD_MUTEX_INIT_FUNCTION (&rand_seed_lock);
133
}
134
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.