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-2013, 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 package Calendar renames Ada.Calendar;
40 Fits_In_32_Bits : constant Boolean :=
42 or else (Rst'Size = 31
43 and then Rst'Pos (Rst'First) < 0);
44 -- This is set True if we do not need more than 32 bits in the result. If
45 -- we need 64-bits, we will only use the meaningful 48 bits of any 64-bit
46 -- number generated, since if more than 48 bits are required, we split the
47 -- computation into two separate parts, since the algorithm does not behave
50 -- The way this expression works is that obviously if the size is 31 bits,
51 -- it fits in 32 bits. In the 32-bit case, it fits in 32-bit signed if the
52 -- range has negative values. It is too conservative in the case that the
53 -- programmer has set a size greater than the default, e.g. a size of 33
54 -- for an integer type with a range of 1..10, but an over-conservative
55 -- result is OK. The important thing is that the value is only True if
56 -- we know the result will fit in 32-bits signed. If the value is False
57 -- when it could be True, the behavior will be correct, just a bit less
58 -- efficient than it could have been in some unusual cases.
60 -- One might assume that we could get a more accurate result by testing
61 -- the lower and upper bounds of the type Rst against the bounds of 32-bit
62 -- Integer. However, there is no easy way to do that. Why? Because in the
63 -- relatively rare case where this expression has to be evaluated at run
64 -- time rather than compile time (when the bounds are dynamic), we need a
65 -- type to use for the computation. But the possible range of upper bound
66 -- values for Rst (remembering the possibility of 64-bit modular types) is
67 -- from -2**63 to 2**64-1, and no run-time type has a big enough range.
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 function Square_Mod_N (X, N : Int) return Int;
74 pragma Inline (Square_Mod_N);
75 -- Computes X**2 mod N avoiding intermediate overflow
81 function Image (Of_State : State) return String is
83 return Int'Image (Of_State.X1) &
85 Int'Image (Of_State.X2) &
87 Int'Image (Of_State.Q);
94 function Random (Gen : Generator) return Rst is
95 S : State renames Gen.Writable.Self.Gen_State;
100 -- Check for flat range here, since we are typically run with checks
101 -- off, note that in practice, this condition will usually be static
102 -- so we will not actually generate any code for the normal case.
104 if Rst'Last < Rst'First then
105 raise Constraint_Error;
108 -- Continue with computation if non-flat range
110 S.X1 := Square_Mod_N (S.X1, S.P);
111 S.X2 := Square_Mod_N (S.X2, S.Q);
114 -- Following duplication is not an error, it is a loop unwinding
124 TF := Offs + (Flt (Temp) * Flt (S.P) + Flt (S.X1)) * S.Scl;
126 -- Pathological, but there do exist cases where the rounding implicit
127 -- in calculating the scale factor will cause rounding to 'Last + 1.
128 -- In those cases, returning 'First results in the least bias.
130 if TF >= Flt (Rst'Pos (Rst'Last)) + 0.5 then
133 elsif not Fits_In_32_Bits then
134 return Rst'Val (Interfaces.Integer_64 (TF));
137 return Rst'Val (Int (TF));
145 procedure Reset (Gen : Generator; Initiator : Integer) is
146 S : State renames Gen.Writable.Self.Gen_State;
150 X1 := 2 + Int (Initiator) mod (K1 - 3);
151 X2 := 2 + Int (Initiator) mod (K2 - 3);
154 X1 := Square_Mod_N (X1, K1);
155 X2 := Square_Mod_N (X2, K2);
158 -- Eliminate effects of small Initiators
173 procedure Reset (Gen : Generator) is
174 S : State renames Gen.Writable.Self.Gen_State;
175 Now : constant Calendar.Time := Calendar.Clock;
180 X1 := Int (Calendar.Year (Now)) * 12 * 31 +
181 Int (Calendar.Month (Now) * 31) +
182 Int (Calendar.Day (Now));
184 X2 := Int (Calendar.Seconds (Now) * Duration (1000.0));
186 X1 := 2 + X1 mod (K1 - 3);
187 X2 := 2 + X2 mod (K2 - 3);
189 -- Eliminate visible effects of same day starts
192 X1 := Square_Mod_N (X1, K1);
193 X2 := Square_Mod_N (X2, K2);
210 procedure Reset (Gen : Generator; From_State : State) is
212 Gen.Writable.Self.Gen_State := From_State;
219 procedure Save (Gen : Generator; To_State : out State) is
221 To_State := Gen.Gen_State;
228 function Square_Mod_N (X, N : Int) return Int is
230 return Int ((Integer_64 (X) ** 2) mod (Integer_64 (N)));
237 function Value (Coded_State : String) return State is
238 Last : constant Natural := Coded_State'Last;
239 Start : Positive := Coded_State'First;
240 Stop : Positive := Coded_State'First;
244 while Stop <= Last and then Coded_State (Stop) /= ',' loop
249 raise Constraint_Error;
252 Outs.X1 := Int'Value (Coded_State (Start .. Stop - 1));
257 exit when Stop > Last or else Coded_State (Stop) = ',';
261 raise Constraint_Error;
264 Outs.X2 := Int'Value (Coded_State (Start .. Stop - 1));
265 Outs.Q := Int'Value (Coded_State (Stop + 1 .. Last));
266 Outs.P := Outs.Q * 2 + 1;
267 Outs.FP := Flt (Outs.P);
268 Outs.Scl := (RstL - RstF + 1.0) / (Flt (Outs.P) * Flt (Outs.Q));
270 -- Now do *some* sanity checks
273 or else Outs.X1 not in 2 .. Outs.P - 1
274 or else Outs.X2 not in 2 .. Outs.Q - 1
276 raise Constraint_Error;
282 end GNAT.MBBS_Discrete_Random;