2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / eval_fat.adb
blobf8d14bfe2fac01bba188b9548f2a7c3ad0abc200
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E V A L _ F A T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2003 Free Software Foundation, Inc. --
10 -- --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Einfo; use Einfo;
28 with Errout; use Errout;
29 with Sem_Util; use Sem_Util;
30 with Ttypef; use Ttypef;
31 with Targparm; use Targparm;
33 package body Eval_Fat is
35 Radix : constant Int := 2;
36 -- This code is currently only correct for the radix 2 case. We use
37 -- the symbolic value Radix where possible to help in the unlikely
38 -- case of anyone ever having to adjust this code for another value,
39 -- and for documentation purposes.
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 function Float_Radix return T renames Ureal_2;
47 -- Radix expressed in real form
49 -----------------------
50 -- Local Subprograms --
51 -----------------------
53 procedure Decompose
54 (RT : R;
55 X : in T;
56 Fraction : out T;
57 Exponent : out UI;
58 Mode : Rounding_Mode := Round);
59 -- Decomposes a non-zero floating-point number into fraction and
60 -- exponent parts. The fraction is in the interval 1.0 / Radix ..
61 -- T'Pred (1.0) and uses Rbase = Radix.
62 -- The result is rounded to a nearest machine number.
64 procedure Decompose_Int
65 (RT : R;
66 X : in T;
67 Fraction : out UI;
68 Exponent : out UI;
69 Mode : Rounding_Mode);
70 -- This is similar to Decompose, except that the Fraction value returned
71 -- is an integer representing the value Fraction * Scale, where Scale is
72 -- the value (Radix ** Machine_Mantissa (RT)). The value is obtained by
73 -- using biased rounding (halfway cases round away from zero), round to
74 -- even, a floor operation or a ceiling operation depending on the setting
75 -- of Mode (see corresponding descriptions in Urealp).
77 function Eps_Model (RT : R) return T;
78 -- Return the smallest model number of R.
80 function Eps_Denorm (RT : R) return T;
81 -- Return the smallest denormal of type R.
83 function Machine_Emin (RT : R) return Int;
84 -- Return value of the Machine_Emin attribute
86 function Machine_Mantissa (RT : R) return Nat;
87 -- Return value of the Machine_Mantissa attribute
89 --------------
90 -- Adjacent --
91 --------------
93 function Adjacent (RT : R; X, Towards : T) return T is
94 begin
95 if Towards = X then
96 return X;
98 elsif Towards > X then
99 return Succ (RT, X);
101 else
102 return Pred (RT, X);
103 end if;
104 end Adjacent;
106 -------------
107 -- Ceiling --
108 -------------
110 function Ceiling (RT : R; X : T) return T is
111 XT : constant T := Truncation (RT, X);
113 begin
114 if UR_Is_Negative (X) then
115 return XT;
117 elsif X = XT then
118 return X;
120 else
121 return XT + Ureal_1;
122 end if;
123 end Ceiling;
125 -------------
126 -- Compose --
127 -------------
129 function Compose (RT : R; Fraction : T; Exponent : UI) return T is
130 Arg_Frac : T;
131 Arg_Exp : UI;
133 begin
134 if UR_Is_Zero (Fraction) then
135 return Fraction;
136 else
137 Decompose (RT, Fraction, Arg_Frac, Arg_Exp);
138 return Scaling (RT, Arg_Frac, Exponent);
139 end if;
140 end Compose;
142 ---------------
143 -- Copy_Sign --
144 ---------------
146 function Copy_Sign (RT : R; Value, Sign : T) return T is
147 pragma Warnings (Off, RT);
148 Result : T;
150 begin
151 Result := abs Value;
153 if UR_Is_Negative (Sign) then
154 return -Result;
155 else
156 return Result;
157 end if;
158 end Copy_Sign;
160 ---------------
161 -- Decompose --
162 ---------------
164 procedure Decompose
165 (RT : R;
166 X : in T;
167 Fraction : out T;
168 Exponent : out UI;
169 Mode : Rounding_Mode := Round)
171 Int_F : UI;
173 begin
174 Decompose_Int (RT, abs X, Int_F, Exponent, Mode);
176 Fraction := UR_From_Components
177 (Num => Int_F,
178 Den => UI_From_Int (Machine_Mantissa (RT)),
179 Rbase => Radix,
180 Negative => False);
182 if UR_Is_Negative (X) then
183 Fraction := -Fraction;
184 end if;
186 return;
187 end Decompose;
189 -------------------
190 -- Decompose_Int --
191 -------------------
193 -- This procedure should be modified with care, as there
194 -- are many non-obvious details that may cause problems
195 -- that are hard to detect. The cases of positive and
196 -- negative zeroes are also special and should be
197 -- verified separately.
199 procedure Decompose_Int
200 (RT : R;
201 X : in T;
202 Fraction : out UI;
203 Exponent : out UI;
204 Mode : Rounding_Mode)
206 Base : Int := Rbase (X);
207 N : UI := abs Numerator (X);
208 D : UI := Denominator (X);
210 N_Times_Radix : UI;
212 Even : Boolean;
213 -- True iff Fraction is even
215 Most_Significant_Digit : constant UI :=
216 Radix ** (Machine_Mantissa (RT) - 1);
218 Uintp_Mark : Uintp.Save_Mark;
219 -- The code is divided into blocks that systematically release
220 -- intermediate values (this routine generates lots of junk!)
222 begin
223 Calculate_D_And_Exponent_1 : begin
224 Uintp_Mark := Mark;
225 Exponent := Uint_0;
227 -- In cases where Base > 1, the actual denominator is
228 -- Base**D. For cases where Base is a power of Radix, use
229 -- the value 1 for the Denominator and adjust the exponent.
231 -- Note: Exponent has different sign from D, because D is a divisor
233 for Power in 1 .. Radix_Powers'Last loop
234 if Base = Radix_Powers (Power) then
235 Exponent := -D * Power;
236 Base := 0;
237 D := Uint_1;
238 exit;
239 end if;
240 end loop;
242 Release_And_Save (Uintp_Mark, D, Exponent);
243 end Calculate_D_And_Exponent_1;
245 if Base > 0 then
246 Calculate_Exponent : begin
247 Uintp_Mark := Mark;
249 -- For bases that are a multiple of the Radix, divide
250 -- the base by Radix and adjust the Exponent. This will
251 -- help because D will be much smaller and faster to process.
253 -- This occurs for decimal bases on a machine with binary
254 -- floating-point for example. When calculating 1E40,
255 -- with Radix = 2, N will be 93 bits instead of 133.
257 -- N E
258 -- ------ * Radix
259 -- D
260 -- Base
262 -- N E
263 -- = -------------------------- * Radix
264 -- D D
265 -- (Base/Radix) * Radix
267 -- N E-D
268 -- = --------------- * Radix
269 -- D
270 -- (Base/Radix)
272 -- This code is commented out, because it causes numerous
273 -- failures in the regression suite. To be studied ???
275 while False and then Base > 0 and then Base mod Radix = 0 loop
276 Base := Base / Radix;
277 Exponent := Exponent + D;
278 end loop;
280 Release_And_Save (Uintp_Mark, Exponent);
281 end Calculate_Exponent;
283 -- For remaining bases we must actually compute
284 -- the exponentiation.
286 -- Because the exponentiation can be negative, and D must
287 -- be integer, the numerator is corrected instead.
289 Calculate_N_And_D : begin
290 Uintp_Mark := Mark;
292 if D < 0 then
293 N := N * Base ** (-D);
294 D := Uint_1;
295 else
296 D := Base ** D;
297 end if;
299 Release_And_Save (Uintp_Mark, N, D);
300 end Calculate_N_And_D;
302 Base := 0;
303 end if;
305 -- Now scale N and D so that N / D is a value in the
306 -- interval [1.0 / Radix, 1.0) and adjust Exponent accordingly,
307 -- so the value N / D * Radix ** Exponent remains unchanged.
309 -- Step 1 - Adjust N so N / D >= 1 / Radix, or N = 0
311 -- N and D are positive, so N / D >= 1 / Radix implies N * Radix >= D.
312 -- This scaling is not possible for N is Uint_0 as there
313 -- is no way to scale Uint_0 so the first digit is non-zero.
315 Calculate_N_And_Exponent : begin
316 Uintp_Mark := Mark;
318 N_Times_Radix := N * Radix;
320 if N /= Uint_0 then
321 while not (N_Times_Radix >= D) loop
322 N := N_Times_Radix;
323 Exponent := Exponent - 1;
325 N_Times_Radix := N * Radix;
326 end loop;
327 end if;
329 Release_And_Save (Uintp_Mark, N, Exponent);
330 end Calculate_N_And_Exponent;
332 -- Step 2 - Adjust D so N / D < 1
334 -- Scale up D so N / D < 1, so N < D
336 Calculate_D_And_Exponent_2 : begin
337 Uintp_Mark := Mark;
339 while not (N < D) loop
341 -- As N / D >= 1, N / (D * Radix) will be at least 1 / Radix,
342 -- so the result of Step 1 stays valid
344 D := D * Radix;
345 Exponent := Exponent + 1;
346 end loop;
348 Release_And_Save (Uintp_Mark, D, Exponent);
349 end Calculate_D_And_Exponent_2;
351 -- Here the value N / D is in the range [1.0 / Radix .. 1.0)
353 -- Now find the fraction by doing a very simple-minded
354 -- division until enough digits have been computed.
356 -- This division works for all radices, but is only efficient for
357 -- a binary radix. It is just like a manual division algorithm,
358 -- but instead of moving the denominator one digit right, we move
359 -- the numerator one digit left so the numerator and denominator
360 -- remain integral.
362 Fraction := Uint_0;
363 Even := True;
365 Calculate_Fraction_And_N : begin
366 Uintp_Mark := Mark;
368 loop
369 while N >= D loop
370 N := N - D;
371 Fraction := Fraction + 1;
372 Even := not Even;
373 end loop;
375 -- Stop when the result is in [1.0 / Radix, 1.0)
377 exit when Fraction >= Most_Significant_Digit;
379 N := N * Radix;
380 Fraction := Fraction * Radix;
381 Even := True;
382 end loop;
384 Release_And_Save (Uintp_Mark, Fraction, N);
385 end Calculate_Fraction_And_N;
387 Calculate_Fraction_And_Exponent : begin
388 Uintp_Mark := Mark;
390 -- Put back sign before applying the rounding.
392 if UR_Is_Negative (X) then
393 Fraction := -Fraction;
394 end if;
396 -- Determine correct rounding based on the remainder
397 -- which is in N and the divisor D.
399 case Mode is
400 when Round_Even =>
402 -- This rounding mode should not be used for static
403 -- expressions, but only for compile-time evaluation
404 -- of non-static expressions.
406 if (Even and then N * 2 > D)
407 or else
408 (not Even and then N * 2 >= D)
409 then
410 Fraction := Fraction + 1;
411 end if;
413 when Round =>
415 -- Do not round to even as is done with IEEE arithmetic,
416 -- but instead round away from zero when the result is
417 -- exactly between two machine numbers. See RM 4.9(38).
419 if N * 2 >= D then
420 Fraction := Fraction + 1;
421 end if;
423 when Ceiling =>
424 if N > Uint_0 then
425 Fraction := Fraction + 1;
426 end if;
428 when Floor => null;
429 end case;
431 -- The result must be normalized to [1.0/Radix, 1.0),
432 -- so adjust if the result is 1.0 because of rounding.
434 if Fraction = Most_Significant_Digit * Radix then
435 Fraction := Most_Significant_Digit;
436 Exponent := Exponent + 1;
437 end if;
439 Release_And_Save (Uintp_Mark, Fraction, Exponent);
440 end Calculate_Fraction_And_Exponent;
441 end Decompose_Int;
443 ----------------
444 -- Eps_Denorm --
445 ----------------
447 function Eps_Denorm (RT : R) return T is
448 begin
449 return Float_Radix ** UI_From_Int
450 (Machine_Emin (RT) - Machine_Mantissa (RT));
451 end Eps_Denorm;
453 ---------------
454 -- Eps_Model --
455 ---------------
457 function Eps_Model (RT : R) return T is
458 begin
459 return Float_Radix ** UI_From_Int (Machine_Emin (RT));
460 end Eps_Model;
462 --------------
463 -- Exponent --
464 --------------
466 function Exponent (RT : R; X : T) return UI is
467 X_Frac : UI;
468 X_Exp : UI;
470 begin
471 if UR_Is_Zero (X) then
472 return Uint_0;
473 else
474 Decompose_Int (RT, X, X_Frac, X_Exp, Round_Even);
475 return X_Exp;
476 end if;
477 end Exponent;
479 -----------
480 -- Floor --
481 -----------
483 function Floor (RT : R; X : T) return T is
484 XT : constant T := Truncation (RT, X);
486 begin
487 if UR_Is_Positive (X) then
488 return XT;
490 elsif XT = X then
491 return X;
493 else
494 return XT - Ureal_1;
495 end if;
496 end Floor;
498 --------------
499 -- Fraction --
500 --------------
502 function Fraction (RT : R; X : T) return T is
503 X_Frac : T;
504 X_Exp : UI;
506 begin
507 if UR_Is_Zero (X) then
508 return X;
509 else
510 Decompose (RT, X, X_Frac, X_Exp);
511 return X_Frac;
512 end if;
513 end Fraction;
515 ------------------
516 -- Leading_Part --
517 ------------------
519 function Leading_Part (RT : R; X : T; Radix_Digits : UI) return T is
520 L : UI;
521 Y, Z : T;
523 begin
524 if Radix_Digits >= Machine_Mantissa (RT) then
525 return X;
527 else
528 L := Exponent (RT, X) - Radix_Digits;
529 Y := Truncation (RT, Scaling (RT, X, -L));
530 Z := Scaling (RT, Y, L);
531 return Z;
532 end if;
533 end Leading_Part;
535 -------------
536 -- Machine --
537 -------------
539 function Machine
540 (RT : R;
541 X : T;
542 Mode : Rounding_Mode;
543 Enode : Node_Id)
544 return T
546 pragma Warnings (Off, Enode); -- not yet referenced
548 X_Frac : T;
549 X_Exp : UI;
550 Emin : constant UI := UI_From_Int (Machine_Emin (RT));
552 begin
553 if UR_Is_Zero (X) then
554 return X;
556 else
557 Decompose (RT, X, X_Frac, X_Exp, Mode);
559 -- Case of denormalized number or (gradual) underflow
561 -- A denormalized number is one with the minimum exponent Emin, but
562 -- that breaks the assumption that the first digit of the mantissa
563 -- is a one. This allows the first non-zero digit to be in any
564 -- of the remaining Mant - 1 spots. The gap between subsequent
565 -- denormalized numbers is the same as for the smallest normalized
566 -- numbers. However, the number of significant digits left decreases
567 -- as a result of the mantissa now having leading seros.
569 if X_Exp < Emin then
570 declare
571 Emin_Den : constant UI :=
572 UI_From_Int
573 (Machine_Emin (RT) - Machine_Mantissa (RT) + 1);
574 begin
575 if X_Exp < Emin_Den or not Denorm_On_Target then
576 if UR_Is_Negative (X) then
577 Error_Msg_N
578 ("floating-point value underflows to -0.0?", Enode);
579 return Ureal_M_0;
581 else
582 Error_Msg_N
583 ("floating-point value underflows to 0.0?", Enode);
584 return Ureal_0;
585 end if;
587 elsif Denorm_On_Target then
589 -- Emin - Mant <= X_Exp < Emin, so result is denormal.
590 -- Handle gradual underflow by first computing the
591 -- number of significant bits still available for the
592 -- mantissa and then truncating the fraction to this
593 -- number of bits.
595 -- If this value is different from the original
596 -- fraction, precision is lost due to gradual underflow.
598 -- We probably should round here and prevent double
599 -- rounding as a result of first rounding to a model
600 -- number and then to a machine number. However, this
601 -- is an extremely rare case that is not worth the extra
602 -- complexity. In any case, a warning is issued in cases
603 -- where gradual underflow occurs.
605 declare
606 Denorm_Sig_Bits : constant UI := X_Exp - Emin_Den + 1;
608 X_Frac_Denorm : constant T := UR_From_Components
609 (UR_Trunc (Scaling (RT, abs X_Frac, Denorm_Sig_Bits)),
610 Denorm_Sig_Bits,
611 Radix,
612 UR_Is_Negative (X));
614 begin
615 if X_Frac_Denorm /= X_Frac then
616 Error_Msg_N
617 ("gradual underflow causes loss of precision?",
618 Enode);
619 X_Frac := X_Frac_Denorm;
620 end if;
621 end;
622 end if;
623 end;
624 end if;
626 return Scaling (RT, X_Frac, X_Exp);
627 end if;
628 end Machine;
630 ------------------
631 -- Machine_Emin --
632 ------------------
634 function Machine_Emin (RT : R) return Int is
635 Digs : constant UI := Digits_Value (RT);
636 Emin : Int;
638 begin
639 if Vax_Float (RT) then
640 if Digs = VAXFF_Digits then
641 Emin := VAXFF_Machine_Emin;
643 elsif Digs = VAXDF_Digits then
644 Emin := VAXDF_Machine_Emin;
646 else
647 pragma Assert (Digs = VAXGF_Digits);
648 Emin := VAXGF_Machine_Emin;
649 end if;
651 elsif Is_AAMP_Float (RT) then
652 if Digs = AAMPS_Digits then
653 Emin := AAMPS_Machine_Emin;
655 else
656 pragma Assert (Digs = AAMPL_Digits);
657 Emin := AAMPL_Machine_Emin;
658 end if;
660 else
661 if Digs = IEEES_Digits then
662 Emin := IEEES_Machine_Emin;
664 elsif Digs = IEEEL_Digits then
665 Emin := IEEEL_Machine_Emin;
667 else
668 pragma Assert (Digs = IEEEX_Digits);
669 Emin := IEEEX_Machine_Emin;
670 end if;
671 end if;
673 return Emin;
674 end Machine_Emin;
676 ----------------------
677 -- Machine_Mantissa --
678 ----------------------
680 function Machine_Mantissa (RT : R) return Nat is
681 Digs : constant UI := Digits_Value (RT);
682 Mant : Nat;
684 begin
685 if Vax_Float (RT) then
686 if Digs = VAXFF_Digits then
687 Mant := VAXFF_Machine_Mantissa;
689 elsif Digs = VAXDF_Digits then
690 Mant := VAXDF_Machine_Mantissa;
692 else
693 pragma Assert (Digs = VAXGF_Digits);
694 Mant := VAXGF_Machine_Mantissa;
695 end if;
697 elsif Is_AAMP_Float (RT) then
698 if Digs = AAMPS_Digits then
699 Mant := AAMPS_Machine_Mantissa;
701 else
702 pragma Assert (Digs = AAMPL_Digits);
703 Mant := AAMPL_Machine_Mantissa;
704 end if;
706 else
707 if Digs = IEEES_Digits then
708 Mant := IEEES_Machine_Mantissa;
710 elsif Digs = IEEEL_Digits then
711 Mant := IEEEL_Machine_Mantissa;
713 else
714 pragma Assert (Digs = IEEEX_Digits);
715 Mant := IEEEX_Machine_Mantissa;
716 end if;
717 end if;
719 return Mant;
720 end Machine_Mantissa;
722 -----------
723 -- Model --
724 -----------
726 function Model (RT : R; X : T) return T is
727 X_Frac : T;
728 X_Exp : UI;
730 begin
731 Decompose (RT, X, X_Frac, X_Exp);
732 return Compose (RT, X_Frac, X_Exp);
733 end Model;
735 ----------
736 -- Pred --
737 ----------
739 function Pred (RT : R; X : T) return T is
740 Result_F : UI;
741 Result_X : UI;
743 begin
744 if abs X < Eps_Model (RT) then
745 if Denorm_On_Target then
746 return X - Eps_Denorm (RT);
748 elsif X > Ureal_0 then
750 -- Target does not support denorms, so predecessor is 0.0
752 return Ureal_0;
754 else
755 -- Target does not support denorms, and X is 0.0
756 -- or at least bigger than -Eps_Model (RT)
758 return -Eps_Model (RT);
759 end if;
761 else
762 Decompose_Int (RT, X, Result_F, Result_X, Ceiling);
763 return UR_From_Components
764 (Num => Result_F - 1,
765 Den => Machine_Mantissa (RT) - Result_X,
766 Rbase => Radix,
767 Negative => False);
768 -- Result_F may be false, but this is OK as UR_From_Components
769 -- handles that situation.
770 end if;
771 end Pred;
773 ---------------
774 -- Remainder --
775 ---------------
777 function Remainder (RT : R; X, Y : T) return T is
778 A : T;
779 B : T;
780 Arg : T;
781 P : T;
782 Arg_Frac : T;
783 P_Frac : T;
784 Sign_X : T;
785 IEEE_Rem : T;
786 Arg_Exp : UI;
787 P_Exp : UI;
788 K : UI;
789 P_Even : Boolean;
791 begin
792 if UR_Is_Positive (X) then
793 Sign_X := Ureal_1;
794 else
795 Sign_X := -Ureal_1;
796 end if;
798 Arg := abs X;
799 P := abs Y;
801 if Arg < P then
802 P_Even := True;
803 IEEE_Rem := Arg;
804 P_Exp := Exponent (RT, P);
806 else
807 -- ??? what about zero cases?
808 Decompose (RT, Arg, Arg_Frac, Arg_Exp);
809 Decompose (RT, P, P_Frac, P_Exp);
811 P := Compose (RT, P_Frac, Arg_Exp);
812 K := Arg_Exp - P_Exp;
813 P_Even := True;
814 IEEE_Rem := Arg;
816 for Cnt in reverse 0 .. UI_To_Int (K) loop
817 if IEEE_Rem >= P then
818 P_Even := False;
819 IEEE_Rem := IEEE_Rem - P;
820 else
821 P_Even := True;
822 end if;
824 P := P * Ureal_Half;
825 end loop;
826 end if;
828 -- That completes the calculation of modulus remainder. The final step
829 -- is get the IEEE remainder. Here we compare Rem with (abs Y) / 2.
831 if P_Exp >= 0 then
832 A := IEEE_Rem;
833 B := abs Y * Ureal_Half;
835 else
836 A := IEEE_Rem * Ureal_2;
837 B := abs Y;
838 end if;
840 if A > B or else (A = B and then not P_Even) then
841 IEEE_Rem := IEEE_Rem - abs Y;
842 end if;
844 return Sign_X * IEEE_Rem;
845 end Remainder;
847 --------------
848 -- Rounding --
849 --------------
851 function Rounding (RT : R; X : T) return T is
852 Result : T;
853 Tail : T;
855 begin
856 Result := Truncation (RT, abs X);
857 Tail := abs X - Result;
859 if Tail >= Ureal_Half then
860 Result := Result + Ureal_1;
861 end if;
863 if UR_Is_Negative (X) then
864 return -Result;
865 else
866 return Result;
867 end if;
868 end Rounding;
870 -------------
871 -- Scaling --
872 -------------
874 function Scaling (RT : R; X : T; Adjustment : UI) return T is
875 pragma Warnings (Off, RT);
877 begin
878 if Rbase (X) = Radix then
879 return UR_From_Components
880 (Num => Numerator (X),
881 Den => Denominator (X) - Adjustment,
882 Rbase => Radix,
883 Negative => UR_Is_Negative (X));
885 elsif Adjustment >= 0 then
886 return X * Radix ** Adjustment;
887 else
888 return X / Radix ** (-Adjustment);
889 end if;
890 end Scaling;
892 ----------
893 -- Succ --
894 ----------
896 function Succ (RT : R; X : T) return T is
897 Result_F : UI;
898 Result_X : UI;
900 begin
901 if abs X < Eps_Model (RT) then
902 if Denorm_On_Target then
903 return X + Eps_Denorm (RT);
905 elsif X < Ureal_0 then
906 -- Target does not support denorms, so successor is 0.0
907 return Ureal_0;
909 else
910 -- Target does not support denorms, and X is 0.0
911 -- or at least smaller than Eps_Model (RT)
913 return Eps_Model (RT);
914 end if;
916 else
917 Decompose_Int (RT, X, Result_F, Result_X, Floor);
918 return UR_From_Components
919 (Num => Result_F + 1,
920 Den => Machine_Mantissa (RT) - Result_X,
921 Rbase => Radix,
922 Negative => False);
923 -- Result_F may be false, but this is OK as UR_From_Components
924 -- handles that situation.
925 end if;
926 end Succ;
928 ----------------
929 -- Truncation --
930 ----------------
932 function Truncation (RT : R; X : T) return T is
933 pragma Warnings (Off, RT);
935 begin
936 return UR_From_Uint (UR_Trunc (X));
937 end Truncation;
939 -----------------------
940 -- Unbiased_Rounding --
941 -----------------------
943 function Unbiased_Rounding (RT : R; X : T) return T is
944 Abs_X : constant T := abs X;
945 Result : T;
946 Tail : T;
948 begin
949 Result := Truncation (RT, Abs_X);
950 Tail := Abs_X - Result;
952 if Tail > Ureal_Half then
953 Result := Result + Ureal_1;
955 elsif Tail = Ureal_Half then
956 Result := Ureal_2 *
957 Truncation (RT, (Result / Ureal_2) + Ureal_Half);
958 end if;
960 if UR_Is_Negative (X) then
961 return -Result;
962 elsif UR_Is_Positive (X) then
963 return Result;
965 -- For zero case, make sure sign of zero is preserved
967 else
968 return X;
969 end if;
970 end Unbiased_Rounding;
972 end Eval_Fat;