1 ;;;; This file contains stuff for controlling floating point traps. It
2 ;;;; is IEEE float specific, but should work for pretty much any FPU
3 ;;;; where the state fits in one word and exceptions are represented
4 ;;;; by bits being set.
6 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
17 (eval-when (:compile-toplevel :load-toplevel :execute)
19 (defparameter *float-trap-alist*
20 (list (cons :underflow float-underflow-trap-bit)
21 (cons :overflow float-overflow-trap-bit)
22 (cons :inexact float-inexact-trap-bit)
23 (cons :invalid float-invalid-trap-bit)
24 (cons :divide-by-zero float-divide-by-zero-trap-bit)
25 #!+x86 (cons :denormalized-operand float-denormal-trap-bit)))
27 (defparameter *rounding-mode-alist*
28 (list (cons :nearest float-round-to-nearest)
29 (cons :zero float-round-to-zero)
30 (cons :positive-infinity float-round-to-positive)
31 (cons :negative-infinity float-round-to-negative)))
33 ;;; Return a mask with all the specified float trap bits set.
34 (defun float-trap-mask (names)
37 (or (cdr (assoc x *float-trap-alist*))
38 (error "unknown float trap kind: ~S" x)))
42 ;;; interpreter stubs for floating point modes get/setters for the
43 ;;; alpha have been removed to alpha-vm.lisp, as they are implemented
44 ;;; in C rather than as VOPs.
47 (defun floating-point-modes ()
48 (floating-point-modes))
49 (defun (setf floating-point-modes) (new)
50 (setf (floating-point-modes) new)))
52 ;;; This function sets options controlling the floating-point
53 ;;; hardware. If a keyword is not supplied, then the current value is
54 ;;; preserved. Possible keywords:
56 ;;; A list of the exception conditions that should cause traps.
57 ;;; Possible exceptions are :UNDERFLOW, :OVERFLOW, :INEXACT, :INVALID,
58 ;;; :DIVIDE-BY-ZERO, and on the X86 :DENORMALIZED-OPERAND.
61 ;;; The rounding mode to use when the result is not exact. Possible
62 ;;; values are :NEAREST, :POSITIVE-INFINITY, :NEGATIVE-INFINITY and
63 ;;; :ZERO. Setting this away from :NEAREST is liable to upset SBCL's
64 ;;; maths routines which depend on it.
66 ;;;:CURRENT-EXCEPTIONS
67 ;;;:ACCRUED-EXCEPTIONS
68 ;;; These arguments allow setting of the exception flags. The main
69 ;;; use is setting the accrued exceptions to NIL to clear them.
72 ;;; Set the hardware's \"fast mode\" flag, if any. When set, IEEE
73 ;;; conformance or debuggability may be impaired. Some machines don't
74 ;;; have this feature, and some SBCL ports don't implement it anyway
75 ;;; -- in such cases the value is always NIL.
77 ;;; GET-FLOATING-POINT-MODES may be used to find the floating point modes
78 ;;; currently in effect. See cold-init.lisp for the list of initially
81 (defun set-floating-point-modes (&key (traps nil traps-p)
82 (rounding-mode nil round-p)
83 (current-exceptions nil current-x-p)
84 (accrued-exceptions nil accrued-x-p)
85 (fast-mode nil fast-mode-p))
86 (let ((modes (floating-point-modes)))
88 (setf (ldb float-traps-byte modes) (float-trap-mask traps)))
90 (setf (ldb float-rounding-mode modes)
91 (or (cdr (assoc rounding-mode *rounding-mode-alist*))
92 (error "unknown rounding mode: ~S" rounding-mode))))
94 (setf (ldb float-exceptions-byte modes)
95 (float-trap-mask current-exceptions)))
97 (setf (ldb float-sticky-bits modes)
98 (float-trap-mask accrued-exceptions)))
101 (setq modes (logior float-fast-bit modes))
102 (setq modes (logand (lognot float-fast-bit) modes))))
103 (setf (floating-point-modes) modes))
107 ;;; This function returns a list representing the state of the floating
108 ;;; point modes. The list is in the same format as the &KEY arguments to
109 ;;; SET-FLOATING-POINT-MODES, i.e.
110 ;;; (apply #'set-floating-point-modes (get-floating-point-modes))
111 ;;; sets the floating point modes to their current values (and thus is a
113 (defun get-floating-point-modes ()
114 (flet ((exc-keys (bits)
117 ,@(mapcar (lambda (x)
118 `(when (logtest bits ,(cdr x))
123 (let ((modes (floating-point-modes)))
124 `(:traps ,(exc-keys (ldb float-traps-byte modes))
125 :rounding-mode ,(car (rassoc (ldb float-rounding-mode modes)
126 *rounding-mode-alist*))
127 :current-exceptions ,(exc-keys (ldb float-exceptions-byte modes))
128 :accrued-exceptions ,(exc-keys (ldb float-sticky-bits modes))
129 :fast-mode ,(logtest float-fast-bit modes)))))
131 ;;; Return true if any of the named traps are currently trapped, false
133 (defmacro current-float-trap (&rest traps)
134 `(not (zerop (logand ,(dpb (float-trap-mask traps) float-traps-byte 0)
135 (floating-point-modes)))))
137 ;;; Signal the appropriate condition when we get a floating-point error.
138 (defun sigfpe-handler (signal info context)
139 (declare (ignore signal info context))
140 (declare (type system-area-pointer context))
141 (let* ((modes (context-floating-point-modes
142 (sb!alien:sap-alien context (* os-context-t))))
143 (traps (logand (ldb float-exceptions-byte modes)
144 (ldb float-traps-byte modes))))
145 (cond ((not (zerop (logand float-divide-by-zero-trap-bit traps)))
146 (error 'division-by-zero))
147 ((not (zerop (logand float-invalid-trap-bit traps)))
148 (error 'floating-point-invalid-operation))
149 ((not (zerop (logand float-overflow-trap-bit traps)))
150 (error 'floating-point-overflow))
151 ((not (zerop (logand float-underflow-trap-bit traps)))
152 (error 'floating-point-underflow))
153 ((not (zerop (logand float-inexact-trap-bit traps)))
154 (error 'floating-point-inexact))
156 ((zerop (ldb float-exceptions-byte modes))
157 ;; I can't tell what caused the exception!!
158 (error 'floating-point-exception
159 :traps (getf (get-floating-point-modes) :traps)))
161 (error 'floating-point-exception)))))
163 ;;; Execute BODY with the floating point exceptions listed in TRAPS
164 ;;; masked (disabled). TRAPS should be a list of possible exceptions
165 ;;; which includes :UNDERFLOW, :OVERFLOW, :INEXACT, :INVALID and
166 ;;; :DIVIDE-BY-ZERO and on the X86 :DENORMALIZED-OPERAND. The
167 ;;; respective accrued exceptions are cleared at the start of the body
168 ;;; to support their testing within, and restored on exit.
169 (defmacro with-float-traps-masked (traps &body body)
170 (let ((traps (dpb (float-trap-mask traps) float-traps-byte 0))
171 (exceptions (dpb (float-trap-mask traps) float-sticky-bits 0))
172 (trap-mask (dpb (lognot (float-trap-mask traps))
173 float-traps-byte #xffffffff))
174 (exception-mask (dpb (lognot (float-trap-mask traps))
175 float-sticky-bits #xffffffff))
176 (orig-modes (gensym)))
177 `(let ((,orig-modes (floating-point-modes)))
180 (setf (floating-point-modes)
181 (logand ,orig-modes ,(logand trap-mask exception-mask)))
183 ;; Restore the original traps and exceptions.
184 (setf (floating-point-modes)
185 (logior (logand ,orig-modes ,(logior traps exceptions))
186 (logand (floating-point-modes)
187 ,(logand trap-mask exception-mask))))))))