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