Unboxed 32-bit arithmetic on 64-bit processor
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-
(defun func32 (x y z)
(declare (type (unsigned-byte 32) x y z)
(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-
:ASDF3 :ASDF2 :ASDF :OS-UNIX :NON-BASE-
:64-BIT-REGISTERS :ALIEN-CALLBACKS :ANSI-CL :ASH-RIGHT-VOPS
:C-STACK-
:COMPLEX-
:FP-AND-
:INTEGER-EQL-VOP :LARGEFILE :LINKAGE-TABLE :LINUX :LITTLE-ENDIAN
:MEMORY-
:OS-PROVIDES-
:OS-PROVIDES-POLL :OS-PROVIDES-PUTWC :OS-PROVIDES-
:PACKAGE-
:SB-CORE-
:SB-SIMD-PACK :SB-SOURCE-
:STACK-
:STACK-
:STACK-
:UNWIND-
Changed in sbcl: | |
importance: | Undecided → Wishlist |