| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- ADA.NUMERICS.BIG_NUMBERS.BIG_INTEGERS -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- This specification is derived from the Ada Reference Manual for use with -- |
| 10 |
-- GNAT. In accordance with the copyright of that document, you can freely -- |
| 11 |
-- copy and modify this specification, provided that if you redistribute a -- |
| 12 |
-- modified version, any changes that you have made are clearly indicated. -- |
| 13 |
-- -- |
| 14 |
------------------------------------------------------------------------------ |
| 15 |
|
| 16 |
with Ada.Strings.Text_Buffers; use Ada.Strings.Text_Buffers; |
| 17 |
|
| 18 |
private with Ada.Finalization; |
| 19 |
private with System; |
| 20 |
|
| 21 |
package Ada.Numerics.Big_Numbers.Big_Integers with |
| 22 |
Preelaborate, |
| 23 |
Always_Terminates |
| 24 |
is |
| 25 |
|
| 26 |
type Big_Integer is private |
| 27 |
with Integer_Literal => From_Universal_Image, |
| 28 |
Put_Image => Put_Image; |
| 29 |
|
| 30 |
function Is_Valid (Arg : Big_Integer) return Boolean |
| 31 |
with |
| 32 |
Convention => Intrinsic, |
| 33 |
Global => null; |
| 34 |
|
| 35 |
subtype Valid_Big_Integer is Big_Integer |
| 36 |
with Dynamic_Predicate => Is_Valid (Valid_Big_Integer), |
| 37 |
Predicate_Failure => raise Program_Error; |
| 38 |
|
| 39 |
function "=" (L, R : Valid_Big_Integer) return Boolean with Global => null; |
| 40 |
|
| 41 |
function "<" (L, R : Valid_Big_Integer) return Boolean with Global => null; |
| 42 |
|
| 43 |
function "<=" (L, R : Valid_Big_Integer) return Boolean with Global => null; |
| 44 |
|
| 45 |
function ">" (L, R : Valid_Big_Integer) return Boolean with Global => null; |
| 46 |
|
| 47 |
function ">=" (L, R : Valid_Big_Integer) return Boolean with Global => null; |
| 48 |
|
| 49 |
function To_Big_Integer (Arg : Integer) return Valid_Big_Integer |
| 50 |
with Global => null; |
| 51 |
|
| 52 |
subtype Big_Positive is Big_Integer |
| 53 |
with Dynamic_Predicate => |
| 54 |
(if Is_Valid (Big_Positive) |
| 55 |
then Big_Positive > To_Big_Integer (0)), |
| 56 |
Predicate_Failure => (raise Constraint_Error); |
| 57 |
|
| 58 |
subtype Big_Natural is Big_Integer |
| 59 |
with Dynamic_Predicate => |
| 60 |
(if Is_Valid (Big_Natural) |
| 61 |
then Big_Natural >= To_Big_Integer (0)), |
| 62 |
Predicate_Failure => (raise Constraint_Error); |
| 63 |
|
| 64 |
function In_Range |
| 65 |
(Arg : Valid_Big_Integer; Low, High : Big_Integer) return Boolean |
| 66 |
is (Low <= Arg and Arg <= High) |
| 67 |
with |
| 68 |
Global => null; |
| 69 |
|
| 70 |
function To_Integer (Arg : Valid_Big_Integer) return Integer |
| 71 |
with |
| 72 |
Pre => In_Range (Arg, |
| 73 |
Low => To_Big_Integer (Integer'First), |
| 74 |
High => To_Big_Integer (Integer'Last)) |
| 75 |
or else (raise Constraint_Error), |
| 76 |
Global => null; |
| 77 |
|
| 78 |
generic |
| 79 |
type Int is range <>; |
| 80 |
package Signed_Conversions is |
| 81 |
|
| 82 |
function To_Big_Integer (Arg : Int) return Valid_Big_Integer |
| 83 |
with Global => null; |
| 84 |
|
| 85 |
function From_Big_Integer (Arg : Valid_Big_Integer) return Int |
| 86 |
with |
| 87 |
Pre => In_Range (Arg, |
| 88 |
Low => To_Big_Integer (Int'First), |
| 89 |
High => To_Big_Integer (Int'Last)) |
| 90 |
or else (raise Constraint_Error), |
| 91 |
Global => null; |
| 92 |
end Signed_Conversions; |
| 93 |
|
| 94 |
generic |
| 95 |
type Int is mod <>; |
| 96 |
package Unsigned_Conversions is |
| 97 |
|
| 98 |
function To_Big_Integer (Arg : Int) return Valid_Big_Integer |
| 99 |
with Global => null; |
| 100 |
|
| 101 |
function From_Big_Integer (Arg : Valid_Big_Integer) return Int |
| 102 |
with |
| 103 |
Pre => In_Range (Arg, |
| 104 |
Low => To_Big_Integer (Int'First), |
| 105 |
High => To_Big_Integer (Int'Last)) |
| 106 |
or else (raise Constraint_Error), |
| 107 |
Global => null; |
| 108 |
|
| 109 |
end Unsigned_Conversions; |
| 110 |
|
| 111 |
function To_String (Arg : Valid_Big_Integer; |
| 112 |
Width : Field := 0; |
| 113 |
Base : Number_Base := 10) return String |
| 114 |
with |
| 115 |
Post => To_String'Result'First = 1, |
| 116 |
Global => null; |
| 117 |
|
| 118 |
function From_String (Arg : String) return Valid_Big_Integer |
| 119 |
with Global => null; |
| 120 |
|
| 121 |
function From_Universal_Image (Arg : String) return Valid_Big_Integer |
| 122 |
renames From_String; |
| 123 |
|
| 124 |
procedure Put_Image (S : in out Root_Buffer_Type'Class; V : Big_Integer); |
| 125 |
|
| 126 |
function "+" (L : Valid_Big_Integer) return Valid_Big_Integer |
| 127 |
with Global => null; |
| 128 |
|
| 129 |
function "-" (L : Valid_Big_Integer) return Valid_Big_Integer |
| 130 |
with Global => null; |
| 131 |
|
| 132 |
function "abs" (L : Valid_Big_Integer) return Valid_Big_Integer |
| 133 |
with Global => null; |
| 134 |
|
| 135 |
function "+" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 136 |
with Global => null; |
| 137 |
|
| 138 |
function "-" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 139 |
with Global => null; |
| 140 |
|
| 141 |
function "*" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 142 |
with Global => null; |
| 143 |
|
| 144 |
function "/" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 145 |
with Global => null; |
| 146 |
|
| 147 |
function "mod" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 148 |
with Global => null; |
| 149 |
|
| 150 |
function "rem" (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 151 |
with Global => null; |
| 152 |
|
| 153 |
function "**" (L : Valid_Big_Integer; R : Natural) return Valid_Big_Integer |
| 154 |
with Global => null; |
| 155 |
|
| 156 |
function Min (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 157 |
with Global => null; |
| 158 |
|
| 159 |
function Max (L, R : Valid_Big_Integer) return Valid_Big_Integer |
| 160 |
with Global => null; |
| 161 |
|
| 162 |
function Greatest_Common_Divisor |
| 163 |
(L, R : Valid_Big_Integer) return Big_Positive |
| 164 |
with |
| 165 |
Pre => (L /= To_Big_Integer (0) and R /= To_Big_Integer (0)) |
| 166 |
or else (raise Constraint_Error), |
| 167 |
Global => null; |
| 168 |
|
| 169 |
private |
| 170 |
|
| 171 |
type Controlled_Bignum is new Ada.Finalization.Controlled with record |
| 172 |
C : System.Address := System.Null_Address; |
| 173 |
end record; |
| 174 |
|
| 175 |
procedure Adjust (This : in out Controlled_Bignum); |
| 176 |
procedure Finalize (This : in out Controlled_Bignum); |
| 177 |
|
| 178 |
type Big_Integer is record |
| 179 |
Value : Controlled_Bignum; |
| 180 |
end record; |
| 181 |
|
| 182 |
end Ada.Numerics.Big_Numbers.Big_Integers; |