1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--               S Y S T E M . G E N E R I C _ B I G N U M S                --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--            Copyright (C) 2012-2019, 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--  This package provides arbitrary precision signed integer arithmetic
33--  and can be used either built into the compiler via System.Bignums or to
34--  implement a default version of Ada.Numerics.Big_Numbers.Big_Integers.
35
36--  If Use_Secondary_Stack is True then all Bignum values are allocated on the
37--  secondary stack. If False, the heap is used and the caller is responsible
38--  for memory management.
39
40with Ada.Unchecked_Conversion;
41with Interfaces;
42
43generic
44   Use_Secondary_Stack : Boolean;
45package System.Generic_Bignums is
46   pragma Preelaborate;
47
48   pragma Assert (Long_Long_Integer'Size = 64);
49   --  This package assumes that Long_Long_Integer size is 64 bit (i.e. that it
50   --  has a range of -2**63 to 2**63-1). The front end ensures that the mode
51   --  ELIMINATED is not allowed for overflow checking if this is not the case.
52
53   subtype Length is Natural range 0 .. 2 ** 23 - 1;
54   --  Represent number of words in Digit_Vector
55
56   Base : constant := 2 ** 32;
57   --  Digit vectors use this base
58
59   subtype SD is Interfaces.Unsigned_32;
60   --  Single length digit
61
62   type Digit_Vector is array (Length range <>) of SD;
63   --  Represent digits of a number (most significant digit first)
64
65   type Bignum_Data (Len : Length) is record
66      Neg : Boolean;
67      --  Set if value is negative, never set for zero
68
69      D : Digit_Vector (1 .. Len);
70      --  Digits of number, most significant first, represented in base
71      --  2**Base. No leading zeroes are stored, and the value of zero is
72      --  represented using an empty vector for D.
73   end record;
74
75   for Bignum_Data use record
76      Len at 0 range 0 .. 23;
77      Neg at 3 range 0 .. 7;
78   end record;
79
80   type Bignum is access all Bignum_Data;
81   --  This is the type that is used externally. Possibly this could be a
82   --  private type, but we leave the structure exposed for now. For one
83   --  thing it helps with debugging. Note that this package never shares
84   --  an allocated Bignum value, so for example for X + 0, a copy of X is
85   --  returned, not X itself.
86
87   function To_Bignum is new Ada.Unchecked_Conversion (System.Address, Bignum);
88   function To_Address is new
89     Ada.Unchecked_Conversion (Bignum, System.Address);
90
91   --  Note: none of the subprograms in this package modify the Bignum_Data
92   --  records referenced by Bignum arguments of mode IN.
93
94   function Big_Add (X, Y : Bignum) return Bignum;  --  "+"
95   function Big_Sub (X, Y : Bignum) return Bignum;  --  "-"
96   function Big_Mul (X, Y : Bignum) return Bignum;  --  "*"
97   function Big_Div (X, Y : Bignum) return Bignum;  --  "/"
98   function Big_Exp (X, Y : Bignum) return Bignum;  --  "**"
99   function Big_Mod (X, Y : Bignum) return Bignum;  --  "mod"
100   function Big_Rem (X, Y : Bignum) return Bignum;  --  "rem"
101   function Big_Neg (X    : Bignum) return Bignum;  --  "-"
102   function Big_Abs (X    : Bignum) return Bignum;  --  "abs"
103   --  Perform indicated arithmetic operation on bignum values. No exception
104   --  raised except for Div/Mod/Rem by 0 which raises Constraint_Error with
105   --  an appropriate message.
106
107   function Big_EQ  (X, Y : Bignum) return Boolean;  -- "="
108   function Big_NE  (X, Y : Bignum) return Boolean;  -- "/="
109   function Big_GE  (X, Y : Bignum) return Boolean;  -- ">="
110   function Big_LE  (X, Y : Bignum) return Boolean;  -- "<="
111   function Big_GT  (X, Y : Bignum) return Boolean;  --  ">"
112   function Big_LT  (X, Y : Bignum) return Boolean;  --  "<"
113   --  Perform indicated comparison on bignums, returning result as Boolean.
114   --  No exception raised for any input arguments.
115
116   function Bignum_In_LLI_Range (X : Bignum) return Boolean;
117   --  Returns True if the Bignum value is in the range of Long_Long_Integer,
118   --  so that a call to From_Bignum is guaranteed not to raise an exception.
119
120   function To_Bignum (X : Long_Long_Integer) return Bignum;
121   --  Convert Long_Long_Integer to Bignum. No exception can be raised for any
122   --  input argument.
123
124   function To_Bignum (X : Interfaces.Unsigned_64) return Bignum;
125   --  Convert Unsigned_64 to Bignum. No exception can be raised for any
126   --  input argument.
127
128   function From_Bignum (X : Bignum) return Long_Long_Integer;
129   --  Convert Bignum to Long_Long_Integer. Constraint_Error raised with
130   --  appropriate message if value is out of range of Long_Long_Integer.
131
132   function Is_Zero (X : Bignum) return Boolean;
133   --  Return True if X = 0
134
135end System.Generic_Bignums;
136