Fix inline fixnum LDB on PowerPC for certain bytespecs
authorDavid Lichteblau <david@lichteblau.com>
Thu, 7 Jun 2012 18:08:34 +0000 (20:08 +0200)
committerDavid Lichteblau <david@lichteblau.com>
Thu, 7 Jun 2012 21:47:05 +0000 (23:47 +0200)
Previously, the inline fixnum version of (ldb (byte 8 24) -1) would
return #x3f instead of #ff.

src/compiler/ppc/arith.lisp
tests/arith.impure.lisp

index 6e90c89..5f4bfe1 100644 (file)
   (:result-types tagged-num)
   (:policy :fast-safe)
   (:generator 2
-    (inst rlwinm res x
-          (mod (- 32 posn) 32)          ; effectively rotate right
-          (- 32 size n-fixnum-tag-bits)
-          (- 31 n-fixnum-tag-bits))))
+    (let ((phantom-bits (- (+ size posn) 30)))
+      (cond
+        ((plusp phantom-bits)
+         ;; The byte to be loaded into RES includes sign bits which are not
+         ;; present in the input X physically.  RLWINM as used below would
+         ;; mask these out with 0 even for negative inputs.
+         (inst srawi res x phantom-bits)
+         (inst rlwinm res x
+               (mod (- 32 posn (- phantom-bits)) 32)
+               (- 32 size n-fixnum-tag-bits)
+               (- 31 n-fixnum-tag-bits)))
+        (t
+         (inst rlwinm res x
+               (mod (- 32 posn) 32)     ; effectively rotate right
+               (- 32 size n-fixnum-tag-bits)
+               (- 31 n-fixnum-tag-bits)))))))
 
 (define-vop (ldb-c/signed)
   (:translate %%ldb)
index 00479d4..71b5734 100644 (file)
 (assert (= (64-bit-logcount (1- (ash 1 48))) 48))
 (assert (= (64-bit-logcount (1- (ash 1 54))) 54))
 \f
+(declaim (inline ppc-ldb-2))
+
+(defun ppc-ldb-2 (fun value)
+  (declare (type stream socket)
+           (type (signed-byte 32) value)
+           (optimize (speed 3) (safety 0) (space 1) (debug 1)
+                     (compilation-speed 0)))
+  (funcall fun (ldb (byte 8 24) value))
+  (funcall fun (ldb (byte 8 16) value))
+  (funcall fun (ldb (byte 8 8) value))
+  (funcall fun (ldb (byte 8 0) value))
+  (values))
+
+(defun ppc-ldb-1 (fun)
+  (declare (optimize (speed 3) (safety 0) (space 1) (debug 1)
+                     (compilation-speed 0)))
+  (loop
+     for param :across (make-array 1 :initial-element nil)
+     for size :across (make-array 1 :element-type 'fixnum :initial-element 3)
+     do (ppc-ldb-2 fun (if param size -1))))
+
+(let ((acc '()))
+  (ppc-ldb-1 (lambda (x)
+         (push x acc)))
+  (assert (equal acc '(#xff #xff #xff #xff))))
+\f