| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . A R I T H _ D O U B L E -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-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 |
-- This generic package provides software routines for doing arithmetic on |
| 33 |
-- double word signed integer values in cases where either overflow checking |
| 34 |
-- is required, or intermediate results are longer than the result type. |
| 35 |
|
| 36 |
with Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 37 |
|
| 38 |
generic |
| 39 |
|
| 40 |
type Double_Int is range <>; |
| 41 |
|
| 42 |
type Double_Uns is mod <>; |
| 43 |
|
| 44 |
type Single_Uns is mod <>; |
| 45 |
|
| 46 |
with function Shift_Left (A : Double_Uns; B : Natural) return Double_Uns |
| 47 |
is <>; |
| 48 |
|
| 49 |
with function Shift_Right (A : Double_Uns; B : Natural) return Double_Uns |
| 50 |
is <>; |
| 51 |
|
| 52 |
with function Shift_Left (A : Single_Uns; B : Natural) return Single_Uns |
| 53 |
is <>; |
| 54 |
|
| 55 |
package System.Arith_Double |
| 56 |
with Pure, SPARK_Mode |
| 57 |
is |
| 58 |
-- Preconditions in this unit are meant for analysis only, not for run-time |
| 59 |
-- checking, so that the expected exceptions are raised. This is enforced |
| 60 |
-- by setting the corresponding assertion policy to Ignore. Postconditions |
| 61 |
-- and contract cases should not be executed at runtime as well, in order |
| 62 |
-- not to slow down the execution of these functions. |
| 63 |
|
| 64 |
pragma Assertion_Policy (Pre => Ignore, |
| 65 |
Post => Ignore, |
| 66 |
Contract_Cases => Ignore, |
| 67 |
Ghost => Ignore); |
| 68 |
|
| 69 |
package BI_Ghost renames Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 70 |
subtype Big_Integer is BI_Ghost.Big_Integer with Ghost; |
| 71 |
subtype Big_Natural is BI_Ghost.Big_Natural with Ghost; |
| 72 |
subtype Big_Positive is BI_Ghost.Big_Positive with Ghost; |
| 73 |
use type BI_Ghost.Big_Integer; |
| 74 |
|
| 75 |
package Signed_Conversion is |
| 76 |
new BI_Ghost.Signed_Conversions (Int => Double_Int); |
| 77 |
|
| 78 |
function Big (Arg : Double_Int) return Big_Integer is |
| 79 |
(Signed_Conversion.To_Big_Integer (Arg)) |
| 80 |
with |
| 81 |
Ghost, |
| 82 |
Annotate => (GNATprove, Inline_For_Proof); |
| 83 |
|
| 84 |
package Unsigned_Conversion is |
| 85 |
new BI_Ghost.Unsigned_Conversions (Int => Double_Uns); |
| 86 |
|
| 87 |
function Big (Arg : Double_Uns) return Big_Integer is |
| 88 |
(Unsigned_Conversion.To_Big_Integer (Arg)) |
| 89 |
with |
| 90 |
Ghost, |
| 91 |
Annotate => (GNATprove, Inline_For_Proof); |
| 92 |
|
| 93 |
function In_Double_Int_Range (Arg : Big_Integer) return Boolean is |
| 94 |
(BI_Ghost.In_Range (Arg, Big (Double_Int'First), Big (Double_Int'Last))) |
| 95 |
with |
| 96 |
Ghost, |
| 97 |
Annotate => (GNATprove, Inline_For_Proof); |
| 98 |
|
| 99 |
function Add_With_Ovflo_Check (X, Y : Double_Int) return Double_Int |
| 100 |
with |
| 101 |
Pre => In_Double_Int_Range (Big (X) + Big (Y)), |
| 102 |
Post => Add_With_Ovflo_Check'Result = X + Y; |
| 103 |
-- Raises Constraint_Error if sum of operands overflows Double_Int, |
| 104 |
-- otherwise returns this sum of operands as Double_Int. |
| 105 |
-- |
| 106 |
-- The sum of ``X`` and ``Y`` is first computed using wrap-around |
| 107 |
-- semantics. |
| 108 |
-- |
| 109 |
-- If the sign of ``X`` and ``Y`` are opposed, no overflow is possible and |
| 110 |
-- the result is correct. |
| 111 |
-- |
| 112 |
-- Otherwise, ``X`` and ``Y`` have the same sign; if the sign of the result |
| 113 |
-- is not identical to ``X`` (or ``Y``), then an overflow occurred and |
| 114 |
-- the exception *Constraint_Error* is raised; otherwise the result is |
| 115 |
-- correct. |
| 116 |
|
| 117 |
function Subtract_With_Ovflo_Check (X, Y : Double_Int) return Double_Int |
| 118 |
with |
| 119 |
Pre => In_Double_Int_Range (Big (X) - Big (Y)), |
| 120 |
Post => Subtract_With_Ovflo_Check'Result = X - Y; |
| 121 |
-- Raises Constraint_Error if difference of operands overflows Double_Int, |
| 122 |
-- otherwise returns this difference of operands as Double_Int. |
| 123 |
-- |
| 124 |
-- The logic of the implementation is reversed from *Add_With_Ovflo_Check*: |
| 125 |
-- if ``X`` and ``Y`` have the same sign, no overflow is checked, otherwise |
| 126 |
-- a sign of the result is compared with the sign of ``X`` to check for |
| 127 |
-- overflow. |
| 128 |
|
| 129 |
function Multiply_With_Ovflo_Check (X, Y : Double_Int) return Double_Int |
| 130 |
with |
| 131 |
Pre => In_Double_Int_Range (Big (X) * Big (Y)), |
| 132 |
Post => Multiply_With_Ovflo_Check'Result = X * Y; |
| 133 |
pragma Convention (C, Multiply_With_Ovflo_Check); |
| 134 |
-- Raises Constraint_Error if product of operands overflows Double_Int, |
| 135 |
-- otherwise returns this product of operands as Double_Int. The code |
| 136 |
-- generator may also generate direct calls to this routine. |
| 137 |
-- |
| 138 |
-- The multiplication is done using pencil and paper algorithm applied to |
| 139 |
-- Single_Uns, that is to say done on unsigned values, then the correct |
| 140 |
-- signed value is returned. Overflow check is performed by looking at |
| 141 |
-- higher digits. |
| 142 |
|
| 143 |
function Same_Sign (X, Y : Big_Integer) return Boolean is |
| 144 |
(X = Big (Double_Int'(0)) |
| 145 |
or else Y = Big (Double_Int'(0)) |
| 146 |
or else (X < Big (Double_Int'(0))) = (Y < Big (Double_Int'(0)))) |
| 147 |
with Ghost; |
| 148 |
|
| 149 |
function Round_Quotient (X, Y, Q, R : Big_Integer) return Big_Integer is |
| 150 |
(if abs R > (abs Y - Big (Double_Int'(1))) / Big (Double_Int'(2)) then |
| 151 |
(if Same_Sign (X, Y) then Q + Big (Double_Int'(1)) |
| 152 |
else Q - Big (Double_Int'(1))) |
| 153 |
else |
| 154 |
Q) |
| 155 |
with |
| 156 |
Ghost, |
| 157 |
Pre => Y /= 0 and then Q = X / Y and then R = X rem Y; |
| 158 |
|
| 159 |
procedure Scaled_Divide |
| 160 |
(X, Y, Z : Double_Int; |
| 161 |
Q, R : out Double_Int; |
| 162 |
Round : Boolean) |
| 163 |
with |
| 164 |
Pre => Z /= 0 |
| 165 |
and then In_Double_Int_Range |
| 166 |
(if Round then Round_Quotient (Big (X) * Big (Y), Big (Z), |
| 167 |
Big (X) * Big (Y) / Big (Z), |
| 168 |
Big (X) * Big (Y) rem Big (Z)) |
| 169 |
else Big (X) * Big (Y) / Big (Z)), |
| 170 |
Post => Big (R) = Big (X) * Big (Y) rem Big (Z) |
| 171 |
and then |
| 172 |
(if Round then |
| 173 |
Big (Q) = Round_Quotient (Big (X) * Big (Y), Big (Z), |
| 174 |
Big (X) * Big (Y) / Big (Z), Big (R)) |
| 175 |
else |
| 176 |
Big (Q) = Big (X) * Big (Y) / Big (Z)); |
| 177 |
-- Performs the division of (``X`` * ``Y``) / ``Z``, storing the quotient |
| 178 |
-- in ``Q`` and the remainder in ``R``. |
| 179 |
-- |
| 180 |
-- Constraint_Error is raised if ``Z`` is zero, or if the quotient does not |
| 181 |
-- fit in ``Double_Int``. |
| 182 |
-- |
| 183 |
-- ``Round`` indicates if the result should be rounded. If ``Round`` is |
| 184 |
-- False, then ``Q``, ``R`` are the normal quotient and remainder from a |
| 185 |
-- truncating division. If ``Round`` is True, then ``Q`` is the rounded |
| 186 |
-- quotient. The remainder ``R`` is not affected by the setting of the |
| 187 |
-- ``Round`` flag. |
| 188 |
-- |
| 189 |
-- The multiplication is done using pencil and paper algorithm applied to |
| 190 |
-- Single_Uns, that is to say done on unsigned values. The result is a |
| 191 |
-- pair of Double_Uns values. |
| 192 |
-- |
| 193 |
-- The overflow is detected on the intermediate value. |
| 194 |
-- |
| 195 |
-- If Z is a Single_Uns value, the division is done using pencil and paper |
| 196 |
-- algorithm. |
| 197 |
-- |
| 198 |
-- Otherwise, the division is performed using the algorithm D from section |
| 199 |
-- 4.3.1 of "The Art of Computer Programming Vol. 2" [TACP2]. Rounding is |
| 200 |
-- applied on the result. |
| 201 |
-- |
| 202 |
-- Finally, the sign is applied to the result and returned. |
| 203 |
|
| 204 |
procedure Double_Divide |
| 205 |
(X, Y, Z : Double_Int; |
| 206 |
Q, R : out Double_Int; |
| 207 |
Round : Boolean) |
| 208 |
with |
| 209 |
Pre => Y /= 0 |
| 210 |
and then Z /= 0 |
| 211 |
and then In_Double_Int_Range |
| 212 |
(if Round then Round_Quotient (Big (X), Big (Y) * Big (Z), |
| 213 |
Big (X) / (Big (Y) * Big (Z)), |
| 214 |
Big (X) rem (Big (Y) * Big (Z))) |
| 215 |
else Big (X) / (Big (Y) * Big (Z))), |
| 216 |
Post => Big (R) = Big (X) rem (Big (Y) * Big (Z)) |
| 217 |
and then |
| 218 |
(if Round then |
| 219 |
Big (Q) = Round_Quotient (Big (X), Big (Y) * Big (Z), |
| 220 |
Big (X) / (Big (Y) * Big (Z)), Big (R)) |
| 221 |
else |
| 222 |
Big (Q) = Big (X) / (Big (Y) * Big (Z))); |
| 223 |
-- Performs the division ``X`` / (``Y`` * ``Z``), storing the quotient in |
| 224 |
-- ``Q`` and the remainder in ``R``. Constraint_Error is raised if ``Y`` or |
| 225 |
-- ``Z`` is zero, or if the quotient does not fit in ``Double_Int``. |
| 226 |
-- |
| 227 |
-- ``Round`` indicates if the result should be rounded. If ``Round`` is |
| 228 |
-- False, then ``Q``, ``R`` are the normal quotient and remainder from a |
| 229 |
-- truncating division. If ``Round`` is True, then ``Q`` is the rounded |
| 230 |
-- quotient. The remainder ``R`` is not affected by the setting of the |
| 231 |
-- ``Round`` flag. |
| 232 |
-- |
| 233 |
-- Division by 0 is first detected. |
| 234 |
-- |
| 235 |
-- The intermediate value ``Y`` * ``Z`` is then computed as a pair of |
| 236 |
-- Double_Uns value. that is to say done on unsigned values. |
| 237 |
-- |
| 238 |
-- If the high Double_Uns of the intermediate value is not 0, then 0 is |
| 239 |
-- returned. The overflow case of the largest negative number divided by |
| 240 |
-- -1 is detected here. |
| 241 |
-- |
| 242 |
-- Double_Uns division is then performed, the result is rounded, its sign |
| 243 |
-- is corrected, and then returned. |
| 244 |
|
| 245 |
end System.Arith_Double; |