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
117 pragma Warnings (Off, Arg_Exp);
119 if UR_Is_Zero (Fraction) then
122 Decompose (RT, Fraction, Arg_Frac, Arg_Exp);
123 return Scaling (RT, Arg_Frac, Exponent);
131 function Copy_Sign (RT : R; Value, Sign : T) return T is
132 pragma Warnings (Off, RT);
138 if UR_Is_Negative (Sign) then
154 Mode : Rounding_Mode := Round)
159 Decompose_Int (RT, abs X, Int_F, Exponent, Mode);
161 Fraction := UR_From_Components
163 Den => UI_From_Int (Machine_Mantissa (RT)),
167 if UR_Is_Negative (X) then
168 Fraction := -Fraction;
178 -- This procedure should be modified with care, as there are many
179 -- non-obvious details that may cause problems that are hard to
180 -- detect. The cases of positive and negative zeroes are also
181 -- special and should be verified separately.
183 procedure Decompose_Int
188 Mode : Rounding_Mode)
190 Base : Int := Rbase (X);
191 N : UI := abs Numerator (X);
192 D : UI := Denominator (X);
197 -- True iff Fraction is even
199 Most_Significant_Digit : constant UI :=
200 Radix ** (Machine_Mantissa (RT) - 1);
202 Uintp_Mark : Uintp.Save_Mark;
203 -- The code is divided into blocks that systematically release
204 -- intermediate values (this routine generates lots of junk!)
207 Calculate_D_And_Exponent_1 : begin
211 -- In cases where Base > 1, the actual denominator is
212 -- Base**D. For cases where Base is a power of Radix, use
213 -- the value 1 for the Denominator and adjust the exponent.
215 -- Note: Exponent has different sign from D, because D is a divisor
217 for Power in 1 .. Radix_Powers'Last loop
218 if Base = Radix_Powers (Power) then
219 Exponent := -D * Power;
226 Release_And_Save (Uintp_Mark, D, Exponent);
227 end Calculate_D_And_Exponent_1;
230 Calculate_Exponent : begin
233 -- For bases that are a multiple of the Radix, divide
234 -- the base by Radix and adjust the Exponent. This will
235 -- help because D will be much smaller and faster to process.
237 -- This occurs for decimal bases on a machine with binary
238 -- floating-point for example. When calculating 1E40,
239 -- with Radix = 2, N will be 93 bits instead of 133.
247 -- = -------------------------- * Radix
249 -- (Base/Radix) * Radix
252 -- = --------------- * Radix
256 -- This code is commented out, because it causes numerous
257 -- failures in the regression suite. To be studied ???
259 while False and then Base > 0 and then Base mod Radix = 0 loop
260 Base := Base / Radix;
261 Exponent := Exponent + D;
264 Release_And_Save (Uintp_Mark, Exponent);
265 end Calculate_Exponent;
267 -- For remaining bases we must actually compute
268 -- the exponentiation.
270 -- Because the exponentiation can be negative, and D must
271 -- be integer, the numerator is corrected instead.
273 Calculate_N_And_D : begin
277 N := N * Base ** (-D);
283 Release_And_Save (Uintp_Mark, N, D);
284 end Calculate_N_And_D;
289 -- Now scale N and D so that N / D is a value in the
290 -- interval [1.0 / Radix, 1.0) and adjust Exponent accordingly,
291 -- so the value N / D * Radix ** Exponent remains unchanged.
293 -- Step 1 - Adjust N so N / D >= 1 / Radix, or N = 0
295 -- N and D are positive, so N / D >= 1 / Radix implies N * Radix >= D.
296 -- This scaling is not possible for N is Uint_0 as there
297 -- is no way to scale Uint_0 so the first digit is non-zero.
299 Calculate_N_And_Exponent : begin
302 N_Times_Radix := N * Radix;
305 while not (N_Times_Radix >= D) loop
307 Exponent := Exponent - 1;
309 N_Times_Radix := N * Radix;
313 Release_And_Save (Uintp_Mark, N, Exponent);
314 end Calculate_N_And_Exponent;
316 -- Step 2 - Adjust D so N / D < 1
318 -- Scale up D so N / D < 1, so N < D
320 Calculate_D_And_Exponent_2 : begin
323 while not (N < D) loop
325 -- As N / D >= 1, N / (D * Radix) will be at least 1 / Radix,
326 -- so the result of Step 1 stays valid
329 Exponent := Exponent + 1;
332 Release_And_Save (Uintp_Mark, D, Exponent);
333 end Calculate_D_And_Exponent_2;
335 -- Here the value N / D is in the range [1.0 / Radix .. 1.0)
337 -- Now find the fraction by doing a very simple-minded
338 -- division until enough digits have been computed.
340 -- This division works for all radices, but is only efficient for
341 -- a binary radix. It is just like a manual division algorithm,
342 -- but instead of moving the denominator one digit right, we move
343 -- the numerator one digit left so the numerator and denominator
349 Calculate_Fraction_And_N : begin
355 Fraction := Fraction + 1;
359 -- Stop when the result is in [1.0 / Radix, 1.0)
361 exit when Fraction >= Most_Significant_Digit;
364 Fraction := Fraction * Radix;
368 Release_And_Save (Uintp_Mark, Fraction, N);
369 end Calculate_Fraction_And_N;
371 Calculate_Fraction_And_Exponent : begin
374 -- Determine correct rounding based on the remainder which is in
375 -- N and the divisor D. The rounding is performed on the absolute
376 -- value of X, so Ceiling and Floor need to check for the sign of
382 -- This rounding mode should not be used for static
383 -- expressions, but only for compile-time evaluation
384 -- of non-static expressions.
386 if (Even and then N * 2 > D)
388 (not Even and then N * 2 >= D)
390 Fraction := Fraction + 1;
395 -- Do not round to even as is done with IEEE arithmetic,
396 -- but instead round away from zero when the result is
397 -- exactly between two machine numbers. See RM 4.9(38).
400 Fraction := Fraction + 1;
404 if N > Uint_0 and then not UR_Is_Negative (X) then
405 Fraction := Fraction + 1;
409 if N > Uint_0 and then UR_Is_Negative (X) then
410 Fraction := Fraction + 1;
414 -- The result must be normalized to [1.0/Radix, 1.0),
415 -- so adjust if the result is 1.0 because of rounding.
417 if Fraction = Most_Significant_Digit * Radix then
418 Fraction := Most_Significant_Digit;
419 Exponent := Exponent + 1;
422 -- Put back sign after applying the rounding
424 if UR_Is_Negative (X) then
425 Fraction := -Fraction;
428 Release_And_Save (Uintp_Mark, Fraction, Exponent);
429 end Calculate_Fraction_And_Exponent;
436 function Exponent (RT : R; X : T) return UI is
439 pragma Warnings (Off, X_Frac);
441 if UR_Is_Zero (X) then
444 Decompose_Int (RT, X, X_Frac, X_Exp, Round_Even);
453 function Floor (RT : R; X : T) return T is
454 XT : constant T := Truncation (RT, X);
457 if UR_Is_Positive (X) then
472 function Fraction (RT : R; X : T) return T is
475 pragma Warnings (Off, X_Exp);
477 if UR_Is_Zero (X) then
480 Decompose (RT, X, X_Frac, X_Exp);
489 function Leading_Part (RT : R; X : T; Radix_Digits : UI) return T is
490 RD : constant UI := UI_Min (Radix_Digits, Machine_Mantissa (RT));
494 L := Exponent (RT, X) - RD;
495 Y := UR_From_Uint (UR_Trunc (Scaling (RT, X, -L)));
496 return Scaling (RT, Y, L);
506 Mode : Rounding_Mode;
507 Enode : Node_Id) return T
511 Emin : constant UI := UI_From_Int (Machine_Emin (RT));
514 if UR_Is_Zero (X) then
518 Decompose (RT, X, X_Frac, X_Exp, Mode);
520 -- Case of denormalized number or (gradual) underflow
522 -- A denormalized number is one with the minimum exponent Emin, but
523 -- that breaks the assumption that the first digit of the mantissa
524 -- is a one. This allows the first non-zero digit to be in any
525 -- of the remaining Mant - 1 spots. The gap between subsequent
526 -- denormalized numbers is the same as for the smallest normalized
527 -- numbers. However, the number of significant digits left decreases
528 -- as a result of the mantissa now having leading seros.
532 Emin_Den : constant UI :=
534 (Machine_Emin (RT) - Machine_Mantissa (RT) + 1);
536 if X_Exp < Emin_Den or not Denorm_On_Target then
537 if UR_Is_Negative (X) then
539 ("floating-point value underflows to -0.0?", Enode);
544 ("floating-point value underflows to 0.0?", Enode);
548 elsif Denorm_On_Target then
550 -- Emin - Mant <= X_Exp < Emin, so result is denormal.
551 -- Handle gradual underflow by first computing the
552 -- number of significant bits still available for the
553 -- mantissa and then truncating the fraction to this
556 -- If this value is different from the original
557 -- fraction, precision is lost due to gradual underflow.
559 -- We probably should round here and prevent double
560 -- rounding as a result of first rounding to a model
561 -- number and then to a machine number. However, this
562 -- is an extremely rare case that is not worth the extra
563 -- complexity. In any case, a warning is issued in cases
564 -- where gradual underflow occurs.
567 Denorm_Sig_Bits : constant UI := X_Exp - Emin_Den + 1;
569 X_Frac_Denorm : constant T := UR_From_Components
570 (UR_Trunc (Scaling (RT, abs X_Frac, Denorm_Sig_Bits)),
576 if X_Frac_Denorm /= X_Frac then
578 ("gradual underflow causes loss of precision?",
580 X_Frac := X_Frac_Denorm;
587 return Scaling (RT, X_Frac, X_Exp);
595 function Machine_Emin (RT : R) return Int is
596 Digs : constant UI := Digits_Value (RT);
600 if Vax_Float (RT) then
601 if Digs = VAXFF_Digits then
602 Emin := VAXFF_Machine_Emin;
604 elsif Digs = VAXDF_Digits then
605 Emin := VAXDF_Machine_Emin;
608 pragma Assert (Digs = VAXGF_Digits);
609 Emin := VAXGF_Machine_Emin;
612 elsif Is_AAMP_Float (RT) then
613 if Digs = AAMPS_Digits then
614 Emin := AAMPS_Machine_Emin;
617 pragma Assert (Digs = AAMPL_Digits);
618 Emin := AAMPL_Machine_Emin;
622 if Digs = IEEES_Digits then
623 Emin := IEEES_Machine_Emin;
625 elsif Digs = IEEEL_Digits then
626 Emin := IEEEL_Machine_Emin;
629 pragma Assert (Digs = IEEEX_Digits);
630 Emin := IEEEX_Machine_Emin;
637 ----------------------
638 -- Machine_Mantissa --
639 ----------------------
641 function Machine_Mantissa (RT : R) return Nat is
642 Digs : constant UI := Digits_Value (RT);
646 if Vax_Float (RT) then
647 if Digs = VAXFF_Digits then
648 Mant := VAXFF_Machine_Mantissa;
650 elsif Digs = VAXDF_Digits then
651 Mant := VAXDF_Machine_Mantissa;
654 pragma Assert (Digs = VAXGF_Digits);
655 Mant := VAXGF_Machine_Mantissa;
658 elsif Is_AAMP_Float (RT) then
659 if Digs = AAMPS_Digits then
660 Mant := AAMPS_Machine_Mantissa;
663 pragma Assert (Digs = AAMPL_Digits);
664 Mant := AAMPL_Machine_Mantissa;
668 if Digs = IEEES_Digits then
669 Mant := IEEES_Machine_Mantissa;
671 elsif Digs = IEEEL_Digits then
672 Mant := IEEEL_Machine_Mantissa;
675 pragma Assert (Digs = IEEEX_Digits);
676 Mant := IEEEX_Machine_Mantissa;
681 end Machine_Mantissa;
687 function Machine_Radix (RT : R) return Nat is
688 pragma Warnings (Off, RT);
697 function Model (RT : R; X : T) return T is
701 Decompose (RT, X, X_Frac, X_Exp);
702 return Compose (RT, X_Frac, X_Exp);
709 function Pred (RT : R; X : T) return T is
711 return -Succ (RT, -X);
718 function Remainder (RT : R; X, Y : T) return T is
732 pragma Warnings (Off, Arg_Frac);
735 if UR_Is_Positive (X) then
747 P_Exp := Exponent (RT, P);
750 -- ??? what about zero cases?
751 Decompose (RT, Arg, Arg_Frac, Arg_Exp);
752 Decompose (RT, P, P_Frac, P_Exp);
754 P := Compose (RT, P_Frac, Arg_Exp);
755 K := Arg_Exp - P_Exp;
759 for Cnt in reverse 0 .. UI_To_Int (K) loop
760 if IEEE_Rem >= P then
762 IEEE_Rem := IEEE_Rem - P;
771 -- That completes the calculation of modulus remainder. The final step
772 -- is get the IEEE remainder. Here we compare Rem with (abs Y) / 2.
776 B := abs Y * Ureal_Half;
779 A := IEEE_Rem * Ureal_2;
783 if A > B or else (A = B and then not P_Even) then
784 IEEE_Rem := IEEE_Rem - abs Y;
787 return Sign_X * IEEE_Rem;
794 function Rounding (RT : R; X : T) return T is
799 Result := Truncation (RT, abs X);
800 Tail := abs X - Result;
802 if Tail >= Ureal_Half then
803 Result := Result + Ureal_1;
806 if UR_Is_Negative (X) then
817 function Scaling (RT : R; X : T; Adjustment : UI) return T is
818 pragma Warnings (Off, RT);
821 if Rbase (X) = Radix then
822 return UR_From_Components
823 (Num => Numerator (X),
824 Den => Denominator (X) - Adjustment,
826 Negative => UR_Is_Negative (X));
828 elsif Adjustment >= 0 then
829 return X * Radix ** Adjustment;
831 return X / Radix ** (-Adjustment);
839 function Succ (RT : R; X : T) return T is
840 Emin : constant UI := UI_From_Int (Machine_Emin (RT));
841 Mantissa : constant UI := UI_From_Int (Machine_Mantissa (RT));
842 Exp : UI := UI_Max (Emin, Exponent (RT, X));
847 if UR_Is_Zero (X) then
851 -- Set exponent such that the radix point will be directly
852 -- following the mantissa after scaling
854 if Denorm_On_Target or Exp /= Emin then
855 Exp := Exp - Mantissa;
860 Frac := Scaling (RT, X, -Exp);
861 New_Frac := Ceiling (RT, Frac);
863 if New_Frac = Frac then
864 if New_Frac = Scaling (RT, -Ureal_1, Mantissa - 1) then
865 New_Frac := New_Frac + Scaling (RT, Ureal_1, Uint_Minus_1);
867 New_Frac := New_Frac + Ureal_1;
871 return Scaling (RT, New_Frac, Exp);
878 function Truncation (RT : R; X : T) return T is
879 pragma Warnings (Off, RT);
881 return UR_From_Uint (UR_Trunc (X));
884 -----------------------
885 -- Unbiased_Rounding --
886 -----------------------
888 function Unbiased_Rounding (RT : R; X : T) return T is
889 Abs_X : constant T := abs X;
894 Result := Truncation (RT, Abs_X);
895 Tail := Abs_X - Result;
897 if Tail > Ureal_Half then
898 Result := Result + Ureal_1;
900 elsif Tail = Ureal_Half then
902 Truncation (RT, (Result / Ureal_2) + Ureal_Half);
905 if UR_Is_Negative (X) then
907 elsif UR_Is_Positive (X) then
910 -- For zero case, make sure sign of zero is preserved
915 end Unbiased_Rounding;