Update concepts branch to revision 131834
[official-gcc.git] / gcc / ada / s-fatgen.adb
blobf690177a59fd8438ac3bf25722ac91a6fa5560a9
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . F A T _ G E N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2007, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
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. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
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;
42 with System;
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 :=
62 (Rad ** 1,
63 Rad ** 2,
64 Rad ** 4,
65 Rad ** 8,
66 Rad ** 16,
67 Rad ** 32,
68 Rad ** 64);
70 R_Neg_Power : constant array (Expbits) of T :=
71 (Invrad ** 1,
72 Invrad ** 2,
73 Invrad ** 4,
74 Invrad ** 8,
75 Invrad ** 16,
76 Invrad ** 32,
77 Invrad ** 64);
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.
85 -- Both results are signed, with Frac having the sign of XX, and UI has
86 -- the sign of the exponent. The absolute value of Frac is in the range
87 -- 0.0 <= Frac < 1.0. If Frac = 0.0 or -0.0, then Expo is always zero.
89 function Gradual_Scaling (Adjustment : UI) return T;
90 -- Like Scaling with a first argument of 1.0, but returns the smallest
91 -- denormal rather than zero when the adjustment is smaller than
92 -- Machine_Emin. Used for Succ and Pred.
94 --------------
95 -- Adjacent --
96 --------------
98 function Adjacent (X, Towards : T) return T is
99 begin
100 if Towards = X then
101 return X;
102 elsif Towards > X then
103 return Succ (X);
104 else
105 return Pred (X);
106 end if;
107 end Adjacent;
109 -------------
110 -- Ceiling --
111 -------------
113 function Ceiling (X : T) return T is
114 XT : constant T := Truncation (X);
115 begin
116 if X <= 0.0 then
117 return XT;
118 elsif X = XT then
119 return X;
120 else
121 return XT + 1.0;
122 end if;
123 end Ceiling;
125 -------------
126 -- Compose --
127 -------------
129 function Compose (Fraction : T; Exponent : UI) return T is
130 Arg_Frac : T;
131 Arg_Exp : UI;
132 pragma Unreferenced (Arg_Exp);
133 begin
134 Decompose (Fraction, Arg_Frac, Arg_Exp);
135 return Scaling (Arg_Frac, Exponent);
136 end Compose;
138 ---------------
139 -- Copy_Sign --
140 ---------------
142 function Copy_Sign (Value, Sign : T) return T is
143 Result : T;
145 function Is_Negative (V : T) return Boolean;
146 pragma Import (Intrinsic, Is_Negative);
148 begin
149 Result := abs Value;
151 if Is_Negative (Sign) then
152 return -Result;
153 else
154 return Result;
155 end if;
156 end Copy_Sign;
158 ---------------
159 -- Decompose --
160 ---------------
162 procedure Decompose (XX : T; Frac : out T; Expo : out UI) is
163 X : constant T := T'Machine (XX);
165 begin
166 if X = 0.0 then
167 Frac := X;
168 Expo := 0;
170 -- More useful would be defining Expo to be T'Machine_Emin - 1 or
171 -- T'Machine_Emin - T'Machine_Mantissa, which would preserve
172 -- monotonicity of the exponent function ???
174 -- Check for infinities, transfinites, whatnot
176 elsif X > T'Safe_Last then
177 Frac := Invrad;
178 Expo := T'Machine_Emax + 1;
180 elsif X < T'Safe_First then
181 Frac := -Invrad;
182 Expo := T'Machine_Emax + 2; -- how many extra negative values?
184 else
185 -- Case of nonzero finite x. Essentially, we just multiply
186 -- by Rad ** (+-2**N) to reduce the range.
188 declare
189 Ax : T := abs X;
190 Ex : UI := 0;
192 -- Ax * Rad ** Ex is invariant
194 begin
195 if Ax >= 1.0 then
196 while Ax >= R_Power (Expbits'Last) loop
197 Ax := Ax * R_Neg_Power (Expbits'Last);
198 Ex := Ex + Log_Power (Expbits'Last);
199 end loop;
201 -- Ax < Rad ** 64
203 for N in reverse Expbits'First .. Expbits'Last - 1 loop
204 if Ax >= R_Power (N) then
205 Ax := Ax * R_Neg_Power (N);
206 Ex := Ex + Log_Power (N);
207 end if;
209 -- Ax < R_Power (N)
210 end loop;
212 -- 1 <= Ax < Rad
214 Ax := Ax * Invrad;
215 Ex := Ex + 1;
217 else
218 -- 0 < ax < 1
220 while Ax < R_Neg_Power (Expbits'Last) loop
221 Ax := Ax * R_Power (Expbits'Last);
222 Ex := Ex - Log_Power (Expbits'Last);
223 end loop;
225 -- Rad ** -64 <= Ax < 1
227 for N in reverse Expbits'First .. Expbits'Last - 1 loop
228 if Ax < R_Neg_Power (N) then
229 Ax := Ax * R_Power (N);
230 Ex := Ex - Log_Power (N);
231 end if;
233 -- R_Neg_Power (N) <= Ax < 1
234 end loop;
235 end if;
237 if X > 0.0 then
238 Frac := Ax;
239 else
240 Frac := -Ax;
241 end if;
243 Expo := Ex;
244 end;
245 end if;
246 end Decompose;
248 --------------
249 -- Exponent --
250 --------------
252 function Exponent (X : T) return UI is
253 X_Frac : T;
254 X_Exp : UI;
255 pragma Unreferenced (X_Frac);
256 begin
257 Decompose (X, X_Frac, X_Exp);
258 return X_Exp;
259 end Exponent;
261 -----------
262 -- Floor --
263 -----------
265 function Floor (X : T) return T is
266 XT : constant T := Truncation (X);
267 begin
268 if X >= 0.0 then
269 return XT;
270 elsif XT = X then
271 return X;
272 else
273 return XT - 1.0;
274 end if;
275 end Floor;
277 --------------
278 -- Fraction --
279 --------------
281 function Fraction (X : T) return T is
282 X_Frac : T;
283 X_Exp : UI;
284 pragma Unreferenced (X_Exp);
285 begin
286 Decompose (X, X_Frac, X_Exp);
287 return X_Frac;
288 end Fraction;
290 ---------------------
291 -- Gradual_Scaling --
292 ---------------------
294 function Gradual_Scaling (Adjustment : UI) return T is
295 Y : T;
296 Y1 : T;
297 Ex : UI := Adjustment;
299 begin
300 if Adjustment < T'Machine_Emin - 1 then
301 Y := 2.0 ** T'Machine_Emin;
302 Y1 := Y;
303 Ex := Ex - T'Machine_Emin;
304 while Ex < 0 loop
305 Y := T'Machine (Y / 2.0);
307 if Y = 0.0 then
308 return Y1;
309 end if;
311 Ex := Ex + 1;
312 Y1 := Y;
313 end loop;
315 return Y1;
317 else
318 return Scaling (1.0, Adjustment);
319 end if;
320 end Gradual_Scaling;
322 ------------------
323 -- Leading_Part --
324 ------------------
326 function Leading_Part (X : T; Radix_Digits : UI) return T is
327 L : UI;
328 Y, Z : T;
330 begin
331 if Radix_Digits >= T'Machine_Mantissa then
332 return X;
334 elsif Radix_Digits <= 0 then
335 raise Constraint_Error;
337 else
338 L := Exponent (X) - Radix_Digits;
339 Y := Truncation (Scaling (X, -L));
340 Z := Scaling (Y, L);
341 return Z;
342 end if;
343 end Leading_Part;
345 -------------
346 -- Machine --
347 -------------
349 -- The trick with Machine is to force the compiler to store the result
350 -- in memory so that we do not have extra precision used. The compiler
351 -- is clever, so we have to outwit its possible optimizations! We do
352 -- this by using an intermediate pragma Volatile location.
354 function Machine (X : T) return T is
355 Temp : T;
356 pragma Volatile (Temp);
357 begin
358 Temp := X;
359 return Temp;
360 end Machine;
362 ----------------------
363 -- Machine_Rounding --
364 ----------------------
366 -- For now, the implementation is identical to that of Rounding, which is
367 -- a permissible behavior, but is not the most efficient possible approach.
369 function Machine_Rounding (X : T) return T is
370 Result : T;
371 Tail : T;
373 begin
374 Result := Truncation (abs X);
375 Tail := abs X - Result;
377 if Tail >= 0.5 then
378 Result := Result + 1.0;
379 end if;
381 if X > 0.0 then
382 return Result;
384 elsif X < 0.0 then
385 return -Result;
387 -- For zero case, make sure sign of zero is preserved
389 else
390 return X;
391 end if;
392 end Machine_Rounding;
394 -----------
395 -- Model --
396 -----------
398 -- We treat Model as identical to Machine. This is true of IEEE and other
399 -- nice floating-point systems, but not necessarily true of all systems.
401 function Model (X : T) return T is
402 begin
403 return Machine (X);
404 end Model;
406 ----------
407 -- Pred --
408 ----------
410 -- Subtract from the given number a number equivalent to the value of its
411 -- least significant bit. Given that the most significant bit represents
412 -- a value of 1.0 * radix ** (exp - 1), the value we want is obtained by
413 -- shifting this by (mantissa-1) bits to the right, i.e. decreasing the
414 -- exponent by that amount.
416 -- Zero has to be treated specially, since its exponent is zero
418 function Pred (X : T) return T is
419 X_Frac : T;
420 X_Exp : UI;
422 begin
423 if X = 0.0 then
424 return -Succ (X);
426 else
427 Decompose (X, X_Frac, X_Exp);
429 -- A special case, if the number we had was a positive power of
430 -- two, then we want to subtract half of what we would otherwise
431 -- subtract, since the exponent is going to be reduced.
433 -- Note that X_Frac has the same sign as X, so if X_Frac is 0.5,
434 -- then we know that we have a positive number (and hence a
435 -- positive power of 2).
437 if X_Frac = 0.5 then
438 return X - Gradual_Scaling (X_Exp - T'Machine_Mantissa - 1);
440 -- Otherwise the exponent is unchanged
442 else
443 return X - Gradual_Scaling (X_Exp - T'Machine_Mantissa);
444 end if;
445 end if;
446 end Pred;
448 ---------------
449 -- Remainder --
450 ---------------
452 function Remainder (X, Y : T) return T is
453 A : T;
454 B : T;
455 Arg : T;
456 P : T;
457 P_Frac : T;
458 Sign_X : T;
459 IEEE_Rem : T;
460 Arg_Exp : UI;
461 P_Exp : UI;
462 K : UI;
463 P_Even : Boolean;
465 Arg_Frac : T;
466 pragma Unreferenced (Arg_Frac);
468 begin
469 if Y = 0.0 then
470 raise Constraint_Error;
471 end if;
473 if X > 0.0 then
474 Sign_X := 1.0;
475 Arg := X;
476 else
477 Sign_X := -1.0;
478 Arg := -X;
479 end if;
481 P := abs Y;
483 if Arg < P then
484 P_Even := True;
485 IEEE_Rem := Arg;
486 P_Exp := Exponent (P);
488 else
489 Decompose (Arg, Arg_Frac, Arg_Exp);
490 Decompose (P, P_Frac, P_Exp);
492 P := Compose (P_Frac, Arg_Exp);
493 K := Arg_Exp - P_Exp;
494 P_Even := True;
495 IEEE_Rem := Arg;
497 for Cnt in reverse 0 .. K loop
498 if IEEE_Rem >= P then
499 P_Even := False;
500 IEEE_Rem := IEEE_Rem - P;
501 else
502 P_Even := True;
503 end if;
505 P := P * 0.5;
506 end loop;
507 end if;
509 -- That completes the calculation of modulus remainder. The final
510 -- step is get the IEEE remainder. Here we need to compare Rem with
511 -- (abs Y) / 2. We must be careful of unrepresentable Y/2 value
512 -- caused by subnormal numbers
514 if P_Exp >= 0 then
515 A := IEEE_Rem;
516 B := abs Y * 0.5;
518 else
519 A := IEEE_Rem * 2.0;
520 B := abs Y;
521 end if;
523 if A > B or else (A = B and then not P_Even) then
524 IEEE_Rem := IEEE_Rem - abs Y;
525 end if;
527 return Sign_X * IEEE_Rem;
528 end Remainder;
530 --------------
531 -- Rounding --
532 --------------
534 function Rounding (X : T) return T is
535 Result : T;
536 Tail : T;
538 begin
539 Result := Truncation (abs X);
540 Tail := abs X - Result;
542 if Tail >= 0.5 then
543 Result := Result + 1.0;
544 end if;
546 if X > 0.0 then
547 return Result;
549 elsif X < 0.0 then
550 return -Result;
552 -- For zero case, make sure sign of zero is preserved
554 else
555 return X;
556 end if;
557 end Rounding;
559 -------------
560 -- Scaling --
561 -------------
563 -- Return x * rad ** adjustment quickly,
564 -- or quietly underflow to zero, or overflow naturally.
566 function Scaling (X : T; Adjustment : UI) return T is
567 begin
568 if X = 0.0 or else Adjustment = 0 then
569 return X;
570 end if;
572 -- Nonzero x essentially, just multiply repeatedly by Rad ** (+-2**n)
574 declare
575 Y : T := X;
576 Ex : UI := Adjustment;
578 -- Y * Rad ** Ex is invariant
580 begin
581 if Ex < 0 then
582 while Ex <= -Log_Power (Expbits'Last) loop
583 Y := Y * R_Neg_Power (Expbits'Last);
584 Ex := Ex + Log_Power (Expbits'Last);
585 end loop;
587 -- -64 < Ex <= 0
589 for N in reverse Expbits'First .. Expbits'Last - 1 loop
590 if Ex <= -Log_Power (N) then
591 Y := Y * R_Neg_Power (N);
592 Ex := Ex + Log_Power (N);
593 end if;
595 -- -Log_Power (N) < Ex <= 0
596 end loop;
598 -- Ex = 0
600 else
601 -- Ex >= 0
603 while Ex >= Log_Power (Expbits'Last) loop
604 Y := Y * R_Power (Expbits'Last);
605 Ex := Ex - Log_Power (Expbits'Last);
606 end loop;
608 -- 0 <= Ex < 64
610 for N in reverse Expbits'First .. Expbits'Last - 1 loop
611 if Ex >= Log_Power (N) then
612 Y := Y * R_Power (N);
613 Ex := Ex - Log_Power (N);
614 end if;
616 -- 0 <= Ex < Log_Power (N)
618 end loop;
620 -- Ex = 0
621 end if;
623 return Y;
624 end;
625 end Scaling;
627 ----------
628 -- Succ --
629 ----------
631 -- Similar computation to that of Pred: find value of least significant
632 -- bit of given number, and add. Zero has to be treated specially since
633 -- the exponent can be zero, and also we want the smallest denormal if
634 -- denormals are supported.
636 function Succ (X : T) return T is
637 X_Frac : T;
638 X_Exp : UI;
639 X1, X2 : T;
641 begin
642 if X = 0.0 then
643 X1 := 2.0 ** T'Machine_Emin;
645 -- Following loop generates smallest denormal
647 loop
648 X2 := T'Machine (X1 / 2.0);
649 exit when X2 = 0.0;
650 X1 := X2;
651 end loop;
653 return X1;
655 else
656 Decompose (X, X_Frac, X_Exp);
658 -- A special case, if the number we had was a negative power of
659 -- two, then we want to add half of what we would otherwise add,
660 -- since the exponent is going to be reduced.
662 -- Note that X_Frac has the same sign as X, so if X_Frac is -0.5,
663 -- then we know that we have a negative number (and hence a
664 -- negative power of 2).
666 if X_Frac = -0.5 then
667 return X + Gradual_Scaling (X_Exp - T'Machine_Mantissa - 1);
669 -- Otherwise the exponent is unchanged
671 else
672 return X + Gradual_Scaling (X_Exp - T'Machine_Mantissa);
673 end if;
674 end if;
675 end Succ;
677 ----------------
678 -- Truncation --
679 ----------------
681 -- The basic approach is to compute
683 -- T'Machine (RM1 + N) - RM1
685 -- where N >= 0.0 and RM1 = radix ** (mantissa - 1)
687 -- This works provided that the intermediate result (RM1 + N) does not
688 -- have extra precision (which is why we call Machine). When we compute
689 -- RM1 + N, the exponent of N will be normalized and the mantissa shifted
690 -- shifted appropriately so the lower order bits, which cannot contribute
691 -- to the integer part of N, fall off on the right. When we subtract RM1
692 -- again, the significant bits of N are shifted to the left, and what we
693 -- have is an integer, because only the first e bits are different from
694 -- zero (assuming binary radix here).
696 function Truncation (X : T) return T is
697 Result : T;
699 begin
700 Result := abs X;
702 if Result >= Radix_To_M_Minus_1 then
703 return Machine (X);
705 else
706 Result := Machine (Radix_To_M_Minus_1 + Result) - Radix_To_M_Minus_1;
708 if Result > abs X then
709 Result := Result - 1.0;
710 end if;
712 if X > 0.0 then
713 return Result;
715 elsif X < 0.0 then
716 return -Result;
718 -- For zero case, make sure sign of zero is preserved
720 else
721 return X;
722 end if;
723 end if;
724 end Truncation;
726 -----------------------
727 -- Unbiased_Rounding --
728 -----------------------
730 function Unbiased_Rounding (X : T) return T is
731 Abs_X : constant T := abs X;
732 Result : T;
733 Tail : T;
735 begin
736 Result := Truncation (Abs_X);
737 Tail := Abs_X - Result;
739 if Tail > 0.5 then
740 Result := Result + 1.0;
742 elsif Tail = 0.5 then
743 Result := 2.0 * Truncation ((Result / 2.0) + 0.5);
744 end if;
746 if X > 0.0 then
747 return Result;
749 elsif X < 0.0 then
750 return -Result;
752 -- For zero case, make sure sign of zero is preserved
754 else
755 return X;
756 end if;
757 end Unbiased_Rounding;
759 -----------
760 -- Valid --
761 -----------
763 -- Note: this routine does not work for VAX float. We compensate for this
764 -- in Exp_Attr by using the Valid functions in Vax_Float_Operations rather
765 -- than the corresponding instantiation of this function.
767 function Valid (X : not null access T) return Boolean is
769 IEEE_Emin : constant Integer := T'Machine_Emin - 1;
770 IEEE_Emax : constant Integer := T'Machine_Emax - 1;
772 IEEE_Bias : constant Integer := -(IEEE_Emin - 1);
774 subtype IEEE_Exponent_Range is
775 Integer range IEEE_Emin - 1 .. IEEE_Emax + 1;
777 -- The implementation of this floating point attribute uses a
778 -- representation type Float_Rep that allows direct access to the
779 -- exponent and mantissa parts of a floating point number.
781 -- The Float_Rep type is an array of Float_Word elements. This
782 -- representation is chosen to make it possible to size the type based
783 -- on a generic parameter. Since the array size is known at compile
784 -- time, efficient code can still be generated. The size of Float_Word
785 -- elements should be large enough to allow accessing the exponent in
786 -- one read, but small enough so that all floating point object sizes
787 -- are a multiple of the Float_Word'Size.
789 -- The following conditions must be met for all possible
790 -- instantiations of the attributes package:
792 -- - T'Size is an integral multiple of Float_Word'Size
794 -- - The exponent and sign are completely contained in a single
795 -- component of Float_Rep, named Most_Significant_Word (MSW).
797 -- - The sign occupies the most significant bit of the MSW and the
798 -- exponent is in the following bits. Unused bits (if any) are in
799 -- the least significant part.
801 type Float_Word is mod 2**Positive'Min (System.Word_Size, 32);
802 type Rep_Index is range 0 .. 7;
804 Rep_Words : constant Positive :=
805 (T'Size + Float_Word'Size - 1) / Float_Word'Size;
806 Rep_Last : constant Rep_Index := Rep_Index'Min
807 (Rep_Index (Rep_Words - 1), (T'Mantissa + 16) / Float_Word'Size);
808 -- Determine the number of Float_Words needed for representing the
809 -- entire floating-point value. Do not take into account excessive
810 -- padding, as occurs on IA-64 where 80 bits floats get padded to 128
811 -- bits. In general, the exponent field cannot be larger than 15 bits,
812 -- even for 128-bit floating-point types, so the final format size
813 -- won't be larger than T'Mantissa + 16.
815 type Float_Rep is
816 array (Rep_Index range 0 .. Rep_Index (Rep_Words - 1)) of Float_Word;
818 pragma Suppress_Initialization (Float_Rep);
819 -- This pragma suppresses the generation of an initialization procedure
820 -- for type Float_Rep when operating in Initialize/Normalize_Scalars
821 -- mode. This is not just a matter of efficiency, but of functionality,
822 -- since Valid has a pragma Inline_Always, which is not permitted if
823 -- there are nested subprograms present.
825 Most_Significant_Word : constant Rep_Index :=
826 Rep_Last * Standard'Default_Bit_Order;
827 -- Finding the location of the Exponent_Word is a bit tricky. In general
828 -- we assume Word_Order = Bit_Order. This expression needs to be refined
829 -- for VMS.
831 Exponent_Factor : constant Float_Word :=
832 2**(Float_Word'Size - 1) /
833 Float_Word (IEEE_Emax - IEEE_Emin + 3) *
834 Boolean'Pos (Most_Significant_Word /= 2) +
835 Boolean'Pos (Most_Significant_Word = 2);
836 -- Factor that the extracted exponent needs to be divided by to be in
837 -- range 0 .. IEEE_Emax - IEEE_Emin + 2. Special kludge: Exponent_Factor
838 -- is 1 for x86/IA64 double extended as GCC adds unused bits to the
839 -- type.
841 Exponent_Mask : constant Float_Word :=
842 Float_Word (IEEE_Emax - IEEE_Emin + 2) *
843 Exponent_Factor;
844 -- Value needed to mask out the exponent field. This assumes that the
845 -- range IEEE_Emin - 1 .. IEEE_Emax + contains 2**N values, for some N
846 -- in Natural.
848 function To_Float is new Ada.Unchecked_Conversion (Float_Rep, T);
850 type Float_Access is access all T;
851 function To_Address is
852 new Ada.Unchecked_Conversion (Float_Access, System.Address);
854 XA : constant System.Address := To_Address (Float_Access (X));
856 R : Float_Rep;
857 pragma Import (Ada, R);
858 for R'Address use XA;
859 -- R is a view of the input floating-point parameter. Note that we
860 -- must avoid copying the actual bits of this parameter in float
861 -- form (since it may be a signalling NaN.
863 E : constant IEEE_Exponent_Range :=
864 Integer ((R (Most_Significant_Word) and Exponent_Mask) /
865 Exponent_Factor)
866 - IEEE_Bias;
867 -- Mask/Shift T to only get bits from the exponent. Then convert biased
868 -- value to integer value.
870 SR : Float_Rep;
871 -- Float_Rep representation of significant of X.all
873 begin
874 if T'Denorm then
876 -- All denormalized numbers are valid, so the only invalid numbers
877 -- are overflows and NaNs, both with exponent = Emax + 1.
879 return E /= IEEE_Emax + 1;
881 end if;
883 -- All denormalized numbers except 0.0 are invalid
885 -- Set exponent of X to zero, so we end up with the significand, which
886 -- definitely is a valid number and can be converted back to a float.
888 SR := R;
889 SR (Most_Significant_Word) :=
890 (SR (Most_Significant_Word)
891 and not Exponent_Mask) + Float_Word (IEEE_Bias) * Exponent_Factor;
893 return (E in IEEE_Emin .. IEEE_Emax) or else
894 ((E = IEEE_Emin - 1) and then abs To_Float (SR) = 1.0);
895 end Valid;
897 ---------------------
898 -- Unaligned_Valid --
899 ---------------------
901 function Unaligned_Valid (A : System.Address) return Boolean is
902 subtype FS is String (1 .. T'Size / Character'Size);
903 type FSP is access FS;
905 function To_FSP is new Ada.Unchecked_Conversion (Address, FSP);
907 Local_T : aliased T;
909 begin
910 -- Note that we have to be sure that we do not load the value into a
911 -- floating-point register, since a signalling NaN may cause a trap.
912 -- The following assignment is what does the actual alignment, since
913 -- we know that the target Local_T is aligned.
915 To_FSP (Local_T'Address).all := To_FSP (A).all;
917 -- Now that we have an aligned value, we can use the normal aligned
918 -- version of Valid to obtain the required result.
920 return Valid (Local_T'Access);
921 end Unaligned_Valid;
923 end System.Fat_Gen;