1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . M B B S _ D I S C R E T E _ R A N D O M --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
34 with Interfaces; use Interfaces;
36 package body GNAT.MBBS_Discrete_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,
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 package Calendar renames Ada.Calendar;
54 type Pointer is access all State;
56 Fits_In_32_Bits : constant Boolean :=
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
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.
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.
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
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
97 function Image (Of_State : State) return String is
99 return Int'Image (Of_State.X1) &
101 Int'Image (Of_State.X2) &
103 Int'Image (Of_State.Q);
110 function Random (Gen : Generator) return Rst is
111 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
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.
120 if Rst'Last < Rst'First then
121 raise Constraint_Error;
124 -- Continue with computation if non-flat range
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;
130 -- Following duplication is not an error, it is a loop unwinding!
133 Temp := Temp + Genp.Q;
137 Temp := Temp + Genp.Q;
140 TF := Offs + (Flt (Temp) * Flt (Genp.P) + Flt (Genp.X1)) * Genp.Scl;
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.
146 if TF >= Flt (Rst'Pos (Rst'Last)) + 0.5 then
149 elsif not Fits_In_32_Bits then
150 return Rst'Val (Interfaces.Integer_64 (TF));
153 return Rst'Val (Int (TF));
161 procedure Reset (Gen : Generator; Initiator : Integer) is
162 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
166 X1 := 2 + Int (Initiator) mod (K1 - 3);
167 X2 := 2 + Int (Initiator) mod (K2 - 3);
170 X1 := Square_Mod_N (X1, K1);
171 X2 := Square_Mod_N (X2, K2);
174 -- Eliminate effects of small Initiators
189 procedure Reset (Gen : Generator) is
190 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
191 Now : constant Calendar.Time := Calendar.Clock;
196 X1 := Int (Calendar.Year (Now)) * 12 * 31 +
197 Int (Calendar.Month (Now) * 31) +
198 Int (Calendar.Day (Now));
200 X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
202 X1 := 2 + X1 mod (K1 - 3);
203 X2 := 2 + X2 mod (K2 - 3);
205 -- Eliminate visible effects of same day starts
208 X1 := Square_Mod_N (X1, K1);
209 X2 := Square_Mod_N (X2, K2);
226 procedure Reset (Gen : Generator; From_State : State) is
227 Genp : constant Pointer := Gen.Gen_State'Unrestricted_Access;
229 Genp.all := From_State;
236 procedure Save (Gen : Generator; To_State : out State) is
238 To_State := Gen.Gen_State;
245 function Square_Mod_N (X, N : Int) return Int is
247 return Int ((Integer_64 (X) ** 2) mod (Integer_64 (N)));
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;
261 while Stop <= Last and then Coded_State (Stop) /= ',' loop
266 raise Constraint_Error;
269 Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
274 exit when Stop > Last or else Coded_State (Stop) = ',';
278 raise Constraint_Error;
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));
287 -- Now do *some* sanity checks
290 or else Outs.X1 not in 2 .. Outs.P - 1
291 or else Outs.X2 not in 2 .. Outs.Q - 1
293 raise Constraint_Error;
299 end GNAT.MBBS_Discrete_Random;