Debian Codegraph @

gcc/ada/libgnat/g-rannum.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . R A N D O M _ N U M B E R S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2007-2025, 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.Numerics.Long_Elementary_Functions;
33 use Ada.Numerics.Long_Elementary_Functions;
34 with Ada.Unchecked_Conversion;
35
36 with System.Random_Numbers; use System.Random_Numbers;
37
38 package body GNAT.Random_Numbers with
39 SPARK_Mode => Off
40 is
41 Sys_Max_Image_Width : constant := System.Random_Numbers.Max_Image_Width;
42
43 subtype Image_String is String (1 .. Max_Image_Width);
44
45 -- Utility function declarations
46
47 procedure Insert_Image
48 (S : in out Image_String;
49 Index : Integer;
50 V : Integer_64);
51 -- Insert string representation of V in S starting at position Index
52
53 ---------------
54 -- To_Signed --
55 ---------------
56
57 function To_Signed is
58 new Ada.Unchecked_Conversion (Unsigned_32, Integer_32);
59 function To_Signed is
60 new Ada.Unchecked_Conversion (Unsigned_64, Integer_64);
61 function To_Signed is
62 new Ada.Unchecked_Conversion (Unsigned_128, Integer_128);
63
64 ------------------
65 -- Insert_Image --
66 ------------------
67
68 procedure Insert_Image
69 (S : in out Image_String;
70 Index : Integer;
71 V : Integer_64)
72 is
73 Image : constant String := Integer_64'Image (V);
74 begin
75 S (Index .. Index + Image'Length - 1) := Image;
76 end Insert_Image;
77
78 ---------------------
79 -- Random_Discrete --
80 ---------------------
81
82 function Random_Discrete
83 (Gen : Generator;
84 Min : Result_Subtype := Default_Min;
85 Max : Result_Subtype := Result_Subtype'Last) return Result_Subtype
86 is
87 function F is
88 new System.Random_Numbers.Random_Discrete
89 (Result_Subtype, Default_Min);
90 begin
91 return F (Gen.Rep, Min, Max);
92 end Random_Discrete;
93
94 --------------------------
95 -- Random_Decimal_Fixed --
96 --------------------------
97
98 function Random_Decimal_Fixed
99 (Gen : Generator;
100 Min : Result_Subtype := Default_Min;
101 Max : Result_Subtype := Result_Subtype'Last) return Result_Subtype
102 is
103 begin
104 if Result_Subtype'Base'Size > 64 then
105 declare
106 subtype IntV is Integer_128 range
107 Integer_128'Integer_Value (Min) ..
108 Integer_128'Integer_Value (Max);
109 function R is new Random_Discrete (Integer_128, IntV'First);
110 begin
111 return Result_Subtype'Fixed_Value (R (Gen, IntV'First, IntV'Last));
112 end;
113
114 elsif Result_Subtype'Base'Size > 32 then
115 declare
116 subtype IntV is Integer_64 range
117 Integer_64'Integer_Value (Min) ..
118 Integer_64'Integer_Value (Max);
119 function R is new Random_Discrete (Integer_64, IntV'First);
120 begin
121 return Result_Subtype'Fixed_Value (R (Gen, IntV'First, IntV'Last));
122 end;
123
124 else
125 declare
126 subtype IntV is Integer_32 range
127 Integer_32'Integer_Value (Min) ..
128 Integer_32'Integer_Value (Max);
129 function R is new Random_Discrete (Integer_32, IntV'First);
130 begin
131 return Result_Subtype'Fixed_Value (R (Gen, IntV'First, IntV'Last));
132 end;
133 end if;
134 end Random_Decimal_Fixed;
135
136 ---------------------------
137 -- Random_Ordinary_Fixed --
138 ---------------------------
139
140 function Random_Ordinary_Fixed
141 (Gen : Generator;
142 Min : Result_Subtype := Default_Min;
143 Max : Result_Subtype := Result_Subtype'Last) return Result_Subtype
144 is
145 begin
146 if Result_Subtype'Base'Size > 64 then
147 declare
148 subtype IntV is Integer_128 range
149 Integer_128'Integer_Value (Min) ..
150 Integer_128'Integer_Value (Max);
151 function R is new Random_Discrete (Integer_128, IntV'First);
152 begin
153 return Result_Subtype'Fixed_Value (R (Gen, IntV'First, IntV'Last));
154 end;
155
156 elsif Result_Subtype'Base'Size > 32 then
157 declare
158 subtype IntV is Integer_64 range
159 Integer_64'Integer_Value (Min) ..
160 Integer_64'Integer_Value (Max);
161 function R is new Random_Discrete (Integer_64, IntV'First);
162 begin
163 return Result_Subtype'Fixed_Value (R (Gen, IntV'First, IntV'Last));
164 end;
165
166 else
167 declare
168 subtype IntV is Integer_32 range
169 Integer_32'Integer_Value (Min) ..
170 Integer_32'Integer_Value (Max);
171 function R is new Random_Discrete (Integer_32, IntV'First);
172 begin
173 return Result_Subtype'Fixed_Value (R (Gen, IntV'First, IntV'Last));
174 end;
175 end if;
176 end Random_Ordinary_Fixed;
177
178 ------------
179 -- Random --
180 ------------
181
182 function Random (Gen : Generator) return Float is
183 begin
184 return Random (Gen.Rep);
185 end Random;
186
187 function Random (Gen : Generator) return Long_Float is
188 begin
189 return Random (Gen.Rep);
190 end Random;
191
192 function Random (Gen : Generator) return Interfaces.Unsigned_32 is
193 begin
194 return Random (Gen.Rep);
195 end Random;
196
197 function Random (Gen : Generator) return Interfaces.Unsigned_64 is
198 begin
199 return Random (Gen.Rep);
200 end Random;
201
202 function Random (Gen : Generator) return Interfaces.Unsigned_128 is
203 begin
204 return Random (Gen.Rep);
205 end Random;
206
207 function Random (Gen : Generator) return Integer_32 is
208 begin
209 return To_Signed (Unsigned_32'(Random (Gen)));
210 end Random;
211
212 function Random (Gen : Generator) return Integer_64 is
213 begin
214 return To_Signed (Unsigned_64'(Random (Gen)));
215 end Random;
216
217 function Random (Gen : Generator) return Integer_128 is
218 begin
219 return To_Signed (Unsigned_128'(Random (Gen)));
220 end Random;
221
222 function Random (Gen : Generator) return Long_Integer is
223 function Random_Long_Integer is new Random_Discrete (Long_Integer);
224 begin
225 return Random_Long_Integer (Gen);
226 end Random;
227
228 function Random (Gen : Generator) return Integer is
229 function Random_Integer is new Random_Discrete (Integer);
230 begin
231 return Random_Integer (Gen);
232 end Random;
233
234 ------------------
235 -- Random_Float --
236 ------------------
237
238 function Random_Float (Gen : Generator) return Result_Subtype is
239 function F is new System.Random_Numbers.Random_Float (Result_Subtype);
240 begin
241 return F (Gen.Rep);
242 end Random_Float;
243
244 ---------------------
245 -- Random_Gaussian --
246 ---------------------
247
248 -- Generates pairs of normally distributed values using the polar method of
249 -- G. E. P. Box, M. E. Muller, and G. Marsaglia. See Donald E. Knuth, The
250 -- Art of Computer Programming, Vol 2: Seminumerical Algorithms, section
251 -- 3.4.1, subsection C, algorithm P. Returns half of the pair on each call,
252 -- using the Next_Gaussian field of Gen to hold the second member on
253 -- even-numbered calls.
254
255 function Random_Gaussian (Gen : Generator) return Long_Float is
256 G : Generator renames Gen'Unrestricted_Access.all;
257
258 V1, V2, Rad2, Mult : Long_Float;
259
260 begin
261 if G.Have_Gaussian then
262 G.Have_Gaussian := False;
263 return G.Next_Gaussian;
264
265 else
266 loop
267 V1 := 2.0 * Random (G) - 1.0;
268 V2 := 2.0 * Random (G) - 1.0;
269 Rad2 := V1 ** 2 + V2 ** 2;
270 exit when Rad2 < 1.0 and then Rad2 /= 0.0;
271 end loop;
272
273 -- Now V1 and V2 are coordinates in the unit circle
274
275 Mult := Sqrt (-2.0 * Log (Rad2) / Rad2);
276 G.Next_Gaussian := V2 * Mult;
277 G.Have_Gaussian := True;
278 return Long_Float'Machine (V1 * Mult);
279 end if;
280 end Random_Gaussian;
281
282 function Random_Gaussian (Gen : Generator) return Float is
283 V : constant Long_Float := Random_Gaussian (Gen);
284 begin
285 return Float'Machine (Float (V));
286 end Random_Gaussian;
287
288 -----------
289 -- Reset --
290 -----------
291
292 procedure Reset (Gen : out Generator) is
293 begin
294 Reset (Gen.Rep);
295 Gen.Have_Gaussian := False;
296 end Reset;
297
298 procedure Reset
299 (Gen : out Generator;
300 Initiator : Initialization_Vector)
301 is
302 begin
303 Reset (Gen.Rep, Initiator);
304 Gen.Have_Gaussian := False;
305 end Reset;
306
307 procedure Reset
308 (Gen : out Generator;
309 Initiator : Interfaces.Integer_32)
310 is
311 begin
312 Reset (Gen.Rep, Initiator);
313 Gen.Have_Gaussian := False;
314 end Reset;
315
316 procedure Reset
317 (Gen : out Generator;
318 Initiator : Interfaces.Unsigned_32)
319 is
320 begin
321 Reset (Gen.Rep, Initiator);
322 Gen.Have_Gaussian := False;
323 end Reset;
324
325 procedure Reset
326 (Gen : out Generator;
327 Initiator : Integer)
328 is
329 begin
330 Reset (Gen.Rep, Initiator);
331 Gen.Have_Gaussian := False;
332 end Reset;
333
334 procedure Reset
335 (Gen : out Generator;
336 From_State : Generator)
337 is
338 begin
339 Reset (Gen.Rep, From_State.Rep);
340 Gen.Have_Gaussian := From_State.Have_Gaussian;
341 Gen.Next_Gaussian := From_State.Next_Gaussian;
342 end Reset;
343
344 Frac_Scale : constant Long_Float :=
345 Long_Float
346 (Long_Float'Machine_Radix) ** Long_Float'Machine_Mantissa;
347
348 function Val64 (Image : String) return Integer_64;
349 -- Renames Integer64'Value
350 -- We cannot use a 'renames Integer64'Value' since for some strange
351 -- reason, this requires a dependency on s-auxdec.ads which not all
352 -- run-times support ???
353
354 function Val64 (Image : String) return Integer_64 is
355 begin
356 return Integer_64'Value (Image);
357 end Val64;
358
359 procedure Reset
360 (Gen : out Generator;
361 From_Image : String)
362 is
363 F0 : constant Integer := From_Image'First;
364 T0 : constant Integer := From_Image'First + Sys_Max_Image_Width;
365
366 begin
367 Reset (Gen.Rep, From_Image (F0 .. F0 + Sys_Max_Image_Width));
368
369 if From_Image (T0 + 1) = '1' then
370 Gen.Have_Gaussian := True;
371 Gen.Next_Gaussian :=
372 Long_Float (Val64 (From_Image (T0 + 3 .. T0 + 23))) / Frac_Scale
373 * Long_Float (Long_Float'Machine_Radix)
374 ** Integer (Val64 (From_Image (T0 + 25 .. From_Image'Last)));
375 else
376 Gen.Have_Gaussian := False;
377 end if;
378 end Reset;
379
380 -----------
381 -- Image --
382 -----------
383
384 function Image (Gen : Generator) return String is
385 Result : Image_String;
386
387 begin
388 Result := [others => ' '];
389 Result (1 .. Sys_Max_Image_Width) := Image (Gen.Rep);
390
391 if Gen.Have_Gaussian then
392 Result (Sys_Max_Image_Width + 2) := '1';
393 Insert_Image (Result, Sys_Max_Image_Width + 4,
394 Integer_64 (Long_Float'Fraction (Gen.Next_Gaussian)
395 * Frac_Scale));
396 Insert_Image (Result, Sys_Max_Image_Width + 24,
397 Integer_64 (Long_Float'Exponent (Gen.Next_Gaussian)));
398
399 else
400 Result (Sys_Max_Image_Width + 2) := '0';
401 end if;
402
403 return Result;
404 end Image;
405
406 end GNAT.Random_Numbers;