From 14dde89d91420d03a37e6b03f2e41e830218b736 Mon Sep 17 00:00:00 2001
From: Paul Khuong <pvk@pvk.ca>
Date: Sun, 19 May 2013 23:47:13 -0400
Subject: [PATCH] Take bitwidth into account in BOOLEAN alien type

Some ABIs (x86/x86-64) allow garbage in unused upper bits of return
values; take that into account when converting BOOLEAN return types
to CL BOOLEANs.
---
 NEWS                         |    2 ++
 src/code/host-alieneval.lisp |    6 ++++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 2b413ca..dd9b144 100644
--- a/NEWS
+++ b/NEWS
@@ -55,6 +55,8 @@ changes relative to sbcl-1.1.7:
     modular arithemtic (mostly to exploit fixnum-width VOPs). (lp#1026634)
   * bug fix: a combination of inlined local function with &optional and
     recursion no longer causes undescriptive compiler errors. (lp#1180992)
+  * bug fix: sub-word BOOLEAN alien types now disregard higher order bits
+    when testing for non-zero-ness.
   * optimization: faster ISQRT on fixnums and small bignums
   * optimization: faster and smaller INTEGER-LENGTH on fixnums on x86-64.
   * optimization: On x86-64, the number of multi-byte NOP instructions used
diff --git a/src/code/host-alieneval.lisp b/src/code/host-alieneval.lisp
index 8e61909..c78853f 100644
--- a/src/code/host-alieneval.lisp
+++ b/src/code/host-alieneval.lisp
@@ -666,8 +666,10 @@
   `(member t nil))
 
 (define-alien-type-method (boolean :naturalize-gen) (type alien)
-  (declare (ignore type))
-  `(not (zerop ,alien)))
+  (let ((bits (alien-boolean-type-bits type)))
+    (if (= bits sb!vm:n-word-bits)
+        `(not (zerop ,alien))
+        `(logtest ,alien ,(ldb (byte bits 0) -1)))))
 
 (define-alien-type-method (boolean :deport-gen) (type value)
   (declare (ignore type))
-- 
1.7.10.4