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 7) ; 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 3) ; remove *4 fixnum bias
88 (inst imul y) ; result in edx:eax
89 (inst jmp :no OKAY) ; still fixnum
91 ;; zzz jrd changed edx to ebx in here, as edx isn't listed as a temp, above
92 ;; pfw says that loses big -- edx is target for arg x and result res
93 ;; note that 'edx' is not defined -- using x
94 (inst shrd rax x 3) ; high bits from edx
95 (inst sar x 3) ; now shift edx too
97 (move rcx x) ; save high bits from cqo
98 (inst cqo) ; edx:eax <- sign-extend of eax
100 (inst jmp :e SINGLE-WORD-BIGNUM)
102 (with-fixed-allocation (res bignum-widetag (+ bignum-digits-offset 2))
103 (storew rax res bignum-digits-offset other-pointer-lowtag)
104 (storew rcx res (1+ bignum-digits-offset) other-pointer-lowtag))
109 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
110 (storew rax res bignum-digits-offset other-pointer-lowtag))
119 (define-assembly-routine (generic-negate
121 (:return-style :full-call)
125 ((:arg x (descriptor-reg any-reg) rdx-offset)
126 (:res res (descriptor-reg any-reg) rdx-offset)
128 (:temp rax unsigned-reg rax-offset)
129 (:temp rcx unsigned-reg rcx-offset))
135 (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp n-word-bytes))
136 (inst sub rsp-tn (fixnumize 2))
138 (inst mov rcx (fixnumize 1)) ; arg count
139 (inst jmp (make-ea :qword
140 :disp (+ nil-value (static-fun-offset '%negate))))
144 (inst neg res) ; (- most-negative-fixnum) is BIGNUM
146 (inst shr res 3) ; sign bit is data - remove type bits
149 (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
150 (storew rcx res bignum-digits-offset other-pointer-lowtag))
156 (macrolet ((define-cond-assem-rtn (name translate static-fn test)
157 `(define-assembly-routine (,name
159 (:return-style :full-call)
161 (:translate ,translate)
163 ((:arg x (descriptor-reg any-reg) rdx-offset)
164 (:arg y (descriptor-reg any-reg) rdi-offset)
166 (:res res descriptor-reg rdx-offset)
168 (:temp eax unsigned-reg rax-offset)
169 (:temp ecx unsigned-reg rcx-offset))
171 ;; KLUDGE: The "3" here is a mask for the bits which will be
172 ;; zero in a fixnum. It should have a symbolic name. (Actually,
173 ;; it might already have a symbolic name which the coder
174 ;; couldn't be bothered to use..) -- WHN 19990917
176 (inst jmp :nz TAIL-CALL-TO-STATIC-FN)
178 (inst jmp :z INLINE-FIXNUM-COMPARE)
180 TAIL-CALL-TO-STATIC-FN
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 INLINE-FIXNUM-COMPARE
198 (inst mov res nil-value)
199 (inst jmp ,test RETURN-FALSE)
205 (define-cond-assem-rtn generic-< < two-arg-< :ge)
206 (define-cond-assem-rtn generic-> > two-arg-> :le))
208 (define-assembly-routine (generic-eql
210 (:return-style :full-call)
214 ((:arg x (descriptor-reg any-reg) rdx-offset)
215 (:arg y (descriptor-reg any-reg) rdi-offset)
217 (:res res descriptor-reg rdx-offset)
219 (:temp eax unsigned-reg rax-offset)
220 (:temp ecx unsigned-reg rcx-offset))
222 (inst jmp :e RETURN-T)
224 (inst jmp :z RETURN-NIL)
226 (inst jmp :nz DO-STATIC-FN)
229 (inst mov res nil-value)
235 (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp n-word-bytes))
236 (inst sub rsp-tn (fixnumize 2))
238 (inst mov ecx (fixnumize 2))
239 (inst jmp (make-ea :qword
240 :disp (+ nil-value (static-fun-offset 'eql))))
246 (define-assembly-routine (generic-=
248 (:return-style :full-call)
252 ((:arg x (descriptor-reg any-reg) rdx-offset)
253 (:arg y (descriptor-reg any-reg) rdi-offset)
255 (:res res descriptor-reg rdx-offset)
257 (:temp eax unsigned-reg rax-offset)
258 (:temp ecx unsigned-reg rcx-offset)
260 (inst test x 7) ; descriptor?
261 (inst jmp :nz DO-STATIC-FN) ; yes, do it here
262 (inst test y 7) ; descriptor?
263 (inst jmp :nz DO-STATIC-FN)
265 (inst jmp :e RETURN-T) ; ok
267 (inst mov res nil-value)
273 (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp n-word-bytes))
274 (inst sub rsp-tn (fixnumize 2))
276 (inst mov ecx (fixnumize 2))
277 (inst jmp (make-ea :qword
278 :disp (+ nil-value (static-fun-offset 'two-arg-=))))