1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . F A T _ G E N --
9 -- Copyright (C) 1992-2003 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 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. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- The implementation here is portable to any IEEE implementation. It does
35 -- not handle non-binary radix, and also assumes that model numbers and
36 -- machine numbers are basically identical, which is not true of all possible
37 -- floating-point implementations. On a non-IEEE machine, this body must be
38 -- specialized appropriately, or better still, its generic instantiations
39 -- should be replaced by efficient machine-specific code.
41 with Ada
.Unchecked_Conversion
;
43 package body System
.Fat_Gen
is
45 Float_Radix
: constant T
:= T
(T
'Machine_Radix);
46 Radix_To_M_Minus_1
: constant T
:= Float_Radix
** (T
'Machine_Mantissa - 1);
48 pragma Assert
(T
'Machine_Radix = 2);
49 -- This version does not handle radix 16
51 -- Constants for Decompose and Scaling
53 Rad
: constant T
:= T
(T
'Machine_Radix);
54 Invrad
: constant T
:= 1.0 / Rad
;
56 subtype Expbits
is Integer range 0 .. 6;
57 -- 2 ** (2 ** 7) might overflow. how big can radix-16 exponents get?
59 Log_Power
: constant array (Expbits
) of Integer := (1, 2, 4, 8, 16, 32, 64);
61 R_Power
: constant array (Expbits
) of T
:=
70 R_Neg_Power
: constant array (Expbits
) of T
:=
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 procedure Decompose
(XX
: T
; Frac
: out T
; Expo
: out UI
);
84 -- Decomposes a floating-point number into fraction and exponent parts
86 function Gradual_Scaling
(Adjustment
: UI
) return T
;
87 -- Like Scaling with a first argument of 1.0, but returns the smallest
88 -- denormal rather than zero when the adjustment is smaller than
89 -- Machine_Emin. Used for Succ and Pred.
95 function Adjacent
(X
, Towards
: T
) return T
is
100 elsif Towards
> X
then
112 function Ceiling
(X
: T
) return T
is
113 XT
: constant T
:= Truncation
(X
);
131 function Compose
(Fraction
: T
; Exponent
: UI
) return T
is
136 Decompose
(Fraction
, Arg_Frac
, Arg_Exp
);
137 return Scaling
(Arg_Frac
, Exponent
);
144 function Copy_Sign
(Value
, Sign
: T
) return T
is
147 function Is_Negative
(V
: T
) return Boolean;
148 pragma Import
(Intrinsic
, Is_Negative
);
153 if Is_Negative
(Sign
) then
164 procedure Decompose
(XX
: T
; Frac
: out T
; Expo
: out UI
) is
165 X
: constant T
:= T
'Machine (XX
);
172 -- More useful would be defining Expo to be T'Machine_Emin - 1 or
173 -- T'Machine_Emin - T'Machine_Mantissa, which would preserve
174 -- monotonicity of the exponent function ???
176 -- Check for infinities, transfinites, whatnot.
178 elsif X
> T
'Safe_Last then
180 Expo
:= T
'Machine_Emax + 1;
182 elsif X
< T
'Safe_First then
184 Expo
:= T
'Machine_Emax + 2; -- how many extra negative values?
187 -- Case of nonzero finite x. Essentially, we just multiply
188 -- by Rad ** (+-2**N) to reduce the range.
194 -- Ax * Rad ** Ex is invariant.
198 while Ax
>= R_Power
(Expbits
'Last) loop
199 Ax
:= Ax
* R_Neg_Power
(Expbits
'Last);
200 Ex
:= Ex
+ Log_Power
(Expbits
'Last);
205 for N
in reverse Expbits
'First .. Expbits
'Last - 1 loop
206 if Ax
>= R_Power
(N
) then
207 Ax
:= Ax
* R_Neg_Power
(N
);
208 Ex
:= Ex
+ Log_Power
(N
);
222 while Ax
< R_Neg_Power
(Expbits
'Last) loop
223 Ax
:= Ax
* R_Power
(Expbits
'Last);
224 Ex
:= Ex
- Log_Power
(Expbits
'Last);
227 -- Rad ** -64 <= Ax < 1
229 for N
in reverse Expbits
'First .. Expbits
'Last - 1 loop
230 if Ax
< R_Neg_Power
(N
) then
231 Ax
:= Ax
* R_Power
(N
);
232 Ex
:= Ex
- Log_Power
(N
);
235 -- R_Neg_Power (N) <= Ax < 1
254 function Exponent
(X
: T
) return UI
is
259 Decompose
(X
, X_Frac
, X_Exp
);
267 function Floor
(X
: T
) return T
is
268 XT
: constant T
:= Truncation
(X
);
286 function Fraction
(X
: T
) return T
is
291 Decompose
(X
, X_Frac
, X_Exp
);
295 ---------------------
296 -- Gradual_Scaling --
297 ---------------------
299 function Gradual_Scaling
(Adjustment
: UI
) return T
is
302 Ex
: UI
:= Adjustment
;
305 if Adjustment
< T
'Machine_Emin then
306 Y
:= 2.0 ** T
'Machine_Emin;
308 Ex
:= Ex
- T
'Machine_Emin;
311 Y
:= T
'Machine (Y
/ 2.0);
324 return Scaling
(1.0, Adjustment
);
332 function Leading_Part
(X
: T
; Radix_Digits
: UI
) return T
is
337 if Radix_Digits
>= T
'Machine_Mantissa then
341 L
:= Exponent
(X
) - Radix_Digits
;
342 Y
:= Truncation
(Scaling
(X
, -L
));
353 -- The trick with Machine is to force the compiler to store the result
354 -- in memory so that we do not have extra precision used. The compiler
355 -- is clever, so we have to outwit its possible optimizations! We do
356 -- this by using an intermediate pragma Volatile location.
358 function Machine
(X
: T
) return T
is
360 pragma Volatile
(Temp
);
371 -- We treat Model as identical to Machine. This is true of IEEE and other
372 -- nice floating-point systems, but not necessarily true of all systems.
374 function Model
(X
: T
) return T
is
383 -- Subtract from the given number a number equivalent to the value of its
384 -- least significant bit. Given that the most significant bit represents
385 -- a value of 1.0 * radix ** (exp - 1), the value we want is obtained by
386 -- shifting this by (mantissa-1) bits to the right, i.e. decreasing the
387 -- exponent by that amount.
389 -- Zero has to be treated specially, since its exponent is zero
391 function Pred
(X
: T
) return T
is
400 Decompose
(X
, X_Frac
, X_Exp
);
402 -- A special case, if the number we had was a positive power of
403 -- two, then we want to subtract half of what we would otherwise
404 -- subtract, since the exponent is going to be reduced.
406 if X_Frac
= 0.5 and then X
> 0.0 then
407 return X
- Gradual_Scaling
(X_Exp
- T
'Machine_Mantissa - 1);
409 -- Otherwise the exponent stays the same
412 return X
- Gradual_Scaling
(X_Exp
- T
'Machine_Mantissa);
421 function Remainder
(X
, Y
: T
) return T
is
449 P_Exp
:= Exponent
(P
);
452 Decompose
(Arg
, Arg_Frac
, Arg_Exp
);
453 Decompose
(P
, P_Frac
, P_Exp
);
455 P
:= Compose
(P_Frac
, Arg_Exp
);
456 K
:= Arg_Exp
- P_Exp
;
460 for Cnt
in reverse 0 .. K
loop
461 if IEEE_Rem
>= P
then
463 IEEE_Rem
:= IEEE_Rem
- P
;
472 -- That completes the calculation of modulus remainder. The final
473 -- step is get the IEEE remainder. Here we need to compare Rem with
474 -- (abs Y) / 2. We must be careful of unrepresentable Y/2 value
475 -- caused by subnormal numbers
486 if A
> B
or else (A
= B
and then not P_Even
) then
487 IEEE_Rem
:= IEEE_Rem
- abs Y
;
490 return Sign_X
* IEEE_Rem
;
498 function Rounding
(X
: T
) return T
is
503 Result
:= Truncation
(abs X
);
504 Tail
:= abs X
- Result
;
507 Result
:= Result
+ 1.0;
516 -- For zero case, make sure sign of zero is preserved
528 -- Return x * rad ** adjustment quickly,
529 -- or quietly underflow to zero, or overflow naturally.
531 function Scaling
(X
: T
; Adjustment
: UI
) return T
is
533 if X
= 0.0 or else Adjustment
= 0 then
537 -- Nonzero x. essentially, just multiply repeatedly by Rad ** (+-2**n).
541 Ex
: UI
:= Adjustment
;
543 -- Y * Rad ** Ex is invariant
547 while Ex
<= -Log_Power
(Expbits
'Last) loop
548 Y
:= Y
* R_Neg_Power
(Expbits
'Last);
549 Ex
:= Ex
+ Log_Power
(Expbits
'Last);
554 for N
in reverse Expbits
'First .. Expbits
'Last - 1 loop
555 if Ex
<= -Log_Power
(N
) then
556 Y
:= Y
* R_Neg_Power
(N
);
557 Ex
:= Ex
+ Log_Power
(N
);
560 -- -Log_Power (N) < Ex <= 0
568 while Ex
>= Log_Power
(Expbits
'Last) loop
569 Y
:= Y
* R_Power
(Expbits
'Last);
570 Ex
:= Ex
- Log_Power
(Expbits
'Last);
575 for N
in reverse Expbits
'First .. Expbits
'Last - 1 loop
576 if Ex
>= Log_Power
(N
) then
577 Y
:= Y
* R_Power
(N
);
578 Ex
:= Ex
- Log_Power
(N
);
581 -- 0 <= Ex < Log_Power (N)
594 -- Similar computation to that of Pred: find value of least significant
595 -- bit of given number, and add. Zero has to be treated specially since
596 -- the exponent can be zero, and also we want the smallest denormal if
597 -- denormals are supported.
599 function Succ
(X
: T
) return T
is
606 X1
:= 2.0 ** T
'Machine_Emin;
608 -- Following loop generates smallest denormal
611 X2
:= T
'Machine (X1
/ 2.0);
619 Decompose
(X
, X_Frac
, X_Exp
);
621 -- A special case, if the number we had was a negative power of
622 -- two, then we want to add half of what we would otherwise add,
623 -- since the exponent is going to be reduced.
625 if X_Frac
= 0.5 and then X
< 0.0 then
626 return X
+ Gradual_Scaling
(X_Exp
- T
'Machine_Mantissa - 1);
628 -- Otherwise the exponent stays the same
631 return X
+ Gradual_Scaling
(X_Exp
- T
'Machine_Mantissa);
640 -- The basic approach is to compute
642 -- T'Machine (RM1 + N) - RM1.
644 -- where N >= 0.0 and RM1 = radix ** (mantissa - 1)
646 -- This works provided that the intermediate result (RM1 + N) does not
647 -- have extra precision (which is why we call Machine). When we compute
648 -- RM1 + N, the exponent of N will be normalized and the mantissa shifted
649 -- shifted appropriately so the lower order bits, which cannot contribute
650 -- to the integer part of N, fall off on the right. When we subtract RM1
651 -- again, the significant bits of N are shifted to the left, and what we
652 -- have is an integer, because only the first e bits are different from
653 -- zero (assuming binary radix here).
655 function Truncation
(X
: T
) return T
is
661 if Result
>= Radix_To_M_Minus_1
then
665 Result
:= Machine
(Radix_To_M_Minus_1
+ Result
) - Radix_To_M_Minus_1
;
667 if Result
> abs X
then
668 Result
:= Result
- 1.0;
677 -- For zero case, make sure sign of zero is preserved
686 -----------------------
687 -- Unbiased_Rounding --
688 -----------------------
690 function Unbiased_Rounding
(X
: T
) return T
is
691 Abs_X
: constant T
:= abs X
;
696 Result
:= Truncation
(Abs_X
);
697 Tail
:= Abs_X
- Result
;
700 Result
:= Result
+ 1.0;
702 elsif Tail
= 0.5 then
703 Result
:= 2.0 * Truncation
((Result
/ 2.0) + 0.5);
712 -- For zero case, make sure sign of zero is preserved
718 end Unbiased_Rounding
;
724 function Valid
(X
: access T
) return Boolean is
726 IEEE_Emin
: constant Integer := T
'Machine_Emin - 1;
727 IEEE_Emax
: constant Integer := T
'Machine_Emax - 1;
729 IEEE_Bias
: constant Integer := -(IEEE_Emin
- 1);
731 subtype IEEE_Exponent_Range
is
732 Integer range IEEE_Emin
- 1 .. IEEE_Emax
+ 1;
734 -- The implementation of this floating point attribute uses
735 -- a representation type Float_Rep that allows direct access to
736 -- the exponent and mantissa parts of a floating point number.
738 -- The Float_Rep type is an array of Float_Word elements. This
739 -- representation is chosen to make it possible to size the
740 -- type based on a generic parameter. Since the array size is
741 -- known at compile-time, efficient code can still be generated.
742 -- The size of Float_Word elements should be large enough to allow
743 -- accessing the exponent in one read, but small enough so that all
744 -- floating point object sizes are a multiple of the Float_Word'Size.
746 -- The following conditions must be met for all possible
747 -- instantiations of the attributes package:
749 -- - T'Size is an integral multiple of Float_Word'Size
751 -- - The exponent and sign are completely contained in a single
752 -- component of Float_Rep, named Most_Significant_Word (MSW).
754 -- - The sign occupies the most significant bit of the MSW
755 -- and the exponent is in the following bits.
756 -- Unused bits (if any) are in the least significant part.
758 type Float_Word
is mod 2**Positive'Min (System
.Word_Size
, 32);
759 type Rep_Index
is range 0 .. 7;
761 Rep_Last
: constant Rep_Index
:= (T
'Size - 1) / Float_Word
'Size;
763 type Float_Rep
is array (Rep_Index
range 0 .. Rep_Last
) of Float_Word
;
765 pragma Suppress_Initialization
(Float_Rep
);
766 -- This pragma supresses the generation of an initialization procedure
767 -- for type Float_Rep when operating in Initialize/Normalize_Scalars
768 -- mode. This is not just a matter of efficiency, but of functionality,
769 -- since Valid has a pragma Inline_Always, which is not permitted if
770 -- there are nested subprograms present.
772 Most_Significant_Word
: constant Rep_Index
:=
773 Rep_Last
* Standard
'Default_Bit_Order;
774 -- Finding the location of the Exponent_Word is a bit tricky.
775 -- In general we assume Word_Order = Bit_Order.
776 -- This expression needs to be refined for VMS.
778 Exponent_Factor
: constant Float_Word
:=
779 2**(Float_Word
'Size - 1) /
780 Float_Word
(IEEE_Emax
- IEEE_Emin
+ 3) *
781 Boolean'Pos (T
'Size /= 96) +
782 Boolean'Pos (T
'Size = 96);
783 -- Factor that the extracted exponent needs to be divided by
784 -- to be in range 0 .. IEEE_Emax - IEEE_Emin + 2.
785 -- Special kludge: Exponent_Factor is 0 for x86 double extended
786 -- as GCC adds 16 unused bits to the type.
788 Exponent_Mask
: constant Float_Word
:=
789 Float_Word
(IEEE_Emax
- IEEE_Emin
+ 2) *
791 -- Value needed to mask out the exponent field.
792 -- This assumes that the range IEEE_Emin - 1 .. IEEE_Emax + 1
793 -- contains 2**N values, for some N in Natural.
795 function To_Float
is new Ada
.Unchecked_Conversion
(Float_Rep
, T
);
797 type Float_Access
is access all T
;
798 function To_Address
is
799 new Ada
.Unchecked_Conversion
(Float_Access
, System
.Address
);
801 XA
: constant System
.Address
:= To_Address
(Float_Access
(X
));
804 pragma Import
(Ada
, R
);
805 for R
'Address use XA
;
806 -- R is a view of the input floating-point parameter. Note that we
807 -- must avoid copying the actual bits of this parameter in float
808 -- form (since it may be a signalling NaN.
810 E
: constant IEEE_Exponent_Range
:=
811 Integer ((R
(Most_Significant_Word
) and Exponent_Mask
) /
814 -- Mask/Shift T to only get bits from the exponent
815 -- Then convert biased value to integer value.
818 -- Float_Rep representation of significant of X.all
823 -- All denormalized numbers are valid, so only invalid numbers
824 -- are overflows and NaN's, both with exponent = Emax + 1.
826 return E
/= IEEE_Emax
+ 1;
830 -- All denormalized numbers except 0.0 are invalid
832 -- Set exponent of X to zero, so we end up with the significand, which
833 -- definitely is a valid number and can be converted back to a float.
836 SR
(Most_Significant_Word
) :=
837 (SR
(Most_Significant_Word
)
838 and not Exponent_Mask
) + Float_Word
(IEEE_Bias
) * Exponent_Factor
;
840 return (E
in IEEE_Emin
.. IEEE_Emax
) or else
841 ((E
= IEEE_Emin
- 1) and then abs To_Float
(SR
) = 1.0);
844 ---------------------
845 -- Unaligned_Valid --
846 ---------------------
848 function Unaligned_Valid
(A
: System
.Address
) return Boolean is
849 subtype FS
is String (1 .. T
'Size / Character'Size);
850 type FSP
is access FS
;
852 function To_FSP
is new Ada
.Unchecked_Conversion
(Address
, FSP
);
857 To_FSP
(Local_T
'Address).all := To_FSP
(A
).all;
858 return Valid
(Local_T
'Access);