0.8.21.42:
[sbcl.git] / src / assembly / x86-64 / arith.lisp
1 ;;;; simple cases for generic arithmetic
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; addition, subtraction, and multiplication
15
16 (macrolet ((define-generic-arith-routine ((fun cost) &body body)
17              `(define-assembly-routine (,(symbolicate "GENERIC-" fun)
18                                         (:cost ,cost)
19                                         (:return-style :full-call)
20                                         (:translate ,fun)
21                                         (:policy :safe)
22                                         (:save-p t))
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?
26                        rdi-offset)
27
28                  (:res res (descriptor-reg any-reg) rdx-offset)
29
30                  (:temp rax unsigned-reg rax-offset)
31                  (:temp rbx unsigned-reg rbx-offset)
32                  (:temp rcx unsigned-reg rcx-offset))
33
34                 (declare (ignorable rbx))
35
36                 (inst test x 7)  ; fixnum?
37                 (inst jmp :nz DO-STATIC-FUN) ; no - do generic
38                 (inst test y 7)  ; fixnum?
39                 (inst jmp :z DO-BODY)   ; yes - doit here
40
41                 DO-STATIC-FUN
42                 (inst pop rax)
43                 (inst push rbp-tn)
44                 (inst lea
45                       rbp-tn
46                       (make-ea :qword :base rsp-tn :disp n-word-bytes))
47                 (inst sub rsp-tn (fixnumize 2))
48                 (inst push rax)  ; callers return addr
49                 (inst mov rcx (fixnumize 2)) ; arg count
50                 (inst jmp
51                       (make-ea :qword
52                                :disp (+ nil-value
53                                         (static-fun-offset
54                                          ',(symbolicate "TWO-ARG-" fun)))))
55
56                 DO-BODY
57                 ,@body)))
58
59   (define-generic-arith-routine (+ 10)
60     (move res x)
61     (inst add res y)
62     (inst jmp :no OKAY)
63     (inst rcr res 1)                  ; carry has correct sign
64     (inst sar res 2)                  ; remove type bits
65
66     (move rcx res)
67
68     (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
69       (storew rcx res bignum-digits-offset other-pointer-lowtag))
70
71     OKAY)
72
73   (define-generic-arith-routine (- 10)
74     ;; FIXME: This is screwed up.
75       ;;; I can't figure out the flags on subtract. Overflow never gets
76       ;;; set and carry always does. (- 0 most-negative-fixnum) can't be
77       ;;; easily detected so just let the upper level stuff do it.
78     (inst jmp DO-STATIC-FUN)
79
80     (move res x)
81     (inst sub res y)
82     (inst jmp :no OKAY)
83     (inst rcr res 1)
84     (inst sar res 2)                  ; remove type bits
85
86     (move rcx res)
87
88     (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
89       (storew rcx res bignum-digits-offset other-pointer-lowtag))
90     OKAY)
91
92   (define-generic-arith-routine (* 30)
93     (move rax x)                   ; must use eax for 64-bit result
94     (inst sar rax 3)               ; remove *4 fixnum bias
95     (inst imul y)                  ; result in edx:eax
96     (inst jmp :no okay)            ; still fixnum
97
98     ;; zzz jrd changed edx to ebx in here, as edx isn't listed as a temp, above
99     ;;     pfw says that loses big -- edx is target for arg x and result res
100     ;;     note that 'edx' is not defined -- using x
101     (inst shrd rax x 3)            ; high bits from edx
102     (inst sar x 3)                 ; now shift edx too
103
104     (move rcx x)                   ; save high bits from cqo
105     (inst cqo)                     ; edx:eax <- sign-extend of eax
106     (inst cmp x rcx)
107     (inst jmp :e SINGLE-WORD-BIGNUM)
108
109     (with-fixed-allocation (res bignum-widetag (+ bignum-digits-offset 2))
110       (storew rax res bignum-digits-offset other-pointer-lowtag)
111       (storew rcx res (1+ bignum-digits-offset) other-pointer-lowtag))
112     (inst jmp DONE)
113
114     SINGLE-WORD-BIGNUM
115
116     (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
117       (storew rax res bignum-digits-offset other-pointer-lowtag))
118     (inst jmp DONE)
119
120     OKAY
121     (move res rax)
122     DONE))
123 \f
124 ;;;; negation
125
126 (define-assembly-routine (generic-negate
127                           (:cost 10)
128                           (:return-style :full-call)
129                           (:policy :safe)
130                           (:translate %negate)
131                           (:save-p t))
132                          ((:arg x (descriptor-reg any-reg) rdx-offset)
133                           (:res res (descriptor-reg any-reg) rdx-offset)
134
135                           (:temp rax unsigned-reg rax-offset)
136                           (:temp rcx unsigned-reg rcx-offset))
137   (inst test x 7)
138   (inst jmp :z FIXNUM)
139
140   (inst pop rax)
141   (inst push rbp-tn)
142   (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp n-word-bytes))
143   (inst sub rsp-tn (fixnumize 2))
144   (inst push rax)
145   (inst mov rcx (fixnumize 1))    ; arg count
146   (inst jmp (make-ea :qword
147                      :disp (+ nil-value (static-fun-offset '%negate))))
148
149   FIXNUM
150   (move res x)
151   (inst neg res)                        ; (- most-negative-fixnum) is BIGNUM
152   (inst jmp :no OKAY)
153   (inst shr res 3)                    ; sign bit is data - remove type bits
154   (move rcx res)
155
156   (with-fixed-allocation (res bignum-widetag (1+ bignum-digits-offset))
157     (storew rcx res bignum-digits-offset other-pointer-lowtag))
158
159   OKAY)
160 \f
161 ;;;; comparison
162
163 (macrolet ((define-cond-assem-rtn (name translate static-fn test)
164              `(define-assembly-routine (,name
165                                         (:cost 10)
166                                         (:return-style :full-call)
167                                         (:policy :safe)
168                                         (:translate ,translate)
169                                         (:save-p t))
170                 ((:arg x (descriptor-reg any-reg) rdx-offset)
171                  (:arg y (descriptor-reg any-reg) rdi-offset)
172
173                  (:res res descriptor-reg rdx-offset)
174
175                  (:temp eax unsigned-reg rax-offset)
176                  (:temp ecx unsigned-reg rcx-offset))
177
178                 ;; KLUDGE: The "3" here is a mask for the bits which will be
179                 ;; zero in a fixnum. It should have a symbolic name. (Actually,
180                 ;; it might already have a symbolic name which the coder
181                 ;; couldn't be bothered to use..) -- WHN 19990917
182                 (inst test x 7)
183                 (inst jmp :nz TAIL-CALL-TO-STATIC-FN)
184                 (inst test y 7)
185                 (inst jmp :z INLINE-FIXNUM-COMPARE)
186
187                 TAIL-CALL-TO-STATIC-FN
188                 (inst pop eax)
189                 (inst push rbp-tn)
190                 (inst lea rbp-tn (make-ea :qword
191                                           :base rsp-tn
192                                           :disp n-word-bytes))
193                 (inst sub rsp-tn (fixnumize 2)) ; FIXME: Push 2 words on stack,
194                                                 ; weirdly?
195                 (inst push eax)
196                 (inst mov ecx (fixnumize 2)) ; FIXME: FIXNUMIZE and
197                                         ; SINGLE-FLOAT-BITS are parallel,
198                                         ; should be named parallelly.
199                 (inst jmp (make-ea :qword
200                                    :disp (+ nil-value
201                                             (static-fun-offset ',static-fn))))
202
203                 INLINE-FIXNUM-COMPARE
204                 (inst cmp x y)
205                 (inst jmp ,test RETURN-TRUE)
206                 (inst mov res nil-value)
207                 ;; FIXME: A note explaining this return convention, or a
208                 ;; symbolic name for it, would be nice. (It looks as though we
209                 ;; should be hand-crafting the same return sequence as would be
210                 ;; produced by GENERATE-RETURN-SEQUENCE, but in that case it's
211                 ;; not clear why we don't just jump to the end of this function
212                 ;; to share the return sequence there.
213                 (inst pop eax)
214                 (inst add eax 3)
215                 (inst jmp eax)
216
217                 RETURN-TRUE
218                 (load-symbol res t))))
219
220   (define-cond-assem-rtn generic-< < two-arg-< :l)
221   (define-cond-assem-rtn generic-> > two-arg-> :g))
222
223 (define-assembly-routine (generic-eql
224                           (:cost 10)
225                           (:return-style :full-call)
226                           (:policy :safe)
227                           (:translate eql)
228                           (:save-p t))
229                          ((:arg x (descriptor-reg any-reg) rdx-offset)
230                           (:arg y (descriptor-reg any-reg) rdi-offset)
231
232                           (:res res descriptor-reg rdx-offset)
233
234                           (:temp eax unsigned-reg rax-offset)
235                           (:temp ecx unsigned-reg rcx-offset))
236   (inst cmp x y)
237   (inst jmp :e RETURN-T)
238   (inst test x 7)
239   (inst jmp :z RETURN-NIL)
240   (inst test y 7)
241   (inst jmp :nz DO-STATIC-FN)
242
243   RETURN-NIL
244   (inst mov res nil-value)
245   (inst pop eax)
246   (inst add eax 3)
247   (inst jmp eax)
248
249   DO-STATIC-FN
250   (inst pop eax)
251   (inst push rbp-tn)
252   (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp n-word-bytes))
253   (inst sub rsp-tn (fixnumize 2))
254   (inst push eax)
255   (inst mov ecx (fixnumize 2))
256   (inst jmp (make-ea :qword
257                      :disp (+ nil-value (static-fun-offset 'eql))))
258
259   RETURN-T
260   (load-symbol res t)
261   ;; FIXME: I don't understand how we return from here..
262   )
263
264 (define-assembly-routine (generic-=
265                           (:cost 10)
266                           (:return-style :full-call)
267                           (:policy :safe)
268                           (:translate =)
269                           (:save-p t))
270                          ((:arg x (descriptor-reg any-reg) rdx-offset)
271                           (:arg y (descriptor-reg any-reg) rdi-offset)
272
273                           (:res res descriptor-reg rdx-offset)
274
275                           (:temp eax unsigned-reg rax-offset)
276                           (:temp ecx unsigned-reg rcx-offset)
277                           )
278   (inst test x 7)                      ; descriptor?
279   (inst jmp :nz DO-STATIC-FN)          ; yes, do it here
280   (inst test y 7)                      ; descriptor?
281   (inst jmp :nz DO-STATIC-FN)
282   (inst cmp x y)
283   (inst jmp :e RETURN-T)                ; ok
284
285   (inst mov res nil-value)
286   (inst pop eax)
287   (inst add eax 3)
288   (inst jmp eax)
289
290   DO-STATIC-FN
291   (inst pop eax)
292   (inst push rbp-tn)
293   (inst lea rbp-tn (make-ea :qword :base rsp-tn :disp n-word-bytes))
294   (inst sub rsp-tn (fixnumize 2))
295   (inst push eax)
296   (inst mov ecx (fixnumize 2))
297   (inst jmp (make-ea :qword
298                      :disp (+ nil-value (static-fun-offset 'two-arg-=))))
299
300   RETURN-T
301   (load-symbol res t))
302
303