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
43 ;; Same as: (inst enter (* n-word-bytes 1))
45 (inst mov rbp-tn rsp-tn)
46 (inst sub rsp-tn (* n-word-bytes 1))
47 (inst push (make-ea :qword :base rbp-tn
48 :disp (frame-byte-offset return-pc-save-offset)))
49 (inst mov rcx (fixnumize 2)) ; arg count
54 ',(symbolicate "TWO-ARG-" fun))))))))
57 (define-generic-arith-routine (+ 10)
61 ;; Unbox the overflowed result, recovering the correct sign from
62 ;; the carry flag, then re-box as a bignum.
64 ,@(when (> n-fixnum-tag-bits 1) ; don't shift by 0
65 '((inst sar res (1- n-fixnum-tag-bits))))
69 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
70 (storew rcx res bignum-digits-offset other-pointer-lowtag))
75 (define-generic-arith-routine (- 10)
79 ;; Unbox the overflowed result, recovering the correct sign from
80 ;; the carry flag, then re-box as a bignum.
81 (inst cmc) ; carry has correct sign now
83 ,@(when (> n-fixnum-tag-bits 1) ; don't shift by 0
84 '((inst sar res (1- n-fixnum-tag-bits))))
88 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
89 (storew rcx res bignum-digits-offset other-pointer-lowtag))
92 (define-generic-arith-routine (* 30)
93 (move rax x) ; must use eax for 64-bit result
94 (inst sar rax n-fixnum-tag-bits) ; remove *8 fixnum bias
95 (inst imul y) ; result in edx:eax
96 (inst jmp :no OKAY) ; still fixnum
98 (inst shrd rax x n-fixnum-tag-bits) ; high bits from edx
99 (inst sar x n-fixnum-tag-bits) ; now shift edx too
101 (move rcx x) ; save high bits from cqo
102 (inst cqo) ; edx:eax <- sign-extend of eax
104 (inst jmp :e SINGLE-WORD-BIGNUM)
106 (with-fixed-allocation (res bignum-widetag (+ bignum-digits-offset 2))
107 (storew rax res bignum-digits-offset other-pointer-lowtag)
108 (storew rcx res (1+ bignum-digits-offset) other-pointer-lowtag))
113 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
114 (storew rax res bignum-digits-offset other-pointer-lowtag))
123 (define-assembly-routine (generic-negate
125 (:return-style :full-call)
129 ((:arg x (descriptor-reg any-reg) rdx-offset)
130 (:res res (descriptor-reg any-reg) rdx-offset)
132 (:temp rax unsigned-reg rax-offset)
133 (:temp rcx unsigned-reg rcx-offset))
134 (inst test x fixnum-tag-mask)
138 (inst mov rbp-tn rsp-tn)
139 (inst sub rsp-tn (* n-word-bytes 1))
140 (inst push (make-ea :qword :base rbp-tn
141 :disp (frame-byte-offset return-pc-save-offset)))
142 (inst mov rcx (fixnumize 1)) ; arg count
143 (inst jmp (make-ea :qword
144 :disp (+ nil-value (static-fun-offset '%negate))))
148 (inst neg res) ; (- most-negative-fixnum) is BIGNUM
150 (inst shr res n-fixnum-tag-bits) ; sign bit is data - remove type bits
153 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
154 (storew rcx res bignum-digits-offset other-pointer-lowtag))
160 (macrolet ((define-cond-assem-rtn (name translate static-fn test)
161 (declare (ignorable translate static-fn))
163 `(define-assembly-routine (,name
164 (:return-style :none))
165 ((:arg x (descriptor-reg any-reg) rdx-offset)
166 (:arg y (descriptor-reg any-reg) rdi-offset)
168 (:temp rcx unsigned-reg rcx-offset))
172 (inst test rcx fixnum-tag-mask)
173 (inst jmp :nz DO-STATIC-FUN) ; are both fixnums?
179 (inst sub rsp-tn (* n-word-bytes 3))
180 (inst mov (make-ea :qword :base rsp-tn
181 :disp (frame-byte-offset
186 (inst lea rbp-tn (make-ea :qword :base rsp-tn
187 :disp (frame-byte-offset
191 (inst mov rcx (fixnumize 2))
192 (inst call (make-ea :qword
194 (static-fun-offset ',static-fn))))
195 ;; HACK: We depend on NIL having the lowest address of all
196 ;; static symbols (including T)
198 (:l `((inst mov y (1+ nil-value))
200 (:g `((inst cmp x (1+ nil-value)))))
204 (:translate ,translate)
207 (:args (x :scs (descriptor-reg any-reg) :target rdx)
208 (y :scs (descriptor-reg any-reg) :target rdi))
210 (:temporary (:sc unsigned-reg :offset rdx-offset
213 (:temporary (:sc unsigned-reg :offset rdi-offset
217 (:temporary (:sc unsigned-reg :offset rcx-offset
224 (inst lea rcx (make-ea :qword
225 :disp (make-fixup ',name :assembly-routine)))
228 (define-cond-assem-rtn generic-< < two-arg-< :l)
229 (define-cond-assem-rtn generic-> > two-arg-> :g))
232 (define-assembly-routine (generic-eql
233 (:return-style :none))
234 ((:arg x (descriptor-reg any-reg) rdx-offset)
235 (:arg y (descriptor-reg any-reg) rdi-offset)
237 (:temp rcx unsigned-reg rcx-offset))
241 (inst test rcx fixnum-tag-mask)
242 (inst jmp :nz DO-STATIC-FUN)
244 ;; At least one fixnum
249 (inst sub rsp-tn (* n-word-bytes 3))
250 (inst mov (make-ea :qword :base rsp-tn
251 :disp (frame-byte-offset
256 (inst lea rbp-tn (make-ea :qword :base rsp-tn
257 :disp (frame-byte-offset
261 (inst mov rcx (fixnumize 2))
262 (inst call (make-ea :qword
263 :disp (+ nil-value (static-fun-offset 'eql))))
269 (define-vop (generic-eql)
273 (:args (x :scs (descriptor-reg any-reg) :target rdx)
274 (y :scs (descriptor-reg any-reg) :target rdi))
276 (:temporary (:sc unsigned-reg :offset rdx-offset
279 (:temporary (:sc unsigned-reg :offset rdi-offset
283 (:temporary (:sc unsigned-reg :offset rcx-offset
290 (inst lea rcx (make-ea :qword
291 :disp (make-fixup 'generic-eql :assembly-routine)))
295 (define-assembly-routine (generic-=
296 (:return-style :none))
297 ((:arg x (descriptor-reg any-reg) rdx-offset)
298 (:arg y (descriptor-reg any-reg) rdi-offset)
300 (:temp rcx unsigned-reg rcx-offset))
303 (inst test rcx fixnum-tag-mask)
304 (inst jmp :nz DO-STATIC-FUN)
311 (inst sub rsp-tn (* n-word-bytes 3))
312 (inst mov (make-ea :qword :base rsp-tn
313 :disp (frame-byte-offset
318 (inst lea rbp-tn (make-ea :qword :base rsp-tn
319 :disp (frame-byte-offset
324 (inst mov rcx (fixnumize 2))
325 (inst call (make-ea :qword
326 :disp (+ nil-value (static-fun-offset 'two-arg-=))))
332 (define-vop (generic-=)
336 (:args (x :scs (descriptor-reg any-reg) :target rdx)
337 (y :scs (descriptor-reg any-reg) :target rdi))
339 (:temporary (:sc unsigned-reg :offset rdx-offset
342 (:temporary (:sc unsigned-reg :offset rdi-offset
346 (:temporary (:sc unsigned-reg :offset rcx-offset
353 (inst lea rcx (make-ea :qword
354 :disp (make-fixup 'generic-= :assembly-routine)))