0.8.18.16:
[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                  ((zerop amount)  )
609                  ((< amount -63)
610                   (inst xor result result))
611                  (t 
612                   ;; shift too far then back again, to zero tag bits
613                   (inst sar result (- 3 amount))
614                   (inst shl result 3)))))))
615
616
617 (define-vop (fast-ash-left/fixnum=>fixnum)
618   (:translate ash)
619   (:args (number :scs (any-reg) :target result
620                  :load-if (not (and (sc-is number control-stack)
621                                     (sc-is result control-stack)
622                                     (location= number result))))
623          (amount :scs (unsigned-reg) :target ecx))
624   (:arg-types tagged-num positive-fixnum)
625   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
626   (:results (result :scs (any-reg) :from (:argument 0)
627                     :load-if (not (and (sc-is number control-stack)
628                                        (sc-is result control-stack)
629                                        (location= number result)))))
630   (:result-types tagged-num)
631   (:policy :fast-safe)
632   (:note "inline ASH")
633   (:generator 3
634     (move result number)
635     (move ecx amount)
636     ;; The result-type ensures us that this shift will not overflow.
637     (inst shl result :cl)))
638
639 (define-vop (fast-ash-c/signed=>signed)
640   (:translate ash)
641   (:policy :fast-safe)
642   (:args (number :scs (signed-reg) :target result
643                  :load-if (not (and (sc-is number signed-stack)
644                                     (sc-is result signed-stack)
645                                     (location= number result)))))
646   (:info amount)
647   (:arg-types signed-num (:constant integer))
648   (:results (result :scs (signed-reg)
649                     :load-if (not (and (sc-is number signed-stack)
650                                        (sc-is result signed-stack)
651                                        (location= number result)))))
652   (:result-types signed-num)
653   (:note "inline ASH")
654   (:generator 3
655     (cond ((and (= amount 1) (not (location= number result)))
656            (inst lea result (make-ea :qword :index number :scale 2)))
657           ((and (= amount 2) (not (location= number result)))
658            (inst lea result (make-ea :qword :index number :scale 4)))
659           ((and (= amount 3) (not (location= number result)))
660            (inst lea result (make-ea :qword :index number :scale 8)))
661           (t
662            (move result number)
663            (cond ((plusp amount) (inst shl result amount))
664                  (t (inst sar result (min 63 (- amount)))))))))
665
666 (define-vop (fast-ash-c/unsigned=>unsigned)
667   (:translate ash)
668   (:policy :fast-safe)
669   (:args (number :scs (unsigned-reg) :target result
670                  :load-if (not (and (sc-is number unsigned-stack)
671                                     (sc-is result unsigned-stack)
672                                     (location= number result)))))
673   (:info amount)
674   (:arg-types unsigned-num (:constant integer))
675   (:results (result :scs (unsigned-reg)
676                     :load-if (not (and (sc-is number unsigned-stack)
677                                        (sc-is result unsigned-stack)
678                                        (location= number result)))))
679   (:result-types unsigned-num)
680   (:note "inline ASH")
681   (:generator 3
682     (cond ((and (= amount 1) (not (location= number result)))
683            (inst lea result (make-ea :qword :index number :scale 2)))
684           ((and (= amount 2) (not (location= number result)))
685            (inst lea result (make-ea :qword :index number :scale 4)))
686           ((and (= amount 3) (not (location= number result)))
687            (inst lea result (make-ea :qword :index number :scale 8)))
688           (t
689            (move result number)
690            (cond ((< -64 amount 64) ;; XXXX
691                   ;; this code is used both in ASH and ASH-MOD32, so
692                   ;; be careful
693                   (if (plusp amount)
694                       (inst shl result amount)
695                       (inst shr result (- amount))))
696                  (t (if (sc-is result unsigned-reg)
697                         (inst xor result result)
698                         (inst mov result 0))))))))
699
700 (define-vop (fast-ash-left/signed=>signed)
701   (:translate ash)
702   (:args (number :scs (signed-reg) :target result
703                  :load-if (not (and (sc-is number signed-stack)
704                                     (sc-is result signed-stack)
705                                     (location= number result))))
706          (amount :scs (unsigned-reg) :target ecx))
707   (:arg-types signed-num positive-fixnum)
708   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
709   (:results (result :scs (signed-reg) :from (:argument 0)
710                     :load-if (not (and (sc-is number signed-stack)
711                                        (sc-is result signed-stack)
712                                        (location= number result)))))
713   (:result-types signed-num)
714   (:policy :fast-safe)
715   (:note "inline ASH")
716   (:generator 4
717     (move result number)
718     (move ecx amount)
719     (inst shl result :cl)))
720
721 (define-vop (fast-ash-left/unsigned=>unsigned)
722   (:translate ash)
723   (:args (number :scs (unsigned-reg) :target result
724                  :load-if (not (and (sc-is number unsigned-stack)
725                                     (sc-is result unsigned-stack)
726                                     (location= number result))))
727          (amount :scs (unsigned-reg) :target ecx))
728   (:arg-types unsigned-num positive-fixnum)
729   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
730   (:results (result :scs (unsigned-reg) :from (:argument 0)
731                     :load-if (not (and (sc-is number unsigned-stack)
732                                        (sc-is result unsigned-stack)
733                                        (location= number result)))))
734   (:result-types unsigned-num)
735   (:policy :fast-safe)
736   (:note "inline ASH")
737   (:generator 4
738     (move result number)
739     (move ecx amount)
740     (inst shl result :cl)))
741
742 (define-vop (fast-ash/signed=>signed)
743   (:translate ash)
744   (:policy :fast-safe)
745   (:args (number :scs (signed-reg) :target result)
746          (amount :scs (signed-reg) :target ecx))
747   (:arg-types signed-num signed-num)
748   (:results (result :scs (signed-reg) :from (:argument 0)))
749   (:result-types signed-num)
750   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
751   (:note "inline ASH")
752   (:generator 5
753     (move result number)
754     (move ecx amount)
755     (inst or ecx ecx)
756     (inst jmp :ns positive)
757     (inst neg ecx)
758     (inst cmp ecx 63)
759     (inst jmp :be okay)
760     (inst mov ecx 63)
761     OKAY
762     (inst sar result :cl)
763     (inst jmp done)
764
765     POSITIVE
766     ;; The result-type ensures us that this shift will not overflow.
767     (inst shl result :cl)
768
769     DONE))
770
771 (define-vop (fast-ash/unsigned=>unsigned)
772   (:translate ash)
773   (:policy :fast-safe)
774   (:args (number :scs (unsigned-reg) :target result)
775          (amount :scs (signed-reg) :target ecx))
776   (:arg-types unsigned-num signed-num)
777   (:results (result :scs (unsigned-reg) :from (:argument 0)))
778   (:result-types unsigned-num)
779   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
780   (:note "inline ASH")
781   (:generator 5
782     (move result number)
783     (move ecx amount)
784     (inst or ecx ecx)
785     (inst jmp :ns positive)
786     (inst neg ecx)
787     (inst cmp ecx 63)
788     (inst jmp :be okay)
789     (inst xor result result)
790     (inst jmp done)
791     OKAY
792     (inst shr result :cl)
793     (inst jmp done)
794
795     POSITIVE
796     ;; The result-type ensures us that this shift will not overflow.
797     (inst shl result :cl)
798
799     DONE))
800
801 (in-package "SB!C")
802
803 (defknown %lea (integer integer (member 1 2 4 8 16) (signed-byte 64))
804   integer
805   (foldable flushable movable))
806
807 (defoptimizer (%lea derive-type) ((base index scale disp))
808   (when (and (constant-lvar-p scale)
809              (constant-lvar-p disp))
810     (let ((scale (lvar-value scale))
811           (disp (lvar-value disp))
812           (base-type (lvar-type base))
813           (index-type (lvar-type index)))
814       (when (and (numeric-type-p base-type)
815                  (numeric-type-p index-type))
816         (let ((base-lo (numeric-type-low base-type))
817               (base-hi (numeric-type-high base-type))
818               (index-lo (numeric-type-low index-type))
819               (index-hi (numeric-type-high index-type)))
820           (make-numeric-type :class 'integer
821                              :complexp :real
822                              :low (when (and base-lo index-lo)
823                                     (+ base-lo (* index-lo scale) disp))
824                              :high (when (and base-hi index-hi)
825                                      (+ base-hi (* index-hi scale) disp))))))))
826
827 (defun %lea (base index scale disp)
828   (+ base (* index scale) disp))
829
830 (in-package "SB!VM")
831
832 (define-vop (%lea/unsigned=>unsigned)
833   (:translate %lea)
834   (:policy :fast-safe)
835   (:args (base :scs (unsigned-reg))
836          (index :scs (unsigned-reg)))
837   (:info scale disp)
838   (:arg-types unsigned-num unsigned-num
839               (:constant (member 1 2 4 8))
840               (:constant (signed-byte 64)))
841   (:results (r :scs (unsigned-reg)))
842   (:result-types unsigned-num)
843   (:generator 5
844     (inst lea r (make-ea :qword :base base :index index
845                          :scale scale :disp disp))))
846
847 (define-vop (%lea/signed=>signed)
848   (:translate %lea)
849   (:policy :fast-safe)
850   (:args (base :scs (signed-reg))
851          (index :scs (signed-reg)))
852   (:info scale disp)
853   (:arg-types signed-num signed-num
854               (:constant (member 1 2 4 8))
855               (:constant (signed-byte 64)))
856   (:results (r :scs (signed-reg)))
857   (:result-types signed-num)
858   (:generator 4
859     (inst lea r (make-ea :qword :base base :index index
860                          :scale scale :disp disp))))
861
862 (define-vop (%lea/fixnum=>fixnum)
863   (:translate %lea)
864   (:policy :fast-safe)
865   (:args (base :scs (any-reg))
866          (index :scs (any-reg)))
867   (:info scale disp)
868   (:arg-types tagged-num tagged-num
869               (:constant (member 1 2 4 8))
870               (:constant (signed-byte 64)))
871   (:results (r :scs (any-reg)))
872   (:result-types tagged-num)
873   (:generator 3
874     (inst lea r (make-ea :qword :base base :index index
875                          :scale scale :disp disp))))
876
877 ;;; FIXME: before making knowledge of this too public, it needs to be
878 ;;; fixed so that it's actually _faster_ than the non-CMOV version; at
879 ;;; least on my Celeron-XXX laptop, this version is marginally slower
880 ;;; than the above version with branches.  -- CSR, 2003-09-04
881 (define-vop (fast-cmov-ash/unsigned=>unsigned)
882   (:translate ash)
883   (:policy :fast-safe)
884   (:args (number :scs (unsigned-reg) :target result)
885          (amount :scs (signed-reg) :target ecx))
886   (:arg-types unsigned-num signed-num)
887   (:results (result :scs (unsigned-reg) :from (:argument 0)))
888   (:result-types unsigned-num)
889   (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
890   (:temporary (:sc any-reg :from (:eval 0) :to (:eval 1)) zero)
891   (:note "inline ASH")
892   (:guard (member :cmov *backend-subfeatures*))
893   (:generator 4
894     (move result number)
895     (move ecx amount)
896     (inst or ecx ecx)
897     (inst jmp :ns positive)
898     (inst neg ecx)
899     (inst xor zero zero)
900     (inst shr result :cl)
901     (inst cmp ecx 63)
902     (inst cmov :nbe result zero)
903     (inst jmp done)
904     
905     POSITIVE
906     ;; The result-type ensures us that this shift will not overflow.
907     (inst shl result :cl)
908
909     DONE))
910 \f
911 ;;; Note: documentation for this function is wrong - rtfm
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 (define-modular-fun +-mod64 (x y) + :unsigned 64)
1238 (define-vop (fast-+-mod64/unsigned=>unsigned fast-+/unsigned=>unsigned)
1239   (:translate +-mod64))
1240 (define-vop (fast-+-mod64-c/unsigned=>unsigned fast-+-c/unsigned=>unsigned)
1241   (:translate +-mod64))
1242 (define-modular-fun --mod64 (x y) - :unsigned 64)
1243 (define-vop (fast---mod64/unsigned=>unsigned fast--/unsigned=>unsigned)
1244   (:translate --mod64))
1245 (define-vop (fast---mod64-c/unsigned=>unsigned fast---c/unsigned=>unsigned)
1246   (:translate --mod64))
1247
1248 (define-modular-fun *-mod64 (x y) * :unsigned 64)
1249 (define-vop (fast-*-mod64/unsigned=>unsigned fast-*/unsigned=>unsigned)
1250   (:translate *-mod64))
1251 ;;; (no -C variant as x86 MUL instruction doesn't take an immediate)
1252
1253 (define-vop (fast-ash-left-mod64-c/unsigned=>unsigned
1254              fast-ash-c/unsigned=>unsigned)
1255   (:translate ash-left-mod64))
1256 (define-vop (fast-ash-left-mod64/unsigned=>unsigned
1257              fast-ash-left/unsigned=>unsigned))
1258 (deftransform ash-left-mod64 ((integer count)
1259                               ((unsigned-byte 64) (unsigned-byte 6)))
1260   (when (sb!c::constant-lvar-p count)
1261     (sb!c::give-up-ir1-transform))
1262   '(%primitive fast-ash-left-mod64/unsigned=>unsigned integer count))
1263
1264 (in-package "SB!C")
1265
1266 (defknown sb!vm::%lea-mod64 (integer integer (member 1 2 4 8) (signed-byte 64))
1267   (unsigned-byte 64)
1268   (foldable flushable movable))
1269
1270 (define-modular-fun-optimizer %lea ((base index scale disp) :unsigned :width width)
1271   (when (and (<= width 64)
1272              (constant-lvar-p scale)
1273              (constant-lvar-p disp))
1274     (cut-to-width base :unsigned width)
1275     (cut-to-width index :unsigned width)
1276     'sb!vm::%lea-mod64))
1277
1278 #+sb-xc-host
1279 (defun sb!vm::%lea-mod64 (base index scale disp)
1280   (ldb (byte 64 0) (%lea base index scale disp)))
1281 #-sb-xc-host
1282 (defun sb!vm::%lea-mod64 (base index scale disp)
1283   (let ((base (logand base #xffffffffffffffff))
1284         (index (logand index #xffffffffffffffff)))
1285     ;; can't use modular version of %LEA, as we only have VOPs for
1286     ;; constant SCALE and DISP.
1287     (ldb (byte 64 0) (+ base (* index scale) disp))))
1288
1289 (in-package "SB!VM")
1290
1291 (define-vop (%lea-mod64/unsigned=>unsigned
1292              %lea/unsigned=>unsigned)
1293   (:translate %lea-mod64))
1294
1295 ;;; logical operations
1296 (define-modular-fun lognot-mod64 (x) lognot :unsigned 64)
1297 (define-vop (lognot-mod64/unsigned=>unsigned)
1298   (:translate lognot-mod64)
1299   (:args (x :scs (unsigned-reg unsigned-stack) :target r
1300             :load-if (not (and (sc-is x unsigned-stack)
1301                                (sc-is r unsigned-stack)
1302                                (location= x r)))))
1303   (:arg-types unsigned-num)
1304   (:results (r :scs (unsigned-reg)
1305                :load-if (not (and (sc-is x unsigned-stack)
1306                                   (sc-is r unsigned-stack)
1307                                   (location= x r)))))
1308   (:result-types unsigned-num)
1309   (:policy :fast-safe)
1310   (:generator 1
1311     (move r x)
1312     (inst not r)))
1313
1314 (define-modular-fun logxor-mod64 (x y) logxor :unsigned 64)
1315 (define-vop (fast-logxor-mod64/unsigned=>unsigned
1316              fast-logxor/unsigned=>unsigned)
1317   (:translate logxor-mod64))
1318 (define-vop (fast-logxor-mod64-c/unsigned=>unsigned
1319              fast-logxor-c/unsigned=>unsigned)
1320   (:translate logxor-mod64))
1321
1322 (define-source-transform logeqv (&rest args)
1323   (if (oddp (length args))
1324       `(logxor ,@args)
1325       `(lognot (logxor ,@args))))
1326 (define-source-transform logandc1 (x y)
1327   `(logand (lognot ,x) ,y))
1328 (define-source-transform logandc2 (x y)
1329   `(logand ,x (lognot ,y)))
1330 (define-source-transform logorc1 (x y)
1331   `(logior (lognot ,x) ,y))
1332 (define-source-transform logorc2 (x y)
1333   `(logior ,x (lognot ,y)))
1334 (define-source-transform lognor (x y)
1335   `(lognot (logior ,x ,y)))
1336 (define-source-transform lognand (x y)
1337   `(lognot (logand ,x ,y)))
1338 \f
1339 ;;;; bignum stuff
1340
1341 (define-vop (bignum-length get-header-data)
1342   (:translate sb!bignum:%bignum-length)
1343   (:policy :fast-safe))
1344
1345 (define-vop (bignum-set-length set-header-data)
1346   (:translate sb!bignum:%bignum-set-length)
1347   (:policy :fast-safe))
1348
1349 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1350   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1351
1352 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1353   (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1354
1355 (define-vop (digit-0-or-plus)
1356   (:translate sb!bignum:%digit-0-or-plusp)
1357   (:policy :fast-safe)
1358   (:args (digit :scs (unsigned-reg)))
1359   (:arg-types unsigned-num)
1360   (:conditional)
1361   (:info target not-p)
1362   (:generator 3
1363     (inst or digit digit)
1364     (inst jmp (if not-p :s :ns) target)))
1365
1366
1367 ;;; For add and sub with carry the sc of carry argument is any-reg so
1368 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1369 ;;; 4. This is easy to deal with and may save a fixnum-word
1370 ;;; conversion.
1371 (define-vop (add-w/carry)
1372   (:translate sb!bignum:%add-with-carry)
1373   (:policy :fast-safe)
1374   (:args (a :scs (unsigned-reg) :target result)
1375          (b :scs (unsigned-reg unsigned-stack) :to :eval)
1376          (c :scs (any-reg) :target temp))
1377   (:arg-types unsigned-num unsigned-num positive-fixnum)
1378   (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1379   (:results (result :scs (unsigned-reg) :from (:argument 0))
1380             (carry :scs (unsigned-reg)))
1381   (:result-types unsigned-num positive-fixnum)
1382   (:generator 4
1383     (move result a)
1384     (move temp c)
1385     (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1386     (inst adc result b)
1387     (inst mov carry 0)
1388     (inst adc carry carry)))
1389
1390 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1391 ;;; borrow and 0 for a borrow.
1392 (define-vop (sub-w/borrow)
1393   (:translate sb!bignum:%subtract-with-borrow)
1394   (:policy :fast-safe)
1395   (:args (a :scs (unsigned-reg) :to :eval :target result)
1396          (b :scs (unsigned-reg unsigned-stack) :to :result)
1397          (c :scs (any-reg control-stack)))
1398   (:arg-types unsigned-num unsigned-num positive-fixnum)
1399   (:results (result :scs (unsigned-reg) :from :eval)
1400             (borrow :scs (unsigned-reg)))
1401   (:result-types unsigned-num positive-fixnum)
1402   (:generator 5
1403     (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1404     (move result a)
1405     (inst sbb result b)
1406     (inst mov borrow 0)
1407     (inst adc borrow borrow)
1408     (inst xor borrow 1)))
1409
1410
1411 (define-vop (bignum-mult-and-add-3-arg)
1412   (:translate sb!bignum:%multiply-and-add)
1413   (:policy :fast-safe)
1414   (:args (x :scs (unsigned-reg) :target eax)
1415          (y :scs (unsigned-reg unsigned-stack))
1416          (carry-in :scs (unsigned-reg unsigned-stack)))
1417   (:arg-types unsigned-num unsigned-num unsigned-num)
1418   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1419                    :to (:result 1) :target lo) eax)
1420   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1421                    :to (:result 0) :target hi) edx)
1422   (:results (hi :scs (unsigned-reg))
1423             (lo :scs (unsigned-reg)))
1424   (:result-types unsigned-num unsigned-num)
1425   (:generator 20
1426     (move eax x)
1427     (inst mul eax y)
1428     (inst add eax carry-in)
1429     (inst adc edx 0)
1430     (move hi edx)
1431     (move lo eax)))
1432
1433 (define-vop (bignum-mult-and-add-4-arg)
1434   (:translate sb!bignum:%multiply-and-add)
1435   (:policy :fast-safe)
1436   (:args (x :scs (unsigned-reg) :target eax)
1437          (y :scs (unsigned-reg unsigned-stack))
1438          (prev :scs (unsigned-reg unsigned-stack))
1439          (carry-in :scs (unsigned-reg unsigned-stack)))
1440   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1441   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1442                    :to (:result 1) :target lo) eax)
1443   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1444                    :to (:result 0) :target hi) edx)
1445   (:results (hi :scs (unsigned-reg))
1446             (lo :scs (unsigned-reg)))
1447   (:result-types unsigned-num unsigned-num)
1448   (:generator 20
1449     (move eax x)
1450     (inst mul eax y)
1451     (inst add eax prev)
1452     (inst adc edx 0)
1453     (inst add eax carry-in)
1454     (inst adc edx 0)
1455     (move hi edx)
1456     (move lo eax)))
1457
1458
1459 (define-vop (bignum-mult)
1460   (:translate sb!bignum:%multiply)
1461   (:policy :fast-safe)
1462   (:args (x :scs (unsigned-reg) :target eax)
1463          (y :scs (unsigned-reg unsigned-stack)))
1464   (:arg-types unsigned-num unsigned-num)
1465   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1466                    :to (:result 1) :target lo) eax)
1467   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1468                    :to (:result 0) :target hi) edx)
1469   (:results (hi :scs (unsigned-reg))
1470             (lo :scs (unsigned-reg)))
1471   (:result-types unsigned-num unsigned-num)
1472   (:generator 20
1473     (move eax x)
1474     (inst mul eax y)
1475     (move hi edx)
1476     (move lo eax)))
1477
1478 (define-vop (bignum-lognot lognot-mod64/unsigned=>unsigned)
1479   (:translate sb!bignum:%lognot))
1480
1481 (define-vop (fixnum-to-digit)
1482   (:translate sb!bignum:%fixnum-to-digit)
1483   (:policy :fast-safe)
1484   (:args (fixnum :scs (any-reg control-stack) :target digit))
1485   (:arg-types tagged-num)
1486   (:results (digit :scs (unsigned-reg)
1487                    :load-if (not (and (sc-is fixnum control-stack)
1488                                       (sc-is digit unsigned-stack)
1489                                       (location= fixnum digit)))))
1490   (:result-types unsigned-num)
1491   (:generator 1
1492     (move digit fixnum)
1493     (inst sar digit 3)))
1494
1495 (define-vop (bignum-floor)
1496   (:translate sb!bignum:%floor)
1497   (:policy :fast-safe)
1498   (:args (div-high :scs (unsigned-reg) :target edx)
1499          (div-low :scs (unsigned-reg) :target eax)
1500          (divisor :scs (unsigned-reg unsigned-stack)))
1501   (:arg-types unsigned-num unsigned-num unsigned-num)
1502   (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1503                    :to (:result 0) :target quo) eax)
1504   (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1505                    :to (:result 1) :target rem) edx)
1506   (:results (quo :scs (unsigned-reg))
1507             (rem :scs (unsigned-reg)))
1508   (:result-types unsigned-num unsigned-num)
1509   (:generator 300
1510     (move edx div-high)
1511     (move eax div-low)
1512     (inst div eax divisor)
1513     (move quo eax)
1514     (move rem edx)))
1515
1516 (define-vop (signify-digit)
1517   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1518   (:policy :fast-safe)
1519   (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1520   (:arg-types unsigned-num)
1521   (:results (res :scs (any-reg signed-reg)
1522                  :load-if (not (and (sc-is digit unsigned-stack)
1523                                     (sc-is res control-stack signed-stack)
1524                                     (location= digit res)))))
1525   (:result-types signed-num)
1526   (:generator 1
1527     (move res digit)
1528     (when (sc-is res any-reg control-stack)
1529       (inst shl res 3))))
1530
1531 (define-vop (digit-ashr)
1532   (:translate sb!bignum:%ashr)
1533   (:policy :fast-safe)
1534   (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1535          (count :scs (unsigned-reg) :target ecx))
1536   (:arg-types unsigned-num positive-fixnum)
1537   (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1538   (:results (result :scs (unsigned-reg) :from (:argument 0)
1539                     :load-if (not (and (sc-is result unsigned-stack)
1540                                        (location= digit result)))))
1541   (:result-types unsigned-num)
1542   (:generator 1
1543     (move result digit)
1544     (move ecx count)
1545     (inst sar result :cl)))
1546
1547 (define-vop (digit-lshr digit-ashr)
1548   (:translate sb!bignum:%digit-logical-shift-right)
1549   (:generator 1
1550     (move result digit)
1551     (move ecx count)
1552     (inst shr result :cl)))
1553
1554 (define-vop (digit-ashl digit-ashr)
1555   (:translate sb!bignum:%ashl)
1556   (:generator 1
1557     (move result digit)
1558     (move ecx count)
1559     (inst shl result :cl)))
1560 \f
1561 ;;;; static functions
1562
1563 (define-static-fun two-arg-/ (x y) :translate /)
1564
1565 (define-static-fun two-arg-gcd (x y) :translate gcd)
1566 (define-static-fun two-arg-lcm (x y) :translate lcm)
1567
1568 (define-static-fun two-arg-and (x y) :translate logand)
1569 (define-static-fun two-arg-ior (x y) :translate logior)
1570 (define-static-fun two-arg-xor (x y) :translate logxor)
1571
1572
1573 (in-package "SB!C")
1574
1575 ;;; This is essentially a straight implementation of the algorithm in
1576 ;;; "Strength Reduction of Multiplications by Integer Constants",
1577 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1578 (defun basic-decompose-multiplication (arg num n-bits condensed)
1579   (case (aref condensed 0)
1580     (0
1581      (let ((tmp (min 3 (aref condensed 1))))
1582        (decf (aref condensed 1) tmp)
1583        `(logand #xffffffff
1584          (%lea ,arg
1585                ,(decompose-multiplication
1586                  arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1587                ,(ash 1 tmp) 0))))
1588     ((1 2 3)
1589      (let ((r0 (aref condensed 0)))
1590        (incf (aref condensed 1) r0)
1591        `(logand #xffffffff
1592          (%lea ,(decompose-multiplication
1593                  arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1594                ,arg
1595                ,(ash 1 r0) 0))))
1596     (t (let ((r0 (aref condensed 0)))
1597          (setf (aref condensed 0) 0)
1598          `(logand #xffffffff
1599            (ash ,(decompose-multiplication
1600                   arg (ash num (- r0)) n-bits condensed)
1601                 ,r0))))))
1602
1603 (defun decompose-multiplication (arg num n-bits condensed)
1604   (cond
1605     ((= n-bits 0) 0)
1606     ((= num 1) arg)
1607     ((= n-bits 1)
1608      `(logand #xffffffff (ash ,arg ,(1- (integer-length num)))))
1609     ((let ((max 0) (end 0))
1610        (loop for i from 2 to (length condensed)
1611              for j = (reduce #'+ (subseq condensed 0 i))
1612              when (and (> (- (* 2 i) 3 j) max)
1613                        (< (+ (ash 1 (1+ j))
1614                              (ash (ldb (byte (- 64 (1+ j)) (1+ j)) num)
1615                                   (1+ j)))
1616                           (ash 1 64)))
1617                do (setq max (- (* 2 i) 3 j)
1618                         end i))
1619        (when (> max 0)
1620          (let ((j (reduce #'+ (subseq condensed 0 end))))
1621            (let ((n2 (+ (ash 1 (1+ j))
1622                         (ash (ldb (byte (- 64 (1+ j)) (1+ j)) num) (1+ j))))
1623                  (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1624            `(logand #xffffffff
1625              (- ,(optimize-multiply arg n2) ,(optimize-multiply arg n1))))))))
1626     ((dolist (i '(9 5 3))
1627        (when (integerp (/ num i))
1628          (when (< (logcount (/ num i)) (logcount num))
1629            (let ((x (gensym)))
1630              (return `(let ((,x ,(optimize-multiply arg (/ num i))))
1631                        (logand #xffffffff
1632                         (%lea ,x ,x (1- ,i) 0)))))))))
1633     (t (basic-decompose-multiplication arg num n-bits condensed))))
1634            
1635 (defun optimize-multiply (arg x)
1636   (let* ((n-bits (logcount x))
1637          (condensed (make-array n-bits)))
1638     (let ((count 0) (bit 0))
1639       (dotimes (i 64)
1640         (cond ((logbitp i x)
1641                (setf (aref condensed bit) count)
1642                (setf count 1)
1643                (incf bit))
1644               (t (incf count)))))
1645     (decompose-multiplication arg x n-bits condensed)))
1646
1647 (defun *-transformer (y)
1648   (cond
1649     (t (give-up-ir1-transform))
1650     ((= y (ash 1 (integer-length y)))
1651      ;; there's a generic transform for y = 2^k
1652      (give-up-ir1-transform))
1653     ((member y '(3 5 9))
1654      ;; we can do these multiplications directly using LEA
1655      `(%lea x x ,(1- y) 0))
1656     ((member :pentium4 *backend-subfeatures*)
1657      ;; the pentium4's multiply unit is reportedly very good
1658      (give-up-ir1-transform))
1659     ;; FIXME: should make this more fine-grained.  If nothing else,
1660     ;; there should probably be a cutoff of about 9 instructions on
1661     ;; pentium-class machines.
1662     (t (optimize-multiply 'x y))))
1663
1664 (deftransform * ((x y)
1665                  ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1666                  (unsigned-byte 64))
1667   "recode as leas, shifts and adds"
1668   (let ((y (lvar-value y)))
1669     (*-transformer y)))
1670
1671 (deftransform sb!vm::*-mod64
1672     ((x y) ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1673      (unsigned-byte 64))
1674   "recode as leas, shifts and adds"
1675   (let ((y (lvar-value y)))
1676     (*-transformer y)))
1677
1678 ;;; FIXME: we should also be able to write an optimizer or two to
1679 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.