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