home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / misc / old / ieee.ads < prev    next >
Encoding:
Text File  |  1996-04-29  |  54.5 KB  |  1,130 lines

  1. ------------------------------------------------------------------------------
  2. -- Ada interface for the IEEE Standard for Binary Floating-Point Arithmetic --
  3. ------------------------------------------------------------------------------
  4. --                          ANSI/IEEE Std 754-1985                          --
  5. ------------------------------------------------------------------------------
  6. --                            Robert B. K. Dewar                            --
  7. ------------------------------------------------------------------------------
  8. --                         Version 5  (05 Jan 1992)                         --
  9. ------------------------------------------------------------------------------
  10.  
  11. -- This specification is an initial proposal, suggested by Robert B. K. Dewar
  12. -- and modified in response to suggestions received. Comments or suggestions
  13. -- are welcome, and should be transmitted by EMAIL to dewar@nyu.edu, or by
  14. -- mail to Robert Dewar, 73 5th Ave, NYC NY 10003. The proposal currently has
  15. -- no official status, but the intention is that it eventually receive some
  16. -- kind of official status if a sufficiently broad positive consensus is
  17. -- achieved. Note: this document is formatted to print using 60 lines per
  18. -- page and the maximum line length is 79 characters.
  19.  
  20.  
  21.                 ----------------------
  22.                 -- Revision History --
  23.                 ----------------------
  24.  
  25. -- Changes made in Version 5 (05 Jan 1992)
  26.  
  27.    -- The facility for trapping errors and returning control has been made
  28.    -- optional, since it is an optional feature of P754, and is not always
  29.    -- implemented on IEEE conforming processors (due to difficulties in
  30.    -- accomodating precise traps in pipelined machines).
  31.  
  32.    -- MACHINE_OVERFLOWS may now be FALSE. If it is FALSE, then exceptions
  33.    -- are never generated on overflow (instead operation is always in the
  34.    -- mode where infinities are generated and recognized).
  35.  
  36.    -- The PRECISION_CONTROL pragma is replaced by FLOAT_EVALUATE, with three
  37.    -- possible settings, following the suggestions in the paper by Brian
  38.    -- Wichman on floating-point evaluation schemes (ref.)
  39.  
  40.    -- A package IEEE_P754_PRECISION_CONTROL is provided for dynamic control
  41.    -- over precision of results, as required in section 4.3 of P754. In
  42.    -- previous versions, the (incorrect) claim was made that the static
  43.    -- pragma controlling precision of intermediate results satisfies the
  44.    -- requirements of section 4.3 of the standard.
  45.  
  46.    -- A number of the standard functions have been removed, on the grounds
  47.    -- that they are provided in GEF. A full implementation of GEF is now
  48.    -- required as part of the basic compatibility requirements.
  49.  
  50. -- Changes made in Version 4 (10 Aug 1989)
  51.  
  52.    -- The name INTERMEDIATE_ROUNDING has been changed to PRECISION_CONTROL,
  53.    -- which more clearly reflects its function and also reflects the language
  54.    -- of the standard. The effect of this pragma now terminates at scope exit,
  55.    -- so that, for example, precision control pragmas within a procedure body
  56.    -- cannot affect statements outside the procedure.
  57.  
  58.    -- Some errors in the comparison result table in section 5.7 have been
  59.    -- corrected (the equal result column entries for LT,LE,LG were wrong).
  60.  
  61.    -- Documentation has been added to section 0.3 to make clear that the
  62.    -- compiler is forced to store and retrieve variable values using the
  63.    -- declared precision of the variable, and that optimizations which
  64.    -- retain higher precision values in registers and violate this rule
  65.    -- are not permitted.
  66.  
  67.    -- The text has been reedited to 78 characters/line format, for convenient
  68.    -- use with versions of EMACS, this format should be maintained in future.
  69.  
  70. -- Changes made in Version 3 (28 May 1989)
  71.  
  72.    -- Section 0.4 has been modified to discuss the issue of values passed
  73.    -- to and returned from functions. It is permissible for the computation
  74.    -- and passing of argument values, and the return of function results to
  75.    -- retain extended precision.
  76.  
  77.    -- Section 0.6 now requires accurate conversion of extended precision
  78.    -- literals. A requirement is also added that literals be converted with
  79.    -- a precision corresponding to the accuracy of the operation performed.
  80.    -- There is also a requirement that compile time operations give identical
  81.    -- results to the corresponding execution time operations.
  82.  
  83.    -- The definition of the type CLASS_TYPE is modified to be more consistent.
  84.  
  85.    -- RESULT_EXPONENT returns a float result, rather than INTEGER, to avoid
  86.    -- the specific use of the implementation dependent INTEGER type.
  87.  
  88. -- Changes made in Version 2 (22 May 1989)
  89.  
  90.    -- A section 0 has been added specifying certain minimum requirements for
  91.    -- an IEEE/P754 compatible Ada environment. This set of specifications
  92.    -- stands alone as a useful partial implementation of the standard, and
  93.    -- also means that the package itself can be simplified because it can
  94.    -- make greater use of the underlying implementation environment. Problems
  95.    -- with the use of predefined operations in generics are also handled by
  96.    -- this approach, since basic operations like addition are now handled
  97.    -- properly by the predefined operations, rather than operations defined
  98.    -- in the package.
  99.  
  100.    -- The package providing the additional P754 specific operations is now a
  101.    -- generic package, to be instantiated with the type or derived type for
  102.    -- which the operations are required.
  103.  
  104.    -- The terminology has been clarified by substituting the term fault
  105.    -- for exception, to avoid confusion with Ada exceptions.
  106.  
  107.    -- A number of these changes reflect Brian Wichmans comments on version 1.
  108.  
  109.  
  110.                  ---------------------------------------------
  111.          -- 0. IEEE/P754 Compatibility Requirements --
  112.          ---------------------------------------------
  113.  
  114. -- The specifications in this file provide an interface from Ada programs to
  115. -- all the required facilities of ANSI/IEEE standard 754-1985, hereinafter
  116. -- referred to simply as P754. A complete implementation of these packages
  117. -- results in an environment which fully conforms to the P754 requirements.
  118.  
  119. -- The packages assume that they are hosted in a P754 compatible Ada compiler.
  120. -- P754 compatibility involves a set of requirements on the implementation of
  121. -- standard Ada features that are compatible with, but go considerably beyond
  122. -- the requirements of the Ada standard. The Ada standard deliberately allows
  123. -- implementation freedoms in the area of floating point operations, in order
  124. -- to accommodate a wide variety of hardware floating point models. The P754
  125. -- compatibility rules specify exact semantics in areas where the Ada standard
  126. -- leaves such freedoms. The requirements are not in any way incompatible with
  127. -- the requirements of the standard, so that a P754 compatible Ada environment
  128. -- is in every way a conforming Ada requirement.
  129.  
  130. -- 0.1  Provision of Standard Types
  131.  
  132.    -- STANDARD must define the base types FLOAT and LONG_FLOAT, which are
  133.    -- respectively correct implementations of the basic single and double
  134.    -- formats defined in section 3.2 of P754. The following attribute values
  135.    -- apply to these types:
  136.  
  137.       --    Attribute              FLOAT              LONG_FLOAT
  138.       --     DIGITS                 6                  15
  139.       --     EMAX                   84                 204
  140.       --     EPSILON                9.53674E-07        8.88178E-16
  141.       --     FIRST                 -3.40282E+38       -1.79769E+308
  142.       --     LARGE                  1.93428E+25        2.57110E+61
  143.       --     LAST                   3.40282E+38        1.79769E+308
  144.       --     MANTISSA               21                 51
  145.       --     SAFE_EMAX              125                1021
  146.       --     SAFE_SMALL             1.17549E-38        2.2250EE-308
  147.       --     MACHINE_MANTISSA       24                 53
  148.       --     MACHINE_RADIX          2                  2
  149.       --     MACHINE_EMAX           128                1024
  150.       --     MACHINE_EMIN          -125               -1021
  151.       --     MACHINE_OVERFLOWS      (see below)        (see below)
  152.       --     MACHINE_ROUNDS         TRUE               TRUE
  153.       --     SIZE                   32                 64
  154.  
  155.    -- Note: MACHINE_OVERFLOWS is set to TRUE if floating-point overflows
  156.    -- result in raising an exception. If MACHINE_OVERFLOWS is false, then
  157.    -- operation is always in the generate-infinities mode, and no exceptions
  158.    -- occur on overflow. This mode of operation is provided to allow for
  159.    -- implementation on pipelined machines that do not accomodate traps.
  160.  
  161.    -- In addition, a type EXTENDED_FLOAT must be provided, representing the
  162.    -- highest precision available. If the underlying P754 implementation
  163.    -- provides double extended format, as described in section 3.3, then
  164.    -- EXTENDED_FLOAT must correspond to this type. If the underlying P754
  165.    -- implementation does NOT provide this format, then EXTENDED_FLOAT may
  166.    -- be equivalent in accuracy and format to LONG_FLOAT. The attributes
  167.    -- for EXTENDED_FLOAT indicate the actual accuracy and other
  168.    -- characteristics of this type.
  169.  
  170. -- 0.2  Predefined Operations
  171.  
  172.    -- The predefined operations (+,-,*,/) applied to these types must conform
  173.    -- to the definitions in P754, with the default behavior (i.e. the behavior
  174.    -- in the absence of the use of the IEEE_P754_ROUNDING_CONTROL package)
  175.    -- being "round to nearest" (see section 4.1). Overflow, divide by zero
  176.    -- and invalid operation faults raise the exception CONSTRAINT_ERROR.
  177.    -- Underflow and inexact faults are disabled (see section 7 of P754 for
  178.    -- further details).
  179.  
  180.    -- Conversion operations between floating point types, and between integer
  181.    -- types and floating point types must be performed in accordance with the
  182.    -- P754 rules, including round to nearest semantics.
  183.  
  184. -- 0.3  Accuracy of Predefined Operations
  185.  
  186.    -- In the absence of the use of the pragmas described in section 0.4, it is
  187.    -- permissible for the calculation of intermediate results to be done with
  188.    -- greater precision than that implied by the result type (for example by
  189.    -- using EXTENDED_FLOAT for intermediate results). However, an explicit
  190.    -- assignment operation requires that the result be rounded as appropriate
  191.    -- to the precision type of the assignment target. Subsequent references to
  192.    -- the variable must yield a copy of the stored value. It is specifically
  193.    -- NOT permissible for an optimizer to use a higher precision value held in
  194.    -- a register. For example, suppose we have the sequence
  195.  
  196.       -- A := B * C * D;
  197.       -- Q := A + C;
  198.  
  199.    -- where all variables are of type FLOAT in an environment where all
  200.    -- intermediate results are computed in double extended. It is not
  201.    -- permissble to compute B*C*D in double extended, store a rounded single
  202.    -- value in A, and then use the double extended value still sitting in a
  203.    -- register for the subsequent addition to C.
  204.  
  205.    -- In deciding to adopt this approach, it is understood that in hardware
  206.    -- environments where intermediate results are naturally computed in
  207.    -- extended precision, this requirement may cause "inefficient" code
  208.    -- sequences. However, it seems fundamental to the approach that floating
  209.    -- point arithmetic should behave in a consistent and predictable manner.
  210.    -- Allowing the compiler to do optimizations which affect the results in
  211.    -- a non-predictable manner is unacceptable as the default behavior.
  212.  
  213.    -- For function and procedure calls, the copy in of the argument value is
  214.    -- not considered to be an explicit assignment. If the argument is a
  215.    -- floating point expression which is computed using extended precision,
  216.    -- then this extended precision may be preserved in passing values to
  217.    -- the procedure. Similarly, functions may return results using a return
  218.    -- statement whose expression is computed using extended precision and
  219.    -- this extended precision value may be used at the point of call.
  220.  
  221.    -- A requirement for this default mode (which corresponds to the setting
  222.    -- pragma FLOAT_EVALUATE (PREDICTABLE)), it is mandatory that complete
  223.    -- documentation be provided indicating exactly when extended precision
  224.    -- is used, so that the behavior is completely predictable from the source.
  225.  
  226. -- 0.4  Control over Intermediate Result Precision
  227.  
  228.    -- The following pragmas must be provided to control the precision of
  229.    -- intermediate results, as required by the standard.
  230.       -- pragma FLOAT_EVALUATE (PORTABLE);
  231.  
  232.     -- In the PORTABLE mode, all intermediate computations are required
  233.     -- to yield results with the precision and range of their returned
  234.     -- types. This means that if EXTENDED_FLOAT is not used, and overflow
  235.     -- is avoided, then precisely identical results are obtained on all
  236.     -- implementations. For example, in the statement:
  237.  
  238.        -- A := (B + C) + D;
  239.  
  240.     -- where all variables are of type FLOAT, the intermediate result B + C
  241.     -- must be rounded to FLOAT precision, even in an implementation which
  242.     -- more efficiently uses extended precision for all operations. In the
  243.     -- case of functions, arguments and returned results must also be
  244.     -- converted to the appropriate type.
  245.  
  246.       -- pragma FLOAT_EVALUATE (PREDICTABLE);
  247.  
  248.     -- In PREDICTABLE mode, explicit assignments must round to the target
  249.     -- type, but intermediate results in expressions can be computed using
  250.     -- extended precision, providing that such usage is documented in com-
  251.     -- plete detail, so that the behavior is completely predictable.  Note
  252.     -- that in this mode, passing arguments to subprograms, or returning
  253.     -- results from functions is not considered to be an assignment, so
  254.     -- that extended precision can also be used in these contexts.
  255.  
  256.       -- pragma FLOAT_EVALUATE (OPTIMIZE);
  257.  
  258.     -- In OPTIMIZE mode, extended precision may be used anywhere, including
  259.     -- the case of assignments to variables of type FLOAT or LONG_FLOAT.
  260.     -- The general principle is that out of range results are permissible
  261.     -- providing the "correct" results are generated.
  262.  
  263.     -- In this mode, it is also not required that the places that use is
  264.     -- made of extended precision need not be exactly documented. For
  265.     -- example, if it is the case that the use of extended precision
  266.     -- depends on unpredictable details of the register allocation
  267.     -- algorithm, then this behavior is acceptable in the OPTIMIZE
  268.     -- mode, but not in any of the other modes.
  269.     -- result types, as described in section 0.3.
  270.  
  271.    -- The default mode in the absence of either form of this pragma is
  272.    -- FLOAT_EVALUATE(PREDICTABLE), so that intermediate results may be
  273.    -- calculated using extra precision, but assignments must be to the
  274.    -- proper type, and all use of extended precision must be documented.
  275.  
  276.    -- The appearence of the FLOAT_EVALUATE pragma affects all subsequent
  277.    -- computations which textually follow the occurence of the pragma and
  278.    -- remains in effect until another FLOAT_EVALUATE pragma appears or
  279.    -- until the end of the current scope is reached. At the end of a scope,
  280.    -- the setting in effect on entry to the scope is restored. This means
  281.    -- for example that precision control pragmas within a procedure body
  282.    -- can never affect statements outside the procedure. The pragma may
  283.    -- appear in the context clause for a compilation unit, in which case
  284.    -- it determines the default behaviour for the entire compilation unit.
  285.  
  286.    -- Note: an implementation must implement FLOAT_EVALUATE (PORTABLE) mode.
  287.    -- The implementation of the other modes is optional, since it is permitted
  288.    -- to ignore the additional freedoms of the additional modes, and treat
  289.    -- all three modes in identical manner (i.e. identical to PORTABLE mode).
  290. -- 0.5  Comparison Operations
  291.  
  292.    -- The predefined comparison operations correspond to the semantics of the
  293.    -- first six predicates listed in Table 4 of P754. In the absence of the
  294.    -- use of the IEEE_P754_FAULT_CONTROL package, infinite and NaN values
  295.    -- will not occur, so the semantics of these operations are consistent with
  296.    -- normal Ada semantics.
  297.  
  298. -- 0.6  Accuracy of Conversion of Literals
  299.  
  300.    -- Floating point literals for basic format values must be accurately
  301.    -- converted using round to nearest throughout the entire range. This
  302.    -- is a more stringent requirement than that specified by P754, see
  303.    -- section 5.6.1 of this specification. The accuracy of conversion of
  304.    -- extended format literals is implementation dependent.
  305.  
  306.    -- When operations are carried out using a precision greater than that
  307.    -- implied by the Ada type, literals must be converted in accordance with
  308.    -- the actual precision used. For example consider the statement:
  309.  
  310.       -- A := B * 3.145 / 4.189
  311.  
  312.    -- where A and B are both of type FLOAT. If the implementation carries out
  313.    -- the multiplication and division in extended precision (possible only if
  314.    -- PRECISION_CONTROL mode is OFF), then the two literals must be converted
  315.    -- with the accuracy of EXTENDED_FLOAT, even though they are of type FLOAT
  316.    -- by Ada rules. Named numbers are treated similarly.
  317.  
  318.    -- Note: any arithmetic carried out at compile time (e.g. folding of
  319.    -- constant expressions) must give absolutely identical results to the
  320.    -- corresponding operations performed at execution time.
  321.  
  322. -- 0.7  Input and Output of Floating Point Values
  323.  
  324.    -- The FLOAT_IO package must input basic format values (FLOAT, LONG_FLOAT)
  325.    -- in a manner that conforms to the error bounds described in section 5.6
  326.    -- of P754. Where possible, exact rounding should be provided. The
  327.    -- accuracy of input conversion for EXTENDED_FLOAT values is
  328.    -- implementation dependent.
  329.  
  330. -- 0.8  Implementation of GEF
  331.  
  332.    -- A conforming implementation must provide a completely implementation of
  333.    -- the GENERIC_ELEMENTARY_FUNCTIONS package (defined in ISO standard .....)
  334.  
  335. -- 0.9  Use of a P754 Compatible Ada Compiler
  336.  
  337.    -- The provision of P754 compatibility as defined in this section is useful
  338.    -- even if an Ada implementation does not fully support the packages
  339.    -- defined in the remainder of this specification. For many purposes, this
  340.    -- basic level of support will prove adequate for IEEE/P754 applications.
  341.  
  342.    -- Implementations for IEEE/P754 compatible targets are encouraged to
  343.    -- conform to these compatibility requirements to the greatest extent
  344.    -- possible, even if the additionally specified packages are not provided.
  345.  
  346.  
  347.                                 --------------
  348.                 -- 1. Scope --
  349.                 --------------
  350.  
  351. -- Note: The section numbers in the remainder of this file match those
  352. -- used in the ANSI/IEEE Std 754-1985 document, except for a few cases of
  353. -- additional sections (e.g. 5.6.1) which are included for clarification.
  354.  
  355. -- 1.1  Implementation Objectives
  356.  
  357.    -- ANSI/IEEE Std 754-1985 specifies a hardware and software standard for
  358.    -- floating point arithmetic. The compatibility requirements described in
  359.    -- the previous section provide the basic P754 compatible environment, but
  360.    -- a full implementation of the standard requires additional facilities.
  361.    -- These facilities are provided with a set of four packages:
  362.  
  363.       -- IEEE_P754_ROUNDING_CONTROL   Control over rounding mode
  364.       -- IEEE_P754_OPERATIONS         Additional required operations
  365.       -- IEEE_P754_IO                 Input-output and string conversions
  366.       -- IEEE_P754_FAULT_CONTROL      Control over exceptions and traps
  367.  
  368. -- 1.2  Inclusions
  369.  
  370.    -- All aspects of the standard, as listed in this section, are included
  371.    -- in these packages and specified in a standard manner.
  372.  
  373. -- 1.3  Exclusions
  374.  
  375.    -- P754 does not specify the formats of decimal strings and integers, or
  376.    -- the interpretation of the sign and significand fields of NaN's, or
  377.    -- binary to decimal conversions to and from extended formats.
  378.  
  379.    -- An implementation of this interface must include the specification of
  380.    -- the following implementation dependent characteristics:
  381.  
  382.       -- The accuracy and representation of the type EXTENDED_FLOAT,
  383.  
  384.       -- The accuracy and representation of any additional FLOAT types,
  385.  
  386.       -- The accuracy of conversion of input values for all FLOAT types,
  387.  
  388.       -- The representation of all NaN values supported, including both the
  389.       -- binary representation, and the representation as string values for
  390.       -- input-output (using IEEE_P754_IO) and the IMAGE and VALUE functions,
  391.  
  392.       -- The manner in which NaN values are produced and recognized,
  393.  
  394.       -- The manner in which intermediate results are computed with extended
  395.       -- precision if PRECISION_CONTROL mode is on,
  396.  
  397.       -- Whether or not the functions COPYSIGN, FINITE, ISNAN, and the
  398.       -- predefined unary negation operation signal an invalid operation fault
  399.       -- if a signalling NaN is used as an operand value,
  400.  
  401.       -- The order of bits used in the representation of the float formats.
  402.  
  403.  
  404.                              --------------------
  405.                  -- 2. Definitions --
  406.                  --------------------
  407.  
  408.    -- The terminology in this specification generally matches that of section
  409.    -- 2 of P754. The one point at which a deliberate departure from the
  410.    -- defined terminology occurs is that the word "fault" is used instead of
  411.    -- "exception" to avoid an otherwise inevitable confusion with the Ada use
  412.    -- of the term exception.
  413.  
  414.  
  415.                    ----------------
  416.                    -- 3. Formats --
  417.                    ----------------
  418.  
  419. -- 3.1  Sets of Values
  420.  
  421.    -- The sets of values provided shall correspond to those defined in section
  422.    -- 3.1 in all respects. The generation of infinities and NaN's is possible
  423.    -- if the facilities of IEEE_P754_FAULT_CONTROL are used to suppress
  424.    -- raising of exceptions or traps on overflow or other faults.
  425.  
  426. -- 3.2  Basic Formats
  427.  
  428.    -- The required P754 basic computational formats are supplied in package
  429.    -- STANDARD as FLOAT and LONG_FLOAT (see section 0.1).
  430.  
  431. -- 3.3  Extended Formats
  432.  
  433.    -- If double extended format is provided by the underlying implementation,
  434.    -- then the type EXTENDED_FLOAT in STANDARD must correspond exactly to the
  435.    -- implementation of this format, and P754 compatible operations are to be
  436.    -- made available for this type. In some implementations, this extended
  437.    -- precision format will automatically be used for all intermediate
  438.    -- computations, but this behavior is not required (and can be suppressed,
  439.    -- see section 0.4).
  440.  
  441.    -- Programs using EXTENDED_FLOAT are potentially implementation dependent.
  442.    -- One portable case involves programs where the extra precision is either
  443.    -- harmless or beneficial. Programs which depend on the precision of the
  444.    -- extended float type must either be parametrized with respect to the
  445.    -- values of the Ada attributes revealing the precision, or must be
  446.    -- understood to be implementation dependent.
  447.  
  448.    -- Note: P754 allows for the implementation of an additional representation
  449.    -- "single extended format", which presumably has an intermediate precision
  450.    -- between FLOAT and DOUBLE_FLOAT. Very few implementations of P754 include
  451.    -- this format. For an implementation which does include this format, the
  452.    -- recommended name of the additional type is SINGLE_EXTENDED_FLOAT.
  453.  
  454. -- 3.4  Combinations of Formats
  455.  
  456.    -- P754 defines four formats (single, single extended, double, and double
  457.    -- extended). A minimum implementation requires that single be supported,
  458.    -- and recommends that the extended format for the widest basic format be
  459.    -- supported.
  460.  
  461.    -- The Ada interface requires that both single and double formats be
  462.    -- supported, but does not require the implementation of any extended
  463.    -- formats, although it allows for the possibility of double extended
  464.    -- format. This corresponds to typical implementations of floating point
  465.    -- hardware which support either single and double only or single, double
  466.    -- and double extended.
  467.  
  468.    -- A hardware implementation supporting only single format would meet the
  469.    -- requirements, but not the recommendations, of P754. An implementation
  470.    -- providing single and single extended formats only would meet both the
  471.    -- requirements and recommendations of P754, but would NOT satisfy the
  472.    -- requirements of the Ada interface, which is thus more stringent than
  473.    -- P754. The reason for this added stringency is that the Ada interface
  474.    -- is intended to promote portability of code including both basic formats.
  475.  
  476.    -- The following is a summary of possible combinations of formats:
  477.  
  478.       --   Formats       Meets P754      Meets P754     Meets Ada Interface
  479.       --  Supported     Requirements   Recommendations     Requirements
  480.       --   S                YES             NO                 NO
  481.       --   S,SX,D           YES             NO                 NO
  482.       --   S,SX             YES             YES                NO
  483.       --   S,D              YES             NO                 YES
  484.       --   S,D,DX           YES             YES                YES
  485.       --   S,SX,D,DX        YES             YES                YES
  486.  
  487.  
  488.                    -----------------
  489.                    -- 4. Rounding --
  490.                    -----------------
  491.  
  492. -- 4.1  Round to Nearest
  493.  
  494.    -- The default rounding mode is round to nearest. This is the mode which
  495.    -- applies to all computations if the IEEE_P754_ROUNDING_CONTROL package
  496.    -- is not used. In the case where a result is exactly half way between
  497.    -- two representable values, the even value is always chosen.
  498.  
  499. -- 4.2  Directed Roundings
  500.  
  501.    -- The package IEEE_P754_ROUNDING_CONTROL can be used to specify the use of
  502.    -- directed rounding modes for all computations. The rounding mode applies
  503.    -- to all operations executed by a single task. The rounding mode may be
  504.    -- specified dynamically and changed as execution proceeds. It applies on
  505.    -- a task by task basis, so that different tasks may have different
  506.    -- rounding modes, but all operations on all floating point types in a
  507.    -- single task are controlled by a single setting of the rounding mode.
  508.  
  509.    package IEEE_P754_ROUNDING_CONTROL is
  510.  
  511.       type ROUNDING_MODE is (
  512.      ROUND_TO_NEAREST,         -- Round to nearest
  513.      ROUND_UP,                 -- Round up toward plus infinity
  514.      ROUND_DOWN,               -- Round down towards minus infinity
  515.      TRUNCATE);                -- Round towards zero
  516.  
  517.       -- Subprograms to obtain and set the current rounding mode. The mode is
  518.       -- valid on a task by task basis, i.e. different tasks can have different
  519.       -- rounding modes. The default initial rounding mode for the environment
  520.       -- task is ROUND_TO_NEAREST. The default initial rounding mode for any
  521.       -- other task is the rounding mode of its parent task. The rounding mode
  522.       -- controls the behavior of all subsequently executed operations.
  523.  
  524.      function CURRENT_ROUNDING_MODE return ROUNDING_MODE;
  525.      -- CURRENT_ROUNDING_MODE yields the current rounding mode for the
  526.      -- currently active task.
  527.  
  528.      procedure SET_ROUNDING_MODE (MODE : ROUNDING_MODE);
  529.      -- SET_ROUNDING_MODE sets the current rounding mode for all
  530.      -- subsequent operations performed by the currently active task.
  531.  
  532.    end IEEE_P754_ROUNDING_CONTROL;
  533.  
  534.    -- Note: The Ada attribute MACHINE_ROUNDS is a static value, and reflects
  535.    -- the default behavior. It's value is thus unaffected by calls to the
  536.    -- SET procedure, and may for example be TRUE even after SET_ROUNDING_MODE
  537.    -- has been used to deliberately set some other rounding mode.
  538.  
  539. -- 4.2.1  Scope of Rounding Effect
  540.  
  541.    -- The rounding mode affects all predefined operations, including the
  542.    -- conversions between floating point types, and between integer types
  543.    -- and floating point types. The operations introduced by the package
  544.    -- IEEE_P754_OPERATIONS are also affected, as is the input rounding for
  545.    -- the IEEE_P754_IO operations. The conversion of literals appearing in
  546.    -- the source text is NOT affected (see section 5.6.1).
  547.  
  548. -- 4.3  Rounding Precision
  549.  
  550.    -- The package IEEE_P754_PRECISION_CONTROL can be used to override the
  551.    -- normal rounding of results for all computations. The mode set applies
  552.    -- to all operations executed by a single task. The precision mode may be
  553.    -- specified dynamically and changed as execution proceeds. It applies on
  554.    -- a task by task basis, so that different tasks may have different
  555.    -- precision modes, but all operations on all floating point types in a
  556.    -- single task are controlled by a single setting of the rounding mode.
  557.  
  558.    package IEEE_P754_PRECISION_CONTROL is
  559.  
  560.       type PRECISION_MODE is (
  561.      NORMAL,                   -- Precision is determined by result
  562.      ROUND_DOUBLE,             -- All results rounded to double
  563.      ROUND_SINGLE);            -- All results rounded to single
  564.  
  565.       -- Subprograms to obtain and set the current precision mode.  The mode
  566.       -- is valid on a task by task basis, i.e. different tasks can have
  567.       -- different precision modes. The default initial precision mode for the
  568.       -- environment task is ROUND_TO_NEAREST. The default initial precision
  569.       -- mode for any other task is the precision mode of its parent task.
  570.       -- The precision mode controls the behavior of all subsequently executed
  571.       -- operations.
  572.  
  573.      function CURRENT_PRECISION_MODE return PRECISION_MODE;
  574.      -- CURRENT_PRECISION_MODE yields the current rounding mode for the
  575.      -- currently active task.
  576.  
  577.      procedure SET_PRECISION_MODE (MODE : PRECISION_MODE);
  578.      -- SET_PRECISION_MODE sets the current precision mode for all
  579.      -- subsequent operations performed by the currently active task.
  580.  
  581.    end IEEE_P754_PRECISION_CONTROL;
  582.  
  583. -- 4.3.1  Scope of Precision Effect
  584.  
  585.    -- The precision mode affects all predefined operations, including the
  586.    -- conversions between floating point types, and between integer types
  587.    -- and floating point types. The operations introduced by the package
  588.    -- IEEE_P754_OPERATIONS are also affected, as is the input precision for
  589.    -- the IEEE_P754_IO operations. The conversion of literals appearing in
  590.    -- the source text is NOT affected (see section 5.6.1).
  591.  
  592.  
  593.                   -------------------
  594.                   -- 5. Operations --
  595.                   -------------------
  596.  
  597.    -- The basic arithmetic operations, and a subset of the required comparison
  598.    -- operations are available as predefined operations (see sections 0.2 and
  599.    -- 0.5) The generic package IEEE_P754_OPERATIONS provides the additional
  600.    -- operations required by section 5 and recommended by the appendix of P754
  601.    -- for the type designated in the generic instantiation.
  602.  
  603.    generic
  604.       type FLOAT_TYPE is digits <>;
  605.    package IEEE_P754_OPERATIONS is
  606.  
  607. -- 5.1  Arithmetic
  608.  
  609.    -- The add, subtract, multiply and divide operations correspond to the
  610.    -- predefined operations +, -, * and / in STANDARD, which are defined on
  611.    -- all FLOAT types. The remainder operation is provided by the appropriate
  612.    -- GEF function.
  613.  
  614.    -- Note: P754 requires that operations be provided on operands of differing
  615.    -- formats. The Ada interface however does NOT provide such "mixed mode"
  616.    -- operations because such operations are not in the style of Ada, and in
  617.    -- particular do not interact well with the provision of derived types. The
  618.    -- effect of mixed mode operations can be achieved using appropriate
  619.    -- conversion operations (and indeed most hardware implementations of P754
  620.    -- require the use of such conversions to perform mixed mode computations).
  621.  
  622. -- 5.2  Square Root
  623.  
  624.  
  625.    -- The square root function is provided by the appropriate function in GEF.
  626.  
  627. -- 5.3  Floating-Point Format Conversions
  628.  
  629.    -- Floating point format conversions are achieved using the predefined
  630.    -- conversion operations as described in section 0.2.
  631.  
  632. -- 5.4  Conversion Between Floating-Point and Integer Formats
  633.  
  634.    -- Conversions between floating point and integer formats are achieved
  635.    -- using the predefined conversion operations as described in section 0.2.
  636.  
  637. -- 5.5  Round Floating-Point Number to Integer Value
  638.  
  639.    -- The round operation rounds a floating point value to an integral valued
  640.    -- floating point value.
  641.  
  642.       function RNDINT (X : FLOAT_TYPE) return FLOAT_TYPE;
  643.       -- The RNDINT function returns its input rounded to an integral value
  644.       -- using the current rounding mode. The result is in the same floating
  645.       -- point format as the input (NOT in INTEGER format).
  646.  
  647. -- 5.6  Binary <--> Decimal Conversion
  648.  
  649.    -- Conversion from string values to and from decimal values may be achieved
  650.    -- using an instantiation of the generic package IEEE_P754_IO whose Ada
  651.    -- specification is identical to FLOAT_IO (see Ada RM section 14.3.8).
  652.    -- The semantics of this package are identical to those of the FLOAT_IO
  653.    -- package except for the following (which would not be permitted in a
  654.    -- correct implementation of FLOAT_IO).
  655.  
  656.       -- On output negative zero is output as -0.0 (the sign is significant)
  657.       -- On input the string "INF" with an optional preceding sign is allowed
  658.       -- On input implementation dependent strings for NaN's may be recognized
  659.  
  660.    -- In addition, the following specific behavior (permitted but not required
  661.    -- by the Ada RM for FLOAT_IO) is required:
  662.  
  663.       -- On input rounding must obey the current rounding mode (see 4.1,4.2)
  664.       -- On output infinite values are output as " INF" or "-INF"
  665.       -- On output NaN's are represented by implementation dependent strings
  666.  
  667.    -- Note: it seems unfortunate that the separate package should be required
  668.    -- simply to satisfy the legalistic requirements that the special behaviour
  669.    -- required in marginal cases is not compatible with the RM. This matter
  670.    -- should be referred to the ARG to determine whether or not the deviations
  671.    -- suggested here could be regarded as acceptable for the standard package.
  672.  
  673.    -- In addition, IEEE_P754_OPERATIONS itself contains the following two
  674.    -- functions for performing binary to decimal conversions:
  675.  
  676.       function VALUE (X : STRING) return FLOAT_TYPE;
  677.       -- The VALUE function converts the string in the same manner as the GET
  678.       -- operation defined in IEEE_P754_IO, including the recognition of -0.0,
  679.       -- INF and implementation defined strings representing NaN values.
  680.  
  681.       function IMAGE (X : FLOAT_TYPE) return STRING;
  682.       -- The IMAGE function converts its argument and returns a string using
  683.       -- a format identical to that generated by the PUT procedure in package
  684.       -- IEEE_P754_IO using the following parameters:
  685.  
  686.      -- FORE  =  2
  687.      -- AFT   =  FLOAT_TYPE'DIGITS - 1
  688.      -- EXP   =  3
  689.  
  690.  
  691. -- 5.6.1  Conversion of Literals
  692.  
  693.    -- The IEEE standard recommends that literals be converted at compile time
  694.    -- in a manner identical to the result of these conversions at run time.
  695.    -- This is a potentially very expensive proposition, since the rounding
  696.    -- mode is not known till run time, and this would prevent compile time
  697.    -- conversion of real literals. The spirit of this recommendation is
  698.    -- considered to be best met by always using round to nearest for
  699.    -- conversion of literals.
  700.  
  701.    -- In cases where literal values are required which must be rounded
  702.    -- according to the currently active rounding mode, or where infinite or
  703.    -- NaN are required as literals, the VALUE function should be used as in
  704.    -- the following examples:
  705.  
  706.       -- INFINITY   : constant FLOAT_TYPE := VALUE ("+INF");
  707.       -- ROUNDED_PI : constant FLOAT_TYPE :=
  708.       --                 VALUE("3.14159_26535_89793_23846");
  709.  
  710. -- 5.7 Comparison
  711.  
  712.    -- P754 specifies two methods for comparisons. The first yields a result
  713.    -- indicating which of four possible relations holds:
  714.  
  715.       type RELATION_TYPE is (GREATER_THAN, LESS_THAN, EQUAL, UNORDERED);
  716.  
  717.       function COMPARE (X, Y : FLOAT_TYPE) return RELATION_TYPE;
  718.       -- COMPARE indicates the ordering relation which holds between its two
  719.       -- operands. The result is UNORDERED if either operand is a NaN. This
  720.       -- operation never signals a fault.
  721.  
  722.    -- The other approach involves the provision of a set of predicate
  723.    -- functions. Note that the first six functions are equivalent to the
  724.    -- predefined comparison operations (= /= > >= < <=). The table at the
  725.    -- right side (which is excerpted from Table 4 in P754) shows the result
  726.    -- for each of the four relations which can hold between the operands
  727.    -- (U = Unordered).
  728.  
  729.                             --    Result if
  730.                             --   >   <   =   U
  731.       function EQ  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   F   F   T   F
  732.       function NE  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   T   T   F   T
  733.       function GT  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   T   F   F   F
  734.       function GE  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   T   F   T   F
  735.       function LT  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   F   T   F   F
  736.       function LE  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   F   T   T   F
  737.       function LG  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   T   T   F   F
  738.       function LEG (X, Y : FLOAT_TYPE) return BOOLEAN;  --   T   T   T   F
  739.       function UG  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   T   F   F   T
  740.       function UGE (X, Y : FLOAT_TYPE) return BOOLEAN;  --   T   F   T   T
  741.       function UL  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   F   T   F   T
  742.       function ULE (X, Y : FLOAT_TYPE) return BOOLEAN;  --   F   T   T   T
  743.       function UE  (X, Y : FLOAT_TYPE) return BOOLEAN;  --   F   F   T   T
  744.  
  745.       -- Note: the "unordered" function (?) listed in Table 4 of P754 is
  746.       -- provided as the UNORDERED function in the following section. There
  747.       -- seems to be some confusion as to whether this function is required,
  748.       -- as implied by its presence in Table 4, or optional, as implied by
  749.       -- its presence in the Appendix. In any case the Ada interface requires
  750.       -- that the functions in the Appendix be provided, so the confusion does
  751.       -- not affect this specification.
  752.  
  753. -- 5.7.1  Recommended Functions and Predicates (from P754 Appendix)
  754.  
  755.    -- P754 lists a number of recommended functions in the Appendix which are
  756.    -- not required. However, a full implementation of the Ada packages
  757.    -- requires that these functions be provided.
  758.  
  759.       function COPYSIGN (X, Y : FLOAT_TYPE) return FLOAT_TYPE;
  760.       -- COPYSIGN returns X with the sign of Y. This operation is not required
  761.       -- to signal the invalid operation fault for signaling NaN's. An
  762.       -- implementation must specify whether or not the fault is signaled.
  763.  
  764.       function SCALB (X : FLOAT_TYPE; Y : INTEGER) return FLOAT_TYPE;
  765.       -- SCALB returns X * 2**Y without computing 2**Y.
  766.  
  767.       function LOGB (X : FLOAT_TYPE) return FLOAT_TYPE;
  768.       -- LOGB returns the unbiased exponent of X, a signed integer in the
  769.       -- same format as X. See P754 for handling of special cases.
  770.  
  771.       function NEXTAFTER (X, Y : FLOAT_TYPE) return FLOAT_TYPE;
  772.       -- NEXTAFTER returns the next representable neighbor of X in the
  773.       -- direction toward Y. See P754 for handling of special cases.
  774.  
  775.       function FINITE (X : FLOAT_TYPE) return BOOLEAN;
  776.       -- FINITE returns TRUE if its argument is neither a NaN nor infinite.
  777.       -- This operation is not required to signal the invalid operation fault
  778.       -- for signaling NaN's.  An implementation must specify whether or not
  779.       -- the fault is signaled.
  780.  
  781.       function ISNAN (X : FLOAT_TYPE) return BOOLEAN;
  782.       -- ISNAN returns TRUE if its argument is a NaN. This operation is not
  783.       -- required to signal the invalid operation fault for signaling NaN's.
  784.       -- An implementation must specify whether or not the fault is signaled.
  785.  
  786.       function UNORDERED (X, Y : FLOAT_TYPE) return BOOLEAN;
  787.       -- Returns TRUE if either X or Y is a NaN. No fault is ever signaled.
  788.  
  789.       type CLASS_TYPE is (
  790.      SIGNALLING_NAN,
  791.      QUIET_NAN,
  792.      NEGATIVE_ZERO,
  793.      NEGATIVE_DENORMALIZED,
  794.      NEGATIVE_NORMALIZED_NONZERO,
  795.      NEGATIVE_INFINITY,
  796.      POSITIVE_ZERO,
  797.      POSITIVE_DENORMALIZED,
  798.      POSITIVE_NORMALIZED_NONZERO,
  799.      POSITIVE_INFINITY);
  800.  
  801.       subtype POSITIVE is CLASS_TYPE range POSITIVE_ZERO .. POSITIVE_INFINITY;
  802.       subtype NEGATIVE is CLASS_TYPE range POSITIVE_ZERO .. POSITIVE_INFINITY;
  803.  
  804.       function CLASS (X : FLOAT_TYPE) return CLASS_TYPE;
  805.       -- CLASS returns the class of its argument. No fault is ever signaled.
  806.  
  807.    -- Note: the negation operation -X is provided in STANDARD. This operation
  808.    -- is not required to signal the invalid operation fault if the operand is
  809.    -- a signalling NaN. An implementation must specify whether the fault is
  810.    -- signaled.
  811.  
  812.    -- Note: some but not all of these functions are provided by GPF, and/or
  813.    -- by the proposed attributes in Ada 9X, so some further discussion is
  814.    -- appropriate.
  815.  
  816.    end IEEE_P754_OPERATIONS;
  817.  
  818.  
  819.             ---------------------------------------
  820.             -- 6. Infinity, NaNs and Signed Zero --
  821.             ---------------------------------------
  822.  
  823.    -- 6.1  Infinity Arithmetic
  824.  
  825.      -- Signed infinities and signed zeroes shall be fully supported by any
  826.      -- implementation of this package. Infinite values can only be generated
  827.      -- if traps and exceptions for overflow faults are suppressed using the
  828.      -- the facilities of the IEEE_P754_FAULT_CONTROL package.
  829.  
  830.    -- 6.2  Operations with NaNs
  831.  
  832.      -- NaN's shall be implemented as defined in section 6.2 of the standard,
  833.      -- and any implementation of this package must describe the NaN formats
  834.      -- supported, their representations, and the manner in which they are
  835.      -- produced and recognized.
  836.  
  837.    -- 6.3 The Sign Bit
  838.  
  839.      -- The implementation must recognize and distinguish negative zeroes
  840.      -- from positive zeroes in cases where such distinction is required.
  841.      -- Notably, the IEEE_P754_IO input routines recognize minus zero, and
  842.      -- preserve the sign on outputting minus zero. Both the predefined
  843.      -- operations and the operations introduced in IEEE_P754_OPERATIONS
  844.      -- treat signed zero values in a manner consistent with P754. For
  845.      -- example the division of positive infinity by zero returns a value
  846.      -- with the same sign as the zero value.
  847.  
  848.  
  849.            -----------------------------------------
  850.            -- 7. Fault Handling (P754 Exceptions) --
  851.            -----------------------------------------
  852.  
  853.    -- Note: as previously mentioned in section 2, this specification uses
  854.    -- the term "fault" rather than "exception", to avoid confusion with the
  855.    -- Ada use of the term exception. Thus any use of the word exception (other
  856.    -- than in the previous sentence and the corresponding sentence in section
  857.    -- 2) refers to Ada exceptions and NOT to P754 exceptions.
  858.  
  859.    -- The required level of control over fault handling is provided by the
  860.    -- by the package IEEE_P754_FAULT_CONTROL. This package makes use of
  861.    -- a user provided trap routine, IEEE_P754_USER_TRAP_ROUTINE. A program
  862.    -- which incorporates the IEEE_P754_FAULT_CONTROL package must provide
  863.    -- a body for this parameterless library procedure. If trap processing is
  864.    -- not required (FAULT_ACTION never set to TRAP), then a dummy body can be
  865.    -- used to satisfy this requirement.
  866.  
  867.    procedure IEEE_P754_USER_TRAP_ROUTINE;
  868.  
  869.    with IEEE_P754_USER_TRAP_ROUTINE;
  870.    package IEEE_P754_FAULT_CONTROL is
  871.  
  872.    -- The following functions read, set and reset the values of the status
  873.    -- flags for faults defined in section 7. These status flags are provided
  874.    -- on a task by task basis, so that each task has its own separate set of
  875.    -- of status flags. The flags are initially FALSE when a task is created.
  876.  
  877.       type FAULT_TYPE is (
  878.      INVALID_OPERATION, DIVISION_BY_ZERO, OVERFLOW, UNDERFLOW, INEXACT);
  879.  
  880.       function FAULT_STATUS_FLAG (FAULT : FAULT_TYPE) return BOOLEAN;
  881.       -- FAULT_STATUS_FLAG returns the setting of the specified status flag
  882.       -- for the currently active task.
  883.  
  884.       procedure SET_FAULT_STATUS_FLAG (FAULT : FAULT_TYPE);
  885.       -- SET_FAULT_STATUS_FLAG sets the specified status flag to TRUE for the
  886.       -- currently active task.
  887.  
  888.       procedure RESET_FAULT_STATUS_FLAG (FAULT : FAULT_TYPE);
  889.       -- RESET_FAULT_STATUS_FLAG sets the specified status flag to FALSE for
  890.       -- the currently active task.
  891.  
  892.       procedure RESET_FAULT_STATUS_FLAGS;
  893.       -- RESET_FAULT_STATUS_FLAGS resets all status flags to FALSE for the
  894.       -- currently active task.
  895.  
  896.    -- 7.1  Invalid Operation
  897.    -- 7.2  Division by Zero
  898.    -- 7.3  Overflow
  899.    -- 7.4  Underflow
  900.    -- 7.5  Inexact
  901.  
  902.       -- These faults are signaled in the situations described in the
  903.       -- corresponding section of P754. The effect of a fault being signaled
  904.       -- is described in the following section.
  905.  
  906.  
  907.                 --------------
  908.                 -- 8. Traps --
  909.                 --------------
  910.  
  911.    -- P754 requires that a user be able to specify whether or not a user trap
  912.    -- routine is called following the occurrence of a fault. In addition this
  913.    -- specification allows for a third possibility of raising an Ada exception
  914.    -- which is more in keeping with the normal Ada view of how exceptional
  915.    -- conditions should be handled. The definitions in this section provide
  916.    -- means of controlling which of the three fault handling approaches is to
  917.    -- be used for subsequent operations.
  918.  
  919.       type FAULT_ACTION is (
  920.  
  921.      TRAP,
  922.         -- When the fault occurs, IEEE_P754_USER_TRAP_ROUTINE is called.
  923.         -- This procedure may use the subprograms defined in section 8.1
  924.         -- to determine the cause of the fault. It can take one of two
  925.         -- possible actions, it may raise an Ada exception, in which case
  926.         -- the Ada exception will be propagated from the location of the
  927.         -- operation signalling the P754 fault, or it may return after
  928.         -- calling SET_RESULT to provide a replacement result, in which
  929.         -- case execution continues from the point of the fault.
  930.  
  931.         -- This mode may only be specified if MACHINE_OVERFLOWS is FALSE.
  932.  
  933.  
  934.      RAISE_FAULT,
  935.         -- The exception FAULT is raised. The exception handler may use
  936.         -- the subprograms in section 8.1 to determine the cause of
  937.         -- the fault.
  938.  
  939.         -- This mode may only be specified if MACHINE_OVERFLOWS is FALSE.
  940.  
  941.      RAISE_ERROR,
  942.         -- The exception CONSTRAINT_ERROR is raised. This is the action
  943.         -- which corresponds to the normal Ada handling on a numeric
  944.         -- error, where no attempt is made to determine the cause of the
  945.         -- fault, and is the default mode if MACHINE_OVERFLOWS is TRUE.
  946.  
  947.         -- This mode may only be specified if MACHINE_OVERFLOWS is FALSE.
  948.  
  949.      NO_ACTION);
  950.         -- No trap occurs and no exception is raised. The operation will
  951.         -- return the default value specified in P754.
  952.  
  953.         -- This is the only mode which may be specified, and is the
  954.         -- default mode, if MACHINE_OVERFLOWS is set to FALSE. It must
  955.         -- still be fully implemented if MACHINE_OVERFLOWS is TRUE.
  956.  
  957.      -- Regardless of the fault action, the corresponding status flag is
  958.      -- set when a fault occurs (and remains set until explicitly reset).
  959.  
  960.       -- The following subprograms may be used to set or determine the trap
  961.       -- action associated with any of the individual fault types.
  962.  
  963.      procedure SET_FAULT_ACTION (FAULT : FAULT_TYPE;
  964.                      ACTION : FAULT_ACTION);
  965.      -- Set the action to be taken on a the specified fault. Any
  966.      -- subsequent operations in the current task causing this fault will
  967.      -- result in the specified action to be taken. It is possible for
  968.      -- different tasks to have different trap actions for the same
  969.      -- fault, and it is possible to have different actions for different
  970.      -- faults within a single task.
  971.  
  972.      -- Note: on a machine where MACHINE_OVERFLOWS is FALSE the attempt
  973.      -- to set any ACTION other than NO_ACTION results in raising the
  974.      -- predefined exception PROGRAM_ERROR.
  975.  
  976.      function GET_FAULT_ACTION (FAULT : FAULT_TYPE) return FAULT_ACTION;
  977.      -- Get the action currently being taken on the specified fault for
  978.      -- the current task.
  979.       -- The initial default actions for the environment task are RAISE_ERROR
  980.       -- for the overflow, divide by zero and invalid operation faults, and
  981.       -- NO_ACTION for the underflow and inexact faults. The initial set of
  982.       -- actions for any other task is inherited from the parent task.
  983.  
  984.    -- 8.1  Trap Handler
  985.  
  986.       -- Section 8.1 requires that a trap handler can obtain certain
  987.       -- information about the nature of the fault causing the trap. This
  988.       -- section of the specification provides a set of functions which can
  989.       -- be used after a fault has occurred to determine the required
  990.       -- information. These functions may only be used in a user trap
  991.       -- routine, or in an exception handler for the exception FAULT.
  992.  
  993.       -- A check is made when any of these functions are used that a fault
  994.       -- has occurred for which the specified fault action is either
  995.       -- RAISE_FAULT or TRAP. If this check fails, then PROGRAM_ERROR is
  996.       -- raised, indicating a flaw in the logical structure of the program.
  997.  
  998.       -- 8.1.1  Determining which exception has occurred
  999.  
  1000.      -- The following functions can be used to determine which fault or
  1001.      -- faults caused the trap:
  1002.  
  1003.         function FAULT_OCCURRED (FAULT : FAULT_TYPE) return BOOLEAN;
  1004.         -- FAULT_OCCURRED returns TRUE if the specified FAULT_TYPE was one
  1005.         -- of the faults which caused the current trap. Note that the
  1006.         -- result differs from that obtained from FAULT_STATUS_FLAG in
  1007.         -- that the status flags are sticky and remain set until
  1008.         -- explicitly reset. FAULT_OCCURRED signals TRUE only for the
  1009.         -- specific faults which have caused the most recent trap or
  1010.         -- exception in the current task.
  1011.  
  1012.       -- 8.1.2  Determining the kind of operation that was being performed
  1013.  
  1014.      -- The following definitions allow the determination of the kind of
  1015.      -- the operation which caused the trap.
  1016.  
  1017.         type OPERATION is
  1018.            (OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_REM, OP_CONVERSION,
  1019.         EQ, NE, GT, GE, LT, LE, LG, LEG, UG, UGE, UL, ULE, UE,
  1020.         COMPARE, VALUE, IMAGE, SQRT, SCALB, LOGB, NEXTAFTER);
  1021.  
  1022.         function FAULT_OPERATION return OPERATION;
  1023.         -- FAULT_OPERATION returns one of the possible values of OPERATION
  1024.         -- to indicate the type of the operation causing the fault.
  1025.  
  1026.         type FORMAT is (FLOAT_FORMAT,
  1027.                 LONG_FLOAT_FORMAT,
  1028.                 EXTENDED_FLOAT_FORMAT,
  1029.                 INTEGER_FORMAT);
  1030.         -- The type FORMAT is used to indicate precisions of operands and
  1031.         -- results. Implementations which provide additional FLOAT formats
  1032.         -- (such as SINGLE_EXTENDED_FLOAT), or additional integer types
  1033.         -- (such as LONG_INTEGER), should extend the definition of FORMAT
  1034.         -- appropriately.
  1035.  
  1036.         function OPERAND_FORMAT return FORMAT;
  1037.         -- The function OPERAND_FORMAT may be used to determine the format
  1038.         -- of the operand or operands of the operation.
  1039.       -- 8.1.3  Determining the destination's format
  1040.  
  1041.       -- The following function may be used to determine the format of
  1042.       -- the destination (result) of the operation causing the trap:
  1043.  
  1044.          function DESTINATION_FORMAT return FORMAT;
  1045.  
  1046.        -- 8.1.4  Determining the correctly rounded result
  1047.  
  1048.      -- In the case where the fault is caused by overflow, underflow or
  1049.      -- inexact results, the following functions may be used to determine
  1050.      -- the correctly rounded result.
  1051.  
  1052.         function RESULT_SIGNIFICAND return FLOAT;
  1053.         function RESULT_SIGNIFICAND return LONG_FLOAT;
  1054.         function RESULT_SIGNIFICAND return EXTENDED_FLOAT;
  1055.         -- RESULT_SIGNIFICAND returns the significand of the correctly
  1056.         -- rounded result. The absolute value of the significand is in
  1057.         -- the range 1.0 <= significand < 2.0, except in the case of a
  1058.         -- true zero result, in which case an appropriately signed zero
  1059.         -- value is returned.
  1060.  
  1061.         function RESULT_EXPONENT return FLOAT;
  1062.         function RESULT_EXPONENT return LONG_FLOAT;
  1063.         function RESULT_EXPONENT return EXTENDED_FLOAT;
  1064.         -- RESULT_EXPONENT returns the exponent of the correctly rounded
  1065.         -- result as a signed unbiased integral value. This exponent value
  1066.         -- may be outside the normally permitted exponent range.
  1067.  
  1068.      -- Note: if the fault causing the trap or FAULT exception was not
  1069.      -- overflow, underflow or inexact, then the use of either of these
  1070.      -- functions represents a logical flaw in the program structure, and
  1071.      -- PROGRAM_ERROR is raised.
  1072.  
  1073.       -- 8.1.5  Determining the operand values
  1074.  
  1075.      -- In the case where the fault is caused by invalid operation or
  1076.      -- divide by zero conditions, the following functions may be used to
  1077.      -- determine the operand values.
  1078.  
  1079.         function OPERAND_X return FLOAT;
  1080.         function OPERAND_X return LONG_FLOAT;
  1081.         function OPERAND_X return EXTENDED_FLOAT;
  1082.         function OPERAND_Y return FLOAT;
  1083.         function OPERAND_Y return LONG_FLOAT;
  1084.         function OPERAND_Y return EXTENDED_FLOAT;
  1085.         -- The corresponding operand value is returned (X and Y refer to
  1086.         -- the names of the formal parameter).
  1087.  
  1088.      -- Note: the use of these functions when the fault condition is other
  1089.      -- than divide by zero or invalid operation, or the use of OPERAND_Y
  1090.      -- when the operation causing the fault has only one operand, or when
  1091.      -- the format does not correspond to the operand format, represents a
  1092.      -- logical flaw in the program structure, and results in raising the
  1093.      -- exception PROGRAM_ERROR.
  1094.  
  1095.      -- Note: even if the original operation involved types derived
  1096.      -- from the standard types, the operands may be fetched using the
  1097.      -- basic types. It is assumed that the trap routine knows only about
  1098.      -- representations and not about types at the Ada level.
  1099.       -- 8.1.6  Setting an Alternate Result
  1100.  
  1101.      -- The following functions may be used in the user trap routine to
  1102.      -- set an alternate result for the operation:
  1103.  
  1104.         procedure SET_RESULT (X : FLOAT);
  1105.         procedure SET_RESULT (X : LONG_FLOAT);
  1106.         procedure SET_RESULT (X : EXTENDED_FLOAT);
  1107.  
  1108.      -- Note: the use of these procedures if the FAULT_ACTION is other
  1109.      -- than TRAP, or an attempt to set a result of inappropriate format,
  1110.      -- represents a logical flaw in the program structure, and results
  1111.      -- in PROGRAM_ERROR being raised.
  1112.  
  1113.    -- 8.2  Precedence
  1114.  
  1115.       -- In accordance with section 8.2, if overflow or underflow traps are
  1116.       -- enabled (i.e. the fault action is other than NO_ACTION), then the
  1117.       -- overflow or underflow trap or exception takes precedence over a
  1118.       -- separate inexact trap or exception.
  1119.  
  1120.    end IEEE_P754_FAULT_CONTROL;
  1121.  
  1122.  
  1123.                 --------------
  1124.                 -- Appendix --
  1125.                 --------------
  1126.  
  1127.    -- P754 lists a number of recommended functions in the Appendix which are
  1128.    -- not required. However, a full implementation of the Ada packages requires
  1129.    -- that these functions be provided. See section 5.7.1 for details.
  1130.