1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . B I G N U M S --
9 -- Copyright (C) 2012-2018, 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 ------------------------------------------------------------------------------
32 -- This package provides arbitrary precision signed integer arithmetic for
33 -- use in computing intermediate values in expressions for the case where
34 -- pragma Overflow_Check (Eliminated) is in effect.
38 package System
.Bignums
is
40 pragma Assert
(Long_Long_Integer'Size = 64);
41 -- This package assumes that Long_Long_Integer size is 64 bit (i.e. that it
42 -- has a range of -2**63 to 2**63-1). The front end ensures that the mode
43 -- ELIMINATED is not allowed for overflow checking if this is not the case.
45 subtype Length
is Natural range 0 .. 2 ** 23 - 1;
46 -- Represent number of words in Digit_Vector
48 Base
: constant := 2 ** 32;
49 -- Digit vectors use this base
51 subtype SD
is Interfaces
.Unsigned_32
;
52 -- Single length digit
54 type Digit_Vector
is array (Length
range <>) of SD
;
55 -- Represent digits of a number (most significant digit first)
57 type Bignum_Data
(Len
: Length
) is record
59 -- Set if value is negative, never set for zero
61 D
: Digit_Vector
(1 .. Len
);
62 -- Digits of number, most significant first, represented in base
63 -- 2**Base. No leading zeroes are stored, and the value of zero is
64 -- represented using an empty vector for D.
67 for Bignum_Data
use record
68 Len
at 0 range 0 .. 23;
69 Neg
at 3 range 0 .. 7;
72 type Bignum
is access all Bignum_Data
;
73 -- This is the type that is used externally. Possibly this could be a
74 -- private type, but we leave the structure exposed for now. For one
75 -- thing it helps with debugging. Note that this package never shares
76 -- an allocated Bignum value, so for example for X + 0, a copy of X is
77 -- returned, not X itself.
79 -- Note: none of the subprograms in this package modify the Bignum_Data
80 -- records referenced by Bignum arguments of mode IN.
82 function Big_Add
(X
, Y
: Bignum
) return Bignum
; -- "+"
83 function Big_Sub
(X
, Y
: Bignum
) return Bignum
; -- "-"
84 function Big_Mul
(X
, Y
: Bignum
) return Bignum
; -- "*"
85 function Big_Div
(X
, Y
: Bignum
) return Bignum
; -- "/"
86 function Big_Exp
(X
, Y
: Bignum
) return Bignum
; -- "**"
87 function Big_Mod
(X
, Y
: Bignum
) return Bignum
; -- "mod"
88 function Big_Rem
(X
, Y
: Bignum
) return Bignum
; -- "rem"
89 function Big_Neg
(X
: Bignum
) return Bignum
; -- "-"
90 function Big_Abs
(X
: Bignum
) return Bignum
; -- "abs"
91 -- Perform indicated arithmetic operation on bignum values. No exception
92 -- raised except for Div/Mod/Rem by 0 which raises Constraint_Error with
93 -- an appropriate message.
95 function Big_EQ
(X
, Y
: Bignum
) return Boolean; -- "="
96 function Big_NE
(X
, Y
: Bignum
) return Boolean; -- "/="
97 function Big_GE
(X
, Y
: Bignum
) return Boolean; -- ">="
98 function Big_LE
(X
, Y
: Bignum
) return Boolean; -- "<="
99 function Big_GT
(X
, Y
: Bignum
) return Boolean; -- ">"
100 function Big_LT
(X
, Y
: Bignum
) return Boolean; -- "<"
101 -- Perform indicated comparison on bignums, returning result as Boolean.
102 -- No exception raised for any input arguments.
104 function Bignum_In_LLI_Range
(X
: Bignum
) return Boolean;
105 -- Returns True if the Bignum value is in the range of Long_Long_Integer,
106 -- so that a call to From_Bignum is guaranteed not to raise an exception.
108 function To_Bignum
(X
: Long_Long_Integer) return Bignum
;
109 -- Convert Long_Long_Integer to Bignum. No exception can be raised for any
112 function From_Bignum
(X
: Bignum
) return Long_Long_Integer;
113 -- Convert Bignum to Long_Long_Integer. Constraint_Error raised with
114 -- appropriate message if value is out of range of Long_Long_Integer.