master: amr64: encode movi immediates without requiring manual shifting

stassats via Sbcl-commits <[email protected]> Thu, 18 Jun 2026 15:35:52 +0000
Newsgroups gmane.lisp.steel-bank.cvs
Message-ID <[email protected]>
The branch "master" has been updated in SBCL:
       via  19cf152c1d8a6af7c408a56874c9bd40d7b73af5 (commit)
      from  830f654b9346f8a3874022f4a364ae1fc9236730 (commit)

- Log -----------------------------------------------------------------
commit 19cf152c1d8a6af7c408a56874c9bd40d7b73af5
Author: Stas Boukarev <[email protected]>
Date:   Thu Jun 18 18:32:38 2026 +0300

    amr64: encode movi immediates without requiring manual shifting
---
 src/code/arm64-simd.lisp             |  6 ++---
 src/compiler/arm64/insts.lisp        | 50 ++++++++++++++++++++++++------------
 src/compiler/arm64/target-insts.lisp | 14 ++++++----
 3 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/src/code/arm64-simd.lisp b/src/code/arm64-simd.lisp
index 60bf61822..579cfcd67 100644
--- a/src/code/arm64-simd.lisp
+++ b/src/code/arm64-simd.lisp
@@ -1550,10 +1550,10 @@
           (inst movi errors 0 :16b)
 
           (inst movi c-7f  #x7F :4s)
-          (inst movi c-7ff #x7 :4s 8 t)
+          (inst movi c-7ff #x7FF :4s)
+          (inst movi c-ffff #x0000ffff0000ffff :2d)
+          (inst movi c-10ffff #x10ffff :4s)
           (inst movi c-1b  #x1b :4s)
-          (inst movi c-ffff #x00ffff0000ffff :2d)
-          (inst movi c-10ffff #x10 :4s 16 t)
 
           (inst b START)
 
diff --git a/src/compiler/arm64/insts.lisp b/src/compiler/arm64/insts.lisp
index b28a8e70a..eb8669f46 100644
--- a/src/compiler/arm64/insts.lisp
+++ b/src/compiler/arm64/insts.lisp
@@ -3604,7 +3604,7 @@
 
 (macrolet
     ((def (name o2 op)
-       `(define-instruction ,name (segment rd imm size &optional (shift 0) ones)
+       `(define-instruction ,name (segment rd imm size)
           (:printer simd-modified-imm ((o2 ,o2)
                                        (op ,op)))
           ,@(when (eq name 'movi)
@@ -3624,22 +3624,40 @@
                ((:8b :16b)
                 (setf cmode #b1110))
                ((:4h :8h)
-                (setf cmode (ecase shift
-                              (8 #b1010)
-                              (0 #b1000))))
+                (cond ((typep imm '(unsigned-byte 8))
+                       (setf cmode #b1000))
+                      ((and (typep imm '(unsigned-byte 16))
+                            (zerop (ldb (byte 8 0) imm)))
+                       (setf abc (ldb (byte 3 (+ 8 5)) imm)
+                             defgh (ldb (byte 5 8) imm))
+                       (setf cmode #b1010))
+                      (t
+                       (error "~x bad immediate" imm))))
                ((:2s :4s)
-                (if ones
-                    (setf cmode
-                          (ecase shift
-                            (8 #b1100)
-                            (16 #b1101)))
-                    (setf cmode
-                          (ash (ecase shift
-                                 (0 0)
-                                 (8 1)
-                                 (16 2)
-                                 (24 3))
-                               1))))
+                (or (cond ((typep imm '(unsigned-byte 8))
+                           (setf cmode #b0000))
+                          ((typep imm '(unsigned-byte 16))
+                           (let ((low (ldb (byte 8 0) imm)))
+                             (when (cond ((zerop low)
+                                          (setf cmode #b0010))
+                                         ((= low #xFF)
+                                          (setf cmode #b1100)))
+                               (setf abc (ldb (byte 3 (+ 8 5)) imm)
+                                     defgh (ldb (byte 5 8) imm)))))
+                          ((typep imm '(unsigned-byte 24))
+                           (let ((low (ldb (byte 16 0) imm)))
+                             (when (cond ((zerop low)
+                                          (setf cmode #b0100))
+                                         ((= low #xFFFF)
+                                          (setf cmode #b1101)))
+                               (setf abc (ldb (byte 3 (+ 16 5)) imm)
+                                     defgh (ldb (byte 5 16) imm)))))
+                          ((typep imm '(unsigned-byte 32))
+                           (when (zerop (ldb (byte 24 0) imm))
+                             (setf abc (ldb (byte 3 (+ 24 5)) imm)
+                                   defgh (ldb (byte 5 24) imm)
+                                   cmode #b0110))))
+                    (error "~x bad immediate" imm)))
                ((:2d)
                 (let ((a (the (member 255 0) (ldb (byte 8 56) imm)))
                       (b (the (member 255 0) (ldb (byte 8 48) imm)))
diff --git a/src/compiler/arm64/target-insts.lisp b/src/compiler/arm64/target-insts.lisp
index 8eff853fc..517eecacd 100644
--- a/src/compiler/arm64/target-insts.lisp
+++ b/src/compiler/arm64/target-insts.lisp
@@ -344,7 +344,8 @@
                    (if (zerop q)
                        "4H"
                        "8H"))
-                  ((zerop (logand cmode #b1001))
+                  ((or (zerop (logand cmode #b1001))
+                       (= (ldb (byte 3 13) cmode) #b110))
                    (if (zerop q)
                        "2S"
                        "4S"))))))
@@ -378,12 +379,15 @@
                   (t 0)))
           (msl
             (cond ((= (ldb (byte 3 1) cmode) #b110)
-                   (ash 8 (ldb (byte 1 0) cmode))))))
-      (princ (dpb abc (byte 3 5) defgh) stream)
+                   (ash 8 (ldb (byte 1 0) cmode)))))
+          (value (dpb abc (byte 3 5) defgh)))
       (cond (msl
-             (format stream ", MSL #~d" msl))
+             (setf value
+                   (logior (ash value msl)
+                           (ldb (byte msl 0) -1))))
             ((plusp shift)
-             (format stream ", LSL #~d" shift))))))
+             (setf value (ash value shift))))
+      (format stream "~x" value))))
 
 (defun print-64-bit-modified-imm (value stream dstate)
   (declare (ignore dstate))

-----------------------------------------------------------------------


hooks/post-receive
-- 
SBCL