eaef9b4bd028c77406469e40d66b962832a8e558
[sbcl.git] / src / compiler / x86-64 / arith.lisp
1 ;;;; the VM definition of arithmetic VOPs for the x86
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 ;;;; unary operations
15
16 (define-vop (fast-safe-arith-op)
17   (:policy :fast-safe)
18   (:effects)
19   (:affected))
20
21 (define-vop (fixnum-unop fast-safe-arith-op)
22   (:args (x :scs (any-reg) :target res))
23   (:results (res :scs (any-reg)))
24   (:note "inline fixnum arithmetic")
25   (:arg-types tagged-num)
26   (:result-types tagged-num))
27
28 (define-vop (signed-unop fast-safe-arith-op)
29   (:args (x :scs (signed-reg) :target res))
30   (:results (res :scs (signed-reg)))
31   (:note "inline (signed-byte 32) arithmetic")
32   (:arg-types signed-num)
33   (:result-types signed-num))
34
35 (define-vop (fast-negate/fixnum fixnum-unop)
36   (:translate %negate)
37   (:generator 1
38     (move res x)
39     (inst neg res)))
40
41 (define-vop (fast-negate/signed signed-unop)
42   (:translate %negate)
43   (:generator 2
44     (move res x)
45     (inst neg res)))
46
47 (define-vop (fast-lognot/fixnum fixnum-unop)
48   (:translate lognot)
49   (:generator 2
50     (move res x)
51     (inst xor res (fixnumize -1))))
52
53 (define-vop (fast-lognot/signed signed-unop)
54   (:translate lognot)
55   (:generator 1
56     (move res x)
57     (inst not res)))
58 \f
59 ;;;; binary fixnum operations
60
61 ;;; Assume that any constant operand is the second arg...
62
63 (define-vop (fast-fixnum-binop fast-safe-arith-op)
64   (:args (x :target r :scs (any-reg)
65             :load-if (not (and (sc-is x control-stack)
66                                (sc-is y any-reg)
67                                (sc-is r control-stack)
68                                (location= x r))))
69          (y :scs (any-reg control-stack)))
70   (:arg-types tagged-num tagged-num)
71   (:results (r :scs (any-reg) :from (:argument 0)
72                :load-if (not (and (sc-is x control-stack)
73                                   (sc-is y any-reg)
74                                   (sc-is r control-stack)
75                                   (location= x r)))))
76   (:result-types tagged-num)
77   (:note "inline fixnum arithmetic"))
78
79 (define-vop (fast-unsigned-binop fast-safe-arith-op)
80   (:args (x :target r :scs (unsigned-reg)
81             :load-if (not (and (sc-is x unsigned-stack)
82                                (sc-is y unsigned-reg)
83                                (sc-is r unsigned-stack)
84                                (location= x r))))
85          (y :scs (unsigned-reg unsigned-stack)))
86   (:arg-types unsigned-num unsigned-num)
87   (:results (r :scs (unsigned-reg) :from (:argument 0)
88             :load-if (not (and (sc-is x unsigned-stack)
89                                (sc-is y unsigned-reg)
90                                (sc-is r unsigned-stack)
91                                (location= x r)))))
92   (:result-types unsigned-num)
93   (:note "inline (unsigned-byte 32) arithmetic"))
94
95 (define-vop (fast-signed-binop fast-safe-arith-op)
96   (:args (x :target r :scs (signed-reg)
97             :load-if (not (and (sc-is x signed-stack)
98                                (sc-is y signed-reg)
99                                (sc-is r signed-stack)
100                                (location= x r))))
101          (y :scs (signed-reg signed-stack)))
102   (:arg-types signed-num signed-num)
103   (:results (r :scs (signed-reg) :from (:argument 0)
104             :load-if (not (and (sc-is x signed-stack)
105                                (sc-is y signed-reg)
106                                (sc-is r signed-stack)
107                                (location= x r)))))
108   (:result-types signed-num)
109   (:note "inline (signed-byte 32) arithmetic"))
110
111 (define-vop (fast-fixnum-binop-c fast-safe-arith-op)
112   (:args (x :target r :scs (any-reg control-stack)))
113   (:info y)
114   (:arg-types tagged-num (:constant (signed-byte 29)))
115   (:results (r :scs (any-reg)
116                :load-if (not (location= x r))))
117   (:result-types tagged-num)
118   (:note "inline fixnum arithmetic"))
119
120 (define-vop (fast-unsigned-binop-c fast-safe-arith-op)
121   (:args (x :target r :scs (unsigned-reg unsigned-stack)))
122   (:info y)
123   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
124   (:results (r :scs (unsigned-reg)
125                :load-if (not (location= x r))))
126   (:result-types unsigned-num)
127   (:note "inline (unsigned-byte 32) arithmetic"))
128
129 ;; 32 not 64 because it's hard work loading 64 bit constants
130 (define-vop (fast-signed-binop-c fast-safe-arith-op)
131   (:args (x :target r :scs (signed-reg signed-stack)))
132   (:info y)
133   (:arg-types signed-num (:constant (signed-byte 32)))
134   (:results (r :scs (signed-reg)
135                :load-if (not (location= x r))))
136   (:result-types signed-num)
137   (:note "inline (signed-byte 64) arithmetic"))
138
139 (macrolet ((define-binop (translate untagged-penalty op)
140              `(progn
141                 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
142                              fast-fixnum-binop)
143                   (:translate ,translate)
144                   (:generator 2
145                               (move r x)
146                               (inst ,op r y)))
147                 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
148                              fast-fixnum-binop-c)
149                   (:translate ,translate)
150                   (:generator 1
151                   (move r x)
152                   (inst ,op r (fixnumize y))))
153                 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
154                              fast-signed-binop)
155                   (:translate ,translate)
156                   (:generator ,(1+ untagged-penalty)
157                   (move r x)
158                   (inst ,op r y)))
159                 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
160                              fast-signed-binop-c)
161                   (:translate ,translate)
162                   (:generator ,untagged-penalty
163                   (move r x)
164                   (inst ,op r y)))
165                 (define-vop (,(symbolicate "FAST-"
166                                            translate
167                                            "/UNSIGNED=>UNSIGNED")
168                 fast-unsigned-binop)
169                   (:translate ,translate)
170                   (:generator ,(1+ untagged-penalty)
171                   (move r x)
172                   (inst ,op r y)))
173                 (define-vop (,(symbolicate 'fast-
174                                            translate
175                                            '-c/unsigned=>unsigned)
176                              fast-unsigned-binop-c)
177                   (:translate ,translate)
178                   (:generator ,untagged-penalty
179                   (move r x)
180                   (inst ,op r y))))))
181
182   ;;(define-binop + 4 add)
183   (define-binop - 4 sub)
184   (define-binop logand 2 and)
185   (define-binop logior 2 or)
186   (define-binop logxor 2 xor))
187
188 ;;; Special handling of add on the x86; can use lea to avoid a
189 ;;; register load, otherwise it uses add.
190 (define-vop (fast-+/fixnum=>fixnum fast-safe-arith-op)
191   (:translate +)
192   (:args (x :scs (any-reg) :target r
193             :load-if (not (and (sc-is x control-stack)
194                                (sc-is y any-reg)
195                                (sc-is r control-stack)
196                                (location= x r))))
197          (y :scs (any-reg control-stack)))
198   (:arg-types tagged-num tagged-num)
199   (:results (r :scs (any-reg) :from (:argument 0)
200                :load-if (not (and (sc-is x control-stack)
201                                   (sc-is y any-reg)
202                                   (sc-is r control-stack)
203                                   (location= x r)))))
204   (:result-types tagged-num)
205   (:note "inline fixnum arithmetic")
206   (:generator 2
207     (cond ((and (sc-is x any-reg) (sc-is y any-reg) (sc-is r any-reg)
208                 (not (location= x r)))
209            (inst lea r (make-ea :qword :base x :index y :scale 1)))
210           (t
211            (move r x)
212            (inst add r y)))))
213
214 (define-vop (fast-+-c/fixnum=>fixnum fast-safe-arith-op)
215   (:translate +)
216   (:args (x :target r :scs (any-reg control-stack)))
217   (:info y)
218   (:arg-types tagged-num (:constant (signed-byte 29)))
219   (:results (r :scs (any-reg)
220                :load-if (not (location= x r))))
221   (:result-types tagged-num)
222   (:note "inline fixnum arithmetic")
223   (:generator 1
224     (cond ((and (sc-is x any-reg) (sc-is r any-reg) (not (location= x r)))
225            (inst lea r (make-ea :qword :base x :disp (fixnumize y))))
226           (t
227            (move r x)
228            (inst add r (fixnumize y))))))
229
230 (define-vop (fast-+/signed=>signed fast-safe-arith-op)
231   (:translate +)
232   (:args (x :scs (signed-reg) :target r
233             :load-if (not (and (sc-is x signed-stack)
234                                (sc-is y signed-reg)
235                                (sc-is r signed-stack)
236                                (location= x r))))
237          (y :scs (signed-reg signed-stack)))
238   (:arg-types signed-num signed-num)
239   (:results (r :scs (signed-reg) :from (:argument 0)
240                :load-if (not (and (sc-is x signed-stack)
241                                   (sc-is y signed-reg)
242                                   (location= x r)))))
243   (:result-types signed-num)
244   (:note "inline (signed-byte 32) arithmetic")
245   (:generator 5
246     (cond ((and (sc-is x signed-reg) (sc-is y signed-reg) (sc-is r signed-reg)
247                 (not (location= x r)))
248            (inst lea r (make-ea :qword :base x :index y :scale 1)))
249           (t
250            (move r x)
251            (inst add r y)))))
252
253
254 ;;;; Special logand cases: (logand signed unsigned) => unsigned
255
256 (define-vop (fast-logand/signed-unsigned=>unsigned
257              fast-logand/unsigned=>unsigned)
258   (:args (x :target r :scs (signed-reg)
259             :load-if (not (and (sc-is x signed-stack)
260                                (sc-is y unsigned-reg)
261                                (sc-is r unsigned-stack)
262                                (location= x r))))
263          (y :scs (unsigned-reg unsigned-stack)))
264   (:arg-types signed-num unsigned-num))
265
266 (define-vop (fast-logand-c/signed-unsigned=>unsigned
267              fast-logand-c/unsigned=>unsigned)
268   (:args (x :target r :scs (signed-reg signed-stack)))
269   (:arg-types signed-num (:constant (unsigned-byte 32))))
270
271 (define-vop (fast-logand/unsigned-signed=>unsigned
272              fast-logand/unsigned=>unsigned)
273   (:args (x :target r :scs (unsigned-reg)
274             :load-if (not (and (sc-is x unsigned-stack)
275                                (sc-is y signed-reg)
276                                (sc-is r unsigned-stack)
277                                (location= x r))))
278          (y :scs (signed-reg signed-stack)))
279   (:arg-types unsigned-num signed-num))
280 \f
281
282 (define-vop (fast-+-c/signed=>signed fast-safe-arith-op)
283   (:translate +)
284   (:args (x :target r :scs (signed-reg signed-stack)))
285   (:info y)
286   (:arg-types signed-num (:constant (signed-byte 32)))
287   (:results (r :scs (signed-reg)
288                :load-if (not (location= x r))))
289   (:result-types signed-num)
290   (:note "inline (signed-byte 32) arithmetic")
291   (:generator 4
292     (cond ((and (sc-is x signed-reg) (sc-is r signed-reg)
293                 (not (location= x r)))
294            (inst lea r (make-ea :qword :base x :disp y)))
295           (t
296            (move r x)
297            (if (= y 1)
298                (inst inc r)
299              (inst add r y))))))
300
301 (define-vop (fast-+/unsigned=>unsigned fast-safe-arith-op)
302   (:translate +)
303   (:args (x :scs (unsigned-reg) :target r
304             :load-if (not (and (sc-is x unsigned-stack)
305                                (sc-is y unsigned-reg)
306                                (sc-is r unsigned-stack)
307                                (location= x r))))
308          (y :scs (unsigned-reg unsigned-stack)))
309   (:arg-types unsigned-num unsigned-num)
310   (:results (r :scs (unsigned-reg) :from (:argument 0)
311                :load-if (not (and (sc-is x unsigned-stack)
312                                   (sc-is y unsigned-reg)
313                                   (sc-is r unsigned-stack)
314                                   (location= x r)))))
315   (:result-types unsigned-num)
316   (:note "inline (unsigned-byte 32) arithmetic")
317   (:generator 5
318     (cond ((and (sc-is x unsigned-reg) (sc-is y unsigned-reg)
319                 (sc-is r unsigned-reg) (not (location= x r)))
320            (inst lea r (make-ea :qword :base x :index y :scale 1)))
321           (t
322            (move r x)
323            (inst add r y)))))
324
325 (define-vop (fast-+-c/unsigned=>unsigned fast-safe-arith-op)
326   (:translate +)
327   (:args (x :target r :scs (unsigned-reg unsigned-stack)))
328   (:info y)
329   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
330   (:results (r :scs (unsigned-reg)
331                :load-if (not (location= x r))))
332   (:result-types unsigned-num)
333   (:note "inline (unsigned-byte 32) arithmetic")
334   (:generator 4
335     (cond ((and (sc-is x unsigned-reg) (sc-is r unsigned-reg)
336                 (not (location= x r)))
337            (inst lea r (make-ea :qword :base x :disp y)))
338           (t
339            (move r x)
340            (if (= y 1)
341                (inst inc r)
342              (inst add r y))))))
343 \f
344 ;;;; multiplication and division
345
346 (define-vop (fast-*/fixnum=>fixnum fast-safe-arith-op)
347   (:translate *)
348   ;; We need different loading characteristics.
349   (:args (x :scs (any-reg) :target r)
350          (y :scs (any-reg control-stack)))
351   (:arg-types tagged-num tagged-num)
352   (:results (r :scs (any-reg) :from (:argument 0)))
353   (:result-types tagged-num)
354   (:note "inline fixnum arithmetic")
355   (:generator 4
356     (move r x)
357     (inst sar r 3)
358     (inst imul r y)))
359
360 (define-vop (fast-*-c/fixnum=>fixnum fast-safe-arith-op)
361   (:translate *)
362   ;; We need different loading characteristics.
363   (:args (x :scs (any-reg control-stack)))
364   (:info y)
365   (:arg-types tagged-num (:constant (signed-byte 29))) 
366   (:results (r :scs (any-reg)))
367   (:result-types tagged-num)
368   (:note "inline fixnum arithmetic")
369   (:generator 3
370     (inst imul r x y)))
371
372 (define-vop (fast-*/signed=>signed fast-safe-arith-op)
373   (:translate *)
374   ;; We need different loading characteristics.
375   (:args (x :scs (signed-reg) :target r)
376          (y :scs (signed-reg signed-stack)))
377   (:arg-types signed-num signed-num)
378   (:results (r :scs (signed-reg) :from (:argument 0)))
379   (:result-types signed-num)
380   (:note "inline (signed-byte 32) arithmetic")
381   (:generator 5
382     (move r x)
383     (inst imul r y)))
384
385 (define-vop (fast-*-c/signed=>signed fast-safe-arith-op)
386   (:translate *)
387   ;; We need different loading characteristics.
388   (:args (x :scs (signed-reg signed-stack)))
389   (:info y)
390   (:arg-types signed-num (:constant (signed-byte 32)))
391   (:results (r :scs (signed-reg)))
392   (:result-types signed-num)
393   (:note "inline (signed-byte 32) arithmetic")
394   (:generator 4
395     (inst imul r x y)))
396
397 (define-vop (fast-*/unsigned=>unsigned fast-safe-arith-op)
398   (:translate *)
399   (:args (x :scs (unsigned-reg) :target eax)
400          (y :scs (unsigned-reg unsigned-stack)))
401   (:arg-types unsigned-num unsigned-num)
402   (:temporary (:sc unsigned-reg :offset eax-offset :target result
403                    :from (:argument 0) :to :result) eax)
404   (:temporary (:sc unsigned-reg :offset edx-offset
405                    :from :eval :to :result) edx)
406   (:ignore edx)
407   (:results (result :scs (unsigned-reg)))
408   (:result-types unsigned-num)
409   (:note "inline (unsigned-byte 32) arithmetic")
410   (:vop-var vop)
411   (:save-p :compute-only)
412   (:generator 6
413     (move eax x)
414     (inst mul eax y)
415     (move result eax)))
416
417
418 (define-vop (fast-truncate/fixnum=>fixnum fast-safe-arith-op)
419   (:translate truncate)
420   (:args (x :scs (any-reg) :target eax)
421          (y :scs (any-reg control-stack)))
422   (:arg-types tagged-num tagged-num)
423   (:temporary (:sc signed-reg :offset eax-offset :target quo
424                    :from (:argument 0) :to (:result 0)) eax)
425   (:temporary (:sc unsigned-reg :offset edx-offset :target rem
426                    :from (:argument 0) :to (:result 1)) edx)
427   (:results (quo :scs (any-reg))
428             (rem :scs (any-reg)))
429   (:result-types tagged-num tagged-num)
430   (:note "inline fixnum arithmetic")
431   (:vop-var vop)
432   (:save-p :compute-only)
433   (:generator 31
434     (let ((zero (generate-error-code vop division-by-zero-error x y)))
435       (if (sc-is y any-reg)
436           (inst test y y)  ; smaller instruction
437           (inst cmp y 0))
438       (inst jmp :eq zero))
439     (move eax x)
440     (inst cqo)
441     (inst idiv eax y)
442     (if (location= quo eax)
443         (inst shl eax 3)
444         (inst lea quo (make-ea :qword :index eax :scale 8)))
445     (move rem edx)))
446
447 (define-vop (fast-truncate-c/fixnum=>fixnum fast-safe-arith-op)
448   (:translate truncate)
449   (:args (x :scs (any-reg) :target eax))
450   (:info y)
451   (:arg-types tagged-num (:constant (signed-byte 29)))
452   (:temporary (:sc signed-reg :offset eax-offset :target quo
453                    :from :argument :to (:result 0)) eax)
454   (:temporary (:sc any-reg :offset edx-offset :target rem
455                    :from :eval :to (:result 1)) edx)
456   (:temporary (:sc any-reg :from :eval :to :result) y-arg)
457   (:results (quo :scs (any-reg))
458             (rem :scs (any-reg)))
459   (:result-types tagged-num tagged-num)
460   (:note "inline fixnum arithmetic")
461   (:vop-var vop)
462   (:save-p :compute-only)
463   (:generator 30
464     (move eax x)
465     (inst cqo)
466     (inst mov y-arg (fixnumize y))
467     (inst idiv eax y-arg)
468     (if (location= quo eax)
469         (inst shl eax 3)
470         (inst lea quo (make-ea :qword :index eax :scale 8)))
471     (move rem edx)))
472
473 (define-vop (fast-truncate/unsigned=>unsigned fast-safe-arith-op)
474   (:translate truncate)
475   (:args (x :scs (unsigned-reg) :target eax)
476          (y :scs (unsigned-reg signed-stack)))
477   (:arg-types unsigned-num unsigned-num)
478   (:temporary (:sc unsigned-reg :offset eax-offset :target quo
479                    :from (:argument 0) :to (:result 0)) eax)
480   (:temporary (:sc unsigned-reg :offset edx-offset :target rem
481                    :from (:argument 0) :to (:result 1)) edx)
482   (:results (quo :scs (unsigned-reg))
483             (rem :scs (unsigned-reg)))
484   (:result-types unsigned-num unsigned-num)
485   (:note "inline (unsigned-byte 32) arithmetic")
486   (:vop-var vop)
487   (:save-p :compute-only)
488   (:generator 33
489     (let ((zero (generate-error-code vop division-by-zero-error x y)))
490       (if (sc-is y unsigned-reg)
491           (inst test y y)  ; smaller instruction
492           (inst cmp y 0))
493       (inst jmp :eq zero))
494     (move eax x)
495     (inst xor edx edx)
496     (inst div eax y)
497     (move quo eax)
498     (move rem edx)))
499
500 (define-vop (fast-truncate-c/unsigned=>unsigned fast-safe-arith-op)
501   (:translate truncate)
502   (:args (x :scs (unsigned-reg) :target eax))
503   (:info y)
504   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
505   (:temporary (:sc unsigned-reg :offset eax-offset :target quo
506                    :from :argument :to (:result 0)) eax)
507   (:temporary (:sc unsigned-reg :offset edx-offset :target rem
508                    :from :eval :to (:result 1)) edx)
509   (:temporary (:sc unsigned-reg :from :eval :to :result) y-arg)
510   (:results (quo :scs (unsigned-reg))
511             (rem :scs (unsigned-reg)))
512   (:result-types unsigned-num unsigned-num)
513   (:note "inline (unsigned-byte 32) arithmetic")
514   (:vop-var vop)
515   (:save-p :compute-only)
516   (:generator 32
517     (move eax x)
518     (inst xor edx edx)
519     (inst mov y-arg y)
520     (inst div eax y-arg)
521     (move quo eax)
522     (move rem edx)))
523
524 (define-vop (fast-truncate/signed=>signed fast-safe-arith-op)
525   (:translate truncate)
526   (:args (x :scs (signed-reg) :target eax)
527          (y :scs (signed-reg signed-stack)))
528   (:arg-types signed-num signed-num)
529   (:temporary (:sc signed-reg :offset eax-offset :target quo
530                    :from (:argument 0) :to (:result 0)) eax)
531   (:temporary (:sc signed-reg :offset edx-offset :target rem
532                    :from (:argument 0) :to (:result 1)) edx)
533   (:results (quo :scs (signed-reg))
534             (rem :scs (signed-reg)))
535   (:result-types signed-num signed-num)
536   (:note "inline (signed-byte 32) arithmetic")
537   (:vop-var vop)
538   (:save-p :compute-only)
539   (:generator 33
540     (let ((zero (generate-error-code vop division-by-zero-error x y)))
541       (if (sc-is y signed-reg)
542           (inst test y y)  ; smaller instruction
543           (inst cmp y 0))
544       (inst jmp :eq zero))
545     (move eax x)
546     (inst cqo)
547     (inst idiv eax y)
548     (move quo eax)
549     (move rem edx)))
550
551 (define-vop (fast-truncate-c/signed=>signed fast-safe-arith-op)
552   (:translate truncate)
553   (:args (x :scs (signed-reg) :target eax))
554   (:info y)
555   (:arg-types signed-num (:constant (signed-byte 32)))
556   (:temporary (:sc signed-reg :offset eax-offset :target quo
557                    :from :argument :to (:result 0)) eax)
558   (:temporary (:sc signed-reg :offset edx-offset :target rem
559                    :from :eval :to (:result 1)) edx)
560   (:temporary (:sc signed-reg :from :eval :to :result) y-arg)
561   (:results (quo :scs (signed-reg))
562             (rem :scs (signed-reg)))
563   (:result-types signed-num signed-num)
564   (:note "inline (signed-byte 32) arithmetic")
565   (:vop-var vop)
566   (:save-p :compute-only)
567   (:generator 32
568     (move eax x)
569     (inst cqo)
570     (inst mov y-arg y)
571     (inst idiv eax y-arg)
572     (move quo eax)
573     (move rem edx)))
574
575
576 \f
577 ;;;; Shifting
578 (define-vop (fast-ash-c/fixnum=>fixnum)
579   (:translate ash)
580   (:policy :fast-safe)
581   (:args (number :scs (any-reg) :target result
582                  :load-if (not (and (sc-is number any-reg control-stack)
583                                     (sc-is result any-reg control-stack)
584                                     (location= number result)))))
585   (:info amount)
586   (:arg-types tagged-num (:constant integer))
587   (:results (result :scs (any-reg)
588                     :load-if (not (and (sc-is number control-stack)
589                                        (sc-is result control-stack)
590                                        (location= number result)))))
591   (:result-types tagged-num)
592   (:note "inline ASH")
593   (:generator 2
594     (cond ((and (= amount 1) (not (location= number result)))
595            (inst lea result (make-ea :qword :index number :scale 2)))
596           ((and (= amount 2) (not (location= number result)))
597            (inst lea result (make-ea :qword :index number :scale 4)))
598           ((and (= amount 3) (not (location= number result)))
599            (inst lea result (make-ea :qword :index number :scale 8)))
600           (t
601            (move result number)
602            (cond ((plusp amount)
603                   ;; We don't have to worry about overflow because of the
604                   ;; result type restriction.
605                   (inst shl result amount))
606                  ((zerop amount)  )
607                  ((< amount -63)
608                   (inst xor result result))
609                  (t 
610                   ;; shift too far then back again, to zero tag bits
611                   (inst sar result (- 3 amount))
612                   (inst lea result
613                         (make-ea :qword :index result :scale 8))))))))
614
615
616 (define-vop (fast-ash-left/fixnum=>fixnum)
617   (:translate ash)
618   (:args (number :scs (any-reg) :target result
619                  :load-if (not (and (sc-is number control-stack)
620                                     (sc-is result control-stack)
621                                     (location= number result))))
622          (amount :scs (unsigned-reg) :target ecx))
623   (:arg-types tagged-num positive-fixnum)
624   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
625   (:results (result :scs (any-reg) :from (:argument 0)
626                     :load-if (not (and (sc-is number control-stack)
627                                        (sc-is result control-stack)
628                                        (location= number result)))))
629   (:result-types tagged-num)
630   (:policy :fast-safe)
631   (:note "inline ASH")
632   (:generator 3
633     (move result number)
634     (move ecx amount)
635     ;; The result-type ensures us that this shift will not overflow.
636     (inst shl result :cl)))
637
638 (define-vop (fast-ash-c/signed=>signed)
639   (:translate ash)
640   (:policy :fast-safe)
641   (:args (number :scs (signed-reg) :target result
642                  :load-if (not (and (sc-is number signed-stack)
643                                     (sc-is result signed-stack)
644                                     (location= number result)))))
645   (:info amount)
646   (:arg-types signed-num (:constant integer))
647   (:results (result :scs (signed-reg)
648                     :load-if (not (and (sc-is number signed-stack)
649                                        (sc-is result signed-stack)
650                                        (location= number result)))))
651   (:result-types signed-num)
652   (:note "inline ASH")
653   (:generator 3
654     (cond ((and (= amount 1) (not (location= number result)))
655            (inst lea result (make-ea :qword :index number :scale 2)))
656           ((and (= amount 2) (not (location= number result)))
657            (inst lea result (make-ea :qword :index number :scale 4)))
658           ((and (= amount 3) (not (location= number result)))
659            (inst lea result (make-ea :qword :index number :scale 8)))
660           (t
661            (move result number)
662            (cond ((plusp amount) (inst shl result amount))
663                  (t (inst sar result (min 63 (- amount)))))))))
664
665 (define-vop (fast-ash-c/unsigned=>unsigned)
666   (:translate ash)
667   (:policy :fast-safe)
668   (:args (number :scs (unsigned-reg) :target result
669                  :load-if (not (and (sc-is number unsigned-stack)
670                                     (sc-is result unsigned-stack)
671                                     (location= number result)))))
672   (:info amount)
673   (:arg-types unsigned-num (:constant integer))
674   (:results (result :scs (unsigned-reg)
675                     :load-if (not (and (sc-is number unsigned-stack)
676                                        (sc-is result unsigned-stack)
677                                        (location= number result)))))
678   (:result-types unsigned-num)
679   (:note "inline ASH")
680   (:generator 3
681     (cond ((and (= amount 1) (not (location= number result)))
682            (inst lea result (make-ea :qword :index number :scale 2)))
683           ((and (= amount 2) (not (location= number result)))
684            (inst lea result (make-ea :qword :index number :scale 4)))
685           ((and (= amount 3) (not (location= number result)))
686            (inst lea result (make-ea :qword :index number :scale 8)))
687           (t
688            (move result number)
689            (cond ((< -64 amount 64) ;; XXXX
690                   ;; this code is used both in ASH and ASH-MOD32, so
691                   ;; be careful
692                   (if (plusp amount)
693                       (inst shl result amount)
694                       (inst shr result (- amount))))
695                  (t (if (sc-is result unsigned-reg)
696                         (inst xor result result)
697                         (inst mov result 0))))))))
698
699 (define-vop (fast-ash-left/signed=>signed)
700   (:translate ash)
701   (:args (number :scs (signed-reg) :target result
702                  :load-if (not (and (sc-is number signed-stack)
703                                     (sc-is result signed-stack)
704                                     (location= number result))))
705          (amount :scs (unsigned-reg) :target ecx))
706   (:arg-types signed-num positive-fixnum)
707   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
708   (:results (result :scs (signed-reg) :from (:argument 0)
709                     :load-if (not (and (sc-is number signed-stack)
710                                        (sc-is result signed-stack)
711                                        (location= number result)))))
712   (:result-types signed-num)
713   (:policy :fast-safe)
714   (:note "inline ASH")
715   (:generator 4
716     (move result number)
717     (move ecx amount)
718     (inst shl result :cl)))
719
720 (define-vop (fast-ash-left/unsigned=>unsigned)
721   (:translate ash)
722   (:args (number :scs (unsigned-reg) :target result
723                  :load-if (not (and (sc-is number unsigned-stack)
724                                     (sc-is result unsigned-stack)
725                                     (location= number result))))
726          (amount :scs (unsigned-reg) :target ecx))
727   (:arg-types unsigned-num positive-fixnum)
728   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
729   (:results (result :scs (unsigned-reg) :from (:argument 0)
730                     :load-if (not (and (sc-is number unsigned-stack)
731                                        (sc-is result unsigned-stack)
732                                        (location= number result)))))
733   (:result-types unsigned-num)
734   (:policy :fast-safe)
735   (:note "inline ASH")
736   (:generator 4
737     (move result number)
738     (move ecx amount)
739     (inst shl result :cl)))
740
741 (define-vop (fast-ash/signed=>signed)
742   (:translate ash)
743   (:policy :fast-safe)
744   (:args (number :scs (signed-reg) :target result)
745          (amount :scs (signed-reg) :target ecx))
746   (:arg-types signed-num signed-num)
747   (:results (result :scs (signed-reg) :from (:argument 0)))
748   (:result-types signed-num)
749   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
750   (:note "inline ASH")
751   (:generator 5
752     (move result number)
753     (move ecx amount)
754     (inst or ecx ecx)
755     (inst jmp :ns positive)
756     (inst neg ecx)
757     (inst cmp ecx 63)
758     (inst jmp :be okay)
759     (inst mov ecx 63)
760     OKAY
761     (inst sar result :cl)
762     (inst jmp done)
763
764     POSITIVE
765     ;; The result-type ensures us that this shift will not overflow.
766     (inst shl result :cl)
767
768     DONE))
769
770 (define-vop (fast-ash/unsigned=>unsigned)
771   (:translate ash)
772   (:policy :fast-safe)
773   (:args (number :scs (unsigned-reg) :target result)
774          (amount :scs (signed-reg) :target ecx))
775   (:arg-types unsigned-num signed-num)
776   (:results (result :scs (unsigned-reg) :from (:argument 0)))
777   (:result-types unsigned-num)
778   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
779   (:note "inline ASH")
780   (:generator 5
781     (move result number)
782     (move ecx amount)
783     (inst or ecx ecx)
784     (inst jmp :ns positive)
785     (inst neg ecx)
786     (inst cmp ecx 63)
787     (inst jmp :be okay)
788     (inst xor result result)
789     (inst jmp done)
790     OKAY
791     (inst shr result :cl)
792     (inst jmp done)
793
794     POSITIVE
795     ;; The result-type ensures us that this shift will not overflow.
796     (inst shl result :cl)
797
798     DONE))
799
800 (in-package "SB!C")
801
802 (defknown %lea (integer integer (member 1 2 4 8 16) (signed-byte 64))
803   integer
804   (foldable flushable movable))
805
806 (defoptimizer (%lea derive-type) ((base index scale disp))
807   (when (and (constant-lvar-p scale)
808              (constant-lvar-p disp))
809     (let ((scale (lvar-value scale))
810           (disp (lvar-value disp))
811           (base-type (lvar-type base))
812           (index-type (lvar-type index)))
813       (when (and (numeric-type-p base-type)
814                  (numeric-type-p index-type))
815         (let ((base-lo (numeric-type-low base-type))
816               (base-hi (numeric-type-high base-type))
817               (index-lo (numeric-type-low index-type))
818               (index-hi (numeric-type-high index-type)))
819           (make-numeric-type :class 'integer
820                              :complexp :real
821                              :low (when (and base-lo index-lo)
822                                     (+ base-lo (* index-lo scale) disp))
823                              :high (when (and base-hi index-hi)
824                                      (+ base-hi (* index-hi scale) disp))))))))
825
826 (defun %lea (base index scale disp)
827   (+ base (* index scale) disp))
828
829 (in-package "SB!VM")
830
831 (define-vop (%lea/unsigned=>unsigned)
832   (:translate %lea)
833   (:policy :fast-safe)
834   (:args (base :scs (unsigned-reg))
835          (index :scs (unsigned-reg)))
836   (:info scale disp)
837   (:arg-types unsigned-num unsigned-num
838               (:constant (member 1 2 4 8))
839               (:constant (signed-byte 64)))
840   (:results (r :scs (unsigned-reg)))
841   (:result-types unsigned-num)
842   (:generator 5
843     (inst lea r (make-ea :qword :base base :index index
844                          :scale scale :disp disp))))
845
846 (define-vop (%lea/signed=>signed)
847   (:translate %lea)
848   (:policy :fast-safe)
849   (:args (base :scs (signed-reg))
850          (index :scs (signed-reg)))
851   (:info scale disp)
852   (:arg-types signed-num signed-num
853               (:constant (member 1 2 4 8))
854               (:constant (signed-byte 64)))
855   (:results (r :scs (signed-reg)))
856   (:result-types signed-num)
857   (:generator 4
858     (inst lea r (make-ea :qword :base base :index index
859                          :scale scale :disp disp))))
860
861 (define-vop (%lea/fixnum=>fixnum)
862   (:translate %lea)
863   (:policy :fast-safe)
864   (:args (base :scs (any-reg))
865          (index :scs (any-reg)))
866   (:info scale disp)
867   (:arg-types tagged-num tagged-num
868               (:constant (member 1 2 4 8))
869               (:constant (signed-byte 64)))
870   (:results (r :scs (any-reg)))
871   (:result-types tagged-num)
872   (:generator 3
873     (inst lea r (make-ea :qword :base base :index index
874                          :scale scale :disp disp))))
875
876 ;;; FIXME: before making knowledge of this too public, it needs to be
877 ;;; fixed so that it's actually _faster_ than the non-CMOV version; at
878 ;;; least on my Celeron-XXX laptop, this version is marginally slower
879 ;;; than the above version with branches.  -- CSR, 2003-09-04
880 (define-vop (fast-cmov-ash/unsigned=>unsigned)
881   (:translate ash)
882   (:policy :fast-safe)
883   (:args (number :scs (unsigned-reg) :target result)
884          (amount :scs (signed-reg) :target ecx))
885   (:arg-types unsigned-num signed-num)
886   (:results (result :scs (unsigned-reg) :from (:argument 0)))
887   (:result-types unsigned-num)
888   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
889   (:temporary (:sc any-reg :from (:eval 0) :to (:eval 1)) zero)
890   (:note "inline ASH")
891   (:guard (member :cmov *backend-subfeatures*))
892   (:generator 4
893     (move result number)
894     (move ecx amount)
895     (inst or ecx ecx)
896     (inst jmp :ns positive)
897     (inst neg ecx)
898     (inst xor zero zero)
899     (inst shr result :cl)
900     (inst cmp ecx 63)
901     (inst cmov :nbe result zero)
902     (inst jmp done)
903     
904     POSITIVE
905     ;; The result-type ensures us that this shift will not overflow.
906     (inst shl result :cl)
907
908     DONE))
909 \f
910 ;;; Note: documentation for this function is wrong - rtfm
911 (define-vop (signed-byte-64-len)
912   (:translate integer-length)
913   (:note "inline (signed-byte 32) integer-length")
914   (:policy :fast-safe)
915   (:args (arg :scs (signed-reg) :target res))
916   (:arg-types signed-num)
917   (:results (res :scs (unsigned-reg)))
918   (:result-types unsigned-num)
919   (:generator 28
920     (move res arg)
921     (inst cmp res 0)
922     (inst jmp :ge POS)
923     (inst not res)
924     POS
925     (inst bsr res res)
926     (inst jmp :z zero)
927     (inst inc res)
928     (inst jmp done)
929     ZERO
930     (inst xor res res)
931     DONE))
932
933 (define-vop (unsigned-byte-64-len)
934   (:translate integer-length)
935   (:note "inline (unsigned-byte 32) integer-length")
936   (:policy :fast-safe)
937   (:args (arg :scs (unsigned-reg)))
938   (:arg-types unsigned-num)
939   (:results (res :scs (unsigned-reg)))
940   (:result-types unsigned-num)
941   (:generator 26
942     (inst bsr res arg)
943     (inst jmp :z zero)
944     (inst inc res)
945     (inst jmp done)
946     ZERO
947     (inst xor res res)
948     DONE))
949
950
951 (define-vop (unsigned-byte-64-count)
952   (:translate logcount)
953   (:note "inline (unsigned-byte 64) logcount")
954   (:policy :fast-safe)
955   (:args (arg :scs (unsigned-reg)))
956   (:arg-types unsigned-num)
957   (:results (result :scs (unsigned-reg)))
958   (:result-types positive-fixnum)
959   (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
960   (:temporary (:sc unsigned-reg :from (:argument 0)) t1)
961   (:generator 60
962     (move result arg)
963
964     (inst mov temp result)  
965     (inst shr temp 1)
966     (inst and result #x55555555)        ; note these masks will restrict the 
967     (inst and temp #x55555555)          ; count to the lower half of arg
968     (inst add result temp)
969
970     (inst mov temp result)
971     (inst shr temp 2)
972     (inst and result #x33333333)
973     (inst and temp #x33333333)
974     (inst add result temp)
975
976     (inst mov temp result)
977     (inst shr temp 4)
978     (inst and result #x0f0f0f0f)
979     (inst and temp #x0f0f0f0f)
980     (inst add result temp)
981
982     (inst mov temp result)
983     (inst shr temp 8)
984     (inst and result #x00ff00ff)
985     (inst and temp #x00ff00ff)
986     (inst add result temp)
987
988     (inst mov temp result)
989     (inst shr temp 16)
990     (inst and result #x0000ffff)
991     (inst and temp #x0000ffff)
992     (inst add result temp)
993
994     ;;; now do the upper half
995     (move t1 arg)
996     (inst bswap t1)
997
998     (inst mov temp t1)  
999     (inst shr temp 1)
1000     (inst and t1 #x55555555) 
1001     (inst and temp #x55555555)
1002     (inst add t1 temp)
1003
1004     (inst mov temp t1)
1005     (inst shr temp 2)
1006     (inst and t1 #x33333333)
1007     (inst and temp #x33333333)
1008     (inst add t1 temp)
1009
1010     (inst mov temp t1)
1011     (inst shr temp 4)
1012     (inst and t1 #x0f0f0f0f)
1013     (inst and temp #x0f0f0f0f)
1014     (inst add t1 temp)
1015
1016     (inst mov temp t1)
1017     (inst shr temp 8)
1018     (inst and t1 #x00ff00ff)
1019     (inst and temp #x00ff00ff)
1020     (inst add t1 temp)
1021
1022     (inst mov temp t1)
1023     (inst shr temp 16)
1024     (inst and t1 #x0000ffff)
1025     (inst and temp #x0000ffff)
1026     (inst add t1 temp)
1027     (inst add result t1)))
1028
1029
1030 \f
1031 ;;;; binary conditional VOPs
1032
1033 (define-vop (fast-conditional)
1034   (:conditional)
1035   (:info target not-p)
1036   (:effects)
1037   (:affected)
1038   (:policy :fast-safe))
1039
1040 ;;; constant variants are declared for 32 bits not 64 bits, because
1041 ;;; loading a 64 bit constant is silly
1042
1043 (define-vop (fast-conditional/fixnum fast-conditional)
1044   (:args (x :scs (any-reg)
1045             :load-if (not (and (sc-is x control-stack)
1046                                (sc-is y any-reg))))
1047          (y :scs (any-reg control-stack)))
1048   (:arg-types tagged-num tagged-num)
1049   (:note "inline fixnum comparison"))
1050
1051 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
1052   (:args (x :scs (any-reg control-stack)))
1053   (:arg-types tagged-num (:constant (signed-byte 29)))
1054   (:info target not-p y))
1055
1056 (define-vop (fast-conditional/signed fast-conditional)
1057   (:args (x :scs (signed-reg)
1058             :load-if (not (and (sc-is x signed-stack)
1059                                (sc-is y signed-reg))))
1060          (y :scs (signed-reg signed-stack)))
1061   (:arg-types signed-num signed-num)
1062   (:note "inline (signed-byte 32) comparison"))
1063
1064 (define-vop (fast-conditional-c/signed fast-conditional/signed)
1065   (:args (x :scs (signed-reg signed-stack)))
1066   (:arg-types signed-num (:constant (signed-byte 32)))
1067   (:info target not-p y))
1068
1069 (define-vop (fast-conditional/unsigned fast-conditional)
1070   (:args (x :scs (unsigned-reg)
1071             :load-if (not (and (sc-is x unsigned-stack)
1072                                (sc-is y unsigned-reg))))
1073          (y :scs (unsigned-reg unsigned-stack)))
1074   (:arg-types unsigned-num unsigned-num)
1075   (:note "inline (unsigned-byte 32) comparison"))
1076
1077 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
1078   (:args (x :scs (unsigned-reg unsigned-stack)))
1079   (:arg-types unsigned-num (:constant (unsigned-byte 32)))
1080   (:info target not-p y))
1081
1082
1083 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
1084              `(progn
1085                 ,@(mapcar
1086                    (lambda (suffix cost signed)
1087                      `(define-vop (;; FIXME: These could be done more
1088                                    ;; cleanly with SYMBOLICATE.
1089                                    ,(intern (format nil "~:@(FAST-IF-~A~A~)"
1090                                                     tran suffix))
1091                                    ,(intern
1092                                      (format nil "~:@(FAST-CONDITIONAL~A~)"
1093                                              suffix)))
1094                         (:translate ,tran)
1095                         (:generator ,cost
1096                                     (inst cmp x
1097                                           ,(if (eq suffix '-c/fixnum)
1098                                                '(fixnumize y)
1099                                                'y))
1100                                     (inst jmp (if not-p
1101                                                   ,(if signed
1102                                                        not-cond
1103                                                        not-unsigned)
1104                                                   ,(if signed
1105                                                        cond
1106                                                        unsigned))
1107                                           target))))
1108                    '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
1109 ;                  '(/fixnum  /signed  /unsigned)
1110                    '(4 3 6 5 6 5)
1111                    '(t t t t nil nil)))))
1112
1113   (define-conditional-vop < :l :b :ge :ae)
1114   (define-conditional-vop > :g :a :le :be))
1115
1116 (define-vop (fast-if-eql/signed fast-conditional/signed)
1117   (:translate eql)
1118   (:generator 6
1119     (inst cmp x y)
1120     (inst jmp (if not-p :ne :e) target)))
1121
1122 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
1123   (:translate eql)
1124   (:generator 5
1125     (cond ((and (sc-is x signed-reg) (zerop y))
1126            (inst test x x))  ; smaller instruction
1127           (t
1128            (inst cmp x y)))
1129     (inst jmp (if not-p :ne :e) target)))
1130
1131 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1132   (:translate eql)
1133   (:generator 6
1134     (inst cmp x y)
1135     (inst jmp (if not-p :ne :e) target)))
1136
1137 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1138   (:translate eql)
1139   (:generator 5
1140     (cond ((and (sc-is x unsigned-reg) (zerop y))
1141            (inst test x x))  ; smaller instruction
1142           (t
1143            (inst cmp x y)))
1144     (inst jmp (if not-p :ne :e) target)))
1145
1146 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1147 ;;; known fixnum.
1148
1149 ;;; These versions specify a fixnum restriction on their first arg. We have
1150 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1151 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1152 ;;; fixnum specific operations from being used on word integers, spuriously
1153 ;;; consing the argument.
1154
1155 (define-vop (fast-eql/fixnum fast-conditional)
1156   (:args (x :scs (any-reg)
1157             :load-if (not (and (sc-is x control-stack)
1158                                (sc-is y any-reg))))
1159          (y :scs (any-reg control-stack)))
1160   (:arg-types tagged-num tagged-num)
1161   (:note "inline fixnum comparison")
1162   (:translate eql)
1163   (:generator 4
1164     (inst cmp x y)
1165     (inst jmp (if not-p :ne :e) target)))
1166 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1167   (:args (x :scs (any-reg descriptor-reg)
1168             :load-if (not (and (sc-is x control-stack)
1169                                (sc-is y any-reg))))
1170          (y :scs (any-reg control-stack)))
1171   (:arg-types * tagged-num)
1172   (:variant-cost 7))
1173
1174
1175 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1176   (:args (x :scs (any-reg control-stack)))
1177   (:arg-types tagged-num (:constant (signed-byte 29)))
1178   (:info target not-p y)
1179   (:translate eql)
1180   (:generator 2
1181     (cond ((and (sc-is x any-reg) (zerop y))
1182            (inst test x x))  ; smaller instruction
1183           (t
1184            (inst cmp x (fixnumize y))))
1185     (inst jmp (if not-p :ne :e) target)))
1186
1187 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1188   (:args (x :scs (any-reg descriptor-reg control-stack)))
1189   (:arg-types * (:constant (signed-byte 29)))
1190   (:variant-cost 6))
1191 \f
1192 ;;;; 32-bit logical operations
1193
1194 (define-vop (merge-bits)
1195   (:translate merge-bits)
1196   (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1197          (prev :scs (unsigned-reg) :target result)
1198          (next :scs (unsigned-reg)))
1199   (:arg-types tagged-num unsigned-num unsigned-num)
1200   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1201   (:results (result :scs (unsigned-reg) :from (:argument 1)))
1202   (:result-types unsigned-num)
1203   (:policy :fast-safe)
1204   (:generator 4
1205     (move ecx shift)
1206     (move result prev)
1207     (inst shrd result next :cl)))
1208
1209 (define-source-transform 64bit-logical-not (x)
1210   `(logand (lognot (the (unsigned-byte 64) ,x)) #.(1- (ash 1 64))))
1211
1212 (deftransform 64bit-logical-and ((x y))
1213   '(logand x y))
1214
1215 (define-source-transform 64bit-logical-nand (x y)
1216   `(64bit-logical-not (64bit-logical-and ,x ,y)))
1217
1218 (deftransform 64bit-logical-or ((x y))
1219   '(logior x y))
1220
1221 (define-source-transform 64bit-logical-nor (x y)
1222   `(64bit-logical-not (64bit-logical-or ,x ,y)))
1223
1224 (deftransform 64bit-logical-xor ((x y))
1225   '(logxor x y))
1226
1227 (define-source-transform 64bit-logical-eqv (x y)
1228   `(64bit-logical-not (64bit-logical-xor ,x ,y)))
1229
1230 (define-source-transform 64bit-logical-orc1 (x y)
1231   `(64bit-logical-or (64bit-logical-not ,x) ,y))
1232
1233 (define-source-transform 64bit-logical-orc2 (x y)
1234   `(64bit-logical-or ,x (64bit-logical-not ,y)))
1235
1236 (define-source-transform 64bit-logical-andc1 (x y)
1237   `(64bit-logical-and (64bit-logical-not ,x) ,y))
1238
1239 (define-source-transform 64bit-logical-andc2 (x y)
1240   `(64bit-logical-and ,x (64bit-logical-not ,y)))
1241
1242 ;;; Only the lower 6 bits of the shift amount are significant.
1243 (define-vop (shift-towards-someplace)
1244   (:policy :fast-safe)
1245   (:args (num :scs (unsigned-reg) :target r)
1246          (amount :scs (signed-reg) :target ecx))
1247   (:arg-types unsigned-num tagged-num)
1248   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1249   (:results (r :scs (unsigned-reg) :from (:argument 0)))
1250   (:result-types unsigned-num))
1251
1252 (define-vop (shift-towards-start shift-towards-someplace)
1253   (:translate shift-towards-start)
1254   (:note "SHIFT-TOWARDS-START")
1255   (:generator 1
1256     (move r num)
1257     (move ecx amount)
1258     (inst shr r :cl)))
1259
1260 (define-vop (shift-towards-end shift-towards-someplace)
1261   (:translate shift-towards-end)
1262   (:note "SHIFT-TOWARDS-END")
1263   (:generator 1
1264     (move r num)
1265     (move ecx amount)
1266     (inst shl r :cl)))
1267 \f
1268 ;;;; Modular functions
1269
1270 (define-modular-fun +-mod64 (x y) + 64)
1271 (define-vop (fast-+-mod64/unsigned=>unsigned fast-+/unsigned=>unsigned)
1272   (:translate +-mod64))
1273 (define-vop (fast-+-mod64-c/unsigned=>unsigned fast-+-c/unsigned=>unsigned)
1274   (:translate +-mod64))
1275 (define-modular-fun --mod64 (x y) - 64)
1276 (define-vop (fast---mod64/unsigned=>unsigned fast--/unsigned=>unsigned)
1277   (:translate --mod64))
1278 (define-vop (fast---mod64-c/unsigned=>unsigned fast---c/unsigned=>unsigned)
1279   (:translate --mod64))
1280
1281 (define-modular-fun *-mod64 (x y) * 64)
1282 (define-vop (fast-*-mod64/unsigned=>unsigned fast-*/unsigned=>unsigned)
1283   (:translate *-mod64))
1284 ;;; (no -C variant as x86 MUL instruction doesn't take an immediate)
1285
1286 (define-vop (fast-ash-left-mod64-c/unsigned=>unsigned
1287              fast-ash-c/unsigned=>unsigned)
1288   (:translate ash-left-mod64))
1289
1290 (in-package "SB!C")
1291
1292 (defknown sb!vm::%lea-mod64 (integer integer (member 1 2 4 8) (signed-byte 64))
1293   (unsigned-byte 64)
1294   (foldable flushable movable))
1295
1296 (define-modular-fun-optimizer %lea ((base index scale disp) :width width)
1297   (when (and (<= width 64)
1298              (constant-lvar-p scale)
1299              (constant-lvar-p disp))
1300     (cut-to-width base width)
1301     (cut-to-width index width)
1302     'sb!vm::%lea-mod64))
1303
1304 #+sb-xc-host
1305 (defun sb!vm::%lea-mod64 (base index scale disp)
1306   (ldb (byte 64 0) (%lea base index scale disp)))
1307 #-sb-xc-host
1308 (defun sb!vm::%lea-mod64 (base index scale disp)
1309   (let ((base (logand base #xffffffffffffffff))
1310         (index (logand index #xffffffffffffffff)))
1311     ;; can't use modular version of %LEA, as we only have VOPs for
1312     ;; constant SCALE and DISP.
1313     (ldb (byte 64 0) (+ base (* index scale) disp))))
1314
1315 (in-package "SB!VM")
1316
1317 (define-vop (%lea-mod64/unsigned=>unsigned
1318              %lea/unsigned=>unsigned)
1319   (:translate %lea-mod64))
1320
1321 ;;; logical operations
1322 (define-modular-fun lognot-mod64 (x) lognot 64)
1323 (define-vop (lognot-mod64/unsigned=>unsigned)
1324   (:translate lognot-mod64)
1325   (:args (x :scs (unsigned-reg unsigned-stack) :target r
1326             :load-if (not (and (sc-is x unsigned-stack)
1327                                (sc-is r unsigned-stack)
1328                                (location= x r)))))
1329   (:arg-types unsigned-num)
1330   (:results (r :scs (unsigned-reg)
1331                :load-if (not (and (sc-is x unsigned-stack)
1332                                   (sc-is r unsigned-stack)
1333                                   (location= x r)))))
1334   (:result-types unsigned-num)
1335   (:policy :fast-safe)
1336   (:generator 1
1337     (move r x)
1338     (inst not r)))
1339
1340 (define-modular-fun logxor-mod64 (x y) logxor 64)
1341 (define-vop (fast-logxor-mod64/unsigned=>unsigned
1342              fast-logxor/unsigned=>unsigned)
1343   (:translate logxor-mod64))
1344 (define-vop (fast-logxor-mod64-c/unsigned=>unsigned
1345              fast-logxor-c/unsigned=>unsigned)
1346   (:translate logxor-mod64))
1347
1348 (define-source-transform logeqv (&rest args)
1349   (if (oddp (length args))
1350       `(logxor ,@args)
1351       `(lognot (logxor ,@args))))
1352 (define-source-transform logandc1 (x y)
1353   `(logand (lognot ,x) ,y))
1354 (define-source-transform logandc2 (x y)
1355   `(logand ,x (lognot ,y)))
1356 (define-source-transform logorc1 (x y)
1357   `(logior (lognot ,x) ,y))
1358 (define-source-transform logorc2 (x y)
1359   `(logior ,x (lognot ,y)))
1360 (define-source-transform lognor (x y)
1361   `(lognot (logior ,x ,y)))
1362 (define-source-transform lognand (x y)
1363   `(lognot (logand ,x ,y)))
1364 \f
1365 ;;;; bignum stuff
1366
1367 (define-vop (bignum-length get-header-data)
1368   (:translate sb!bignum:%bignum-length)
1369   (:policy :fast-safe))
1370
1371 (define-vop (bignum-set-length set-header-data)
1372   (:translate sb!bignum:%bignum-set-length)
1373   (:policy :fast-safe))
1374
1375 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1376   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1377
1378 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1379   (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1380
1381 (define-vop (digit-0-or-plus)
1382   (:translate sb!bignum:%digit-0-or-plusp)
1383   (:policy :fast-safe)
1384   (:args (digit :scs (unsigned-reg)))
1385   (:arg-types unsigned-num)
1386   (:conditional)
1387   (:info target not-p)
1388   (:generator 3
1389     (inst or digit digit)
1390     (inst jmp (if not-p :s :ns) target)))
1391
1392
1393 ;;; For add and sub with carry the sc of carry argument is any-reg so
1394 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1395 ;;; 4. This is easy to deal with and may save a fixnum-word
1396 ;;; conversion.
1397 (define-vop (add-w/carry)
1398   (:translate sb!bignum:%add-with-carry)
1399   (:policy :fast-safe)
1400   (:args (a :scs (unsigned-reg) :target result)
1401          (b :scs (unsigned-reg unsigned-stack) :to :eval)
1402          (c :scs (any-reg) :target temp))
1403   (:arg-types unsigned-num unsigned-num positive-fixnum)
1404   (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1405   (:results (result :scs (unsigned-reg) :from (:argument 0))
1406             (carry :scs (unsigned-reg)))
1407   (:result-types unsigned-num positive-fixnum)
1408   (:generator 4
1409     (move result a)
1410     (move temp c)
1411     (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1412     (inst adc result b)
1413     (inst mov carry 0)
1414     (inst adc carry carry)))
1415
1416 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1417 ;;; borrow and 0 for a borrow.
1418 (define-vop (sub-w/borrow)
1419   (:translate sb!bignum:%subtract-with-borrow)
1420   (:policy :fast-safe)
1421   (:args (a :scs (unsigned-reg) :to :eval :target result)
1422          (b :scs (unsigned-reg unsigned-stack) :to :result)
1423          (c :scs (any-reg control-stack)))
1424   (:arg-types unsigned-num unsigned-num positive-fixnum)
1425   (:results (result :scs (unsigned-reg) :from :eval)
1426             (borrow :scs (unsigned-reg)))
1427   (:result-types unsigned-num positive-fixnum)
1428   (:generator 5
1429     (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1430     (move result a)
1431     (inst sbb result b)
1432     (inst mov borrow 0)
1433     (inst adc borrow borrow)
1434     (inst xor borrow 1)))
1435
1436
1437 (define-vop (bignum-mult-and-add-3-arg)
1438   (:translate sb!bignum:%multiply-and-add)
1439   (:policy :fast-safe)
1440   (:args (x :scs (unsigned-reg) :target eax)
1441          (y :scs (unsigned-reg unsigned-stack))
1442          (carry-in :scs (unsigned-reg unsigned-stack)))
1443   (:arg-types unsigned-num unsigned-num unsigned-num)
1444   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1445                    :to (:result 1) :target lo) eax)
1446   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1447                    :to (:result 0) :target hi) edx)
1448   (:results (hi :scs (unsigned-reg))
1449             (lo :scs (unsigned-reg)))
1450   (:result-types unsigned-num unsigned-num)
1451   (:generator 20
1452     (move eax x)
1453     (inst mul eax y)
1454     (inst add eax carry-in)
1455     (inst adc edx 0)
1456     (move hi edx)
1457     (move lo eax)))
1458
1459 (define-vop (bignum-mult-and-add-4-arg)
1460   (:translate sb!bignum:%multiply-and-add)
1461   (:policy :fast-safe)
1462   (:args (x :scs (unsigned-reg) :target eax)
1463          (y :scs (unsigned-reg unsigned-stack))
1464          (prev :scs (unsigned-reg unsigned-stack))
1465          (carry-in :scs (unsigned-reg unsigned-stack)))
1466   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1467   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1468                    :to (:result 1) :target lo) eax)
1469   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1470                    :to (:result 0) :target hi) edx)
1471   (:results (hi :scs (unsigned-reg))
1472             (lo :scs (unsigned-reg)))
1473   (:result-types unsigned-num unsigned-num)
1474   (:generator 20
1475     (move eax x)
1476     (inst mul eax y)
1477     (inst add eax prev)
1478     (inst adc edx 0)
1479     (inst add eax carry-in)
1480     (inst adc edx 0)
1481     (move hi edx)
1482     (move lo eax)))
1483
1484
1485 (define-vop (bignum-mult)
1486   (:translate sb!bignum:%multiply)
1487   (:policy :fast-safe)
1488   (:args (x :scs (unsigned-reg) :target eax)
1489          (y :scs (unsigned-reg unsigned-stack)))
1490   (:arg-types unsigned-num unsigned-num)
1491   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1492                    :to (:result 1) :target lo) eax)
1493   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1494                    :to (:result 0) :target hi) edx)
1495   (:results (hi :scs (unsigned-reg))
1496             (lo :scs (unsigned-reg)))
1497   (:result-types unsigned-num unsigned-num)
1498   (:generator 20
1499     (move eax x)
1500     (inst mul eax y)
1501     (move hi edx)
1502     (move lo eax)))
1503
1504 (define-vop (bignum-lognot lognot-mod64/unsigned=>unsigned)
1505   (:translate sb!bignum:%lognot))
1506
1507 (define-vop (fixnum-to-digit)
1508   (:translate sb!bignum:%fixnum-to-digit)
1509   (:policy :fast-safe)
1510   (:args (fixnum :scs (any-reg control-stack) :target digit))
1511   (:arg-types tagged-num)
1512   (:results (digit :scs (unsigned-reg)
1513                    :load-if (not (and (sc-is fixnum control-stack)
1514                                       (sc-is digit unsigned-stack)
1515                                       (location= fixnum digit)))))
1516   (:result-types unsigned-num)
1517   (:generator 1
1518     (move digit fixnum)
1519     (inst sar digit 3)))
1520
1521 (define-vop (bignum-floor)
1522   (:translate sb!bignum:%floor)
1523   (:policy :fast-safe)
1524   (:args (div-high :scs (unsigned-reg) :target edx)
1525          (div-low :scs (unsigned-reg) :target eax)
1526          (divisor :scs (unsigned-reg unsigned-stack)))
1527   (:arg-types unsigned-num unsigned-num unsigned-num)
1528   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1529                    :to (:result 0) :target quo) eax)
1530   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1531                    :to (:result 1) :target rem) edx)
1532   (:results (quo :scs (unsigned-reg))
1533             (rem :scs (unsigned-reg)))
1534   (:result-types unsigned-num unsigned-num)
1535   (:generator 300
1536     (move edx div-high)
1537     (move eax div-low)
1538     (inst div eax divisor)
1539     (move quo eax)
1540     (move rem edx)))
1541
1542 (define-vop (signify-digit)
1543   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1544   (:policy :fast-safe)
1545   (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1546   (:arg-types unsigned-num)
1547   (:results (res :scs (any-reg signed-reg)
1548                  :load-if (not (and (sc-is digit unsigned-stack)
1549                                     (sc-is res control-stack signed-stack)
1550                                     (location= digit res)))))
1551   (:result-types signed-num)
1552   (:generator 1
1553     (move res digit)
1554     (when (sc-is res any-reg control-stack)
1555       (inst shl res 3))))
1556
1557 (define-vop (digit-ashr)
1558   (:translate sb!bignum:%ashr)
1559   (:policy :fast-safe)
1560   (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1561          (count :scs (unsigned-reg) :target ecx))
1562   (:arg-types unsigned-num positive-fixnum)
1563   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1564   (:results (result :scs (unsigned-reg) :from (:argument 0)
1565                     :load-if (not (and (sc-is result unsigned-stack)
1566                                        (location= digit result)))))
1567   (:result-types unsigned-num)
1568   (:generator 1
1569     (move result digit)
1570     (move ecx count)
1571     (inst sar result :cl)))
1572
1573 (define-vop (digit-lshr digit-ashr)
1574   (:translate sb!bignum:%digit-logical-shift-right)
1575   (:generator 1
1576     (move result digit)
1577     (move ecx count)
1578     (inst shr result :cl)))
1579
1580 (define-vop (digit-ashl digit-ashr)
1581   (:translate sb!bignum:%ashl)
1582   (:generator 1
1583     (move result digit)
1584     (move ecx count)
1585     (inst shl result :cl)))
1586 \f
1587 ;;;; static functions
1588
1589 (define-static-fun two-arg-/ (x y) :translate /)
1590
1591 (define-static-fun two-arg-gcd (x y) :translate gcd)
1592 (define-static-fun two-arg-lcm (x y) :translate lcm)
1593
1594 (define-static-fun two-arg-and (x y) :translate logand)
1595 (define-static-fun two-arg-ior (x y) :translate logior)
1596 (define-static-fun two-arg-xor (x y) :translate logxor)
1597
1598
1599 (in-package "SB!C")
1600
1601 ;;; This is essentially a straight implementation of the algorithm in
1602 ;;; "Strength Reduction of Multiplications by Integer Constants",
1603 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1604 (defun basic-decompose-multiplication (arg num n-bits condensed)
1605   (case (aref condensed 0)
1606     (0
1607      (let ((tmp (min 3 (aref condensed 1))))
1608        (decf (aref condensed 1) tmp)
1609        `(logand #xffffffff
1610          (%lea ,arg
1611                ,(decompose-multiplication
1612                  arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1613                ,(ash 1 tmp) 0))))
1614     ((1 2 3)
1615      (let ((r0 (aref condensed 0)))
1616        (incf (aref condensed 1) r0)
1617        `(logand #xffffffff
1618          (%lea ,(decompose-multiplication
1619                  arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1620                ,arg
1621                ,(ash 1 r0) 0))))
1622     (t (let ((r0 (aref condensed 0)))
1623          (setf (aref condensed 0) 0)
1624          `(logand #xffffffff
1625            (ash ,(decompose-multiplication
1626                   arg (ash num (- r0)) n-bits condensed)
1627                 ,r0))))))
1628
1629 (defun decompose-multiplication (arg num n-bits condensed)
1630   (cond
1631     ((= n-bits 0) 0)
1632     ((= num 1) arg)
1633     ((= n-bits 1)
1634      `(logand #xffffffff (ash ,arg ,(1- (integer-length num)))))
1635     ((let ((max 0) (end 0))
1636        (loop for i from 2 to (length condensed)
1637              for j = (reduce #'+ (subseq condensed 0 i))
1638              when (and (> (- (* 2 i) 3 j) max)
1639                        (< (+ (ash 1 (1+ j))
1640                              (ash (ldb (byte (- 64 (1+ j)) (1+ j)) num)
1641                                   (1+ j)))
1642                           (ash 1 64)))
1643                do (setq max (- (* 2 i) 3 j)
1644                         end i))
1645        (when (> max 0)
1646          (let ((j (reduce #'+ (subseq condensed 0 end))))
1647            (let ((n2 (+ (ash 1 (1+ j))
1648                         (ash (ldb (byte (- 64 (1+ j)) (1+ j)) num) (1+ j))))
1649                  (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1650            `(logand #xffffffff
1651              (- ,(optimize-multiply arg n2) ,(optimize-multiply arg n1))))))))
1652     ((dolist (i '(9 5 3))
1653        (when (integerp (/ num i))
1654          (when (< (logcount (/ num i)) (logcount num))
1655            (let ((x (gensym)))
1656              (return `(let ((,x ,(optimize-multiply arg (/ num i))))
1657                        (logand #xffffffff
1658                         (%lea ,x ,x (1- ,i) 0)))))))))
1659     (t (basic-decompose-multiplication arg num n-bits condensed))))
1660            
1661 (defun optimize-multiply (arg x)
1662   (let* ((n-bits (logcount x))
1663          (condensed (make-array n-bits)))
1664     (let ((count 0) (bit 0))
1665       (dotimes (i 64)
1666         (cond ((logbitp i x)
1667                (setf (aref condensed bit) count)
1668                (setf count 1)
1669                (incf bit))
1670               (t (incf count)))))
1671     (decompose-multiplication arg x n-bits condensed)))
1672
1673 (defun *-transformer (y)
1674   (cond
1675     (t (give-up-ir1-transform))
1676     ((= y (ash 1 (integer-length y)))
1677      ;; there's a generic transform for y = 2^k
1678      (give-up-ir1-transform))
1679     ((member y '(3 5 9))
1680      ;; we can do these multiplications directly using LEA
1681      `(%lea x x ,(1- y) 0))
1682     ((member :pentium4 *backend-subfeatures*)
1683      ;; the pentium4's multiply unit is reportedly very good
1684      (give-up-ir1-transform))
1685     ;; FIXME: should make this more fine-grained.  If nothing else,
1686     ;; there should probably be a cutoff of about 9 instructions on
1687     ;; pentium-class machines.
1688     (t (optimize-multiply 'x y))))
1689
1690 (deftransform * ((x y)
1691                  ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1692                  (unsigned-byte 64))
1693   "recode as leas, shifts and adds"
1694   (let ((y (lvar-value y)))
1695     (*-transformer y)))
1696
1697 (deftransform sb!vm::*-mod64
1698     ((x y) ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1699      (unsigned-byte 64))
1700   "recode as leas, shifts and adds"
1701   (let ((y (lvar-value y)))
1702     (*-transformer y)))
1703
1704 ;;; FIXME: we should also be able to write an optimizer or two to
1705 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.