re PR rtl-optimization/34522 (inefficient code for long long multiply when only low...
[official-gcc.git] / gcc / ada / a-nudira.adb
blobdd8408f2d81433f4bf3f16ed8a9859d93f1d2f39
1 ------------------------------------------------------------------------------
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-2005, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 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;
57 -- Set if we need more than 32 bits in the result. In practice we will
58 -- only use the meaningful 48 bits of any 64 bit number generated, since
59 -- if more than 48 bits are required, we split the computation into two
60 -- separate parts, since the algorithm does not behave above 48 bits.
62 -----------------------
63 -- Local Subprograms --
64 -----------------------
66 function Square_Mod_N (X, N : Int) return Int;
67 pragma Inline (Square_Mod_N);
68 -- Computes X**2 mod N avoiding intermediate overflow
70 -----------
71 -- Image --
72 -----------
74 function Image (Of_State : State) return String is
75 begin
76 return Int'Image (Of_State.X1) &
77 ',' &
78 Int'Image (Of_State.X2) &
79 ',' &
80 Int'Image (Of_State.Q);
81 end Image;
83 ------------
84 -- Random --
85 ------------
87 function Random (Gen : Generator) return Rst is
88 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
89 Temp : Int;
90 TF : Flt;
92 begin
93 -- Check for flat range here, since we are typically run with checks
94 -- off, note that in practice, this condition will usually be static
95 -- so we will not actually generate any code for the normal case.
97 if Rst'Last < Rst'First then
98 raise Constraint_Error;
99 end if;
101 -- Continue with computation if non-flat range
103 Genp.X1 := Square_Mod_N (Genp.X1, Genp.P);
104 Genp.X2 := Square_Mod_N (Genp.X2, Genp.Q);
105 Temp := Genp.X2 - Genp.X1;
107 -- Following duplication is not an error, it is a loop unwinding!
109 if Temp < 0 then
110 Temp := Temp + Genp.Q;
111 end if;
113 if Temp < 0 then
114 Temp := Temp + Genp.Q;
115 end if;
117 TF := Offs + (Flt (Temp) * Flt (Genp.P) + Flt (Genp.X1)) * Genp.Scl;
119 -- Pathological, but there do exist cases where the rounding implicit
120 -- in calculating the scale factor will cause rounding to 'Last + 1.
121 -- In those cases, returning 'First results in the least bias.
123 if TF >= Flt (Rst'Pos (Rst'Last)) + 0.5 then
124 return Rst'First;
126 elsif Need_64 then
127 return Rst'Val (Interfaces.Integer_64 (TF));
129 else
130 return Rst'Val (Int (TF));
131 end if;
132 end Random;
134 -----------
135 -- Reset --
136 -----------
138 procedure Reset (Gen : Generator; Initiator : Integer) is
139 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
140 X1, X2 : Int;
142 begin
143 X1 := 2 + Int (Initiator) mod (K1 - 3);
144 X2 := 2 + Int (Initiator) mod (K2 - 3);
146 for J in 1 .. 5 loop
147 X1 := Square_Mod_N (X1, K1);
148 X2 := Square_Mod_N (X2, K2);
149 end loop;
151 -- Eliminate effects of small Initiators
153 Genp.all :=
154 (X1 => X1,
155 X2 => X2,
156 P => K1,
157 Q => K2,
158 FP => K1F,
159 Scl => Scal);
160 end Reset;
162 -----------
163 -- Reset --
164 -----------
166 procedure Reset (Gen : Generator) is
167 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
168 Now : constant Calendar.Time := Calendar.Clock;
169 X1 : Int;
170 X2 : Int;
172 begin
173 X1 := Int (Calendar.Year (Now)) * 12 * 31 +
174 Int (Calendar.Month (Now) * 31) +
175 Int (Calendar.Day (Now));
177 X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
179 X1 := 2 + X1 mod (K1 - 3);
180 X2 := 2 + X2 mod (K2 - 3);
182 -- Eliminate visible effects of same day starts
184 for J in 1 .. 5 loop
185 X1 := Square_Mod_N (X1, K1);
186 X2 := Square_Mod_N (X2, K2);
187 end loop;
189 Genp.all :=
190 (X1 => X1,
191 X2 => X2,
192 P => K1,
193 Q => K2,
194 FP => K1F,
195 Scl => Scal);
197 end Reset;
199 -----------
200 -- Reset --
201 -----------
203 procedure Reset (Gen : Generator; From_State : State) is
204 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
205 begin
206 Genp.all := From_State;
207 end Reset;
209 ----------
210 -- Save --
211 ----------
213 procedure Save (Gen : Generator; To_State : out State) is
214 begin
215 To_State := Gen.Gen_State;
216 end Save;
218 ------------------
219 -- Square_Mod_N --
220 ------------------
222 function Square_Mod_N (X, N : Int) return Int is
223 begin
224 return Int ((Integer_64 (X) ** 2) mod (Integer_64 (N)));
225 end Square_Mod_N;
227 -----------
228 -- Value --
229 -----------
231 function Value (Coded_State : String) return State is
232 Last : constant Natural := Coded_State'Last;
233 Start : Positive := Coded_State'First;
234 Stop : Positive := Coded_State'First;
235 Outs : State;
237 begin
238 while Stop <= Last and then Coded_State (Stop) /= ',' loop
239 Stop := Stop + 1;
240 end loop;
242 if Stop > Last then
243 raise Constraint_Error;
244 end if;
246 Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
247 Start := Stop + 1;
249 loop
250 Stop := Stop + 1;
251 exit when Stop > Last or else Coded_State (Stop) = ',';
252 end loop;
254 if Stop > Last then
255 raise Constraint_Error;
256 end if;
258 Outs.X2 := Int'Value (Coded_State (Start .. Stop - 1));
259 Outs.Q := Int'Value (Coded_State (Stop + 1 .. Last));
260 Outs.P := Outs.Q * 2 + 1;
261 Outs.FP := Flt (Outs.P);
262 Outs.Scl := (RstL - RstF + 1.0) / (Flt (Outs.P) * Flt (Outs.Q));
264 -- Now do *some* sanity checks
266 if Outs.Q < 31
267 or else Outs.X1 not in 2 .. Outs.P - 1
268 or else Outs.X2 not in 2 .. Outs.Q - 1
269 then
270 raise Constraint_Error;
271 end if;
273 return Outs;
274 end Value;
276 end Ada.Numerics.Discrete_Random;