* sh.h (REG_CLASS_FROM_LETTER): Change to:
[official-gcc.git] / gcc / ada / a-nudira.adb
blob298aecc986c85316438490abade8d4e8e1bf7f18
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 -- Copyright (C) 1992-1999 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;
35 with Interfaces; use Interfaces;
37 package body Ada.Numerics.Discrete_Random is
39 -------------------------
40 -- Implementation Note --
41 -------------------------
43 -- The design of this spec is very awkward, as a result of Ada 95 not
44 -- permitting in-out parameters for function formals (most naturally
45 -- Generator values would be passed this way). In pure Ada 95, the only
46 -- solution is to use the heap and pointers, and, to avoid memory leaks,
47 -- controlled types.
49 -- This is awfully heavy, so what we do is to use Unrestricted_Access to
50 -- get a pointer to the state in the passed Generator. This works because
51 -- Generator is a limited type and will thus always be passed by reference.
53 type Pointer is access all State;
55 Need_64 : constant Boolean := Rst'Pos (Rst'Last) > Int'Last;
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 function Square_Mod_N (X, N : Int) return Int;
62 pragma Inline (Square_Mod_N);
63 -- Computes X**2 mod N avoiding intermediate overflow
65 -----------
66 -- Image --
67 -----------
69 function Image (Of_State : State) return String is
70 begin
71 return Int'Image (Of_State.X1) &
72 ',' &
73 Int'Image (Of_State.X2) &
74 ',' &
75 Int'Image (Of_State.Q);
76 end Image;
78 ------------
79 -- Random --
80 ------------
82 function Random (Gen : Generator) return Rst is
83 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
84 Temp : Int;
85 TF : Flt;
87 begin
88 -- Check for flat range here, since we are typically run with checks
89 -- off, note that in practice, this condition will usually be static
90 -- so we will not actually generate any code for the normal case.
92 if Rst'Last < Rst'First then
93 raise Constraint_Error;
94 end if;
96 -- Continue with computation if non-flat range
98 Genp.X1 := Square_Mod_N (Genp.X1, Genp.P);
99 Genp.X2 := Square_Mod_N (Genp.X2, Genp.Q);
100 Temp := Genp.X2 - Genp.X1;
102 -- Following duplication is not an error, it is a loop unwinding!
104 if Temp < 0 then
105 Temp := Temp + Genp.Q;
106 end if;
108 if Temp < 0 then
109 Temp := Temp + Genp.Q;
110 end if;
112 TF := Offs + (Flt (Temp) * Flt (Genp.P) + Flt (Genp.X1)) * Genp.Scl;
114 -- Pathological, but there do exist cases where the rounding implicit
115 -- in calculating the scale factor will cause rounding to 'Last + 1.
116 -- In those cases, returning 'First results in the least bias.
118 if TF >= Flt (Rst'Pos (Rst'Last)) + 0.5 then
119 return Rst'First;
121 elsif Need_64 then
122 return Rst'Val (Interfaces.Integer_64 (TF));
124 else
125 return Rst'Val (Int (TF));
126 end if;
128 end Random;
130 -----------
131 -- Reset --
132 -----------
134 procedure Reset (Gen : Generator; Initiator : Integer) is
135 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
136 X1, X2 : Int;
138 begin
139 X1 := 2 + Int (Initiator) mod (K1 - 3);
140 X2 := 2 + Int (Initiator) mod (K2 - 3);
142 for J in 1 .. 5 loop
143 X1 := Square_Mod_N (X1, K1);
144 X2 := Square_Mod_N (X2, K2);
145 end loop;
147 -- eliminate effects of small Initiators.
149 Genp.all :=
150 (X1 => X1,
151 X2 => X2,
152 P => K1,
153 Q => K2,
154 FP => K1F,
155 Scl => Scal);
156 end Reset;
158 -----------
159 -- Reset --
160 -----------
162 procedure Reset (Gen : Generator) is
163 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
164 Now : constant Calendar.Time := Calendar.Clock;
165 X1 : Int;
166 X2 : Int;
168 begin
169 X1 := Int (Calendar.Year (Now)) * 12 * 31 +
170 Int (Calendar.Month (Now) * 31) +
171 Int (Calendar.Day (Now));
173 X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
175 X1 := 2 + X1 mod (K1 - 3);
176 X2 := 2 + X2 mod (K2 - 3);
178 -- Eliminate visible effects of same day starts
180 for J in 1 .. 5 loop
181 X1 := Square_Mod_N (X1, K1);
182 X2 := Square_Mod_N (X2, K2);
183 end loop;
185 Genp.all :=
186 (X1 => X1,
187 X2 => X2,
188 P => K1,
189 Q => K2,
190 FP => K1F,
191 Scl => Scal);
193 end Reset;
195 -----------
196 -- Reset --
197 -----------
199 procedure Reset (Gen : Generator; From_State : State) is
200 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
202 begin
203 Genp.all := From_State;
204 end Reset;
206 ----------
207 -- Save --
208 ----------
210 procedure Save (Gen : Generator; To_State : out State) is
211 begin
212 To_State := Gen.Gen_State;
213 end Save;
215 ------------------
216 -- Square_Mod_N --
217 ------------------
219 function Square_Mod_N (X, N : Int) return Int is
220 begin
221 return Int ((Integer_64 (X) ** 2) mod (Integer_64 (N)));
222 end Square_Mod_N;
224 -----------
225 -- Value --
226 -----------
228 function Value (Coded_State : String) return State is
229 Start : Positive := Coded_State'First;
230 Stop : Positive := Coded_State'First;
231 Outs : State;
233 begin
234 while Coded_State (Stop) /= ',' loop
235 Stop := Stop + 1;
236 end loop;
238 Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
239 Start := Stop + 1;
241 loop
242 Stop := Stop + 1;
243 exit when Coded_State (Stop) = ',';
244 end loop;
246 Outs.X2 := Int'Value (Coded_State (Start .. Stop - 1));
247 Outs.Q := Int'Value (Coded_State (Stop + 1 .. Coded_State'Last));
248 Outs.P := Outs.Q * 2 + 1;
249 Outs.FP := Flt (Outs.P);
250 Outs.Scl := (RstL - RstF + 1.0) / (Flt (Outs.P) * Flt (Outs.Q));
252 -- Now do *some* sanity checks.
254 if Outs.Q < 31
255 or else Outs.X1 not in 2 .. Outs.P - 1
256 or else Outs.X2 not in 2 .. Outs.Q - 1
257 then
258 raise Constraint_Error;
259 end if;
261 return Outs;
262 end Value;
264 end Ada.Numerics.Discrete_Random;