Debian Codegraph @

gcc/ada/libgnat/s-veboop.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . V E C T O R S . B O O L E A N _ O P E R A T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2002-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 -- Ghost code, loop invariants and assertions in this unit are meant for
33 -- analysis only, not for run-time checking, as it would be too costly
34 -- otherwise. This is enforced by setting the assertion policy to Ignore.
35
36 pragma Assertion_Policy (Ghost => Ignore,
37 Loop_Invariant => Ignore,
38 Assert => Ignore);
39
40 package body System.Vectors.Boolean_Operations
41 with SPARK_Mode
42 is
43
44 SU : constant := Storage_Unit;
45 -- Convenient short hand, used throughout
46
47 -- The coding of this unit depends on the fact that the Component_Size
48 -- of a normally declared array of Boolean is equal to Storage_Unit. We
49 -- can't use the Component_Size directly since it is non-static. The
50 -- following declaration checks that this declaration is correct
51
52 type Boolean_Array is array (Integer range <>) of Boolean;
53 pragma Compile_Time_Error
54 (Boolean_Array'Component_Size /= SU, "run time compile failure");
55
56 -- NOTE: The boolean literals must be qualified here to avoid visibility
57 -- anomalies when this package is compiled through Rtsfind, in a context
58 -- that includes a user-defined type derived from boolean.
59
60 True_Val : constant Vector := Standard.True'Enum_Rep
61 + Standard.True'Enum_Rep * 2**SU
62 + Standard.True'Enum_Rep * 2**(SU * 2)
63 + Standard.True'Enum_Rep * 2**(SU * 3)
64 + Standard.True'Enum_Rep * 2**(SU * 4)
65 + Standard.True'Enum_Rep * 2**(SU * 5)
66 + Standard.True'Enum_Rep * 2**(SU * 6)
67 + Standard.True'Enum_Rep * 2**(SU * 7);
68 -- This constant represents the bits to be flipped to perform a logical
69 -- "not" on a vector of booleans, independent of the actual
70 -- representation of True.
71
72 -- The representations of (False, True) are assumed to be zero/one and
73 -- the maximum number of unpacked booleans per Vector is assumed to be 8.
74
75 pragma Assert (Standard.False'Enum_Rep = 0);
76 pragma Assert (Standard.True'Enum_Rep = 1);
77 pragma Assert (Vector'Size / Storage_Unit <= 8);
78
79 -- The reason we need to do these gymnastics is that no call to
80 -- Unchecked_Conversion can be made at the library level since this
81 -- unit is pure. Also a conversion from the array type to the Vector type
82 -- inside the body of "not" is inefficient because of alignment issues.
83
84 -----------
85 -- "not" --
86 -----------
87
88 function "not" (Item : Vectors.Vector) return Vectors.Vector is
89
90 procedure Prove_Not (Result : Vectors.Vector)
91 with
92 Ghost,
93 Pre => Valid (Item)
94 and then Result = (Item xor True_Val),
95 Post => Valid (Result)
96 and then (for all J in 1 .. Vector_Boolean_Size =>
97 Model (Result) (J) = not Model (Item) (J));
98
99 procedure Prove_Not (Result : Vectors.Vector) is
100 begin
101 for J in 1 .. Vector_Boolean_Size loop
102 pragma Assert
103 (Element (Result, J) = 1 - Element (Item, J));
104 end loop;
105 end Prove_Not;
106
107 begin
108 Prove_Not (Item xor True_Val);
109 return Item xor True_Val;
110 end "not";
111
112 ----------
113 -- Nand --
114 ----------
115
116 function Nand (Left, Right : Boolean) return Boolean is
117 begin
118 return not (Left and Right);
119 end Nand;
120
121 function Nand (Left, Right : Vectors.Vector) return Vectors.Vector is
122
123 procedure Prove_And (Result : Vectors.Vector)
124 with
125 Ghost,
126 Pre => Valid (Left)
127 and then Valid (Right)
128 and then Result = (Left and Right),
129 Post => Valid (Result)
130 and then (for all J in 1 .. Vector_Boolean_Size =>
131 Model (Result) (J) =
132 (Model (Left) (J) and Model (Right) (J)));
133
134 procedure Prove_And (Result : Vectors.Vector) is
135 begin
136 for J in 1 .. Vector_Boolean_Size loop
137 pragma Assert
138 (Element (Result, J) =
139 (if Element (Left, J) = 1
140 and Element (Right, J) = 1
141 then 1
142 else 0));
143 end loop;
144 end Prove_And;
145
146 begin
147 Prove_And (Left and Right);
148 return not (Left and Right);
149 end Nand;
150
151 ---------
152 -- Nor --
153 ---------
154
155 function Nor (Left, Right : Boolean) return Boolean is
156 begin
157 return not (Left or Right);
158 end Nor;
159
160 function Nor (Left, Right : Vectors.Vector) return Vectors.Vector is
161
162 procedure Prove_Or (Result : Vectors.Vector)
163 with
164 Ghost,
165 Pre => Valid (Left)
166 and then Valid (Right)
167 and then Result = (Left or Right),
168 Post => Valid (Result)
169 and then (for all J in 1 .. Vector_Boolean_Size =>
170 Model (Result) (J) =
171 (Model (Left) (J) or Model (Right) (J)));
172
173 procedure Prove_Or (Result : Vectors.Vector) is
174 begin
175 for J in 1 .. Vector_Boolean_Size loop
176 pragma Assert
177 (Element (Result, J) =
178 (if Element (Left, J) = 1
179 or Element (Right, J) = 1
180 then 1
181 else 0));
182 end loop;
183 end Prove_Or;
184
185 begin
186 Prove_Or (Left or Right);
187 return not (Left or Right);
188 end Nor;
189
190 ----------
191 -- Nxor --
192 ----------
193
194 function Nxor (Left, Right : Boolean) return Boolean is
195 begin
196 return not (Left xor Right);
197 end Nxor;
198
199 function Nxor (Left, Right : Vectors.Vector) return Vectors.Vector is
200
201 procedure Prove_Xor (Result : Vectors.Vector)
202 with
203 Ghost,
204 Pre => Valid (Left)
205 and then Valid (Right)
206 and then Result = (Left xor Right),
207 Post => Valid (Result)
208 and then (for all J in 1 .. Vector_Boolean_Size =>
209 Model (Result) (J) =
210 (Model (Left) (J) xor Model (Right) (J)));
211
212 procedure Prove_Xor (Result : Vectors.Vector) is
213 begin
214 for J in 1 .. Vector_Boolean_Size loop
215 pragma Assert
216 (Element (Result, J) =
217 (if Element (Left, J) = 1
218 xor Element (Right, J) = 1
219 then 1
220 else 0));
221 end loop;
222 end Prove_Xor;
223
224 begin
225 Prove_Xor (Left xor Right);
226 return not (Left xor Right);
227 end Nxor;
228
229 end System.Vectors.Boolean_Operations;