PR tree-optimization/19853
[official-gcc.git] / gcc / ada / a-nuflra.adb
blob14d74593af0f0da010a607c895c5dab83a75145b
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUNTIME COMPONENTS --
4 -- --
5 -- A D A . N U M E R I C S . F L O A T _ R A N D O M --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2002, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with Ada.Calendar;
36 package body Ada.Numerics.Float_Random is
38 -------------------------
39 -- Implementation Note --
40 -------------------------
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.
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.
52 type Pointer is access all State;
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
58 procedure Euclid (P, Q : in Int; X, Y : out Int; GCD : out Int);
60 function Euclid (P, Q : Int) return Int;
62 function Square_Mod_N (X, N : Int) return Int;
64 ------------
65 -- Euclid --
66 ------------
68 procedure Euclid (P, Q : in Int; X, Y : out Int; GCD : out Int) is
70 XT : Int := 1;
71 YT : Int := 0;
73 procedure Recur
74 (P, Q : in Int; -- a (i-1), a (i)
75 X, Y : in Int; -- x (i), y (i)
76 XP, YP : in out Int; -- x (i-1), y (i-1)
77 GCD : out Int);
79 procedure Recur
80 (P, Q : in Int;
81 X, Y : in Int;
82 XP, YP : in out Int;
83 GCD : out Int)
85 Quo : Int := P / Q; -- q <-- |_ a (i-1) / a (i) _|
86 XT : Int := X; -- x (i)
87 YT : Int := Y; -- y (i)
89 begin
90 if P rem Q = 0 then -- while does not divide
91 GCD := Q;
92 XP := X;
93 YP := Y;
94 else
95 Recur (Q, P - Q * Quo, XP - Quo * X, YP - Quo * Y, XT, YT, Quo);
97 -- a (i) <== a (i)
98 -- a (i+1) <-- a (i-1) - q*a (i)
99 -- x (i+1) <-- x (i-1) - q*x (i)
100 -- y (i+1) <-- y (i-1) - q*y (i)
101 -- x (i) <== x (i)
102 -- y (i) <== y (i)
104 XP := XT;
105 YP := YT;
106 GCD := Quo;
107 end if;
108 end Recur;
110 -- Start of processing for Euclid
112 begin
113 Recur (P, Q, 0, 1, XT, YT, GCD);
114 X := XT;
115 Y := YT;
116 end Euclid;
118 function Euclid (P, Q : Int) return Int is
119 X, Y, GCD : Int;
121 begin
122 Euclid (P, Q, X, Y, GCD);
123 return X;
124 end Euclid;
126 -----------
127 -- Image --
128 -----------
130 function Image (Of_State : State) return String is
131 begin
132 return Int'Image (Of_State.X1) & ',' & Int'Image (Of_State.X2)
133 & ',' &
134 Int'Image (Of_State.P) & ',' & Int'Image (Of_State.Q);
135 end Image;
137 ------------
138 -- Random --
139 ------------
141 function Random (Gen : Generator) return Uniformly_Distributed is
142 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
144 begin
145 Genp.X1 := Square_Mod_N (Genp.X1, Genp.P);
146 Genp.X2 := Square_Mod_N (Genp.X2, Genp.Q);
147 return
148 Float ((Flt (((Genp.X2 - Genp.X1) * Genp.X)
149 mod Genp.Q) * Flt (Genp.P)
150 + Flt (Genp.X1)) * Genp.Scl);
151 end Random;
153 -----------
154 -- Reset --
155 -----------
157 -- Version that works from given initiator value
159 procedure Reset (Gen : in Generator; Initiator : in Integer) is
160 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
161 X1, X2 : Int;
163 begin
164 X1 := 2 + Int (Initiator) mod (K1 - 3);
165 X2 := 2 + Int (Initiator) mod (K2 - 3);
167 -- Eliminate effects of small Initiators.
169 for J in 1 .. 5 loop
170 X1 := Square_Mod_N (X1, K1);
171 X2 := Square_Mod_N (X2, K2);
172 end loop;
174 Genp.all :=
175 (X1 => X1,
176 X2 => X2,
177 P => K1,
178 Q => K2,
179 X => 1,
180 Scl => Scal);
181 end Reset;
183 -- Version that works from specific saved state
185 procedure Reset (Gen : Generator; From_State : State) is
186 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
188 begin
189 Genp.all := From_State;
190 end Reset;
192 -- Version that works from calendar
194 procedure Reset (Gen : Generator) is
195 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
196 Now : constant Calendar.Time := Calendar.Clock;
197 X1, X2 : Int;
199 begin
200 X1 := Int (Calendar.Year (Now)) * 12 * 31 +
201 Int (Calendar.Month (Now)) * 31 +
202 Int (Calendar.Day (Now));
204 X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
206 X1 := 2 + X1 mod (K1 - 3);
207 X2 := 2 + X2 mod (K2 - 3);
209 -- Eliminate visible effects of same day starts
211 for J in 1 .. 5 loop
212 X1 := Square_Mod_N (X1, K1);
213 X2 := Square_Mod_N (X2, K2);
214 end loop;
216 Genp.all :=
217 (X1 => X1,
218 X2 => X2,
219 P => K1,
220 Q => K2,
221 X => 1,
222 Scl => Scal);
224 end Reset;
226 ----------
227 -- Save --
228 ----------
230 procedure Save (Gen : in Generator; To_State : out State) is
231 begin
232 To_State := Gen.Gen_State;
233 end Save;
235 ------------------
236 -- Square_Mod_N --
237 ------------------
239 function Square_Mod_N (X, N : Int) return Int is
240 Temp : constant Flt := Flt (X) * Flt (X);
241 Div : Int;
243 begin
244 Div := Int (Temp / Flt (N));
245 Div := Int (Temp - Flt (Div) * Flt (N));
247 if Div < 0 then
248 return Div + N;
249 else
250 return Div;
251 end if;
252 end Square_Mod_N;
254 -----------
255 -- Value --
256 -----------
258 function Value (Coded_State : String) return State is
259 Start : Positive := Coded_State'First;
260 Stop : Positive := Coded_State'First;
261 Outs : State;
263 begin
264 while Coded_State (Stop) /= ',' loop
265 Stop := Stop + 1;
266 end loop;
268 Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
269 Start := Stop + 1;
271 loop
272 Stop := Stop + 1;
273 exit when Coded_State (Stop) = ',';
274 end loop;
276 Outs.X2 := Int'Value (Coded_State (Start .. Stop - 1));
277 Start := Stop + 1;
279 loop
280 Stop := Stop + 1;
281 exit when Coded_State (Stop) = ',';
282 end loop;
284 Outs.P := Int'Value (Coded_State (Start .. Stop - 1));
285 Outs.Q := Int'Value (Coded_State (Stop + 1 .. Coded_State'Last));
286 Outs.X := Euclid (Outs.P, Outs.Q);
287 Outs.Scl := 1.0 / (Flt (Outs.P) * Flt (Outs.Q));
289 -- Now do *some* sanity checks.
291 if Outs.Q < 31 or else Outs.P < 31
292 or else Outs.X1 not in 2 .. Outs.P - 1
293 or else Outs.X2 not in 2 .. Outs.Q - 1
294 then
295 raise Constraint_Error;
296 end if;
298 return Outs;
299 end Value;
300 end Ada.Numerics.Float_Random;