4bd83cb3aeaac9fe7122536131d6a14159a40f9f
[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 #!+x86
34 (defparameter *precision-mode-alist*
35   (list (cons :24-bit float-precision-24-bit)
36         (cons :53-bit float-precision-53-bit)
37         (cons :64-bit float-precision-64-bit)))
38
39 ;;; Return a mask with all the specified float trap bits set.
40 (defun float-trap-mask (names)
41   (reduce #'logior
42           (mapcar (lambda (x)
43                     (or (cdr (assoc x *float-trap-alist*))
44                         (error "unknown float trap kind: ~S" x)))
45                   names)))
46 ) ; EVAL-WHEN
47
48 ;;; interpreter stubs for floating point modes get/setters for the
49 ;;; alpha have been removed to alpha-vm.lisp, as they are implemented
50 ;;; in C rather than as VOPs. Likewise for x86-64 and mips.
51 #!-(or alpha x86-64 mips)
52 (progn
53   (defun floating-point-modes ()
54     (floating-point-modes))
55   (defun (setf floating-point-modes) (new)
56     (setf (floating-point-modes) new)))
57
58 (defun set-floating-point-modes (&key
59                                  (traps nil traps-p)
60                                  (rounding-mode nil round-p)
61                                  (current-exceptions nil current-x-p)
62                                  (accrued-exceptions nil accrued-x-p)
63                                  (fast-mode nil fast-mode-p)
64                                  #!+x86 (precision nil precisionp))
65   #!+sb-doc
66   "This function sets options controlling the floating-point
67 hardware. If a keyword is not supplied, then the current value is
68 preserved. Possible keywords:
69
70  :TRAPS
71    A list of the exception conditions that should cause traps.
72    Possible exceptions are :UNDERFLOW, :OVERFLOW, :INEXACT, :INVALID,
73   :DIVIDE-BY-ZERO, and on the X86 :DENORMALIZED-OPERAND.
74
75 :ROUNDING-MODE
76    The rounding mode to use when the result is not exact. Possible
77    values are :NEAREST, :POSITIVE-INFINITY, :NEGATIVE-INFINITY and
78    :ZERO.  Setting this away from :NEAREST is liable to upset SBCL's
79    maths routines which depend on it.
80
81 :CURRENT-EXCEPTIONS
82 :ACCRUED-EXCEPTIONS
83    These arguments allow setting of the exception flags. The main
84    use is setting the accrued exceptions to NIL to clear them.
85
86 :FAST-MODE
87    Set the hardware's \"fast mode\" flag, if any. When set, IEEE
88    conformance or debuggability may be impaired. Some machines don't
89    have this feature, and some SBCL ports don't implement it anyway
90    -- in such cases the value is always NIL.
91
92 :PRECISION (x86 only)
93   :24-bit, :53-bit and :64-bit, for the internal precision of the mantissa.
94
95 GET-FLOATING-POINT-MODES may be used to find the floating point modes
96 currently in effect. SAVE-LISP-AND-DIE preserves the floating point modes
97 in effect."
98   (let ((modes (floating-point-modes)))
99     (when traps-p
100       (setf (ldb float-traps-byte modes) (float-trap-mask traps)))
101     (when round-p
102       (setf (ldb float-rounding-mode modes)
103             (or (cdr (assoc rounding-mode *rounding-mode-alist*))
104                 (error "unknown rounding mode: ~S" rounding-mode))))
105     (when current-x-p
106       (setf (ldb float-exceptions-byte modes)
107             (float-trap-mask current-exceptions)))
108     (when accrued-x-p
109       (setf (ldb float-sticky-bits modes)
110             (float-trap-mask accrued-exceptions)))
111     (when fast-mode-p
112       (if fast-mode
113           (setq modes (logior float-fast-bit modes))
114           (setq modes (logand (lognot float-fast-bit) modes))))
115     #!+x86
116     (when precisionp
117       (setf (ldb float-precision-control modes)
118             (or (cdr (assoc precision *precision-mode-alist*))
119                 (error "unknown precision mode: ~S" precision))))
120     ;; FIXME: This apparently doesn't work on Darwin
121     #!-darwin (setf (floating-point-modes) modes))
122   (values))
123
124 (defun get-floating-point-modes ()
125   #!+sb-doc
126   "This function returns a list representing the state of the floating
127 point modes. The list is in the same format as the &KEY arguments to
128 SET-FLOATING-POINT-MODES, i.e.
129
130   (apply #'set-floating-point-modes (get-floating-point-modes))
131
132 sets the floating point modes to their current values (and thus is a no-op)."
133   (flet ((exc-keys (bits)
134            (macrolet ((frob ()
135                         `(collect ((res))
136                            ,@(mapcar (lambda (x)
137                                        `(when (logtest bits ,(cdr x))
138                                           (res ',(car x))))
139                                      *float-trap-alist*)
140                            (res))))
141              (frob))))
142     (let ((modes (floating-point-modes)))
143       `(:traps ,(exc-keys (ldb float-traps-byte modes))
144         :rounding-mode ,(car (rassoc (ldb float-rounding-mode modes)
145                                      *rounding-mode-alist*))
146         :current-exceptions ,(exc-keys (ldb float-exceptions-byte modes))
147         :accrued-exceptions ,(exc-keys (ldb float-sticky-bits modes))
148         :fast-mode ,(logtest float-fast-bit modes)
149         #!+x86 :precision
150         #!+x86 ,(car (rassoc (ldb float-precision-control modes)
151                              *precision-mode-alist*))))))
152
153 ;;; FIXME: For some unknown reason, NetBSD/x86 won't run with the
154 ;;; :INVALID trap enabled. That should be fixed, but not today...
155 ;;;
156 ;;; PRINT seems not to like x86 NPX denormal floats like
157 ;;; LEAST-NEGATIVE-SINGLE-FLOAT, so the :UNDERFLOW exceptions are
158 ;;; disabled by default. Joe User can explicitly enable them if
159 ;;; desired.
160 (defvar *saved-floating-point-modes*
161   '(:traps (:overflow #!-(or netbsd ppc) :invalid :divide-by-zero)
162     :rounding-mode :nearest :current-exceptions nil
163     :accrued-exceptions nil :fast-mode nil
164     #!+x86 :precision #!+x86 :53-bit))
165
166 (defun float-cold-init-or-reinit ()
167   (apply #'set-floating-point-modes *saved-floating-point-modes*))
168
169 (defun float-deinit ()
170   (setf *saved-floating-point-modes* (get-floating-point-modes)))
171
172 ;;; Return true if any of the named traps are currently trapped, false
173 ;;; otherwise.
174 (defmacro current-float-trap (&rest traps)
175   `(not (zerop (logand ,(dpb (float-trap-mask traps) float-traps-byte 0)
176                        (floating-point-modes)))))
177
178 ;;; SIGFPE code to floating-point error
179 #!-win32
180 (defparameter *sigfpe-code-error-alist*
181   (list (cons sb!unix::fpe-intovf 'floating-point-overflow)
182         (cons sb!unix::fpe-intdiv 'division-by-zero)
183         (cons sb!unix::fpe-fltdiv 'division-by-zero)
184         (cons sb!unix::fpe-fltovf 'floating-point-overflow)
185         (cons sb!unix::fpe-fltund 'floating-point-underflow)
186         (cons sb!unix::fpe-fltres 'floating-point-inexact)
187         (cons sb!unix::fpe-fltinv 'floating-point-invalid-operation)
188         (cons sb!unix::fpe-fltsub 'floating-point-exception)))
189
190 ;;; Signal the appropriate condition when we get a floating-point error.
191 #!-win32
192 (defun sigfpe-handler (signal info context)
193   (declare (ignore signal context))
194   (declare (type system-area-pointer info))
195   (let ((code (sb!unix::siginfo-code info)))
196     (with-interrupts
197         (error (or (cdr (assoc code *sigfpe-code-error-alist*))
198                    'floating-point-exception)))))
199
200 ;;; Execute BODY with the floating point exceptions listed in TRAPS
201 ;;; masked (disabled). TRAPS should be a list of possible exceptions
202 ;;; which includes :UNDERFLOW, :OVERFLOW, :INEXACT, :INVALID and
203 ;;; :DIVIDE-BY-ZERO and on the X86 :DENORMALIZED-OPERAND. The
204 ;;; respective accrued exceptions are cleared at the start of the body
205 ;;; to support their testing within, and restored on exit.
206 (defmacro with-float-traps-masked (traps &body body)
207   (let ((traps (dpb (float-trap-mask traps) float-traps-byte 0))
208         (exceptions (dpb (float-trap-mask traps) float-sticky-bits 0))
209         (trap-mask (dpb (lognot (float-trap-mask traps))
210                         float-traps-byte #xffffffff))
211         (exception-mask (dpb (lognot (float-trap-mask traps))
212                              float-sticky-bits #xffffffff))
213         (orig-modes (gensym)))
214     `(let ((,orig-modes (floating-point-modes)))
215       (unwind-protect
216            (progn
217              (setf (floating-point-modes)
218                    (logand ,orig-modes ,(logand trap-mask exception-mask)))
219              ,@body)
220         ;; Restore the original traps and exceptions.
221         (setf (floating-point-modes)
222               (logior (logand ,orig-modes ,(logior traps exceptions))
223                       (logand (floating-point-modes)
224                               ,(logand trap-mask exception-mask))))))))