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