Unboxed 32-bit arithmetic on 64-bit processor

Bug #1629602 reported by Guillaume LE VAILLANT
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
New
Wishlist
Unassigned

Bug Description

When calling a sequence of arithmetic functions working on a few 32-bit unsigned integers on a 64-bit processor, the integers are not unboxed.

(require 'sb-rotate-byte)

(declaim (inline mod32+ rol32)
         (ftype (function ((unsigned-byte 32) (unsigned-byte 32)) (unsigned-byte 32)) mod32+)
         (ftype (function ((unsigned-byte 32) (unsigned-byte 5)) (unsigned-byte 32)) rol32))

(defun mod32+ (a b)
  (declare (type (unsigned-byte 32) a b))
  (ldb (byte 32 0) (+ a b)))

(defun rol32 (a s)
  (declare (type (unsigned-byte 32) a) (type (integer 0 32) s))
  (sb-rotate-byte:rotate-byte s (byte 32 0) a))

(defun func32 (x y z)
  (declare (type (unsigned-byte 32) x y z)
           (optimize (speed 3) (space 0) (safety 0) (debug 0)))
  (mod32+ (rol32 (mod32+ x y) 5) z))

(disassemble 'func32)
; disassembly for FUNC32
; Size: 38 bytes. Origin: #x1003847822
; 22: 4801F9 ADD RCX, RDI ; no-arg-parsing entry point
; 25: 48230DA4FFFFFF AND RCX, [RIP-92] ; [#x10038477D0] = 1FFFFFFFE
; 2C: 48D1F9 SAR RCX, 1
; 2F: C1C105 ROL ECX, 5
; 32: 48D1E1 SHL RCX, 1
; 35: 4801F1 ADD RCX, RSI
; 38: 48230D91FFFFFF AND RCX, [RIP-111] ; [#x10038477D0] = 1FFFFFFFE
; 3F: 488BD1 MOV RDX, RCX
; 42: 488BE5 MOV RSP, RBP
; 45: F8 CLC
; 46: 5D POP RBP
; 47: C3 RET

Unboxing the integers would allow getting rid of "AND RCX #x1FFFFFFFE", "SAR RCX 1" and "SHL RCX 1". In functions where a lot of arithmetic instructions are applied to a few integers (like hash functions or ciphers), it could divide the number of assembly instructions by 2 or 3.

$ uname -a
Linux 4.7.3-gentoo #1 SMP Fri Sep 9 11:21:14 CEST 2016 x86_64 Intel(R) Core(TM) i7-3630QM CPU @ 2.40GHz GenuineIntel GNU/Linux

$ sbcl --version
SBCL 1.3.9

*features*
(:SWANK :QUICKLISP :SB-BSD-SOCKETS-ADDRINFO :ASDF-PACKAGE-SYSTEM :ASDF3.1
 :ASDF3 :ASDF2 :ASDF :OS-UNIX :NON-BASE-CHARS-EXIST-P :ASDF-UNICODE :64-BIT
 :64-BIT-REGISTERS :ALIEN-CALLBACKS :ANSI-CL :ASH-RIGHT-VOPS
 :C-STACK-IS-CONTROL-STACK :COMMON-LISP :COMPARE-AND-SWAP-VOPS
 :COMPLEX-FLOAT-VOPS :CYCLE-COUNTER :ELF :FLOAT-EQL-VOPS
 :FP-AND-PC-STANDARD-SAVE :GENCGC :IEEE-FLOATING-POINT :INLINE-CONSTANTS
 :INTEGER-EQL-VOP :LARGEFILE :LINKAGE-TABLE :LINUX :LITTLE-ENDIAN
 :MEMORY-BARRIER-VOPS :MULTIPLY-HIGH-VOPS :OS-PROVIDES-BLKSIZE-T
 :OS-PROVIDES-DLADDR :OS-PROVIDES-DLOPEN :OS-PROVIDES-GETPROTOBY-R
 :OS-PROVIDES-POLL :OS-PROVIDES-PUTWC :OS-PROVIDES-SUSECONDS-T
 :PACKAGE-LOCAL-NICKNAMES :PRECISE-ARG-COUNT-ERROR :RAW-INSTANCE-INIT-VOPS
 :SB-CORE-COMPRESSION :SB-DOC :SB-EVAL :SB-FUTEX :SB-LDB :SB-PACKAGE-LOCKS
 :SB-SIMD-PACK :SB-SOURCE-LOCATIONS :SB-THREAD :SB-UNICODE :SBCL
 :STACK-ALLOCATABLE-CLOSURES :STACK-ALLOCATABLE-FIXED-OBJECTS
 :STACK-ALLOCATABLE-LISTS :STACK-ALLOCATABLE-VECTORS
 :STACK-GROWS-DOWNWARD-NOT-UPWARD :SYMBOL-INFO-VOPS :UNBIND-N-VOP :UNIX
 :UNWIND-TO-FRAME-AND-CALL-VOP :X86-64)

Stas Boukarev (stassats)
Changed in sbcl:
importance: Undecided → Wishlist
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.