Previous: , Up: Floats   [Contents][Index]


2.1.2.6 Accessing the Floating Point Modes

These functions can be used to modify or read the floating point modes:

Function: extensions:set-floating-point-modes &key :traps :rounding-mode :fast-mode :accrued-exceptions :current-exceptions
Function: extensions:get-floating-point-modes

The keyword arguments to set-floating-point-modes set various modes controlling how floating point arithmetic is done:

:traps

A list of the exception conditions that should cause traps. Possible exceptions are :underflow, :overflow, :inexact, :invalid and :divide-by-zero. Initially all traps except :inexact are enabled. See float-traps.

:rounding-mode

The rounding mode to use when the result is not exact. Possible values are :nearest, :positive-infinity, :negative-infinity and :zero. Initially, the rounding mode is :nearest. See the warning in section float-rounding-modes about use of other rounding modes.

:current-exceptions, :accrued-exceptions

Lists of exception keywords used to set the exception flags. The current-exceptions are the exceptions for the previous operation, so setting it is not very useful. The accrued-exceptions are a cumulative record of the exceptions that occurred since the last time these flags were cleared. Specifying () will clear any accrued exceptions.

:fast-mode

Set the hardware’s “fast mode” flag, if any. When set, IEEE conformance or debuggability may be impaired. Some machines may not have this feature, in which case the value is always nil. Sparc platforms support a fast mode where denormal numbers are silently truncated to zero.

If a keyword argument is not supplied, then the associated state is not changed.

get-floating-point-modes returns a list representing the state of the floating point modes. The list is in the same format as the keyword arguments to set-floating-point-modes, so apply could be used with set-floating-point-modes to restore the modes in effect at the time of the call to get-floating-point-modes.

To make handling control of floating-point exceptions, the following macro is useful.

Macro: ext:with-float-traps-masked traps &body body

body is executed with the selected floating-point exceptions given by traps masked out (disabled). traps should be a list of possible floating-point exceptions that should be ignored. Possible values are :underflow, :overflow, :inexact, :invalid and :divide-by-zero.

This is equivalent to saving the current traps from get-floating-point-modes, setting the floating-point modes to the desired exceptions, running the body, and restoring the saved floating-point modes. The advantage of this macro is that it causes less consing to occur.

Some points about the with-float-traps-masked:

  • Two approaches are available for detecting FP exceptions:
    1. enabling the traps and handling the exceptions
    2. disabling the traps and either handling the return values or checking the accrued exceptions.

    Of these the latter is the most portable because on the alpha port it is not possible to enable some traps at run-time.

  • To assist the checking of the exceptions within the body any accrued exceptions matching the given traps are cleared at the start of the body when the traps are masked.
  • To allow the macros to be nested these accrued exceptions are restored at the end of the body to their values at the start of the body. Thus any exceptions that occurred within the body will not affect the accrued exceptions outside the macro.
  • Note that only the given exceptions are restored at the end of the body so other exception will be visible in the accrued exceptions outside the body.
  • On the x86, setting the accrued exceptions of an unmasked exception would cause a FP trap. The macro behaviour of restoring the accrued exceptions ensures than if an accrued exception is initially not flagged and occurs within the body it will be restored/cleared at the exit of the body and thus not cause a trap.
  • On the x86, and, perhaps, the hppa, the FP exceptions may be delivered at the next FP instruction which requires a FP wait instruction (x86::float-wait) if using the lisp conditions to catch trap within a handler-bind. The handler-bind macro does the right thing and inserts a float-wait (at the end of its body on the x86). The masking and noting of exceptions is also safe here.
  • The setting of the FP flags uses the (floating-point-modes) and the (set (floating-point-modes)...) VOPs. These VOPs blindly update the flags which may include other state. We assume this state hasn’t changed in between getting and setting the state. For example, if you used the FP unit between the above calls, the state may be incorrectly restored! The with-float-traps-masked macro keeps the intervening code to a minimum and uses only integer operations.

Previous: Floating Point Rounding Mode, Up: Floats   [Contents][Index]