Debian Codegraph @

gcc/ada/libgnat/s-arit32.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . A R I T H _ 3 2 --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2020-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 32-bit
33 -- signed integer values in cases where either overflow checking is
34 -- required, or intermediate results are longer than 32 bits.
35
36 -- Preconditions in this unit are meant for analysis only, not for run-time
37 -- checking, so that the expected exceptions are raised. This is enforced
38 -- by setting the corresponding assertion policy to Ignore. Postconditions
39 -- and contract cases should not be executed at runtime as well, in order
40 -- not to slow down the execution of these functions.
41
42 pragma Assertion_Policy (Pre => Ignore,
43 Post => Ignore,
44 Contract_Cases => Ignore,
45 Ghost => Ignore);
46
47 with Interfaces;
48 with Ada.Numerics.Big_Numbers.Big_Integers_Ghost;
49
50 package System.Arith_32
51 with Pure, SPARK_Mode
52 is
53 use type Ada.Numerics.Big_Numbers.Big_Integers_Ghost.Big_Integer;
54 use type Interfaces.Integer_32;
55
56 subtype Int32 is Interfaces.Integer_32;
57
58 subtype Big_Integer is
59 Ada.Numerics.Big_Numbers.Big_Integers_Ghost.Big_Integer
60 with Ghost;
61
62 package Signed_Conversion is new
63 Ada.Numerics.Big_Numbers.Big_Integers_Ghost.Signed_Conversions
64 (Int => Int32);
65
66 function Big (Arg : Int32) return Big_Integer is
67 (Signed_Conversion.To_Big_Integer (Arg))
68 with Ghost;
69
70 function In_Int32_Range (Arg : Big_Integer) return Boolean is
71 (Ada.Numerics.Big_Numbers.Big_Integers_Ghost.In_Range
72 (Arg, Big (Int32'First), Big (Int32'Last)))
73 with Ghost;
74
75 function Same_Sign (X, Y : Big_Integer) return Boolean is
76 (X = Big (Int32'(0))
77 or else Y = Big (Int32'(0))
78 or else (X < Big (Int32'(0))) = (Y < Big (Int32'(0))))
79 with Ghost;
80
81 function Round_Quotient (X, Y, Q, R : Big_Integer) return Big_Integer is
82 (if abs R > (abs Y - Big (Int32'(1))) / Big (Int32'(2)) then
83 (if Same_Sign (X, Y) then Q + Big (Int32'(1))
84 else Q - Big (Int32'(1)))
85 else
86 Q)
87 with
88 Ghost,
89 Pre => Y /= 0 and then Q = X / Y and then R = X rem Y;
90
91 procedure Scaled_Divide32
92 (X, Y, Z : Int32;
93 Q, R : out Int32;
94 Round : Boolean)
95 with
96 Pre => Z /= 0
97 and then In_Int32_Range
98 (if Round then Round_Quotient (Big (X) * Big (Y), Big (Z),
99 Big (X) * Big (Y) / Big (Z),
100 Big (X) * Big (Y) rem Big (Z))
101 else Big (X) * Big (Y) / Big (Z)),
102 Post => Big (R) = Big (X) * Big (Y) rem Big (Z)
103 and then
104 (if Round then
105 Big (Q) = Round_Quotient (Big (X) * Big (Y), Big (Z),
106 Big (X) * Big (Y) / Big (Z), Big (R))
107 else
108 Big (Q) = Big (X) * Big (Y) / Big (Z));
109 -- Performs the division of (``X`` * ``Y``) / ``Z``, storing the quotient
110 -- in ``Q`` and the remainder in ``R``.
111 --
112 -- Constraint_Error is raised if ``Z`` is zero, or if the quotient does not
113 -- fit in 32-bits.
114 --
115 -- ``Round`` indicates if the result should be rounded. If ``Round`` is
116 -- False, then ``Q``, ``R`` are the normal quotient and remainder from a
117 -- truncating division. If ``Round`` is True, then ``Q`` is the rounded
118 -- quotient. The remainder ``R`` is not affected by the setting of the
119 -- ``Round`` flag.
120 --
121 -- The multiplication is done using pencil and paper algorithm using base
122 -- 2**32. The multiplication is done on unsigned values. The result is a
123 -- 128 bit value.
124 --
125 -- The overflow is detected on the intermediate value.
126 --
127 -- If Z is a 32 bit value, the division is done using pencil and paper
128 -- algorithm.
129 --
130 -- Otherwise, the division is performed using the algorithm D from section
131 -- 4.3.1 of "The Art of Computer Programming Vol. 2" [TACP2]. Rounding is
132 -- applied on the result.
133 --
134 -- Finally, the sign is applied to the result and returned.
135
136 end System.Arith_32;