1 ;;;; simple cases for generic arithmetic
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 ;;;; addition, subtraction, and multiplication
16 (macrolet ((define-generic-arith-routine ((fun cost) &body body)
17 `(define-assembly-routine (,(symbolicate "GENERIC-" fun)
19 (:return-style :full-call)
23 ((:arg x (descriptor-reg any-reg) rdx-offset)
24 (:arg y (descriptor-reg any-reg)
25 ;; this seems wrong esi-offset -- FIXME: What's it mean?
28 (:res res (descriptor-reg any-reg) rdx-offset)
30 (:temp rax unsigned-reg rax-offset)
31 (:temp rcx unsigned-reg rcx-offset))
35 (inst test rcx fixnum-tag-mask) ; both fixnums?
36 (inst jmp :nz DO-STATIC-FUN) ; no - do generic
39 (inst clc) ; single-value return
44 (inst lea rbp-tn (make-ea :qword
46 :disp (* 2 n-word-bytes)))
47 (inst sub rsp-tn (fixnumize 1))
48 (inst push (make-ea :qword :base rbp-tn :disp (- n-word-bytes)))
49 (inst mov rcx (fixnumize 2)) ; arg count
54 ',(symbolicate "TWO-ARG-" fun))))))))
56 (define-generic-arith-routine (+ 10)
60 (inst rcr res 1) ; carry has correct sign
61 (inst sar res 2) ; remove type bits
65 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
66 (storew rcx res bignum-digits-offset other-pointer-lowtag))
70 (define-generic-arith-routine (- 10)
74 (inst cmc) ; carry has correct sign now
76 (inst sar res 2) ; remove type bits
80 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
81 (storew rcx res bignum-digits-offset other-pointer-lowtag))
84 (define-generic-arith-routine (* 30)
85 (move rax x) ; must use eax for 64-bit result
86 (inst sar rax n-fixnum-tag-bits) ; remove *8 fixnum bias
87 (inst imul y) ; result in edx:eax
88 (inst jmp :no OKAY) ; still fixnum
90 (inst shrd rax x n-fixnum-tag-bits) ; high bits from edx
91 (inst sar x n-fixnum-tag-bits) ; now shift edx too
93 (move rcx x) ; save high bits from cqo
94 (inst cqo) ; edx:eax <- sign-extend of eax
96 (inst jmp :e SINGLE-WORD-BIGNUM)
98 (with-fixed-allocation (res bignum-widetag (+ bignum-digits-offset 2))
99 (storew rax res bignum-digits-offset other-pointer-lowtag)
100 (storew rcx res (1+ bignum-digits-offset) other-pointer-lowtag))
105 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
106 (storew rax res bignum-digits-offset other-pointer-lowtag))
115 (define-assembly-routine (generic-negate
117 (:return-style :full-call)
121 ((:arg x (descriptor-reg any-reg) rdx-offset)
122 (:res res (descriptor-reg any-reg) rdx-offset)
124 (:temp rax unsigned-reg rax-offset)
125 (:temp rcx unsigned-reg rcx-offset))
126 (inst test x fixnum-tag-mask)
130 (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp (* 2 n-word-bytes)))
131 (inst sub rsp-tn (fixnumize 1))
132 (inst push (make-ea :qword :base rbp-tn :disp (- n-word-bytes)))
133 (inst mov rcx (fixnumize 1)) ; arg count
134 (inst jmp (make-ea :qword
135 :disp (+ nil-value (static-fun-offset '%negate))))
139 (inst neg res) ; (- most-negative-fixnum) is BIGNUM
141 (inst shr res n-fixnum-tag-bits) ; sign bit is data - remove type bits
144 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
145 (storew rcx res bignum-digits-offset other-pointer-lowtag))
151 (macrolet ((define-cond-assem-rtn (name translate static-fn test)
152 (declare (ignorable translate static-fn))
154 `(define-assembly-routine (,name
155 (:return-style :none))
156 ((:arg x (descriptor-reg any-reg) rdx-offset)
157 (:arg y (descriptor-reg any-reg) rdi-offset)
159 (:temp rcx unsigned-reg rcx-offset))
163 (inst test rcx fixnum-tag-mask)
164 (inst jmp :nz DO-STATIC-FUN) ; are both fixnums?
171 (inst sub rsp-tn (fixnumize 3))
172 (inst mov (make-ea :qword
174 :disp (frame-byte-offset ocfp-save-offset))
177 (inst mov rcx (fixnumize 2))
178 (inst call (make-ea :qword
180 (static-fun-offset ',static-fn))))
181 ;; HACK: We depend on NIL having the lowest address of all
182 ;; static symbols (including T)
184 (:l `((inst mov y (1+ nil-value))
186 (:g `((inst cmp x (1+ nil-value)))))
190 (:translate ,translate)
193 (:args (x :scs (descriptor-reg any-reg) :target rdx)
194 (y :scs (descriptor-reg any-reg) :target rdi))
196 (:temporary (:sc unsigned-reg :offset rdx-offset
199 (:temporary (:sc unsigned-reg :offset rdi-offset
203 (:temporary (:sc unsigned-reg :offset rcx-offset
210 (inst lea rcx (make-ea :qword
211 :disp (make-fixup ',name :assembly-routine)))
214 (define-cond-assem-rtn generic-< < two-arg-< :l)
215 (define-cond-assem-rtn generic-> > two-arg-> :g))
218 (define-assembly-routine (generic-eql
219 (:return-style :none))
220 ((:arg x (descriptor-reg any-reg) rdx-offset)
221 (:arg y (descriptor-reg any-reg) rdi-offset)
223 (:temp rcx unsigned-reg rcx-offset))
227 (inst test rcx fixnum-tag-mask)
228 (inst jmp :nz DO-STATIC-FUN)
230 ;; At least one fixnum
236 (inst sub rsp-tn (fixnumize 3))
237 (inst mov (make-ea :qword
239 :disp (frame-byte-offset ocfp-save-offset))
242 (inst mov rcx (fixnumize 2))
243 (inst call (make-ea :qword
244 :disp (+ nil-value (static-fun-offset 'eql))))
250 (define-vop (generic-eql)
254 (:args (x :scs (descriptor-reg any-reg) :target rdx)
255 (y :scs (descriptor-reg any-reg) :target rdi))
257 (:temporary (:sc unsigned-reg :offset rdx-offset
260 (:temporary (:sc unsigned-reg :offset rdi-offset
264 (:temporary (:sc unsigned-reg :offset rcx-offset
271 (inst lea rcx (make-ea :qword
272 :disp (make-fixup 'generic-eql :assembly-routine)))
276 (define-assembly-routine (generic-=
277 (:return-style :none))
278 ((:arg x (descriptor-reg any-reg) rdx-offset)
279 (:arg y (descriptor-reg any-reg) rdi-offset)
281 (:temp rcx unsigned-reg rcx-offset))
284 (inst test rcx fixnum-tag-mask)
285 (inst jmp :nz DO-STATIC-FUN)
293 (inst sub rsp-tn (fixnumize 3))
294 (inst mov (make-ea :qword
296 :disp (frame-byte-offset ocfp-save-offset))
299 (inst mov rcx (fixnumize 2))
300 (inst call (make-ea :qword
301 :disp (+ nil-value (static-fun-offset 'two-arg-=))))
307 (define-vop (generic-=)
311 (:args (x :scs (descriptor-reg any-reg) :target rdx)
312 (y :scs (descriptor-reg any-reg) :target rdi))
314 (:temporary (:sc unsigned-reg :offset rdx-offset
317 (:temporary (:sc unsigned-reg :offset rdi-offset
321 (:temporary (:sc unsigned-reg :offset rcx-offset
328 (inst lea rcx (make-ea :qword
329 :disp (make-fixup 'generic-= :assembly-routine)))