OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [ada/] [a-nudira.adb] - Blame information for rev 300

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 281 jeremybenn
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT RUN-TIME COMPONENTS                         --
4
--                                                                          --
5
--         A D A . N U M E R I C S . D I S C R E T E _ R A N D O M          --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
10
--                                                                          --
11
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12
-- terms of the  GNU General Public License as published  by the Free Soft- --
13
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17
--                                                                          --
18
-- As a special exception under Section 7 of GPL version 3, you are granted --
19
-- additional permissions described in the GCC Runtime Library Exception,   --
20
-- version 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
-- GNAT was originally developed  by the GNAT team at  New York University. --
28
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
29
--                                                                          --
30
------------------------------------------------------------------------------
31
 
32
with Ada.Calendar;
33
 
34
with Interfaces; use Interfaces;
35
 
36
package body Ada.Numerics.Discrete_Random is
37
 
38
   -------------------------
39
   -- Implementation Note --
40
   -------------------------
41
 
42
   --  The design of this spec is very awkward, as a result of Ada 95 not
43
   --  permitting in-out parameters for function formals (most naturally
44
   --  Generator values would be passed this way). In pure Ada 95, the only
45
   --  solution is to use the heap and pointers, and, to avoid memory leaks,
46
   --  controlled types.
47
 
48
   --  This is awfully heavy, so what we do is to use Unrestricted_Access to
49
   --  get a pointer to the state in the passed Generator. This works because
50
   --  Generator is a limited type and will thus always be passed by reference.
51
 
52
   type Pointer is access all State;
53
 
54
   Fits_In_32_Bits : constant Boolean :=
55
                       Rst'Size < 31
56
                         or else (Rst'Size = 31
57
                                  and then Rst'Pos (Rst'First) < 0);
58
   --  This is set True if we do not need more than 32 bits in the result. If
59
   --  we need 64-bits, we will only use the meaningful 48 bits of any 64-bit
60
   --  number generated, since if more than 48 bits are required, we split the
61
   --  computation into two separate parts, since the algorithm does not behave
62
   --  above 48 bits.
63
 
64
   --  The way this expression works is that obviously if the size is 31 bits,
65
   --  it fits in 32 bits. In the 32-bit case, it fits in 32-bit signed if the
66
   --  range has negative values. It is too conservative in the case that the
67
   --  programmer has set a size greater than the default, e.g. a size of 33
68
   --  for an integer type with a range of 1..10, but an over-conservative
69
   --  result is OK. The important thing is that the value is only True if
70
   --  we know the result will fit in 32-bits signed. If the value is False
71
   --  when it could be True, the behavior will be correct, just a bit less
72
   --  efficient than it could have been in some unusual cases.
73
   --
74
   --  One might assume that we could get a more accurate result by testing
75
   --  the lower and upper bounds of the type Rst against the bounds of 32-bit
76
   --  Integer. However, there is no easy way to do that. Why? Because in the
77
   --  relatively rare case where this expresion has to be evaluated at run
78
   --  time rather than compile time (when the bounds are dynamic), we need a
79
   --  type to use for the computation. But the possible range of upper bound
80
   --  values for Rst (remembering the possibility of 64-bit modular types) is
81
   --  from -2**63 to 2**64-1, and no run-time type has a big enough range.
82
 
83
   -----------------------
84
   -- Local Subprograms --
85
   -----------------------
86
 
87
   function Square_Mod_N (X, N : Int) return Int;
88
   pragma Inline (Square_Mod_N);
89
   --  Computes X**2 mod N avoiding intermediate overflow
90
 
91
   -----------
92
   -- Image --
93
   -----------
94
 
95
   function Image (Of_State : State) return String is
96
   begin
97
      return Int'Image (Of_State.X1) &
98
             ','                     &
99
             Int'Image (Of_State.X2) &
100
             ','                     &
101
             Int'Image (Of_State.Q);
102
   end Image;
103
 
104
   ------------
105
   -- Random --
106
   ------------
107
 
108
   function Random (Gen : Generator) return Rst is
109
      Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
110
      Temp : Int;
111
      TF   : Flt;
112
 
113
   begin
114
      --  Check for flat range here, since we are typically run with checks
115
      --  off, note that in practice, this condition will usually be static
116
      --  so we will not actually generate any code for the normal case.
117
 
118
      if Rst'Last < Rst'First then
119
         raise Constraint_Error;
120
      end if;
121
 
122
      --  Continue with computation if non-flat range
123
 
124
      Genp.X1 := Square_Mod_N (Genp.X1, Genp.P);
125
      Genp.X2 := Square_Mod_N (Genp.X2, Genp.Q);
126
      Temp := Genp.X2 - Genp.X1;
127
 
128
      --  Following duplication is not an error, it is a loop unwinding!
129
 
130
      if Temp < 0 then
131
         Temp := Temp + Genp.Q;
132
      end if;
133
 
134
      if Temp < 0 then
135
         Temp := Temp + Genp.Q;
136
      end if;
137
 
138
      TF := Offs + (Flt (Temp) * Flt (Genp.P) + Flt (Genp.X1)) * Genp.Scl;
139
 
140
      --  Pathological, but there do exist cases where the rounding implicit
141
      --  in calculating the scale factor will cause rounding to 'Last + 1.
142
      --  In those cases, returning 'First results in the least bias.
143
 
144
      if TF >= Flt (Rst'Pos (Rst'Last)) + 0.5 then
145
         return Rst'First;
146
 
147
      elsif not Fits_In_32_Bits then
148
         return Rst'Val (Interfaces.Integer_64 (TF));
149
 
150
      else
151
         return Rst'Val (Int (TF));
152
      end if;
153
   end Random;
154
 
155
   -----------
156
   -- Reset --
157
   -----------
158
 
159
   procedure Reset (Gen : Generator; Initiator : Integer) is
160
      Genp   : constant Pointer := Gen.Gen_State'Unrestricted_Access;
161
      X1, X2 : Int;
162
 
163
   begin
164
      X1 := 2 + Int (Initiator) mod (K1 - 3);
165
      X2 := 2 + Int (Initiator) mod (K2 - 3);
166
 
167
      for J in 1 .. 5 loop
168
         X1 := Square_Mod_N (X1, K1);
169
         X2 := Square_Mod_N (X2, K2);
170
      end loop;
171
 
172
      --  Eliminate effects of small Initiators
173
 
174
      Genp.all :=
175
        (X1  => X1,
176
         X2  => X2,
177
         P   => K1,
178
         Q   => K2,
179
         FP  => K1F,
180
         Scl => Scal);
181
   end Reset;
182
 
183
   -----------
184
   -- Reset --
185
   -----------
186
 
187
   procedure Reset (Gen : Generator) is
188
      Genp : constant Pointer       := Gen.Gen_State'Unrestricted_Access;
189
      Now  : constant Calendar.Time := Calendar.Clock;
190
      X1   : Int;
191
      X2   : Int;
192
 
193
   begin
194
      X1 := Int (Calendar.Year    (Now)) * 12 * 31 +
195
            Int (Calendar.Month   (Now) * 31)     +
196
            Int (Calendar.Day     (Now));
197
 
198
      X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
199
 
200
      X1 := 2 + X1 mod (K1 - 3);
201
      X2 := 2 + X2 mod (K2 - 3);
202
 
203
      --  Eliminate visible effects of same day starts
204
 
205
      for J in 1 .. 5 loop
206
         X1 := Square_Mod_N (X1, K1);
207
         X2 := Square_Mod_N (X2, K2);
208
      end loop;
209
 
210
      Genp.all :=
211
        (X1  => X1,
212
         X2  => X2,
213
         P   => K1,
214
         Q   => K2,
215
         FP  => K1F,
216
         Scl => Scal);
217
 
218
   end Reset;
219
 
220
   -----------
221
   -- Reset --
222
   -----------
223
 
224
   procedure Reset (Gen : Generator; From_State : State) is
225
      Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
226
   begin
227
      Genp.all := From_State;
228
   end Reset;
229
 
230
   ----------
231
   -- Save --
232
   ----------
233
 
234
   procedure Save (Gen : Generator; To_State : out State) is
235
   begin
236
      To_State := Gen.Gen_State;
237
   end Save;
238
 
239
   ------------------
240
   -- Square_Mod_N --
241
   ------------------
242
 
243
   function Square_Mod_N (X, N : Int) return Int is
244
   begin
245
      return Int ((Integer_64 (X) ** 2) mod (Integer_64 (N)));
246
   end Square_Mod_N;
247
 
248
   -----------
249
   -- Value --
250
   -----------
251
 
252
   function Value (Coded_State : String) return State is
253
      Last  : constant Natural := Coded_State'Last;
254
      Start : Positive := Coded_State'First;
255
      Stop  : Positive := Coded_State'First;
256
      Outs  : State;
257
 
258
   begin
259
      while Stop <= Last and then Coded_State (Stop) /= ',' loop
260
         Stop := Stop + 1;
261
      end loop;
262
 
263
      if Stop > Last then
264
         raise Constraint_Error;
265
      end if;
266
 
267
      Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
268
      Start := Stop + 1;
269
 
270
      loop
271
         Stop := Stop + 1;
272
         exit when Stop > Last or else Coded_State (Stop) = ',';
273
      end loop;
274
 
275
      if Stop > Last then
276
         raise Constraint_Error;
277
      end if;
278
 
279
      Outs.X2  := Int'Value (Coded_State (Start .. Stop - 1));
280
      Outs.Q   := Int'Value (Coded_State (Stop + 1 .. Last));
281
      Outs.P   := Outs.Q * 2 + 1;
282
      Outs.FP  := Flt (Outs.P);
283
      Outs.Scl := (RstL - RstF + 1.0) / (Flt (Outs.P) * Flt (Outs.Q));
284
 
285
      --  Now do *some* sanity checks
286
 
287
      if Outs.Q < 31
288
        or else Outs.X1 not in 2 .. Outs.P - 1
289
        or else Outs.X2 not in 2 .. Outs.Q - 1
290
      then
291
         raise Constraint_Error;
292
      end if;
293
 
294
      return Outs;
295
   end Value;
296
 
297
end Ada.Numerics.Discrete_Random;

powered by: WebSVN 2.1.0

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