1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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 Targparm; use Targparm;
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.
60 procedure Decompose_Int
65 Mode : Rounding_Mode);
66 -- This is similar to Decompose, except that the Fraction value returned
67 -- is an integer representing the value Fraction * Scale, where Scale is
68 -- the value (Machine_Radix_Value (RT) ** Machine_Mantissa_Value (RT)). The
69 -- value is obtained by using biased rounding (halfway cases round away
70 -- from zero), round to even, a floor operation or a ceiling operation
71 -- depending on the setting of Mode (see corresponding descriptions in
78 function Adjacent (RT : R; X, Towards : T) return T is
82 elsif Towards > X then
93 function Ceiling (RT : R; X : T) return T is
94 XT : constant T := Truncation (RT, X);
96 if UR_Is_Negative (X) then
109 function Compose (RT : R; Fraction : T; Exponent : UI) return T is
112 pragma Warnings (Off, Arg_Exp);
114 Decompose (RT, Fraction, Arg_Frac, Arg_Exp);
115 return Scaling (RT, Arg_Frac, Exponent);
122 function Copy_Sign (RT : R; Value, Sign : T) return T is
123 pragma Warnings (Off, RT);
129 if UR_Is_Negative (Sign) then
145 Mode : Rounding_Mode := Round)
150 Decompose_Int (RT, abs X, Int_F, Exponent, Mode);
152 Fraction := UR_From_Components
154 Den => Machine_Mantissa_Value (RT),
158 if UR_Is_Negative (X) then
159 Fraction := -Fraction;
169 -- This procedure should be modified with care, as there are many non-
170 -- obvious details that may cause problems that are hard to detect. For
171 -- zero arguments, Fraction and Exponent are set to zero. Note that sign
172 -- of zero cannot be preserved.
174 procedure Decompose_Int
179 Mode : Rounding_Mode)
181 Base : Int := Rbase (X);
182 N : UI := abs Numerator (X);
183 D : UI := Denominator (X);
188 -- True iff Fraction is even
190 Most_Significant_Digit : constant UI :=
191 Radix ** (Machine_Mantissa_Value (RT) - 1);
193 Uintp_Mark : Uintp.Save_Mark;
194 -- The code is divided into blocks that systematically release
195 -- intermediate values (this routine generates lots of junk!)
204 Calculate_D_And_Exponent_1 : begin
208 -- In cases where Base > 1, the actual denominator is Base**D. For
209 -- cases where Base is a power of Radix, use the value 1 for the
210 -- Denominator and adjust the exponent.
212 -- Note: Exponent has different sign from D, because D is a divisor
214 for Power in 1 .. Radix_Powers'Last loop
215 if Base = Radix_Powers (Power) then
216 Exponent := -D * Power;
223 Release_And_Save (Uintp_Mark, D, Exponent);
224 end Calculate_D_And_Exponent_1;
227 Calculate_Exponent : begin
230 -- For bases that are a multiple of the Radix, divide the base by
231 -- Radix and adjust the Exponent. This will help because D will be
232 -- much smaller and faster to process.
234 -- This occurs for decimal bases on machines with binary floating-
235 -- point for example. When calculating 1E40, with Radix = 2, N
236 -- will be 93 bits instead of 133.
244 -- = -------------------------- * Radix
246 -- (Base/Radix) * Radix
249 -- = --------------- * Radix
253 -- This code is commented out, because it causes numerous
254 -- failures in the regression suite. To be studied ???
256 while False and then Base > 0 and then Base mod Radix = 0 loop
257 Base := Base / Radix;
258 Exponent := Exponent + D;
261 Release_And_Save (Uintp_Mark, Exponent);
262 end Calculate_Exponent;
264 -- For remaining bases we must actually compute the exponentiation
266 -- Because the exponentiation can be negative, and D must be integer,
267 -- the numerator is corrected instead.
269 Calculate_N_And_D : begin
273 N := N * Base ** (-D);
279 Release_And_Save (Uintp_Mark, N, D);
280 end Calculate_N_And_D;
285 -- Now scale N and D so that N / D is a value in the interval [1.0 /
286 -- Radix, 1.0) and adjust Exponent accordingly, so the value N / D *
287 -- Radix ** Exponent remains unchanged.
289 -- Step 1 - Adjust N so N / D >= 1 / Radix, or N = 0
291 -- N and D are positive, so N / D >= 1 / Radix implies N * Radix >= D.
292 -- As this scaling is not possible for N is Uint_0, zero is handled
293 -- explicitly at the start of this subprogram.
295 Calculate_N_And_Exponent : begin
298 N_Times_Radix := N * Radix;
299 while not (N_Times_Radix >= D) loop
301 Exponent := Exponent - 1;
302 N_Times_Radix := N * Radix;
305 Release_And_Save (Uintp_Mark, N, Exponent);
306 end Calculate_N_And_Exponent;
308 -- Step 2 - Adjust D so N / D < 1
310 -- Scale up D so N / D < 1, so N < D
312 Calculate_D_And_Exponent_2 : begin
315 while not (N < D) loop
317 -- As N / D >= 1, N / (D * Radix) will be at least 1 / Radix, so
318 -- the result of Step 1 stays valid
321 Exponent := Exponent + 1;
324 Release_And_Save (Uintp_Mark, D, Exponent);
325 end Calculate_D_And_Exponent_2;
327 -- Here the value N / D is in the range [1.0 / Radix .. 1.0)
329 -- Now find the fraction by doing a very simple-minded division until
330 -- enough digits have been computed.
332 -- This division works for all radices, but is only efficient for a
333 -- binary radix. It is just like a manual division algorithm, but
334 -- instead of moving the denominator one digit right, we move the
335 -- numerator one digit left so the numerator and denominator remain
341 Calculate_Fraction_And_N : begin
347 Fraction := Fraction + 1;
351 -- Stop when the result is in [1.0 / Radix, 1.0)
353 exit when Fraction >= Most_Significant_Digit;
356 Fraction := Fraction * Radix;
360 Release_And_Save (Uintp_Mark, Fraction, N);
361 end Calculate_Fraction_And_N;
363 Calculate_Fraction_And_Exponent : begin
366 -- Determine correct rounding based on the remainder which is in
367 -- N and the divisor D. The rounding is performed on the absolute
368 -- value of X, so Ceiling and Floor need to check for the sign of
374 -- This rounding mode should not be used for static
375 -- expressions, but only for compile-time evaluation of
376 -- non-static expressions.
378 if (Even and then N * 2 > D)
380 (not Even and then N * 2 >= D)
382 Fraction := Fraction + 1;
387 -- Do not round to even as is done with IEEE arithmetic, but
388 -- instead round away from zero when the result is exactly
389 -- between two machine numbers. See RM 4.9(38).
392 Fraction := Fraction + 1;
396 if N > Uint_0 and then not UR_Is_Negative (X) then
397 Fraction := Fraction + 1;
401 if N > Uint_0 and then UR_Is_Negative (X) then
402 Fraction := Fraction + 1;
406 -- The result must be normalized to [1.0/Radix, 1.0), so adjust if
407 -- the result is 1.0 because of rounding.
409 if Fraction = Most_Significant_Digit * Radix then
410 Fraction := Most_Significant_Digit;
411 Exponent := Exponent + 1;
414 -- Put back sign after applying the rounding
416 if UR_Is_Negative (X) then
417 Fraction := -Fraction;
420 Release_And_Save (Uintp_Mark, Fraction, Exponent);
421 end Calculate_Fraction_And_Exponent;
428 function Exponent (RT : R; X : T) return UI is
431 pragma Warnings (Off, X_Frac);
433 Decompose_Int (RT, X, X_Frac, X_Exp, Round_Even);
441 function Floor (RT : R; X : T) return T is
442 XT : constant T := Truncation (RT, X);
445 if UR_Is_Positive (X) then
460 function Fraction (RT : R; X : T) return T is
463 pragma Warnings (Off, X_Exp);
465 Decompose (RT, X, X_Frac, X_Exp);
473 function Leading_Part (RT : R; X : T; Radix_Digits : UI) return T is
474 RD : constant UI := UI_Min (Radix_Digits, Machine_Mantissa_Value (RT));
478 L := Exponent (RT, X) - RD;
479 Y := UR_From_Uint (UR_Trunc (Scaling (RT, X, -L)));
480 return Scaling (RT, Y, L);
490 Mode : Rounding_Mode;
491 Enode : Node_Id) return T
495 Emin : constant UI := Machine_Emin_Value (RT);
498 Decompose (RT, X, X_Frac, X_Exp, Mode);
500 -- Case of denormalized number or (gradual) underflow
502 -- A denormalized number is one with the minimum exponent Emin, but that
503 -- breaks the assumption that the first digit of the mantissa is a one.
504 -- This allows the first non-zero digit to be in any of the remaining
505 -- Mant - 1 spots. The gap between subsequent denormalized numbers is
506 -- the same as for the smallest normalized numbers. However, the number
507 -- of significant digits left decreases as a result of the mantissa now
508 -- having leading seros.
512 Emin_Den : constant UI := Machine_Emin_Value (RT)
513 - Machine_Mantissa_Value (RT) + Uint_1;
515 if X_Exp < Emin_Den or not Denorm_On_Target then
516 if UR_Is_Negative (X) then
518 ("floating-point value underflows to -0.0?", Enode);
523 ("floating-point value underflows to 0.0?", Enode);
527 elsif Denorm_On_Target then
529 -- Emin - Mant <= X_Exp < Emin, so result is denormal. Handle
530 -- gradual underflow by first computing the number of
531 -- significant bits still available for the mantissa and
532 -- then truncating the fraction to this number of bits.
534 -- If this value is different from the original fraction,
535 -- precision is lost due to gradual underflow.
537 -- We probably should round here and prevent double rounding as
538 -- a result of first rounding to a model number and then to a
539 -- machine number. However, this is an extremely rare case that
540 -- is not worth the extra complexity. In any case, a warning is
541 -- issued in cases where gradual underflow occurs.
544 Denorm_Sig_Bits : constant UI := X_Exp - Emin_Den + 1;
546 X_Frac_Denorm : constant T := UR_From_Components
547 (UR_Trunc (Scaling (RT, abs X_Frac, Denorm_Sig_Bits)),
553 if X_Frac_Denorm /= X_Frac then
555 ("gradual underflow causes loss of precision?",
557 X_Frac := X_Frac_Denorm;
564 return Scaling (RT, X_Frac, X_Exp);
571 function Model (RT : R; X : T) return T is
575 Decompose (RT, X, X_Frac, X_Exp);
576 return Compose (RT, X_Frac, X_Exp);
583 function Pred (RT : R; X : T) return T is
585 return -Succ (RT, -X);
592 function Remainder (RT : R; X, Y : T) return T is
606 pragma Warnings (Off, Arg_Frac);
609 if UR_Is_Positive (X) then
621 P_Exp := Exponent (RT, P);
624 -- ??? what about zero cases?
625 Decompose (RT, Arg, Arg_Frac, Arg_Exp);
626 Decompose (RT, P, P_Frac, P_Exp);
628 P := Compose (RT, P_Frac, Arg_Exp);
629 K := Arg_Exp - P_Exp;
633 for Cnt in reverse 0 .. UI_To_Int (K) loop
634 if IEEE_Rem >= P then
636 IEEE_Rem := IEEE_Rem - P;
645 -- That completes the calculation of modulus remainder. The final step
646 -- is get the IEEE remainder. Here we compare Rem with (abs Y) / 2.
650 B := abs Y * Ureal_Half;
653 A := IEEE_Rem * Ureal_2;
657 if A > B or else (A = B and then not P_Even) then
658 IEEE_Rem := IEEE_Rem - abs Y;
661 return Sign_X * IEEE_Rem;
668 function Rounding (RT : R; X : T) return T is
673 Result := Truncation (RT, abs X);
674 Tail := abs X - Result;
676 if Tail >= Ureal_Half then
677 Result := Result + Ureal_1;
680 if UR_Is_Negative (X) then
691 function Scaling (RT : R; X : T; Adjustment : UI) return T is
692 pragma Warnings (Off, RT);
695 if Rbase (X) = Radix then
696 return UR_From_Components
697 (Num => Numerator (X),
698 Den => Denominator (X) - Adjustment,
700 Negative => UR_Is_Negative (X));
702 elsif Adjustment >= 0 then
703 return X * Radix ** Adjustment;
705 return X / Radix ** (-Adjustment);
713 function Succ (RT : R; X : T) return T is
714 Emin : constant UI := Machine_Emin_Value (RT);
715 Mantissa : constant UI := Machine_Mantissa_Value (RT);
716 Exp : UI := UI_Max (Emin, Exponent (RT, X));
721 if UR_Is_Zero (X) then
725 -- Set exponent such that the radix point will be directly following the
726 -- mantissa after scaling.
728 if Denorm_On_Target or Exp /= Emin then
729 Exp := Exp - Mantissa;
734 Frac := Scaling (RT, X, -Exp);
735 New_Frac := Ceiling (RT, Frac);
737 if New_Frac = Frac then
738 if New_Frac = Scaling (RT, -Ureal_1, Mantissa - 1) then
739 New_Frac := New_Frac + Scaling (RT, Ureal_1, Uint_Minus_1);
741 New_Frac := New_Frac + Ureal_1;
745 return Scaling (RT, New_Frac, Exp);
752 function Truncation (RT : R; X : T) return T is
753 pragma Warnings (Off, RT);
755 return UR_From_Uint (UR_Trunc (X));
758 -----------------------
759 -- Unbiased_Rounding --
760 -----------------------
762 function Unbiased_Rounding (RT : R; X : T) return T is
763 Abs_X : constant T := abs X;
768 Result := Truncation (RT, Abs_X);
769 Tail := Abs_X - Result;
771 if Tail > Ureal_Half then
772 Result := Result + Ureal_1;
774 elsif Tail = Ureal_Half then
776 Truncation (RT, (Result / Ureal_2) + Ureal_Half);
779 if UR_Is_Negative (X) then
781 elsif UR_Is_Positive (X) then
784 -- For zero case, make sure sign of zero is preserved
789 end Unbiased_Rounding;