From edb5af9baaa3b4ba86e6eb859a12873e708216ec Mon Sep 17 00:00:00 2001
From: Raymond Toy <toy.raymond@gmail.com>
Date: Tue, 29 Dec 2015 18:24:24 -0800
Subject: [PATCH] WITH-FLOAT-TRAPS-ENABLED was incorrectly setting accrued
 exceptions.

Fix issue #14.

WITH-FLOAT-TRAPS-ENABLED was leaving the accrued (and current)
exceptions unchanged, but it should have cleared out any values there
that matched the exceptions to be enabled.  Without this, the next x87
operation would signal an exception if an accrued exception matched
an enabled exception.  This was the cause of issue #14.  (Note that
for x87, the accrued exception is the same as current exception.)
---
 src/code/float-trap.lisp | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/code/float-trap.lisp b/src/code/float-trap.lisp
index 611feba54..8dbd3c31d 100644
--- a/src/code/float-trap.lisp
+++ b/src/code/float-trap.lisp
@@ -417,11 +417,14 @@
 			code)))))))
 
 (macrolet
-    ((with-float-traps (name logical-op docstring)
+    ((with-float-traps (name merge-traps docstring)
        ;; Define macros to enable or disable floating-point
        ;; exceptions.  Masked exceptions and enabled exceptions only
        ;; differ whether we AND in the bits or OR them, respectively.
-       ;; Logical-op is the operation to use.
+       ;; MERGE-TRAPS is the logical operation to merge the traps with
+       ;; the current floating-point mode.  Thus, use  and MERGE-EXCEPTIONS is the
+       ;; logical operation to merge the exceptions (sticky bits) with
+       ;; the current mode.
        (let ((macro-name (symbolicate "WITH-FLOAT-TRAPS-" name)))
 	 `(progn
 	    (defmacro ,macro-name (traps &body body)
@@ -450,7 +453,8 @@
 			(progn
 			  (setf (floating-point-modes)
 				(ldb (byte 32 0)
-				     (,',logical-op ,orig-modes ,(logand trap-mask exception-mask))))
+				     (logand (,',merge-traps ,orig-modes ,trap-mask)
+					     ,exception-mask)))
 			  ,@body)
 		     ;; Restore the original traps and exceptions.
 		     (setf (floating-point-modes)
-- 
GitLab