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
47 (make-ea :qword :base rsp-tn :disp n-word-bytes))
48 (inst sub rsp-tn (fixnumize 2))
49 (inst push rax) ; callers return addr
50 (inst mov rcx (fixnumize 2)) ; arg count
55 ',(symbolicate "TWO-ARG-" fun))))))))
57 (define-generic-arith-routine (+ 10)
61 (inst rcr res 1) ; carry has correct sign
62 (inst sar res 2) ; remove type bits
66 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
67 (storew rcx res bignum-digits-offset other-pointer-lowtag))
71 (define-generic-arith-routine (- 10)
75 (inst cmc) ; carry has correct sign now
77 (inst sar res 2) ; remove type bits
81 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
82 (storew rcx res bignum-digits-offset other-pointer-lowtag))
85 (define-generic-arith-routine (* 30)
86 (move rax x) ; must use eax for 64-bit result
87 (inst sar rax n-fixnum-tag-bits) ; remove *8 fixnum bias
88 (inst imul y) ; result in edx:eax
89 (inst jmp :no OKAY) ; still fixnum
91 (inst shrd rax x n-fixnum-tag-bits) ; high bits from edx
92 (inst sar x n-fixnum-tag-bits) ; now shift edx too
94 (move rcx x) ; save high bits from cqo
95 (inst cqo) ; edx:eax <- sign-extend of eax
97 (inst jmp :e SINGLE-WORD-BIGNUM)
99 (with-fixed-allocation (res bignum-widetag (+ bignum-digits-offset 2))
100 (storew rax res bignum-digits-offset other-pointer-lowtag)
101 (storew rcx res (1+ bignum-digits-offset) other-pointer-lowtag))
106 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
107 (storew rax res bignum-digits-offset other-pointer-lowtag))
116 (define-assembly-routine (generic-negate
118 (:return-style :full-call)
122 ((:arg x (descriptor-reg any-reg) rdx-offset)
123 (:res res (descriptor-reg any-reg) rdx-offset)
125 (:temp rax unsigned-reg rax-offset)
126 (:temp rcx unsigned-reg rcx-offset))
127 (inst test x fixnum-tag-mask)
132 (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp n-word-bytes))
133 (inst sub rsp-tn (fixnumize 2))
135 (inst mov rcx (fixnumize 1)) ; arg count
136 (inst jmp (make-ea :qword
137 :disp (+ nil-value (static-fun-offset '%negate))))
141 (inst neg res) ; (- most-negative-fixnum) is BIGNUM
143 (inst shr res n-fixnum-tag-bits) ; sign bit is data - remove type bits
146 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
147 (storew rcx res bignum-digits-offset other-pointer-lowtag))
153 (macrolet ((define-cond-assem-rtn (name translate static-fn test)
154 `(define-assembly-routine (,name
156 (:return-style :full-call)
158 (:translate ,translate)
160 ((:arg x (descriptor-reg any-reg) rdx-offset)
161 (:arg y (descriptor-reg any-reg) rdi-offset)
163 (:res res descriptor-reg rdx-offset)
165 (:temp eax unsigned-reg rax-offset)
166 (:temp ecx unsigned-reg rcx-offset))
170 (inst test ecx fixnum-tag-mask)
171 (inst jmp :nz DO-STATIC-FUN)
175 (inst mov eax nil-value)
176 (inst cmov ,test res eax)
177 (inst clc) ; single-value return
183 (inst lea rbp-tn (make-ea :qword
186 (inst sub rsp-tn (fixnumize 2)) ; FIXME: Push 2 words on stack,
189 (inst mov ecx (fixnumize 2)) ; FIXME: FIXNUMIZE and
190 ; SINGLE-FLOAT-BITS are parallel,
191 ; should be named parallelly.
192 (inst jmp (make-ea :qword
194 (static-fun-offset ',static-fn)))))))
196 (define-cond-assem-rtn generic-< < two-arg-< :ge)
197 (define-cond-assem-rtn generic-> > two-arg-> :le))
199 (define-assembly-routine (generic-eql
201 (:return-style :full-call)
205 ((:arg x (descriptor-reg any-reg) rdx-offset)
206 (:arg y (descriptor-reg any-reg) rdi-offset)
208 (:res res descriptor-reg rdx-offset)
210 (:temp rax unsigned-reg rax-offset)
211 (:temp rcx unsigned-reg rcx-offset))
214 (inst test rcx fixnum-tag-mask)
215 (inst jmp :nz DO-STATIC-FUN)
217 ;; At least one fixnum
220 (inst mov rax nil-value)
221 (inst cmov :ne res rax)
228 (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp n-word-bytes))
229 (inst sub rsp-tn (fixnumize 2))
231 (inst mov rcx (fixnumize 2))
232 (inst jmp (make-ea :qword
233 :disp (+ nil-value (static-fun-offset 'eql)))))
235 (define-assembly-routine (generic-=
237 (:return-style :full-call)
241 ((:arg x (descriptor-reg any-reg) rdx-offset)
242 (:arg y (descriptor-reg any-reg) rdi-offset)
244 (:res res descriptor-reg rdx-offset)
246 (:temp rax unsigned-reg rax-offset)
247 (:temp rcx unsigned-reg rcx-offset))
250 (inst test rcx fixnum-tag-mask)
251 (inst jmp :nz DO-STATIC-FUN)
256 (inst mov rax nil-value)
257 (inst cmov :ne res rax)
264 (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp n-word-bytes))
265 (inst sub rsp-tn (fixnumize 2))
267 (inst mov rcx (fixnumize 2))
268 (inst jmp (make-ea :qword
269 :disp (+ nil-value (static-fun-offset 'two-arg-=)))))