f99ec5b080189b5206b7898a3919cec3ee0dc935
[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 (defconstant 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 ;;; Return a mask with all the specified float trap bits set.
28 (defun float-trap-mask (names)
29   (reduce #'logior
30           (mapcar #'(lambda (x)
31                       (or (cdr (assoc x float-trap-alist))
32                           (error "Unknown float trap kind: ~S." x)))
33                   names)))
34
35 (defconstant rounding-mode-alist
36   (list (cons :nearest float-round-to-nearest)
37         (cons :zero float-round-to-zero)
38         (cons :positive-infinity float-round-to-positive)
39         (cons :negative-infinity float-round-to-negative)))
40
41 ); Eval-When (Compile Load Eval)
42
43 ;;; interpreter stubs
44 (defun floating-point-modes () (floating-point-modes))
45 (defun (setf floating-point-modes) (new) (setf (floating-point-modes) new))
46
47 (defun set-floating-point-modes (&key (traps nil traps-p)
48                                       (rounding-mode nil round-p)
49                                       (current-exceptions nil current-x-p)
50                                       (accrued-exceptions nil accrued-x-p)
51                                       (fast-mode nil fast-mode-p))
52   #!+sb-doc
53   "This function sets options controlling the floating-point hardware. If a
54   keyword is not supplied, then the current value is preserved. Possible
55   keywords:
56
57    :TRAPS
58        A list of the exception conditions that should cause traps. Possible
59        exceptions are :UNDERFLOW, :OVERFLOW, :INEXACT, :INVALID,
60        :DIVIDE-BY-ZERO, and on the X86 :DENORMALIZED-OPERAND. Initially
61        all traps except :INEXACT are enabled.
62
63    :ROUNDING-MODE
64        The rounding mode to use when the result is not exact. Possible values
65        are :NEAREST, :POSITIVE-INFINITY, :NEGATIVE-INFINITY and :ZERO.
66        Initially, the rounding mode is :NEAREST.
67
68    :CURRENT-EXCEPTIONS
69    :ACCRUED-EXCEPTIONS
70        These arguments allow setting of the exception flags. The main use is
71        setting the accrued exceptions to NIL to clear them.
72
73    :FAST-MODE
74        Set the hardware's \"fast mode\" flag, if any. When set, IEEE
75        conformance or debuggability may be impaired. Some machines may not
76        have this feature, in which case the value is always NIL.
77
78    GET-FLOATING-POINT-MODES may be used to find the floating point modes
79    currently in effect."
80   (let ((modes (floating-point-modes)))
81     (when traps-p
82       (setf (ldb float-traps-byte modes) (float-trap-mask traps)))
83     (when round-p
84       (setf (ldb float-rounding-mode modes)
85             (or (cdr (assoc rounding-mode rounding-mode-alist))
86                 (error "Unknown rounding mode: ~S." rounding-mode))))
87     (when current-x-p
88       (setf (ldb float-exceptions-byte modes)
89             (float-trap-mask current-exceptions)))
90     (when accrued-x-p
91       (setf (ldb float-sticky-bits modes)
92             (float-trap-mask accrued-exceptions)))
93     (when fast-mode-p
94       (if fast-mode
95           (setq modes (logior float-fast-bit modes))
96           (setq modes (logand (lognot float-fast-bit) modes))))
97     (setf (floating-point-modes) modes))
98
99   (values))
100
101 (defun get-floating-point-modes ()
102   #!+sb-doc
103   "This function returns a list representing the state of the floating point
104   modes. The list is in the same format as the keyword arguments to
105   SET-FLOATING-POINT-MODES, i.e.
106       (apply #'set-floating-point-modes (get-floating-point-modes))
107
108   sets the floating point modes to their current values (and thus is a no-op)."
109   (flet ((exc-keys (bits)
110            (macrolet ((frob ()
111                         `(collect ((res))
112                            ,@(mapcar #'(lambda (x)
113                                          `(when (logtest bits ,(cdr x))
114                                             (res ',(car x))))
115                                      float-trap-alist)
116                            (res))))
117              (frob))))
118     (let ((modes (floating-point-modes)))
119       `(:traps ,(exc-keys (ldb float-traps-byte modes))
120         :rounding-mode ,(car (rassoc (ldb float-rounding-mode modes)
121                                      rounding-mode-alist))
122         :current-exceptions ,(exc-keys (ldb float-exceptions-byte modes))
123         :accrued-exceptions ,(exc-keys (ldb float-sticky-bits modes))
124         :fast-mode ,(logtest float-fast-bit modes)))))
125
126 (defmacro current-float-trap (&rest traps)
127   #!+sb-doc
128   "Current-Float-Trap Trap-Name*
129   Return true if any of the named traps are currently trapped, false
130   otherwise."
131   `(not (zerop (logand ,(dpb (float-trap-mask traps) float-traps-byte 0)
132                        (floating-point-modes)))))
133
134 ;;; Signal the appropriate condition when we get a floating-point error.
135 (defun sigfpe-handler (signal info context)
136   (declare (ignore signal info))
137   (declare (type system-area-pointer context))
138   ;; FIXME: The find-the-detailed-problem code below went stale with
139   ;; the big switchover to POSIX signal handling and signal contexts
140   ;; which are opaque at the Lisp level ca plod-0.6.7. It needs to be
141   ;; revived, which will require writing a C-level os-dependent
142   ;; function to extract floating point modes, and a Lisp-level
143   ;; DEF-ALIEN-ROUTINE to get to the C-level os-dependent function.
144   ;; Meanwhile we just say "something went wrong".
145   (error 'floating-point-exception)
146   #|
147   (let* ((modes (context-floating-point-modes
148                  (sb!alien:sap-alien context (* os-context-t))))
149          (traps (logand (ldb float-exceptions-byte modes)
150                         (ldb float-traps-byte modes))))
151     (cond ((not (zerop (logand float-divide-by-zero-trap-bit traps)))
152            (error 'division-by-zero))
153           ((not (zerop (logand float-invalid-trap-bit traps)))
154            (error 'floating-point-invalid-operation))
155           ((not (zerop (logand float-overflow-trap-bit traps)))
156            (error 'floating-point-overflow))
157           ((not (zerop (logand float-underflow-trap-bit traps)))
158            (error 'floating-point-underflow))
159           ((not (zerop (logand float-inexact-trap-bit traps)))
160            (error 'floating-point-inexact))
161           #!+FreeBSD
162           ((zerop (ldb float-exceptions-byte modes))
163            ;; I can't tell what caused the exception!!
164            (error 'floating-point-exception
165                   :traps (getf (get-floating-point-modes) :traps)))
166           (t
167            (error "SIGFPE with no exceptions currently enabled?"))))
168   |#
169   )
170
171 (defmacro with-float-traps-masked (traps &body body)
172   #!+sb-doc
173   "Execute BODY with the floating point exceptions listed in TRAPS
174   masked (disabled). TRAPS should be a list of possible exceptions
175   which includes :UNDERFLOW, :OVERFLOW, :INEXACT, :INVALID and
176   :DIVIDE-BY-ZERO and on the X86 :DENORMALIZED-OPERAND. The respective
177   accrued exceptions are cleared at the start of the body to support
178   their testing within, and restored on exit."
179   (let ((traps (dpb (float-trap-mask traps) float-traps-byte 0))
180         (exceptions (dpb (float-trap-mask traps) float-sticky-bits 0))
181         (trap-mask (dpb (lognot (float-trap-mask traps))
182                         float-traps-byte #xffffffff))
183         (exception-mask (dpb (lognot (sb!vm::float-trap-mask traps))
184                              float-sticky-bits #xffffffff)))
185     `(let ((orig-modes (floating-point-modes)))
186       (unwind-protect
187            (progn
188              (setf (floating-point-modes)
189                    (logand orig-modes ,(logand trap-mask exception-mask)))
190              ,@body)
191         ;; Restore the original traps and exceptions.
192         (setf (floating-point-modes)
193               (logior (logand orig-modes ,(logior traps exceptions))
194                       (logand (floating-point-modes)
195                               ,(logand trap-mask exception-mask))))))))