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