possible to optimize `max` and similar functions

Bug #1823285 reported by Michael Fiano on 2019-04-05
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
Undecided
Unassigned

Bug Description

Just wondering if it would be a lot of effort to optimize `max` etc to work better on integral values.

CL-USER> (disassemble (lambda (x) (declare (fixnum x)) (max x 1)))
; disassembly for (LAMBDA (X))
; Size: 33 bytes. Origin: #x53860CCA
; CA: 498B5D10 MOV RBX, [R13+16] ; no-arg-parsing entry point
                                                              ; thread.binding-stack-pointer
; CE: 48895DF8 MOV [RBP-8], RBX
; D2: 488BD0 MOV RDX, RAX
; D5: 4883F802 CMP RAX, 2
; D9: 41BB02000000 MOV R11D, 2
; DF: 490F4CD3 CMOVL RDX, R11
; E3: 488BE5 MOV RSP, RBP
; E6: F8 CLC
; E7: 5D POP RBP
; E8: C3 RET
; E9: CC0F BREAK 15 ; Invalid argument count trap
NIL
CL-USER> (disassemble (lambda (x) (declare (fixnum x)) (if (plusp x) x 1)))
; disassembly for (LAMBDA (X))
; Size: 31 bytes. Origin: #x53860D6A
; 6A: 498B5D10 MOV RBX, [R13+16] ; no-arg-parsing entry point
                                                              ; thread.binding-stack-pointer
; 6E: 48895DF8 MOV [RBP-8], RBX
; 72: 488BD8 MOV RBX, RAX
; 75: 4885C0 TEST RAX, RAX
; 78: BA02000000 MOV EDX, 2
; 7D: 480F4FD3 CMOVNLE RDX, RBX
; 81: 488BE5 MOV RSP, RBP
; 84: F8 CLC
; 85: 5D POP RBP
; 86: C3 RET
; 87: CC0F BREAK 15 ; Invalid argument count trap
NIL

or slightly different with max speed:

CL-USER> (disassemble (lambda (x) (declare (fixnum x) (optimize speed)) (if (plusp x) x 1)))
; disassembly for (LAMBDA (X))
; Size: 23 bytes. Origin: #x53860E0B
; 0B: 488BC2 MOV RAX, RDX ; no-arg-parsing entry point
; 0E: 4885D2 TEST RDX, RDX
; 11: BA02000000 MOV EDX, 2
; 16: 480F4FD0 CMOVNLE RDX, RAX
; 1A: 488BE5 MOV RSP, RBP
; 1D: F8 CLC
; 1E: 5D POP RBP
; 1F: C3 RET
; 20: CC0F BREAK 15 ; Invalid argument count trap
NIL
CL-USER> (disassemble (lambda (x) (declare (fixnum x) (optimize speed)) (max x 1)))
; disassembly for (LAMBDA (X))
; Size: 28 bytes. Origin: #x53860E94
; 94: 488BC2 MOV RAX, RDX ; no-arg-parsing entry point
; 97: 4883FA02 CMP RDX, 2
; 9B: 488BD0 MOV RDX, RAX
; 9E: 41BB02000000 MOV R11D, 2
; A4: 490F4CD3 CMOVL RDX, R11
; A8: 488BE5 MOV RSP, RBP
; AB: F8 CLC
; AC: 5D POP RBP
; AD: C3 RET
; AE: CC0F BREAK 15 ; Invalid argument count trap
NIL

Stas Boukarev (stassats) wrote :

It's difficult to see what you're saying, you should use words.

Douglas Katzman (dougk) on 2019-04-16
Changed in sbcl:
status: New → Incomplete
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers