aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/cxg/cxg2022.a
blob: f9e4d1cae3329aef6d965d5dbdbe616a1ffda46a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
-- CXG2022.A
--
--                             Grant of Unlimited Rights
--
--     Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
--     F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained 
--     unlimited rights in the software and documentation contained herein.
--     Unlimited rights are defined in DFAR 252.227-7013(a)(19).  By making 
--     this public release, the Government intends to confer upon all 
--     recipients unlimited rights  equal to those held by the Government.  
--     These rights include rights to use, duplicate, release or disclose the 
--     released technical data and computer software in whole or in part, in 
--     any manner and for any purpose whatsoever, and to have or permit others 
--     to do so.
--
--                                    DISCLAIMER
--
--     ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
--     DISCLOSED ARE AS IS.  THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED 
--     WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
--     SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE 
--     OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
--     PARTICULAR PURPOSE OF SAID MATERIAL.
--*
--
-- OBJECTIVE:
--      Check that multiplication and division of binary fixed point 
--      numbers with compatible 'small values produce exact results.
--
-- TEST DESCRIPTION:
--      Signed, unsigned, and a mixture of signed and unsigned
--      binary fixed point values are multiplied and divided.
--      The result is checked against the expected "perfect result set"
--
-- SPECIAL REQUIREMENTS
--      The Strict Mode for the numerical accuracy must be
--      selected.  The method by which this mode is selected
--      is implementation dependent.
--
-- APPLICABILITY CRITERIA:
--      This test applies only to implementations supporting the
--      Numerics Annex.
--      This test only applies to the Strict Mode for numerical
--      accuracy.
--
--
-- CHANGE HISTORY:
--       1 Apr 96   SAIC    Initial release for 2.1
--      29 Jan 1998 EDS     Repaired fixed point errors ("**" and 
--                          assumptions about 'Small)
--!

with System;
with Report;
procedure CXG2022 is
   Verbose : constant Boolean := False;

procedure Check_Signed is
   type Pairs is delta 2.0 range -2.0 ** (System.Max_Mantissa) ..
                                  2.0 ** (System.Max_Mantissa) - 1.0;
   type Halves is delta 0.5 range -2.0 ** (System.Max_Mantissa-2) ..
                                   2.0 ** (System.Max_Mantissa-2) - 1.0;
   P1, P2, P3, P4 : Pairs;
   H1, H2, H3, H4 : Halves;

   procedure Dont_Opt is
   -- keep optimizer from knowing the constant value of expressions
   begin
      if Report.Ident_Bool (False) then
         P1 :=  2.0;  P2 := 4.0;  P3 := 6.0;
         H1 := -2.0;  H2 := 9.0;  H3 := 3.0;
      end if;
   end Dont_Opt;

begin
   H1 := -0.5;
   H2 := Halves'First;
   H3 := 1.0;
   P1 := 12.0;
   P2 := Pairs'First;
   P3 := Pairs'Last;
   Dont_Opt;

   P4 := Pairs (P1 * H1);     -- 12.0 * -0.5
   if P4 /= -6.0 then
      Report.Failed ("12.0 * -0.5 = " & Pairs'Image (P4));
   end if;

   H4 := Halves (P1 / H1);     -- 12.0 / -0.5
   if H4 /= -24.0 then
      Report.Failed ("12.0 / -0.5 = " & Halves'Image (H4));
   end if;

   P4 := P3 * H3;     -- Pairs'Last * 1.0
   if P4 /= Pairs'Last then
      Report.Failed ("Pairs'Last * 1.0 = " & Pairs'Image (P4));
   end if;

   P4 := P3 / H3;     -- Pairs'Last / 1.0
   if P4 /= Pairs'Last then
      Report.Failed ("Pairs'Last / 1.0 = " & Pairs'Image (P4));
   end if;

   P4 := P2 * 0.25;     -- Pairs'First * 0.25
   if P4 /= Pairs (-2.0 ** (System.Max_Mantissa - 2)) then
      Report.Failed ("Pairs'First * 0.25 = " & Pairs'Image (P4));
   end if;

   P4 := 100.5 / H1;   -- 100.5 / -0.5
   if P4 = -201.0 then
      null;   -- Perfect result
   elsif Pairs'Small = 2.0 and ( P4 = -200.0 or P4 = -202.0 ) then
      null;   -- Allowed variation
   else
      Report.Failed ("Pairs'Small =" & Pairs'Image (Pairs'Small) &
                     " and 100.5/-0.5 = " & Pairs'Image (P4) );
   end if;

   H4 := H1 * H2;   --  -0.5 * Halves'First
   if H4 /= Halves (2.0 ** (System.Max_Mantissa-3)) then
      Report.Failed ("-0.5 * Halves'First =" & Halves'Image (H4) &
                     " instead of " &
                     Halves'Image( Halves(2.0 ** (System.Max_Mantissa-3)))); 
   end if;

exception
   when others =>
      Report.Failed ("unexpected exception in Check_Signed");
end Check_Signed;



procedure Check_Unsigned is
   type Pairs is delta 2.0 range 0.0 .. 2.0 ** (System.Max_Mantissa+1) - 1.0;
   type Halves is delta 0.5 range 0.0 .. 2.0 ** (System.Max_Mantissa-1) - 1.0;
   P1, P2, P3, P4 : Pairs;
   H1, H2, H3, H4 : Halves;

   procedure Dont_Opt is
   -- keep optimizer from knowing the constant value of expressions
   begin
      if Report.Ident_Bool (False) then
         P1 :=  2.0;  P2 := 4.0;  P3 := 6.0;
         H1 :=  2.0;  H2 := 9.0;  H3 := 3.0;
      end if;
   end Dont_Opt;

begin
   H1 := 10.5;
   H2 := Halves(2.0 ** (System.Max_Mantissa - 6));
   H3 := 1.0;
   P1 := 12.0;
   P2 := Pairs'Last / 2;
   P3 := Pairs'Last;
   Dont_Opt;

   P4 := Pairs (P1 * H1);     -- 12.0 * 10.5
   if P4 /= 126.0 then
      Report.Failed ("12.0 * 10.5 = " & Pairs'Image (P4));
   end if;

   H4 := Halves (P1 / H1);     -- 12.0 / 10.5
   if H4 /= 1.0 and H4 /= 1.5 then
      Report.Failed ("12.0 / 10.5 = " & Halves'Image (H4));
   end if;

   P4 := P3 * H3;     -- Pairs'Last * 1.0
   if P4 /= Pairs'Last then
      Report.Failed ("Pairs'Last * 1.0 = " & Pairs'Image (P4));
   end if;

   P4 := P3 / H3;     -- Pairs'Last / 1.0
   if P4 /= Pairs'Last then
      Report.Failed ("Pairs'Last / 1.0 = " & Pairs'Image (P4));
   end if;

   P4 := P1 * 0.25;     -- 12.0 * 0.25
   if P4 /= 2.0 and P4 /= 4.0 then
      Report.Failed ("12.0 * 0.25 = " & Pairs'Image (P4));
   end if;

   P4 := 100.5 / H1;   -- 100.5 / 10.5    = 9.571...
   if P4 /= 8.0 and P4 /= 10.0 then
      Report.Failed ("100.5/10.5 = " & Pairs'Image (P4));
   end if;

   H4 := H2 * 2;   --  2**(max_mantissa-6) * 2
   if H4 /= Halves(2.0 ** (System.Max_Mantissa-5)) then
      Report.Failed ("2**(System.Max_Mantissa-6) * 2=" & Halves'Image (H4) &
                     " instead of " &
                     Halves'Image( Halves(2.0 ** (System.Max_Mantissa-5)))); 
   end if;

exception
   when others =>
      Report.Failed ("unexpected exception in Check_Unsigned");
end Check_Unsigned;



procedure Check_Mixed is
   type Pairs is delta 2.0 range -2.0 ** (System.Max_Mantissa) ..
                                  2.0 ** (System.Max_Mantissa) - 1.0;
   type Halves is delta 0.5 range 0.0 .. 2.0 ** (System.Max_Mantissa-1) - 1.0;
   P1, P2, P3, P4 : Pairs;
   H1, H2, H3, H4 : Halves;

   procedure Dont_Opt is
   -- keep optimizer from knowing the constant value of expressions
   begin
      if Report.Ident_Bool (False) then
         P1 :=  2.0;  P2 := 4.0;  P3 := 6.0;
         H1 :=  2.0;  H2 := 9.0;  H3 := 3.0;
      end if;
   end Dont_Opt;

begin
   H1 := 10.5;
   H2 := Halves(2.0 ** (System.Max_Mantissa - 6));
   H3 := 1.0;
   P1 := 12.0;
   P2 := -4.0;
   P3 := Pairs'Last;
   Dont_Opt;

   P4 := Pairs (P1 * H1);     -- 12.0 * 10.5
   if P4 /= 126.0 then
      Report.Failed ("12.0 * 10.5 = " & Pairs'Image (P4));
   end if;

   H4 := Halves (P1 / H1);     -- 12.0 / 10.5
   if H4 /= 1.0 and H4 /= 1.5 then
      Report.Failed ("12.0 / 10.5 = " & Halves'Image (H4));
   end if;

   P4 := P3 * H3;     -- Pairs'Last * 1.0
   if P4 /= Pairs'Last then
      Report.Failed ("Pairs'Last * 1.0 = " & Pairs'Image (P4));
   end if;

   P4 := P3 / H3;     -- Pairs'Last / 1.0
   if P4 /= Pairs'Last then
      Report.Failed ("Pairs'Last / 1.0 = " & Pairs'Image (P4));
   end if;

   P4 := P1 * 0.25;   -- 12.0 * 0.25
   if P4 = 3.0 then
      null;    -- Perfect result
   elsif Pairs'Small = 2.0 and then ( P4 = 2.0 or P4 = 4.0 ) then
      null;    -- Allowed deviation
   else
      Report.Failed ("Pairs'Small =" & Pairs'Image (Pairs'Small) &
                     "and 12.0 * 0.25 = " & Pairs'Image (P4) );
   end if;

   P4 := 100.5 / H1;   -- 100.5 / 10.5    = 9.571...
   if P4 = 9.0 then
      null;    -- Perfect result
   elsif Pairs'Small = 2.0 and then ( P4 = 8.0 or P4 = 10.0 ) then
      null;    -- Allowed values
   else
     Report.Failed ("Pairs'Small =" & Pairs'Image (Pairs'Small) &
                    "and 100.5/10.5 = " & Pairs'Image (P4) );
   end if;

   H4 := H2 * 2;   --  2**(max_mantissa-6) * 2
   if H4 /= Halves(2.0 ** (System.Max_Mantissa-5)) then
      Report.Failed ("2**(System.Max_Mantissa-6) * 2=" & Halves'Image (H4) &
                     " instead of " &
                     Halves'Image( Halves(2.0 ** (System.Max_Mantissa-5)))); 
   end if;

   P4 := Pairs(P1 * 6) / P2;    -- 12 * 6 / -4
   if (P4 /= -18.0)  then
      Report.Failed ("12*6/-4 = " & Pairs'Image(P4));
   end if;

   P4 := Halves(P1 * 6.0) / P2;    -- 12 * 6 / -4
   if (P4 /= -18.0)  then
      Report.Failed ("Halves(12*6)/-4 = " & Pairs'Image(P4));
   end if;

exception
   when others =>
      Report.Failed ("unexpected exception in Check_Mixed");
end Check_Mixed;


begin  -- main
   Report.Test ("CXG2022",
                "Check the accuracy of multiplication and division" &
                " of binary fixed point numbers"); 
   if Verbose then
      Report.Comment ("starting signed test");
   end if;
   Check_Signed;

   if Verbose then
      Report.Comment ("starting unsigned test");
   end if;
   Check_Unsigned;

   if Verbose then
      Report.Comment ("starting mixed sign test");
   end if;
   Check_Mixed;

   Report.Result;
end CXG2022;