annotate gcc/ada/exp_fixd.adb @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- E X P _ F I X D --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
kono
parents:
diff changeset
17 -- for more details. You should have received a copy of the GNU General --
kono
parents:
diff changeset
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
kono
parents:
diff changeset
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
kono
parents:
diff changeset
20 -- --
kono
parents:
diff changeset
21 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
23 -- --
kono
parents:
diff changeset
24 ------------------------------------------------------------------------------
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 with Atree; use Atree;
kono
parents:
diff changeset
27 with Checks; use Checks;
kono
parents:
diff changeset
28 with Einfo; use Einfo;
kono
parents:
diff changeset
29 with Exp_Util; use Exp_Util;
kono
parents:
diff changeset
30 with Nlists; use Nlists;
kono
parents:
diff changeset
31 with Nmake; use Nmake;
kono
parents:
diff changeset
32 with Restrict; use Restrict;
kono
parents:
diff changeset
33 with Rident; use Rident;
kono
parents:
diff changeset
34 with Rtsfind; use Rtsfind;
kono
parents:
diff changeset
35 with Sem; use Sem;
kono
parents:
diff changeset
36 with Sem_Eval; use Sem_Eval;
kono
parents:
diff changeset
37 with Sem_Res; use Sem_Res;
kono
parents:
diff changeset
38 with Sem_Util; use Sem_Util;
kono
parents:
diff changeset
39 with Sinfo; use Sinfo;
kono
parents:
diff changeset
40 with Snames; use Snames;
kono
parents:
diff changeset
41 with Stand; use Stand;
kono
parents:
diff changeset
42 with Tbuild; use Tbuild;
kono
parents:
diff changeset
43 with Uintp; use Uintp;
kono
parents:
diff changeset
44 with Urealp; use Urealp;
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 package body Exp_Fixd is
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 -----------------------
kono
parents:
diff changeset
49 -- Local Subprograms --
kono
parents:
diff changeset
50 -----------------------
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 -- General note; in this unit, a number of routines are driven by the
kono
parents:
diff changeset
53 -- types (Etype) of their operands. Since we are dealing with unanalyzed
kono
parents:
diff changeset
54 -- expressions as they are constructed, the Etypes would not normally be
kono
parents:
diff changeset
55 -- set, but the construction routines that we use in this unit do in fact
kono
parents:
diff changeset
56 -- set the Etype values correctly. In addition, setting the Etype ensures
kono
parents:
diff changeset
57 -- that the analyzer does not try to redetermine the type when the node
kono
parents:
diff changeset
58 -- is analyzed (which would be wrong, since in the case where we set the
kono
parents:
diff changeset
59 -- Treat_Fixed_As_Integer or Conversion_OK flags, it would think it was
kono
parents:
diff changeset
60 -- still dealing with a normal fixed-point operation and mess it up).
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 function Build_Conversion
kono
parents:
diff changeset
63 (N : Node_Id;
kono
parents:
diff changeset
64 Typ : Entity_Id;
kono
parents:
diff changeset
65 Expr : Node_Id;
kono
parents:
diff changeset
66 Rchk : Boolean := False;
kono
parents:
diff changeset
67 Trunc : Boolean := False) return Node_Id;
kono
parents:
diff changeset
68 -- Build an expression that converts the expression Expr to type Typ,
kono
parents:
diff changeset
69 -- taking the source location from Sloc (N). If the conversions involve
kono
parents:
diff changeset
70 -- fixed-point types, then the Conversion_OK flag will be set so that the
kono
parents:
diff changeset
71 -- resulting conversions do not get re-expanded. On return the resulting
kono
parents:
diff changeset
72 -- node has its Etype set. If Rchk is set, then Do_Range_Check is set
kono
parents:
diff changeset
73 -- in the resulting conversion node. If Trunc is set, then the
kono
parents:
diff changeset
74 -- Float_Truncate flag is set on the conversion, which must be from
kono
parents:
diff changeset
75 -- a floating-point type to an integer type.
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 function Build_Divide (N : Node_Id; L, R : Node_Id) return Node_Id;
kono
parents:
diff changeset
78 -- Builds an N_Op_Divide node from the given left and right operand
kono
parents:
diff changeset
79 -- expressions, using the source location from Sloc (N). The operands are
kono
parents:
diff changeset
80 -- either both Universal_Real, in which case Build_Divide differs from
kono
parents:
diff changeset
81 -- Make_Op_Divide only in that the Etype of the resulting node is set (to
kono
parents:
diff changeset
82 -- Universal_Real), or they can be integer types. In this case the integer
kono
parents:
diff changeset
83 -- types need not be the same, and Build_Divide converts the operand with
kono
parents:
diff changeset
84 -- the smaller sized type to match the type of the other operand and sets
kono
parents:
diff changeset
85 -- this as the result type. The Rounded_Result flag of the result in this
kono
parents:
diff changeset
86 -- case is set from the Rounded_Result flag of node N. On return, the
kono
parents:
diff changeset
87 -- resulting node is analyzed, and has its Etype set.
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 function Build_Double_Divide
kono
parents:
diff changeset
90 (N : Node_Id;
kono
parents:
diff changeset
91 X, Y, Z : Node_Id) return Node_Id;
kono
parents:
diff changeset
92 -- Returns a node corresponding to the value X/(Y*Z) using the source
kono
parents:
diff changeset
93 -- location from Sloc (N). The division is rounded if the Rounded_Result
kono
parents:
diff changeset
94 -- flag of N is set. The integer types of X, Y, Z may be different. On
kono
parents:
diff changeset
95 -- return the resulting node is analyzed, and has its Etype set.
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 procedure Build_Double_Divide_Code
kono
parents:
diff changeset
98 (N : Node_Id;
kono
parents:
diff changeset
99 X, Y, Z : Node_Id;
kono
parents:
diff changeset
100 Qnn, Rnn : out Entity_Id;
kono
parents:
diff changeset
101 Code : out List_Id);
kono
parents:
diff changeset
102 -- Generates a sequence of code for determining the quotient and remainder
kono
parents:
diff changeset
103 -- of the division X/(Y*Z), using the source location from Sloc (N).
kono
parents:
diff changeset
104 -- Entities of appropriate types are allocated for the quotient and
kono
parents:
diff changeset
105 -- remainder and returned in Qnn and Rnn. The result is rounded if the
kono
parents:
diff changeset
106 -- Rounded_Result flag of N is set. The Etype fields of Qnn and Rnn are
kono
parents:
diff changeset
107 -- appropriately set on return.
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 function Build_Multiply (N : Node_Id; L, R : Node_Id) return Node_Id;
kono
parents:
diff changeset
110 -- Builds an N_Op_Multiply node from the given left and right operand
kono
parents:
diff changeset
111 -- expressions, using the source location from Sloc (N). The operands are
kono
parents:
diff changeset
112 -- either both Universal_Real, in which case Build_Multiply differs from
kono
parents:
diff changeset
113 -- Make_Op_Multiply only in that the Etype of the resulting node is set (to
kono
parents:
diff changeset
114 -- Universal_Real), or they can be integer types. In this case the integer
kono
parents:
diff changeset
115 -- types need not be the same, and Build_Multiply chooses a type long
kono
parents:
diff changeset
116 -- enough to hold the product (i.e. twice the size of the longer of the two
kono
parents:
diff changeset
117 -- operand types), and both operands are converted to this type. The Etype
kono
parents:
diff changeset
118 -- of the result is also set to this value. However, the result can never
kono
parents:
diff changeset
119 -- overflow Integer_64, so this is the largest type that is ever generated.
kono
parents:
diff changeset
120 -- On return, the resulting node is analyzed and has its Etype set.
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 function Build_Rem (N : Node_Id; L, R : Node_Id) return Node_Id;
kono
parents:
diff changeset
123 -- Builds an N_Op_Rem node from the given left and right operand
kono
parents:
diff changeset
124 -- expressions, using the source location from Sloc (N). The operands are
kono
parents:
diff changeset
125 -- both integer types, which need not be the same. Build_Rem converts the
kono
parents:
diff changeset
126 -- operand with the smaller sized type to match the type of the other
kono
parents:
diff changeset
127 -- operand and sets this as the result type. The result is never rounded
kono
parents:
diff changeset
128 -- (rem operations cannot be rounded in any case). On return, the resulting
kono
parents:
diff changeset
129 -- node is analyzed and has its Etype set.
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 function Build_Scaled_Divide
kono
parents:
diff changeset
132 (N : Node_Id;
kono
parents:
diff changeset
133 X, Y, Z : Node_Id) return Node_Id;
kono
parents:
diff changeset
134 -- Returns a node corresponding to the value X*Y/Z using the source
kono
parents:
diff changeset
135 -- location from Sloc (N). The division is rounded if the Rounded_Result
kono
parents:
diff changeset
136 -- flag of N is set. The integer types of X, Y, Z may be different. On
kono
parents:
diff changeset
137 -- return the resulting node is analyzed and has is Etype set.
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 procedure Build_Scaled_Divide_Code
kono
parents:
diff changeset
140 (N : Node_Id;
kono
parents:
diff changeset
141 X, Y, Z : Node_Id;
kono
parents:
diff changeset
142 Qnn, Rnn : out Entity_Id;
kono
parents:
diff changeset
143 Code : out List_Id);
kono
parents:
diff changeset
144 -- Generates a sequence of code for determining the quotient and remainder
kono
parents:
diff changeset
145 -- of the division X*Y/Z, using the source location from Sloc (N). Entities
kono
parents:
diff changeset
146 -- of appropriate types are allocated for the quotient and remainder and
kono
parents:
diff changeset
147 -- returned in Qnn and Rrr. The integer types for X, Y, Z may be different.
kono
parents:
diff changeset
148 -- The division is rounded if the Rounded_Result flag of N is set. The
kono
parents:
diff changeset
149 -- Etype fields of Qnn and Rnn are appropriately set on return.
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 procedure Do_Divide_Fixed_Fixed (N : Node_Id);
kono
parents:
diff changeset
152 -- Handles expansion of divide for case of two fixed-point operands
kono
parents:
diff changeset
153 -- (neither of them universal), with an integer or fixed-point result.
kono
parents:
diff changeset
154 -- N is the N_Op_Divide node to be expanded.
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 procedure Do_Divide_Fixed_Universal (N : Node_Id);
kono
parents:
diff changeset
157 -- Handles expansion of divide for case of a fixed-point operand divided
kono
parents:
diff changeset
158 -- by a universal real operand, with an integer or fixed-point result. N
kono
parents:
diff changeset
159 -- is the N_Op_Divide node to be expanded.
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 procedure Do_Divide_Universal_Fixed (N : Node_Id);
kono
parents:
diff changeset
162 -- Handles expansion of divide for case of a universal real operand
kono
parents:
diff changeset
163 -- divided by a fixed-point operand, with an integer or fixed-point
kono
parents:
diff changeset
164 -- result. N is the N_Op_Divide node to be expanded.
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 procedure Do_Multiply_Fixed_Fixed (N : Node_Id);
kono
parents:
diff changeset
167 -- Handles expansion of multiply for case of two fixed-point operands
kono
parents:
diff changeset
168 -- (neither of them universal), with an integer or fixed-point result.
kono
parents:
diff changeset
169 -- N is the N_Op_Multiply node to be expanded.
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 procedure Do_Multiply_Fixed_Universal (N : Node_Id; Left, Right : Node_Id);
kono
parents:
diff changeset
172 -- Handles expansion of multiply for case of a fixed-point operand
kono
parents:
diff changeset
173 -- multiplied by a universal real operand, with an integer or fixed-
kono
parents:
diff changeset
174 -- point result. N is the N_Op_Multiply node to be expanded, and
kono
parents:
diff changeset
175 -- Left, Right are the operands (which may have been switched).
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 procedure Expand_Convert_Fixed_Static (N : Node_Id);
kono
parents:
diff changeset
178 -- This routine is called where the node N is a conversion of a literal
kono
parents:
diff changeset
179 -- or other static expression of a fixed-point type to some other type.
kono
parents:
diff changeset
180 -- In such cases, we simply rewrite the operand as a real literal and
kono
parents:
diff changeset
181 -- reanalyze. This avoids problems which would otherwise result from
kono
parents:
diff changeset
182 -- attempting to build and fold expressions involving constants.
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 function Fpt_Value (N : Node_Id) return Node_Id;
kono
parents:
diff changeset
185 -- Given an operand of fixed-point operation, return an expression that
kono
parents:
diff changeset
186 -- represents the corresponding Universal_Real value. The expression
kono
parents:
diff changeset
187 -- can be of integer type, floating-point type, or fixed-point type.
kono
parents:
diff changeset
188 -- The expression returned is neither analyzed and resolved. The Etype
kono
parents:
diff changeset
189 -- of the result is properly set (to Universal_Real).
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 function Integer_Literal
kono
parents:
diff changeset
192 (N : Node_Id;
kono
parents:
diff changeset
193 V : Uint;
kono
parents:
diff changeset
194 Negative : Boolean := False) return Node_Id;
kono
parents:
diff changeset
195 -- Given a non-negative universal integer value, build a typed integer
kono
parents:
diff changeset
196 -- literal node, using the smallest applicable standard integer type. If
kono
parents:
diff changeset
197 -- and only if Negative is true a negative literal is built. If V exceeds
kono
parents:
diff changeset
198 -- 2**63-1, the largest value allowed for perfect result set scaling
kono
parents:
diff changeset
199 -- factors (see RM G.2.3(22)), then Empty is returned. The node N provides
kono
parents:
diff changeset
200 -- the Sloc value for the constructed literal. The Etype of the resulting
kono
parents:
diff changeset
201 -- literal is correctly set, and it is marked as analyzed.
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 function Real_Literal (N : Node_Id; V : Ureal) return Node_Id;
kono
parents:
diff changeset
204 -- Build a real literal node from the given value, the Etype of the
kono
parents:
diff changeset
205 -- returned node is set to Universal_Real, since all floating-point
kono
parents:
diff changeset
206 -- arithmetic operations that we construct use Universal_Real
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 function Rounded_Result_Set (N : Node_Id) return Boolean;
kono
parents:
diff changeset
209 -- Returns True if N is a node that contains the Rounded_Result flag
kono
parents:
diff changeset
210 -- and if the flag is true or the target type is an integer type.
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 procedure Set_Result
kono
parents:
diff changeset
213 (N : Node_Id;
kono
parents:
diff changeset
214 Expr : Node_Id;
kono
parents:
diff changeset
215 Rchk : Boolean := False;
kono
parents:
diff changeset
216 Trunc : Boolean := False);
kono
parents:
diff changeset
217 -- N is the node for the current conversion, division or multiplication
kono
parents:
diff changeset
218 -- operation, and Expr is an expression representing the result. Expr may
kono
parents:
diff changeset
219 -- be of floating-point or integer type. If the operation result is fixed-
kono
parents:
diff changeset
220 -- point, then the value of Expr is in units of small of the result type
kono
parents:
diff changeset
221 -- (i.e. small's have already been dealt with). The result of the call is
kono
parents:
diff changeset
222 -- to replace N by an appropriate conversion to the result type, dealing
kono
parents:
diff changeset
223 -- with rounding for the decimal types case. The node is then analyzed and
kono
parents:
diff changeset
224 -- resolved using the result type. If Rchk or Trunc are True, then
kono
parents:
diff changeset
225 -- respectively Do_Range_Check and Float_Truncate are set in the
kono
parents:
diff changeset
226 -- resulting conversion.
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 ----------------------
kono
parents:
diff changeset
229 -- Build_Conversion --
kono
parents:
diff changeset
230 ----------------------
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 function Build_Conversion
kono
parents:
diff changeset
233 (N : Node_Id;
kono
parents:
diff changeset
234 Typ : Entity_Id;
kono
parents:
diff changeset
235 Expr : Node_Id;
kono
parents:
diff changeset
236 Rchk : Boolean := False;
kono
parents:
diff changeset
237 Trunc : Boolean := False) return Node_Id
kono
parents:
diff changeset
238 is
kono
parents:
diff changeset
239 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
240 Result : Node_Id;
kono
parents:
diff changeset
241 Rcheck : Boolean := Rchk;
kono
parents:
diff changeset
242
kono
parents:
diff changeset
243 begin
kono
parents:
diff changeset
244 -- A special case, if the expression is an integer literal and the
kono
parents:
diff changeset
245 -- target type is an integer type, then just retype the integer
kono
parents:
diff changeset
246 -- literal to the desired target type. Don't do this if we need
kono
parents:
diff changeset
247 -- a range check.
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 if Nkind (Expr) = N_Integer_Literal
kono
parents:
diff changeset
250 and then Is_Integer_Type (Typ)
kono
parents:
diff changeset
251 and then not Rchk
kono
parents:
diff changeset
252 then
kono
parents:
diff changeset
253 Result := Expr;
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 -- Cases where we end up with a conversion. Note that we do not use the
kono
parents:
diff changeset
256 -- Convert_To abstraction here, since we may be decorating the resulting
kono
parents:
diff changeset
257 -- conversion with Rounded_Result and/or Conversion_OK, so we want the
kono
parents:
diff changeset
258 -- conversion node present, even if it appears to be redundant.
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 else
kono
parents:
diff changeset
261 -- Remove inner conversion if both inner and outer conversions are
kono
parents:
diff changeset
262 -- to integer types, since the inner one serves no purpose (except
kono
parents:
diff changeset
263 -- perhaps to set rounding, so we preserve the Rounded_Result flag)
kono
parents:
diff changeset
264 -- and also we preserve the range check flag on the inner operand
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 if Is_Integer_Type (Typ)
kono
parents:
diff changeset
267 and then Is_Integer_Type (Etype (Expr))
kono
parents:
diff changeset
268 and then Nkind (Expr) = N_Type_Conversion
kono
parents:
diff changeset
269 then
kono
parents:
diff changeset
270 Result :=
kono
parents:
diff changeset
271 Make_Type_Conversion (Loc,
kono
parents:
diff changeset
272 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
kono
parents:
diff changeset
273 Expression => Expression (Expr));
kono
parents:
diff changeset
274 Set_Rounded_Result (Result, Rounded_Result_Set (Expr));
kono
parents:
diff changeset
275 Rcheck := Rcheck or Do_Range_Check (Expr);
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 -- For all other cases, a simple type conversion will work
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 else
kono
parents:
diff changeset
280 Result :=
kono
parents:
diff changeset
281 Make_Type_Conversion (Loc,
kono
parents:
diff changeset
282 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
kono
parents:
diff changeset
283 Expression => Expr);
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 Set_Float_Truncate (Result, Trunc);
kono
parents:
diff changeset
286 end if;
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 -- Set Conversion_OK if either result or expression type is a
kono
parents:
diff changeset
289 -- fixed-point type, since from a semantic point of view, we are
kono
parents:
diff changeset
290 -- treating fixed-point values as integers at this stage.
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 if Is_Fixed_Point_Type (Typ)
kono
parents:
diff changeset
293 or else Is_Fixed_Point_Type (Etype (Expression (Result)))
kono
parents:
diff changeset
294 then
kono
parents:
diff changeset
295 Set_Conversion_OK (Result);
kono
parents:
diff changeset
296 end if;
kono
parents:
diff changeset
297
kono
parents:
diff changeset
298 -- Set Do_Range_Check if either it was requested by the caller,
kono
parents:
diff changeset
299 -- or if an eliminated inner conversion had a range check.
kono
parents:
diff changeset
300
kono
parents:
diff changeset
301 if Rcheck then
kono
parents:
diff changeset
302 Enable_Range_Check (Result);
kono
parents:
diff changeset
303 else
kono
parents:
diff changeset
304 Set_Do_Range_Check (Result, False);
kono
parents:
diff changeset
305 end if;
kono
parents:
diff changeset
306 end if;
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 Set_Etype (Result, Typ);
kono
parents:
diff changeset
309 return Result;
kono
parents:
diff changeset
310 end Build_Conversion;
kono
parents:
diff changeset
311
kono
parents:
diff changeset
312 ------------------
kono
parents:
diff changeset
313 -- Build_Divide --
kono
parents:
diff changeset
314 ------------------
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 function Build_Divide (N : Node_Id; L, R : Node_Id) return Node_Id is
kono
parents:
diff changeset
317 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
318 Left_Type : constant Entity_Id := Base_Type (Etype (L));
kono
parents:
diff changeset
319 Right_Type : constant Entity_Id := Base_Type (Etype (R));
kono
parents:
diff changeset
320 Result_Type : Entity_Id;
kono
parents:
diff changeset
321 Rnode : Node_Id;
kono
parents:
diff changeset
322
kono
parents:
diff changeset
323 begin
kono
parents:
diff changeset
324 -- Deal with floating-point case first
kono
parents:
diff changeset
325
kono
parents:
diff changeset
326 if Is_Floating_Point_Type (Left_Type) then
kono
parents:
diff changeset
327 pragma Assert (Left_Type = Universal_Real);
kono
parents:
diff changeset
328 pragma Assert (Right_Type = Universal_Real);
kono
parents:
diff changeset
329
kono
parents:
diff changeset
330 Rnode := Make_Op_Divide (Loc, L, R);
kono
parents:
diff changeset
331 Result_Type := Universal_Real;
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 -- Integer and fixed-point cases
kono
parents:
diff changeset
334
kono
parents:
diff changeset
335 else
kono
parents:
diff changeset
336 -- An optimization. If the right operand is the literal 1, then we
kono
parents:
diff changeset
337 -- can just return the left hand operand. Putting the optimization
kono
parents:
diff changeset
338 -- here allows us to omit the check at the call site.
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 if Nkind (R) = N_Integer_Literal and then Intval (R) = 1 then
kono
parents:
diff changeset
341 return L;
kono
parents:
diff changeset
342 end if;
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 -- If left and right types are the same, no conversion needed
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 if Left_Type = Right_Type then
kono
parents:
diff changeset
347 Result_Type := Left_Type;
kono
parents:
diff changeset
348 Rnode :=
kono
parents:
diff changeset
349 Make_Op_Divide (Loc,
kono
parents:
diff changeset
350 Left_Opnd => L,
kono
parents:
diff changeset
351 Right_Opnd => R);
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 -- Use left type if it is the larger of the two
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 elsif Esize (Left_Type) >= Esize (Right_Type) then
kono
parents:
diff changeset
356 Result_Type := Left_Type;
kono
parents:
diff changeset
357 Rnode :=
kono
parents:
diff changeset
358 Make_Op_Divide (Loc,
kono
parents:
diff changeset
359 Left_Opnd => L,
kono
parents:
diff changeset
360 Right_Opnd => Build_Conversion (N, Left_Type, R));
kono
parents:
diff changeset
361
kono
parents:
diff changeset
362 -- Otherwise right type is larger of the two, us it
kono
parents:
diff changeset
363
kono
parents:
diff changeset
364 else
kono
parents:
diff changeset
365 Result_Type := Right_Type;
kono
parents:
diff changeset
366 Rnode :=
kono
parents:
diff changeset
367 Make_Op_Divide (Loc,
kono
parents:
diff changeset
368 Left_Opnd => Build_Conversion (N, Right_Type, L),
kono
parents:
diff changeset
369 Right_Opnd => R);
kono
parents:
diff changeset
370 end if;
kono
parents:
diff changeset
371 end if;
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 -- We now have a divide node built with Result_Type set. First
kono
parents:
diff changeset
374 -- set Etype of result, as required for all Build_xxx routines
kono
parents:
diff changeset
375
kono
parents:
diff changeset
376 Set_Etype (Rnode, Base_Type (Result_Type));
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 -- Set Treat_Fixed_As_Integer if operation on fixed-point type
kono
parents:
diff changeset
379 -- since this is a literal arithmetic operation, to be performed
kono
parents:
diff changeset
380 -- by Gigi without any consideration of small values.
kono
parents:
diff changeset
381
kono
parents:
diff changeset
382 if Is_Fixed_Point_Type (Result_Type) then
kono
parents:
diff changeset
383 Set_Treat_Fixed_As_Integer (Rnode);
kono
parents:
diff changeset
384 end if;
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 -- The result is rounded if the target of the operation is decimal
kono
parents:
diff changeset
387 -- and Rounded_Result is set, or if the target of the operation
kono
parents:
diff changeset
388 -- is an integer type.
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390 if Is_Integer_Type (Etype (N))
kono
parents:
diff changeset
391 or else Rounded_Result_Set (N)
kono
parents:
diff changeset
392 then
kono
parents:
diff changeset
393 Set_Rounded_Result (Rnode);
kono
parents:
diff changeset
394 end if;
kono
parents:
diff changeset
395
kono
parents:
diff changeset
396 return Rnode;
kono
parents:
diff changeset
397 end Build_Divide;
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 -------------------------
kono
parents:
diff changeset
400 -- Build_Double_Divide --
kono
parents:
diff changeset
401 -------------------------
kono
parents:
diff changeset
402
kono
parents:
diff changeset
403 function Build_Double_Divide
kono
parents:
diff changeset
404 (N : Node_Id;
kono
parents:
diff changeset
405 X, Y, Z : Node_Id) return Node_Id
kono
parents:
diff changeset
406 is
kono
parents:
diff changeset
407 Y_Size : constant Nat := UI_To_Int (Esize (Etype (Y)));
kono
parents:
diff changeset
408 Z_Size : constant Nat := UI_To_Int (Esize (Etype (Z)));
kono
parents:
diff changeset
409 Expr : Node_Id;
kono
parents:
diff changeset
410
kono
parents:
diff changeset
411 begin
kono
parents:
diff changeset
412 -- If denominator fits in 64 bits, we can build the operations directly
kono
parents:
diff changeset
413 -- without causing any intermediate overflow, so that's what we do.
kono
parents:
diff changeset
414
kono
parents:
diff changeset
415 if Nat'Max (Y_Size, Z_Size) <= 32 then
kono
parents:
diff changeset
416 return
kono
parents:
diff changeset
417 Build_Divide (N, X, Build_Multiply (N, Y, Z));
kono
parents:
diff changeset
418
kono
parents:
diff changeset
419 -- Otherwise we use the runtime routine
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 -- [Qnn : Interfaces.Integer_64,
kono
parents:
diff changeset
422 -- Rnn : Interfaces.Integer_64;
kono
parents:
diff changeset
423 -- Double_Divide (X, Y, Z, Qnn, Rnn, Round);
kono
parents:
diff changeset
424 -- Qnn]
kono
parents:
diff changeset
425
kono
parents:
diff changeset
426 else
kono
parents:
diff changeset
427 declare
kono
parents:
diff changeset
428 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
429 Qnn : Entity_Id;
kono
parents:
diff changeset
430 Rnn : Entity_Id;
kono
parents:
diff changeset
431 Code : List_Id;
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 pragma Warnings (Off, Rnn);
kono
parents:
diff changeset
434
kono
parents:
diff changeset
435 begin
kono
parents:
diff changeset
436 Build_Double_Divide_Code (N, X, Y, Z, Qnn, Rnn, Code);
kono
parents:
diff changeset
437 Insert_Actions (N, Code);
kono
parents:
diff changeset
438 Expr := New_Occurrence_Of (Qnn, Loc);
kono
parents:
diff changeset
439
kono
parents:
diff changeset
440 -- Set type of result in case used elsewhere (see note at start)
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 Set_Etype (Expr, Etype (Qnn));
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 -- Set result as analyzed (see note at start on build routines)
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 return Expr;
kono
parents:
diff changeset
447 end;
kono
parents:
diff changeset
448 end if;
kono
parents:
diff changeset
449 end Build_Double_Divide;
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 ------------------------------
kono
parents:
diff changeset
452 -- Build_Double_Divide_Code --
kono
parents:
diff changeset
453 ------------------------------
kono
parents:
diff changeset
454
kono
parents:
diff changeset
455 -- If the denominator can be computed in 64-bits, we build
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 -- [Nnn : constant typ := typ (X);
kono
parents:
diff changeset
458 -- Dnn : constant typ := typ (Y) * typ (Z)
kono
parents:
diff changeset
459 -- Qnn : constant typ := Nnn / Dnn;
kono
parents:
diff changeset
460 -- Rnn : constant typ := Nnn / Dnn;
kono
parents:
diff changeset
461
kono
parents:
diff changeset
462 -- If the numerator cannot be computed in 64 bits, we build
kono
parents:
diff changeset
463
kono
parents:
diff changeset
464 -- [Qnn : typ;
kono
parents:
diff changeset
465 -- Rnn : typ;
kono
parents:
diff changeset
466 -- Double_Divide (X, Y, Z, Qnn, Rnn, Round);]
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 procedure Build_Double_Divide_Code
kono
parents:
diff changeset
469 (N : Node_Id;
kono
parents:
diff changeset
470 X, Y, Z : Node_Id;
kono
parents:
diff changeset
471 Qnn, Rnn : out Entity_Id;
kono
parents:
diff changeset
472 Code : out List_Id)
kono
parents:
diff changeset
473 is
kono
parents:
diff changeset
474 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
475
kono
parents:
diff changeset
476 X_Size : constant Nat := UI_To_Int (Esize (Etype (X)));
kono
parents:
diff changeset
477 Y_Size : constant Nat := UI_To_Int (Esize (Etype (Y)));
kono
parents:
diff changeset
478 Z_Size : constant Nat := UI_To_Int (Esize (Etype (Z)));
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 QR_Siz : Nat;
kono
parents:
diff changeset
481 QR_Typ : Entity_Id;
kono
parents:
diff changeset
482
kono
parents:
diff changeset
483 Nnn : Entity_Id;
kono
parents:
diff changeset
484 Dnn : Entity_Id;
kono
parents:
diff changeset
485
kono
parents:
diff changeset
486 Quo : Node_Id;
kono
parents:
diff changeset
487 Rnd : Entity_Id;
kono
parents:
diff changeset
488
kono
parents:
diff changeset
489 begin
kono
parents:
diff changeset
490 -- Find type that will allow computation of numerator
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 QR_Siz := Nat'Max (X_Size, 2 * Nat'Max (Y_Size, Z_Size));
kono
parents:
diff changeset
493
kono
parents:
diff changeset
494 if QR_Siz <= 16 then
kono
parents:
diff changeset
495 QR_Typ := Standard_Integer_16;
kono
parents:
diff changeset
496 elsif QR_Siz <= 32 then
kono
parents:
diff changeset
497 QR_Typ := Standard_Integer_32;
kono
parents:
diff changeset
498 elsif QR_Siz <= 64 then
kono
parents:
diff changeset
499 QR_Typ := Standard_Integer_64;
kono
parents:
diff changeset
500
kono
parents:
diff changeset
501 -- For more than 64, bits, we use the 64-bit integer defined in
kono
parents:
diff changeset
502 -- Interfaces, so that it can be handled by the runtime routine.
kono
parents:
diff changeset
503
kono
parents:
diff changeset
504 else
kono
parents:
diff changeset
505 QR_Typ := RTE (RE_Integer_64);
kono
parents:
diff changeset
506 end if;
kono
parents:
diff changeset
507
kono
parents:
diff changeset
508 -- Define quotient and remainder, and set their Etypes, so
kono
parents:
diff changeset
509 -- that they can be picked up by Build_xxx routines.
kono
parents:
diff changeset
510
kono
parents:
diff changeset
511 Qnn := Make_Temporary (Loc, 'S');
kono
parents:
diff changeset
512 Rnn := Make_Temporary (Loc, 'R');
kono
parents:
diff changeset
513
kono
parents:
diff changeset
514 Set_Etype (Qnn, QR_Typ);
kono
parents:
diff changeset
515 Set_Etype (Rnn, QR_Typ);
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 -- Case that we can compute the denominator in 64 bits
kono
parents:
diff changeset
518
kono
parents:
diff changeset
519 if QR_Siz <= 64 then
kono
parents:
diff changeset
520
kono
parents:
diff changeset
521 -- Create temporaries for numerator and denominator and set Etypes,
kono
parents:
diff changeset
522 -- so that New_Occurrence_Of picks them up for Build_xxx calls.
kono
parents:
diff changeset
523
kono
parents:
diff changeset
524 Nnn := Make_Temporary (Loc, 'N');
kono
parents:
diff changeset
525 Dnn := Make_Temporary (Loc, 'D');
kono
parents:
diff changeset
526
kono
parents:
diff changeset
527 Set_Etype (Nnn, QR_Typ);
kono
parents:
diff changeset
528 Set_Etype (Dnn, QR_Typ);
kono
parents:
diff changeset
529
kono
parents:
diff changeset
530 Code := New_List (
kono
parents:
diff changeset
531 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
532 Defining_Identifier => Nnn,
kono
parents:
diff changeset
533 Object_Definition => New_Occurrence_Of (QR_Typ, Loc),
kono
parents:
diff changeset
534 Constant_Present => True,
kono
parents:
diff changeset
535 Expression => Build_Conversion (N, QR_Typ, X)),
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
538 Defining_Identifier => Dnn,
kono
parents:
diff changeset
539 Object_Definition => New_Occurrence_Of (QR_Typ, Loc),
kono
parents:
diff changeset
540 Constant_Present => True,
kono
parents:
diff changeset
541 Expression =>
kono
parents:
diff changeset
542 Build_Multiply (N,
kono
parents:
diff changeset
543 Build_Conversion (N, QR_Typ, Y),
kono
parents:
diff changeset
544 Build_Conversion (N, QR_Typ, Z))));
kono
parents:
diff changeset
545
kono
parents:
diff changeset
546 Quo :=
kono
parents:
diff changeset
547 Build_Divide (N,
kono
parents:
diff changeset
548 New_Occurrence_Of (Nnn, Loc),
kono
parents:
diff changeset
549 New_Occurrence_Of (Dnn, Loc));
kono
parents:
diff changeset
550
kono
parents:
diff changeset
551 Set_Rounded_Result (Quo, Rounded_Result_Set (N));
kono
parents:
diff changeset
552
kono
parents:
diff changeset
553 Append_To (Code,
kono
parents:
diff changeset
554 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
555 Defining_Identifier => Qnn,
kono
parents:
diff changeset
556 Object_Definition => New_Occurrence_Of (QR_Typ, Loc),
kono
parents:
diff changeset
557 Constant_Present => True,
kono
parents:
diff changeset
558 Expression => Quo));
kono
parents:
diff changeset
559
kono
parents:
diff changeset
560 Append_To (Code,
kono
parents:
diff changeset
561 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
562 Defining_Identifier => Rnn,
kono
parents:
diff changeset
563 Object_Definition => New_Occurrence_Of (QR_Typ, Loc),
kono
parents:
diff changeset
564 Constant_Present => True,
kono
parents:
diff changeset
565 Expression =>
kono
parents:
diff changeset
566 Build_Rem (N,
kono
parents:
diff changeset
567 New_Occurrence_Of (Nnn, Loc),
kono
parents:
diff changeset
568 New_Occurrence_Of (Dnn, Loc))));
kono
parents:
diff changeset
569
kono
parents:
diff changeset
570 -- Case where denominator does not fit in 64 bits, so we have to
kono
parents:
diff changeset
571 -- call the runtime routine to compute the quotient and remainder
kono
parents:
diff changeset
572
kono
parents:
diff changeset
573 else
kono
parents:
diff changeset
574 Rnd := Boolean_Literals (Rounded_Result_Set (N));
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 Code := New_List (
kono
parents:
diff changeset
577 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
578 Defining_Identifier => Qnn,
kono
parents:
diff changeset
579 Object_Definition => New_Occurrence_Of (QR_Typ, Loc)),
kono
parents:
diff changeset
580
kono
parents:
diff changeset
581 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
582 Defining_Identifier => Rnn,
kono
parents:
diff changeset
583 Object_Definition => New_Occurrence_Of (QR_Typ, Loc)),
kono
parents:
diff changeset
584
kono
parents:
diff changeset
585 Make_Procedure_Call_Statement (Loc,
kono
parents:
diff changeset
586 Name => New_Occurrence_Of (RTE (RE_Double_Divide), Loc),
kono
parents:
diff changeset
587 Parameter_Associations => New_List (
kono
parents:
diff changeset
588 Build_Conversion (N, QR_Typ, X),
kono
parents:
diff changeset
589 Build_Conversion (N, QR_Typ, Y),
kono
parents:
diff changeset
590 Build_Conversion (N, QR_Typ, Z),
kono
parents:
diff changeset
591 New_Occurrence_Of (Qnn, Loc),
kono
parents:
diff changeset
592 New_Occurrence_Of (Rnn, Loc),
kono
parents:
diff changeset
593 New_Occurrence_Of (Rnd, Loc))));
kono
parents:
diff changeset
594 end if;
kono
parents:
diff changeset
595 end Build_Double_Divide_Code;
kono
parents:
diff changeset
596
kono
parents:
diff changeset
597 --------------------
kono
parents:
diff changeset
598 -- Build_Multiply --
kono
parents:
diff changeset
599 --------------------
kono
parents:
diff changeset
600
kono
parents:
diff changeset
601 function Build_Multiply (N : Node_Id; L, R : Node_Id) return Node_Id is
kono
parents:
diff changeset
602 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
603 Left_Type : constant Entity_Id := Etype (L);
kono
parents:
diff changeset
604 Right_Type : constant Entity_Id := Etype (R);
kono
parents:
diff changeset
605 Left_Size : Int;
kono
parents:
diff changeset
606 Right_Size : Int;
kono
parents:
diff changeset
607 Rsize : Int;
kono
parents:
diff changeset
608 Result_Type : Entity_Id;
kono
parents:
diff changeset
609 Rnode : Node_Id;
kono
parents:
diff changeset
610
kono
parents:
diff changeset
611 begin
kono
parents:
diff changeset
612 -- Deal with floating-point case first
kono
parents:
diff changeset
613
kono
parents:
diff changeset
614 if Is_Floating_Point_Type (Left_Type) then
kono
parents:
diff changeset
615 pragma Assert (Left_Type = Universal_Real);
kono
parents:
diff changeset
616 pragma Assert (Right_Type = Universal_Real);
kono
parents:
diff changeset
617
kono
parents:
diff changeset
618 Result_Type := Universal_Real;
kono
parents:
diff changeset
619 Rnode := Make_Op_Multiply (Loc, L, R);
kono
parents:
diff changeset
620
kono
parents:
diff changeset
621 -- Integer and fixed-point cases
kono
parents:
diff changeset
622
kono
parents:
diff changeset
623 else
kono
parents:
diff changeset
624 -- An optimization. If the right operand is the literal 1, then we
kono
parents:
diff changeset
625 -- can just return the left hand operand. Putting the optimization
kono
parents:
diff changeset
626 -- here allows us to omit the check at the call site. Similarly, if
kono
parents:
diff changeset
627 -- the left operand is the integer 1 we can return the right operand.
kono
parents:
diff changeset
628
kono
parents:
diff changeset
629 if Nkind (R) = N_Integer_Literal and then Intval (R) = 1 then
kono
parents:
diff changeset
630 return L;
kono
parents:
diff changeset
631 elsif Nkind (L) = N_Integer_Literal and then Intval (L) = 1 then
kono
parents:
diff changeset
632 return R;
kono
parents:
diff changeset
633 end if;
kono
parents:
diff changeset
634
kono
parents:
diff changeset
635 -- Otherwise we need to figure out the correct result type size
kono
parents:
diff changeset
636 -- First figure out the effective sizes of the operands. Normally
kono
parents:
diff changeset
637 -- the effective size of an operand is the RM_Size of the operand.
kono
parents:
diff changeset
638 -- But a special case arises with operands whose size is known at
kono
parents:
diff changeset
639 -- compile time. In this case, we can use the actual value of the
kono
parents:
diff changeset
640 -- operand to get its size if it would fit signed in 8 or 16 bits.
kono
parents:
diff changeset
641
kono
parents:
diff changeset
642 Left_Size := UI_To_Int (RM_Size (Left_Type));
kono
parents:
diff changeset
643
kono
parents:
diff changeset
644 if Compile_Time_Known_Value (L) then
kono
parents:
diff changeset
645 declare
kono
parents:
diff changeset
646 Val : constant Uint := Expr_Value (L);
kono
parents:
diff changeset
647 begin
kono
parents:
diff changeset
648 if Val < Int'(2 ** 7) then
kono
parents:
diff changeset
649 Left_Size := 8;
kono
parents:
diff changeset
650 elsif Val < Int'(2 ** 15) then
kono
parents:
diff changeset
651 Left_Size := 16;
kono
parents:
diff changeset
652 end if;
kono
parents:
diff changeset
653 end;
kono
parents:
diff changeset
654 end if;
kono
parents:
diff changeset
655
kono
parents:
diff changeset
656 Right_Size := UI_To_Int (RM_Size (Right_Type));
kono
parents:
diff changeset
657
kono
parents:
diff changeset
658 if Compile_Time_Known_Value (R) then
kono
parents:
diff changeset
659 declare
kono
parents:
diff changeset
660 Val : constant Uint := Expr_Value (R);
kono
parents:
diff changeset
661 begin
kono
parents:
diff changeset
662 if Val <= Int'(2 ** 7) then
kono
parents:
diff changeset
663 Right_Size := 8;
kono
parents:
diff changeset
664 elsif Val <= Int'(2 ** 15) then
kono
parents:
diff changeset
665 Right_Size := 16;
kono
parents:
diff changeset
666 end if;
kono
parents:
diff changeset
667 end;
kono
parents:
diff changeset
668 end if;
kono
parents:
diff changeset
669
kono
parents:
diff changeset
670 -- Now the result size must be at least twice the longer of
kono
parents:
diff changeset
671 -- the two sizes, to accommodate all possible results.
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 Rsize := 2 * Int'Max (Left_Size, Right_Size);
kono
parents:
diff changeset
674
kono
parents:
diff changeset
675 if Rsize <= 8 then
kono
parents:
diff changeset
676 Result_Type := Standard_Integer_8;
kono
parents:
diff changeset
677
kono
parents:
diff changeset
678 elsif Rsize <= 16 then
kono
parents:
diff changeset
679 Result_Type := Standard_Integer_16;
kono
parents:
diff changeset
680
kono
parents:
diff changeset
681 elsif Rsize <= 32 then
kono
parents:
diff changeset
682 Result_Type := Standard_Integer_32;
kono
parents:
diff changeset
683
kono
parents:
diff changeset
684 else
kono
parents:
diff changeset
685 Result_Type := Standard_Integer_64;
kono
parents:
diff changeset
686 end if;
kono
parents:
diff changeset
687
kono
parents:
diff changeset
688 Rnode :=
kono
parents:
diff changeset
689 Make_Op_Multiply (Loc,
kono
parents:
diff changeset
690 Left_Opnd => Build_Conversion (N, Result_Type, L),
kono
parents:
diff changeset
691 Right_Opnd => Build_Conversion (N, Result_Type, R));
kono
parents:
diff changeset
692 end if;
kono
parents:
diff changeset
693
kono
parents:
diff changeset
694 -- We now have a multiply node built with Result_Type set. First
kono
parents:
diff changeset
695 -- set Etype of result, as required for all Build_xxx routines
kono
parents:
diff changeset
696
kono
parents:
diff changeset
697 Set_Etype (Rnode, Base_Type (Result_Type));
kono
parents:
diff changeset
698
kono
parents:
diff changeset
699 -- Set Treat_Fixed_As_Integer if operation on fixed-point type
kono
parents:
diff changeset
700 -- since this is a literal arithmetic operation, to be performed
kono
parents:
diff changeset
701 -- by Gigi without any consideration of small values.
kono
parents:
diff changeset
702
kono
parents:
diff changeset
703 if Is_Fixed_Point_Type (Result_Type) then
kono
parents:
diff changeset
704 Set_Treat_Fixed_As_Integer (Rnode);
kono
parents:
diff changeset
705 end if;
kono
parents:
diff changeset
706
kono
parents:
diff changeset
707 return Rnode;
kono
parents:
diff changeset
708 end Build_Multiply;
kono
parents:
diff changeset
709
kono
parents:
diff changeset
710 ---------------
kono
parents:
diff changeset
711 -- Build_Rem --
kono
parents:
diff changeset
712 ---------------
kono
parents:
diff changeset
713
kono
parents:
diff changeset
714 function Build_Rem (N : Node_Id; L, R : Node_Id) return Node_Id is
kono
parents:
diff changeset
715 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
716 Left_Type : constant Entity_Id := Etype (L);
kono
parents:
diff changeset
717 Right_Type : constant Entity_Id := Etype (R);
kono
parents:
diff changeset
718 Result_Type : Entity_Id;
kono
parents:
diff changeset
719 Rnode : Node_Id;
kono
parents:
diff changeset
720
kono
parents:
diff changeset
721 begin
kono
parents:
diff changeset
722 if Left_Type = Right_Type then
kono
parents:
diff changeset
723 Result_Type := Left_Type;
kono
parents:
diff changeset
724 Rnode :=
kono
parents:
diff changeset
725 Make_Op_Rem (Loc,
kono
parents:
diff changeset
726 Left_Opnd => L,
kono
parents:
diff changeset
727 Right_Opnd => R);
kono
parents:
diff changeset
728
kono
parents:
diff changeset
729 -- If left size is larger, we do the remainder operation using the
kono
parents:
diff changeset
730 -- size of the left type (i.e. the larger of the two integer types).
kono
parents:
diff changeset
731
kono
parents:
diff changeset
732 elsif Esize (Left_Type) >= Esize (Right_Type) then
kono
parents:
diff changeset
733 Result_Type := Left_Type;
kono
parents:
diff changeset
734 Rnode :=
kono
parents:
diff changeset
735 Make_Op_Rem (Loc,
kono
parents:
diff changeset
736 Left_Opnd => L,
kono
parents:
diff changeset
737 Right_Opnd => Build_Conversion (N, Left_Type, R));
kono
parents:
diff changeset
738
kono
parents:
diff changeset
739 -- Similarly, if the right size is larger, we do the remainder
kono
parents:
diff changeset
740 -- operation using the right type.
kono
parents:
diff changeset
741
kono
parents:
diff changeset
742 else
kono
parents:
diff changeset
743 Result_Type := Right_Type;
kono
parents:
diff changeset
744 Rnode :=
kono
parents:
diff changeset
745 Make_Op_Rem (Loc,
kono
parents:
diff changeset
746 Left_Opnd => Build_Conversion (N, Right_Type, L),
kono
parents:
diff changeset
747 Right_Opnd => R);
kono
parents:
diff changeset
748 end if;
kono
parents:
diff changeset
749
kono
parents:
diff changeset
750 -- We now have an N_Op_Rem node built with Result_Type set. First
kono
parents:
diff changeset
751 -- set Etype of result, as required for all Build_xxx routines
kono
parents:
diff changeset
752
kono
parents:
diff changeset
753 Set_Etype (Rnode, Base_Type (Result_Type));
kono
parents:
diff changeset
754
kono
parents:
diff changeset
755 -- Set Treat_Fixed_As_Integer if operation on fixed-point type
kono
parents:
diff changeset
756 -- since this is a literal arithmetic operation, to be performed
kono
parents:
diff changeset
757 -- by Gigi without any consideration of small values.
kono
parents:
diff changeset
758
kono
parents:
diff changeset
759 if Is_Fixed_Point_Type (Result_Type) then
kono
parents:
diff changeset
760 Set_Treat_Fixed_As_Integer (Rnode);
kono
parents:
diff changeset
761 end if;
kono
parents:
diff changeset
762
kono
parents:
diff changeset
763 -- One more check. We did the rem operation using the larger of the
kono
parents:
diff changeset
764 -- two types, which is reasonable. However, in the case where the
kono
parents:
diff changeset
765 -- two types have unequal sizes, it is impossible for the result of
kono
parents:
diff changeset
766 -- a remainder operation to be larger than the smaller of the two
kono
parents:
diff changeset
767 -- types, so we can put a conversion round the result to keep the
kono
parents:
diff changeset
768 -- evolving operation size as small as possible.
kono
parents:
diff changeset
769
kono
parents:
diff changeset
770 if Esize (Left_Type) >= Esize (Right_Type) then
kono
parents:
diff changeset
771 Rnode := Build_Conversion (N, Right_Type, Rnode);
kono
parents:
diff changeset
772 elsif Esize (Right_Type) >= Esize (Left_Type) then
kono
parents:
diff changeset
773 Rnode := Build_Conversion (N, Left_Type, Rnode);
kono
parents:
diff changeset
774 end if;
kono
parents:
diff changeset
775
kono
parents:
diff changeset
776 return Rnode;
kono
parents:
diff changeset
777 end Build_Rem;
kono
parents:
diff changeset
778
kono
parents:
diff changeset
779 -------------------------
kono
parents:
diff changeset
780 -- Build_Scaled_Divide --
kono
parents:
diff changeset
781 -------------------------
kono
parents:
diff changeset
782
kono
parents:
diff changeset
783 function Build_Scaled_Divide
kono
parents:
diff changeset
784 (N : Node_Id;
kono
parents:
diff changeset
785 X, Y, Z : Node_Id) return Node_Id
kono
parents:
diff changeset
786 is
kono
parents:
diff changeset
787 X_Size : constant Nat := UI_To_Int (Esize (Etype (X)));
kono
parents:
diff changeset
788 Y_Size : constant Nat := UI_To_Int (Esize (Etype (Y)));
kono
parents:
diff changeset
789 Expr : Node_Id;
kono
parents:
diff changeset
790
kono
parents:
diff changeset
791 begin
kono
parents:
diff changeset
792 -- If numerator fits in 64 bits, we can build the operations directly
kono
parents:
diff changeset
793 -- without causing any intermediate overflow, so that's what we do.
kono
parents:
diff changeset
794
kono
parents:
diff changeset
795 if Nat'Max (X_Size, Y_Size) <= 32 then
kono
parents:
diff changeset
796 return
kono
parents:
diff changeset
797 Build_Divide (N, Build_Multiply (N, X, Y), Z);
kono
parents:
diff changeset
798
kono
parents:
diff changeset
799 -- Otherwise we use the runtime routine
kono
parents:
diff changeset
800
kono
parents:
diff changeset
801 -- [Qnn : Integer_64,
kono
parents:
diff changeset
802 -- Rnn : Integer_64;
kono
parents:
diff changeset
803 -- Scaled_Divide (X, Y, Z, Qnn, Rnn, Round);
kono
parents:
diff changeset
804 -- Qnn]
kono
parents:
diff changeset
805
kono
parents:
diff changeset
806 else
kono
parents:
diff changeset
807 declare
kono
parents:
diff changeset
808 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
809 Qnn : Entity_Id;
kono
parents:
diff changeset
810 Rnn : Entity_Id;
kono
parents:
diff changeset
811 Code : List_Id;
kono
parents:
diff changeset
812
kono
parents:
diff changeset
813 pragma Warnings (Off, Rnn);
kono
parents:
diff changeset
814
kono
parents:
diff changeset
815 begin
kono
parents:
diff changeset
816 Build_Scaled_Divide_Code (N, X, Y, Z, Qnn, Rnn, Code);
kono
parents:
diff changeset
817 Insert_Actions (N, Code);
kono
parents:
diff changeset
818 Expr := New_Occurrence_Of (Qnn, Loc);
kono
parents:
diff changeset
819
kono
parents:
diff changeset
820 -- Set type of result in case used elsewhere (see note at start)
kono
parents:
diff changeset
821
kono
parents:
diff changeset
822 Set_Etype (Expr, Etype (Qnn));
kono
parents:
diff changeset
823 return Expr;
kono
parents:
diff changeset
824 end;
kono
parents:
diff changeset
825 end if;
kono
parents:
diff changeset
826 end Build_Scaled_Divide;
kono
parents:
diff changeset
827
kono
parents:
diff changeset
828 ------------------------------
kono
parents:
diff changeset
829 -- Build_Scaled_Divide_Code --
kono
parents:
diff changeset
830 ------------------------------
kono
parents:
diff changeset
831
kono
parents:
diff changeset
832 -- If the numerator can be computed in 64-bits, we build
kono
parents:
diff changeset
833
kono
parents:
diff changeset
834 -- [Nnn : constant typ := typ (X) * typ (Y);
kono
parents:
diff changeset
835 -- Dnn : constant typ := typ (Z)
kono
parents:
diff changeset
836 -- Qnn : constant typ := Nnn / Dnn;
kono
parents:
diff changeset
837 -- Rnn : constant typ := Nnn / Dnn;
kono
parents:
diff changeset
838
kono
parents:
diff changeset
839 -- If the numerator cannot be computed in 64 bits, we build
kono
parents:
diff changeset
840
kono
parents:
diff changeset
841 -- [Qnn : Interfaces.Integer_64;
kono
parents:
diff changeset
842 -- Rnn : Interfaces.Integer_64;
kono
parents:
diff changeset
843 -- Scaled_Divide (X, Y, Z, Qnn, Rnn, Round);]
kono
parents:
diff changeset
844
kono
parents:
diff changeset
845 procedure Build_Scaled_Divide_Code
kono
parents:
diff changeset
846 (N : Node_Id;
kono
parents:
diff changeset
847 X, Y, Z : Node_Id;
kono
parents:
diff changeset
848 Qnn, Rnn : out Entity_Id;
kono
parents:
diff changeset
849 Code : out List_Id)
kono
parents:
diff changeset
850 is
kono
parents:
diff changeset
851 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
852
kono
parents:
diff changeset
853 X_Size : constant Nat := UI_To_Int (Esize (Etype (X)));
kono
parents:
diff changeset
854 Y_Size : constant Nat := UI_To_Int (Esize (Etype (Y)));
kono
parents:
diff changeset
855 Z_Size : constant Nat := UI_To_Int (Esize (Etype (Z)));
kono
parents:
diff changeset
856
kono
parents:
diff changeset
857 QR_Siz : Nat;
kono
parents:
diff changeset
858 QR_Typ : Entity_Id;
kono
parents:
diff changeset
859
kono
parents:
diff changeset
860 Nnn : Entity_Id;
kono
parents:
diff changeset
861 Dnn : Entity_Id;
kono
parents:
diff changeset
862
kono
parents:
diff changeset
863 Quo : Node_Id;
kono
parents:
diff changeset
864 Rnd : Entity_Id;
kono
parents:
diff changeset
865
kono
parents:
diff changeset
866 begin
kono
parents:
diff changeset
867 -- Find type that will allow computation of numerator
kono
parents:
diff changeset
868
kono
parents:
diff changeset
869 QR_Siz := Nat'Max (X_Size, 2 * Nat'Max (Y_Size, Z_Size));
kono
parents:
diff changeset
870
kono
parents:
diff changeset
871 if QR_Siz <= 16 then
kono
parents:
diff changeset
872 QR_Typ := Standard_Integer_16;
kono
parents:
diff changeset
873 elsif QR_Siz <= 32 then
kono
parents:
diff changeset
874 QR_Typ := Standard_Integer_32;
kono
parents:
diff changeset
875 elsif QR_Siz <= 64 then
kono
parents:
diff changeset
876 QR_Typ := Standard_Integer_64;
kono
parents:
diff changeset
877
kono
parents:
diff changeset
878 -- For more than 64, bits, we use the 64-bit integer defined in
kono
parents:
diff changeset
879 -- Interfaces, so that it can be handled by the runtime routine.
kono
parents:
diff changeset
880
kono
parents:
diff changeset
881 else
kono
parents:
diff changeset
882 QR_Typ := RTE (RE_Integer_64);
kono
parents:
diff changeset
883 end if;
kono
parents:
diff changeset
884
kono
parents:
diff changeset
885 -- Define quotient and remainder, and set their Etypes, so
kono
parents:
diff changeset
886 -- that they can be picked up by Build_xxx routines.
kono
parents:
diff changeset
887
kono
parents:
diff changeset
888 Qnn := Make_Temporary (Loc, 'S');
kono
parents:
diff changeset
889 Rnn := Make_Temporary (Loc, 'R');
kono
parents:
diff changeset
890
kono
parents:
diff changeset
891 Set_Etype (Qnn, QR_Typ);
kono
parents:
diff changeset
892 Set_Etype (Rnn, QR_Typ);
kono
parents:
diff changeset
893
kono
parents:
diff changeset
894 -- Case that we can compute the numerator in 64 bits
kono
parents:
diff changeset
895
kono
parents:
diff changeset
896 if QR_Siz <= 64 then
kono
parents:
diff changeset
897 Nnn := Make_Temporary (Loc, 'N');
kono
parents:
diff changeset
898 Dnn := Make_Temporary (Loc, 'D');
kono
parents:
diff changeset
899
kono
parents:
diff changeset
900 -- Set Etypes, so that they can be picked up by New_Occurrence_Of
kono
parents:
diff changeset
901
kono
parents:
diff changeset
902 Set_Etype (Nnn, QR_Typ);
kono
parents:
diff changeset
903 Set_Etype (Dnn, QR_Typ);
kono
parents:
diff changeset
904
kono
parents:
diff changeset
905 Code := New_List (
kono
parents:
diff changeset
906 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
907 Defining_Identifier => Nnn,
kono
parents:
diff changeset
908 Object_Definition => New_Occurrence_Of (QR_Typ, Loc),
kono
parents:
diff changeset
909 Constant_Present => True,
kono
parents:
diff changeset
910 Expression =>
kono
parents:
diff changeset
911 Build_Multiply (N,
kono
parents:
diff changeset
912 Build_Conversion (N, QR_Typ, X),
kono
parents:
diff changeset
913 Build_Conversion (N, QR_Typ, Y))),
kono
parents:
diff changeset
914
kono
parents:
diff changeset
915 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
916 Defining_Identifier => Dnn,
kono
parents:
diff changeset
917 Object_Definition => New_Occurrence_Of (QR_Typ, Loc),
kono
parents:
diff changeset
918 Constant_Present => True,
kono
parents:
diff changeset
919 Expression => Build_Conversion (N, QR_Typ, Z)));
kono
parents:
diff changeset
920
kono
parents:
diff changeset
921 Quo :=
kono
parents:
diff changeset
922 Build_Divide (N,
kono
parents:
diff changeset
923 New_Occurrence_Of (Nnn, Loc),
kono
parents:
diff changeset
924 New_Occurrence_Of (Dnn, Loc));
kono
parents:
diff changeset
925
kono
parents:
diff changeset
926 Append_To (Code,
kono
parents:
diff changeset
927 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
928 Defining_Identifier => Qnn,
kono
parents:
diff changeset
929 Object_Definition => New_Occurrence_Of (QR_Typ, Loc),
kono
parents:
diff changeset
930 Constant_Present => True,
kono
parents:
diff changeset
931 Expression => Quo));
kono
parents:
diff changeset
932
kono
parents:
diff changeset
933 Append_To (Code,
kono
parents:
diff changeset
934 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
935 Defining_Identifier => Rnn,
kono
parents:
diff changeset
936 Object_Definition => New_Occurrence_Of (QR_Typ, Loc),
kono
parents:
diff changeset
937 Constant_Present => True,
kono
parents:
diff changeset
938 Expression =>
kono
parents:
diff changeset
939 Build_Rem (N,
kono
parents:
diff changeset
940 New_Occurrence_Of (Nnn, Loc),
kono
parents:
diff changeset
941 New_Occurrence_Of (Dnn, Loc))));
kono
parents:
diff changeset
942
kono
parents:
diff changeset
943 -- Case where numerator does not fit in 64 bits, so we have to
kono
parents:
diff changeset
944 -- call the runtime routine to compute the quotient and remainder
kono
parents:
diff changeset
945
kono
parents:
diff changeset
946 else
kono
parents:
diff changeset
947 Rnd := Boolean_Literals (Rounded_Result_Set (N));
kono
parents:
diff changeset
948
kono
parents:
diff changeset
949 Code := New_List (
kono
parents:
diff changeset
950 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
951 Defining_Identifier => Qnn,
kono
parents:
diff changeset
952 Object_Definition => New_Occurrence_Of (QR_Typ, Loc)),
kono
parents:
diff changeset
953
kono
parents:
diff changeset
954 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
955 Defining_Identifier => Rnn,
kono
parents:
diff changeset
956 Object_Definition => New_Occurrence_Of (QR_Typ, Loc)),
kono
parents:
diff changeset
957
kono
parents:
diff changeset
958 Make_Procedure_Call_Statement (Loc,
kono
parents:
diff changeset
959 Name => New_Occurrence_Of (RTE (RE_Scaled_Divide), Loc),
kono
parents:
diff changeset
960 Parameter_Associations => New_List (
kono
parents:
diff changeset
961 Build_Conversion (N, QR_Typ, X),
kono
parents:
diff changeset
962 Build_Conversion (N, QR_Typ, Y),
kono
parents:
diff changeset
963 Build_Conversion (N, QR_Typ, Z),
kono
parents:
diff changeset
964 New_Occurrence_Of (Qnn, Loc),
kono
parents:
diff changeset
965 New_Occurrence_Of (Rnn, Loc),
kono
parents:
diff changeset
966 New_Occurrence_Of (Rnd, Loc))));
kono
parents:
diff changeset
967 end if;
kono
parents:
diff changeset
968
kono
parents:
diff changeset
969 -- Set type of result, for use in caller
kono
parents:
diff changeset
970
kono
parents:
diff changeset
971 Set_Etype (Qnn, QR_Typ);
kono
parents:
diff changeset
972 end Build_Scaled_Divide_Code;
kono
parents:
diff changeset
973
kono
parents:
diff changeset
974 ---------------------------
kono
parents:
diff changeset
975 -- Do_Divide_Fixed_Fixed --
kono
parents:
diff changeset
976 ---------------------------
kono
parents:
diff changeset
977
kono
parents:
diff changeset
978 -- We have:
kono
parents:
diff changeset
979
kono
parents:
diff changeset
980 -- (Result_Value * Result_Small) =
kono
parents:
diff changeset
981 -- (Left_Value * Left_Small) / (Right_Value * Right_Small)
kono
parents:
diff changeset
982
kono
parents:
diff changeset
983 -- Result_Value = (Left_Value / Right_Value) *
kono
parents:
diff changeset
984 -- (Left_Small / (Right_Small * Result_Small));
kono
parents:
diff changeset
985
kono
parents:
diff changeset
986 -- we can do the operation in integer arithmetic if this fraction is an
kono
parents:
diff changeset
987 -- integer or the reciprocal of an integer, as detailed in (RM G.2.3(21)).
kono
parents:
diff changeset
988 -- Otherwise the result is in the close result set and our approach is to
kono
parents:
diff changeset
989 -- use floating-point to compute this close result.
kono
parents:
diff changeset
990
kono
parents:
diff changeset
991 procedure Do_Divide_Fixed_Fixed (N : Node_Id) is
kono
parents:
diff changeset
992 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
993 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
994 Left_Type : constant Entity_Id := Etype (Left);
kono
parents:
diff changeset
995 Right_Type : constant Entity_Id := Etype (Right);
kono
parents:
diff changeset
996 Result_Type : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
997 Right_Small : constant Ureal := Small_Value (Right_Type);
kono
parents:
diff changeset
998 Left_Small : constant Ureal := Small_Value (Left_Type);
kono
parents:
diff changeset
999
kono
parents:
diff changeset
1000 Result_Small : Ureal;
kono
parents:
diff changeset
1001 Frac : Ureal;
kono
parents:
diff changeset
1002 Frac_Num : Uint;
kono
parents:
diff changeset
1003 Frac_Den : Uint;
kono
parents:
diff changeset
1004 Lit_Int : Node_Id;
kono
parents:
diff changeset
1005
kono
parents:
diff changeset
1006 begin
kono
parents:
diff changeset
1007 -- Rounding is required if the result is integral
kono
parents:
diff changeset
1008
kono
parents:
diff changeset
1009 if Is_Integer_Type (Result_Type) then
kono
parents:
diff changeset
1010 Set_Rounded_Result (N);
kono
parents:
diff changeset
1011 end if;
kono
parents:
diff changeset
1012
kono
parents:
diff changeset
1013 -- Get result small. If the result is an integer, treat it as though
kono
parents:
diff changeset
1014 -- it had a small of 1.0, all other processing is identical.
kono
parents:
diff changeset
1015
kono
parents:
diff changeset
1016 if Is_Integer_Type (Result_Type) then
kono
parents:
diff changeset
1017 Result_Small := Ureal_1;
kono
parents:
diff changeset
1018 else
kono
parents:
diff changeset
1019 Result_Small := Small_Value (Result_Type);
kono
parents:
diff changeset
1020 end if;
kono
parents:
diff changeset
1021
kono
parents:
diff changeset
1022 -- Get small ratio
kono
parents:
diff changeset
1023
kono
parents:
diff changeset
1024 Frac := Left_Small / (Right_Small * Result_Small);
kono
parents:
diff changeset
1025 Frac_Num := Norm_Num (Frac);
kono
parents:
diff changeset
1026 Frac_Den := Norm_Den (Frac);
kono
parents:
diff changeset
1027
kono
parents:
diff changeset
1028 -- If the fraction is an integer, then we get the result by multiplying
kono
parents:
diff changeset
1029 -- the left operand by the integer, and then dividing by the right
kono
parents:
diff changeset
1030 -- operand (the order is important, if we did the divide first, we
kono
parents:
diff changeset
1031 -- would lose precision).
kono
parents:
diff changeset
1032
kono
parents:
diff changeset
1033 if Frac_Den = 1 then
kono
parents:
diff changeset
1034 Lit_Int := Integer_Literal (N, Frac_Num); -- always positive
kono
parents:
diff changeset
1035
kono
parents:
diff changeset
1036 if Present (Lit_Int) then
kono
parents:
diff changeset
1037 Set_Result (N, Build_Scaled_Divide (N, Left, Lit_Int, Right));
kono
parents:
diff changeset
1038 return;
kono
parents:
diff changeset
1039 end if;
kono
parents:
diff changeset
1040
kono
parents:
diff changeset
1041 -- If the fraction is the reciprocal of an integer, then we get the
kono
parents:
diff changeset
1042 -- result by first multiplying the divisor by the integer, and then
kono
parents:
diff changeset
1043 -- doing the division with the adjusted divisor.
kono
parents:
diff changeset
1044
kono
parents:
diff changeset
1045 -- Note: this is much better than doing two divisions: multiplications
kono
parents:
diff changeset
1046 -- are much faster than divisions (and certainly faster than rounded
kono
parents:
diff changeset
1047 -- divisions), and we don't get inaccuracies from double rounding.
kono
parents:
diff changeset
1048
kono
parents:
diff changeset
1049 elsif Frac_Num = 1 then
kono
parents:
diff changeset
1050 Lit_Int := Integer_Literal (N, Frac_Den); -- always positive
kono
parents:
diff changeset
1051
kono
parents:
diff changeset
1052 if Present (Lit_Int) then
kono
parents:
diff changeset
1053 Set_Result (N, Build_Double_Divide (N, Left, Right, Lit_Int));
kono
parents:
diff changeset
1054 return;
kono
parents:
diff changeset
1055 end if;
kono
parents:
diff changeset
1056 end if;
kono
parents:
diff changeset
1057
kono
parents:
diff changeset
1058 -- If we fall through, we use floating-point to compute the result
kono
parents:
diff changeset
1059
kono
parents:
diff changeset
1060 Set_Result (N,
kono
parents:
diff changeset
1061 Build_Multiply (N,
kono
parents:
diff changeset
1062 Build_Divide (N, Fpt_Value (Left), Fpt_Value (Right)),
kono
parents:
diff changeset
1063 Real_Literal (N, Frac)));
kono
parents:
diff changeset
1064 end Do_Divide_Fixed_Fixed;
kono
parents:
diff changeset
1065
kono
parents:
diff changeset
1066 -------------------------------
kono
parents:
diff changeset
1067 -- Do_Divide_Fixed_Universal --
kono
parents:
diff changeset
1068 -------------------------------
kono
parents:
diff changeset
1069
kono
parents:
diff changeset
1070 -- We have:
kono
parents:
diff changeset
1071
kono
parents:
diff changeset
1072 -- (Result_Value * Result_Small) = (Left_Value * Left_Small) / Lit_Value;
kono
parents:
diff changeset
1073 -- Result_Value = Left_Value * Left_Small /(Lit_Value * Result_Small);
kono
parents:
diff changeset
1074
kono
parents:
diff changeset
1075 -- The result is required to be in the perfect result set if the literal
kono
parents:
diff changeset
1076 -- can be factored so that the resulting small ratio is an integer or the
kono
parents:
diff changeset
1077 -- reciprocal of an integer (RM G.2.3(21-22)). We now give a detailed
kono
parents:
diff changeset
1078 -- analysis of these RM requirements:
kono
parents:
diff changeset
1079
kono
parents:
diff changeset
1080 -- We must factor the literal, finding an integer K:
kono
parents:
diff changeset
1081
kono
parents:
diff changeset
1082 -- Lit_Value = K * Right_Small
kono
parents:
diff changeset
1083 -- Right_Small = Lit_Value / K
kono
parents:
diff changeset
1084
kono
parents:
diff changeset
1085 -- such that the small ratio:
kono
parents:
diff changeset
1086
kono
parents:
diff changeset
1087 -- Left_Small
kono
parents:
diff changeset
1088 -- ------------------------------
kono
parents:
diff changeset
1089 -- (Lit_Value / K) * Result_Small
kono
parents:
diff changeset
1090
kono
parents:
diff changeset
1091 -- Left_Small
kono
parents:
diff changeset
1092 -- = ------------------------ * K
kono
parents:
diff changeset
1093 -- Lit_Value * Result_Small
kono
parents:
diff changeset
1094
kono
parents:
diff changeset
1095 -- is an integer or the reciprocal of an integer, and for
kono
parents:
diff changeset
1096 -- implementation efficiency we need the smallest such K.
kono
parents:
diff changeset
1097
kono
parents:
diff changeset
1098 -- First we reduce the left fraction to lowest terms
kono
parents:
diff changeset
1099
kono
parents:
diff changeset
1100 -- If numerator = 1, then for K = 1, the small ratio is the reciprocal
kono
parents:
diff changeset
1101 -- of an integer, and this is clearly the minimum K case, so set K = 1,
kono
parents:
diff changeset
1102 -- Right_Small = Lit_Value.
kono
parents:
diff changeset
1103
kono
parents:
diff changeset
1104 -- If numerator > 1, then set K to the denominator of the fraction so
kono
parents:
diff changeset
1105 -- that the resulting small ratio is an integer (the numerator value).
kono
parents:
diff changeset
1106
kono
parents:
diff changeset
1107 procedure Do_Divide_Fixed_Universal (N : Node_Id) is
kono
parents:
diff changeset
1108 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
1109 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
1110 Left_Type : constant Entity_Id := Etype (Left);
kono
parents:
diff changeset
1111 Result_Type : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
1112 Left_Small : constant Ureal := Small_Value (Left_Type);
kono
parents:
diff changeset
1113 Lit_Value : constant Ureal := Realval (Right);
kono
parents:
diff changeset
1114
kono
parents:
diff changeset
1115 Result_Small : Ureal;
kono
parents:
diff changeset
1116 Frac : Ureal;
kono
parents:
diff changeset
1117 Frac_Num : Uint;
kono
parents:
diff changeset
1118 Frac_Den : Uint;
kono
parents:
diff changeset
1119 Lit_K : Node_Id;
kono
parents:
diff changeset
1120 Lit_Int : Node_Id;
kono
parents:
diff changeset
1121
kono
parents:
diff changeset
1122 begin
kono
parents:
diff changeset
1123 -- Get result small. If the result is an integer, treat it as though
kono
parents:
diff changeset
1124 -- it had a small of 1.0, all other processing is identical.
kono
parents:
diff changeset
1125
kono
parents:
diff changeset
1126 if Is_Integer_Type (Result_Type) then
kono
parents:
diff changeset
1127 Result_Small := Ureal_1;
kono
parents:
diff changeset
1128 else
kono
parents:
diff changeset
1129 Result_Small := Small_Value (Result_Type);
kono
parents:
diff changeset
1130 end if;
kono
parents:
diff changeset
1131
kono
parents:
diff changeset
1132 -- Determine if literal can be rewritten successfully
kono
parents:
diff changeset
1133
kono
parents:
diff changeset
1134 Frac := Left_Small / (Lit_Value * Result_Small);
kono
parents:
diff changeset
1135 Frac_Num := Norm_Num (Frac);
kono
parents:
diff changeset
1136 Frac_Den := Norm_Den (Frac);
kono
parents:
diff changeset
1137
kono
parents:
diff changeset
1138 -- Case where fraction is the reciprocal of an integer (K = 1, integer
kono
parents:
diff changeset
1139 -- = denominator). If this integer is not too large, this is the case
kono
parents:
diff changeset
1140 -- where the result can be obtained by dividing by this integer value.
kono
parents:
diff changeset
1141
kono
parents:
diff changeset
1142 if Frac_Num = 1 then
kono
parents:
diff changeset
1143 Lit_Int := Integer_Literal (N, Frac_Den, UR_Is_Negative (Frac));
kono
parents:
diff changeset
1144
kono
parents:
diff changeset
1145 if Present (Lit_Int) then
kono
parents:
diff changeset
1146 Set_Result (N, Build_Divide (N, Left, Lit_Int));
kono
parents:
diff changeset
1147 return;
kono
parents:
diff changeset
1148 end if;
kono
parents:
diff changeset
1149
kono
parents:
diff changeset
1150 -- Case where we choose K to make fraction an integer (K = denominator
kono
parents:
diff changeset
1151 -- of fraction, integer = numerator of fraction). If both K and the
kono
parents:
diff changeset
1152 -- numerator are small enough, this is the case where the result can
kono
parents:
diff changeset
1153 -- be obtained by first multiplying by the integer value and then
kono
parents:
diff changeset
1154 -- dividing by K (the order is important, if we divided first, we
kono
parents:
diff changeset
1155 -- would lose precision).
kono
parents:
diff changeset
1156
kono
parents:
diff changeset
1157 else
kono
parents:
diff changeset
1158 Lit_Int := Integer_Literal (N, Frac_Num, UR_Is_Negative (Frac));
kono
parents:
diff changeset
1159 Lit_K := Integer_Literal (N, Frac_Den, False);
kono
parents:
diff changeset
1160
kono
parents:
diff changeset
1161 if Present (Lit_Int) and then Present (Lit_K) then
kono
parents:
diff changeset
1162 Set_Result (N, Build_Scaled_Divide (N, Left, Lit_Int, Lit_K));
kono
parents:
diff changeset
1163 return;
kono
parents:
diff changeset
1164 end if;
kono
parents:
diff changeset
1165 end if;
kono
parents:
diff changeset
1166
kono
parents:
diff changeset
1167 -- Fall through if the literal cannot be successfully rewritten, or if
kono
parents:
diff changeset
1168 -- the small ratio is out of range of integer arithmetic. In the former
kono
parents:
diff changeset
1169 -- case it is fine to use floating-point to get the close result set,
kono
parents:
diff changeset
1170 -- and in the latter case, it means that the result is zero or raises
kono
parents:
diff changeset
1171 -- constraint error, and we can do that accurately in floating-point.
kono
parents:
diff changeset
1172
kono
parents:
diff changeset
1173 -- If we end up using floating-point, then we take the right integer
kono
parents:
diff changeset
1174 -- to be one, and its small to be the value of the original right real
kono
parents:
diff changeset
1175 -- literal. That way, we need only one floating-point multiplication.
kono
parents:
diff changeset
1176
kono
parents:
diff changeset
1177 Set_Result (N,
kono
parents:
diff changeset
1178 Build_Multiply (N, Fpt_Value (Left), Real_Literal (N, Frac)));
kono
parents:
diff changeset
1179 end Do_Divide_Fixed_Universal;
kono
parents:
diff changeset
1180
kono
parents:
diff changeset
1181 -------------------------------
kono
parents:
diff changeset
1182 -- Do_Divide_Universal_Fixed --
kono
parents:
diff changeset
1183 -------------------------------
kono
parents:
diff changeset
1184
kono
parents:
diff changeset
1185 -- We have:
kono
parents:
diff changeset
1186
kono
parents:
diff changeset
1187 -- (Result_Value * Result_Small) =
kono
parents:
diff changeset
1188 -- Lit_Value / (Right_Value * Right_Small)
kono
parents:
diff changeset
1189 -- Result_Value =
kono
parents:
diff changeset
1190 -- (Lit_Value / (Right_Small * Result_Small)) / Right_Value
kono
parents:
diff changeset
1191
kono
parents:
diff changeset
1192 -- The result is required to be in the perfect result set if the literal
kono
parents:
diff changeset
1193 -- can be factored so that the resulting small ratio is an integer or the
kono
parents:
diff changeset
1194 -- reciprocal of an integer (RM G.2.3(21-22)). We now give a detailed
kono
parents:
diff changeset
1195 -- analysis of these RM requirements:
kono
parents:
diff changeset
1196
kono
parents:
diff changeset
1197 -- We must factor the literal, finding an integer K:
kono
parents:
diff changeset
1198
kono
parents:
diff changeset
1199 -- Lit_Value = K * Left_Small
kono
parents:
diff changeset
1200 -- Left_Small = Lit_Value / K
kono
parents:
diff changeset
1201
kono
parents:
diff changeset
1202 -- such that the small ratio:
kono
parents:
diff changeset
1203
kono
parents:
diff changeset
1204 -- (Lit_Value / K)
kono
parents:
diff changeset
1205 -- --------------------------
kono
parents:
diff changeset
1206 -- Right_Small * Result_Small
kono
parents:
diff changeset
1207
kono
parents:
diff changeset
1208 -- Lit_Value 1
kono
parents:
diff changeset
1209 -- = -------------------------- * -
kono
parents:
diff changeset
1210 -- Right_Small * Result_Small K
kono
parents:
diff changeset
1211
kono
parents:
diff changeset
1212 -- is an integer or the reciprocal of an integer, and for
kono
parents:
diff changeset
1213 -- implementation efficiency we need the smallest such K.
kono
parents:
diff changeset
1214
kono
parents:
diff changeset
1215 -- First we reduce the left fraction to lowest terms
kono
parents:
diff changeset
1216
kono
parents:
diff changeset
1217 -- If denominator = 1, then for K = 1, the small ratio is an integer
kono
parents:
diff changeset
1218 -- (the numerator) and this is clearly the minimum K case, so set K = 1,
kono
parents:
diff changeset
1219 -- and Left_Small = Lit_Value.
kono
parents:
diff changeset
1220
kono
parents:
diff changeset
1221 -- If denominator > 1, then set K to the numerator of the fraction so
kono
parents:
diff changeset
1222 -- that the resulting small ratio is the reciprocal of an integer (the
kono
parents:
diff changeset
1223 -- numerator value).
kono
parents:
diff changeset
1224
kono
parents:
diff changeset
1225 procedure Do_Divide_Universal_Fixed (N : Node_Id) is
kono
parents:
diff changeset
1226 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
1227 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
1228 Right_Type : constant Entity_Id := Etype (Right);
kono
parents:
diff changeset
1229 Result_Type : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
1230 Right_Small : constant Ureal := Small_Value (Right_Type);
kono
parents:
diff changeset
1231 Lit_Value : constant Ureal := Realval (Left);
kono
parents:
diff changeset
1232
kono
parents:
diff changeset
1233 Result_Small : Ureal;
kono
parents:
diff changeset
1234 Frac : Ureal;
kono
parents:
diff changeset
1235 Frac_Num : Uint;
kono
parents:
diff changeset
1236 Frac_Den : Uint;
kono
parents:
diff changeset
1237 Lit_K : Node_Id;
kono
parents:
diff changeset
1238 Lit_Int : Node_Id;
kono
parents:
diff changeset
1239
kono
parents:
diff changeset
1240 begin
kono
parents:
diff changeset
1241 -- Get result small. If the result is an integer, treat it as though
kono
parents:
diff changeset
1242 -- it had a small of 1.0, all other processing is identical.
kono
parents:
diff changeset
1243
kono
parents:
diff changeset
1244 if Is_Integer_Type (Result_Type) then
kono
parents:
diff changeset
1245 Result_Small := Ureal_1;
kono
parents:
diff changeset
1246 else
kono
parents:
diff changeset
1247 Result_Small := Small_Value (Result_Type);
kono
parents:
diff changeset
1248 end if;
kono
parents:
diff changeset
1249
kono
parents:
diff changeset
1250 -- Determine if literal can be rewritten successfully
kono
parents:
diff changeset
1251
kono
parents:
diff changeset
1252 Frac := Lit_Value / (Right_Small * Result_Small);
kono
parents:
diff changeset
1253 Frac_Num := Norm_Num (Frac);
kono
parents:
diff changeset
1254 Frac_Den := Norm_Den (Frac);
kono
parents:
diff changeset
1255
kono
parents:
diff changeset
1256 -- Case where fraction is an integer (K = 1, integer = numerator). If
kono
parents:
diff changeset
1257 -- this integer is not too large, this is the case where the result
kono
parents:
diff changeset
1258 -- can be obtained by dividing this integer by the right operand.
kono
parents:
diff changeset
1259
kono
parents:
diff changeset
1260 if Frac_Den = 1 then
kono
parents:
diff changeset
1261 Lit_Int := Integer_Literal (N, Frac_Num, UR_Is_Negative (Frac));
kono
parents:
diff changeset
1262
kono
parents:
diff changeset
1263 if Present (Lit_Int) then
kono
parents:
diff changeset
1264 Set_Result (N, Build_Divide (N, Lit_Int, Right));
kono
parents:
diff changeset
1265 return;
kono
parents:
diff changeset
1266 end if;
kono
parents:
diff changeset
1267
kono
parents:
diff changeset
1268 -- Case where we choose K to make the fraction the reciprocal of an
kono
parents:
diff changeset
1269 -- integer (K = numerator of fraction, integer = numerator of fraction).
kono
parents:
diff changeset
1270 -- If both K and the integer are small enough, this is the case where
kono
parents:
diff changeset
1271 -- the result can be obtained by multiplying the right operand by K
kono
parents:
diff changeset
1272 -- and then dividing by the integer value. The order of the operations
kono
parents:
diff changeset
1273 -- is important (if we divided first, we would lose precision).
kono
parents:
diff changeset
1274
kono
parents:
diff changeset
1275 else
kono
parents:
diff changeset
1276 Lit_Int := Integer_Literal (N, Frac_Den, UR_Is_Negative (Frac));
kono
parents:
diff changeset
1277 Lit_K := Integer_Literal (N, Frac_Num, False);
kono
parents:
diff changeset
1278
kono
parents:
diff changeset
1279 if Present (Lit_Int) and then Present (Lit_K) then
kono
parents:
diff changeset
1280 Set_Result (N, Build_Double_Divide (N, Lit_K, Right, Lit_Int));
kono
parents:
diff changeset
1281 return;
kono
parents:
diff changeset
1282 end if;
kono
parents:
diff changeset
1283 end if;
kono
parents:
diff changeset
1284
kono
parents:
diff changeset
1285 -- Fall through if the literal cannot be successfully rewritten, or if
kono
parents:
diff changeset
1286 -- the small ratio is out of range of integer arithmetic. In the former
kono
parents:
diff changeset
1287 -- case it is fine to use floating-point to get the close result set,
kono
parents:
diff changeset
1288 -- and in the latter case, it means that the result is zero or raises
kono
parents:
diff changeset
1289 -- constraint error, and we can do that accurately in floating-point.
kono
parents:
diff changeset
1290
kono
parents:
diff changeset
1291 -- If we end up using floating-point, then we take the right integer
kono
parents:
diff changeset
1292 -- to be one, and its small to be the value of the original right real
kono
parents:
diff changeset
1293 -- literal. That way, we need only one floating-point division.
kono
parents:
diff changeset
1294
kono
parents:
diff changeset
1295 Set_Result (N,
kono
parents:
diff changeset
1296 Build_Divide (N, Real_Literal (N, Frac), Fpt_Value (Right)));
kono
parents:
diff changeset
1297 end Do_Divide_Universal_Fixed;
kono
parents:
diff changeset
1298
kono
parents:
diff changeset
1299 -----------------------------
kono
parents:
diff changeset
1300 -- Do_Multiply_Fixed_Fixed --
kono
parents:
diff changeset
1301 -----------------------------
kono
parents:
diff changeset
1302
kono
parents:
diff changeset
1303 -- We have:
kono
parents:
diff changeset
1304
kono
parents:
diff changeset
1305 -- (Result_Value * Result_Small) =
kono
parents:
diff changeset
1306 -- (Left_Value * Left_Small) * (Right_Value * Right_Small)
kono
parents:
diff changeset
1307
kono
parents:
diff changeset
1308 -- Result_Value = (Left_Value * Right_Value) *
kono
parents:
diff changeset
1309 -- (Left_Small * Right_Small) / Result_Small;
kono
parents:
diff changeset
1310
kono
parents:
diff changeset
1311 -- we can do the operation in integer arithmetic if this fraction is an
kono
parents:
diff changeset
1312 -- integer or the reciprocal of an integer, as detailed in (RM G.2.3(21)).
kono
parents:
diff changeset
1313 -- Otherwise the result is in the close result set and our approach is to
kono
parents:
diff changeset
1314 -- use floating-point to compute this close result.
kono
parents:
diff changeset
1315
kono
parents:
diff changeset
1316 procedure Do_Multiply_Fixed_Fixed (N : Node_Id) is
kono
parents:
diff changeset
1317 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
1318 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
1319
kono
parents:
diff changeset
1320 Left_Type : constant Entity_Id := Etype (Left);
kono
parents:
diff changeset
1321 Right_Type : constant Entity_Id := Etype (Right);
kono
parents:
diff changeset
1322 Result_Type : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
1323 Right_Small : constant Ureal := Small_Value (Right_Type);
kono
parents:
diff changeset
1324 Left_Small : constant Ureal := Small_Value (Left_Type);
kono
parents:
diff changeset
1325
kono
parents:
diff changeset
1326 Result_Small : Ureal;
kono
parents:
diff changeset
1327 Frac : Ureal;
kono
parents:
diff changeset
1328 Frac_Num : Uint;
kono
parents:
diff changeset
1329 Frac_Den : Uint;
kono
parents:
diff changeset
1330 Lit_Int : Node_Id;
kono
parents:
diff changeset
1331
kono
parents:
diff changeset
1332 begin
kono
parents:
diff changeset
1333 -- Get result small. If the result is an integer, treat it as though
kono
parents:
diff changeset
1334 -- it had a small of 1.0, all other processing is identical.
kono
parents:
diff changeset
1335
kono
parents:
diff changeset
1336 if Is_Integer_Type (Result_Type) then
kono
parents:
diff changeset
1337 Result_Small := Ureal_1;
kono
parents:
diff changeset
1338 else
kono
parents:
diff changeset
1339 Result_Small := Small_Value (Result_Type);
kono
parents:
diff changeset
1340 end if;
kono
parents:
diff changeset
1341
kono
parents:
diff changeset
1342 -- Get small ratio
kono
parents:
diff changeset
1343
kono
parents:
diff changeset
1344 Frac := (Left_Small * Right_Small) / Result_Small;
kono
parents:
diff changeset
1345 Frac_Num := Norm_Num (Frac);
kono
parents:
diff changeset
1346 Frac_Den := Norm_Den (Frac);
kono
parents:
diff changeset
1347
kono
parents:
diff changeset
1348 -- If the fraction is an integer, then we get the result by multiplying
kono
parents:
diff changeset
1349 -- the operands, and then multiplying the result by the integer value.
kono
parents:
diff changeset
1350
kono
parents:
diff changeset
1351 if Frac_Den = 1 then
kono
parents:
diff changeset
1352 Lit_Int := Integer_Literal (N, Frac_Num); -- always positive
kono
parents:
diff changeset
1353
kono
parents:
diff changeset
1354 if Present (Lit_Int) then
kono
parents:
diff changeset
1355 Set_Result (N,
kono
parents:
diff changeset
1356 Build_Multiply (N, Build_Multiply (N, Left, Right),
kono
parents:
diff changeset
1357 Lit_Int));
kono
parents:
diff changeset
1358 return;
kono
parents:
diff changeset
1359 end if;
kono
parents:
diff changeset
1360
kono
parents:
diff changeset
1361 -- If the fraction is the reciprocal of an integer, then we get the
kono
parents:
diff changeset
1362 -- result by multiplying the operands, and then dividing the result by
kono
parents:
diff changeset
1363 -- the integer value. The order of the operations is important, if we
kono
parents:
diff changeset
1364 -- divided first, we would lose precision.
kono
parents:
diff changeset
1365
kono
parents:
diff changeset
1366 elsif Frac_Num = 1 then
kono
parents:
diff changeset
1367 Lit_Int := Integer_Literal (N, Frac_Den); -- always positive
kono
parents:
diff changeset
1368
kono
parents:
diff changeset
1369 if Present (Lit_Int) then
kono
parents:
diff changeset
1370 Set_Result (N, Build_Scaled_Divide (N, Left, Right, Lit_Int));
kono
parents:
diff changeset
1371 return;
kono
parents:
diff changeset
1372 end if;
kono
parents:
diff changeset
1373 end if;
kono
parents:
diff changeset
1374
kono
parents:
diff changeset
1375 -- If we fall through, we use floating-point to compute the result
kono
parents:
diff changeset
1376
kono
parents:
diff changeset
1377 Set_Result (N,
kono
parents:
diff changeset
1378 Build_Multiply (N,
kono
parents:
diff changeset
1379 Build_Multiply (N, Fpt_Value (Left), Fpt_Value (Right)),
kono
parents:
diff changeset
1380 Real_Literal (N, Frac)));
kono
parents:
diff changeset
1381 end Do_Multiply_Fixed_Fixed;
kono
parents:
diff changeset
1382
kono
parents:
diff changeset
1383 ---------------------------------
kono
parents:
diff changeset
1384 -- Do_Multiply_Fixed_Universal --
kono
parents:
diff changeset
1385 ---------------------------------
kono
parents:
diff changeset
1386
kono
parents:
diff changeset
1387 -- We have:
kono
parents:
diff changeset
1388
kono
parents:
diff changeset
1389 -- (Result_Value * Result_Small) = (Left_Value * Left_Small) * Lit_Value;
kono
parents:
diff changeset
1390 -- Result_Value = Left_Value * (Left_Small * Lit_Value) / Result_Small;
kono
parents:
diff changeset
1391
kono
parents:
diff changeset
1392 -- The result is required to be in the perfect result set if the literal
kono
parents:
diff changeset
1393 -- can be factored so that the resulting small ratio is an integer or the
kono
parents:
diff changeset
1394 -- reciprocal of an integer (RM G.2.3(21-22)). We now give a detailed
kono
parents:
diff changeset
1395 -- analysis of these RM requirements:
kono
parents:
diff changeset
1396
kono
parents:
diff changeset
1397 -- We must factor the literal, finding an integer K:
kono
parents:
diff changeset
1398
kono
parents:
diff changeset
1399 -- Lit_Value = K * Right_Small
kono
parents:
diff changeset
1400 -- Right_Small = Lit_Value / K
kono
parents:
diff changeset
1401
kono
parents:
diff changeset
1402 -- such that the small ratio:
kono
parents:
diff changeset
1403
kono
parents:
diff changeset
1404 -- Left_Small * (Lit_Value / K)
kono
parents:
diff changeset
1405 -- ----------------------------
kono
parents:
diff changeset
1406 -- Result_Small
kono
parents:
diff changeset
1407
kono
parents:
diff changeset
1408 -- Left_Small * Lit_Value 1
kono
parents:
diff changeset
1409 -- = ---------------------- * -
kono
parents:
diff changeset
1410 -- Result_Small K
kono
parents:
diff changeset
1411
kono
parents:
diff changeset
1412 -- is an integer or the reciprocal of an integer, and for
kono
parents:
diff changeset
1413 -- implementation efficiency we need the smallest such K.
kono
parents:
diff changeset
1414
kono
parents:
diff changeset
1415 -- First we reduce the left fraction to lowest terms
kono
parents:
diff changeset
1416
kono
parents:
diff changeset
1417 -- If denominator = 1, then for K = 1, the small ratio is an integer, and
kono
parents:
diff changeset
1418 -- this is clearly the minimum K case, so set
kono
parents:
diff changeset
1419
kono
parents:
diff changeset
1420 -- K = 1, Right_Small = Lit_Value
kono
parents:
diff changeset
1421
kono
parents:
diff changeset
1422 -- If denominator > 1, then set K to the numerator of the fraction, so
kono
parents:
diff changeset
1423 -- that the resulting small ratio is the reciprocal of the integer (the
kono
parents:
diff changeset
1424 -- denominator value).
kono
parents:
diff changeset
1425
kono
parents:
diff changeset
1426 procedure Do_Multiply_Fixed_Universal
kono
parents:
diff changeset
1427 (N : Node_Id;
kono
parents:
diff changeset
1428 Left, Right : Node_Id)
kono
parents:
diff changeset
1429 is
kono
parents:
diff changeset
1430 Left_Type : constant Entity_Id := Etype (Left);
kono
parents:
diff changeset
1431 Result_Type : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
1432 Left_Small : constant Ureal := Small_Value (Left_Type);
kono
parents:
diff changeset
1433 Lit_Value : constant Ureal := Realval (Right);
kono
parents:
diff changeset
1434
kono
parents:
diff changeset
1435 Result_Small : Ureal;
kono
parents:
diff changeset
1436 Frac : Ureal;
kono
parents:
diff changeset
1437 Frac_Num : Uint;
kono
parents:
diff changeset
1438 Frac_Den : Uint;
kono
parents:
diff changeset
1439 Lit_K : Node_Id;
kono
parents:
diff changeset
1440 Lit_Int : Node_Id;
kono
parents:
diff changeset
1441
kono
parents:
diff changeset
1442 begin
kono
parents:
diff changeset
1443 -- Get result small. If the result is an integer, treat it as though
kono
parents:
diff changeset
1444 -- it had a small of 1.0, all other processing is identical.
kono
parents:
diff changeset
1445
kono
parents:
diff changeset
1446 if Is_Integer_Type (Result_Type) then
kono
parents:
diff changeset
1447 Result_Small := Ureal_1;
kono
parents:
diff changeset
1448 else
kono
parents:
diff changeset
1449 Result_Small := Small_Value (Result_Type);
kono
parents:
diff changeset
1450 end if;
kono
parents:
diff changeset
1451
kono
parents:
diff changeset
1452 -- Determine if literal can be rewritten successfully
kono
parents:
diff changeset
1453
kono
parents:
diff changeset
1454 Frac := (Left_Small * Lit_Value) / Result_Small;
kono
parents:
diff changeset
1455 Frac_Num := Norm_Num (Frac);
kono
parents:
diff changeset
1456 Frac_Den := Norm_Den (Frac);
kono
parents:
diff changeset
1457
kono
parents:
diff changeset
1458 -- Case where fraction is an integer (K = 1, integer = numerator). If
kono
parents:
diff changeset
1459 -- this integer is not too large, this is the case where the result can
kono
parents:
diff changeset
1460 -- be obtained by multiplying by this integer value.
kono
parents:
diff changeset
1461
kono
parents:
diff changeset
1462 if Frac_Den = 1 then
kono
parents:
diff changeset
1463 Lit_Int := Integer_Literal (N, Frac_Num, UR_Is_Negative (Frac));
kono
parents:
diff changeset
1464
kono
parents:
diff changeset
1465 if Present (Lit_Int) then
kono
parents:
diff changeset
1466 Set_Result (N, Build_Multiply (N, Left, Lit_Int));
kono
parents:
diff changeset
1467 return;
kono
parents:
diff changeset
1468 end if;
kono
parents:
diff changeset
1469
kono
parents:
diff changeset
1470 -- Case where we choose K to make fraction the reciprocal of an integer
kono
parents:
diff changeset
1471 -- (K = numerator of fraction, integer = denominator of fraction). If
kono
parents:
diff changeset
1472 -- both K and the denominator are small enough, this is the case where
kono
parents:
diff changeset
1473 -- the result can be obtained by first multiplying by K, and then
kono
parents:
diff changeset
1474 -- dividing by the integer value.
kono
parents:
diff changeset
1475
kono
parents:
diff changeset
1476 else
kono
parents:
diff changeset
1477 Lit_Int := Integer_Literal (N, Frac_Den, UR_Is_Negative (Frac));
kono
parents:
diff changeset
1478 Lit_K := Integer_Literal (N, Frac_Num);
kono
parents:
diff changeset
1479
kono
parents:
diff changeset
1480 if Present (Lit_Int) and then Present (Lit_K) then
kono
parents:
diff changeset
1481 Set_Result (N, Build_Scaled_Divide (N, Left, Lit_K, Lit_Int));
kono
parents:
diff changeset
1482 return;
kono
parents:
diff changeset
1483 end if;
kono
parents:
diff changeset
1484 end if;
kono
parents:
diff changeset
1485
kono
parents:
diff changeset
1486 -- Fall through if the literal cannot be successfully rewritten, or if
kono
parents:
diff changeset
1487 -- the small ratio is out of range of integer arithmetic. In the former
kono
parents:
diff changeset
1488 -- case it is fine to use floating-point to get the close result set,
kono
parents:
diff changeset
1489 -- and in the latter case, it means that the result is zero or raises
kono
parents:
diff changeset
1490 -- constraint error, and we can do that accurately in floating-point.
kono
parents:
diff changeset
1491
kono
parents:
diff changeset
1492 -- If we end up using floating-point, then we take the right integer
kono
parents:
diff changeset
1493 -- to be one, and its small to be the value of the original right real
kono
parents:
diff changeset
1494 -- literal. That way, we need only one floating-point multiplication.
kono
parents:
diff changeset
1495
kono
parents:
diff changeset
1496 Set_Result (N,
kono
parents:
diff changeset
1497 Build_Multiply (N, Fpt_Value (Left), Real_Literal (N, Frac)));
kono
parents:
diff changeset
1498 end Do_Multiply_Fixed_Universal;
kono
parents:
diff changeset
1499
kono
parents:
diff changeset
1500 ---------------------------------
kono
parents:
diff changeset
1501 -- Expand_Convert_Fixed_Static --
kono
parents:
diff changeset
1502 ---------------------------------
kono
parents:
diff changeset
1503
kono
parents:
diff changeset
1504 procedure Expand_Convert_Fixed_Static (N : Node_Id) is
kono
parents:
diff changeset
1505 begin
kono
parents:
diff changeset
1506 Rewrite (N,
kono
parents:
diff changeset
1507 Convert_To (Etype (N),
kono
parents:
diff changeset
1508 Make_Real_Literal (Sloc (N), Expr_Value_R (Expression (N)))));
kono
parents:
diff changeset
1509 Analyze_And_Resolve (N);
kono
parents:
diff changeset
1510 end Expand_Convert_Fixed_Static;
kono
parents:
diff changeset
1511
kono
parents:
diff changeset
1512 -----------------------------------
kono
parents:
diff changeset
1513 -- Expand_Convert_Fixed_To_Fixed --
kono
parents:
diff changeset
1514 -----------------------------------
kono
parents:
diff changeset
1515
kono
parents:
diff changeset
1516 -- We have:
kono
parents:
diff changeset
1517
kono
parents:
diff changeset
1518 -- Result_Value * Result_Small = Source_Value * Source_Small
kono
parents:
diff changeset
1519 -- Result_Value = Source_Value * (Source_Small / Result_Small)
kono
parents:
diff changeset
1520
kono
parents:
diff changeset
1521 -- If the small ratio (Source_Small / Result_Small) is a sufficiently small
kono
parents:
diff changeset
1522 -- integer, then the perfect result set is obtained by a single integer
kono
parents:
diff changeset
1523 -- multiplication.
kono
parents:
diff changeset
1524
kono
parents:
diff changeset
1525 -- If the small ratio is the reciprocal of a sufficiently small integer,
kono
parents:
diff changeset
1526 -- then the perfect result set is obtained by a single integer division.
kono
parents:
diff changeset
1527
kono
parents:
diff changeset
1528 -- In other cases, we obtain the close result set by calculating the
kono
parents:
diff changeset
1529 -- result in floating-point.
kono
parents:
diff changeset
1530
kono
parents:
diff changeset
1531 procedure Expand_Convert_Fixed_To_Fixed (N : Node_Id) is
kono
parents:
diff changeset
1532 Rng_Check : constant Boolean := Do_Range_Check (N);
kono
parents:
diff changeset
1533 Expr : constant Node_Id := Expression (N);
kono
parents:
diff changeset
1534 Result_Type : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
1535 Source_Type : constant Entity_Id := Etype (Expr);
kono
parents:
diff changeset
1536 Small_Ratio : Ureal;
kono
parents:
diff changeset
1537 Ratio_Num : Uint;
kono
parents:
diff changeset
1538 Ratio_Den : Uint;
kono
parents:
diff changeset
1539 Lit : Node_Id;
kono
parents:
diff changeset
1540
kono
parents:
diff changeset
1541 begin
kono
parents:
diff changeset
1542 if Is_OK_Static_Expression (Expr) then
kono
parents:
diff changeset
1543 Expand_Convert_Fixed_Static (N);
kono
parents:
diff changeset
1544 return;
kono
parents:
diff changeset
1545 end if;
kono
parents:
diff changeset
1546
kono
parents:
diff changeset
1547 Small_Ratio := Small_Value (Source_Type) / Small_Value (Result_Type);
kono
parents:
diff changeset
1548 Ratio_Num := Norm_Num (Small_Ratio);
kono
parents:
diff changeset
1549 Ratio_Den := Norm_Den (Small_Ratio);
kono
parents:
diff changeset
1550
kono
parents:
diff changeset
1551 if Ratio_Den = 1 then
kono
parents:
diff changeset
1552 if Ratio_Num = 1 then
kono
parents:
diff changeset
1553 Set_Result (N, Expr);
kono
parents:
diff changeset
1554 return;
kono
parents:
diff changeset
1555
kono
parents:
diff changeset
1556 else
kono
parents:
diff changeset
1557 Lit := Integer_Literal (N, Ratio_Num);
kono
parents:
diff changeset
1558
kono
parents:
diff changeset
1559 if Present (Lit) then
kono
parents:
diff changeset
1560 Set_Result (N, Build_Multiply (N, Expr, Lit));
kono
parents:
diff changeset
1561 return;
kono
parents:
diff changeset
1562 end if;
kono
parents:
diff changeset
1563 end if;
kono
parents:
diff changeset
1564
kono
parents:
diff changeset
1565 elsif Ratio_Num = 1 then
kono
parents:
diff changeset
1566 Lit := Integer_Literal (N, Ratio_Den);
kono
parents:
diff changeset
1567
kono
parents:
diff changeset
1568 if Present (Lit) then
kono
parents:
diff changeset
1569 Set_Result (N, Build_Divide (N, Expr, Lit), Rng_Check);
kono
parents:
diff changeset
1570 return;
kono
parents:
diff changeset
1571 end if;
kono
parents:
diff changeset
1572 end if;
kono
parents:
diff changeset
1573
kono
parents:
diff changeset
1574 -- Fall through to use floating-point for the close result set case
kono
parents:
diff changeset
1575 -- either as a result of the small ratio not being an integer or the
kono
parents:
diff changeset
1576 -- reciprocal of an integer, or if the integer is out of range.
kono
parents:
diff changeset
1577
kono
parents:
diff changeset
1578 Set_Result (N,
kono
parents:
diff changeset
1579 Build_Multiply (N,
kono
parents:
diff changeset
1580 Fpt_Value (Expr),
kono
parents:
diff changeset
1581 Real_Literal (N, Small_Ratio)),
kono
parents:
diff changeset
1582 Rng_Check);
kono
parents:
diff changeset
1583 end Expand_Convert_Fixed_To_Fixed;
kono
parents:
diff changeset
1584
kono
parents:
diff changeset
1585 -----------------------------------
kono
parents:
diff changeset
1586 -- Expand_Convert_Fixed_To_Float --
kono
parents:
diff changeset
1587 -----------------------------------
kono
parents:
diff changeset
1588
kono
parents:
diff changeset
1589 -- If the small of the fixed type is 1.0, then we simply convert the
kono
parents:
diff changeset
1590 -- integer value directly to the target floating-point type, otherwise
kono
parents:
diff changeset
1591 -- we first have to multiply by the small, in Universal_Real, and then
kono
parents:
diff changeset
1592 -- convert the result to the target floating-point type.
kono
parents:
diff changeset
1593
kono
parents:
diff changeset
1594 procedure Expand_Convert_Fixed_To_Float (N : Node_Id) is
kono
parents:
diff changeset
1595 Rng_Check : constant Boolean := Do_Range_Check (N);
kono
parents:
diff changeset
1596 Expr : constant Node_Id := Expression (N);
kono
parents:
diff changeset
1597 Source_Type : constant Entity_Id := Etype (Expr);
kono
parents:
diff changeset
1598 Small : constant Ureal := Small_Value (Source_Type);
kono
parents:
diff changeset
1599
kono
parents:
diff changeset
1600 begin
kono
parents:
diff changeset
1601 if Is_OK_Static_Expression (Expr) then
kono
parents:
diff changeset
1602 Expand_Convert_Fixed_Static (N);
kono
parents:
diff changeset
1603 return;
kono
parents:
diff changeset
1604 end if;
kono
parents:
diff changeset
1605
kono
parents:
diff changeset
1606 if Small = Ureal_1 then
kono
parents:
diff changeset
1607 Set_Result (N, Expr);
kono
parents:
diff changeset
1608
kono
parents:
diff changeset
1609 else
kono
parents:
diff changeset
1610 Set_Result (N,
kono
parents:
diff changeset
1611 Build_Multiply (N,
kono
parents:
diff changeset
1612 Fpt_Value (Expr),
kono
parents:
diff changeset
1613 Real_Literal (N, Small)),
kono
parents:
diff changeset
1614 Rng_Check);
kono
parents:
diff changeset
1615 end if;
kono
parents:
diff changeset
1616 end Expand_Convert_Fixed_To_Float;
kono
parents:
diff changeset
1617
kono
parents:
diff changeset
1618 -------------------------------------
kono
parents:
diff changeset
1619 -- Expand_Convert_Fixed_To_Integer --
kono
parents:
diff changeset
1620 -------------------------------------
kono
parents:
diff changeset
1621
kono
parents:
diff changeset
1622 -- We have:
kono
parents:
diff changeset
1623
kono
parents:
diff changeset
1624 -- Result_Value = Source_Value * Source_Small
kono
parents:
diff changeset
1625
kono
parents:
diff changeset
1626 -- If the small value is a sufficiently small integer, then the perfect
kono
parents:
diff changeset
1627 -- result set is obtained by a single integer multiplication.
kono
parents:
diff changeset
1628
kono
parents:
diff changeset
1629 -- If the small value is the reciprocal of a sufficiently small integer,
kono
parents:
diff changeset
1630 -- then the perfect result set is obtained by a single integer division.
kono
parents:
diff changeset
1631
kono
parents:
diff changeset
1632 -- In other cases, we obtain the close result set by calculating the
kono
parents:
diff changeset
1633 -- result in floating-point.
kono
parents:
diff changeset
1634
kono
parents:
diff changeset
1635 procedure Expand_Convert_Fixed_To_Integer (N : Node_Id) is
kono
parents:
diff changeset
1636 Rng_Check : constant Boolean := Do_Range_Check (N);
kono
parents:
diff changeset
1637 Expr : constant Node_Id := Expression (N);
kono
parents:
diff changeset
1638 Source_Type : constant Entity_Id := Etype (Expr);
kono
parents:
diff changeset
1639 Small : constant Ureal := Small_Value (Source_Type);
kono
parents:
diff changeset
1640 Small_Num : constant Uint := Norm_Num (Small);
kono
parents:
diff changeset
1641 Small_Den : constant Uint := Norm_Den (Small);
kono
parents:
diff changeset
1642 Lit : Node_Id;
kono
parents:
diff changeset
1643
kono
parents:
diff changeset
1644 begin
kono
parents:
diff changeset
1645 if Is_OK_Static_Expression (Expr) then
kono
parents:
diff changeset
1646 Expand_Convert_Fixed_Static (N);
kono
parents:
diff changeset
1647 return;
kono
parents:
diff changeset
1648 end if;
kono
parents:
diff changeset
1649
kono
parents:
diff changeset
1650 if Small_Den = 1 then
kono
parents:
diff changeset
1651 Lit := Integer_Literal (N, Small_Num);
kono
parents:
diff changeset
1652
kono
parents:
diff changeset
1653 if Present (Lit) then
kono
parents:
diff changeset
1654 Set_Result (N, Build_Multiply (N, Expr, Lit), Rng_Check);
kono
parents:
diff changeset
1655 return;
kono
parents:
diff changeset
1656 end if;
kono
parents:
diff changeset
1657
kono
parents:
diff changeset
1658 elsif Small_Num = 1 then
kono
parents:
diff changeset
1659 Lit := Integer_Literal (N, Small_Den);
kono
parents:
diff changeset
1660
kono
parents:
diff changeset
1661 if Present (Lit) then
kono
parents:
diff changeset
1662 Set_Result (N, Build_Divide (N, Expr, Lit), Rng_Check);
kono
parents:
diff changeset
1663 return;
kono
parents:
diff changeset
1664 end if;
kono
parents:
diff changeset
1665 end if;
kono
parents:
diff changeset
1666
kono
parents:
diff changeset
1667 -- Fall through to use floating-point for the close result set case
kono
parents:
diff changeset
1668 -- either as a result of the small value not being an integer or the
kono
parents:
diff changeset
1669 -- reciprocal of an integer, or if the integer is out of range.
kono
parents:
diff changeset
1670
kono
parents:
diff changeset
1671 Set_Result (N,
kono
parents:
diff changeset
1672 Build_Multiply (N,
kono
parents:
diff changeset
1673 Fpt_Value (Expr),
kono
parents:
diff changeset
1674 Real_Literal (N, Small)),
kono
parents:
diff changeset
1675 Rng_Check);
kono
parents:
diff changeset
1676 end Expand_Convert_Fixed_To_Integer;
kono
parents:
diff changeset
1677
kono
parents:
diff changeset
1678 -----------------------------------
kono
parents:
diff changeset
1679 -- Expand_Convert_Float_To_Fixed --
kono
parents:
diff changeset
1680 -----------------------------------
kono
parents:
diff changeset
1681
kono
parents:
diff changeset
1682 -- We have
kono
parents:
diff changeset
1683
kono
parents:
diff changeset
1684 -- Result_Value * Result_Small = Operand_Value
kono
parents:
diff changeset
1685
kono
parents:
diff changeset
1686 -- so compute:
kono
parents:
diff changeset
1687
kono
parents:
diff changeset
1688 -- Result_Value = Operand_Value * (1.0 / Result_Small)
kono
parents:
diff changeset
1689
kono
parents:
diff changeset
1690 -- We do the small scaling in floating-point, and we do a multiplication
kono
parents:
diff changeset
1691 -- rather than a division, since it is accurate enough for the perfect
kono
parents:
diff changeset
1692 -- result cases, and faster.
kono
parents:
diff changeset
1693
kono
parents:
diff changeset
1694 procedure Expand_Convert_Float_To_Fixed (N : Node_Id) is
kono
parents:
diff changeset
1695 Expr : constant Node_Id := Expression (N);
kono
parents:
diff changeset
1696 Orig_N : constant Node_Id := Original_Node (N);
kono
parents:
diff changeset
1697 Result_Type : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
1698 Rng_Check : constant Boolean := Do_Range_Check (N);
kono
parents:
diff changeset
1699 Small : constant Ureal := Small_Value (Result_Type);
kono
parents:
diff changeset
1700 Truncate : Boolean;
kono
parents:
diff changeset
1701
kono
parents:
diff changeset
1702 begin
kono
parents:
diff changeset
1703 -- Optimize small = 1, where we can avoid the multiply completely
kono
parents:
diff changeset
1704
kono
parents:
diff changeset
1705 if Small = Ureal_1 then
kono
parents:
diff changeset
1706 Set_Result (N, Expr, Rng_Check, Trunc => True);
kono
parents:
diff changeset
1707
kono
parents:
diff changeset
1708 -- Normal case where multiply is required. Rounding is truncating
kono
parents:
diff changeset
1709 -- for decimal fixed point types only, see RM 4.6(29), except if the
kono
parents:
diff changeset
1710 -- conversion comes from an attribute reference 'Round (RM 3.5.10 (14)):
kono
parents:
diff changeset
1711 -- The attribute is implemented by means of a conversion that must
kono
parents:
diff changeset
1712 -- round.
kono
parents:
diff changeset
1713
kono
parents:
diff changeset
1714 else
kono
parents:
diff changeset
1715 if Is_Decimal_Fixed_Point_Type (Result_Type) then
kono
parents:
diff changeset
1716 Truncate :=
kono
parents:
diff changeset
1717 Nkind (Orig_N) /= N_Attribute_Reference
kono
parents:
diff changeset
1718 or else Get_Attribute_Id
kono
parents:
diff changeset
1719 (Attribute_Name (Orig_N)) /= Attribute_Round;
kono
parents:
diff changeset
1720 else
kono
parents:
diff changeset
1721 Truncate := False;
kono
parents:
diff changeset
1722 end if;
kono
parents:
diff changeset
1723
kono
parents:
diff changeset
1724 Set_Result
kono
parents:
diff changeset
1725 (N => N,
kono
parents:
diff changeset
1726 Expr =>
kono
parents:
diff changeset
1727 Build_Multiply
kono
parents:
diff changeset
1728 (N => N,
kono
parents:
diff changeset
1729 L => Fpt_Value (Expr),
kono
parents:
diff changeset
1730 R => Real_Literal (N, Ureal_1 / Small)),
kono
parents:
diff changeset
1731 Rchk => Rng_Check,
kono
parents:
diff changeset
1732 Trunc => Truncate);
kono
parents:
diff changeset
1733 end if;
kono
parents:
diff changeset
1734 end Expand_Convert_Float_To_Fixed;
kono
parents:
diff changeset
1735
kono
parents:
diff changeset
1736 -------------------------------------
kono
parents:
diff changeset
1737 -- Expand_Convert_Integer_To_Fixed --
kono
parents:
diff changeset
1738 -------------------------------------
kono
parents:
diff changeset
1739
kono
parents:
diff changeset
1740 -- We have
kono
parents:
diff changeset
1741
kono
parents:
diff changeset
1742 -- Result_Value * Result_Small = Operand_Value
kono
parents:
diff changeset
1743 -- Result_Value = Operand_Value / Result_Small
kono
parents:
diff changeset
1744
kono
parents:
diff changeset
1745 -- If the small value is a sufficiently small integer, then the perfect
kono
parents:
diff changeset
1746 -- result set is obtained by a single integer division.
kono
parents:
diff changeset
1747
kono
parents:
diff changeset
1748 -- If the small value is the reciprocal of a sufficiently small integer,
kono
parents:
diff changeset
1749 -- the perfect result set is obtained by a single integer multiplication.
kono
parents:
diff changeset
1750
kono
parents:
diff changeset
1751 -- In other cases, we obtain the close result set by calculating the
kono
parents:
diff changeset
1752 -- result in floating-point using a multiplication by the reciprocal
kono
parents:
diff changeset
1753 -- of the Result_Small.
kono
parents:
diff changeset
1754
kono
parents:
diff changeset
1755 procedure Expand_Convert_Integer_To_Fixed (N : Node_Id) is
kono
parents:
diff changeset
1756 Rng_Check : constant Boolean := Do_Range_Check (N);
kono
parents:
diff changeset
1757 Expr : constant Node_Id := Expression (N);
kono
parents:
diff changeset
1758 Result_Type : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
1759 Small : constant Ureal := Small_Value (Result_Type);
kono
parents:
diff changeset
1760 Small_Num : constant Uint := Norm_Num (Small);
kono
parents:
diff changeset
1761 Small_Den : constant Uint := Norm_Den (Small);
kono
parents:
diff changeset
1762 Lit : Node_Id;
kono
parents:
diff changeset
1763
kono
parents:
diff changeset
1764 begin
kono
parents:
diff changeset
1765 if Small_Den = 1 then
kono
parents:
diff changeset
1766 Lit := Integer_Literal (N, Small_Num);
kono
parents:
diff changeset
1767
kono
parents:
diff changeset
1768 if Present (Lit) then
kono
parents:
diff changeset
1769 Set_Result (N, Build_Divide (N, Expr, Lit), Rng_Check);
kono
parents:
diff changeset
1770 return;
kono
parents:
diff changeset
1771 end if;
kono
parents:
diff changeset
1772
kono
parents:
diff changeset
1773 elsif Small_Num = 1 then
kono
parents:
diff changeset
1774 Lit := Integer_Literal (N, Small_Den);
kono
parents:
diff changeset
1775
kono
parents:
diff changeset
1776 if Present (Lit) then
kono
parents:
diff changeset
1777 Set_Result (N, Build_Multiply (N, Expr, Lit), Rng_Check);
kono
parents:
diff changeset
1778 return;
kono
parents:
diff changeset
1779 end if;
kono
parents:
diff changeset
1780 end if;
kono
parents:
diff changeset
1781
kono
parents:
diff changeset
1782 -- Fall through to use floating-point for the close result set case
kono
parents:
diff changeset
1783 -- either as a result of the small value not being an integer or the
kono
parents:
diff changeset
1784 -- reciprocal of an integer, or if the integer is out of range.
kono
parents:
diff changeset
1785
kono
parents:
diff changeset
1786 Set_Result (N,
kono
parents:
diff changeset
1787 Build_Multiply (N,
kono
parents:
diff changeset
1788 Fpt_Value (Expr),
kono
parents:
diff changeset
1789 Real_Literal (N, Ureal_1 / Small)),
kono
parents:
diff changeset
1790 Rng_Check);
kono
parents:
diff changeset
1791 end Expand_Convert_Integer_To_Fixed;
kono
parents:
diff changeset
1792
kono
parents:
diff changeset
1793 --------------------------------
kono
parents:
diff changeset
1794 -- Expand_Decimal_Divide_Call --
kono
parents:
diff changeset
1795 --------------------------------
kono
parents:
diff changeset
1796
kono
parents:
diff changeset
1797 -- We have four operands
kono
parents:
diff changeset
1798
kono
parents:
diff changeset
1799 -- Dividend
kono
parents:
diff changeset
1800 -- Divisor
kono
parents:
diff changeset
1801 -- Quotient
kono
parents:
diff changeset
1802 -- Remainder
kono
parents:
diff changeset
1803
kono
parents:
diff changeset
1804 -- All of which are decimal types, and which thus have associated
kono
parents:
diff changeset
1805 -- decimal scales.
kono
parents:
diff changeset
1806
kono
parents:
diff changeset
1807 -- Computing the quotient is a similar problem to that faced by the
kono
parents:
diff changeset
1808 -- normal fixed-point division, except that it is simpler, because
kono
parents:
diff changeset
1809 -- we always have compatible smalls.
kono
parents:
diff changeset
1810
kono
parents:
diff changeset
1811 -- Quotient = (Dividend / Divisor) * 10**q
kono
parents:
diff changeset
1812
kono
parents:
diff changeset
1813 -- where 10 ** q = Dividend'Small / (Divisor'Small * Quotient'Small)
kono
parents:
diff changeset
1814 -- so q = Divisor'Scale + Quotient'Scale - Dividend'Scale
kono
parents:
diff changeset
1815
kono
parents:
diff changeset
1816 -- For q >= 0, we compute
kono
parents:
diff changeset
1817
kono
parents:
diff changeset
1818 -- Numerator := Dividend * 10 ** q
kono
parents:
diff changeset
1819 -- Denominator := Divisor
kono
parents:
diff changeset
1820 -- Quotient := Numerator / Denominator
kono
parents:
diff changeset
1821
kono
parents:
diff changeset
1822 -- For q < 0, we compute
kono
parents:
diff changeset
1823
kono
parents:
diff changeset
1824 -- Numerator := Dividend
kono
parents:
diff changeset
1825 -- Denominator := Divisor * 10 ** q
kono
parents:
diff changeset
1826 -- Quotient := Numerator / Denominator
kono
parents:
diff changeset
1827
kono
parents:
diff changeset
1828 -- Both these divisions are done in truncated mode, and the remainder
kono
parents:
diff changeset
1829 -- from these divisions is used to compute the result Remainder. This
kono
parents:
diff changeset
1830 -- remainder has the effective scale of the numerator of the division,
kono
parents:
diff changeset
1831
kono
parents:
diff changeset
1832 -- For q >= 0, the remainder scale is Dividend'Scale + q
kono
parents:
diff changeset
1833 -- For q < 0, the remainder scale is Dividend'Scale
kono
parents:
diff changeset
1834
kono
parents:
diff changeset
1835 -- The result Remainder is then computed by a normal truncating decimal
kono
parents:
diff changeset
1836 -- conversion from this scale to the scale of the remainder, i.e. by a
kono
parents:
diff changeset
1837 -- division or multiplication by the appropriate power of 10.
kono
parents:
diff changeset
1838
kono
parents:
diff changeset
1839 procedure Expand_Decimal_Divide_Call (N : Node_Id) is
kono
parents:
diff changeset
1840 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
1841
kono
parents:
diff changeset
1842 Dividend : Node_Id := First_Actual (N);
kono
parents:
diff changeset
1843 Divisor : Node_Id := Next_Actual (Dividend);
kono
parents:
diff changeset
1844 Quotient : Node_Id := Next_Actual (Divisor);
kono
parents:
diff changeset
1845 Remainder : Node_Id := Next_Actual (Quotient);
kono
parents:
diff changeset
1846
kono
parents:
diff changeset
1847 Dividend_Type : constant Entity_Id := Etype (Dividend);
kono
parents:
diff changeset
1848 Divisor_Type : constant Entity_Id := Etype (Divisor);
kono
parents:
diff changeset
1849 Quotient_Type : constant Entity_Id := Etype (Quotient);
kono
parents:
diff changeset
1850 Remainder_Type : constant Entity_Id := Etype (Remainder);
kono
parents:
diff changeset
1851
kono
parents:
diff changeset
1852 Dividend_Scale : constant Uint := Scale_Value (Dividend_Type);
kono
parents:
diff changeset
1853 Divisor_Scale : constant Uint := Scale_Value (Divisor_Type);
kono
parents:
diff changeset
1854 Quotient_Scale : constant Uint := Scale_Value (Quotient_Type);
kono
parents:
diff changeset
1855 Remainder_Scale : constant Uint := Scale_Value (Remainder_Type);
kono
parents:
diff changeset
1856
kono
parents:
diff changeset
1857 Q : Uint;
kono
parents:
diff changeset
1858 Numerator_Scale : Uint;
kono
parents:
diff changeset
1859 Stmts : List_Id;
kono
parents:
diff changeset
1860 Qnn : Entity_Id;
kono
parents:
diff changeset
1861 Rnn : Entity_Id;
kono
parents:
diff changeset
1862 Computed_Remainder : Node_Id;
kono
parents:
diff changeset
1863 Adjusted_Remainder : Node_Id;
kono
parents:
diff changeset
1864 Scale_Adjust : Uint;
kono
parents:
diff changeset
1865
kono
parents:
diff changeset
1866 begin
kono
parents:
diff changeset
1867 -- Relocate the operands, since they are now list elements, and we
kono
parents:
diff changeset
1868 -- need to reference them separately as operands in the expanded code.
kono
parents:
diff changeset
1869
kono
parents:
diff changeset
1870 Dividend := Relocate_Node (Dividend);
kono
parents:
diff changeset
1871 Divisor := Relocate_Node (Divisor);
kono
parents:
diff changeset
1872 Quotient := Relocate_Node (Quotient);
kono
parents:
diff changeset
1873 Remainder := Relocate_Node (Remainder);
kono
parents:
diff changeset
1874
kono
parents:
diff changeset
1875 -- Now compute Q, the adjustment scale
kono
parents:
diff changeset
1876
kono
parents:
diff changeset
1877 Q := Divisor_Scale + Quotient_Scale - Dividend_Scale;
kono
parents:
diff changeset
1878
kono
parents:
diff changeset
1879 -- If Q is non-negative then we need a scaled divide
kono
parents:
diff changeset
1880
kono
parents:
diff changeset
1881 if Q >= 0 then
kono
parents:
diff changeset
1882 Build_Scaled_Divide_Code
kono
parents:
diff changeset
1883 (N,
kono
parents:
diff changeset
1884 Dividend,
kono
parents:
diff changeset
1885 Integer_Literal (N, Uint_10 ** Q),
kono
parents:
diff changeset
1886 Divisor,
kono
parents:
diff changeset
1887 Qnn, Rnn, Stmts);
kono
parents:
diff changeset
1888
kono
parents:
diff changeset
1889 Numerator_Scale := Dividend_Scale + Q;
kono
parents:
diff changeset
1890
kono
parents:
diff changeset
1891 -- If Q is negative, then we need a double divide
kono
parents:
diff changeset
1892
kono
parents:
diff changeset
1893 else
kono
parents:
diff changeset
1894 Build_Double_Divide_Code
kono
parents:
diff changeset
1895 (N,
kono
parents:
diff changeset
1896 Dividend,
kono
parents:
diff changeset
1897 Divisor,
kono
parents:
diff changeset
1898 Integer_Literal (N, Uint_10 ** (-Q)),
kono
parents:
diff changeset
1899 Qnn, Rnn, Stmts);
kono
parents:
diff changeset
1900
kono
parents:
diff changeset
1901 Numerator_Scale := Dividend_Scale;
kono
parents:
diff changeset
1902 end if;
kono
parents:
diff changeset
1903
kono
parents:
diff changeset
1904 -- Add statement to set quotient value
kono
parents:
diff changeset
1905
kono
parents:
diff changeset
1906 -- Quotient := quotient-type!(Qnn);
kono
parents:
diff changeset
1907
kono
parents:
diff changeset
1908 Append_To (Stmts,
kono
parents:
diff changeset
1909 Make_Assignment_Statement (Loc,
kono
parents:
diff changeset
1910 Name => Quotient,
kono
parents:
diff changeset
1911 Expression =>
kono
parents:
diff changeset
1912 Unchecked_Convert_To (Quotient_Type,
kono
parents:
diff changeset
1913 Build_Conversion (N, Quotient_Type,
kono
parents:
diff changeset
1914 New_Occurrence_Of (Qnn, Loc)))));
kono
parents:
diff changeset
1915
kono
parents:
diff changeset
1916 -- Now we need to deal with computing and setting the remainder. The
kono
parents:
diff changeset
1917 -- scale of the remainder is in Numerator_Scale, and the desired
kono
parents:
diff changeset
1918 -- scale is the scale of the given Remainder argument. There are
kono
parents:
diff changeset
1919 -- three cases:
kono
parents:
diff changeset
1920
kono
parents:
diff changeset
1921 -- Numerator_Scale > Remainder_Scale
kono
parents:
diff changeset
1922
kono
parents:
diff changeset
1923 -- in this case, there are extra digits in the computed remainder
kono
parents:
diff changeset
1924 -- which must be eliminated by an extra division:
kono
parents:
diff changeset
1925
kono
parents:
diff changeset
1926 -- computed-remainder := Numerator rem Denominator
kono
parents:
diff changeset
1927 -- scale_adjust = Numerator_Scale - Remainder_Scale
kono
parents:
diff changeset
1928 -- adjusted-remainder := computed-remainder / 10 ** scale_adjust
kono
parents:
diff changeset
1929
kono
parents:
diff changeset
1930 -- Numerator_Scale = Remainder_Scale
kono
parents:
diff changeset
1931
kono
parents:
diff changeset
1932 -- in this case, the we have the remainder we need
kono
parents:
diff changeset
1933
kono
parents:
diff changeset
1934 -- computed-remainder := Numerator rem Denominator
kono
parents:
diff changeset
1935 -- adjusted-remainder := computed-remainder
kono
parents:
diff changeset
1936
kono
parents:
diff changeset
1937 -- Numerator_Scale < Remainder_Scale
kono
parents:
diff changeset
1938
kono
parents:
diff changeset
1939 -- in this case, we have insufficient digits in the computed
kono
parents:
diff changeset
1940 -- remainder, which must be eliminated by an extra multiply
kono
parents:
diff changeset
1941
kono
parents:
diff changeset
1942 -- computed-remainder := Numerator rem Denominator
kono
parents:
diff changeset
1943 -- scale_adjust = Remainder_Scale - Numerator_Scale
kono
parents:
diff changeset
1944 -- adjusted-remainder := computed-remainder * 10 ** scale_adjust
kono
parents:
diff changeset
1945
kono
parents:
diff changeset
1946 -- Finally we assign the adjusted-remainder to the result Remainder
kono
parents:
diff changeset
1947 -- with conversions to get the proper fixed-point type representation.
kono
parents:
diff changeset
1948
kono
parents:
diff changeset
1949 Computed_Remainder := New_Occurrence_Of (Rnn, Loc);
kono
parents:
diff changeset
1950
kono
parents:
diff changeset
1951 if Numerator_Scale > Remainder_Scale then
kono
parents:
diff changeset
1952 Scale_Adjust := Numerator_Scale - Remainder_Scale;
kono
parents:
diff changeset
1953 Adjusted_Remainder :=
kono
parents:
diff changeset
1954 Build_Divide
kono
parents:
diff changeset
1955 (N, Computed_Remainder, Integer_Literal (N, 10 ** Scale_Adjust));
kono
parents:
diff changeset
1956
kono
parents:
diff changeset
1957 elsif Numerator_Scale = Remainder_Scale then
kono
parents:
diff changeset
1958 Adjusted_Remainder := Computed_Remainder;
kono
parents:
diff changeset
1959
kono
parents:
diff changeset
1960 else -- Numerator_Scale < Remainder_Scale
kono
parents:
diff changeset
1961 Scale_Adjust := Remainder_Scale - Numerator_Scale;
kono
parents:
diff changeset
1962 Adjusted_Remainder :=
kono
parents:
diff changeset
1963 Build_Multiply
kono
parents:
diff changeset
1964 (N, Computed_Remainder, Integer_Literal (N, 10 ** Scale_Adjust));
kono
parents:
diff changeset
1965 end if;
kono
parents:
diff changeset
1966
kono
parents:
diff changeset
1967 -- Assignment of remainder result
kono
parents:
diff changeset
1968
kono
parents:
diff changeset
1969 Append_To (Stmts,
kono
parents:
diff changeset
1970 Make_Assignment_Statement (Loc,
kono
parents:
diff changeset
1971 Name => Remainder,
kono
parents:
diff changeset
1972 Expression =>
kono
parents:
diff changeset
1973 Unchecked_Convert_To (Remainder_Type, Adjusted_Remainder)));
kono
parents:
diff changeset
1974
kono
parents:
diff changeset
1975 -- Final step is to rewrite the call with a block containing the
kono
parents:
diff changeset
1976 -- above sequence of constructed statements for the divide operation.
kono
parents:
diff changeset
1977
kono
parents:
diff changeset
1978 Rewrite (N,
kono
parents:
diff changeset
1979 Make_Block_Statement (Loc,
kono
parents:
diff changeset
1980 Handled_Statement_Sequence =>
kono
parents:
diff changeset
1981 Make_Handled_Sequence_Of_Statements (Loc,
kono
parents:
diff changeset
1982 Statements => Stmts)));
kono
parents:
diff changeset
1983
kono
parents:
diff changeset
1984 Analyze (N);
kono
parents:
diff changeset
1985 end Expand_Decimal_Divide_Call;
kono
parents:
diff changeset
1986
kono
parents:
diff changeset
1987 -----------------------------------------------
kono
parents:
diff changeset
1988 -- Expand_Divide_Fixed_By_Fixed_Giving_Fixed --
kono
parents:
diff changeset
1989 -----------------------------------------------
kono
parents:
diff changeset
1990
kono
parents:
diff changeset
1991 procedure Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N : Node_Id) is
kono
parents:
diff changeset
1992 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
1993 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
1994
kono
parents:
diff changeset
1995 begin
kono
parents:
diff changeset
1996 -- Suppress expansion of a fixed-by-fixed division if the
kono
parents:
diff changeset
1997 -- operation is supported directly by the target.
kono
parents:
diff changeset
1998
kono
parents:
diff changeset
1999 if Target_Has_Fixed_Ops (Etype (Left), Etype (Right), Etype (N)) then
kono
parents:
diff changeset
2000 return;
kono
parents:
diff changeset
2001 end if;
kono
parents:
diff changeset
2002
kono
parents:
diff changeset
2003 if Etype (Left) = Universal_Real then
kono
parents:
diff changeset
2004 Do_Divide_Universal_Fixed (N);
kono
parents:
diff changeset
2005
kono
parents:
diff changeset
2006 elsif Etype (Right) = Universal_Real then
kono
parents:
diff changeset
2007 Do_Divide_Fixed_Universal (N);
kono
parents:
diff changeset
2008
kono
parents:
diff changeset
2009 else
kono
parents:
diff changeset
2010 Do_Divide_Fixed_Fixed (N);
kono
parents:
diff changeset
2011
kono
parents:
diff changeset
2012 -- A focused optimization: if after constant folding the
kono
parents:
diff changeset
2013 -- expression is of the form: T ((Exp * D) / D), where D is
kono
parents:
diff changeset
2014 -- a static constant, return T (Exp). This form will show up
kono
parents:
diff changeset
2015 -- when D is the denominator of the static expression for the
kono
parents:
diff changeset
2016 -- 'small of fixed-point types involved. This transformation
kono
parents:
diff changeset
2017 -- removes a division that may be expensive on some targets.
kono
parents:
diff changeset
2018
kono
parents:
diff changeset
2019 if Nkind (N) = N_Type_Conversion
kono
parents:
diff changeset
2020 and then Nkind (Expression (N)) = N_Op_Divide
kono
parents:
diff changeset
2021 then
kono
parents:
diff changeset
2022 declare
kono
parents:
diff changeset
2023 Num : constant Node_Id := Left_Opnd (Expression (N));
kono
parents:
diff changeset
2024 Den : constant Node_Id := Right_Opnd (Expression (N));
kono
parents:
diff changeset
2025
kono
parents:
diff changeset
2026 begin
kono
parents:
diff changeset
2027 if Nkind (Den) = N_Integer_Literal
kono
parents:
diff changeset
2028 and then Nkind (Num) = N_Op_Multiply
kono
parents:
diff changeset
2029 and then Nkind (Right_Opnd (Num)) = N_Integer_Literal
kono
parents:
diff changeset
2030 and then Intval (Den) = Intval (Right_Opnd (Num))
kono
parents:
diff changeset
2031 then
kono
parents:
diff changeset
2032 Rewrite (Expression (N), Left_Opnd (Num));
kono
parents:
diff changeset
2033 end if;
kono
parents:
diff changeset
2034 end;
kono
parents:
diff changeset
2035 end if;
kono
parents:
diff changeset
2036 end if;
kono
parents:
diff changeset
2037 end Expand_Divide_Fixed_By_Fixed_Giving_Fixed;
kono
parents:
diff changeset
2038
kono
parents:
diff changeset
2039 -----------------------------------------------
kono
parents:
diff changeset
2040 -- Expand_Divide_Fixed_By_Fixed_Giving_Float --
kono
parents:
diff changeset
2041 -----------------------------------------------
kono
parents:
diff changeset
2042
kono
parents:
diff changeset
2043 -- The division is done in Universal_Real, and the result is multiplied
kono
parents:
diff changeset
2044 -- by the small ratio, which is Small (Right) / Small (Left). Special
kono
parents:
diff changeset
2045 -- treatment is required for universal operands, which represent their
kono
parents:
diff changeset
2046 -- own value and do not require conversion.
kono
parents:
diff changeset
2047
kono
parents:
diff changeset
2048 procedure Expand_Divide_Fixed_By_Fixed_Giving_Float (N : Node_Id) is
kono
parents:
diff changeset
2049 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
2050 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
2051
kono
parents:
diff changeset
2052 Left_Type : constant Entity_Id := Etype (Left);
kono
parents:
diff changeset
2053 Right_Type : constant Entity_Id := Etype (Right);
kono
parents:
diff changeset
2054
kono
parents:
diff changeset
2055 begin
kono
parents:
diff changeset
2056 -- Case of left operand is universal real, the result we want is:
kono
parents:
diff changeset
2057
kono
parents:
diff changeset
2058 -- Left_Value / (Right_Value * Right_Small)
kono
parents:
diff changeset
2059
kono
parents:
diff changeset
2060 -- so we compute this as:
kono
parents:
diff changeset
2061
kono
parents:
diff changeset
2062 -- (Left_Value / Right_Small) / Right_Value
kono
parents:
diff changeset
2063
kono
parents:
diff changeset
2064 if Left_Type = Universal_Real then
kono
parents:
diff changeset
2065 Set_Result (N,
kono
parents:
diff changeset
2066 Build_Divide (N,
kono
parents:
diff changeset
2067 Real_Literal (N, Realval (Left) / Small_Value (Right_Type)),
kono
parents:
diff changeset
2068 Fpt_Value (Right)));
kono
parents:
diff changeset
2069
kono
parents:
diff changeset
2070 -- Case of right operand is universal real, the result we want is
kono
parents:
diff changeset
2071
kono
parents:
diff changeset
2072 -- (Left_Value * Left_Small) / Right_Value
kono
parents:
diff changeset
2073
kono
parents:
diff changeset
2074 -- so we compute this as:
kono
parents:
diff changeset
2075
kono
parents:
diff changeset
2076 -- Left_Value * (Left_Small / Right_Value)
kono
parents:
diff changeset
2077
kono
parents:
diff changeset
2078 -- Note we invert to a multiplication since usually floating-point
kono
parents:
diff changeset
2079 -- multiplication is much faster than floating-point division.
kono
parents:
diff changeset
2080
kono
parents:
diff changeset
2081 elsif Right_Type = Universal_Real then
kono
parents:
diff changeset
2082 Set_Result (N,
kono
parents:
diff changeset
2083 Build_Multiply (N,
kono
parents:
diff changeset
2084 Fpt_Value (Left),
kono
parents:
diff changeset
2085 Real_Literal (N, Small_Value (Left_Type) / Realval (Right))));
kono
parents:
diff changeset
2086
kono
parents:
diff changeset
2087 -- Both operands are fixed, so the value we want is
kono
parents:
diff changeset
2088
kono
parents:
diff changeset
2089 -- (Left_Value * Left_Small) / (Right_Value * Right_Small)
kono
parents:
diff changeset
2090
kono
parents:
diff changeset
2091 -- which we compute as:
kono
parents:
diff changeset
2092
kono
parents:
diff changeset
2093 -- (Left_Value / Right_Value) * (Left_Small / Right_Small)
kono
parents:
diff changeset
2094
kono
parents:
diff changeset
2095 else
kono
parents:
diff changeset
2096 Set_Result (N,
kono
parents:
diff changeset
2097 Build_Multiply (N,
kono
parents:
diff changeset
2098 Build_Divide (N, Fpt_Value (Left), Fpt_Value (Right)),
kono
parents:
diff changeset
2099 Real_Literal (N,
kono
parents:
diff changeset
2100 Small_Value (Left_Type) / Small_Value (Right_Type))));
kono
parents:
diff changeset
2101 end if;
kono
parents:
diff changeset
2102 end Expand_Divide_Fixed_By_Fixed_Giving_Float;
kono
parents:
diff changeset
2103
kono
parents:
diff changeset
2104 -------------------------------------------------
kono
parents:
diff changeset
2105 -- Expand_Divide_Fixed_By_Fixed_Giving_Integer --
kono
parents:
diff changeset
2106 -------------------------------------------------
kono
parents:
diff changeset
2107
kono
parents:
diff changeset
2108 procedure Expand_Divide_Fixed_By_Fixed_Giving_Integer (N : Node_Id) is
kono
parents:
diff changeset
2109 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
2110 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
2111 begin
kono
parents:
diff changeset
2112 if Etype (Left) = Universal_Real then
kono
parents:
diff changeset
2113 Do_Divide_Universal_Fixed (N);
kono
parents:
diff changeset
2114 elsif Etype (Right) = Universal_Real then
kono
parents:
diff changeset
2115 Do_Divide_Fixed_Universal (N);
kono
parents:
diff changeset
2116 else
kono
parents:
diff changeset
2117 Do_Divide_Fixed_Fixed (N);
kono
parents:
diff changeset
2118 end if;
kono
parents:
diff changeset
2119 end Expand_Divide_Fixed_By_Fixed_Giving_Integer;
kono
parents:
diff changeset
2120
kono
parents:
diff changeset
2121 -------------------------------------------------
kono
parents:
diff changeset
2122 -- Expand_Divide_Fixed_By_Integer_Giving_Fixed --
kono
parents:
diff changeset
2123 -------------------------------------------------
kono
parents:
diff changeset
2124
kono
parents:
diff changeset
2125 -- Since the operand and result fixed-point type is the same, this is
kono
parents:
diff changeset
2126 -- a straight divide by the right operand, the small can be ignored.
kono
parents:
diff changeset
2127
kono
parents:
diff changeset
2128 procedure Expand_Divide_Fixed_By_Integer_Giving_Fixed (N : Node_Id) is
kono
parents:
diff changeset
2129 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
2130 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
2131 begin
kono
parents:
diff changeset
2132 Set_Result (N, Build_Divide (N, Left, Right));
kono
parents:
diff changeset
2133 end Expand_Divide_Fixed_By_Integer_Giving_Fixed;
kono
parents:
diff changeset
2134
kono
parents:
diff changeset
2135 -------------------------------------------------
kono
parents:
diff changeset
2136 -- Expand_Multiply_Fixed_By_Fixed_Giving_Fixed --
kono
parents:
diff changeset
2137 -------------------------------------------------
kono
parents:
diff changeset
2138
kono
parents:
diff changeset
2139 procedure Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N : Node_Id) is
kono
parents:
diff changeset
2140 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
2141 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
2142
kono
parents:
diff changeset
2143 procedure Rewrite_Non_Static_Universal (Opnd : Node_Id);
kono
parents:
diff changeset
2144 -- The operand may be a non-static universal value, such an
kono
parents:
diff changeset
2145 -- exponentiation with a non-static exponent. In that case, treat
kono
parents:
diff changeset
2146 -- as a fixed * fixed multiplication, and convert the argument to
kono
parents:
diff changeset
2147 -- the target fixed type.
kono
parents:
diff changeset
2148
kono
parents:
diff changeset
2149 ----------------------------------
kono
parents:
diff changeset
2150 -- Rewrite_Non_Static_Universal --
kono
parents:
diff changeset
2151 ----------------------------------
kono
parents:
diff changeset
2152
kono
parents:
diff changeset
2153 procedure Rewrite_Non_Static_Universal (Opnd : Node_Id) is
kono
parents:
diff changeset
2154 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
2155 begin
kono
parents:
diff changeset
2156 Rewrite (Opnd,
kono
parents:
diff changeset
2157 Make_Type_Conversion (Loc,
kono
parents:
diff changeset
2158 Subtype_Mark => New_Occurrence_Of (Etype (N), Loc),
kono
parents:
diff changeset
2159 Expression => Expression (Opnd)));
kono
parents:
diff changeset
2160 Analyze_And_Resolve (Opnd, Etype (N));
kono
parents:
diff changeset
2161 end Rewrite_Non_Static_Universal;
kono
parents:
diff changeset
2162
kono
parents:
diff changeset
2163 -- Start of processing for Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
kono
parents:
diff changeset
2164
kono
parents:
diff changeset
2165 begin
kono
parents:
diff changeset
2166 -- Suppress expansion of a fixed-by-fixed multiplication if the
kono
parents:
diff changeset
2167 -- operation is supported directly by the target.
kono
parents:
diff changeset
2168
kono
parents:
diff changeset
2169 if Target_Has_Fixed_Ops (Etype (Left), Etype (Right), Etype (N)) then
kono
parents:
diff changeset
2170 return;
kono
parents:
diff changeset
2171 end if;
kono
parents:
diff changeset
2172
kono
parents:
diff changeset
2173 if Etype (Left) = Universal_Real then
kono
parents:
diff changeset
2174 if Nkind (Left) = N_Real_Literal then
kono
parents:
diff changeset
2175 Do_Multiply_Fixed_Universal (N, Left => Right, Right => Left);
kono
parents:
diff changeset
2176
kono
parents:
diff changeset
2177 elsif Nkind (Left) = N_Type_Conversion then
kono
parents:
diff changeset
2178 Rewrite_Non_Static_Universal (Left);
kono
parents:
diff changeset
2179 Do_Multiply_Fixed_Fixed (N);
kono
parents:
diff changeset
2180 end if;
kono
parents:
diff changeset
2181
kono
parents:
diff changeset
2182 elsif Etype (Right) = Universal_Real then
kono
parents:
diff changeset
2183 if Nkind (Right) = N_Real_Literal then
kono
parents:
diff changeset
2184 Do_Multiply_Fixed_Universal (N, Left, Right);
kono
parents:
diff changeset
2185
kono
parents:
diff changeset
2186 elsif Nkind (Right) = N_Type_Conversion then
kono
parents:
diff changeset
2187 Rewrite_Non_Static_Universal (Right);
kono
parents:
diff changeset
2188 Do_Multiply_Fixed_Fixed (N);
kono
parents:
diff changeset
2189 end if;
kono
parents:
diff changeset
2190
kono
parents:
diff changeset
2191 else
kono
parents:
diff changeset
2192 Do_Multiply_Fixed_Fixed (N);
kono
parents:
diff changeset
2193 end if;
kono
parents:
diff changeset
2194 end Expand_Multiply_Fixed_By_Fixed_Giving_Fixed;
kono
parents:
diff changeset
2195
kono
parents:
diff changeset
2196 -------------------------------------------------
kono
parents:
diff changeset
2197 -- Expand_Multiply_Fixed_By_Fixed_Giving_Float --
kono
parents:
diff changeset
2198 -------------------------------------------------
kono
parents:
diff changeset
2199
kono
parents:
diff changeset
2200 -- The multiply is done in Universal_Real, and the result is multiplied
kono
parents:
diff changeset
2201 -- by the adjustment for the smalls which is Small (Right) * Small (Left).
kono
parents:
diff changeset
2202 -- Special treatment is required for universal operands.
kono
parents:
diff changeset
2203
kono
parents:
diff changeset
2204 procedure Expand_Multiply_Fixed_By_Fixed_Giving_Float (N : Node_Id) is
kono
parents:
diff changeset
2205 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
2206 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
2207
kono
parents:
diff changeset
2208 Left_Type : constant Entity_Id := Etype (Left);
kono
parents:
diff changeset
2209 Right_Type : constant Entity_Id := Etype (Right);
kono
parents:
diff changeset
2210
kono
parents:
diff changeset
2211 begin
kono
parents:
diff changeset
2212 -- Case of left operand is universal real, the result we want is
kono
parents:
diff changeset
2213
kono
parents:
diff changeset
2214 -- Left_Value * (Right_Value * Right_Small)
kono
parents:
diff changeset
2215
kono
parents:
diff changeset
2216 -- so we compute this as:
kono
parents:
diff changeset
2217
kono
parents:
diff changeset
2218 -- (Left_Value * Right_Small) * Right_Value;
kono
parents:
diff changeset
2219
kono
parents:
diff changeset
2220 if Left_Type = Universal_Real then
kono
parents:
diff changeset
2221 Set_Result (N,
kono
parents:
diff changeset
2222 Build_Multiply (N,
kono
parents:
diff changeset
2223 Real_Literal (N, Realval (Left) * Small_Value (Right_Type)),
kono
parents:
diff changeset
2224 Fpt_Value (Right)));
kono
parents:
diff changeset
2225
kono
parents:
diff changeset
2226 -- Case of right operand is universal real, the result we want is
kono
parents:
diff changeset
2227
kono
parents:
diff changeset
2228 -- (Left_Value * Left_Small) * Right_Value
kono
parents:
diff changeset
2229
kono
parents:
diff changeset
2230 -- so we compute this as:
kono
parents:
diff changeset
2231
kono
parents:
diff changeset
2232 -- Left_Value * (Left_Small * Right_Value)
kono
parents:
diff changeset
2233
kono
parents:
diff changeset
2234 elsif Right_Type = Universal_Real then
kono
parents:
diff changeset
2235 Set_Result (N,
kono
parents:
diff changeset
2236 Build_Multiply (N,
kono
parents:
diff changeset
2237 Fpt_Value (Left),
kono
parents:
diff changeset
2238 Real_Literal (N, Small_Value (Left_Type) * Realval (Right))));
kono
parents:
diff changeset
2239
kono
parents:
diff changeset
2240 -- Both operands are fixed, so the value we want is
kono
parents:
diff changeset
2241
kono
parents:
diff changeset
2242 -- (Left_Value * Left_Small) * (Right_Value * Right_Small)
kono
parents:
diff changeset
2243
kono
parents:
diff changeset
2244 -- which we compute as:
kono
parents:
diff changeset
2245
kono
parents:
diff changeset
2246 -- (Left_Value * Right_Value) * (Right_Small * Left_Small)
kono
parents:
diff changeset
2247
kono
parents:
diff changeset
2248 else
kono
parents:
diff changeset
2249 Set_Result (N,
kono
parents:
diff changeset
2250 Build_Multiply (N,
kono
parents:
diff changeset
2251 Build_Multiply (N, Fpt_Value (Left), Fpt_Value (Right)),
kono
parents:
diff changeset
2252 Real_Literal (N,
kono
parents:
diff changeset
2253 Small_Value (Right_Type) * Small_Value (Left_Type))));
kono
parents:
diff changeset
2254 end if;
kono
parents:
diff changeset
2255 end Expand_Multiply_Fixed_By_Fixed_Giving_Float;
kono
parents:
diff changeset
2256
kono
parents:
diff changeset
2257 ---------------------------------------------------
kono
parents:
diff changeset
2258 -- Expand_Multiply_Fixed_By_Fixed_Giving_Integer --
kono
parents:
diff changeset
2259 ---------------------------------------------------
kono
parents:
diff changeset
2260
kono
parents:
diff changeset
2261 procedure Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N : Node_Id) is
kono
parents:
diff changeset
2262 Loc : constant Source_Ptr := Sloc (N);
kono
parents:
diff changeset
2263 Left : constant Node_Id := Left_Opnd (N);
kono
parents:
diff changeset
2264 Right : constant Node_Id := Right_Opnd (N);
kono
parents:
diff changeset
2265
kono
parents:
diff changeset
2266 begin
kono
parents:
diff changeset
2267 if Etype (Left) = Universal_Real then
kono
parents:
diff changeset
2268 Do_Multiply_Fixed_Universal (N, Left => Right, Right => Left);
kono
parents:
diff changeset
2269
kono
parents:
diff changeset
2270 elsif Etype (Right) = Universal_Real then
kono
parents:
diff changeset
2271 Do_Multiply_Fixed_Universal (N, Left, Right);
kono
parents:
diff changeset
2272
kono
parents:
diff changeset
2273 -- If both types are equal and we need to avoid floating point
kono
parents:
diff changeset
2274 -- instructions, it's worth introducing a temporary with the
kono
parents:
diff changeset
2275 -- common type, because it may be evaluated more simply without
kono
parents:
diff changeset
2276 -- the need for run-time use of floating point.
kono
parents:
diff changeset
2277
kono
parents:
diff changeset
2278 elsif Etype (Right) = Etype (Left)
kono
parents:
diff changeset
2279 and then Restriction_Active (No_Floating_Point)
kono
parents:
diff changeset
2280 then
kono
parents:
diff changeset
2281 declare
kono
parents:
diff changeset
2282 Temp : constant Entity_Id := Make_Temporary (Loc, 'F');
kono
parents:
diff changeset
2283 Mult : constant Node_Id := Make_Op_Multiply (Loc, Left, Right);
kono
parents:
diff changeset
2284 Decl : constant Node_Id :=
kono
parents:
diff changeset
2285 Make_Object_Declaration (Loc,
kono
parents:
diff changeset
2286 Defining_Identifier => Temp,
kono
parents:
diff changeset
2287 Object_Definition => New_Occurrence_Of (Etype (Right), Loc),
kono
parents:
diff changeset
2288 Expression => Mult);
kono
parents:
diff changeset
2289
kono
parents:
diff changeset
2290 begin
kono
parents:
diff changeset
2291 Insert_Action (N, Decl);
kono
parents:
diff changeset
2292 Rewrite (N,
kono
parents:
diff changeset
2293 OK_Convert_To (Etype (N), New_Occurrence_Of (Temp, Loc)));
kono
parents:
diff changeset
2294 Analyze_And_Resolve (N, Standard_Integer);
kono
parents:
diff changeset
2295 end;
kono
parents:
diff changeset
2296
kono
parents:
diff changeset
2297 else
kono
parents:
diff changeset
2298 Do_Multiply_Fixed_Fixed (N);
kono
parents:
diff changeset
2299 end if;
kono
parents:
diff changeset
2300 end Expand_Multiply_Fixed_By_Fixed_Giving_Integer;
kono
parents:
diff changeset
2301
kono
parents:
diff changeset
2302 ---------------------------------------------------
kono
parents:
diff changeset
2303 -- Expand_Multiply_Fixed_By_Integer_Giving_Fixed --
kono
parents:
diff changeset
2304 ---------------------------------------------------
kono
parents:
diff changeset
2305
kono
parents:
diff changeset
2306 -- Since the operand and result fixed-point type is the same, this is
kono
parents:
diff changeset
2307 -- a straight multiply by the right operand, the small can be ignored.
kono
parents:
diff changeset
2308
kono
parents:
diff changeset
2309 procedure Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N : Node_Id) is
kono
parents:
diff changeset
2310 begin
kono
parents:
diff changeset
2311 Set_Result (N,
kono
parents:
diff changeset
2312 Build_Multiply (N, Left_Opnd (N), Right_Opnd (N)));
kono
parents:
diff changeset
2313 end Expand_Multiply_Fixed_By_Integer_Giving_Fixed;
kono
parents:
diff changeset
2314
kono
parents:
diff changeset
2315 ---------------------------------------------------
kono
parents:
diff changeset
2316 -- Expand_Multiply_Integer_By_Fixed_Giving_Fixed --
kono
parents:
diff changeset
2317 ---------------------------------------------------
kono
parents:
diff changeset
2318
kono
parents:
diff changeset
2319 -- Since the operand and result fixed-point type is the same, this is
kono
parents:
diff changeset
2320 -- a straight multiply by the right operand, the small can be ignored.
kono
parents:
diff changeset
2321
kono
parents:
diff changeset
2322 procedure Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N : Node_Id) is
kono
parents:
diff changeset
2323 begin
kono
parents:
diff changeset
2324 Set_Result (N,
kono
parents:
diff changeset
2325 Build_Multiply (N, Left_Opnd (N), Right_Opnd (N)));
kono
parents:
diff changeset
2326 end Expand_Multiply_Integer_By_Fixed_Giving_Fixed;
kono
parents:
diff changeset
2327
kono
parents:
diff changeset
2328 ---------------
kono
parents:
diff changeset
2329 -- Fpt_Value --
kono
parents:
diff changeset
2330 ---------------
kono
parents:
diff changeset
2331
kono
parents:
diff changeset
2332 function Fpt_Value (N : Node_Id) return Node_Id is
kono
parents:
diff changeset
2333 Typ : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
2334
kono
parents:
diff changeset
2335 begin
kono
parents:
diff changeset
2336 if Is_Integer_Type (Typ)
kono
parents:
diff changeset
2337 or else Is_Floating_Point_Type (Typ)
kono
parents:
diff changeset
2338 then
kono
parents:
diff changeset
2339 return Build_Conversion (N, Universal_Real, N);
kono
parents:
diff changeset
2340
kono
parents:
diff changeset
2341 -- Fixed-point case, must get integer value first
kono
parents:
diff changeset
2342
kono
parents:
diff changeset
2343 else
kono
parents:
diff changeset
2344 return Build_Conversion (N, Universal_Real, N);
kono
parents:
diff changeset
2345 end if;
kono
parents:
diff changeset
2346 end Fpt_Value;
kono
parents:
diff changeset
2347
kono
parents:
diff changeset
2348 ---------------------
kono
parents:
diff changeset
2349 -- Integer_Literal --
kono
parents:
diff changeset
2350 ---------------------
kono
parents:
diff changeset
2351
kono
parents:
diff changeset
2352 function Integer_Literal
kono
parents:
diff changeset
2353 (N : Node_Id;
kono
parents:
diff changeset
2354 V : Uint;
kono
parents:
diff changeset
2355 Negative : Boolean := False) return Node_Id
kono
parents:
diff changeset
2356 is
kono
parents:
diff changeset
2357 T : Entity_Id;
kono
parents:
diff changeset
2358 L : Node_Id;
kono
parents:
diff changeset
2359
kono
parents:
diff changeset
2360 begin
kono
parents:
diff changeset
2361 if V < Uint_2 ** 7 then
kono
parents:
diff changeset
2362 T := Standard_Integer_8;
kono
parents:
diff changeset
2363
kono
parents:
diff changeset
2364 elsif V < Uint_2 ** 15 then
kono
parents:
diff changeset
2365 T := Standard_Integer_16;
kono
parents:
diff changeset
2366
kono
parents:
diff changeset
2367 elsif V < Uint_2 ** 31 then
kono
parents:
diff changeset
2368 T := Standard_Integer_32;
kono
parents:
diff changeset
2369
kono
parents:
diff changeset
2370 elsif V < Uint_2 ** 63 then
kono
parents:
diff changeset
2371 T := Standard_Integer_64;
kono
parents:
diff changeset
2372
kono
parents:
diff changeset
2373 else
kono
parents:
diff changeset
2374 return Empty;
kono
parents:
diff changeset
2375 end if;
kono
parents:
diff changeset
2376
kono
parents:
diff changeset
2377 if Negative then
kono
parents:
diff changeset
2378 L := Make_Integer_Literal (Sloc (N), UI_Negate (V));
kono
parents:
diff changeset
2379 else
kono
parents:
diff changeset
2380 L := Make_Integer_Literal (Sloc (N), V);
kono
parents:
diff changeset
2381 end if;
kono
parents:
diff changeset
2382
kono
parents:
diff changeset
2383 -- Set type of result in case used elsewhere (see note at start)
kono
parents:
diff changeset
2384
kono
parents:
diff changeset
2385 Set_Etype (L, T);
kono
parents:
diff changeset
2386 Set_Is_Static_Expression (L);
kono
parents:
diff changeset
2387
kono
parents:
diff changeset
2388 -- We really need to set Analyzed here because we may be creating a
kono
parents:
diff changeset
2389 -- very strange beast, namely an integer literal typed as fixed-point
kono
parents:
diff changeset
2390 -- and the analyzer won't like that. Probably we should allow the
kono
parents:
diff changeset
2391 -- Treat_Fixed_As_Integer flag to appear on integer literal nodes
kono
parents:
diff changeset
2392 -- and teach the analyzer how to handle them ???
kono
parents:
diff changeset
2393
kono
parents:
diff changeset
2394 Set_Analyzed (L);
kono
parents:
diff changeset
2395 return L;
kono
parents:
diff changeset
2396 end Integer_Literal;
kono
parents:
diff changeset
2397
kono
parents:
diff changeset
2398 ------------------
kono
parents:
diff changeset
2399 -- Real_Literal --
kono
parents:
diff changeset
2400 ------------------
kono
parents:
diff changeset
2401
kono
parents:
diff changeset
2402 function Real_Literal (N : Node_Id; V : Ureal) return Node_Id is
kono
parents:
diff changeset
2403 L : Node_Id;
kono
parents:
diff changeset
2404
kono
parents:
diff changeset
2405 begin
kono
parents:
diff changeset
2406 L := Make_Real_Literal (Sloc (N), V);
kono
parents:
diff changeset
2407
kono
parents:
diff changeset
2408 -- Set type of result in case used elsewhere (see note at start)
kono
parents:
diff changeset
2409
kono
parents:
diff changeset
2410 Set_Etype (L, Universal_Real);
kono
parents:
diff changeset
2411 return L;
kono
parents:
diff changeset
2412 end Real_Literal;
kono
parents:
diff changeset
2413
kono
parents:
diff changeset
2414 ------------------------
kono
parents:
diff changeset
2415 -- Rounded_Result_Set --
kono
parents:
diff changeset
2416 ------------------------
kono
parents:
diff changeset
2417
kono
parents:
diff changeset
2418 function Rounded_Result_Set (N : Node_Id) return Boolean is
kono
parents:
diff changeset
2419 K : constant Node_Kind := Nkind (N);
kono
parents:
diff changeset
2420 begin
kono
parents:
diff changeset
2421 if (K = N_Type_Conversion or else
kono
parents:
diff changeset
2422 K = N_Op_Divide or else
kono
parents:
diff changeset
2423 K = N_Op_Multiply)
kono
parents:
diff changeset
2424 and then
kono
parents:
diff changeset
2425 (Rounded_Result (N) or else Is_Integer_Type (Etype (N)))
kono
parents:
diff changeset
2426 then
kono
parents:
diff changeset
2427 return True;
kono
parents:
diff changeset
2428 else
kono
parents:
diff changeset
2429 return False;
kono
parents:
diff changeset
2430 end if;
kono
parents:
diff changeset
2431 end Rounded_Result_Set;
kono
parents:
diff changeset
2432
kono
parents:
diff changeset
2433 ----------------
kono
parents:
diff changeset
2434 -- Set_Result --
kono
parents:
diff changeset
2435 ----------------
kono
parents:
diff changeset
2436
kono
parents:
diff changeset
2437 procedure Set_Result
kono
parents:
diff changeset
2438 (N : Node_Id;
kono
parents:
diff changeset
2439 Expr : Node_Id;
kono
parents:
diff changeset
2440 Rchk : Boolean := False;
kono
parents:
diff changeset
2441 Trunc : Boolean := False)
kono
parents:
diff changeset
2442 is
kono
parents:
diff changeset
2443 Cnode : Node_Id;
kono
parents:
diff changeset
2444
kono
parents:
diff changeset
2445 Expr_Type : constant Entity_Id := Etype (Expr);
kono
parents:
diff changeset
2446 Result_Type : constant Entity_Id := Etype (N);
kono
parents:
diff changeset
2447
kono
parents:
diff changeset
2448 begin
kono
parents:
diff changeset
2449 -- No conversion required if types match and no range check or truncate
kono
parents:
diff changeset
2450
kono
parents:
diff changeset
2451 if Result_Type = Expr_Type and then not (Rchk or Trunc) then
kono
parents:
diff changeset
2452 Cnode := Expr;
kono
parents:
diff changeset
2453
kono
parents:
diff changeset
2454 -- Else perform required conversion
kono
parents:
diff changeset
2455
kono
parents:
diff changeset
2456 else
kono
parents:
diff changeset
2457 Cnode := Build_Conversion (N, Result_Type, Expr, Rchk, Trunc);
kono
parents:
diff changeset
2458 end if;
kono
parents:
diff changeset
2459
kono
parents:
diff changeset
2460 Rewrite (N, Cnode);
kono
parents:
diff changeset
2461 Analyze_And_Resolve (N, Result_Type);
kono
parents:
diff changeset
2462 end Set_Result;
kono
parents:
diff changeset
2463
kono
parents:
diff changeset
2464 end Exp_Fixd;