home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / s-fatgen.adb < prev    next >
Text File  |  2000-07-19  |  23KB  |  825 lines

  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. --                            $Revision: 1.15 $
  10. --                                                                          --
  11. --          Copyright (C) 1992-2000 Free Software Foundation, Inc.          --
  12. --                                                                          --
  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.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. --  The implementation here is portable to any IEEE implementation. It does
  37. --  not handle non-binary radix, and also assumes that model numbers and
  38. --  machine numbers are basically identical, which is not true of all possible
  39. --  floating-point implementations. On a non-IEEE machine, this body must be
  40. --  specialized appropriately, or better still, its generic instantiations
  41. --  should be replaced by efficient machine-specific code.
  42.  
  43. with Ada.Unchecked_Conversion; use Ada;
  44. package body System.Fat_Gen is
  45.  
  46.    Float_Radix        : constant T := T (T'Machine_Radix);
  47.    Float_Radix_Inv    : constant T := 1.0 / Float_Radix;
  48.    Radix_To_M_Minus_1 : constant T := Float_Radix ** (T'Machine_Mantissa - 1);
  49.  
  50.    pragma Assert (T'Machine_Radix = 2);
  51.    --  This version does not handle radix 16
  52.  
  53.    --  Constants for Decompose and Scaling
  54.  
  55.    Rad    : constant T := T (T'Machine_Radix);
  56.    Invrad : constant T := 1.0 / Rad;
  57.  
  58.    subtype Expbits is Integer range 0 .. 6;
  59.    --  2 ** (2 ** 7) might overflow.  how big can radix-16 exponents get?
  60.  
  61.    Log_Power : constant array (Expbits) of Integer := (1, 2, 4, 8, 16, 32, 64);
  62.  
  63.    R_Power : constant array (Expbits) of T :=
  64.      (Rad **  1,
  65.       Rad **  2,
  66.       Rad **  4,
  67.       Rad **  8,
  68.       Rad ** 16,
  69.       Rad ** 32,
  70.       Rad ** 64);
  71.  
  72.    R_Neg_Power : constant array (Expbits) of T :=
  73.      (Invrad **  1,
  74.       Invrad **  2,
  75.       Invrad **  4,
  76.       Invrad **  8,
  77.       Invrad ** 16,
  78.       Invrad ** 32,
  79.       Invrad ** 64);
  80.  
  81.    -----------------------
  82.    -- Local Subprograms --
  83.    -----------------------
  84.  
  85.    procedure Decompose (XX : T; Frac : out T; Expo : out UI);
  86.    --  Decomposes a floating-point number into fraction and exponent parts
  87.  
  88.    function Gradual_Scaling  (Adjustment : UI) return T;
  89.    --  Like Scaling with a first argument of 1.0, but returns the smallest
  90.    --  denormal rather than zero when the adjustment is smaller than
  91.    --  Machine_Emin. Used for Succ and Pred.
  92.  
  93.    --------------
  94.    -- Adjacent --
  95.    --------------
  96.  
  97.    function Adjacent (X, Towards : T) return T is
  98.    begin
  99.       if Towards = X then
  100.          return X;
  101.  
  102.       elsif Towards > X then
  103.          return Succ (X);
  104.  
  105.       else
  106.          return Pred (X);
  107.       end if;
  108.    end Adjacent;
  109.  
  110.    -------------
  111.    -- Ceiling --
  112.    -------------
  113.  
  114.    function Ceiling (X : T) return T is
  115.       XT : constant T := Truncation (X);
  116.  
  117.    begin
  118.       if X <= 0.0 then
  119.          return XT;
  120.  
  121.       elsif X = XT then
  122.          return X;
  123.  
  124.       else
  125.          return XT + 1.0;
  126.       end if;
  127.    end Ceiling;
  128.  
  129.    -------------
  130.    -- Compose --
  131.    -------------
  132.  
  133.    function Compose (Fraction : T; Exponent : UI) return T is
  134.       Arg_Frac : T;
  135.       Arg_Exp  : UI;
  136.  
  137.    begin
  138.       Decompose (Fraction, Arg_Frac, Arg_Exp);
  139.       return Scaling (Arg_Frac, Exponent);
  140.    end Compose;
  141.  
  142.    ---------------
  143.    -- Copy_Sign --
  144.    ---------------
  145.  
  146.    function Copy_Sign (Value, Sign : T) return T is
  147.       Result : T;
  148.  
  149.       function Is_Negative (V : T) return Boolean;
  150.       pragma Import (Intrinsic, Is_Negative);
  151.  
  152.    begin
  153.       Result := abs Value;
  154.  
  155.       if Is_Negative (Sign) then
  156.          return -Result;
  157.       else
  158.          return Result;
  159.       end if;
  160.    end Copy_Sign;
  161.  
  162.    ---------------
  163.    -- Decompose --
  164.    ---------------
  165.  
  166.    procedure Decompose (XX : T; Frac : out T; Expo : out UI) is
  167.       X : T := T'Machine (XX);
  168.  
  169.    begin
  170.       if X = 0.0 then
  171.          Frac := X;
  172.          Expo := 0;
  173.  
  174.          --  More useful would be defining Expo to be T'Machine_Emin - 1 or
  175.          --  T'Machine_Emin - T'Machine_Mantissa, which would preserve
  176.          --  monotonicity of the exponent fuction ???
  177.  
  178.       --  Check for infinities, transfinites, whatnot.
  179.  
  180.       elsif X > T'Safe_Last then
  181.          Frac := Invrad;
  182.          Expo := T'Machine_Emax + 1;
  183.  
  184.       elsif X < T'Safe_First then
  185.          Frac := -Invrad;
  186.          Expo := T'Machine_Emax + 2;    -- how many extra negative values?
  187.  
  188.       else
  189.          --  Case of nonzero finite x. Essentially, we just multiply
  190.          --  by Rad ** (+-2**N) to reduce the range.
  191.  
  192.          declare
  193.             Ax : T  := abs X;
  194.             Ex : UI := 0;
  195.  
  196.          --  Ax * Rad ** Ex is invariant.
  197.  
  198.          begin
  199.             if Ax >= 1.0 then
  200.                while Ax >= R_Power (Expbits'Last) loop
  201.                   Ax := Ax * R_Neg_Power (Expbits'Last);
  202.                   Ex := Ex + Log_Power (Expbits'Last);
  203.                end loop;
  204.  
  205.                --  Ax < Rad ** 64
  206.  
  207.                for N in reverse Expbits'First .. Expbits'Last - 1 loop
  208.                   if Ax >= R_Power (N) then
  209.                      Ax := Ax * R_Neg_Power (N);
  210.                      Ex := Ex + Log_Power (N);
  211.                   end if;
  212.  
  213.                   --  Ax < R_Power (N)
  214.                end loop;
  215.  
  216.                --  1 <= Ax < Rad
  217.  
  218.                Ax := Ax * Invrad;
  219.                Ex := Ex + 1;
  220.  
  221.             else
  222.                --  0 < ax < 1
  223.  
  224.                while Ax < R_Neg_Power (Expbits'Last) loop
  225.                   Ax := Ax * R_Power (Expbits'Last);
  226.                   Ex := Ex - Log_Power (Expbits'Last);
  227.                end loop;
  228.  
  229.                --  Rad ** -64 <= Ax < 1
  230.  
  231.                for N in reverse Expbits'First .. Expbits'Last - 1 loop
  232.                   if Ax < R_Neg_Power (N) then
  233.                      Ax := Ax * R_Power (N);
  234.                      Ex := Ex - Log_Power (N);
  235.                   end if;
  236.  
  237.                   --  R_Neg_Power (N) <= Ax < 1
  238.                end loop;
  239.             end if;
  240.  
  241.             if X > 0.0 then
  242.                Frac := Ax;
  243.             else
  244.                Frac := -Ax;
  245.             end if;
  246.  
  247.             Expo := Ex;
  248.          end;
  249.       end if;
  250.    end Decompose;
  251.  
  252.    ---------------------
  253.    -- Gradual_Scaling --
  254.    ---------------------
  255.  
  256.    function Gradual_Scaling  (Adjustment : UI) return T is
  257.       Y  : T;
  258.       Y1 : T;
  259.       Ex : UI := Adjustment;
  260.  
  261.    begin
  262.       if Adjustment < T'Machine_Emin then
  263.          Y  := 2.0 ** T'Machine_Emin;
  264.          Y1 := Y;
  265.          Ex := Ex - T'Machine_Emin;
  266.  
  267.          while Ex <= 0 loop
  268.             Y := T'Machine (Y / 2.0);
  269.  
  270.             if Y = 0.0 then
  271.                return Y1;
  272.             end if;
  273.  
  274.             Ex := Ex + 1;
  275.             Y1 := Y;
  276.          end loop;
  277.  
  278.          return Y1;
  279.  
  280.       else
  281.          return Scaling (1.0, Adjustment);
  282.       end if;
  283.    end Gradual_Scaling;
  284.  
  285.    --------------
  286.    -- Exponent --
  287.    --------------
  288.  
  289.    function Exponent (X : T) return UI is
  290.       X_Frac : T;
  291.       X_Exp  : UI;
  292.  
  293.    begin
  294.       Decompose (X, X_Frac, X_Exp);
  295.       return X_Exp;
  296.    end Exponent;
  297.  
  298.    -----------
  299.    -- Floor --
  300.    -----------
  301.  
  302.    function Floor (X : T) return T is
  303.       XT : constant T := Truncation (X);
  304.  
  305.    begin
  306.       if X >= 0.0 then
  307.          return XT;
  308.  
  309.       elsif XT = X then
  310.          return X;
  311.  
  312.       else
  313.          return XT - 1.0;
  314.       end if;
  315.    end Floor;
  316.  
  317.    --------------
  318.    -- Fraction --
  319.    --------------
  320.  
  321.    function Fraction (X : T) return T is
  322.       X_Frac : T;
  323.       X_Exp  : UI;
  324.  
  325.    begin
  326.       Decompose (X, X_Frac, X_Exp);
  327.       return X_Frac;
  328.    end Fraction;
  329.  
  330.    ------------------
  331.    -- Leading_Part --
  332.    ------------------
  333.  
  334.    function Leading_Part (X : T; Radix_Digits : UI) return T is
  335.       L    : UI;
  336.       Y, Z : T;
  337.  
  338.    begin
  339.       if Radix_Digits >= T'Machine_Mantissa then
  340.          return X;
  341.  
  342.       else
  343.          L := Exponent (X) - Radix_Digits;
  344.          Y := Truncation (Scaling (X, -L));
  345.          Z := Scaling (Y, L);
  346.          return Z;
  347.       end if;
  348.  
  349.    end Leading_Part;
  350.  
  351.    -------------
  352.    -- Machine --
  353.    -------------
  354.  
  355.    --  The trick with Machine is to force the compiler to store the result
  356.    --  in memory so that we do not have extra precision used. The compiler
  357.    --  is clever, so we have to outwit its possible optimizations! We do
  358.    --  this by using an intermediate pragma Volatile location.
  359.  
  360.    function Machine (X : T) return T is
  361.       Temp : T;
  362.       pragma Volatile (Temp);
  363.  
  364.    begin
  365.       Temp := X;
  366.       return Temp;
  367.    end Machine;
  368.  
  369.    -----------
  370.    -- Model --
  371.    -----------
  372.  
  373.    --  We treat Model as identical to Machine. This is true of IEEE and other
  374.    --  nice floating-point systems, but not necessarily true of all systems.
  375.  
  376.    function Model (X : T) return T is
  377.    begin
  378.       return Machine (X);
  379.    end Model;
  380.  
  381.    ----------
  382.    -- Pred --
  383.    ----------
  384.  
  385.    --  Subtract from the given number a number equivalent to the value of its
  386.    --  least significant bit. Given that the most significant bit represents
  387.    --  a value of 1.0 * radix ** (exp - 1), the value we want is obtained by
  388.    --  shifting this by (mantissa-1) bits to the right, i.e. decreasing the
  389.    --  exponent by that amount.
  390.  
  391.    --  Zero has to be treated specially, since its exponent is zero
  392.  
  393.    function Pred (X : T) return T is
  394.       X_Frac : T;
  395.       X_Exp  : UI;
  396.  
  397.    begin
  398.       if X = 0.0 then
  399.          return -Succ (X);
  400.  
  401.       else
  402.          Decompose (X, X_Frac, X_Exp);
  403.  
  404.          --  A special case, if the number we had was a positive power of
  405.          --  two, then we want to subtract half of what we would otherwise
  406.          --  subtract, since the exponent is going to be reduced.
  407.  
  408.          if X_Frac = 0.5 and then X > 0.0 then
  409.             return X - Gradual_Scaling (X_Exp - T'Machine_Mantissa - 1);
  410.  
  411.          --  Otherwise the exponent stays the same
  412.  
  413.          else
  414.             return X - Gradual_Scaling (X_Exp - T'Machine_Mantissa);
  415.          end if;
  416.       end if;
  417.    end Pred;
  418.  
  419.    ---------------
  420.    -- Remainder --
  421.    ---------------
  422.  
  423.    function Remainder (X, Y : T) return T is
  424.       A        : T;
  425.       B        : T;
  426.       Arg      : T;
  427.       P        : T;
  428.       Arg_Frac : T;
  429.       P_Frac   : T;
  430.       Sign_X   : T;
  431.       IEEE_Rem : T;
  432.       Arg_Exp  : UI;
  433.       P_Exp    : UI;
  434.       K        : UI;
  435.       P_Even   : Boolean;
  436.  
  437.    begin
  438.       if X > 0.0 then
  439.          Sign_X :=  1.0;
  440.          Arg := X;
  441.       else
  442.          Sign_X := -1.0;
  443.          Arg := -X;
  444.       end if;
  445.  
  446.       P := abs Y;
  447.  
  448.       if Arg < P then
  449.          P_Even := True;
  450.          IEEE_Rem := Arg;
  451.          P_Exp := Exponent (P);
  452.  
  453.       else
  454.          Decompose (Arg, Arg_Frac, Arg_Exp);
  455.          Decompose (P,   P_Frac,   P_Exp);
  456.  
  457.          P := Compose (P_Frac, Arg_Exp);
  458.          K := UI (Arg_Exp) - UI (P_Exp);
  459.          P_Even := True;
  460.          IEEE_Rem := Arg;
  461.  
  462.          for Cnt in reverse 0 .. K loop
  463.             if IEEE_Rem >= P then
  464.                P_Even := False;
  465.                IEEE_Rem := IEEE_Rem - P;
  466.             else
  467.                P_Even := True;
  468.             end if;
  469.  
  470.             P := P * 0.5;
  471.          end loop;
  472.       end if;
  473.  
  474.       --  That completes the calculation of modulus remainder. The final
  475.       --  step is get the IEEE remainder. Here we need to compare Rem with
  476.       --  (abs Y) / 2. We must be careful of unrepresentable Y/2 value
  477.       --  caused by subnormal numbers
  478.  
  479.       if P_Exp >= 0 then
  480.          A := IEEE_Rem;
  481.          B := abs Y * 0.5;
  482.  
  483.       else
  484.          A := IEEE_Rem * 2.0;
  485.          B := abs Y;
  486.       end if;
  487.  
  488.       if A > B or else (A = B and then not P_Even) then
  489.          IEEE_Rem := IEEE_Rem - abs Y;
  490.       end if;
  491.  
  492.       return Sign_X * IEEE_Rem;
  493.  
  494.    end Remainder;
  495.  
  496.    --------------
  497.    -- Rounding --
  498.    --------------
  499.  
  500.    function Rounding (X : T) return T is
  501.       Result : T;
  502.       Tail   : T;
  503.  
  504.    begin
  505.       Result := Truncation (abs X);
  506.       Tail   := abs X - Result;
  507.  
  508.       if Tail >= 0.5  then
  509.          Result := Result + 1.0;
  510.       end if;
  511.  
  512.       if X > 0.0 then
  513.          return Result;
  514.  
  515.       elsif X < 0.0 then
  516.          return -Result;
  517.  
  518.       --  For zero case, make sure sign of zero is preserved
  519.  
  520.       else
  521.          return X;
  522.       end if;
  523.  
  524.    end Rounding;
  525.  
  526.    -------------
  527.    -- Scaling --
  528.    -------------
  529.  
  530.    --  Return x * rad ** adjustment quickly,
  531.    --  or quietly underflow to zero, or overflow naturally.
  532.  
  533.    function Scaling (X : T; Adjustment : UI) return T is
  534.    begin
  535.       if X = 0.0 or else Adjustment = 0 then
  536.          return X;
  537.       end if;
  538.  
  539.       --  Nonzero x. essentially, just multiply repeatedly by Rad ** (+-2**n).
  540.  
  541.       declare
  542.          Y  : T  := X;
  543.          Ex : UI := Adjustment;
  544.  
  545.       --  Y * Rad ** Ex is invariant
  546.  
  547.       begin
  548.          if Ex < 0 then
  549.             while Ex <= -Log_Power (Expbits'Last) loop
  550.                Y := Y * R_Neg_Power (Expbits'Last);
  551.                Ex := Ex + Log_Power (Expbits'Last);
  552.             end loop;
  553.  
  554.             --  -64 < Ex <= 0
  555.  
  556.             for N in reverse Expbits'First .. Expbits'Last - 1 loop
  557.                if Ex <= -Log_Power (N) then
  558.                   Y := Y * R_Neg_Power (N);
  559.                   Ex := Ex + Log_Power (N);
  560.                end if;
  561.  
  562.                --  -Log_Power (N) < Ex <= 0
  563.             end loop;
  564.  
  565.             --  Ex = 0
  566.  
  567.          else
  568.             --  Ex >= 0
  569.  
  570.             while Ex >= Log_Power (Expbits'Last) loop
  571.                Y := Y * R_Power (Expbits'Last);
  572.                Ex := Ex - Log_Power (Expbits'Last);
  573.             end loop;
  574.  
  575.             --  0 <= Ex < 64
  576.  
  577.             for N in reverse Expbits'First .. Expbits'Last - 1 loop
  578.                if Ex >= Log_Power (N) then
  579.                   Y := Y * R_Power (N);
  580.                   Ex := Ex - Log_Power (N);
  581.                end if;
  582.  
  583.                --  0 <= Ex < Log_Power (N)
  584.             end loop;
  585.  
  586.             --  Ex = 0
  587.          end if;
  588.          return Y;
  589.       end;
  590.    end Scaling;
  591.  
  592.    ----------
  593.    -- Succ --
  594.    ----------
  595.  
  596.    --  Similar computation to that of Pred: find value of least significant
  597.    --  bit of given number, and add. Zero has to be treated specially since
  598.    --  the exponent can be zero, and also we want the smallest denormal if
  599.    --  denormals are supported.
  600.  
  601.    function Succ (X : T) return T is
  602.       X_Frac : T;
  603.       X_Exp  : UI;
  604.       X1, X2 : T;
  605.  
  606.    begin
  607.       if X = 0.0 then
  608.          X1 := 2.0 ** T'Machine_Emin;
  609.  
  610.          --  Following loop generates smallest denormal
  611.  
  612.          loop
  613.             X2 := T'Machine (X1 / 2.0);
  614.             exit when X2 = 0.0;
  615.             X1 := X2;
  616.          end loop;
  617.  
  618.          return X1;
  619.  
  620.       else
  621.          Decompose (X, X_Frac, X_Exp);
  622.  
  623.          --  A special case, if the number we had was a negative power of
  624.          --  two, then we want to add half of what we would otherwise add,
  625.          --  since the exponent is going to be reduced.
  626.  
  627.          if X_Frac = 0.5 and then X < 0.0 then
  628.             return X + Gradual_Scaling (X_Exp - T'Machine_Mantissa - 1);
  629.  
  630.          --  Otherwise the exponent stays the same
  631.  
  632.          else
  633.             return X + Gradual_Scaling (X_Exp - T'Machine_Mantissa);
  634.          end if;
  635.       end if;
  636.    end Succ;
  637.  
  638.    ----------------
  639.    -- Truncation --
  640.    ----------------
  641.  
  642.    --  The basic approach is to compute
  643.  
  644.    --    T'Machine (RM1 + N) - RM1.
  645.  
  646.    --  where N >= 0.0 and RM1 = radix ** (mantissa - 1)
  647.  
  648.    --  This works provided that the intermediate result (RM1 + N) does not
  649.    --  have extra precision (which is why we call Machine). When we compute
  650.    --  RM1 + N, the exponent of N will be normalized and the mantissa shifted
  651.    --  shifted appropriately so the lower order bits, which cannot contribute
  652.    --  to the integer part of N, fall off on the right. When we subtract RM1
  653.    --  again, the significant bits of N are shifted to the left, and what we
  654.    --  have is an integer, because only the first e bits are different from
  655.    --  zero (assuming binary radix here).
  656.  
  657.    function Truncation (X : T) return T is
  658.       Result : T;
  659.  
  660.    begin
  661.       Result := abs X;
  662.  
  663.       if Result >= Radix_To_M_Minus_1 then
  664.          return Machine (X);
  665.  
  666.       else
  667.          Result := Machine (Radix_To_M_Minus_1 + Result) - Radix_To_M_Minus_1;
  668.  
  669.          if Result > abs X  then
  670.             Result := Result - 1.0;
  671.          end if;
  672.  
  673.          if X > 0.0 then
  674.             return  Result;
  675.  
  676.          elsif X < 0.0 then
  677.             return -Result;
  678.  
  679.          --  For zero case, make sure sign of zero is preserved
  680.  
  681.          else
  682.             return X;
  683.          end if;
  684.       end if;
  685.  
  686.    end Truncation;
  687.  
  688.    -----------------------
  689.    -- Unbiased_Rounding --
  690.    -----------------------
  691.  
  692.    function Unbiased_Rounding (X : T) return T is
  693.       Abs_X  : constant T := abs X;
  694.       Result : T;
  695.       Tail   : T;
  696.  
  697.    begin
  698.       Result := Truncation (Abs_X);
  699.       Tail   := Abs_X - Result;
  700.  
  701.       if Tail > 0.5  then
  702.          Result := Result + 1.0;
  703.  
  704.       elsif Tail = 0.5 then
  705.          Result := 2.0 * Truncation ((Result / 2.0) + 0.5);
  706.       end if;
  707.  
  708.       if X > 0.0 then
  709.          return Result;
  710.  
  711.       elsif X < 0.0 then
  712.          return -Result;
  713.  
  714.       --  For zero case, make sure sign of zero is preserved
  715.  
  716.       else
  717.          return X;
  718.       end if;
  719.  
  720.    end Unbiased_Rounding;
  721.  
  722.    -----------
  723.    -- Valid --
  724.    -----------
  725.  
  726.    function Valid (X : System.Address) return Boolean is
  727.  
  728.       IEEE_Emin : constant Integer := T'Machine_Emin - 1;
  729.       IEEE_Emax : constant Integer := T'Machine_Emax - 1;
  730.  
  731.       IEEE_Bias : constant Integer := -(IEEE_Emin - 1);
  732.  
  733.       subtype IEEE_Exponent_Range is
  734.         Integer range IEEE_Emin - 1 .. IEEE_Emax + 1;
  735.  
  736.       --  The implementation of this floating point attribute uses
  737.       --  a representation type Float_Rep that allows direct access to
  738.       --  the exponent and mantissa parts of a floating point number.
  739.  
  740.       --  The Float_Rep type is an array of Float_Word elements. This
  741.       --  representation is chosen to make it possible to size the
  742.       --  type based on a generic parameter.
  743.  
  744.       --  The following conditions must be met for all possible
  745.       --  instantiations of the attributes package:
  746.       --    - T'Size is an integral multiple of Float_Word'Size
  747.       --    - The exponent and sign are completely contained in a single
  748.       --      component of Float_Rep, named Most_Significant_Word (MSW).
  749.       --   -  The sign occupies the most significant bit of the MSW
  750.       --      and the exponent is in the following bits.
  751.       --      Unused bits (if any) are in the least significant part.
  752.  
  753.       type Float_Word is mod 2**32;
  754.       type Rep_Index is range 0 .. 7;
  755.  
  756.       Rep_Last : constant Rep_Index := (T'Size - 1) / Float_Word'Size;
  757.  
  758.       type Float_Rep is array (Rep_Index range 0 .. Rep_Last) of Float_Word;
  759.  
  760.       Most_Significant_Word : constant Rep_Index :=
  761.                                 Rep_Last * Standard'Default_Bit_Order;
  762.       --  Finding the location of the Exponent_Word is a bit tricky.
  763.       --  In general we assume Word_Order = Bit_Order.
  764.       --  This expression needs to be refined for VMS.
  765.  
  766.       Exponent_Factor : constant Float_Word :=
  767.                           2**(Float_Word'Size - 1) /
  768.                             Float_Word (IEEE_Emax - IEEE_Emin + 3) *
  769.                               Boolean'Pos (T'Size /= 96) +
  770.                                 Boolean'Pos (T'Size = 96);
  771.       --  Factor that the extracted exponent needs to be divided by
  772.       --  to be in range 0 .. IEEE_Emax - IEEE_Emin + 2.
  773.       --  Special kludge: Exponent_Factor is 0 for x86 double extended
  774.       --  as GCC adds 16 unused bits to the type.
  775.  
  776.       Exponent_Mask : constant Float_Word :=
  777.                         Float_Word (IEEE_Emax - IEEE_Emin + 2) *
  778.                           Exponent_Factor;
  779.       --  Value needed to mask out the exponent field.
  780.       --  This assumes that the range IEEE_Emin - 1 .. IEEE_Emax + 1
  781.       --  contains 2**N values, for some N in Natural.
  782.  
  783.       function To_Float is new Unchecked_Conversion (Float_Rep, T);
  784.  
  785.       R : Float_Rep;
  786.       pragma Import (Ada, R);
  787.       for R'Address use X;
  788.       --  R is a view of the input floating-point parameter. Note that we
  789.       --  must avoid copying the actual bits of this parameter in float
  790.       --  form (since it may be a signalling NaN.
  791.  
  792.       E  : constant IEEE_Exponent_Range :=
  793.              Integer ((R (Most_Significant_Word) and Exponent_Mask) /
  794.                                                         Exponent_Factor)
  795.                - IEEE_Bias;
  796.       --  Mask/Shift T to only get bits from the exponent
  797.       --  Then convert biased value to integer value.
  798.  
  799.       SR : Float_Rep;
  800.       --  Float_Rep representation of significant of X.all
  801.  
  802.    begin
  803.       if T'Denorm then
  804.          --  All denormalized numbers are valid, so only invalid numbers
  805.          --  are overflows and NaN's, both with exponent = Emax + 1.
  806.          return E /= IEEE_Emax + 1;
  807.  
  808.       end if;
  809.  
  810.       --  All denormalized numbers except 0.0 are invalid
  811.  
  812.       --  Set exponent of X to zero, so we end up with the significand, which
  813.       --  definitely is a valid number and can be converted back to a float.
  814.  
  815.       SR := R;
  816.       SR (Most_Significant_Word) :=
  817.            (SR (Most_Significant_Word)
  818.              and not Exponent_Mask) + Float_Word (IEEE_Bias) * Exponent_Factor;
  819.  
  820.       return (E in IEEE_Emin .. IEEE_Emax) or else
  821.          ((E = IEEE_Emin - 1) and then abs To_Float (SR) = 1.0);
  822.    end Valid;
  823.  
  824. end System.Fat_Gen;
  825.