OSDN Git Service

2010-06-22 Paul Hilfinger <hilfinger@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-mbdira.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --            G N A T . M B S 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-2010, 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 3,  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.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 with Ada.Calendar;
33
34 with Interfaces; use Interfaces;
35
36 package body GNAT.MBBS_Discrete_Random is
37
38    -------------------------
39    -- Implementation Note --
40    -------------------------
41
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.
47
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.
51
52    package Calendar renames Ada.Calendar;
53
54    type Pointer is access all State;
55
56    Fits_In_32_Bits : constant Boolean :=
57                        Rst'Size < 31
58                          or else (Rst'Size = 31
59                                   and then Rst'Pos (Rst'First) < 0);
60    --  This is set True if we do not need more than 32 bits in the result. If
61    --  we need 64-bits, we will only use the meaningful 48 bits of any 64-bit
62    --  number generated, since if more than 48 bits are required, we split the
63    --  computation into two separate parts, since the algorithm does not behave
64    --  above 48 bits.
65
66    --  The way this expression works is that obviously if the size is 31 bits,
67    --  it fits in 32 bits. In the 32-bit case, it fits in 32-bit signed if the
68    --  range has negative values. It is too conservative in the case that the
69    --  programmer has set a size greater than the default, e.g. a size of 33
70    --  for an integer type with a range of 1..10, but an over-conservative
71    --  result is OK. The important thing is that the value is only True if
72    --  we know the result will fit in 32-bits signed. If the value is False
73    --  when it could be True, the behavior will be correct, just a bit less
74    --  efficient than it could have been in some unusual cases.
75    --
76    --  One might assume that we could get a more accurate result by testing
77    --  the lower and upper bounds of the type Rst against the bounds of 32-bit
78    --  Integer. However, there is no easy way to do that. Why? Because in the
79    --  relatively rare case where this expresion has to be evaluated at run
80    --  time rather than compile time (when the bounds are dynamic), we need a
81    --  type to use for the computation. But the possible range of upper bound
82    --  values for Rst (remembering the possibility of 64-bit modular types) is
83    --  from -2**63 to 2**64-1, and no run-time type has a big enough range.
84
85    -----------------------
86    -- Local Subprograms --
87    -----------------------
88
89    function Square_Mod_N (X, N : Int) return Int;
90    pragma Inline (Square_Mod_N);
91    --  Computes X**2 mod N avoiding intermediate overflow
92
93    -----------
94    -- Image --
95    -----------
96
97    function Image (Of_State : State) return String is
98    begin
99       return Int'Image (Of_State.X1) &
100              ','                     &
101              Int'Image (Of_State.X2) &
102              ','                     &
103              Int'Image (Of_State.Q);
104    end Image;
105
106    ------------
107    -- Random --
108    ------------
109
110    function Random (Gen : Generator) return Rst is
111       Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
112       Temp : Int;
113       TF   : Flt;
114
115    begin
116       --  Check for flat range here, since we are typically run with checks
117       --  off, note that in practice, this condition will usually be static
118       --  so we will not actually generate any code for the normal case.
119
120       if Rst'Last < Rst'First then
121          raise Constraint_Error;
122       end if;
123
124       --  Continue with computation if non-flat range
125
126       Genp.X1 := Square_Mod_N (Genp.X1, Genp.P);
127       Genp.X2 := Square_Mod_N (Genp.X2, Genp.Q);
128       Temp := Genp.X2 - Genp.X1;
129
130       --  Following duplication is not an error, it is a loop unwinding!
131
132       if Temp < 0 then
133          Temp := Temp + Genp.Q;
134       end if;
135
136       if Temp < 0 then
137          Temp := Temp + Genp.Q;
138       end if;
139
140       TF := Offs + (Flt (Temp) * Flt (Genp.P) + Flt (Genp.X1)) * Genp.Scl;
141
142       --  Pathological, but there do exist cases where the rounding implicit
143       --  in calculating the scale factor will cause rounding to 'Last + 1.
144       --  In those cases, returning 'First results in the least bias.
145
146       if TF >= Flt (Rst'Pos (Rst'Last)) + 0.5 then
147          return Rst'First;
148
149       elsif not Fits_In_32_Bits then
150          return Rst'Val (Interfaces.Integer_64 (TF));
151
152       else
153          return Rst'Val (Int (TF));
154       end if;
155    end Random;
156
157    -----------
158    -- Reset --
159    -----------
160
161    procedure Reset (Gen : Generator; Initiator : Integer) is
162       Genp   : constant Pointer := Gen.Gen_State'Unrestricted_Access;
163       X1, X2 : Int;
164
165    begin
166       X1 := 2 + Int (Initiator) mod (K1 - 3);
167       X2 := 2 + Int (Initiator) mod (K2 - 3);
168
169       for J in 1 .. 5 loop
170          X1 := Square_Mod_N (X1, K1);
171          X2 := Square_Mod_N (X2, K2);
172       end loop;
173
174       --  Eliminate effects of small Initiators
175
176       Genp.all :=
177         (X1  => X1,
178          X2  => X2,
179          P   => K1,
180          Q   => K2,
181          FP  => K1F,
182          Scl => Scal);
183    end Reset;
184
185    -----------
186    -- Reset --
187    -----------
188
189    procedure Reset (Gen : Generator) is
190       Genp : constant Pointer       := Gen.Gen_State'Unrestricted_Access;
191       Now  : constant Calendar.Time := Calendar.Clock;
192       X1   : Int;
193       X2   : Int;
194
195    begin
196       X1 := Int (Calendar.Year    (Now)) * 12 * 31 +
197             Int (Calendar.Month   (Now) * 31)     +
198             Int (Calendar.Day     (Now));
199
200       X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
201
202       X1 := 2 + X1 mod (K1 - 3);
203       X2 := 2 + X2 mod (K2 - 3);
204
205       --  Eliminate visible effects of same day starts
206
207       for J in 1 .. 5 loop
208          X1 := Square_Mod_N (X1, K1);
209          X2 := Square_Mod_N (X2, K2);
210       end loop;
211
212       Genp.all :=
213         (X1  => X1,
214          X2  => X2,
215          P   => K1,
216          Q   => K2,
217          FP  => K1F,
218          Scl => Scal);
219
220    end Reset;
221
222    -----------
223    -- Reset --
224    -----------
225
226    procedure Reset (Gen : Generator; From_State : State) is
227       Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
228    begin
229       Genp.all := From_State;
230    end Reset;
231
232    ----------
233    -- Save --
234    ----------
235
236    procedure Save (Gen : Generator; To_State : out State) is
237    begin
238       To_State := Gen.Gen_State;
239    end Save;
240
241    ------------------
242    -- Square_Mod_N --
243    ------------------
244
245    function Square_Mod_N (X, N : Int) return Int is
246    begin
247       return Int ((Integer_64 (X) ** 2) mod (Integer_64 (N)));
248    end Square_Mod_N;
249
250    -----------
251    -- Value --
252    -----------
253
254    function Value (Coded_State : String) return State is
255       Last  : constant Natural := Coded_State'Last;
256       Start : Positive := Coded_State'First;
257       Stop  : Positive := Coded_State'First;
258       Outs  : State;
259
260    begin
261       while Stop <= Last and then Coded_State (Stop) /= ',' loop
262          Stop := Stop + 1;
263       end loop;
264
265       if Stop > Last then
266          raise Constraint_Error;
267       end if;
268
269       Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
270       Start := Stop + 1;
271
272       loop
273          Stop := Stop + 1;
274          exit when Stop > Last or else Coded_State (Stop) = ',';
275       end loop;
276
277       if Stop > Last then
278          raise Constraint_Error;
279       end if;
280
281       Outs.X2  := Int'Value (Coded_State (Start .. Stop - 1));
282       Outs.Q   := Int'Value (Coded_State (Stop + 1 .. Last));
283       Outs.P   := Outs.Q * 2 + 1;
284       Outs.FP  := Flt (Outs.P);
285       Outs.Scl := (RstL - RstF + 1.0) / (Flt (Outs.P) * Flt (Outs.Q));
286
287       --  Now do *some* sanity checks
288
289       if Outs.Q < 31
290         or else Outs.X1 not in 2 .. Outs.P - 1
291         or else Outs.X2 not in 2 .. Outs.Q - 1
292       then
293          raise Constraint_Error;
294       end if;
295
296       return Outs;
297    end Value;
298
299 end GNAT.MBBS_Discrete_Random;