annotate gcc/testsuite/ada/acats/tests/cxg/cxg2001.a @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 -- CXG2001.A
kono
parents:
diff changeset
2 --
kono
parents:
diff changeset
3 -- Grant of Unlimited Rights
kono
parents:
diff changeset
4 --
kono
parents:
diff changeset
5 -- Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
kono
parents:
diff changeset
6 -- F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained
kono
parents:
diff changeset
7 -- unlimited rights in the software and documentation contained herein.
kono
parents:
diff changeset
8 -- Unlimited rights are defined in DFAR 252.227-7013(a)(19). By making
kono
parents:
diff changeset
9 -- this public release, the Government intends to confer upon all
kono
parents:
diff changeset
10 -- recipients unlimited rights equal to those held by the Government.
kono
parents:
diff changeset
11 -- These rights include rights to use, duplicate, release or disclose the
kono
parents:
diff changeset
12 -- released technical data and computer software in whole or in part, in
kono
parents:
diff changeset
13 -- any manner and for any purpose whatsoever, and to have or permit others
kono
parents:
diff changeset
14 -- to do so.
kono
parents:
diff changeset
15 --
kono
parents:
diff changeset
16 -- DISCLAIMER
kono
parents:
diff changeset
17 --
kono
parents:
diff changeset
18 -- ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
kono
parents:
diff changeset
19 -- DISCLOSED ARE AS IS. THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED
kono
parents:
diff changeset
20 -- WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
kono
parents:
diff changeset
21 -- SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE
kono
parents:
diff changeset
22 -- OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
kono
parents:
diff changeset
23 -- PARTICULAR PURPOSE OF SAID MATERIAL.
kono
parents:
diff changeset
24 --*
kono
parents:
diff changeset
25 --
kono
parents:
diff changeset
26 -- OBJECTIVE:
kono
parents:
diff changeset
27 -- Check that the floating point attributes Model_Mantissa,
kono
parents:
diff changeset
28 -- Machine_Mantissa, Machine_Radix, and Machine_Rounds
kono
parents:
diff changeset
29 -- are properly reported.
kono
parents:
diff changeset
30 --
kono
parents:
diff changeset
31 -- TEST DESCRIPTION:
kono
parents:
diff changeset
32 -- This test uses a generic package to compute and check the
kono
parents:
diff changeset
33 -- values of the Machine_ attributes listed above. The
kono
parents:
diff changeset
34 -- generic package is instantiated with the standard FLOAT
kono
parents:
diff changeset
35 -- type and a floating point type for the maximum number
kono
parents:
diff changeset
36 -- of digits of precision.
kono
parents:
diff changeset
37 --
kono
parents:
diff changeset
38 -- APPLICABILITY CRITERIA:
kono
parents:
diff changeset
39 -- This test applies only to implementations supporting the
kono
parents:
diff changeset
40 -- Numerics Annex.
kono
parents:
diff changeset
41 --
kono
parents:
diff changeset
42 --
kono
parents:
diff changeset
43 -- CHANGE HISTORY:
kono
parents:
diff changeset
44 -- 26 JAN 96 SAIC Initial Release for 2.1
kono
parents:
diff changeset
45 --
kono
parents:
diff changeset
46 --!
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 -- References:
kono
parents:
diff changeset
49 --
kono
parents:
diff changeset
50 -- "Algorithms To Reveal Properties of Floating-Point Arithmetic"
kono
parents:
diff changeset
51 -- Michael A. Malcolm; CACM November 1972; pgs 949-951.
kono
parents:
diff changeset
52 --
kono
parents:
diff changeset
53 -- Software Manual for Elementary Functions; W. J. Cody and W. Waite;
kono
parents:
diff changeset
54 -- Prentice-Hall; 1980
kono
parents:
diff changeset
55 -----------------------------------------------------------------------
kono
parents:
diff changeset
56 --
kono
parents:
diff changeset
57 -- This test relies upon the fact that
kono
parents:
diff changeset
58 -- (A+2.0)-A is not necessarily 2.0. If A is large enough then adding
kono
parents:
diff changeset
59 -- a small value to A does not change the value of A. Consider the case
kono
parents:
diff changeset
60 -- where we have a decimal based floating point representation with 4
kono
parents:
diff changeset
61 -- digits of precision. A floating point number would logically be
kono
parents:
diff changeset
62 -- represented as "DDDD * 10 ** exp" where D is a value in the range 0..9.
kono
parents:
diff changeset
63 -- The first loop of the test starts A at 2.0 and doubles it until
kono
parents:
diff changeset
64 -- ((A+1.0)-A)-1.0 is no longer zero. For our decimal floating point
kono
parents:
diff changeset
65 -- number this will be 1638 * 10**1 (the value 16384 rounded or truncated
kono
parents:
diff changeset
66 -- to fit in 4 digits).
kono
parents:
diff changeset
67 -- The second loop starts B at 2.0 and keeps doubling B until (A+B)-A is
kono
parents:
diff changeset
68 -- no longer 0. This will keep looping until B is 8.0 because that is
kono
parents:
diff changeset
69 -- the first value where rounding (assuming our machine rounds and addition
kono
parents:
diff changeset
70 -- employs a guard digit) will change the upper 4 digits of the result:
kono
parents:
diff changeset
71 -- 1638_
kono
parents:
diff changeset
72 -- + 8
kono
parents:
diff changeset
73 -- -------
kono
parents:
diff changeset
74 -- 1639_
kono
parents:
diff changeset
75 -- Without rounding the second loop will continue until
kono
parents:
diff changeset
76 -- B is 16:
kono
parents:
diff changeset
77 -- 1638_
kono
parents:
diff changeset
78 -- + 16
kono
parents:
diff changeset
79 -- -------
kono
parents:
diff changeset
80 -- 1639_
kono
parents:
diff changeset
81 --
kono
parents:
diff changeset
82 -- The radix is then determined by (A+B)-A which will give 10.
kono
parents:
diff changeset
83 --
kono
parents:
diff changeset
84 -- The use of Tmp and ITmp in the test is to force values to be
kono
parents:
diff changeset
85 -- stored into memory in the event that register precision is greater
kono
parents:
diff changeset
86 -- than the stored precision of the floating point values.
kono
parents:
diff changeset
87 --
kono
parents:
diff changeset
88 --
kono
parents:
diff changeset
89 -- The test for rounding is (ignoring the temporary variables used to
kono
parents:
diff changeset
90 -- get the stored precision) is
kono
parents:
diff changeset
91 -- Rounds := A + Radix/2.0 - A /= 0.0 ;
kono
parents:
diff changeset
92 -- where A is the value determined in the first step that is the smallest
kono
parents:
diff changeset
93 -- power of 2 such that A + 1.0 = A. This means that the true value of
kono
parents:
diff changeset
94 -- A has one more digit in its value than 'Machine_Mantissa.
kono
parents:
diff changeset
95 -- This check will detect the case where a value is always rounded.
kono
parents:
diff changeset
96 -- There is an additional case where values are rounded to the nearest
kono
parents:
diff changeset
97 -- even value. That is referred to as IEEE style rounding in the test.
kono
parents:
diff changeset
98 --
kono
parents:
diff changeset
99 -----------------------------------------------------------------------
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 with System;
kono
parents:
diff changeset
102 with Report;
kono
parents:
diff changeset
103 with Ada.Numerics.Generic_Elementary_Functions;
kono
parents:
diff changeset
104 procedure CXG2001 is
kono
parents:
diff changeset
105 Verbose : constant Boolean := False;
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 -- if one of the attribute computation loops exceeds Max_Iterations
kono
parents:
diff changeset
108 -- it is most likely due to the compiler reordering an expression
kono
parents:
diff changeset
109 -- that should not be reordered.
kono
parents:
diff changeset
110 Illegal_Optimization : exception;
kono
parents:
diff changeset
111 Max_Iterations : constant := 10_000;
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 generic
kono
parents:
diff changeset
114 type Real is digits <>;
kono
parents:
diff changeset
115 package Chk_Attrs is
kono
parents:
diff changeset
116 procedure Do_Test;
kono
parents:
diff changeset
117 end Chk_Attrs;
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 package body Chk_Attrs is
kono
parents:
diff changeset
120 package EF is new Ada.Numerics.Generic_Elementary_Functions (Real);
kono
parents:
diff changeset
121 function Log (X : Real) return Real renames EF.Log;
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 -- names used in paper
kono
parents:
diff changeset
125 Radix : Integer; -- Beta
kono
parents:
diff changeset
126 Mantissa_Digits : Integer; -- t
kono
parents:
diff changeset
127 Rounds : Boolean; -- RND
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 -- made global to Determine_Attributes to help thwart optimization
kono
parents:
diff changeset
130 A, B : Real := 2.0;
kono
parents:
diff changeset
131 Tmp, Tmpa, Tmp1 : Real;
kono
parents:
diff changeset
132 ITmp : Integer;
kono
parents:
diff changeset
133 Half_Radix : Real;
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 -- special constants - not declared as constants so that
kono
parents:
diff changeset
136 -- the "stored" precision will be used instead of a "register"
kono
parents:
diff changeset
137 -- precision.
kono
parents:
diff changeset
138 Zero : Real := 0.0;
kono
parents:
diff changeset
139 One : Real := 1.0;
kono
parents:
diff changeset
140 Two : Real := 2.0;
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 procedure Thwart_Optimization is
kono
parents:
diff changeset
144 -- the purpose of this procedure is to reference the
kono
parents:
diff changeset
145 -- global variables used by Determine_Attributes so
kono
parents:
diff changeset
146 -- that the compiler is not likely to keep them in
kono
parents:
diff changeset
147 -- a higher precision register for their entire lifetime.
kono
parents:
diff changeset
148 begin
kono
parents:
diff changeset
149 if Report.Ident_Bool (False) then
kono
parents:
diff changeset
150 -- never executed
kono
parents:
diff changeset
151 A := A + 5.0;
kono
parents:
diff changeset
152 B := B + 6.0;
kono
parents:
diff changeset
153 Tmp := Tmp + 1.0;
kono
parents:
diff changeset
154 Tmp1 := Tmp1 + 2.0;
kono
parents:
diff changeset
155 Tmpa := Tmpa + 2.0;
kono
parents:
diff changeset
156 One := 12.34; Two := 56.78; Zero := 90.12;
kono
parents:
diff changeset
157 end if;
kono
parents:
diff changeset
158 end Thwart_Optimization;
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 -- determines values for Radix, Mantissa_Digits, and Rounds
kono
parents:
diff changeset
162 -- This is mostly a straight translation of the C code.
kono
parents:
diff changeset
163 -- The only significant addition is the iteration count
kono
parents:
diff changeset
164 -- to prevent endless looping if things are really screwed up.
kono
parents:
diff changeset
165 procedure Determine_Attributes is
kono
parents:
diff changeset
166 Iterations : Integer;
kono
parents:
diff changeset
167 begin
kono
parents:
diff changeset
168 Rounds := True;
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 Iterations := 0;
kono
parents:
diff changeset
171 Tmp := Real'Machine (((A + One) - A) - One);
kono
parents:
diff changeset
172 while Tmp = Zero loop
kono
parents:
diff changeset
173 A := Real'Machine(A + A);
kono
parents:
diff changeset
174 Tmp := Real'Machine(A + One);
kono
parents:
diff changeset
175 Tmp1 := Real'Machine(Tmp - A);
kono
parents:
diff changeset
176 Tmp := Real'Machine(Tmp1 - One);
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 Iterations := Iterations + 1;
kono
parents:
diff changeset
179 if Iterations > Max_Iterations then
kono
parents:
diff changeset
180 raise Illegal_Optimization;
kono
parents:
diff changeset
181 end if;
kono
parents:
diff changeset
182 end loop;
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 Iterations := 0;
kono
parents:
diff changeset
185 Tmp := Real'Machine(A + B);
kono
parents:
diff changeset
186 ITmp := Integer (Tmp - A);
kono
parents:
diff changeset
187 while ITmp = 0 loop
kono
parents:
diff changeset
188 B := Real'Machine(B + B);
kono
parents:
diff changeset
189 Tmp := Real'Machine(A + B);
kono
parents:
diff changeset
190 ITmp := Integer (Tmp - A);
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 Iterations := Iterations + 1;
kono
parents:
diff changeset
193 if Iterations > Max_Iterations then
kono
parents:
diff changeset
194 raise Illegal_Optimization;
kono
parents:
diff changeset
195 end if;
kono
parents:
diff changeset
196 end loop;
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 Radix := ITmp;
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 Mantissa_Digits := 0;
kono
parents:
diff changeset
201 B := 1.0;
kono
parents:
diff changeset
202 Tmp := Real'Machine(((B + One) - B) - One);
kono
parents:
diff changeset
203 Iterations := 0;
kono
parents:
diff changeset
204 while (Tmp = Zero) loop
kono
parents:
diff changeset
205 Mantissa_Digits := Mantissa_Digits + 1;
kono
parents:
diff changeset
206 B := B * Real (Radix);
kono
parents:
diff changeset
207 Tmp := Real'Machine(B + One);
kono
parents:
diff changeset
208 Tmp1 := Real'Machine(Tmp - B);
kono
parents:
diff changeset
209 Tmp := Real'Machine(Tmp1 - One);
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 Iterations := Iterations + 1;
kono
parents:
diff changeset
212 if Iterations > Max_Iterations then
kono
parents:
diff changeset
213 raise Illegal_Optimization;
kono
parents:
diff changeset
214 end if;
kono
parents:
diff changeset
215 end loop;
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 Rounds := False;
kono
parents:
diff changeset
218 Half_Radix := Real (Radix) / Two;
kono
parents:
diff changeset
219 Tmp := Real'Machine(A + Half_Radix);
kono
parents:
diff changeset
220 Tmp1 := Real'Machine(Tmp - A);
kono
parents:
diff changeset
221 if (Tmp1 /= Zero) then
kono
parents:
diff changeset
222 Rounds := True;
kono
parents:
diff changeset
223 end if;
kono
parents:
diff changeset
224 Tmpa := Real'Machine(A + Real (Radix));
kono
parents:
diff changeset
225 Tmp := Real'Machine(Tmpa + Half_Radix);
kono
parents:
diff changeset
226 if not Rounds and (Tmp - TmpA /= Zero) then
kono
parents:
diff changeset
227 Rounds := True;
kono
parents:
diff changeset
228 if Verbose then
kono
parents:
diff changeset
229 Report.Comment ("IEEE style rounding");
kono
parents:
diff changeset
230 end if;
kono
parents:
diff changeset
231 end if;
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 exception
kono
parents:
diff changeset
234 when others =>
kono
parents:
diff changeset
235 Thwart_Optimization;
kono
parents:
diff changeset
236 raise;
kono
parents:
diff changeset
237 end Determine_Attributes;
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 procedure Do_Test is
kono
parents:
diff changeset
241 Show_Results : Boolean := Verbose;
kono
parents:
diff changeset
242 Min_Mantissa_Digits : Integer;
kono
parents:
diff changeset
243 begin
kono
parents:
diff changeset
244 -- compute the actual Machine_* attribute values
kono
parents:
diff changeset
245 Determine_Attributes;
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 if Real'Machine_Radix /= Radix then
kono
parents:
diff changeset
248 Report.Failed ("'Machine_Radix incorrectly reports" &
kono
parents:
diff changeset
249 Integer'Image (Real'Machine_Radix));
kono
parents:
diff changeset
250 Show_Results := True;
kono
parents:
diff changeset
251 end if;
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 if Real'Machine_Mantissa /= Mantissa_Digits then
kono
parents:
diff changeset
254 Report.Failed ("'Machine_Mantissa incorrectly reports" &
kono
parents:
diff changeset
255 Integer'Image (Real'Machine_Mantissa));
kono
parents:
diff changeset
256 Show_Results := True;
kono
parents:
diff changeset
257 end if;
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 if Real'Machine_Rounds /= Rounds then
kono
parents:
diff changeset
260 Report.Failed ("'Machine_Rounds incorrectly reports " &
kono
parents:
diff changeset
261 Boolean'Image (Real'Machine_Rounds));
kono
parents:
diff changeset
262 Show_Results := True;
kono
parents:
diff changeset
263 end if;
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 if Show_Results then
kono
parents:
diff changeset
266 Report.Comment ("computed Machine_Mantissa is" &
kono
parents:
diff changeset
267 Integer'Image (Mantissa_Digits));
kono
parents:
diff changeset
268 Report.Comment ("computed Radix is" &
kono
parents:
diff changeset
269 Integer'Image (Radix));
kono
parents:
diff changeset
270 Report.Comment ("computed Rounds is " &
kono
parents:
diff changeset
271 Boolean'Image (Rounds));
kono
parents:
diff changeset
272 end if;
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 -- check the model attributes against the machine attributes
kono
parents:
diff changeset
275 -- G.2.2(3)/3;6.0
kono
parents:
diff changeset
276 if Real'Model_Mantissa > Real'Machine_Mantissa then
kono
parents:
diff changeset
277 Report.Failed ("model mantissa > machine mantissa");
kono
parents:
diff changeset
278 end if;
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 -- G.2.2(3)/2;6.0
kono
parents:
diff changeset
281 -- 'Model_Mantissa >= ceiling(d*log(10)/log(radix))+1
kono
parents:
diff changeset
282 Min_Mantissa_Digits :=
kono
parents:
diff changeset
283 Integer (
kono
parents:
diff changeset
284 Real'Ceiling (
kono
parents:
diff changeset
285 Real(Real'Digits) * Log(10.0) / Log(Real(Real'Machine_Radix))
kono
parents:
diff changeset
286 ) ) + 1;
kono
parents:
diff changeset
287 if Real'Model_Mantissa < Min_Mantissa_Digits then
kono
parents:
diff changeset
288 Report.Failed ("Model_Mantissa [" &
kono
parents:
diff changeset
289 Integer'Image (Real'Model_Mantissa) &
kono
parents:
diff changeset
290 "] < minimum mantissa digits [" &
kono
parents:
diff changeset
291 Integer'Image (Min_Mantissa_Digits) &
kono
parents:
diff changeset
292 "]");
kono
parents:
diff changeset
293 end if;
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 exception
kono
parents:
diff changeset
296 when Illegal_Optimization =>
kono
parents:
diff changeset
297 Report.Failed ("illegal optimization of" &
kono
parents:
diff changeset
298 " floating point expression");
kono
parents:
diff changeset
299 end Do_Test;
kono
parents:
diff changeset
300 end Chk_Attrs;
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 package Chk_Float is new Chk_Attrs (Float);
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 -- check the floating point type with the most digits
kono
parents:
diff changeset
305 type A_Long_Float is digits System.Max_Digits;
kono
parents:
diff changeset
306 package Chk_A_Long_Float is new Chk_Attrs (A_Long_Float);
kono
parents:
diff changeset
307 begin
kono
parents:
diff changeset
308 Report.Test ("CXG2001",
kono
parents:
diff changeset
309 "Check the attributes Model_Mantissa," &
kono
parents:
diff changeset
310 " Machine_Mantissa, Machine_Radix," &
kono
parents:
diff changeset
311 " and Machine_Rounds");
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 Report.Comment ("checking Standard.Float");
kono
parents:
diff changeset
314 Chk_Float.Do_Test;
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 Report.Comment ("checking a digits" &
kono
parents:
diff changeset
317 Integer'Image (System.Max_Digits) &
kono
parents:
diff changeset
318 " floating point type");
kono
parents:
diff changeset
319 Chk_A_Long_Float.Do_Test;
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 Report.Result;
kono
parents:
diff changeset
322 end CXG2001;