1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Einfo
; use Einfo
;
27 with Errout
; use Errout
;
28 with Sem_Util
; use Sem_Util
;
30 package body Eval_Fat
is
32 Radix
: constant Int
:= 2;
33 -- This code is currently only correct for the radix 2 case. We use the
34 -- symbolic value Radix where possible to help in the unlikely case of
35 -- anyone ever having to adjust this code for another value, and for
36 -- documentation purposes.
38 -- Another assumption is that the range of the floating-point type is
39 -- symmetric around zero.
41 type Radix_Power_Table
is array (Int
range 1 .. 4) of Int
;
43 Radix_Powers
: constant Radix_Power_Table
:=
44 (Radix
** 1, Radix
** 2, Radix
** 3, Radix
** 4);
46 -----------------------
47 -- Local Subprograms --
48 -----------------------
55 Mode
: Rounding_Mode
:= Round
);
56 -- Decomposes a non-zero floating-point number into fraction and exponent
57 -- parts. The fraction is in the interval 1.0 / Radix .. T'Pred (1.0) and
58 -- uses Rbase = Radix. The result is rounded to a nearest machine number.
64 function Adjacent
(RT
: R
; X
, Towards
: T
) return T
is
68 elsif Towards
> X
then
79 function Ceiling
(RT
: R
; X
: T
) return T
is
80 XT
: constant T
:= Truncation
(RT
, X
);
82 if UR_Is_Negative
(X
) then
95 function Compose
(RT
: R
; Fraction
: T
; Exponent
: UI
) return T
is
98 pragma Warnings
(Off
, Arg_Exp
);
100 Decompose
(RT
, Fraction
, Arg_Frac
, Arg_Exp
);
101 return Scaling
(RT
, Arg_Frac
, Exponent
);
108 function Copy_Sign
(RT
: R
; Value
, Sign
: T
) return T
is
109 pragma Warnings
(Off
, RT
);
115 if UR_Is_Negative
(Sign
) then
131 Mode
: Rounding_Mode
:= Round
)
136 Decompose_Int
(RT
, abs X
, Int_F
, Exponent
, Mode
);
138 Fraction
:= UR_From_Components
140 Den
=> Machine_Mantissa_Value
(RT
),
144 if UR_Is_Negative
(X
) then
145 Fraction
:= -Fraction
;
155 -- This procedure should be modified with care, as there are many non-
156 -- obvious details that may cause problems that are hard to detect. For
157 -- zero arguments, Fraction and Exponent are set to zero. Note that sign
158 -- of zero cannot be preserved.
160 procedure Decompose_Int
165 Mode
: Rounding_Mode
)
167 Base
: Int
:= Rbase
(X
);
168 N
: UI
:= abs Numerator
(X
);
169 D
: UI
:= Denominator
(X
);
174 -- True iff Fraction is even
176 Most_Significant_Digit
: constant UI
:=
177 Radix
** (Machine_Mantissa_Value
(RT
) - 1);
179 Uintp_Mark
: Uintp
.Save_Mark
;
180 -- The code is divided into blocks that systematically release
181 -- intermediate values (this routine generates lots of junk).
190 Calculate_D_And_Exponent_1
: begin
194 -- In cases where Base > 1, the actual denominator is Base**D. For
195 -- cases where Base is a power of Radix, use the value 1 for the
196 -- Denominator and adjust the exponent.
198 -- Note: Exponent has different sign from D, because D is a divisor
200 for Power
in 1 .. Radix_Powers
'Last loop
201 if Base
= Radix_Powers
(Power
) then
202 Exponent
:= -D
* Power
;
209 Release_And_Save
(Uintp_Mark
, D
, Exponent
);
210 end Calculate_D_And_Exponent_1
;
213 Calculate_Exponent
: begin
216 -- For bases that are a multiple of the Radix, divide the base by
217 -- Radix and adjust the Exponent. This will help because D will be
218 -- much smaller and faster to process.
220 -- This occurs for decimal bases on machines with binary floating-
221 -- point for example. When calculating 1E40, with Radix = 2, N
222 -- will be 93 bits instead of 133.
230 -- = -------------------------- * Radix
232 -- (Base/Radix) * Radix
235 -- = --------------- * Radix
239 -- This code is commented out, because it causes numerous
240 -- failures in the regression suite. To be studied ???
242 while False and then Base
> 0 and then Base
mod Radix
= 0 loop
243 Base
:= Base
/ Radix
;
244 Exponent
:= Exponent
+ D
;
247 Release_And_Save
(Uintp_Mark
, Exponent
);
248 end Calculate_Exponent
;
250 -- For remaining bases we must actually compute the exponentiation
252 -- Because the exponentiation can be negative, and D must be integer,
253 -- the numerator is corrected instead.
255 Calculate_N_And_D
: begin
259 N
:= N
* Base
** (-D
);
265 Release_And_Save
(Uintp_Mark
, N
, D
);
266 end Calculate_N_And_D
;
271 -- Now scale N and D so that N / D is a value in the interval [1.0 /
272 -- Radix, 1.0) and adjust Exponent accordingly, so the value N / D *
273 -- Radix ** Exponent remains unchanged.
275 -- Step 1 - Adjust N so N / D >= 1 / Radix, or N = 0
277 -- N and D are positive, so N / D >= 1 / Radix implies N * Radix >= D.
278 -- As this scaling is not possible for N is Uint_0, zero is handled
279 -- explicitly at the start of this subprogram.
281 Calculate_N_And_Exponent
: begin
284 N_Times_Radix
:= N
* Radix
;
285 while not (N_Times_Radix
>= D
) loop
287 Exponent
:= Exponent
- 1;
288 N_Times_Radix
:= N
* Radix
;
291 Release_And_Save
(Uintp_Mark
, N
, Exponent
);
292 end Calculate_N_And_Exponent
;
294 -- Step 2 - Adjust D so N / D < 1
296 -- Scale up D so N / D < 1, so N < D
298 Calculate_D_And_Exponent_2
: begin
301 while not (N
< D
) loop
303 -- As N / D >= 1, N / (D * Radix) will be at least 1 / Radix, so
304 -- the result of Step 1 stays valid
307 Exponent
:= Exponent
+ 1;
310 Release_And_Save
(Uintp_Mark
, D
, Exponent
);
311 end Calculate_D_And_Exponent_2
;
313 -- Here the value N / D is in the range [1.0 / Radix .. 1.0)
315 -- Now find the fraction by doing a very simple-minded division until
316 -- enough digits have been computed.
318 -- This division works for all radices, but is only efficient for a
319 -- binary radix. It is just like a manual division algorithm, but
320 -- instead of moving the denominator one digit right, we move the
321 -- numerator one digit left so the numerator and denominator remain
327 Calculate_Fraction_And_N
: begin
333 Fraction
:= Fraction
+ 1;
337 -- Stop when the result is in [1.0 / Radix, 1.0)
339 exit when Fraction
>= Most_Significant_Digit
;
342 Fraction
:= Fraction
* Radix
;
346 Release_And_Save
(Uintp_Mark
, Fraction
, N
);
347 end Calculate_Fraction_And_N
;
349 Calculate_Fraction_And_Exponent
: begin
352 -- Determine correct rounding based on the remainder which is in
353 -- N and the divisor D. The rounding is performed on the absolute
354 -- value of X, so Ceiling and Floor need to check for the sign of
360 -- This rounding mode corresponds to the unbiased rounding
361 -- method that is used at run time. When the real value is
362 -- exactly between two machine numbers, choose the machine
363 -- number with its least significant bit equal to zero.
365 -- The recommendation advice in RM 4.9(38) is that static
366 -- expressions are rounded to machine numbers in the same
367 -- way as the target machine does.
369 if (Even
and then N
* 2 > D
)
371 (not Even
and then N
* 2 >= D
)
373 Fraction
:= Fraction
+ 1;
378 -- Do not round to even as is done with IEEE arithmetic, but
379 -- instead round away from zero when the result is exactly
380 -- between two machine numbers. This biased rounding method
381 -- should not be used to convert static expressions to
382 -- machine numbers, see AI95-268.
385 Fraction
:= Fraction
+ 1;
389 if N
> Uint_0
and then not UR_Is_Negative
(X
) then
390 Fraction
:= Fraction
+ 1;
394 if N
> Uint_0
and then UR_Is_Negative
(X
) then
395 Fraction
:= Fraction
+ 1;
399 -- The result must be normalized to [1.0/Radix, 1.0), so adjust if
400 -- the result is 1.0 because of rounding.
402 if Fraction
= Most_Significant_Digit
* Radix
then
403 Fraction
:= Most_Significant_Digit
;
404 Exponent
:= Exponent
+ 1;
407 -- Put back sign after applying the rounding
409 if UR_Is_Negative
(X
) then
410 Fraction
:= -Fraction
;
413 Release_And_Save
(Uintp_Mark
, Fraction
, Exponent
);
414 end Calculate_Fraction_And_Exponent
;
421 function Exponent
(RT
: R
; X
: T
) return UI
is
424 pragma Warnings
(Off
, X_Frac
);
426 Decompose_Int
(RT
, X
, X_Frac
, X_Exp
, Round_Even
);
434 function Floor
(RT
: R
; X
: T
) return T
is
435 XT
: constant T
:= Truncation
(RT
, X
);
438 if UR_Is_Positive
(X
) then
453 function Fraction
(RT
: R
; X
: T
) return T
is
456 pragma Warnings
(Off
, X_Exp
);
458 Decompose
(RT
, X
, X_Frac
, X_Exp
);
466 function Leading_Part
(RT
: R
; X
: T
; Radix_Digits
: UI
) return T
is
467 RD
: constant UI
:= UI_Min
(Radix_Digits
, Machine_Mantissa_Value
(RT
));
471 L
:= Exponent
(RT
, X
) - RD
;
472 Y
:= UR_From_Uint
(UR_Trunc
(Scaling
(RT
, X
, -L
)));
473 return Scaling
(RT
, Y
, L
);
483 Mode
: Rounding_Mode
;
484 Enode
: Node_Id
) return T
488 Emin
: constant UI
:= Machine_Emin_Value
(RT
);
491 Decompose
(RT
, X
, X_Frac
, X_Exp
, Mode
);
493 -- Case of denormalized number or (gradual) underflow
495 -- A denormalized number is one with the minimum exponent Emin, but that
496 -- breaks the assumption that the first digit of the mantissa is a one.
497 -- This allows the first non-zero digit to be in any of the remaining
498 -- Mant - 1 spots. The gap between subsequent denormalized numbers is
499 -- the same as for the smallest normalized numbers. However, the number
500 -- of significant digits left decreases as a result of the mantissa now
501 -- having leading seros.
505 Emin_Den
: constant UI
:= Machine_Emin_Value
(RT
)
506 - Machine_Mantissa_Value
(RT
) + Uint_1
;
508 if X_Exp
< Emin_Den
or not Has_Denormals
(RT
) then
509 if Has_Signed_Zeros
(RT
) and then UR_Is_Negative
(X
) then
511 ("floating-point value underflows to -0.0?", Enode
);
516 ("floating-point value underflows to 0.0?", Enode
);
520 elsif Has_Denormals
(RT
) then
522 -- Emin - Mant <= X_Exp < Emin, so result is denormal. Handle
523 -- gradual underflow by first computing the number of
524 -- significant bits still available for the mantissa and
525 -- then truncating the fraction to this number of bits.
527 -- If this value is different from the original fraction,
528 -- precision is lost due to gradual underflow.
530 -- We probably should round here and prevent double rounding as
531 -- a result of first rounding to a model number and then to a
532 -- machine number. However, this is an extremely rare case that
533 -- is not worth the extra complexity. In any case, a warning is
534 -- issued in cases where gradual underflow occurs.
537 Denorm_Sig_Bits
: constant UI
:= X_Exp
- Emin_Den
+ 1;
539 X_Frac_Denorm
: constant T
:= UR_From_Components
540 (UR_Trunc
(Scaling
(RT
, abs X_Frac
, Denorm_Sig_Bits
)),
546 if X_Frac_Denorm
/= X_Frac
then
548 ("gradual underflow causes loss of precision?",
550 X_Frac
:= X_Frac_Denorm
;
557 return Scaling
(RT
, X_Frac
, X_Exp
);
564 function Model
(RT
: R
; X
: T
) return T
is
568 Decompose
(RT
, X
, X_Frac
, X_Exp
);
569 return Compose
(RT
, X_Frac
, X_Exp
);
576 function Pred
(RT
: R
; X
: T
) return T
is
578 return -Succ
(RT
, -X
);
585 function Remainder
(RT
: R
; X
, Y
: T
) return T
is
599 pragma Warnings
(Off
, Arg_Frac
);
602 if UR_Is_Positive
(X
) then
614 P_Exp
:= Exponent
(RT
, P
);
617 -- ??? what about zero cases?
618 Decompose
(RT
, Arg
, Arg_Frac
, Arg_Exp
);
619 Decompose
(RT
, P
, P_Frac
, P_Exp
);
621 P
:= Compose
(RT
, P_Frac
, Arg_Exp
);
622 K
:= Arg_Exp
- P_Exp
;
626 for Cnt
in reverse 0 .. UI_To_Int
(K
) loop
627 if IEEE_Rem
>= P
then
629 IEEE_Rem
:= IEEE_Rem
- P
;
638 -- That completes the calculation of modulus remainder. The final step
639 -- is get the IEEE remainder. Here we compare Rem with (abs Y) / 2.
643 B
:= abs Y
* Ureal_Half
;
646 A
:= IEEE_Rem
* Ureal_2
;
650 if A
> B
or else (A
= B
and then not P_Even
) then
651 IEEE_Rem
:= IEEE_Rem
- abs Y
;
654 return Sign_X
* IEEE_Rem
;
661 function Rounding
(RT
: R
; X
: T
) return T
is
666 Result
:= Truncation
(RT
, abs X
);
667 Tail
:= abs X
- Result
;
669 if Tail
>= Ureal_Half
then
670 Result
:= Result
+ Ureal_1
;
673 if UR_Is_Negative
(X
) then
684 function Scaling
(RT
: R
; X
: T
; Adjustment
: UI
) return T
is
685 pragma Warnings
(Off
, RT
);
688 if Rbase
(X
) = Radix
then
689 return UR_From_Components
690 (Num
=> Numerator
(X
),
691 Den
=> Denominator
(X
) - Adjustment
,
693 Negative
=> UR_Is_Negative
(X
));
695 elsif Adjustment
>= 0 then
696 return X
* Radix
** Adjustment
;
698 return X
/ Radix
** (-Adjustment
);
706 function Succ
(RT
: R
; X
: T
) return T
is
707 Emin
: constant UI
:= Machine_Emin_Value
(RT
);
708 Mantissa
: constant UI
:= Machine_Mantissa_Value
(RT
);
709 Exp
: UI
:= UI_Max
(Emin
, Exponent
(RT
, X
));
714 if UR_Is_Zero
(X
) then
718 -- Set exponent such that the radix point will be directly following the
719 -- mantissa after scaling.
721 if Has_Denormals
(RT
) or Exp
/= Emin
then
722 Exp
:= Exp
- Mantissa
;
727 Frac
:= Scaling
(RT
, X
, -Exp
);
728 New_Frac
:= Ceiling
(RT
, Frac
);
730 if New_Frac
= Frac
then
731 if New_Frac
= Scaling
(RT
, -Ureal_1
, Mantissa
- 1) then
732 New_Frac
:= New_Frac
+ Scaling
(RT
, Ureal_1
, Uint_Minus_1
);
734 New_Frac
:= New_Frac
+ Ureal_1
;
738 return Scaling
(RT
, New_Frac
, Exp
);
745 function Truncation
(RT
: R
; X
: T
) return T
is
746 pragma Warnings
(Off
, RT
);
748 return UR_From_Uint
(UR_Trunc
(X
));
751 -----------------------
752 -- Unbiased_Rounding --
753 -----------------------
755 function Unbiased_Rounding
(RT
: R
; X
: T
) return T
is
756 Abs_X
: constant T
:= abs X
;
761 Result
:= Truncation
(RT
, Abs_X
);
762 Tail
:= Abs_X
- Result
;
764 if Tail
> Ureal_Half
then
765 Result
:= Result
+ Ureal_1
;
767 elsif Tail
= Ureal_Half
then
769 Truncation
(RT
, (Result
/ Ureal_2
) + Ureal_Half
);
772 if UR_Is_Negative
(X
) then
774 elsif UR_Is_Positive
(X
) then
777 -- For zero case, make sure sign of zero is preserved
782 end Unbiased_Rounding
;