Debian Codegraph @

gcc/ada/libgnat/a-nbnbin.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- ADA.NUMERICS.BIG_NUMBERS.BIG_INTEGERS --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2019-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.Unchecked_Deallocation;
33
34 with Interfaces; use Interfaces;
35
36 with System.Generic_Bignums;
37 with System.Shared_Bignums; use System.Shared_Bignums;
38
39 package body Ada.Numerics.Big_Numbers.Big_Integers is
40
41 function Allocate_Bignum (D : Digit_Vector; Neg : Boolean) return Bignum;
42 -- Allocate Bignum value with the given contents
43
44 procedure Free_Bignum (X : in out Bignum);
45 -- Free memory associated with X
46
47 function To_Bignum (X : aliased in out Bignum) return Bignum is (X);
48
49 procedure Free is new Ada.Unchecked_Deallocation (Bignum_Data, Bignum);
50
51 ---------------------
52 -- Allocate_Bignum --
53 ---------------------
54
55 function Allocate_Bignum (D : Digit_Vector; Neg : Boolean) return Bignum is
56 begin
57 return new Bignum_Data'(D'Length, Neg, D);
58 end Allocate_Bignum;
59
60 -----------------
61 -- Free_Bignum --
62 -----------------
63
64 procedure Free_Bignum (X : in out Bignum) is
65 begin
66 Free (X);
67 end Free_Bignum;
68
69 package Bignums is new System.Generic_Bignums
70 (Bignum, Allocate_Bignum, Free_Bignum, To_Bignum);
71
72 use System, Bignums;
73 subtype Bignum is Bignums.Bignum;
74
75 function Get_Bignum (Arg : Big_Integer) return Bignum is
76 (if Arg.Value.C = System.Null_Address
77 then raise Constraint_Error with "invalid big integer"
78 else To_Bignum (Arg.Value.C));
79 -- Check for validity of Arg and return the Bignum value stored in Arg.
80 -- Raise Constraint_Error if Arg is uninitialized.
81
82 procedure Set_Bignum (Arg : out Big_Integer; Value : Bignum)
83 with Inline;
84 -- Set the Bignum value stored in Arg to Value
85
86 ----------------
87 -- Set_Bignum --
88 ----------------
89
90 procedure Set_Bignum (Arg : out Big_Integer; Value : Bignum) is
91 begin
92 Arg.Value.C := To_Address (Value);
93 end Set_Bignum;
94
95 --------------
96 -- Is_Valid --
97 --------------
98
99 function Is_Valid (Arg : Big_Integer) return Boolean is
100 (Arg.Value.C /= System.Null_Address);
101
102 ---------
103 -- "=" --
104 ---------
105
106 function "=" (L, R : Valid_Big_Integer) return Boolean is
107 begin
108 return Big_EQ (Get_Bignum (L), Get_Bignum (R));
109 end "=";
110
111 ---------
112 -- "<" --
113 ---------
114
115 function "<" (L, R : Valid_Big_Integer) return Boolean is
116 begin
117 return Big_LT (Get_Bignum (L), Get_Bignum (R));
118 end "<";
119
120 ----------
121 -- "<=" --
122 ----------
123
124 function "<=" (L, R : Valid_Big_Integer) return Boolean is
125 begin
126 return Big_LE (Get_Bignum (L), Get_Bignum (R));
127 end "<=";
128
129 ---------
130 -- ">" --
131 ---------
132
133 function ">" (L, R : Valid_Big_Integer) return Boolean is
134 begin
135 return Big_GT (Get_Bignum (L), Get_Bignum (R));
136 end ">";
137
138 ----------
139 -- ">=" --
140 ----------
141
142 function ">=" (L, R : Valid_Big_Integer) return Boolean is
143 begin
144 return Big_GE (Get_Bignum (L), Get_Bignum (R));
145 end ">=";
146
147 --------------------
148 -- To_Big_Integer --
149 --------------------
150
151 function To_Big_Integer (Arg : Integer) return Valid_Big_Integer is
152 Result : Big_Integer;
153 begin
154 Set_Bignum (Result, To_Bignum (Long_Long_Integer (Arg)));
155 return Result;
156 end To_Big_Integer;
157
158 ----------------
159 -- To_Integer --
160 ----------------
161
162 function To_Integer (Arg : Valid_Big_Integer) return Integer is
163 begin
164 return Integer (Long_Long_Integer'(From_Bignum (Get_Bignum (Arg))));
165 end To_Integer;
166
167 ------------------------
168 -- Signed_Conversions --
169 ------------------------
170
171 package body Signed_Conversions is
172
173 --------------------
174 -- To_Big_Integer --
175 --------------------
176
177 function To_Big_Integer (Arg : Int) return Valid_Big_Integer is
178 Result : Big_Integer;
179 begin
180 Set_Bignum (Result, To_Bignum (Long_Long_Long_Integer (Arg)));
181 return Result;
182 end To_Big_Integer;
183
184 ----------------------
185 -- From_Big_Integer --
186 ----------------------
187
188 function From_Big_Integer (Arg : Valid_Big_Integer) return Int is
189 begin
190 return Int (Long_Long_Long_Integer'(From_Bignum (Get_Bignum (Arg))));
191 end From_Big_Integer;
192
193 end Signed_Conversions;
194
195 --------------------------
196 -- Unsigned_Conversions --
197 --------------------------
198
199 package body Unsigned_Conversions is
200
201 --------------------
202 -- To_Big_Integer --
203 --------------------
204
205 function To_Big_Integer (Arg : Int) return Valid_Big_Integer is
206 Result : Big_Integer;
207 begin
208 Set_Bignum (Result, To_Bignum (Unsigned_128 (Arg)));
209 return Result;
210 end To_Big_Integer;
211
212 ----------------------
213 -- From_Big_Integer --
214 ----------------------
215
216 function From_Big_Integer (Arg : Valid_Big_Integer) return Int is
217 begin
218 return Int (Unsigned_128'(From_Bignum (Get_Bignum (Arg))));
219 end From_Big_Integer;
220
221 end Unsigned_Conversions;
222
223 ---------------
224 -- To_String --
225 ---------------
226
227 function To_String
228 (Arg : Valid_Big_Integer; Width : Field := 0; Base : Number_Base := 10)
229 return String is
230 begin
231 return To_String (Get_Bignum (Arg), Natural (Width), Positive (Base));
232 end To_String;
233
234 -----------------
235 -- From_String --
236 -----------------
237
238 function From_String (Arg : String) return Valid_Big_Integer is
239 procedure Scan_Decimal
240 (Arg : String; J : in out Natural; Result : out Big_Integer);
241 -- Scan decimal value starting at Arg (J). Store value in Result if
242 -- successful, raise Constraint_Error if not. On exit, J points to the
243 -- first index past the decimal value.
244
245 ------------------
246 -- Scan_Decimal --
247 ------------------
248
249 procedure Scan_Decimal
250 (Arg : String; J : in out Natural; Result : out Big_Integer)
251 is
252 Initial_J : constant Natural := J;
253 Ten : constant Big_Integer := To_Big_Integer (10);
254 begin
255 Result := To_Big_Integer (0);
256
257 while J <= Arg'Last loop
258 if Arg (J) in '0' .. '9' then
259 Result :=
260 Result * Ten + To_Big_Integer (Character'Pos (Arg (J))
261 - Character'Pos ('0'));
262
263 elsif Arg (J) = '_' then
264 if J in Initial_J | Arg'Last
265 or else Arg (J - 1) not in '0' .. '9'
266 or else Arg (J + 1) not in '0' .. '9'
267 then
268 raise Constraint_Error with "invalid integer value: " & Arg;
269 end if;
270 else
271 exit;
272 end if;
273
274 J := J + 1;
275 end loop;
276 end Scan_Decimal;
277
278 Result : Big_Integer;
279
280 begin
281 -- First try the fast path via Long_Long_Long_Integer'Value
282
283 Set_Bignum (Result, To_Bignum (Long_Long_Long_Integer'Value (Arg)));
284 return Result;
285
286 exception
287 when Constraint_Error =>
288 -- Then try the slow path
289
290 declare
291 Neg : Boolean := False;
292 Base_Found : Boolean := False;
293 Base_Int : Positive := 10;
294 J : Natural := Arg'First;
295 Val : Natural;
296 Base : Big_Integer;
297 Exp : Big_Integer;
298
299 begin
300 -- Scan past leading blanks
301
302 while J <= Arg'Last and then Arg (J) = ' ' loop
303 J := J + 1;
304 end loop;
305
306 if J > Arg'Last then
307 raise;
308 end if;
309
310 -- Scan and store negative sign if found
311
312 if Arg (J) = '-' then
313 Neg := True;
314 J := J + 1;
315 end if;
316
317 -- Scan decimal value: either the result itself, or the base
318 -- value if followed by a '#'.
319
320 Scan_Decimal (Arg, J, Result);
321
322 -- Scan explicit base if requested
323
324 if J <= Arg'Last and then Arg (J) = '#' then
325 Base_Int := To_Integer (Result);
326
327 if Base_Int not in 2 .. 16 then
328 raise;
329 end if;
330
331 Base_Found := True;
332 Base := Result;
333 Result := To_Big_Integer (0);
334 J := J + 1;
335
336 while J <= Arg'Last loop
337 case Arg (J) is
338 when '0' .. '9' =>
339 Val := Character'Pos (Arg (J)) - Character'Pos ('0');
340
341 if Val >= Base_Int then
342 raise;
343 end if;
344
345 Result := Result * Base + To_Big_Integer (Val);
346
347 when 'a' .. 'f' =>
348 Val :=
349 10 + Character'Pos (Arg (J)) - Character'Pos ('a');
350
351 if Val >= Base_Int then
352 raise;
353 end if;
354
355 Result := Result * Base + To_Big_Integer (Val);
356
357 when 'A' .. 'F' =>
358 Val :=
359 10 + Character'Pos (Arg (J)) - Character'Pos ('A');
360
361 if Val >= Base_Int then
362 raise;
363 end if;
364
365 Result := Result * Base + To_Big_Integer (Val);
366
367 when '_' =>
368
369 -- We only allow _ preceded and followed by a valid
370 -- number and not any other character.
371
372 if J in Arg'First | Arg'Last
373 or else Arg (J - 1) in '_' | '#'
374 or else Arg (J + 1) = '#'
375 then
376 raise;
377 end if;
378
379 when '#' =>
380 J := J + 1;
381 exit;
382
383 when others =>
384 raise;
385 end case;
386
387 J := J + 1;
388 end loop;
389 else
390 Base := To_Big_Integer (10);
391 end if;
392
393 if Base_Found and then Arg (J - 1) /= '#' then
394 raise;
395 end if;
396
397 if J <= Arg'Last then
398
399 -- Scan exponent
400
401 if Arg (J) in 'e' | 'E' then
402 J := J + 1;
403
404 if Arg (J) = '+' then
405 J := J + 1;
406 end if;
407
408 Scan_Decimal (Arg, J, Exp);
409 Result := Result * (Base ** To_Integer (Exp));
410 end if;
411
412 -- Scan past trailing blanks
413
414 while J <= Arg'Last and then Arg (J) = ' ' loop
415 J := J + 1;
416 end loop;
417
418 if J <= Arg'Last then
419 raise;
420 end if;
421 end if;
422
423 if Neg then
424 return -Result;
425 else
426 return Result;
427 end if;
428 end;
429 end From_String;
430
431 ---------------
432 -- Put_Image --
433 ---------------
434
435 procedure Put_Image (S : in out Root_Buffer_Type'Class; V : Big_Integer) is
436 -- This is implemented in terms of To_String. It might be more elegant
437 -- and more efficient to do it the other way around, but this is the
438 -- most expedient implementation for now.
439 begin
440 Strings.Text_Buffers.Put_UTF_8 (S, To_String (V));
441 end Put_Image;
442
443 ---------
444 -- "+" --
445 ---------
446
447 function "+" (L : Valid_Big_Integer) return Valid_Big_Integer is
448 Result : Big_Integer;
449 begin
450 Set_Bignum (Result, new Bignum_Data'(Get_Bignum (L).all));
451 return Result;
452 end "+";
453
454 ---------
455 -- "-" --
456 ---------
457
458 function "-" (L : Valid_Big_Integer) return Valid_Big_Integer is
459 Result : Big_Integer;
460 begin
461 Set_Bignum (Result, Big_Neg (Get_Bignum (L)));
462 return Result;
463 end "-";
464
465 -----------
466 -- "abs" --
467 -----------
468
469 function "abs" (L : Valid_Big_Integer) return Valid_Big_Integer is
470 Result : Big_Integer;
471 begin
472 Set_Bignum (Result, Big_Abs (Get_Bignum (L)));
473 return Result;
474 end "abs";
475
476 ---------
477 -- "+" --
478 ---------
479
480 function "+" (L, R : Valid_Big_Integer) return Valid_Big_Integer is
481 Result : Big_Integer;
482 begin
483 Set_Bignum (Result, Big_Add (Get_Bignum (L), Get_Bignum (R)));
484 return Result;
485 end "+";
486
487 ---------
488 -- "-" --
489 ---------
490
491 function "-" (L, R : Valid_Big_Integer) return Valid_Big_Integer is
492 Result : Big_Integer;
493 begin
494 Set_Bignum (Result, Big_Sub (Get_Bignum (L), Get_Bignum (R)));
495 return Result;
496 end "-";
497
498 ---------
499 -- "*" --
500 ---------
501
502 function "*" (L, R : Valid_Big_Integer) return Valid_Big_Integer is
503 Result : Big_Integer;
504 begin
505 Set_Bignum (Result, Big_Mul (Get_Bignum (L), Get_Bignum (R)));
506 return Result;
507 end "*";
508
509 ---------
510 -- "/" --
511 ---------
512
513 function "/" (L, R : Valid_Big_Integer) return Valid_Big_Integer is
514 Result : Big_Integer;
515 begin
516 Set_Bignum (Result, Big_Div (Get_Bignum (L), Get_Bignum (R)));
517 return Result;
518 end "/";
519
520 -----------
521 -- "mod" --
522 -----------
523
524 function "mod" (L, R : Valid_Big_Integer) return Valid_Big_Integer is
525 Result : Big_Integer;
526 begin
527 Set_Bignum (Result, Big_Mod (Get_Bignum (L), Get_Bignum (R)));
528 return Result;
529 end "mod";
530
531 -----------
532 -- "rem" --
533 -----------
534
535 function "rem" (L, R : Valid_Big_Integer) return Valid_Big_Integer is
536 Result : Big_Integer;
537 begin
538 Set_Bignum (Result, Big_Rem (Get_Bignum (L), Get_Bignum (R)));
539 return Result;
540 end "rem";
541
542 ----------
543 -- "**" --
544 ----------
545
546 function "**"
547 (L : Valid_Big_Integer; R : Natural) return Valid_Big_Integer is
548 begin
549 declare
550 Exp : Bignum := To_Bignum (Long_Long_Integer (R));
551 Result : Big_Integer;
552 begin
553 Set_Bignum (Result, Big_Exp (Get_Bignum (L), Exp));
554 Free (Exp);
555 return Result;
556 end;
557 end "**";
558
559 ---------
560 -- Min --
561 ---------
562
563 function Min (L, R : Valid_Big_Integer) return Valid_Big_Integer is
564 (if L < R then L else R);
565
566 ---------
567 -- Max --
568 ---------
569
570 function Max (L, R : Valid_Big_Integer) return Valid_Big_Integer is
571 (if L > R then L else R);
572
573 -----------------------------
574 -- Greatest_Common_Divisor --
575 -----------------------------
576
577 function Greatest_Common_Divisor
578 (L, R : Valid_Big_Integer) return Big_Positive
579 is
580 function GCD (A, B : Big_Integer) return Big_Integer;
581 -- Recursive internal version
582
583 ---------
584 -- GCD --
585 ---------
586
587 function GCD (A, B : Big_Integer) return Big_Integer is
588 begin
589 if Is_Zero (Get_Bignum (B)) then
590 return A;
591 else
592 return GCD (B, A rem B);
593 end if;
594 end GCD;
595
596 begin
597 return GCD (abs L, abs R);
598 end Greatest_Common_Divisor;
599
600 ------------
601 -- Adjust --
602 ------------
603
604 procedure Adjust (This : in out Controlled_Bignum) is
605 begin
606 if This.C /= System.Null_Address then
607 This.C := To_Address (new Bignum_Data'(To_Bignum (This.C).all));
608 end if;
609 end Adjust;
610
611 --------------
612 -- Finalize --
613 --------------
614
615 procedure Finalize (This : in out Controlled_Bignum) is
616 Tmp : Bignum := To_Bignum (This.C);
617 begin
618 Free (Tmp);
619 This.C := System.Null_Address;
620 end Finalize;
621
622 end Ada.Numerics.Big_Numbers.Big_Integers;