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