1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Einfo; use Einfo;
30 with Sem_Util; use Sem_Util;
31 with Ttypef; use Ttypef;
32 with Targparm; use Targparm;
34 package body Eval_Fat is
36 Radix : constant Int := 2;
37 -- This code is currently only correct for the radix 2 case. We use
38 -- the symbolic value Radix where possible to help in the unlikely
39 -- case of anyone ever having to adjust this code for another value,
40 -- and for documentation purposes.
42 type Radix_Power_Table is array (Int range 1 .. 4) of Int;
44 Radix_Powers : constant Radix_Power_Table
45 := (Radix**1, Radix**2, Radix**3, Radix**4);
47 function Float_Radix return T renames Ureal_2;
48 -- Radix expressed in real form
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
59 Mode : Rounding_Mode := Round);
60 -- Decomposes a non-zero floating-point number into fraction and
61 -- exponent parts. The fraction is in the interval 1.0 / Radix ..
62 -- T'Pred (1.0) and uses Rbase = Radix.
63 -- The result is rounded to a nearest machine number.
65 procedure Decompose_Int
70 Mode : Rounding_Mode);
71 -- This is similar to Decompose, except that the Fraction value returned
72 -- is an integer representing the value Fraction * Scale, where Scale is
73 -- the value (Radix ** Machine_Mantissa (RT)). The value is obtained by
74 -- using biased rounding (halfway cases round away from zero), round to
75 -- even, a floor operation or a ceiling operation depending on the setting
76 -- of Mode (see corresponding descriptions in Urealp).
77 -- In case rounding was specified, Rounding_Was_Biased is set True
78 -- if the input was indeed halfway between to machine numbers and
79 -- got rounded away from zero to an odd number.
81 function Eps_Model (RT : R) return T;
82 -- Return the smallest model number of R.
84 function Eps_Denorm (RT : R) return T;
85 -- Return the smallest denormal of type R.
87 function Machine_Mantissa (RT : R) return Nat;
88 -- Get value of machine mantissa
94 function Adjacent (RT : R; X, Towards : T) return T is
99 elsif Towards > X then
111 function Ceiling (RT : R; X : T) return T is
112 XT : constant T := Truncation (RT, X);
115 if UR_Is_Negative (X) then
130 function Compose (RT : R; Fraction : T; Exponent : UI) return T is
135 if UR_Is_Zero (Fraction) then
138 Decompose (RT, Fraction, Arg_Frac, Arg_Exp);
139 return Scaling (RT, Arg_Frac, Exponent);
147 function Copy_Sign (RT : R; Value, Sign : T) return T is
148 pragma Warnings (Off, RT);
154 if UR_Is_Negative (Sign) then
170 Mode : Rounding_Mode := Round)
175 Decompose_Int (RT, abs X, Int_F, Exponent, Mode);
177 Fraction := UR_From_Components
179 Den => UI_From_Int (Machine_Mantissa (RT)),
183 if UR_Is_Negative (X) then
184 Fraction := -Fraction;
194 -- This procedure should be modified with care, as there
195 -- are many non-obvious details that may cause problems
196 -- that are hard to detect. The cases of positive and
197 -- negative zeroes are also special and should be
198 -- verified separately.
200 procedure Decompose_Int
205 Mode : Rounding_Mode)
207 Base : Int := Rbase (X);
208 N : UI := abs Numerator (X);
209 D : UI := Denominator (X);
214 -- True iff Fraction is even
216 Most_Significant_Digit : constant UI :=
217 Radix ** (Machine_Mantissa (RT) - 1);
219 Uintp_Mark : Uintp.Save_Mark;
220 -- The code is divided into blocks that systematically release
221 -- intermediate values (this routine generates lots of junk!)
224 Calculate_D_And_Exponent_1 : begin
228 -- In cases where Base > 1, the actual denominator is
229 -- Base**D. For cases where Base is a power of Radix, use
230 -- the value 1 for the Denominator and adjust the exponent.
232 -- Note: Exponent has different sign from D, because D is a divisor
234 for Power in 1 .. Radix_Powers'Last loop
235 if Base = Radix_Powers (Power) then
236 Exponent := -D * Power;
243 Release_And_Save (Uintp_Mark, D, Exponent);
244 end Calculate_D_And_Exponent_1;
247 Calculate_Exponent : begin
250 -- For bases that are a multiple of the Radix, divide
251 -- the base by Radix and adjust the Exponent. This will
252 -- help because D will be much smaller and faster to process.
254 -- This occurs for decimal bases on a machine with binary
255 -- floating-point for example. When calculating 1E40,
256 -- with Radix = 2, N will be 93 bits instead of 133.
264 -- = -------------------------- * Radix
266 -- (Base/Radix) * Radix
269 -- = --------------- * Radix
273 -- This code is commented out, because it causes numerous
274 -- failures in the regression suite. To be studied ???
276 while False and then Base > 0 and then Base mod Radix = 0 loop
277 Base := Base / Radix;
278 Exponent := Exponent + D;
281 Release_And_Save (Uintp_Mark, Exponent);
282 end Calculate_Exponent;
284 -- For remaining bases we must actually compute
285 -- the exponentiation.
287 -- Because the exponentiation can be negative, and D must
288 -- be integer, the numerator is corrected instead.
290 Calculate_N_And_D : begin
294 N := N * Base ** (-D);
300 Release_And_Save (Uintp_Mark, N, D);
301 end Calculate_N_And_D;
306 -- Now scale N and D so that N / D is a value in the
307 -- interval [1.0 / Radix, 1.0) and adjust Exponent accordingly,
308 -- so the value N / D * Radix ** Exponent remains unchanged.
310 -- Step 1 - Adjust N so N / D >= 1 / Radix, or N = 0
312 -- N and D are positive, so N / D >= 1 / Radix implies N * Radix >= D.
313 -- This scaling is not possible for N is Uint_0 as there
314 -- is no way to scale Uint_0 so the first digit is non-zero.
316 Calculate_N_And_Exponent : begin
319 N_Times_Radix := N * Radix;
322 while not (N_Times_Radix >= D) loop
324 Exponent := Exponent - 1;
326 N_Times_Radix := N * Radix;
330 Release_And_Save (Uintp_Mark, N, Exponent);
331 end Calculate_N_And_Exponent;
333 -- Step 2 - Adjust D so N / D < 1
335 -- Scale up D so N / D < 1, so N < D
337 Calculate_D_And_Exponent_2 : begin
340 while not (N < D) loop
342 -- As N / D >= 1, N / (D * Radix) will be at least 1 / Radix,
343 -- so the result of Step 1 stays valid
346 Exponent := Exponent + 1;
349 Release_And_Save (Uintp_Mark, D, Exponent);
350 end Calculate_D_And_Exponent_2;
352 -- Here the value N / D is in the range [1.0 / Radix .. 1.0)
354 -- Now find the fraction by doing a very simple-minded
355 -- division until enough digits have been computed.
357 -- This division works for all radices, but is only efficient for
358 -- a binary radix. It is just like a manual division algorithm,
359 -- but instead of moving the denominator one digit right, we move
360 -- the numerator one digit left so the numerator and denominator
366 Calculate_Fraction_And_N : begin
372 Fraction := Fraction + 1;
376 -- Stop when the result is in [1.0 / Radix, 1.0)
378 exit when Fraction >= Most_Significant_Digit;
381 Fraction := Fraction * Radix;
385 Release_And_Save (Uintp_Mark, Fraction, N);
386 end Calculate_Fraction_And_N;
388 Calculate_Fraction_And_Exponent : begin
391 -- Put back sign before applying the rounding.
393 if UR_Is_Negative (X) then
394 Fraction := -Fraction;
397 -- Determine correct rounding based on the remainder
398 -- which is in N and the divisor D.
400 Rounding_Was_Biased := False; -- Until proven otherwise
405 -- This rounding mode should not be used for static
406 -- expressions, but only for compile-time evaluation
407 -- of non-static expressions.
409 if (Even and then N * 2 > D)
411 (not Even and then N * 2 >= D)
413 Fraction := Fraction + 1;
418 -- Do not round to even as is done with IEEE arithmetic,
419 -- but instead round away from zero when the result is
420 -- exactly between two machine numbers. See RM 4.9(38).
423 Fraction := Fraction + 1;
425 Rounding_Was_Biased := Even and then N * 2 = D;
426 -- Check for the case where the result is actually
427 -- different from Round_Even.
432 Fraction := Fraction + 1;
438 -- The result must be normalized to [1.0/Radix, 1.0),
439 -- so adjust if the result is 1.0 because of rounding.
441 if Fraction = Most_Significant_Digit * Radix then
442 Fraction := Most_Significant_Digit;
443 Exponent := Exponent + 1;
446 Release_And_Save (Uintp_Mark, Fraction, Exponent);
447 end Calculate_Fraction_And_Exponent;
455 function Eps_Denorm (RT : R) return T is
456 Digs : constant UI := Digits_Value (RT);
461 if Vax_Float (RT) then
462 if Digs = VAXFF_Digits then
463 Emin := VAXFF_Machine_Emin;
464 Mant := VAXFF_Machine_Mantissa;
466 elsif Digs = VAXDF_Digits then
467 Emin := VAXDF_Machine_Emin;
468 Mant := VAXDF_Machine_Mantissa;
471 pragma Assert (Digs = VAXGF_Digits);
472 Emin := VAXGF_Machine_Emin;
473 Mant := VAXGF_Machine_Mantissa;
476 elsif Is_AAMP_Float (RT) then
477 if Digs = AAMPS_Digits then
478 Emin := AAMPS_Machine_Emin;
479 Mant := AAMPS_Machine_Mantissa;
482 pragma Assert (Digs = AAMPL_Digits);
483 Emin := AAMPL_Machine_Emin;
484 Mant := AAMPL_Machine_Mantissa;
488 if Digs = IEEES_Digits then
489 Emin := IEEES_Machine_Emin;
490 Mant := IEEES_Machine_Mantissa;
492 elsif Digs = IEEEL_Digits then
493 Emin := IEEEL_Machine_Emin;
494 Mant := IEEEL_Machine_Mantissa;
497 pragma Assert (Digs = IEEEX_Digits);
498 Emin := IEEEX_Machine_Emin;
499 Mant := IEEEX_Machine_Mantissa;
503 return Float_Radix ** UI_From_Int (Emin - Mant);
510 function Eps_Model (RT : R) return T is
511 Digs : constant UI := Digits_Value (RT);
515 if Vax_Float (RT) then
516 if Digs = VAXFF_Digits then
517 Emin := VAXFF_Machine_Emin;
519 elsif Digs = VAXDF_Digits then
520 Emin := VAXDF_Machine_Emin;
523 pragma Assert (Digs = VAXGF_Digits);
524 Emin := VAXGF_Machine_Emin;
527 elsif Is_AAMP_Float (RT) then
528 if Digs = AAMPS_Digits then
529 Emin := AAMPS_Machine_Emin;
532 pragma Assert (Digs = AAMPL_Digits);
533 Emin := AAMPL_Machine_Emin;
537 if Digs = IEEES_Digits then
538 Emin := IEEES_Machine_Emin;
540 elsif Digs = IEEEL_Digits then
541 Emin := IEEEL_Machine_Emin;
544 pragma Assert (Digs = IEEEX_Digits);
545 Emin := IEEEX_Machine_Emin;
549 return Float_Radix ** UI_From_Int (Emin);
556 function Exponent (RT : R; X : T) return UI is
561 if UR_Is_Zero (X) then
564 Decompose_Int (RT, X, X_Frac, X_Exp, Round_Even);
573 function Floor (RT : R; X : T) return T is
574 XT : constant T := Truncation (RT, X);
577 if UR_Is_Positive (X) then
592 function Fraction (RT : R; X : T) return T is
597 if UR_Is_Zero (X) then
600 Decompose (RT, X, X_Frac, X_Exp);
609 function Leading_Part (RT : R; X : T; Radix_Digits : UI) return T is
614 if Radix_Digits >= Machine_Mantissa (RT) then
618 L := Exponent (RT, X) - Radix_Digits;
619 Y := Truncation (RT, Scaling (RT, X, -L));
620 Z := Scaling (RT, Y, L);
630 function Machine (RT : R; X : T; Mode : Rounding_Mode) return T is
635 if UR_Is_Zero (X) then
638 Decompose (RT, X, X_Frac, X_Exp, Mode);
639 return Scaling (RT, X_Frac, X_Exp);
643 ----------------------
644 -- Machine_Mantissa --
645 ----------------------
647 function Machine_Mantissa (RT : R) return Nat is
648 Digs : constant UI := Digits_Value (RT);
652 if Vax_Float (RT) then
653 if Digs = VAXFF_Digits then
654 Mant := VAXFF_Machine_Mantissa;
656 elsif Digs = VAXDF_Digits then
657 Mant := VAXDF_Machine_Mantissa;
660 pragma Assert (Digs = VAXGF_Digits);
661 Mant := VAXGF_Machine_Mantissa;
664 elsif Is_AAMP_Float (RT) then
665 if Digs = AAMPS_Digits then
666 Mant := AAMPS_Machine_Mantissa;
669 pragma Assert (Digs = AAMPL_Digits);
670 Mant := AAMPL_Machine_Mantissa;
674 if Digs = IEEES_Digits then
675 Mant := IEEES_Machine_Mantissa;
677 elsif Digs = IEEEL_Digits then
678 Mant := IEEEL_Machine_Mantissa;
681 pragma Assert (Digs = IEEEX_Digits);
682 Mant := IEEEX_Machine_Mantissa;
687 end Machine_Mantissa;
693 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
711 if abs X < Eps_Model (RT) then
712 if Denorm_On_Target then
713 return X - Eps_Denorm (RT);
715 elsif X > Ureal_0 then
716 -- Target does not support denorms, so predecessor is 0.0
720 -- Target does not support denorms, and X is 0.0
721 -- or at least bigger than -Eps_Model (RT)
723 return -Eps_Model (RT);
727 Decompose_Int (RT, X, Result_F, Result_X, Ceiling);
728 return UR_From_Components
729 (Num => Result_F - 1,
730 Den => Machine_Mantissa (RT) - Result_X,
733 -- Result_F may be false, but this is OK as UR_From_Components
734 -- handles that situation.
742 function Remainder (RT : R; X, Y : T) return T is
757 if UR_Is_Positive (X) then
769 P_Exp := Exponent (RT, P);
772 -- ??? what about zero cases?
773 Decompose (RT, Arg, Arg_Frac, Arg_Exp);
774 Decompose (RT, P, P_Frac, P_Exp);
776 P := Compose (RT, P_Frac, Arg_Exp);
777 K := Arg_Exp - P_Exp;
781 for Cnt in reverse 0 .. UI_To_Int (K) loop
782 if IEEE_Rem >= P then
784 IEEE_Rem := IEEE_Rem - P;
793 -- That completes the calculation of modulus remainder. The final step
794 -- is get the IEEE remainder. Here we compare Rem with (abs Y) / 2.
798 B := abs Y * Ureal_Half;
801 A := IEEE_Rem * Ureal_2;
805 if A > B or else (A = B and then not P_Even) then
806 IEEE_Rem := IEEE_Rem - abs Y;
809 return Sign_X * IEEE_Rem;
817 function Rounding (RT : R; X : T) return T is
822 Result := Truncation (RT, abs X);
823 Tail := abs X - Result;
825 if Tail >= Ureal_Half then
826 Result := Result + Ureal_1;
829 if UR_Is_Negative (X) then
841 function Scaling (RT : R; X : T; Adjustment : UI) return T is
842 pragma Warnings (Off, RT);
845 if Rbase (X) = Radix then
846 return UR_From_Components
847 (Num => Numerator (X),
848 Den => Denominator (X) - Adjustment,
850 Negative => UR_Is_Negative (X));
852 elsif Adjustment >= 0 then
853 return X * Radix ** Adjustment;
855 return X / Radix ** (-Adjustment);
863 function Succ (RT : R; X : T) return T is
868 if abs X < Eps_Model (RT) then
869 if Denorm_On_Target then
870 return X + Eps_Denorm (RT);
872 elsif X < Ureal_0 then
873 -- Target does not support denorms, so successor is 0.0
877 -- Target does not support denorms, and X is 0.0
878 -- or at least smaller than Eps_Model (RT)
880 return Eps_Model (RT);
884 Decompose_Int (RT, X, Result_F, Result_X, Floor);
885 return UR_From_Components
886 (Num => Result_F + 1,
887 Den => Machine_Mantissa (RT) - Result_X,
890 -- Result_F may be false, but this is OK as UR_From_Components
891 -- handles that situation.
899 function Truncation (RT : R; X : T) return T is
900 pragma Warnings (Off, RT);
903 return UR_From_Uint (UR_Trunc (X));
906 -----------------------
907 -- Unbiased_Rounding --
908 -----------------------
910 function Unbiased_Rounding (RT : R; X : T) return T is
911 Abs_X : constant T := abs X;
916 Result := Truncation (RT, Abs_X);
917 Tail := Abs_X - Result;
919 if Tail > Ureal_Half then
920 Result := Result + Ureal_1;
922 elsif Tail = Ureal_Half then
924 Truncation (RT, (Result / Ureal_2) + Ureal_Half);
927 if UR_Is_Negative (X) then
929 elsif UR_Is_Positive (X) then
932 -- For zero case, make sure sign of zero is preserved
938 end Unbiased_Rounding;