3 -- Grant of Unlimited Rights
5 -- Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
6 -- F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained
7 -- unlimited rights in the software and documentation contained herein.
8 -- Unlimited rights are defined in DFAR 252.227-7013(a)(19). By making
9 -- this public release, the Government intends to confer upon all
10 -- recipients unlimited rights equal to those held by the Government.
11 -- These rights include rights to use, duplicate, release or disclose the
12 -- released technical data and computer software in whole or in part, in
13 -- any manner and for any purpose whatsoever, and to have or permit others
18 -- ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
19 -- DISCLOSED ARE AS IS. THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED
20 -- WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
21 -- SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE
22 -- OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
23 -- PARTICULAR PURPOSE OF SAID MATERIAL.
27 -- Check that the floating point attributes Model_Mantissa,
28 -- Machine_Mantissa, Machine_Radix, and Machine_Rounds
29 -- are properly reported.
32 -- This test uses a generic package to compute and check the
33 -- values of the Machine_ attributes listed above. The
34 -- generic package is instantiated with the standard FLOAT
35 -- type and a floating point type for the maximum number
36 -- of digits of precision.
38 -- APPLICABILITY CRITERIA:
39 -- This test applies only to implementations supporting the
44 -- 26 JAN 96 SAIC Initial Release for 2.1
50 -- "Algorithms To Reveal Properties of Floating-Point Arithmetic"
51 -- Michael A. Malcolm; CACM November 1972; pgs 949-951.
53 -- Software Manual for Elementary Functions; W. J. Cody and W. Waite;
54 -- Prentice-Hall; 1980
55 -----------------------------------------------------------------------
57 -- This test relies upon the fact that
58 -- (A+2.0)-A is not necessarily 2.0. If A is large enough then adding
59 -- a small value to A does not change the value of A. Consider the case
60 -- where we have a decimal based floating point representation with 4
61 -- digits of precision. A floating point number would logically be
62 -- represented as "DDDD * 10 ** exp" where D is a value in the range 0..9.
63 -- The first loop of the test starts A at 2.0 and doubles it until
64 -- ((A+1.0)-A)-1.0 is no longer zero. For our decimal floating point
65 -- number this will be 1638 * 10**1 (the value 16384 rounded or truncated
66 -- to fit in 4 digits).
67 -- The second loop starts B at 2.0 and keeps doubling B until (A+B)-A is
68 -- no longer 0. This will keep looping until B is 8.0 because that is
69 -- the first value where rounding (assuming our machine rounds and addition
70 -- employs a guard digit) will change the upper 4 digits of the result:
75 -- Without rounding the second loop will continue until
82 -- The radix is then determined by (A+B)-A which will give 10.
84 -- The use of Tmp and ITmp in the test is to force values to be
85 -- stored into memory in the event that register precision is greater
86 -- than the stored precision of the floating point values.
89 -- The test for rounding is (ignoring the temporary variables used to
90 -- get the stored precision) is
91 -- Rounds := A + Radix/2.0 - A /= 0.0 ;
92 -- where A is the value determined in the first step that is the smallest
93 -- power of 2 such that A + 1.0 = A. This means that the true value of
94 -- A has one more digit in its value than 'Machine_Mantissa.
95 -- This check will detect the case where a value is always rounded.
96 -- There is an additional case where values are rounded to the nearest
97 -- even value. That is referred to as IEEE style rounding in the test.
99 -----------------------------------------------------------------------
103 with Ada
.Numerics
.Generic_Elementary_Functions
;
105 Verbose
: constant Boolean := False;
107 -- if one of the attribute computation loops exceeds Max_Iterations
108 -- it is most likely due to the compiler reordering an expression
109 -- that should not be reordered.
110 Illegal_Optimization
: exception;
111 Max_Iterations
: constant := 10_000
;
114 type Real
is digits <>;
119 package body Chk_Attrs
is
120 package EF
is new Ada
.Numerics
.Generic_Elementary_Functions
(Real
);
121 function Log
(X
: Real
) return Real
renames EF
.Log
;
124 -- names used in paper
125 Radix
: Integer; -- Beta
126 Mantissa_Digits
: Integer; -- t
127 Rounds
: Boolean; -- RND
129 -- made global to Determine_Attributes to help thwart optimization
131 Tmp
, Tmpa
, Tmp1
: Real
;
135 -- special constants - not declared as constants so that
136 -- the "stored" precision will be used instead of a "register"
143 procedure Thwart_Optimization
is
144 -- the purpose of this procedure is to reference the
145 -- global variables used by Determine_Attributes so
146 -- that the compiler is not likely to keep them in
147 -- a higher precision register for their entire lifetime.
149 if Report
.Ident_Bool
(False) then
156 One
:= 12.34; Two
:= 56.78; Zero
:= 90.12;
158 end Thwart_Optimization
;
161 -- determines values for Radix, Mantissa_Digits, and Rounds
162 -- This is mostly a straight translation of the C code.
163 -- The only significant addition is the iteration count
164 -- to prevent endless looping if things are really screwed up.
165 procedure Determine_Attributes
is
166 Iterations
: Integer;
171 Tmp
:= Real
'Machine (((A
+ One
) - A
) - One
);
172 while Tmp
= Zero
loop
173 A
:= Real
'Machine(A
+ A
);
174 Tmp
:= Real
'Machine(A
+ One
);
175 Tmp1
:= Real
'Machine(Tmp
- A
);
176 Tmp
:= Real
'Machine(Tmp1
- One
);
178 Iterations
:= Iterations
+ 1;
179 if Iterations
> Max_Iterations
then
180 raise Illegal_Optimization
;
185 Tmp
:= Real
'Machine(A
+ B
);
186 ITmp
:= Integer (Tmp
- A
);
188 B
:= Real
'Machine(B
+ B
);
189 Tmp
:= Real
'Machine(A
+ B
);
190 ITmp
:= Integer (Tmp
- A
);
192 Iterations
:= Iterations
+ 1;
193 if Iterations
> Max_Iterations
then
194 raise Illegal_Optimization
;
200 Mantissa_Digits
:= 0;
202 Tmp
:= Real
'Machine(((B
+ One
) - B
) - One
);
204 while (Tmp
= Zero
) loop
205 Mantissa_Digits
:= Mantissa_Digits
+ 1;
206 B
:= B
* Real
(Radix
);
207 Tmp
:= Real
'Machine(B
+ One
);
208 Tmp1
:= Real
'Machine(Tmp
- B
);
209 Tmp
:= Real
'Machine(Tmp1
- One
);
211 Iterations
:= Iterations
+ 1;
212 if Iterations
> Max_Iterations
then
213 raise Illegal_Optimization
;
218 Half_Radix
:= Real
(Radix
) / Two
;
219 Tmp
:= Real
'Machine(A
+ Half_Radix
);
220 Tmp1
:= Real
'Machine(Tmp
- A
);
221 if (Tmp1
/= Zero
) then
224 Tmpa
:= Real
'Machine(A
+ Real
(Radix
));
225 Tmp
:= Real
'Machine(Tmpa
+ Half_Radix
);
226 if not Rounds
and (Tmp
- TmpA
/= Zero
) then
229 Report
.Comment
("IEEE style rounding");
237 end Determine_Attributes
;
241 Show_Results
: Boolean := Verbose
;
242 Min_Mantissa_Digits
: Integer;
244 -- compute the actual Machine_* attribute values
245 Determine_Attributes
;
247 if Real
'Machine_Radix /= Radix
then
248 Report
.Failed
("'Machine_Radix incorrectly reports" &
249 Integer'Image (Real
'Machine_Radix));
250 Show_Results
:= True;
253 if Real
'Machine_Mantissa /= Mantissa_Digits
then
254 Report
.Failed
("'Machine_Mantissa incorrectly reports" &
255 Integer'Image (Real
'Machine_Mantissa));
256 Show_Results
:= True;
259 if Real
'Machine_Rounds /= Rounds
then
260 Report
.Failed
("'Machine_Rounds incorrectly reports " &
261 Boolean'Image (Real
'Machine_Rounds));
262 Show_Results
:= True;
266 Report
.Comment
("computed Machine_Mantissa is" &
267 Integer'Image (Mantissa_Digits
));
268 Report
.Comment
("computed Radix is" &
269 Integer'Image (Radix
));
270 Report
.Comment
("computed Rounds is " &
271 Boolean'Image (Rounds
));
274 -- check the model attributes against the machine attributes
276 if Real
'Model_Mantissa > Real
'Machine_Mantissa then
277 Report
.Failed
("model mantissa > machine mantissa");
281 -- 'Model_Mantissa >= ceiling(d*log(10)/log(radix))+1
282 Min_Mantissa_Digits
:=
285 Real
(Real
'Digits) * Log
(10.0) / Log
(Real
(Real
'Machine_Radix))
287 if Real
'Model_Mantissa < Min_Mantissa_Digits
then
288 Report
.Failed
("Model_Mantissa [" &
289 Integer'Image (Real
'Model_Mantissa) &
290 "] < minimum mantissa digits [" &
291 Integer'Image (Min_Mantissa_Digits
) &
296 when Illegal_Optimization
=>
297 Report
.Failed
("illegal optimization of" &
298 " floating point expression");
302 package Chk_Float
is new Chk_Attrs
(Float);
304 -- check the floating point type with the most digits
305 type A_Long_Float
is digits System
.Max_Digits
;
306 package Chk_A_Long_Float
is new Chk_Attrs
(A_Long_Float
);
308 Report
.Test
("CXG2001",
309 "Check the attributes Model_Mantissa," &
310 " Machine_Mantissa, Machine_Radix," &
311 " and Machine_Rounds");
313 Report
.Comment
("checking Standard.Float");
316 Report
.Comment
("checking a digits" &
317 Integer'Image (System
.Max_Digits
) &
318 " floating point type");
319 Chk_A_Long_Float
.Do_Test
;