e3d7216911598020099e5ec687d961fd7f457aa6
[sbcl.git] / src / assembly / alpha / arith.lisp
1 ;;;; stuff to handle 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
14 (define-assembly-routine (generic-+
15                           (:cost 10)
16                           (:return-style :full-call)
17                           (:translate +)
18                           (:policy :safe)
19                           (:save-p t))
20                          ((:arg x (descriptor-reg any-reg) a0-offset)
21                           (:arg y (descriptor-reg any-reg) a1-offset)
22
23                           (:res res (descriptor-reg any-reg) a0-offset)
24
25                           (:temp temp non-descriptor-reg nl0-offset)
26                           (:temp temp2 non-descriptor-reg nl1-offset)
27                           (:temp temp3 non-descriptor-reg nl2-offset)
28                           (:temp lip interior-reg lip-offset)
29                           (:temp lra descriptor-reg lra-offset)
30                           (:temp nargs any-reg nargs-offset)
31                           (:temp ocfp any-reg ocfp-offset))
32   (inst and x 3 temp)
33   (inst bne temp DO-STATIC-FUN)
34   (inst and y 3 temp)
35   (inst bne temp DO-STATIC-FUN)
36   (inst addq x y res)
37   
38   ; Check whether we need a bignum.
39   (inst sra res 31 temp)
40   (inst beq temp DONE)
41   (inst not temp temp)
42   (inst beq temp DONE)
43   (inst sra res 2 temp3)
44   
45   ; from move-from-signed
46   (inst li 2 temp2)
47   (inst sra temp3 31 temp)
48   (inst cmoveq temp 1 temp2)
49   (inst not temp temp)
50   (inst cmoveq temp 1 temp2)
51   (inst sll temp2 n-widetag-bits temp2)
52   (inst bis temp2 bignum-widetag temp2)
53   
54   (pseudo-atomic (:extra (pad-data-block (+ bignum-digits-offset 3)))
55     (inst bis alloc-tn other-pointer-lowtag res)
56     (storew temp2 res 0 other-pointer-lowtag)
57     (storew temp3 res bignum-digits-offset other-pointer-lowtag)
58     (inst srl temp3 32 temp)
59     (storew temp res (1+ bignum-digits-offset) other-pointer-lowtag))
60   DONE
61   (lisp-return lra lip :offset 2)
62
63   DO-STATIC-FUN
64   (inst ldl lip (static-fun-offset 'two-arg-+) null-tn)
65   (inst li (fixnumize 2) nargs)
66   (inst move cfp-tn ocfp)
67   (inst move csp-tn cfp-tn)
68   (inst jmp zero-tn lip))
69
70
71 (define-assembly-routine (generic--
72                           (:cost 10)
73                           (:return-style :full-call)
74                           (:translate -)
75                           (:policy :safe)
76                           (:save-p t))
77                          ((:arg x (descriptor-reg any-reg) a0-offset)
78                           (:arg y (descriptor-reg any-reg) a1-offset)
79
80                           (:res res (descriptor-reg any-reg) a0-offset)
81
82                           (:temp temp non-descriptor-reg nl0-offset)
83                           (:temp temp2 non-descriptor-reg nl1-offset)
84                           (:temp temp3 non-descriptor-reg nl2-offset)
85                           (:temp lip interior-reg lip-offset)
86                           (:temp lra descriptor-reg lra-offset)
87                           (:temp nargs any-reg nargs-offset)
88                           (:temp ocfp any-reg ocfp-offset))
89   (inst and x 3 temp)
90   (inst bne temp DO-STATIC-FUN)
91   (inst and y 3 temp)
92   (inst bne temp DO-STATIC-FUN)
93   (inst subq x y res)
94   
95   ; Check whether we need a bignum.
96   (inst sra res 31 temp)
97   (inst beq temp DONE)
98   (inst not temp temp)
99   (inst beq temp DONE)
100   (inst sra res 2 temp3)
101   
102   ; from move-from-signed
103   (inst li 2 temp2)
104   (inst sra temp3 31 temp)
105   (inst cmoveq temp 1 temp2)
106   (inst not temp temp)
107   (inst cmoveq temp 1 temp2)
108   (inst sll temp2 n-widetag-bits temp2)
109   (inst bis temp2 bignum-widetag temp2)
110   
111   (pseudo-atomic (:extra (pad-data-block (+ bignum-digits-offset 3)))
112     (inst bis alloc-tn other-pointer-lowtag res)
113     (storew temp2 res 0 other-pointer-lowtag)
114     (storew temp3 res bignum-digits-offset other-pointer-lowtag)
115     (inst srl temp3 32 temp)
116     (storew temp res (1+ bignum-digits-offset) other-pointer-lowtag))
117   DONE
118   (lisp-return lra lip :offset 2)
119
120   DO-STATIC-FUN
121   (inst ldl lip (static-fun-offset 'two-arg--) null-tn)
122   (inst li (fixnumize 2) nargs)
123   (inst move cfp-tn ocfp)
124   (inst move csp-tn cfp-tn)
125   (inst jmp zero-tn lip))
126
127
128 (define-assembly-routine (generic-*
129                           (:cost 25)
130                           (:return-style :full-call)
131                           (:translate *)
132                           (:policy :safe)
133                           (:save-p t))
134                          ((:arg x (descriptor-reg any-reg) a0-offset)
135                           (:arg y (descriptor-reg any-reg) a1-offset)
136
137                           (:res res (descriptor-reg any-reg) a0-offset)
138
139                           (:temp temp non-descriptor-reg nl0-offset)
140                           (:temp lo non-descriptor-reg nl1-offset)
141                           (:temp hi non-descriptor-reg nl2-offset)
142                           (:temp temp2 non-descriptor-reg nl3-offset)
143                           (:temp lip interior-reg lip-offset)
144                           (:temp lra descriptor-reg lra-offset)
145                           (:temp nargs any-reg nargs-offset)
146                           (:temp ocfp any-reg ocfp-offset))
147   ;; If either arg is not a fixnum, call the static function.
148   (inst and x 3 temp)
149   (inst bne temp DO-STATIC-FUN)
150   (inst and y 3 temp)
151   (inst bne temp DO-STATIC-FUN)
152
153   ;; Remove the tag from one arg so that the result will have the
154   ;; correct fixnum tag.
155   (inst sra x 2 temp)
156   (inst mulq temp y lo)
157   (inst sra lo 32 hi)
158   (inst sll lo 32 res)
159   (inst sra res 32 res)
160   ;; Check to see if the result will fit in a fixnum. (I.e. the high
161   ;; word is just 32 copies of the sign bit of the low word).
162   (inst sra res 31 temp)
163   (inst xor hi temp temp)
164   (inst beq temp DONE)
165   ;; Shift the double word hi:res down two bits into hi:low to get rid
166   ;; of the fixnum tag.
167   (inst sra lo 2 lo)
168   (inst sra lo 32 hi)
169
170   ;; Do we need one word or two?  Assume two.
171   (inst li (logior (ash 2 n-widetag-bits) bignum-widetag) temp2)
172   (inst sra lo 31 temp)
173   (inst xor temp hi temp)
174   (inst bne temp two-words)
175
176   ;; Only need one word, fix the header.
177   (inst li (logior (ash 1 n-widetag-bits) bignum-widetag) temp2)
178   ;; Allocate one word.
179   (pseudo-atomic (:extra (pad-data-block (1+ bignum-digits-offset)))
180     (inst bis alloc-tn other-pointer-lowtag res)
181     (storew temp2 res 0 other-pointer-lowtag))
182   ;; Store one word
183   (storew lo res bignum-digits-offset other-pointer-lowtag)
184   ;; Out of here
185   (lisp-return lra lip :offset 2)
186
187   TWO-WORDS
188   ;; Allocate two words.
189   (pseudo-atomic (:extra (pad-data-block (+ 2 bignum-digits-offset)))
190     (inst bis alloc-tn other-pointer-lowtag res)
191     (storew temp2 res 0 other-pointer-lowtag))
192   ;; Store two words.
193   (storew lo res bignum-digits-offset other-pointer-lowtag)
194   (storew hi res (1+ bignum-digits-offset) other-pointer-lowtag)
195   ;; out of here
196   (lisp-return lra lip :offset 2)
197
198   DO-STATIC-FUN
199   (inst ldl lip (static-fun-offset 'two-arg-*) null-tn)
200   (inst li (fixnumize 2) nargs)
201   (inst move cfp-tn ocfp)
202   (inst move csp-tn cfp-tn)
203   (inst jmp zero-tn lip)
204
205   DONE)
206
207 \f
208 ;;;; division
209
210 (define-assembly-routine (signed-truncate
211                           (:note "(signed-byte 32) truncate")
212                           (:cost 60)
213                           (:policy :fast-safe)
214                           (:translate truncate)
215                           (:arg-types signed-num signed-num)
216                           (:result-types signed-num signed-num))
217
218                          ((:arg dividend signed-reg nl0-offset)
219                           (:arg divisor signed-reg nl1-offset)
220
221                           (:res quo signed-reg nl2-offset)
222                           (:res rem signed-reg nl3-offset)
223
224                           (:temp quo-sign signed-reg nl5-offset)
225                           (:temp rem-sign signed-reg nargs-offset)
226                           (:temp temp1 non-descriptor-reg nl4-offset))
227   
228   (let ((error (generate-error-code nil division-by-zero-error
229                                     dividend divisor)))
230     (inst beq divisor error))
231
232   (inst xor dividend divisor quo-sign)
233   (inst move dividend rem-sign)
234   (let ((label (gen-label)))
235     (inst bge dividend label)
236     (inst subq zero-tn dividend dividend)
237     (emit-label label))
238   (let ((label (gen-label)))
239     (inst bge divisor label)
240     (inst subq zero-tn divisor divisor)
241     (emit-label label))
242   (inst move zero-tn rem)
243   (inst move zero-tn quo)
244   (inst sll dividend 32 dividend)
245
246   (dotimes (i 32)
247     (inst srl dividend 63 temp1)
248     (inst sll rem 1 rem)
249     (inst bis temp1 rem rem)
250     (inst cmple divisor rem temp1)
251     (inst sll quo 1 quo)
252     (inst bis temp1 quo quo)
253     (inst sll dividend 1 dividend)
254     (inst subq temp1 1 temp1)
255     (inst zap divisor temp1 temp1)
256     (inst subq rem temp1 rem))
257
258   (let ((label (gen-label)))
259     ;; If the quo-sign is negative, we need to negate quo.
260     (inst bge quo-sign label)
261     (inst subq zero-tn quo quo)
262     (emit-label label))
263   (let ((label (gen-label)))
264     ;; If the rem-sign is negative, we need to negate rem.
265     (inst bge rem-sign label)
266     (inst subq zero-tn rem rem)
267     (emit-label label)))
268
269 \f
270 ;;;; comparison routines
271
272 (macrolet
273     ((define-cond-assem-rtn (name translate static-fn cmp not-p)
274        `(define-assembly-routine (,name
275                                   (:cost 10)
276                                   (:return-style :full-call)
277                                   (:policy :safe)
278                                   (:translate ,translate)
279                                   (:save-p t))
280                                  ((:arg x (descriptor-reg any-reg) a0-offset)
281                                   (:arg y (descriptor-reg any-reg) a1-offset)
282                                   
283                                   (:res res descriptor-reg a0-offset)
284                                   
285                                   (:temp temp non-descriptor-reg nl0-offset)
286                                   (:temp lip interior-reg lip-offset)
287                                   (:temp nargs any-reg nargs-offset)
288                                   (:temp ocfp any-reg ocfp-offset))
289           (inst and x 3 temp)
290           (inst bne temp DO-STATIC-FN)
291           (inst and y 3 temp)
292           (inst beq temp DO-COMPARE)
293           
294           DO-STATIC-FN
295           (inst ldl lip (static-fun-offset ',static-fn) null-tn)
296           (inst li (fixnumize 2) nargs)
297           (inst move cfp-tn ocfp)
298           (inst move csp-tn cfp-tn)
299           (inst jmp zero-tn lip)
300           
301           DO-COMPARE
302           ,cmp
303           (inst move null-tn res)
304           (inst ,(if not-p 'bne 'beq) temp done)
305           (load-symbol res t)
306           DONE)))
307
308   (define-cond-assem-rtn generic-< < two-arg-< (inst cmplt x y temp) nil)
309   (define-cond-assem-rtn generic-> > two-arg-> (inst cmplt y x temp) nil))
310
311
312 (define-assembly-routine (generic-eql
313                           (:cost 10)
314                           (:return-style :full-call)
315                           (:policy :safe)
316                           (:translate eql)
317                           (:save-p t))
318                          ((:arg x (descriptor-reg any-reg) a0-offset)
319                           (:arg y (descriptor-reg any-reg) a1-offset)
320                           
321                           (:res res descriptor-reg a0-offset)
322                           
323                           (:temp temp non-descriptor-reg nl0-offset)
324                           (:temp lip interior-reg lip-offset)
325                           (:temp lra descriptor-reg lra-offset)
326                           (:temp nargs any-reg nargs-offset)
327                           (:temp ocfp any-reg ocfp-offset))
328   (inst cmpeq x y temp)
329   (inst bne temp RETURN-T)
330   (inst and x 3 temp)
331   (inst beq temp RETURN-NIL)
332   (inst and y 3 temp)
333   (inst bne temp DO-STATIC-FN)
334
335   RETURN-NIL
336   (inst move null-tn res)
337   (lisp-return lra lip :offset 2)
338
339   DO-STATIC-FN
340   (inst ldl lip (static-fun-offset 'eql) null-tn)
341   (inst li (fixnumize 2) nargs)
342   (inst move cfp-tn ocfp)
343   (inst move csp-tn cfp-tn)
344   (inst jmp zero-tn lip)
345
346   RETURN-T
347   (load-symbol res t))
348
349 (define-assembly-routine (generic-=
350                           (:cost 10)
351                           (:return-style :full-call)
352                           (:policy :safe)
353                           (:translate =)
354                           (:save-p t))
355                          ((:arg x (descriptor-reg any-reg) a0-offset)
356                           (:arg y (descriptor-reg any-reg) a1-offset)
357                           
358                           (:res res descriptor-reg a0-offset)
359                           
360                           (:temp temp non-descriptor-reg nl0-offset)
361                           (:temp lip interior-reg lip-offset)
362                           (:temp lra descriptor-reg lra-offset)
363                           (:temp nargs any-reg nargs-offset)
364                           (:temp ocfp any-reg ocfp-offset))
365   (inst and x 3 temp)
366   (inst bne temp DO-STATIC-FN)
367   (inst and y 3 temp)
368   (inst bne temp DO-STATIC-FN)
369   (inst cmpeq x y temp)
370   (inst bne temp RETURN-T)
371
372   (inst move null-tn res)
373   (lisp-return lra lip :offset 2)
374
375   DO-STATIC-FN
376   (inst ldl lip (static-fun-offset 'two-arg-=) null-tn)
377   (inst li (fixnumize 2) nargs)
378   (inst move cfp-tn ocfp)
379   (inst move csp-tn cfp-tn)
380   (inst jmp zero-tn lip)
381
382   RETURN-T
383   (load-symbol res t))
384
385 (define-assembly-routine (generic-/=
386                           (:cost 10)
387                           (:return-style :full-call)
388                           (:policy :safe)
389                           (:translate /=)
390                           (:save-p t))
391                          ((:arg x (descriptor-reg any-reg) a0-offset)
392                           (:arg y (descriptor-reg any-reg) a1-offset)
393                           
394                           (:res res descriptor-reg a0-offset)
395                           
396                           (:temp temp non-descriptor-reg nl0-offset)
397                           (:temp lip interior-reg lip-offset)
398                           (:temp lra descriptor-reg lra-offset)
399                           (:temp nargs any-reg nargs-offset)
400                           (:temp ocfp any-reg ocfp-offset))
401   (inst and x 3 temp)
402   (inst bne temp DO-STATIC-FN)
403   (inst and y 3 temp)
404   (inst bne temp DO-STATIC-FN)
405   (inst cmpeq x y temp)
406   (inst bne temp RETURN-NIL)
407
408   (load-symbol res t)
409   (lisp-return lra lip :offset 2)
410
411   DO-STATIC-FN
412   (inst ldl lip (static-fun-offset 'two-arg-=) null-tn)
413   (inst li (fixnumize 2) nargs)
414   (inst move cfp-tn ocfp)
415   (inst move csp-tn cfp-tn)
416   (inst jmp zero-tn lip)
417
418   RETURN-NIL
419   (inst move null-tn res))