0.8.2.8:
[sbcl.git] / src / code / float-trap.lisp
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.
5
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
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.
14
15 (in-package "SB!VM")
16
17 (eval-when (:compile-toplevel :load-toplevel :execute)
18
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)))
26
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)))
32
33 ;;; Return a mask with all the specified float trap bits set.
34 (defun float-trap-mask (names)
35   (reduce #'logior
36           (mapcar (lambda (x)
37                     (or (cdr (assoc x *float-trap-alist*))
38                         (error "unknown float trap kind: ~S" x)))
39                   names)))
40 ) ; EVAL-WHEN
41
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.
45 #!-alpha
46 (progn
47   (defun floating-point-modes () 
48     (floating-point-modes))
49   (defun (setf floating-point-modes) (new) 
50     (setf (floating-point-modes) new)))
51
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:
55 ;;; :TRAPS
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.
59 ;;;
60 ;;;:ROUNDING-MODE
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.
65 ;;;
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.
70 ;;;
71 ;;;:FAST-MODE
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.
76 ;;;
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
79 ;;; enabled traps
80
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)))
87     (when traps-p
88       (setf (ldb float-traps-byte modes) (float-trap-mask traps)))
89     (when round-p
90       (setf (ldb float-rounding-mode modes)
91             (or (cdr (assoc rounding-mode *rounding-mode-alist*))
92                 (error "unknown rounding mode: ~S" rounding-mode))))
93     (when current-x-p
94       (setf (ldb float-exceptions-byte modes)
95             (float-trap-mask current-exceptions)))
96     (when accrued-x-p
97       (setf (ldb float-sticky-bits modes)
98             (float-trap-mask accrued-exceptions)))
99     (when fast-mode-p
100       (if fast-mode
101           (setq modes (logior float-fast-bit modes))
102           (setq modes (logand (lognot float-fast-bit) modes))))
103     ;; FIXME: This apparently doesn't work on Darwin
104     #!-darwin (setf (floating-point-modes) modes))
105
106   (values))
107
108 ;;; This function returns a list representing the state of the floating 
109 ;;; point modes. The list is in the same format as the &KEY arguments to
110 ;;; SET-FLOATING-POINT-MODES, i.e.
111 ;;;    (apply #'set-floating-point-modes (get-floating-point-modes))
112 ;;; sets the floating point modes to their current values (and thus is a
113 ;;; no-op).
114 (defun get-floating-point-modes ()
115   (flet ((exc-keys (bits)
116            (macrolet ((frob ()
117                         `(collect ((res))
118                            ,@(mapcar (lambda (x)
119                                        `(when (logtest bits ,(cdr x))
120                                           (res ',(car x))))
121                                      *float-trap-alist*)
122                            (res))))
123              (frob))))
124     (let ((modes (floating-point-modes)))
125       `(:traps ,(exc-keys (ldb float-traps-byte modes))
126         :rounding-mode ,(car (rassoc (ldb float-rounding-mode modes)
127                                      *rounding-mode-alist*))
128         :current-exceptions ,(exc-keys (ldb float-exceptions-byte modes))
129         :accrued-exceptions ,(exc-keys (ldb float-sticky-bits modes))
130         :fast-mode ,(logtest float-fast-bit modes)))))
131
132 ;;; Return true if any of the named traps are currently trapped, false
133 ;;; otherwise.
134 (defmacro current-float-trap (&rest traps)
135   `(not (zerop (logand ,(dpb (float-trap-mask traps) float-traps-byte 0)
136                        (floating-point-modes)))))
137
138 ;;; Signal the appropriate condition when we get a floating-point error.
139 (defun sigfpe-handler (signal info context)
140   (declare (ignore signal info context))
141   (declare (type system-area-pointer context))
142   (let* ((modes (context-floating-point-modes
143                  (sb!alien:sap-alien context (* os-context-t))))
144          (traps (logand (ldb float-exceptions-byte modes)
145                         (ldb float-traps-byte modes))))
146     (cond ((not (zerop (logand float-divide-by-zero-trap-bit traps)))
147            (error 'division-by-zero))
148           ((not (zerop (logand float-invalid-trap-bit traps)))
149            (error 'floating-point-invalid-operation))
150           ((not (zerop (logand float-overflow-trap-bit traps)))
151            (error 'floating-point-overflow))
152           ((not (zerop (logand float-underflow-trap-bit traps)))
153            (error 'floating-point-underflow))
154           ((not (zerop (logand float-inexact-trap-bit traps)))
155            (error 'floating-point-inexact))
156           #!+FreeBSD
157           ((zerop (ldb float-exceptions-byte modes))
158            ;; I can't tell what caused the exception!!
159            (error 'floating-point-exception
160                   :traps (getf (get-floating-point-modes) :traps)))
161           (t
162            (error 'floating-point-exception)))))
163
164 ;;; Execute BODY with the floating point exceptions listed in TRAPS
165 ;;; masked (disabled). TRAPS should be a list of possible exceptions
166 ;;; which includes :UNDERFLOW, :OVERFLOW, :INEXACT, :INVALID and
167 ;;; :DIVIDE-BY-ZERO and on the X86 :DENORMALIZED-OPERAND. The
168 ;;; respective accrued exceptions are cleared at the start of the body
169 ;;; to support their testing within, and restored on exit.
170 (defmacro with-float-traps-masked (traps &body body)
171   (let ((traps (dpb (float-trap-mask traps) float-traps-byte 0))
172         (exceptions (dpb (float-trap-mask traps) float-sticky-bits 0))
173         (trap-mask (dpb (lognot (float-trap-mask traps))
174                         float-traps-byte #xffffffff))
175         (exception-mask (dpb (lognot (float-trap-mask traps))
176                              float-sticky-bits #xffffffff))
177         (orig-modes (gensym)))
178     `(let ((,orig-modes (floating-point-modes)))
179       (unwind-protect
180            (progn
181              (setf (floating-point-modes)
182                    (logand ,orig-modes ,(logand trap-mask exception-mask)))
183              ,@body)
184         ;; Restore the original traps and exceptions.
185         (setf (floating-point-modes)
186               (logior (logand ,orig-modes ,(logior traps exceptions))
187                       (logand (floating-point-modes)
188                               ,(logand trap-mask exception-mask))))))))