1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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;
29 with Ttypef; use Ttypef;
30 with Targparm; use Targparm;
32 package body Eval_Fat is
34 Radix : constant Int := 2;
35 -- This code is currently only correct for the radix 2 case. We use
36 -- the symbolic value Radix where possible to help in the unlikely
37 -- case of anyone ever having to adjust this code for another value,
38 -- and for documentation purposes.
40 -- Another assumption is that the range of the floating-point type
41 -- is symmetric around zero.
43 type Radix_Power_Table is array (Int range 1 .. 4) of Int;
45 Radix_Powers : constant Radix_Power_Table :=
46 (Radix ** 1, Radix ** 2, Radix ** 3, Radix ** 4);
48 -----------------------
49 -- Local Subprograms --
50 -----------------------
57 Mode : Rounding_Mode := Round);
58 -- Decomposes a non-zero floating-point number into fraction and
59 -- exponent parts. The fraction is in the interval 1.0 / Radix ..
60 -- T'Pred (1.0) and uses Rbase = Radix.
61 -- The result is rounded to a nearest machine number.
63 procedure Decompose_Int
68 Mode : Rounding_Mode);
69 -- This is similar to Decompose, except that the Fraction value returned
70 -- is an integer representing the value Fraction * Scale, where Scale is
71 -- the value (Radix ** Machine_Mantissa (RT)). The value is obtained by
72 -- using biased rounding (halfway cases round away from zero), round to
73 -- even, a floor operation or a ceiling operation depending on the setting
74 -- of Mode (see corresponding descriptions in Urealp).
76 function Machine_Emin (RT : R) return Int;
77 -- Return value of the Machine_Emin attribute
83 function Adjacent (RT : R; X, Towards : T) return T is
87 elsif Towards > X then
98 function Ceiling (RT : R; X : T) return T is
99 XT : constant T := Truncation (RT, X);
101 if UR_Is_Negative (X) then
114 function Compose (RT : R; Fraction : T; Exponent : UI) return T is
118 if UR_Is_Zero (Fraction) then
121 Decompose (RT, Fraction, Arg_Frac, Arg_Exp);
122 return Scaling (RT, Arg_Frac, Exponent);
130 function Copy_Sign (RT : R; Value, Sign : T) return T is
131 pragma Warnings (Off, RT);
137 if UR_Is_Negative (Sign) then
153 Mode : Rounding_Mode := Round)
158 Decompose_Int (RT, abs X, Int_F, Exponent, Mode);
160 Fraction := UR_From_Components
162 Den => UI_From_Int (Machine_Mantissa (RT)),
166 if UR_Is_Negative (X) then
167 Fraction := -Fraction;
177 -- This procedure should be modified with care, as there are many
178 -- non-obvious details that may cause problems that are hard to
179 -- detect. The cases of positive and negative zeroes are also
180 -- special and should be verified separately.
182 procedure Decompose_Int
187 Mode : Rounding_Mode)
189 Base : Int := Rbase (X);
190 N : UI := abs Numerator (X);
191 D : UI := Denominator (X);
196 -- True iff Fraction is even
198 Most_Significant_Digit : constant UI :=
199 Radix ** (Machine_Mantissa (RT) - 1);
201 Uintp_Mark : Uintp.Save_Mark;
202 -- The code is divided into blocks that systematically release
203 -- intermediate values (this routine generates lots of junk!)
206 Calculate_D_And_Exponent_1 : begin
210 -- In cases where Base > 1, the actual denominator is
211 -- Base**D. For cases where Base is a power of Radix, use
212 -- the value 1 for the Denominator and adjust the exponent.
214 -- Note: Exponent has different sign from D, because D is a divisor
216 for Power in 1 .. Radix_Powers'Last loop
217 if Base = Radix_Powers (Power) then
218 Exponent := -D * Power;
225 Release_And_Save (Uintp_Mark, D, Exponent);
226 end Calculate_D_And_Exponent_1;
229 Calculate_Exponent : begin
232 -- For bases that are a multiple of the Radix, divide
233 -- the base by Radix and adjust the Exponent. This will
234 -- help because D will be much smaller and faster to process.
236 -- This occurs for decimal bases on a machine with binary
237 -- floating-point for example. When calculating 1E40,
238 -- with Radix = 2, N will be 93 bits instead of 133.
246 -- = -------------------------- * Radix
248 -- (Base/Radix) * Radix
251 -- = --------------- * Radix
255 -- This code is commented out, because it causes numerous
256 -- failures in the regression suite. To be studied ???
258 while False and then Base > 0 and then Base mod Radix = 0 loop
259 Base := Base / Radix;
260 Exponent := Exponent + D;
263 Release_And_Save (Uintp_Mark, Exponent);
264 end Calculate_Exponent;
266 -- For remaining bases we must actually compute
267 -- the exponentiation.
269 -- Because the exponentiation can be negative, and D must
270 -- be integer, the numerator is corrected instead.
272 Calculate_N_And_D : begin
276 N := N * Base ** (-D);
282 Release_And_Save (Uintp_Mark, N, D);
283 end Calculate_N_And_D;
288 -- Now scale N and D so that N / D is a value in the
289 -- interval [1.0 / Radix, 1.0) and adjust Exponent accordingly,
290 -- so the value N / D * Radix ** Exponent remains unchanged.
292 -- Step 1 - Adjust N so N / D >= 1 / Radix, or N = 0
294 -- N and D are positive, so N / D >= 1 / Radix implies N * Radix >= D.
295 -- This scaling is not possible for N is Uint_0 as there
296 -- is no way to scale Uint_0 so the first digit is non-zero.
298 Calculate_N_And_Exponent : begin
301 N_Times_Radix := N * Radix;
304 while not (N_Times_Radix >= D) loop
306 Exponent := Exponent - 1;
308 N_Times_Radix := N * Radix;
312 Release_And_Save (Uintp_Mark, N, Exponent);
313 end Calculate_N_And_Exponent;
315 -- Step 2 - Adjust D so N / D < 1
317 -- Scale up D so N / D < 1, so N < D
319 Calculate_D_And_Exponent_2 : begin
322 while not (N < D) loop
324 -- As N / D >= 1, N / (D * Radix) will be at least 1 / Radix,
325 -- so the result of Step 1 stays valid
328 Exponent := Exponent + 1;
331 Release_And_Save (Uintp_Mark, D, Exponent);
332 end Calculate_D_And_Exponent_2;
334 -- Here the value N / D is in the range [1.0 / Radix .. 1.0)
336 -- Now find the fraction by doing a very simple-minded
337 -- division until enough digits have been computed.
339 -- This division works for all radices, but is only efficient for
340 -- a binary radix. It is just like a manual division algorithm,
341 -- but instead of moving the denominator one digit right, we move
342 -- the numerator one digit left so the numerator and denominator
348 Calculate_Fraction_And_N : begin
354 Fraction := Fraction + 1;
358 -- Stop when the result is in [1.0 / Radix, 1.0)
360 exit when Fraction >= Most_Significant_Digit;
363 Fraction := Fraction * Radix;
367 Release_And_Save (Uintp_Mark, Fraction, N);
368 end Calculate_Fraction_And_N;
370 Calculate_Fraction_And_Exponent : begin
373 -- Determine correct rounding based on the remainder which is in
374 -- N and the divisor D. The rounding is performed on the absolute
375 -- value of X, so Ceiling and Floor need to check for the sign of
381 -- This rounding mode should not be used for static
382 -- expressions, but only for compile-time evaluation
383 -- of non-static expressions.
385 if (Even and then N * 2 > D)
387 (not Even and then N * 2 >= D)
389 Fraction := Fraction + 1;
394 -- Do not round to even as is done with IEEE arithmetic,
395 -- but instead round away from zero when the result is
396 -- exactly between two machine numbers. See RM 4.9(38).
399 Fraction := Fraction + 1;
403 if N > Uint_0 and then not UR_Is_Negative (X) then
404 Fraction := Fraction + 1;
408 if N > Uint_0 and then UR_Is_Negative (X) then
409 Fraction := Fraction + 1;
413 -- The result must be normalized to [1.0/Radix, 1.0),
414 -- so adjust if the result is 1.0 because of rounding.
416 if Fraction = Most_Significant_Digit * Radix then
417 Fraction := Most_Significant_Digit;
418 Exponent := Exponent + 1;
421 -- Put back sign after applying the rounding
423 if UR_Is_Negative (X) then
424 Fraction := -Fraction;
427 Release_And_Save (Uintp_Mark, Fraction, Exponent);
428 end Calculate_Fraction_And_Exponent;
435 function Exponent (RT : R; X : T) return UI is
439 if UR_Is_Zero (X) then
442 Decompose_Int (RT, X, X_Frac, X_Exp, Round_Even);
451 function Floor (RT : R; X : T) return T is
452 XT : constant T := Truncation (RT, X);
455 if UR_Is_Positive (X) then
470 function Fraction (RT : R; X : T) return T is
474 if UR_Is_Zero (X) then
477 Decompose (RT, X, X_Frac, X_Exp);
486 function Leading_Part (RT : R; X : T; Radix_Digits : UI) return T is
487 RD : constant UI := UI_Min (Radix_Digits, Machine_Mantissa (RT));
491 L := Exponent (RT, X) - RD;
492 Y := UR_From_Uint (UR_Trunc (Scaling (RT, X, -L)));
493 return Scaling (RT, Y, L);
503 Mode : Rounding_Mode;
504 Enode : Node_Id) return T
508 Emin : constant UI := UI_From_Int (Machine_Emin (RT));
511 if UR_Is_Zero (X) then
515 Decompose (RT, X, X_Frac, X_Exp, Mode);
517 -- Case of denormalized number or (gradual) underflow
519 -- A denormalized number is one with the minimum exponent Emin, but
520 -- that breaks the assumption that the first digit of the mantissa
521 -- is a one. This allows the first non-zero digit to be in any
522 -- of the remaining Mant - 1 spots. The gap between subsequent
523 -- denormalized numbers is the same as for the smallest normalized
524 -- numbers. However, the number of significant digits left decreases
525 -- as a result of the mantissa now having leading seros.
529 Emin_Den : constant UI :=
531 (Machine_Emin (RT) - Machine_Mantissa (RT) + 1);
533 if X_Exp < Emin_Den or not Denorm_On_Target then
534 if UR_Is_Negative (X) then
536 ("floating-point value underflows to -0.0?", Enode);
541 ("floating-point value underflows to 0.0?", Enode);
545 elsif Denorm_On_Target then
547 -- Emin - Mant <= X_Exp < Emin, so result is denormal.
548 -- Handle gradual underflow by first computing the
549 -- number of significant bits still available for the
550 -- mantissa and then truncating the fraction to this
553 -- If this value is different from the original
554 -- fraction, precision is lost due to gradual underflow.
556 -- We probably should round here and prevent double
557 -- rounding as a result of first rounding to a model
558 -- number and then to a machine number. However, this
559 -- is an extremely rare case that is not worth the extra
560 -- complexity. In any case, a warning is issued in cases
561 -- where gradual underflow occurs.
564 Denorm_Sig_Bits : constant UI := X_Exp - Emin_Den + 1;
566 X_Frac_Denorm : constant T := UR_From_Components
567 (UR_Trunc (Scaling (RT, abs X_Frac, Denorm_Sig_Bits)),
573 if X_Frac_Denorm /= X_Frac then
575 ("gradual underflow causes loss of precision?",
577 X_Frac := X_Frac_Denorm;
584 return Scaling (RT, X_Frac, X_Exp);
592 function Machine_Emin (RT : R) return Int is
593 Digs : constant UI := Digits_Value (RT);
597 if Vax_Float (RT) then
598 if Digs = VAXFF_Digits then
599 Emin := VAXFF_Machine_Emin;
601 elsif Digs = VAXDF_Digits then
602 Emin := VAXDF_Machine_Emin;
605 pragma Assert (Digs = VAXGF_Digits);
606 Emin := VAXGF_Machine_Emin;
609 elsif Is_AAMP_Float (RT) then
610 if Digs = AAMPS_Digits then
611 Emin := AAMPS_Machine_Emin;
614 pragma Assert (Digs = AAMPL_Digits);
615 Emin := AAMPL_Machine_Emin;
619 if Digs = IEEES_Digits then
620 Emin := IEEES_Machine_Emin;
622 elsif Digs = IEEEL_Digits then
623 Emin := IEEEL_Machine_Emin;
626 pragma Assert (Digs = IEEEX_Digits);
627 Emin := IEEEX_Machine_Emin;
634 ----------------------
635 -- Machine_Mantissa --
636 ----------------------
638 function Machine_Mantissa (RT : R) return Nat is
639 Digs : constant UI := Digits_Value (RT);
643 if Vax_Float (RT) then
644 if Digs = VAXFF_Digits then
645 Mant := VAXFF_Machine_Mantissa;
647 elsif Digs = VAXDF_Digits then
648 Mant := VAXDF_Machine_Mantissa;
651 pragma Assert (Digs = VAXGF_Digits);
652 Mant := VAXGF_Machine_Mantissa;
655 elsif Is_AAMP_Float (RT) then
656 if Digs = AAMPS_Digits then
657 Mant := AAMPS_Machine_Mantissa;
660 pragma Assert (Digs = AAMPL_Digits);
661 Mant := AAMPL_Machine_Mantissa;
665 if Digs = IEEES_Digits then
666 Mant := IEEES_Machine_Mantissa;
668 elsif Digs = IEEEL_Digits then
669 Mant := IEEEL_Machine_Mantissa;
672 pragma Assert (Digs = IEEEX_Digits);
673 Mant := IEEEX_Machine_Mantissa;
678 end Machine_Mantissa;
684 function Machine_Radix (RT : R) return Nat is
685 pragma Warnings (Off, RT);
694 function Model (RT : R; X : T) return T is
698 Decompose (RT, X, X_Frac, X_Exp);
699 return Compose (RT, X_Frac, X_Exp);
706 function Pred (RT : R; X : T) return T is
708 return -Succ (RT, -X);
715 function Remainder (RT : R; X, Y : T) return T is
730 if UR_Is_Positive (X) then
742 P_Exp := Exponent (RT, P);
745 -- ??? what about zero cases?
746 Decompose (RT, Arg, Arg_Frac, Arg_Exp);
747 Decompose (RT, P, P_Frac, P_Exp);
749 P := Compose (RT, P_Frac, Arg_Exp);
750 K := Arg_Exp - P_Exp;
754 for Cnt in reverse 0 .. UI_To_Int (K) loop
755 if IEEE_Rem >= P then
757 IEEE_Rem := IEEE_Rem - P;
766 -- That completes the calculation of modulus remainder. The final step
767 -- is get the IEEE remainder. Here we compare Rem with (abs Y) / 2.
771 B := abs Y * Ureal_Half;
774 A := IEEE_Rem * Ureal_2;
778 if A > B or else (A = B and then not P_Even) then
779 IEEE_Rem := IEEE_Rem - abs Y;
782 return Sign_X * IEEE_Rem;
789 function Rounding (RT : R; X : T) return T is
794 Result := Truncation (RT, abs X);
795 Tail := abs X - Result;
797 if Tail >= Ureal_Half then
798 Result := Result + Ureal_1;
801 if UR_Is_Negative (X) then
812 function Scaling (RT : R; X : T; Adjustment : UI) return T is
813 pragma Warnings (Off, RT);
816 if Rbase (X) = Radix then
817 return UR_From_Components
818 (Num => Numerator (X),
819 Den => Denominator (X) - Adjustment,
821 Negative => UR_Is_Negative (X));
823 elsif Adjustment >= 0 then
824 return X * Radix ** Adjustment;
826 return X / Radix ** (-Adjustment);
834 function Succ (RT : R; X : T) return T is
835 Emin : constant UI := UI_From_Int (Machine_Emin (RT));
836 Mantissa : constant UI := UI_From_Int (Machine_Mantissa (RT));
837 Exp : UI := UI_Max (Emin, Exponent (RT, X));
842 if UR_Is_Zero (X) then
846 -- Set exponent such that the radix point will be directly
847 -- following the mantissa after scaling
849 if Denorm_On_Target or Exp /= Emin then
850 Exp := Exp - Mantissa;
855 Frac := Scaling (RT, X, -Exp);
856 New_Frac := Ceiling (RT, Frac);
858 if New_Frac = Frac then
859 if New_Frac = Scaling (RT, -Ureal_1, Mantissa - 1) then
860 New_Frac := New_Frac + Scaling (RT, Ureal_1, Uint_Minus_1);
862 New_Frac := New_Frac + Ureal_1;
866 return Scaling (RT, New_Frac, Exp);
873 function Truncation (RT : R; X : T) return T is
874 pragma Warnings (Off, RT);
876 return UR_From_Uint (UR_Trunc (X));
879 -----------------------
880 -- Unbiased_Rounding --
881 -----------------------
883 function Unbiased_Rounding (RT : R; X : T) return T is
884 Abs_X : constant T := abs X;
889 Result := Truncation (RT, Abs_X);
890 Tail := Abs_X - Result;
892 if Tail > Ureal_Half then
893 Result := Result + Ureal_1;
895 elsif Tail = Ureal_Half then
897 Truncation (RT, (Result / Ureal_2) + Ureal_Half);
900 if UR_Is_Negative (X) then
902 elsif UR_Is_Positive (X) then
905 -- For zero case, make sure sign of zero is preserved
910 end Unbiased_Rounding;