OSDN Git Service

* rtl.h (mem_attrs): Rename decl to expr; adjust all users.
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-nudira.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUNTIME 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 --                            $Revision: 1.17 $
10 --                                                                          --
11 --          Copyright (C) 1992-1999 Free Software Foundation, Inc.          --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNAT was originally developed  by the GNAT team at  New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 --                                                                          --
34 ------------------------------------------------------------------------------
35
36 with Ada.Calendar;
37 with Interfaces; use Interfaces;
38
39 package body Ada.Numerics.Discrete_Random is
40
41    -------------------------
42    -- Implementation Note --
43    -------------------------
44
45    --  The design of this spec is very awkward, as a result of Ada 95 not
46    --  permitting in-out parameters for function formals (most naturally
47    --  Generator values would be passed this way). In pure Ada 95, the only
48    --  solution is to use the heap and pointers, and, to avoid memory leaks,
49    --  controlled types.
50
51    --  This is awfully heavy, so what we do is to use Unrestricted_Access to
52    --  get a pointer to the state in the passed Generator. This works because
53    --  Generator is a limited type and will thus always be passed by reference.
54
55    type Pointer is access all State;
56
57    Need_64 : constant Boolean := Rst'Pos (Rst'Last) > Int'Last;
58
59    -----------------------
60    -- Local Subprograms --
61    -----------------------
62
63    function Square_Mod_N (X, N : Int) return Int;
64    pragma Inline (Square_Mod_N);
65    --  Computes X**2 mod N avoiding intermediate overflow
66
67    -----------
68    -- Image --
69    -----------
70
71    function Image (Of_State : State) return String is
72    begin
73       return Int'Image (Of_State.X1) &
74              ','                            &
75              Int'Image (Of_State.X2) &
76              ','                            &
77              Int'Image (Of_State.Q);
78    end Image;
79
80    ------------
81    -- Random --
82    ------------
83
84    function Random (Gen : Generator) return Rst is
85       Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
86       Temp : Int;
87       TF   : Flt;
88
89    begin
90       --  Check for flat range here, since we are typically run with checks
91       --  off, note that in practice, this condition will usually be static
92       --  so we will not actually generate any code for the normal case.
93
94       if Rst'Last < Rst'First then
95          raise Constraint_Error;
96       end if;
97
98       --  Continue with computation if non-flat range
99
100       Genp.X1 := Square_Mod_N (Genp.X1, Genp.P);
101       Genp.X2 := Square_Mod_N (Genp.X2, Genp.Q);
102       Temp := Genp.X2 - Genp.X1;
103
104       --  Following duplication is not an error, it is a loop unwinding!
105
106       if Temp < 0 then
107          Temp := Temp + Genp.Q;
108       end if;
109
110       if Temp < 0 then
111          Temp := Temp + Genp.Q;
112       end if;
113
114       TF :=  Offs + (Flt (Temp) * Flt (Genp.P) + Flt (Genp.X1)) * Genp.Scl;
115
116       --  Pathological, but there do exist cases where the rounding implicit
117       --  in calculating the scale factor will cause rounding to 'Last + 1.
118       --  In those cases, returning 'First results in the least bias.
119
120       if TF >= Flt (Rst'Pos (Rst'Last)) + 0.5 then
121          return Rst'First;
122
123       elsif Need_64 then
124          return Rst'Val (Interfaces.Integer_64 (TF));
125
126       else
127          return Rst'Val (Int (TF));
128       end if;
129
130    end Random;
131
132    -----------
133    -- Reset --
134    -----------
135
136    procedure Reset (Gen : Generator; Initiator : Integer) is
137       Genp   : constant Pointer := Gen.Gen_State'Unrestricted_Access;
138       X1, X2 : Int;
139
140    begin
141       X1 := 2 + Int (Initiator) mod (K1 - 3);
142       X2 := 2 + Int (Initiator) mod (K2 - 3);
143
144       for J in 1 .. 5 loop
145          X1 := Square_Mod_N (X1, K1);
146          X2 := Square_Mod_N (X2, K2);
147       end loop;
148
149       --  eliminate effects of small Initiators.
150
151       Genp.all :=
152         (X1  => X1,
153          X2  => X2,
154          P   => K1,
155          Q   => K2,
156          FP  => K1F,
157          Scl => Scal);
158    end Reset;
159
160    -----------
161    -- Reset --
162    -----------
163
164    procedure Reset (Gen : Generator) is
165       Genp : constant Pointer       := Gen.Gen_State'Unrestricted_Access;
166       Now  : constant Calendar.Time := Calendar.Clock;
167       X1   : Int;
168       X2   : Int;
169
170    begin
171       X1 := Int (Calendar.Year    (Now)) * 12 * 31 +
172             Int (Calendar.Month   (Now) * 31)     +
173             Int (Calendar.Day     (Now));
174
175       X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
176
177       X1 := 2 + X1 mod (K1 - 3);
178       X2 := 2 + X2 mod (K2 - 3);
179
180       --  Eliminate visible effects of same day starts
181
182       for J in 1 .. 5 loop
183          X1 := Square_Mod_N (X1, K1);
184          X2 := Square_Mod_N (X2, K2);
185       end loop;
186
187       Genp.all :=
188         (X1  => X1,
189          X2  => X2,
190          P   => K1,
191          Q   => K2,
192          FP  => K1F,
193          Scl => Scal);
194
195    end Reset;
196
197    -----------
198    -- Reset --
199    -----------
200
201    procedure Reset (Gen : Generator; From_State : State) is
202       Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
203
204    begin
205       Genp.all := From_State;
206    end Reset;
207
208    ----------
209    -- Save --
210    ----------
211
212    procedure Save (Gen : Generator; To_State : out State) is
213    begin
214       To_State := Gen.Gen_State;
215    end Save;
216
217    ------------------
218    -- Square_Mod_N --
219    ------------------
220
221    function Square_Mod_N (X, N : Int) return Int is
222    begin
223       return Int ((Integer_64 (X) ** 2) mod (Integer_64 (N)));
224    end Square_Mod_N;
225
226    -----------
227    -- Value --
228    -----------
229
230    function Value (Coded_State : String) return State is
231       Start : Positive := Coded_State'First;
232       Stop  : Positive := Coded_State'First;
233       Outs  : State;
234
235    begin
236       while Coded_State (Stop) /= ',' loop
237          Stop := Stop + 1;
238       end loop;
239
240       Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
241       Start := Stop + 1;
242
243       loop
244          Stop := Stop + 1;
245          exit when Coded_State (Stop) = ',';
246       end loop;
247
248       Outs.X2  := Int'Value (Coded_State (Start .. Stop - 1));
249       Outs.Q   := Int'Value (Coded_State (Stop + 1 .. Coded_State'Last));
250       Outs.P   := Outs.Q * 2 + 1;
251       Outs.FP  := Flt (Outs.P);
252       Outs.Scl := (RstL - RstF + 1.0) / (Flt (Outs.P) * Flt (Outs.Q));
253
254       --  Now do *some* sanity checks.
255
256       if Outs.Q < 31
257         or else Outs.X1 not in 2 .. Outs.P - 1
258         or else Outs.X2 not in 2 .. Outs.Q - 1
259       then
260          raise Constraint_Error;
261       end if;
262
263       return Outs;
264    end Value;
265
266 end Ada.Numerics.Discrete_Random;