|
|
|
|
|
by lispm
702 days ago
|
|
> Are you serious? This should be 1, max 2 instructions, with no branches and no memory use. Sure. ---> One could add two fixnums and get a bignum. CL-USER 31 > (fixnump (+ MOST-POSITIVE-FIXNUM MOST-POSITIVE-FIXNUM))
NIL
As you see here, adding two fixnums can have a result which is not a fixnum.Yes, Common Lisp does by default determine whether to return a bignum or fixnum. The machine code you've shown takes care of that. Let's see what the SBCL file compiler says: CL-USER> (compile-file "/tmp/test.lisp")
; compiling file "/tmp/test.lisp" (written 11 JUL 2024 10:02:04 PM):
; file: /tmp/test.lisp
; in: DEFUN FX-ADD
; (DEFUN FX-ADD (X Y)
; (DECLARE (OPTIMIZE (SPEED 3) (SAFETY 0) (DEBUG 0))
; (TYPE FIXNUM X Y))
; (+ X Y))
; --> SB-IMPL::%DEFUN SB-IMPL::%DEFUN SB-INT:NAMED-LAMBDA
; ==>
; #'(SB-INT:NAMED-LAMBDA FX-ADD
; (X Y)
; (DECLARE (SB-C::TOP-LEVEL-FORM))
; (DECLARE (OPTIMIZE (SPEED 3) (SAFETY 0) (DEBUG 0))
; (TYPE FIXNUM X Y))
; (BLOCK FX-ADD (+ X Y)))
;
; note: doing signed word to integer coercion (cost 20) to "<return value>"
;
; compilation unit finished
; printed 1 note
; wrote /tmp/test.fasl
; compilation finished in 0:00:00.031
#P"/private/tmp/test.fasl"
NIL
NIL
Well, the SBCL compiler does tell us that it can't optimize that. Isn't that nice?!If you want a fixnum to fixnum addition, then you need to tell the compiler that the result should be a fixnum. (the fixnum (+ x y))
Adding above and now the compiler no longer gives that efficiency hint: CL-USER> (compile-file "/tmp/test.lisp")
; compiling file "/tmp/test.lisp" (written 11 JUL 2024 10:03:11 PM):
; wrote /tmp/test.fasl
; compilation finished in 0:00:00.037
#P"/private/tmp/test.fasl"
NIL
NIL
|
|