1 ;;;; the VM definition of arithmetic VOPs for the x86
3 ;;;; This software is part of the SBCL system. See the README file for
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.
16 (define-vop (fast-safe-arith-op)
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))
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))
35 (define-vop (fast-negate/fixnum fixnum-unop)
41 (define-vop (fast-negate/signed signed-unop)
47 (define-vop (fast-lognot/fixnum fixnum-unop)
51 (inst xor res (fixnumize -1))))
53 (define-vop (fast-lognot/signed signed-unop)
59 ;;;; binary fixnum operations
61 ;;; Assume that any constant operand is the second arg...
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)
67 (sc-is r control-stack)
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)
74 (sc-is r control-stack)
76 (:result-types tagged-num)
77 (:note "inline fixnum arithmetic"))
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)
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)
92 (:result-types unsigned-num)
93 (:note "inline (unsigned-byte 64) arithmetic"))
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)
99 (sc-is r signed-stack)
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)
106 (sc-is r signed-stack)
108 (:result-types signed-num)
109 (:note "inline (signed-byte 64) arithmetic"))
111 (define-vop (fast-fixnum-binop-c fast-safe-arith-op)
112 (:args (x :target r :scs (any-reg control-stack)))
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"))
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)))
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 64) arithmetic"))
131 (define-vop (fast-signed-binop-c fast-safe-arith-op)
132 (:args (x :target r :scs (signed-reg signed-stack)))
134 (:arg-types signed-num (:constant (signed-byte 32)))
135 (:results (r :scs (signed-reg)
136 :load-if (not (location= x r))))
137 (:result-types signed-num)
138 (:note "inline (signed-byte 64) arithmetic"))
140 (macrolet ((define-binop (translate untagged-penalty op)
142 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
144 (:translate ,translate)
148 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
150 (:translate ,translate)
153 (inst ,op r (fixnumize y))))
154 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
156 (:translate ,translate)
157 (:generator ,(1+ untagged-penalty)
160 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
162 (:translate ,translate)
163 (:generator ,untagged-penalty
166 (define-vop (,(symbolicate "FAST-"
168 "/UNSIGNED=>UNSIGNED")
170 (:translate ,translate)
171 (:generator ,(1+ untagged-penalty)
174 (define-vop (,(symbolicate 'fast-
176 '-c/unsigned=>unsigned)
177 fast-unsigned-binop-c)
178 (:translate ,translate)
179 (:generator ,untagged-penalty
183 ;;(define-binop + 4 add)
184 (define-binop - 4 sub)
185 (define-binop logand 2 and)
186 (define-binop logior 2 or)
187 (define-binop logxor 2 xor))
189 ;;; Special handling of add on the x86; can use lea to avoid a
190 ;;; register load, otherwise it uses add.
191 (define-vop (fast-+/fixnum=>fixnum fast-safe-arith-op)
193 (:args (x :scs (any-reg) :target r
194 :load-if (not (and (sc-is x control-stack)
196 (sc-is r control-stack)
198 (y :scs (any-reg control-stack)))
199 (:arg-types tagged-num tagged-num)
200 (:results (r :scs (any-reg) :from (:argument 0)
201 :load-if (not (and (sc-is x control-stack)
203 (sc-is r control-stack)
205 (:result-types tagged-num)
206 (:note "inline fixnum arithmetic")
208 (cond ((and (sc-is x any-reg) (sc-is y any-reg) (sc-is r any-reg)
209 (not (location= x r)))
210 (inst lea r (make-ea :qword :base x :index y :scale 1)))
215 (define-vop (fast-+-c/fixnum=>fixnum fast-safe-arith-op)
217 (:args (x :target r :scs (any-reg control-stack)))
219 (:arg-types tagged-num (:constant (signed-byte 29)))
220 (:results (r :scs (any-reg)
221 :load-if (not (location= x r))))
222 (:result-types tagged-num)
223 (:note "inline fixnum arithmetic")
225 (cond ((and (sc-is x any-reg) (sc-is r any-reg) (not (location= x r)))
226 (inst lea r (make-ea :qword :base x :disp (fixnumize y))))
229 (inst add r (fixnumize y))))))
231 (define-vop (fast-+/signed=>signed fast-safe-arith-op)
233 (:args (x :scs (signed-reg) :target r
234 :load-if (not (and (sc-is x signed-stack)
236 (sc-is r signed-stack)
238 (y :scs (signed-reg signed-stack)))
239 (:arg-types signed-num signed-num)
240 (:results (r :scs (signed-reg) :from (:argument 0)
241 :load-if (not (and (sc-is x signed-stack)
244 (:result-types signed-num)
245 (:note "inline (signed-byte 64) arithmetic")
247 (cond ((and (sc-is x signed-reg) (sc-is y signed-reg) (sc-is r signed-reg)
248 (not (location= x r)))
249 (inst lea r (make-ea :qword :base x :index y :scale 1)))
255 ;;;; Special logand cases: (logand signed unsigned) => unsigned
257 (define-vop (fast-logand/signed-unsigned=>unsigned
258 fast-logand/unsigned=>unsigned)
259 (:args (x :target r :scs (signed-reg)
260 :load-if (not (and (sc-is x signed-stack)
261 (sc-is y unsigned-reg)
262 (sc-is r unsigned-stack)
264 (y :scs (unsigned-reg unsigned-stack)))
265 (:arg-types signed-num unsigned-num))
267 (define-vop (fast-logand-c/signed-unsigned=>unsigned
268 fast-logand-c/unsigned=>unsigned)
269 (:args (x :target r :scs (signed-reg signed-stack)))
270 (:arg-types signed-num (:constant (unsigned-byte 31))))
272 (define-vop (fast-logand/unsigned-signed=>unsigned
273 fast-logand/unsigned=>unsigned)
274 (:args (x :target r :scs (unsigned-reg)
275 :load-if (not (and (sc-is x unsigned-stack)
277 (sc-is r unsigned-stack)
279 (y :scs (signed-reg signed-stack)))
280 (:arg-types unsigned-num signed-num))
283 (define-vop (fast-+-c/signed=>signed fast-safe-arith-op)
285 (:args (x :target r :scs (signed-reg signed-stack)))
287 (:arg-types signed-num (:constant (signed-byte 32)))
288 (:results (r :scs (signed-reg)
289 :load-if (not (location= x r))))
290 (:result-types signed-num)
291 (:note "inline (signed-byte 64) arithmetic")
293 (cond ((and (sc-is x signed-reg) (sc-is r signed-reg)
294 (not (location= x r)))
295 (inst lea r (make-ea :qword :base x :disp y)))
302 (define-vop (fast-+/unsigned=>unsigned fast-safe-arith-op)
304 (:args (x :scs (unsigned-reg) :target r
305 :load-if (not (and (sc-is x unsigned-stack)
306 (sc-is y unsigned-reg)
307 (sc-is r unsigned-stack)
309 (y :scs (unsigned-reg unsigned-stack)))
310 (:arg-types unsigned-num unsigned-num)
311 (:results (r :scs (unsigned-reg) :from (:argument 0)
312 :load-if (not (and (sc-is x unsigned-stack)
313 (sc-is y unsigned-reg)
314 (sc-is r unsigned-stack)
316 (:result-types unsigned-num)
317 (:note "inline (unsigned-byte 64) arithmetic")
319 (cond ((and (sc-is x unsigned-reg) (sc-is y unsigned-reg)
320 (sc-is r unsigned-reg) (not (location= x r)))
321 (inst lea r (make-ea :qword :base x :index y :scale 1)))
326 (define-vop (fast-+-c/unsigned=>unsigned fast-safe-arith-op)
328 (:args (x :target r :scs (unsigned-reg unsigned-stack)))
330 (:arg-types unsigned-num (:constant (unsigned-byte 31)))
331 (:results (r :scs (unsigned-reg)
332 :load-if (not (location= x r))))
333 (:result-types unsigned-num)
334 (:note "inline (unsigned-byte 64) arithmetic")
336 (cond ((and (sc-is x unsigned-reg) (sc-is r unsigned-reg)
337 (not (location= x r)))
338 (inst lea r (make-ea :qword :base x :disp y)))
345 ;;;; multiplication and division
347 (define-vop (fast-*/fixnum=>fixnum fast-safe-arith-op)
349 ;; We need different loading characteristics.
350 (:args (x :scs (any-reg) :target r)
351 (y :scs (any-reg control-stack)))
352 (:arg-types tagged-num tagged-num)
353 (:results (r :scs (any-reg) :from (:argument 0)))
354 (:result-types tagged-num)
355 (:note "inline fixnum arithmetic")
361 (define-vop (fast-*-c/fixnum=>fixnum fast-safe-arith-op)
363 ;; We need different loading characteristics.
364 (:args (x :scs (any-reg control-stack)))
366 (:arg-types tagged-num (:constant (signed-byte 29)))
367 (:results (r :scs (any-reg)))
368 (:result-types tagged-num)
369 (:note "inline fixnum arithmetic")
373 (define-vop (fast-*/signed=>signed fast-safe-arith-op)
375 ;; We need different loading characteristics.
376 (:args (x :scs (signed-reg) :target r)
377 (y :scs (signed-reg signed-stack)))
378 (:arg-types signed-num signed-num)
379 (:results (r :scs (signed-reg) :from (:argument 0)))
380 (:result-types signed-num)
381 (:note "inline (signed-byte 64) arithmetic")
386 (define-vop (fast-*-c/signed=>signed fast-safe-arith-op)
388 ;; We need different loading characteristics.
389 (:args (x :scs (signed-reg signed-stack)))
391 (:arg-types signed-num (:constant (signed-byte 32)))
392 (:results (r :scs (signed-reg)))
393 (:result-types signed-num)
394 (:note "inline (signed-byte 64) arithmetic")
398 (define-vop (fast-*/unsigned=>unsigned fast-safe-arith-op)
400 (:args (x :scs (unsigned-reg) :target eax)
401 (y :scs (unsigned-reg unsigned-stack)))
402 (:arg-types unsigned-num unsigned-num)
403 (:temporary (:sc unsigned-reg :offset eax-offset :target result
404 :from (:argument 0) :to :result) eax)
405 (:temporary (:sc unsigned-reg :offset edx-offset
406 :from :eval :to :result) edx)
408 (:results (result :scs (unsigned-reg)))
409 (:result-types unsigned-num)
410 (:note "inline (unsigned-byte 64) arithmetic")
412 (:save-p :compute-only)
419 (define-vop (fast-truncate/fixnum=>fixnum fast-safe-arith-op)
420 (:translate truncate)
421 (:args (x :scs (any-reg) :target eax)
422 (y :scs (any-reg control-stack)))
423 (:arg-types tagged-num tagged-num)
424 (:temporary (:sc signed-reg :offset eax-offset :target quo
425 :from (:argument 0) :to (:result 0)) eax)
426 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
427 :from (:argument 0) :to (:result 1)) edx)
428 (:results (quo :scs (any-reg))
429 (rem :scs (any-reg)))
430 (:result-types tagged-num tagged-num)
431 (:note "inline fixnum arithmetic")
433 (:save-p :compute-only)
435 (let ((zero (generate-error-code vop division-by-zero-error x y)))
436 (if (sc-is y any-reg)
437 (inst test y y) ; smaller instruction
443 (if (location= quo eax)
445 (inst lea quo (make-ea :qword :index eax :scale 8)))
448 (define-vop (fast-truncate-c/fixnum=>fixnum fast-safe-arith-op)
449 (:translate truncate)
450 (:args (x :scs (any-reg) :target eax))
452 (:arg-types tagged-num (:constant (signed-byte 29)))
453 (:temporary (:sc signed-reg :offset eax-offset :target quo
454 :from :argument :to (:result 0)) eax)
455 (:temporary (:sc any-reg :offset edx-offset :target rem
456 :from :eval :to (:result 1)) edx)
457 (:temporary (:sc any-reg :from :eval :to :result) y-arg)
458 (:results (quo :scs (any-reg))
459 (rem :scs (any-reg)))
460 (:result-types tagged-num tagged-num)
461 (:note "inline fixnum arithmetic")
463 (:save-p :compute-only)
467 (inst mov y-arg (fixnumize y))
468 (inst idiv eax y-arg)
469 (if (location= quo eax)
471 (inst lea quo (make-ea :qword :index eax :scale 8)))
474 (define-vop (fast-truncate/unsigned=>unsigned fast-safe-arith-op)
475 (:translate truncate)
476 (:args (x :scs (unsigned-reg) :target eax)
477 (y :scs (unsigned-reg signed-stack)))
478 (:arg-types unsigned-num unsigned-num)
479 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
480 :from (:argument 0) :to (:result 0)) eax)
481 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
482 :from (:argument 0) :to (:result 1)) edx)
483 (:results (quo :scs (unsigned-reg))
484 (rem :scs (unsigned-reg)))
485 (:result-types unsigned-num unsigned-num)
486 (:note "inline (unsigned-byte 64) arithmetic")
488 (:save-p :compute-only)
490 (let ((zero (generate-error-code vop division-by-zero-error x y)))
491 (if (sc-is y unsigned-reg)
492 (inst test y y) ; smaller instruction
501 (define-vop (fast-truncate-c/unsigned=>unsigned fast-safe-arith-op)
502 (:translate truncate)
503 (:args (x :scs (unsigned-reg) :target eax))
505 (:arg-types unsigned-num (:constant (unsigned-byte 31)))
506 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
507 :from :argument :to (:result 0)) eax)
508 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
509 :from :eval :to (:result 1)) edx)
510 (:temporary (:sc unsigned-reg :from :eval :to :result) y-arg)
511 (:results (quo :scs (unsigned-reg))
512 (rem :scs (unsigned-reg)))
513 (:result-types unsigned-num unsigned-num)
514 (:note "inline (unsigned-byte 64) arithmetic")
516 (:save-p :compute-only)
525 (define-vop (fast-truncate/signed=>signed fast-safe-arith-op)
526 (:translate truncate)
527 (:args (x :scs (signed-reg) :target eax)
528 (y :scs (signed-reg signed-stack)))
529 (:arg-types signed-num signed-num)
530 (:temporary (:sc signed-reg :offset eax-offset :target quo
531 :from (:argument 0) :to (:result 0)) eax)
532 (:temporary (:sc signed-reg :offset edx-offset :target rem
533 :from (:argument 0) :to (:result 1)) edx)
534 (:results (quo :scs (signed-reg))
535 (rem :scs (signed-reg)))
536 (:result-types signed-num signed-num)
537 (:note "inline (signed-byte 64) arithmetic")
539 (:save-p :compute-only)
541 (let ((zero (generate-error-code vop division-by-zero-error x y)))
542 (if (sc-is y signed-reg)
543 (inst test y y) ; smaller instruction
552 (define-vop (fast-truncate-c/signed=>signed fast-safe-arith-op)
553 (:translate truncate)
554 (:args (x :scs (signed-reg) :target eax))
556 (:arg-types signed-num (:constant (signed-byte 32)))
557 (:temporary (:sc signed-reg :offset eax-offset :target quo
558 :from :argument :to (:result 0)) eax)
559 (:temporary (:sc signed-reg :offset edx-offset :target rem
560 :from :eval :to (:result 1)) edx)
561 (:temporary (:sc signed-reg :from :eval :to :result) y-arg)
562 (:results (quo :scs (signed-reg))
563 (rem :scs (signed-reg)))
564 (:result-types signed-num signed-num)
565 (:note "inline (signed-byte 64) arithmetic")
567 (:save-p :compute-only)
572 (inst idiv eax y-arg)
579 (define-vop (fast-ash-c/fixnum=>fixnum)
582 (:args (number :scs (any-reg) :target result
583 :load-if (not (and (sc-is number any-reg control-stack)
584 (sc-is result any-reg control-stack)
585 (location= number result)))))
587 (:arg-types tagged-num (:constant integer))
588 (:results (result :scs (any-reg)
589 :load-if (not (and (sc-is number control-stack)
590 (sc-is result control-stack)
591 (location= number result)))))
592 (:result-types tagged-num)
595 (cond ((and (= amount 1) (not (location= number result)))
596 (inst lea result (make-ea :qword :index number :scale 2)))
597 ((and (= amount 2) (not (location= number result)))
598 (inst lea result (make-ea :qword :index number :scale 4)))
599 ((and (= amount 3) (not (location= number result)))
600 (inst lea result (make-ea :qword :index number :scale 8)))
603 (cond ((plusp amount)
604 ;; We don't have to worry about overflow because of the
605 ;; result type restriction.
606 (inst shl result amount))
608 ;; Since the shift instructions take the shift amount
609 ;; modulo 64 we must special case amounts of 64 and more.
610 ;; Because fixnums have only 61 bits, the result is 0 or
611 ;; -1 for all amounts of 60 or more, so use this as the
613 (inst sar result (min (- n-word-bits n-fixnum-tag-bits 1)
615 (inst and result (lognot fixnum-tag-mask))))))))
617 (define-vop (fast-ash-left/fixnum=>fixnum)
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)
636 ;; The result-type ensures us that this shift will not overflow.
637 (inst shl result :cl)))
639 (define-vop (fast-ash-c/signed=>signed)
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)))))
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)
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)))
663 (cond ((plusp amount) (inst shl result amount))
664 (t (inst sar result (min 63 (- amount)))))))))
666 (define-vop (fast-ash-c/unsigned=>unsigned)
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)))))
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)
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)))
690 (cond ((< -64 amount 64) ;; XXXX
691 ;; this code is used both in ASH and ASH-MOD32, so
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))))))))
700 (define-vop (fast-ash-left/signed=>signed)
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)
719 (inst shl result :cl)))
721 (define-vop (fast-ash-left/unsigned=>unsigned)
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)
740 (inst shl result :cl)))
742 (define-vop (fast-ash/signed=>signed)
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)
756 (inst jmp :ns POSITIVE)
762 (inst sar result :cl)
766 ;; The result-type ensures us that this shift will not overflow.
767 (inst shl result :cl)
771 (define-vop (fast-ash/unsigned=>unsigned)
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)
785 (inst jmp :ns POSITIVE)
789 (inst xor result result)
792 (inst shr result :cl)
796 ;; The result-type ensures us that this shift will not overflow.
797 (inst shl result :cl)
803 (defknown %lea (integer integer (member 1 2 4 8 16) (signed-byte 64))
805 (foldable flushable movable))
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
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))))))))
827 (defun %lea (base index scale disp)
828 (+ base (* index scale) disp))
832 (define-vop (%lea/unsigned=>unsigned)
835 (:args (base :scs (unsigned-reg))
836 (index :scs (unsigned-reg)))
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)
844 (inst lea r (make-ea :qword :base base :index index
845 :scale scale :disp disp))))
847 (define-vop (%lea/signed=>signed)
850 (:args (base :scs (signed-reg))
851 (index :scs (signed-reg)))
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)
859 (inst lea r (make-ea :qword :base base :index index
860 :scale scale :disp disp))))
862 (define-vop (%lea/fixnum=>fixnum)
865 (:args (base :scs (any-reg))
866 (index :scs (any-reg)))
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)
874 (inst lea r (make-ea :qword :base base :index index
875 :scale scale :disp disp))))
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)
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)
892 (:guard (member :cmov *backend-subfeatures*))
897 (inst jmp :ns POSITIVE)
900 (inst shr result :cl)
902 (inst cmov :nbe result zero)
906 ;; The result-type ensures us that this shift will not overflow.
907 (inst shl result :cl)
911 (define-vop (signed-byte-64-len)
912 (:translate integer-length)
913 (:note "inline (signed-byte 64) integer-length")
915 (:args (arg :scs (signed-reg) :target res))
916 (:arg-types signed-num)
917 (:results (res :scs (unsigned-reg)))
918 (:result-types unsigned-num)
933 (define-vop (unsigned-byte-64-len)
934 (:translate integer-length)
935 (:note "inline (unsigned-byte 64) integer-length")
937 (:args (arg :scs (unsigned-reg)))
938 (:arg-types unsigned-num)
939 (:results (res :scs (unsigned-reg)))
940 (:result-types unsigned-num)
951 (define-vop (unsigned-byte-64-count)
952 (:translate logcount)
953 (:note "inline (unsigned-byte 64) logcount")
955 (:args (arg :scs (unsigned-reg)))
956 (:arg-types unsigned-num)
957 (:results (result :scs (unsigned-reg)))
958 (:result-types positive-fixnum)
959 (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
960 (:temporary (:sc unsigned-reg :from (:argument 0)) t1)
965 (inst mov temp result)
967 (inst and result #x55555555) ; note these masks will restrict the
968 (inst and temp #x55555555) ; count to the lower half of arg
969 (inst add result temp)
971 (inst mov temp result)
973 (inst and result #x33333333)
974 (inst and temp #x33333333)
975 (inst add result temp)
977 (inst mov temp result)
979 (inst and result #x0f0f0f0f)
980 (inst and temp #x0f0f0f0f)
981 (inst add result temp)
983 (inst mov temp result)
985 (inst and result #x00ff00ff)
986 (inst and temp #x00ff00ff)
987 (inst add result temp)
989 (inst mov temp result)
991 (inst and result #x0000ffff)
992 (inst and temp #x0000ffff)
993 (inst add result temp)
995 ;;; now do the upper half
1000 (inst and t1 #x55555555)
1001 (inst and temp #x55555555)
1006 (inst and t1 #x33333333)
1007 (inst and temp #x33333333)
1012 (inst and t1 #x0f0f0f0f)
1013 (inst and temp #x0f0f0f0f)
1018 (inst and t1 #x00ff00ff)
1019 (inst and temp #x00ff00ff)
1024 (inst and t1 #x0000ffff)
1025 (inst and temp #x0000ffff)
1027 (inst add result t1)))
1031 ;;;; binary conditional VOPs
1033 (define-vop (fast-conditional)
1035 (:info target not-p)
1038 (:policy :fast-safe))
1040 ;;; constant variants are declared for 32 bits not 64 bits, because
1041 ;;; loading a 64 bit constant is silly
1043 (define-vop (fast-conditional/fixnum fast-conditional)
1044 (:args (x :scs (any-reg)
1045 :load-if (not (and (sc-is x control-stack)
1046 (sc-is y any-reg))))
1047 (y :scs (any-reg control-stack)))
1048 (:arg-types tagged-num tagged-num)
1049 (:note "inline fixnum comparison"))
1051 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
1052 (:args (x :scs (any-reg control-stack)))
1053 (:arg-types tagged-num (:constant (signed-byte 29)))
1054 (:info target not-p y))
1056 (define-vop (fast-conditional/signed fast-conditional)
1057 (:args (x :scs (signed-reg)
1058 :load-if (not (and (sc-is x signed-stack)
1059 (sc-is y signed-reg))))
1060 (y :scs (signed-reg signed-stack)))
1061 (:arg-types signed-num signed-num)
1062 (:note "inline (signed-byte 64) comparison"))
1064 (define-vop (fast-conditional-c/signed fast-conditional/signed)
1065 (:args (x :scs (signed-reg signed-stack)))
1066 (:arg-types signed-num (:constant (signed-byte 31)))
1067 (:info target not-p y))
1069 (define-vop (fast-conditional/unsigned fast-conditional)
1070 (:args (x :scs (unsigned-reg)
1071 :load-if (not (and (sc-is x unsigned-stack)
1072 (sc-is y unsigned-reg))))
1073 (y :scs (unsigned-reg unsigned-stack)))
1074 (:arg-types unsigned-num unsigned-num)
1075 (:note "inline (unsigned-byte 64) comparison"))
1077 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
1078 (:args (x :scs (unsigned-reg unsigned-stack)))
1079 (:arg-types unsigned-num (:constant (unsigned-byte 31)))
1080 (:info target not-p y))
1082 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
1085 (lambda (suffix cost signed)
1086 `(define-vop (;; FIXME: These could be done more
1087 ;; cleanly with SYMBOLICATE.
1088 ,(intern (format nil "~:@(FAST-IF-~A~A~)"
1091 (format nil "~:@(FAST-CONDITIONAL~A~)"
1096 ,(if (eq suffix '-c/fixnum)
1107 '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
1108 ; '(/fixnum /signed /unsigned)
1110 '(t t t t nil nil)))))
1112 (define-conditional-vop < :l :b :ge :ae)
1113 (define-conditional-vop > :g :a :le :be))
1115 (define-vop (fast-if-eql/signed fast-conditional/signed)
1119 (inst jmp (if not-p :ne :e) target)))
1121 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
1124 (cond ((and (sc-is x signed-reg) (zerop y))
1125 (inst test x x)) ; smaller instruction
1128 (inst jmp (if not-p :ne :e) target)))
1130 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1134 (inst jmp (if not-p :ne :e) target)))
1136 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1139 (cond ((and (sc-is x unsigned-reg) (zerop y))
1140 (inst test x x)) ; smaller instruction
1143 (inst jmp (if not-p :ne :e) target)))
1145 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1148 ;;; These versions specify a fixnum restriction on their first arg. We have
1149 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1150 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1151 ;;; fixnum specific operations from being used on word integers, spuriously
1152 ;;; consing the argument.
1154 (define-vop (fast-eql/fixnum fast-conditional)
1155 (:args (x :scs (any-reg)
1156 :load-if (not (and (sc-is x control-stack)
1157 (sc-is y any-reg))))
1158 (y :scs (any-reg control-stack)))
1159 (:arg-types tagged-num tagged-num)
1160 (:note "inline fixnum comparison")
1164 (inst jmp (if not-p :ne :e) target)))
1165 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1166 (:args (x :scs (any-reg descriptor-reg)
1167 :load-if (not (and (sc-is x control-stack)
1168 (sc-is y any-reg))))
1169 (y :scs (any-reg control-stack)))
1170 (:arg-types * tagged-num)
1174 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1175 (:args (x :scs (any-reg control-stack)))
1176 (:arg-types tagged-num (:constant (signed-byte 29)))
1177 (:info target not-p y)
1180 (cond ((and (sc-is x any-reg) (zerop y))
1181 (inst test x x)) ; smaller instruction
1183 (inst cmp x (fixnumize y))))
1184 (inst jmp (if not-p :ne :e) target)))
1186 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1187 (:args (x :scs (any-reg descriptor-reg control-stack)))
1188 (:arg-types * (:constant (signed-byte 29)))
1191 ;;;; 32-bit logical operations
1193 (define-vop (merge-bits)
1194 (:translate merge-bits)
1195 (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1196 (prev :scs (unsigned-reg) :target result)
1197 (next :scs (unsigned-reg)))
1198 (:arg-types tagged-num unsigned-num unsigned-num)
1199 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1200 (:results (result :scs (unsigned-reg) :from (:argument 1)))
1201 (:result-types unsigned-num)
1202 (:policy :fast-safe)
1206 (inst shrd result next :cl)))
1208 ;;; Only the lower 6 bits of the shift amount are significant.
1209 (define-vop (shift-towards-someplace)
1210 (:policy :fast-safe)
1211 (:args (num :scs (unsigned-reg) :target r)
1212 (amount :scs (signed-reg) :target ecx))
1213 (:arg-types unsigned-num tagged-num)
1214 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1215 (:results (r :scs (unsigned-reg) :from (:argument 0)))
1216 (:result-types unsigned-num))
1218 (define-vop (shift-towards-start shift-towards-someplace)
1219 (:translate shift-towards-start)
1220 (:note "SHIFT-TOWARDS-START")
1226 (define-vop (shift-towards-end shift-towards-someplace)
1227 (:translate shift-towards-end)
1228 (:note "SHIFT-TOWARDS-END")
1234 ;;;; Modular functions
1236 (macrolet ((def (name -c-p)
1237 (let ((fun64 (intern (format nil "~S-MOD64" name)))
1238 (vopu (intern (format nil "FAST-~S/UNSIGNED=>UNSIGNED" name)))
1239 (vopcu (intern (format nil "FAST-~S-C/UNSIGNED=>UNSIGNED" name)))
1240 (vopf (intern (format nil "FAST-~S/FIXNUM=>FIXNUM" name)))
1241 (vopcf (intern (format nil "FAST-~S-C/FIXNUM=>FIXNUM" name)))
1242 (vop64u (intern (format nil "FAST-~S-MOD64/UNSIGNED=>UNSIGNED" name)))
1243 (vop64f (intern (format nil "FAST-~S-MOD64/FIXNUM=>FIXNUM" name)))
1244 (vop64cu (intern (format nil "FAST-~S-MOD64-C/UNSIGNED=>UNSIGNED" name)))
1245 (vop64cf (intern (format nil "FAST-~S-MOD64-C/FIXNUM=>FIXNUM" name)))
1246 (sfun61 (intern (format nil "~S-SMOD61" name)))
1247 (svop61f (intern (format nil "FAST-~S-SMOD61/FIXNUM=>FIXNUM" name)))
1248 (svop61cf (intern (format nil "FAST-~S-SMOD61-C/FIXNUM=>FIXNUM" name))))
1250 (define-modular-fun ,fun64 (x y) ,name :unsigned 64)
1251 (define-modular-fun ,sfun61 (x y) ,name :signed 61)
1252 (define-vop (,vop64u ,vopu) (:translate ,fun64))
1253 (define-vop (,vop64f ,vopf) (:translate ,fun64))
1254 (define-vop (,svop61f ,vopf) (:translate ,sfun61))
1256 `((define-vop (,vop64cu ,vopcu) (:translate ,fun64))
1257 (define-vop (,svop61cf ,vopcf) (:translate ,sfun61))))))))
1260 ;; (no -C variant as x86 MUL instruction doesn't take an immediate)
1263 (define-vop (fast-ash-left-mod64-c/unsigned=>unsigned
1264 fast-ash-c/unsigned=>unsigned)
1265 (:translate ash-left-mod64))
1266 (define-vop (fast-ash-left-mod64/unsigned=>unsigned
1267 fast-ash-left/unsigned=>unsigned))
1268 (deftransform ash-left-mod64 ((integer count)
1269 ((unsigned-byte 64) (unsigned-byte 6)))
1270 (when (sb!c::constant-lvar-p count)
1271 (sb!c::give-up-ir1-transform))
1272 '(%primitive fast-ash-left-mod64/unsigned=>unsigned integer count))
1274 (define-vop (fast-ash-left-smod61-c/fixnum=>fixnum
1275 fast-ash-c/fixnum=>fixnum)
1276 (:translate ash-left-smod61))
1277 (define-vop (fast-ash-left-smod61/fixnum=>fixnum
1278 fast-ash-left/fixnum=>fixnum))
1279 (deftransform ash-left-smod61 ((integer count)
1280 ((signed-byte 61) (unsigned-byte 6)))
1281 (when (sb!c::constant-lvar-p count)
1282 (sb!c::give-up-ir1-transform))
1283 '(%primitive fast-ash-left-smod61/fixnum=>fixnum integer count))
1287 (defknown sb!vm::%lea-mod64 (integer integer (member 1 2 4 8) (signed-byte 64))
1289 (foldable flushable movable))
1290 (defknown sb!vm::%lea-smod61 (integer integer (member 1 2 4 8) (signed-byte 64))
1292 (foldable flushable movable))
1294 (define-modular-fun-optimizer %lea ((base index scale disp) :unsigned :width width)
1295 (when (and (<= width 64)
1296 (constant-lvar-p scale)
1297 (constant-lvar-p disp))
1298 (cut-to-width base :unsigned width)
1299 (cut-to-width index :unsigned width)
1300 'sb!vm::%lea-mod64))
1301 (define-modular-fun-optimizer %lea ((base index scale disp) :signed :width width)
1302 (when (and (<= width 61)
1303 (constant-lvar-p scale)
1304 (constant-lvar-p disp))
1305 (cut-to-width base :signed width)
1306 (cut-to-width index :signed width)
1307 'sb!vm::%lea-smod61))
1311 (defun sb!vm::%lea-mod64 (base index scale disp)
1312 (ldb (byte 64 0) (%lea base index scale disp)))
1313 (defun sb!vm::%lea-smod61 (base index scale disp)
1314 (mask-signed-field 61 (%lea base index scale disp))))
1317 (defun sb!vm::%lea-mod64 (base index scale disp)
1318 (let ((base (logand base #xffffffffffffffff))
1319 (index (logand index #xffffffffffffffff)))
1320 ;; can't use modular version of %LEA, as we only have VOPs for
1321 ;; constant SCALE and DISP.
1322 (ldb (byte 64 0) (+ base (* index scale) disp))))
1323 (defun sb!vm::%lea-smod61 (base index scale disp)
1324 (let ((base (mask-signed-field 61 base))
1325 (index (mask-signed-field 61 index)))
1326 ;; can't use modular version of %LEA, as we only have VOPs for
1327 ;; constant SCALE and DISP.
1328 (mask-signed-field 61 (+ base (* index scale) disp)))))
1330 (in-package "SB!VM")
1332 (define-vop (%lea-mod64/unsigned=>unsigned
1333 %lea/unsigned=>unsigned)
1334 (:translate %lea-mod64))
1335 (define-vop (%lea-smod61/fixnum=>fixnum
1336 %lea/fixnum=>fixnum)
1337 (:translate %lea-smod61))
1339 ;;; logical operations
1340 (define-modular-fun lognot-mod64 (x) lognot :unsigned 64)
1341 (define-vop (lognot-mod64/unsigned=>unsigned)
1342 (:translate lognot-mod64)
1343 (:args (x :scs (unsigned-reg unsigned-stack) :target r
1344 :load-if (not (and (sc-is x unsigned-stack)
1345 (sc-is r unsigned-stack)
1347 (:arg-types unsigned-num)
1348 (:results (r :scs (unsigned-reg)
1349 :load-if (not (and (sc-is x unsigned-stack)
1350 (sc-is r unsigned-stack)
1352 (:result-types unsigned-num)
1353 (:policy :fast-safe)
1358 (define-modular-fun logxor-mod64 (x y) logxor :unsigned 64)
1359 (define-vop (fast-logxor-mod64/unsigned=>unsigned
1360 fast-logxor/unsigned=>unsigned)
1361 (:translate logxor-mod64))
1362 (define-vop (fast-logxor-mod64-c/unsigned=>unsigned
1363 fast-logxor-c/unsigned=>unsigned)
1364 (:translate logxor-mod64))
1365 (define-vop (fast-logxor-mod64/fixnum=>fixnum
1366 fast-logxor/fixnum=>fixnum)
1367 (:translate logxor-mod64))
1368 (define-vop (fast-logxor-mod64-c/fixnum=>fixnum
1369 fast-logxor-c/fixnum=>fixnum)
1370 (:translate logxor-mod64))
1372 (define-source-transform logeqv (&rest args)
1373 (if (oddp (length args))
1375 `(lognot (logxor ,@args))))
1376 (define-source-transform logandc1 (x y)
1377 `(logand (lognot ,x) ,y))
1378 (define-source-transform logandc2 (x y)
1379 `(logand ,x (lognot ,y)))
1380 (define-source-transform logorc1 (x y)
1381 `(logior (lognot ,x) ,y))
1382 (define-source-transform logorc2 (x y)
1383 `(logior ,x (lognot ,y)))
1384 (define-source-transform lognor (x y)
1385 `(lognot (logior ,x ,y)))
1386 (define-source-transform lognand (x y)
1387 `(lognot (logand ,x ,y)))
1391 (define-vop (bignum-length get-header-data)
1392 (:translate sb!bignum:%bignum-length)
1393 (:policy :fast-safe))
1395 (define-vop (bignum-set-length set-header-data)
1396 (:translate sb!bignum:%bignum-set-length)
1397 (:policy :fast-safe))
1399 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1400 (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1402 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1403 (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1405 (define-vop (digit-0-or-plus)
1406 (:translate sb!bignum:%digit-0-or-plusp)
1407 (:policy :fast-safe)
1408 (:args (digit :scs (unsigned-reg)))
1409 (:arg-types unsigned-num)
1411 (:info target not-p)
1413 (inst or digit digit)
1414 (inst jmp (if not-p :s :ns) target)))
1417 ;;; For add and sub with carry the sc of carry argument is any-reg so
1418 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1419 ;;; 4. This is easy to deal with and may save a fixnum-word
1421 (define-vop (add-w/carry)
1422 (:translate sb!bignum:%add-with-carry)
1423 (:policy :fast-safe)
1424 (:args (a :scs (unsigned-reg) :target result)
1425 (b :scs (unsigned-reg unsigned-stack) :to :eval)
1426 (c :scs (any-reg) :target temp))
1427 (:arg-types unsigned-num unsigned-num positive-fixnum)
1428 (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1429 (:results (result :scs (unsigned-reg) :from (:argument 0))
1430 (carry :scs (unsigned-reg)))
1431 (:result-types unsigned-num positive-fixnum)
1435 (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1438 (inst adc carry carry)))
1440 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1441 ;;; borrow and 0 for a borrow.
1442 (define-vop (sub-w/borrow)
1443 (:translate sb!bignum:%subtract-with-borrow)
1444 (:policy :fast-safe)
1445 (:args (a :scs (unsigned-reg) :to :eval :target result)
1446 (b :scs (unsigned-reg unsigned-stack) :to :result)
1447 (c :scs (any-reg control-stack)))
1448 (:arg-types unsigned-num unsigned-num positive-fixnum)
1449 (:results (result :scs (unsigned-reg) :from :eval)
1450 (borrow :scs (unsigned-reg)))
1451 (:result-types unsigned-num positive-fixnum)
1453 (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1457 (inst adc borrow borrow)
1458 (inst xor borrow 1)))
1461 (define-vop (bignum-mult-and-add-3-arg)
1462 (:translate sb!bignum:%multiply-and-add)
1463 (:policy :fast-safe)
1464 (:args (x :scs (unsigned-reg) :target eax)
1465 (y :scs (unsigned-reg unsigned-stack))
1466 (carry-in :scs (unsigned-reg unsigned-stack)))
1467 (:arg-types unsigned-num unsigned-num unsigned-num)
1468 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1469 :to (:result 1) :target lo) eax)
1470 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1471 :to (:result 0) :target hi) edx)
1472 (:results (hi :scs (unsigned-reg))
1473 (lo :scs (unsigned-reg)))
1474 (:result-types unsigned-num unsigned-num)
1478 (inst add eax carry-in)
1483 (define-vop (bignum-mult-and-add-4-arg)
1484 (:translate sb!bignum:%multiply-and-add)
1485 (:policy :fast-safe)
1486 (:args (x :scs (unsigned-reg) :target eax)
1487 (y :scs (unsigned-reg unsigned-stack))
1488 (prev :scs (unsigned-reg unsigned-stack))
1489 (carry-in :scs (unsigned-reg unsigned-stack)))
1490 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1491 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1492 :to (:result 1) :target lo) eax)
1493 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1494 :to (:result 0) :target hi) edx)
1495 (:results (hi :scs (unsigned-reg))
1496 (lo :scs (unsigned-reg)))
1497 (:result-types unsigned-num unsigned-num)
1503 (inst add eax carry-in)
1509 (define-vop (bignum-mult)
1510 (:translate sb!bignum:%multiply)
1511 (:policy :fast-safe)
1512 (:args (x :scs (unsigned-reg) :target eax)
1513 (y :scs (unsigned-reg unsigned-stack)))
1514 (:arg-types unsigned-num unsigned-num)
1515 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1516 :to (:result 1) :target lo) eax)
1517 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1518 :to (:result 0) :target hi) edx)
1519 (:results (hi :scs (unsigned-reg))
1520 (lo :scs (unsigned-reg)))
1521 (:result-types unsigned-num unsigned-num)
1528 (define-vop (bignum-lognot lognot-mod64/unsigned=>unsigned)
1529 (:translate sb!bignum:%lognot))
1531 (define-vop (fixnum-to-digit)
1532 (:translate sb!bignum:%fixnum-to-digit)
1533 (:policy :fast-safe)
1534 (:args (fixnum :scs (any-reg control-stack) :target digit))
1535 (:arg-types tagged-num)
1536 (:results (digit :scs (unsigned-reg)
1537 :load-if (not (and (sc-is fixnum control-stack)
1538 (sc-is digit unsigned-stack)
1539 (location= fixnum digit)))))
1540 (:result-types unsigned-num)
1543 (inst sar digit 3)))
1545 (define-vop (bignum-floor)
1546 (:translate sb!bignum:%floor)
1547 (:policy :fast-safe)
1548 (:args (div-high :scs (unsigned-reg) :target edx)
1549 (div-low :scs (unsigned-reg) :target eax)
1550 (divisor :scs (unsigned-reg unsigned-stack)))
1551 (:arg-types unsigned-num unsigned-num unsigned-num)
1552 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1553 :to (:result 0) :target quo) eax)
1554 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1555 :to (:result 1) :target rem) edx)
1556 (:results (quo :scs (unsigned-reg))
1557 (rem :scs (unsigned-reg)))
1558 (:result-types unsigned-num unsigned-num)
1562 (inst div eax divisor)
1566 (define-vop (signify-digit)
1567 (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1568 (:policy :fast-safe)
1569 (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1570 (:arg-types unsigned-num)
1571 (:results (res :scs (any-reg signed-reg)
1572 :load-if (not (and (sc-is digit unsigned-stack)
1573 (sc-is res control-stack signed-stack)
1574 (location= digit res)))))
1575 (:result-types signed-num)
1578 (when (sc-is res any-reg control-stack)
1581 (define-vop (digit-ashr)
1582 (:translate sb!bignum:%ashr)
1583 (:policy :fast-safe)
1584 (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1585 (count :scs (unsigned-reg) :target ecx))
1586 (:arg-types unsigned-num positive-fixnum)
1587 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1588 (:results (result :scs (unsigned-reg) :from (:argument 0)
1589 :load-if (not (and (sc-is result unsigned-stack)
1590 (location= digit result)))))
1591 (:result-types unsigned-num)
1595 (inst sar result :cl)))
1597 (define-vop (digit-lshr digit-ashr)
1598 (:translate sb!bignum:%digit-logical-shift-right)
1602 (inst shr result :cl)))
1604 (define-vop (digit-ashl digit-ashr)
1605 (:translate sb!bignum:%ashl)
1609 (inst shl result :cl)))
1611 ;;;; static functions
1613 (define-static-fun two-arg-/ (x y) :translate /)
1615 (define-static-fun two-arg-gcd (x y) :translate gcd)
1616 (define-static-fun two-arg-lcm (x y) :translate lcm)
1618 (define-static-fun two-arg-and (x y) :translate logand)
1619 (define-static-fun two-arg-ior (x y) :translate logior)
1620 (define-static-fun two-arg-xor (x y) :translate logxor)
1625 (defun *-transformer (y)
1627 ((= y (ash 1 (integer-length y)))
1628 ;; there's a generic transform for y = 2^k
1629 (give-up-ir1-transform))
1630 ((member y '(3 5 9))
1631 ;; we can do these multiplications directly using LEA
1632 `(%lea x x ,(1- y) 0))
1634 ;; A normal 64-bit multiplication takes 4 cycles on Athlon 64/Opteron.
1635 ;; Optimizing multiplications (other than the above cases) to
1636 ;; shifts/adds/leas gives a maximum improvement of 1 cycle, but requires
1637 ;; quite a lot of hairy code.
1638 (give-up-ir1-transform))))
1640 (deftransform * ((x y)
1641 ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1643 "recode as leas, shifts and adds"
1644 (let ((y (lvar-value y)))
1646 (deftransform sb!vm::*-mod64
1647 ((x y) ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1649 "recode as leas, shifts and adds"
1650 (let ((y (lvar-value y)))
1653 (deftransform * ((x y)
1654 ((signed-byte 61) (constant-arg (unsigned-byte 64)))
1656 "recode as leas, shifts and adds"
1657 (let ((y (lvar-value y)))
1659 (deftransform sb!vm::*-smod61
1660 ((x y) ((signed-byte 61) (constant-arg (unsigned-byte 64)))
1662 "recode as leas, shifts and adds"
1663 (let ((y (lvar-value y)))