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