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 32) 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 32) 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 32) 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 (define-vop (fast-unsigned-binop-c fast-safe-arith-op)
121 (:args (x :target r :scs (unsigned-reg unsigned-stack)))
123 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
124 (:results (r :scs (unsigned-reg)
125 :load-if (not (location= x r))))
126 (:result-types unsigned-num)
127 (:note "inline (unsigned-byte 32) arithmetic"))
129 ;; 32 not 64 because it's hard work loading 64 bit constants
130 (define-vop (fast-signed-binop-c fast-safe-arith-op)
131 (:args (x :target r :scs (signed-reg signed-stack)))
133 (:arg-types signed-num (:constant (signed-byte 32)))
134 (:results (r :scs (signed-reg)
135 :load-if (not (location= x r))))
136 (:result-types signed-num)
137 (:note "inline (signed-byte 64) arithmetic"))
139 (macrolet ((define-binop (translate untagged-penalty op)
141 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
143 (:translate ,translate)
147 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
149 (:translate ,translate)
152 (inst ,op r (fixnumize y))))
153 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
155 (:translate ,translate)
156 (:generator ,(1+ untagged-penalty)
159 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
161 (:translate ,translate)
162 (:generator ,untagged-penalty
165 (define-vop (,(symbolicate "FAST-"
167 "/UNSIGNED=>UNSIGNED")
169 (:translate ,translate)
170 (:generator ,(1+ untagged-penalty)
173 (define-vop (,(symbolicate 'fast-
175 '-c/unsigned=>unsigned)
176 fast-unsigned-binop-c)
177 (:translate ,translate)
178 (:generator ,untagged-penalty
182 ;;(define-binop + 4 add)
183 (define-binop - 4 sub)
184 (define-binop logand 2 and)
185 (define-binop logior 2 or)
186 (define-binop logxor 2 xor))
188 ;;; Special handling of add on the x86; can use lea to avoid a
189 ;;; register load, otherwise it uses add.
190 (define-vop (fast-+/fixnum=>fixnum fast-safe-arith-op)
192 (:args (x :scs (any-reg) :target r
193 :load-if (not (and (sc-is x control-stack)
195 (sc-is r control-stack)
197 (y :scs (any-reg control-stack)))
198 (:arg-types tagged-num tagged-num)
199 (:results (r :scs (any-reg) :from (:argument 0)
200 :load-if (not (and (sc-is x control-stack)
202 (sc-is r control-stack)
204 (:result-types tagged-num)
205 (:note "inline fixnum arithmetic")
207 (cond ((and (sc-is x any-reg) (sc-is y any-reg) (sc-is r any-reg)
208 (not (location= x r)))
209 (inst lea r (make-ea :qword :base x :index y :scale 1)))
214 (define-vop (fast-+-c/fixnum=>fixnum fast-safe-arith-op)
216 (:args (x :target r :scs (any-reg control-stack)))
218 (:arg-types tagged-num (:constant (signed-byte 29)))
219 (:results (r :scs (any-reg)
220 :load-if (not (location= x r))))
221 (:result-types tagged-num)
222 (:note "inline fixnum arithmetic")
224 (cond ((and (sc-is x any-reg) (sc-is r any-reg) (not (location= x r)))
225 (inst lea r (make-ea :qword :base x :disp (fixnumize y))))
228 (inst add r (fixnumize y))))))
230 (define-vop (fast-+/signed=>signed fast-safe-arith-op)
232 (:args (x :scs (signed-reg) :target r
233 :load-if (not (and (sc-is x signed-stack)
235 (sc-is r signed-stack)
237 (y :scs (signed-reg signed-stack)))
238 (:arg-types signed-num signed-num)
239 (:results (r :scs (signed-reg) :from (:argument 0)
240 :load-if (not (and (sc-is x signed-stack)
243 (:result-types signed-num)
244 (:note "inline (signed-byte 32) arithmetic")
246 (cond ((and (sc-is x signed-reg) (sc-is y signed-reg) (sc-is r signed-reg)
247 (not (location= x r)))
248 (inst lea r (make-ea :qword :base x :index y :scale 1)))
254 ;;;; Special logand cases: (logand signed unsigned) => unsigned
256 (define-vop (fast-logand/signed-unsigned=>unsigned
257 fast-logand/unsigned=>unsigned)
258 (:args (x :target r :scs (signed-reg)
259 :load-if (not (and (sc-is x signed-stack)
260 (sc-is y unsigned-reg)
261 (sc-is r unsigned-stack)
263 (y :scs (unsigned-reg unsigned-stack)))
264 (:arg-types signed-num unsigned-num))
266 (define-vop (fast-logand-c/signed-unsigned=>unsigned
267 fast-logand-c/unsigned=>unsigned)
268 (:args (x :target r :scs (signed-reg signed-stack)))
269 (:arg-types signed-num (:constant (unsigned-byte 32))))
271 (define-vop (fast-logand/unsigned-signed=>unsigned
272 fast-logand/unsigned=>unsigned)
273 (:args (x :target r :scs (unsigned-reg)
274 :load-if (not (and (sc-is x unsigned-stack)
276 (sc-is r unsigned-stack)
278 (y :scs (signed-reg signed-stack)))
279 (:arg-types unsigned-num signed-num))
282 (define-vop (fast-+-c/signed=>signed fast-safe-arith-op)
284 (:args (x :target r :scs (signed-reg signed-stack)))
286 (:arg-types signed-num (:constant (signed-byte 32)))
287 (:results (r :scs (signed-reg)
288 :load-if (not (location= x r))))
289 (:result-types signed-num)
290 (:note "inline (signed-byte 32) arithmetic")
292 (cond ((and (sc-is x signed-reg) (sc-is r signed-reg)
293 (not (location= x r)))
294 (inst lea r (make-ea :qword :base x :disp y)))
301 (define-vop (fast-+/unsigned=>unsigned fast-safe-arith-op)
303 (:args (x :scs (unsigned-reg) :target r
304 :load-if (not (and (sc-is x unsigned-stack)
305 (sc-is y unsigned-reg)
306 (sc-is r unsigned-stack)
308 (y :scs (unsigned-reg unsigned-stack)))
309 (:arg-types unsigned-num unsigned-num)
310 (:results (r :scs (unsigned-reg) :from (:argument 0)
311 :load-if (not (and (sc-is x unsigned-stack)
312 (sc-is y unsigned-reg)
313 (sc-is r unsigned-stack)
315 (:result-types unsigned-num)
316 (:note "inline (unsigned-byte 32) arithmetic")
318 (cond ((and (sc-is x unsigned-reg) (sc-is y unsigned-reg)
319 (sc-is r unsigned-reg) (not (location= x r)))
320 (inst lea r (make-ea :qword :base x :index y :scale 1)))
325 (define-vop (fast-+-c/unsigned=>unsigned fast-safe-arith-op)
327 (:args (x :target r :scs (unsigned-reg unsigned-stack)))
329 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
330 (:results (r :scs (unsigned-reg)
331 :load-if (not (location= x r))))
332 (:result-types unsigned-num)
333 (:note "inline (unsigned-byte 32) arithmetic")
335 (cond ((and (sc-is x unsigned-reg) (sc-is r unsigned-reg)
336 (not (location= x r)))
337 (inst lea r (make-ea :qword :base x :disp y)))
344 ;;;; multiplication and division
346 (define-vop (fast-*/fixnum=>fixnum fast-safe-arith-op)
348 ;; We need different loading characteristics.
349 (:args (x :scs (any-reg) :target r)
350 (y :scs (any-reg control-stack)))
351 (:arg-types tagged-num tagged-num)
352 (:results (r :scs (any-reg) :from (:argument 0)))
353 (:result-types tagged-num)
354 (:note "inline fixnum arithmetic")
360 (define-vop (fast-*-c/fixnum=>fixnum fast-safe-arith-op)
362 ;; We need different loading characteristics.
363 (:args (x :scs (any-reg control-stack)))
365 (:arg-types tagged-num (:constant (signed-byte 29)))
366 (:results (r :scs (any-reg)))
367 (:result-types tagged-num)
368 (:note "inline fixnum arithmetic")
372 (define-vop (fast-*/signed=>signed fast-safe-arith-op)
374 ;; We need different loading characteristics.
375 (:args (x :scs (signed-reg) :target r)
376 (y :scs (signed-reg signed-stack)))
377 (:arg-types signed-num signed-num)
378 (:results (r :scs (signed-reg) :from (:argument 0)))
379 (:result-types signed-num)
380 (:note "inline (signed-byte 32) arithmetic")
385 (define-vop (fast-*-c/signed=>signed fast-safe-arith-op)
387 ;; We need different loading characteristics.
388 (:args (x :scs (signed-reg signed-stack)))
390 (:arg-types signed-num (:constant (signed-byte 32)))
391 (:results (r :scs (signed-reg)))
392 (:result-types signed-num)
393 (:note "inline (signed-byte 32) arithmetic")
397 (define-vop (fast-*/unsigned=>unsigned fast-safe-arith-op)
399 (:args (x :scs (unsigned-reg) :target eax)
400 (y :scs (unsigned-reg unsigned-stack)))
401 (:arg-types unsigned-num unsigned-num)
402 (:temporary (:sc unsigned-reg :offset eax-offset :target result
403 :from (:argument 0) :to :result) eax)
404 (:temporary (:sc unsigned-reg :offset edx-offset
405 :from :eval :to :result) edx)
407 (:results (result :scs (unsigned-reg)))
408 (:result-types unsigned-num)
409 (:note "inline (unsigned-byte 32) arithmetic")
411 (:save-p :compute-only)
418 (define-vop (fast-truncate/fixnum=>fixnum fast-safe-arith-op)
419 (:translate truncate)
420 (:args (x :scs (any-reg) :target eax)
421 (y :scs (any-reg control-stack)))
422 (:arg-types tagged-num tagged-num)
423 (:temporary (:sc signed-reg :offset eax-offset :target quo
424 :from (:argument 0) :to (:result 0)) eax)
425 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
426 :from (:argument 0) :to (:result 1)) edx)
427 (:results (quo :scs (any-reg))
428 (rem :scs (any-reg)))
429 (:result-types tagged-num tagged-num)
430 (:note "inline fixnum arithmetic")
432 (:save-p :compute-only)
434 (let ((zero (generate-error-code vop division-by-zero-error x y)))
435 (if (sc-is y any-reg)
436 (inst test y y) ; smaller instruction
442 (if (location= quo eax)
444 (inst lea quo (make-ea :qword :index eax :scale 8)))
447 (define-vop (fast-truncate-c/fixnum=>fixnum fast-safe-arith-op)
448 (:translate truncate)
449 (:args (x :scs (any-reg) :target eax))
451 (:arg-types tagged-num (:constant (signed-byte 29)))
452 (:temporary (:sc signed-reg :offset eax-offset :target quo
453 :from :argument :to (:result 0)) eax)
454 (:temporary (:sc any-reg :offset edx-offset :target rem
455 :from :eval :to (:result 1)) edx)
456 (:temporary (:sc any-reg :from :eval :to :result) y-arg)
457 (:results (quo :scs (any-reg))
458 (rem :scs (any-reg)))
459 (:result-types tagged-num tagged-num)
460 (:note "inline fixnum arithmetic")
462 (:save-p :compute-only)
466 (inst mov y-arg (fixnumize y))
467 (inst idiv eax y-arg)
468 (if (location= quo eax)
470 (inst lea quo (make-ea :qword :index eax :scale 8)))
473 (define-vop (fast-truncate/unsigned=>unsigned fast-safe-arith-op)
474 (:translate truncate)
475 (:args (x :scs (unsigned-reg) :target eax)
476 (y :scs (unsigned-reg signed-stack)))
477 (:arg-types unsigned-num unsigned-num)
478 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
479 :from (:argument 0) :to (:result 0)) eax)
480 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
481 :from (:argument 0) :to (:result 1)) edx)
482 (:results (quo :scs (unsigned-reg))
483 (rem :scs (unsigned-reg)))
484 (:result-types unsigned-num unsigned-num)
485 (:note "inline (unsigned-byte 32) arithmetic")
487 (:save-p :compute-only)
489 (let ((zero (generate-error-code vop division-by-zero-error x y)))
490 (if (sc-is y unsigned-reg)
491 (inst test y y) ; smaller instruction
500 (define-vop (fast-truncate-c/unsigned=>unsigned fast-safe-arith-op)
501 (:translate truncate)
502 (:args (x :scs (unsigned-reg) :target eax))
504 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
505 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
506 :from :argument :to (:result 0)) eax)
507 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
508 :from :eval :to (:result 1)) edx)
509 (:temporary (:sc unsigned-reg :from :eval :to :result) y-arg)
510 (:results (quo :scs (unsigned-reg))
511 (rem :scs (unsigned-reg)))
512 (:result-types unsigned-num unsigned-num)
513 (:note "inline (unsigned-byte 32) arithmetic")
515 (:save-p :compute-only)
524 (define-vop (fast-truncate/signed=>signed fast-safe-arith-op)
525 (:translate truncate)
526 (:args (x :scs (signed-reg) :target eax)
527 (y :scs (signed-reg signed-stack)))
528 (:arg-types signed-num signed-num)
529 (:temporary (:sc signed-reg :offset eax-offset :target quo
530 :from (:argument 0) :to (:result 0)) eax)
531 (:temporary (:sc signed-reg :offset edx-offset :target rem
532 :from (:argument 0) :to (:result 1)) edx)
533 (:results (quo :scs (signed-reg))
534 (rem :scs (signed-reg)))
535 (:result-types signed-num signed-num)
536 (:note "inline (signed-byte 32) arithmetic")
538 (:save-p :compute-only)
540 (let ((zero (generate-error-code vop division-by-zero-error x y)))
541 (if (sc-is y signed-reg)
542 (inst test y y) ; smaller instruction
551 (define-vop (fast-truncate-c/signed=>signed fast-safe-arith-op)
552 (:translate truncate)
553 (:args (x :scs (signed-reg) :target eax))
555 (:arg-types signed-num (:constant (signed-byte 32)))
556 (:temporary (:sc signed-reg :offset eax-offset :target quo
557 :from :argument :to (:result 0)) eax)
558 (:temporary (:sc signed-reg :offset edx-offset :target rem
559 :from :eval :to (:result 1)) edx)
560 (:temporary (:sc signed-reg :from :eval :to :result) y-arg)
561 (:results (quo :scs (signed-reg))
562 (rem :scs (signed-reg)))
563 (:result-types signed-num signed-num)
564 (:note "inline (signed-byte 32) arithmetic")
566 (:save-p :compute-only)
571 (inst idiv eax y-arg)
578 (define-vop (fast-ash-c/fixnum=>fixnum)
581 (:args (number :scs (any-reg) :target result
582 :load-if (not (and (sc-is number any-reg control-stack)
583 (sc-is result any-reg control-stack)
584 (location= number result)))))
586 (:arg-types tagged-num (:constant integer))
587 (:results (result :scs (any-reg)
588 :load-if (not (and (sc-is number control-stack)
589 (sc-is result control-stack)
590 (location= number result)))))
591 (:result-types tagged-num)
594 (cond ((and (= amount 1) (not (location= number result)))
595 (inst lea result (make-ea :qword :index number :scale 2)))
596 ((and (= amount 2) (not (location= number result)))
597 (inst lea result (make-ea :qword :index number :scale 4)))
598 ((and (= amount 3) (not (location= number result)))
599 (inst lea result (make-ea :qword :index number :scale 8)))
602 (cond ((plusp amount)
603 ;; We don't have to worry about overflow because of the
604 ;; result type restriction.
605 (inst shl result amount))
608 (inst xor result result))
610 ;; shift too far then back again, to zero tag bits
611 (inst sar result (- 3 amount))
613 (make-ea :qword :index result :scale 8))))))))
616 (define-vop (fast-ash-left/fixnum=>fixnum)
618 (:args (number :scs (any-reg) :target result
619 :load-if (not (and (sc-is number control-stack)
620 (sc-is result control-stack)
621 (location= number result))))
622 (amount :scs (unsigned-reg) :target ecx))
623 (:arg-types tagged-num positive-fixnum)
624 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
625 (:results (result :scs (any-reg) :from (:argument 0)
626 :load-if (not (and (sc-is number control-stack)
627 (sc-is result control-stack)
628 (location= number result)))))
629 (:result-types tagged-num)
635 ;; The result-type ensures us that this shift will not overflow.
636 (inst shl result :cl)))
638 (define-vop (fast-ash-c/signed=>signed)
641 (:args (number :scs (signed-reg) :target result
642 :load-if (not (and (sc-is number signed-stack)
643 (sc-is result signed-stack)
644 (location= number result)))))
646 (:arg-types signed-num (:constant integer))
647 (:results (result :scs (signed-reg)
648 :load-if (not (and (sc-is number signed-stack)
649 (sc-is result signed-stack)
650 (location= number result)))))
651 (:result-types signed-num)
654 (cond ((and (= amount 1) (not (location= number result)))
655 (inst lea result (make-ea :qword :index number :scale 2)))
656 ((and (= amount 2) (not (location= number result)))
657 (inst lea result (make-ea :qword :index number :scale 4)))
658 ((and (= amount 3) (not (location= number result)))
659 (inst lea result (make-ea :qword :index number :scale 8)))
662 (cond ((plusp amount) (inst shl result amount))
663 (t (inst sar result (min 63 (- amount)))))))))
665 (define-vop (fast-ash-c/unsigned=>unsigned)
668 (:args (number :scs (unsigned-reg) :target result
669 :load-if (not (and (sc-is number unsigned-stack)
670 (sc-is result unsigned-stack)
671 (location= number result)))))
673 (:arg-types unsigned-num (:constant integer))
674 (:results (result :scs (unsigned-reg)
675 :load-if (not (and (sc-is number unsigned-stack)
676 (sc-is result unsigned-stack)
677 (location= number result)))))
678 (:result-types unsigned-num)
681 (cond ((and (= amount 1) (not (location= number result)))
682 (inst lea result (make-ea :qword :index number :scale 2)))
683 ((and (= amount 2) (not (location= number result)))
684 (inst lea result (make-ea :qword :index number :scale 4)))
685 ((and (= amount 3) (not (location= number result)))
686 (inst lea result (make-ea :qword :index number :scale 8)))
689 (cond ((< -64 amount 64) ;; XXXX
690 ;; this code is used both in ASH and ASH-MOD32, so
693 (inst shl result amount)
694 (inst shr result (- amount))))
695 (t (if (sc-is result unsigned-reg)
696 (inst xor result result)
697 (inst mov result 0))))))))
699 (define-vop (fast-ash-left/signed=>signed)
701 (:args (number :scs (signed-reg) :target result
702 :load-if (not (and (sc-is number signed-stack)
703 (sc-is result signed-stack)
704 (location= number result))))
705 (amount :scs (unsigned-reg) :target ecx))
706 (:arg-types signed-num positive-fixnum)
707 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
708 (:results (result :scs (signed-reg) :from (:argument 0)
709 :load-if (not (and (sc-is number signed-stack)
710 (sc-is result signed-stack)
711 (location= number result)))))
712 (:result-types signed-num)
718 (inst shl result :cl)))
720 (define-vop (fast-ash-left/unsigned=>unsigned)
722 (:args (number :scs (unsigned-reg) :target result
723 :load-if (not (and (sc-is number unsigned-stack)
724 (sc-is result unsigned-stack)
725 (location= number result))))
726 (amount :scs (unsigned-reg) :target ecx))
727 (:arg-types unsigned-num positive-fixnum)
728 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
729 (:results (result :scs (unsigned-reg) :from (:argument 0)
730 :load-if (not (and (sc-is number unsigned-stack)
731 (sc-is result unsigned-stack)
732 (location= number result)))))
733 (:result-types unsigned-num)
739 (inst shl result :cl)))
741 (define-vop (fast-ash/signed=>signed)
744 (:args (number :scs (signed-reg) :target result)
745 (amount :scs (signed-reg) :target ecx))
746 (:arg-types signed-num signed-num)
747 (:results (result :scs (signed-reg) :from (:argument 0)))
748 (:result-types signed-num)
749 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
755 (inst jmp :ns positive)
761 (inst sar result :cl)
765 ;; The result-type ensures us that this shift will not overflow.
766 (inst shl result :cl)
770 (define-vop (fast-ash/unsigned=>unsigned)
773 (:args (number :scs (unsigned-reg) :target result)
774 (amount :scs (signed-reg) :target ecx))
775 (:arg-types unsigned-num signed-num)
776 (:results (result :scs (unsigned-reg) :from (:argument 0)))
777 (:result-types unsigned-num)
778 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
784 (inst jmp :ns positive)
788 (inst xor result result)
791 (inst shr result :cl)
795 ;; The result-type ensures us that this shift will not overflow.
796 (inst shl result :cl)
802 (defknown %lea (integer integer (member 1 2 4 8 16) (signed-byte 64))
804 (foldable flushable movable))
806 (defoptimizer (%lea derive-type) ((base index scale disp))
807 (when (and (constant-lvar-p scale)
808 (constant-lvar-p disp))
809 (let ((scale (lvar-value scale))
810 (disp (lvar-value disp))
811 (base-type (lvar-type base))
812 (index-type (lvar-type index)))
813 (when (and (numeric-type-p base-type)
814 (numeric-type-p index-type))
815 (let ((base-lo (numeric-type-low base-type))
816 (base-hi (numeric-type-high base-type))
817 (index-lo (numeric-type-low index-type))
818 (index-hi (numeric-type-high index-type)))
819 (make-numeric-type :class 'integer
821 :low (when (and base-lo index-lo)
822 (+ base-lo (* index-lo scale) disp))
823 :high (when (and base-hi index-hi)
824 (+ base-hi (* index-hi scale) disp))))))))
826 (defun %lea (base index scale disp)
827 (+ base (* index scale) disp))
831 (define-vop (%lea/unsigned=>unsigned)
834 (:args (base :scs (unsigned-reg))
835 (index :scs (unsigned-reg)))
837 (:arg-types unsigned-num unsigned-num
838 (:constant (member 1 2 4 8))
839 (:constant (signed-byte 64)))
840 (:results (r :scs (unsigned-reg)))
841 (:result-types unsigned-num)
843 (inst lea r (make-ea :qword :base base :index index
844 :scale scale :disp disp))))
846 (define-vop (%lea/signed=>signed)
849 (:args (base :scs (signed-reg))
850 (index :scs (signed-reg)))
852 (:arg-types signed-num signed-num
853 (:constant (member 1 2 4 8))
854 (:constant (signed-byte 64)))
855 (:results (r :scs (signed-reg)))
856 (:result-types signed-num)
858 (inst lea r (make-ea :qword :base base :index index
859 :scale scale :disp disp))))
861 (define-vop (%lea/fixnum=>fixnum)
864 (:args (base :scs (any-reg))
865 (index :scs (any-reg)))
867 (:arg-types tagged-num tagged-num
868 (:constant (member 1 2 4 8))
869 (:constant (signed-byte 64)))
870 (:results (r :scs (any-reg)))
871 (:result-types tagged-num)
873 (inst lea r (make-ea :qword :base base :index index
874 :scale scale :disp disp))))
876 ;;; FIXME: before making knowledge of this too public, it needs to be
877 ;;; fixed so that it's actually _faster_ than the non-CMOV version; at
878 ;;; least on my Celeron-XXX laptop, this version is marginally slower
879 ;;; than the above version with branches. -- CSR, 2003-09-04
880 (define-vop (fast-cmov-ash/unsigned=>unsigned)
883 (:args (number :scs (unsigned-reg) :target result)
884 (amount :scs (signed-reg) :target ecx))
885 (:arg-types unsigned-num signed-num)
886 (:results (result :scs (unsigned-reg) :from (:argument 0)))
887 (:result-types unsigned-num)
888 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
889 (:temporary (:sc any-reg :from (:eval 0) :to (:eval 1)) zero)
891 (:guard (member :cmov *backend-subfeatures*))
896 (inst jmp :ns positive)
899 (inst shr result :cl)
901 (inst cmov :nbe result zero)
905 ;; The result-type ensures us that this shift will not overflow.
906 (inst shl result :cl)
910 ;;; Note: documentation for this function is wrong - rtfm
911 (define-vop (signed-byte-64-len)
912 (:translate integer-length)
913 (:note "inline (signed-byte 32) integer-length")
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 32) 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)
964 (inst mov temp result)
966 (inst and result #x55555555) ; note these masks will restrict the
967 (inst and temp #x55555555) ; count to the lower half of arg
968 (inst add result temp)
970 (inst mov temp result)
972 (inst and result #x33333333)
973 (inst and temp #x33333333)
974 (inst add result temp)
976 (inst mov temp result)
978 (inst and result #x0f0f0f0f)
979 (inst and temp #x0f0f0f0f)
980 (inst add result temp)
982 (inst mov temp result)
984 (inst and result #x00ff00ff)
985 (inst and temp #x00ff00ff)
986 (inst add result temp)
988 (inst mov temp result)
990 (inst and result #x0000ffff)
991 (inst and temp #x0000ffff)
992 (inst add result temp)
994 ;;; 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 32) 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 32)))
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 32) 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 32)))
1080 (:info target not-p y))
1083 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
1086 (lambda (suffix cost signed)
1087 `(define-vop (;; FIXME: These could be done more
1088 ;; cleanly with SYMBOLICATE.
1089 ,(intern (format nil "~:@(FAST-IF-~A~A~)"
1092 (format nil "~:@(FAST-CONDITIONAL~A~)"
1097 ,(if (eq suffix '-c/fixnum)
1108 '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
1109 ; '(/fixnum /signed /unsigned)
1111 '(t t t t nil nil)))))
1113 (define-conditional-vop < :l :b :ge :ae)
1114 (define-conditional-vop > :g :a :le :be))
1116 (define-vop (fast-if-eql/signed fast-conditional/signed)
1120 (inst jmp (if not-p :ne :e) target)))
1122 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
1125 (cond ((and (sc-is x signed-reg) (zerop y))
1126 (inst test x x)) ; smaller instruction
1129 (inst jmp (if not-p :ne :e) target)))
1131 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1135 (inst jmp (if not-p :ne :e) target)))
1137 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1140 (cond ((and (sc-is x unsigned-reg) (zerop y))
1141 (inst test x x)) ; smaller instruction
1144 (inst jmp (if not-p :ne :e) target)))
1146 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1149 ;;; These versions specify a fixnum restriction on their first arg. We have
1150 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1151 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1152 ;;; fixnum specific operations from being used on word integers, spuriously
1153 ;;; consing the argument.
1155 (define-vop (fast-eql/fixnum fast-conditional)
1156 (:args (x :scs (any-reg)
1157 :load-if (not (and (sc-is x control-stack)
1158 (sc-is y any-reg))))
1159 (y :scs (any-reg control-stack)))
1160 (:arg-types tagged-num tagged-num)
1161 (:note "inline fixnum comparison")
1165 (inst jmp (if not-p :ne :e) target)))
1166 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1167 (:args (x :scs (any-reg descriptor-reg)
1168 :load-if (not (and (sc-is x control-stack)
1169 (sc-is y any-reg))))
1170 (y :scs (any-reg control-stack)))
1171 (:arg-types * tagged-num)
1175 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1176 (:args (x :scs (any-reg control-stack)))
1177 (:arg-types tagged-num (:constant (signed-byte 29)))
1178 (:info target not-p y)
1181 (cond ((and (sc-is x any-reg) (zerop y))
1182 (inst test x x)) ; smaller instruction
1184 (inst cmp x (fixnumize y))))
1185 (inst jmp (if not-p :ne :e) target)))
1187 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1188 (:args (x :scs (any-reg descriptor-reg control-stack)))
1189 (:arg-types * (:constant (signed-byte 29)))
1192 ;;;; 32-bit logical operations
1194 (define-vop (merge-bits)
1195 (:translate merge-bits)
1196 (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1197 (prev :scs (unsigned-reg) :target result)
1198 (next :scs (unsigned-reg)))
1199 (:arg-types tagged-num unsigned-num unsigned-num)
1200 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1201 (:results (result :scs (unsigned-reg) :from (:argument 1)))
1202 (:result-types unsigned-num)
1203 (:policy :fast-safe)
1207 (inst shrd result next :cl)))
1209 ;;; Only the lower 6 bits of the shift amount are significant.
1210 (define-vop (shift-towards-someplace)
1211 (:policy :fast-safe)
1212 (:args (num :scs (unsigned-reg) :target r)
1213 (amount :scs (signed-reg) :target ecx))
1214 (:arg-types unsigned-num tagged-num)
1215 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1216 (:results (r :scs (unsigned-reg) :from (:argument 0)))
1217 (:result-types unsigned-num))
1219 (define-vop (shift-towards-start shift-towards-someplace)
1220 (:translate shift-towards-start)
1221 (:note "SHIFT-TOWARDS-START")
1227 (define-vop (shift-towards-end shift-towards-someplace)
1228 (:translate shift-towards-end)
1229 (:note "SHIFT-TOWARDS-END")
1235 ;;;; Modular functions
1237 (macrolet ((define-modular-backend (fun &optional constantp)
1239 (dolist (info '((60 fixnum) (64 unsigned)))
1240 (destructuring-bind (width regtype) info
1241 (let ((mfun-name (intern (format nil "~A-MOD~A" fun width)))
1242 (mvop (intern (format nil "FAST-~A-MOD~A/~A=>~A"
1243 fun width regtype regtype)))
1244 (mcvop (intern (format nil "FAST-~A-MOD~A-C/~A=>~A"
1245 fun width regtype regtype)))
1246 (vop (intern (format nil "FAST-~A/~A=>~A"
1247 fun regtype regtype)))
1248 (cvop (intern (format nil "FAST-~A-C/~A=>~A"
1249 fun regtype regtype))))
1250 (forms `(define-modular-fun ,mfun-name (x y) ,fun ,width))
1251 (forms `(define-vop (,mvop ,vop)
1252 (:translate ,mfun-name)))
1254 (forms `(define-vop (,mcvop ,cvop)
1255 (:translate ,mfun-name)))))))
1256 `(progn ,@(forms)))))
1257 (define-modular-backend + t)
1258 (define-modular-backend - t)
1259 (define-modular-backend *) ; FIXME: there exists a
1260 ; FAST-*-C/FIXNUM=>FIXNUM VOP which
1261 ; should be used for the MOD60 case,
1262 ; but the MOD64 case cannot accept
1263 ; immediate arguments.
1264 (define-modular-backend logxor t))
1266 (define-vop (fast-ash-left-mod64-c/unsigned=>unsigned
1267 fast-ash-c/unsigned=>unsigned)
1268 (:translate ash-left-mod64))
1272 (defknown sb!vm::%lea-mod64 (integer integer (member 1 2 4 8) (signed-byte 64))
1274 (foldable flushable movable))
1276 (define-modular-fun-optimizer %lea ((base index scale disp) :width width)
1277 (when (and (<= width 64)
1278 (constant-lvar-p scale)
1279 (constant-lvar-p disp))
1280 (cut-to-width base width)
1281 (cut-to-width index width)
1282 'sb!vm::%lea-mod64))
1285 (defun sb!vm::%lea-mod64 (base index scale disp)
1286 (ldb (byte 64 0) (%lea base index scale disp)))
1288 (defun sb!vm::%lea-mod64 (base index scale disp)
1289 (let ((base (logand base #xffffffffffffffff))
1290 (index (logand index #xffffffffffffffff)))
1291 ;; can't use modular version of %LEA, as we only have VOPs for
1292 ;; constant SCALE and DISP.
1293 (ldb (byte 64 0) (+ base (* index scale) disp))))
1295 (in-package "SB!VM")
1297 (define-vop (%lea-mod64/unsigned=>unsigned
1298 %lea/unsigned=>unsigned)
1299 (:translate %lea-mod64))
1301 ;;; logical operations
1302 (define-modular-fun lognot-mod64 (x) lognot 64)
1303 (define-vop (lognot-mod64/unsigned=>unsigned)
1304 (:translate lognot-mod64)
1305 (:args (x :scs (unsigned-reg unsigned-stack) :target r
1306 :load-if (not (and (sc-is x unsigned-stack)
1307 (sc-is r unsigned-stack)
1309 (:arg-types unsigned-num)
1310 (:results (r :scs (unsigned-reg)
1311 :load-if (not (and (sc-is x unsigned-stack)
1312 (sc-is r unsigned-stack)
1314 (:result-types unsigned-num)
1315 (:policy :fast-safe)
1320 (define-source-transform logeqv (&rest args)
1321 (if (oddp (length args))
1323 `(lognot (logxor ,@args))))
1324 (define-source-transform logandc1 (x y)
1325 `(logand (lognot ,x) ,y))
1326 (define-source-transform logandc2 (x y)
1327 `(logand ,x (lognot ,y)))
1328 (define-source-transform logorc1 (x y)
1329 `(logior (lognot ,x) ,y))
1330 (define-source-transform logorc2 (x y)
1331 `(logior ,x (lognot ,y)))
1332 (define-source-transform lognor (x y)
1333 `(lognot (logior ,x ,y)))
1334 (define-source-transform lognand (x y)
1335 `(lognot (logand ,x ,y)))
1339 (define-vop (bignum-length get-header-data)
1340 (:translate sb!bignum:%bignum-length)
1341 (:policy :fast-safe))
1343 (define-vop (bignum-set-length set-header-data)
1344 (:translate sb!bignum:%bignum-set-length)
1345 (:policy :fast-safe))
1347 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1348 (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1350 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1351 (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1353 (define-vop (digit-0-or-plus)
1354 (:translate sb!bignum:%digit-0-or-plusp)
1355 (:policy :fast-safe)
1356 (:args (digit :scs (unsigned-reg)))
1357 (:arg-types unsigned-num)
1359 (:info target not-p)
1361 (inst or digit digit)
1362 (inst jmp (if not-p :s :ns) target)))
1365 ;;; For add and sub with carry the sc of carry argument is any-reg so
1366 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1367 ;;; 4. This is easy to deal with and may save a fixnum-word
1369 (define-vop (add-w/carry)
1370 (:translate sb!bignum:%add-with-carry)
1371 (:policy :fast-safe)
1372 (:args (a :scs (unsigned-reg) :target result)
1373 (b :scs (unsigned-reg unsigned-stack) :to :eval)
1374 (c :scs (any-reg) :target temp))
1375 (:arg-types unsigned-num unsigned-num positive-fixnum)
1376 (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1377 (:results (result :scs (unsigned-reg) :from (:argument 0))
1378 (carry :scs (unsigned-reg)))
1379 (:result-types unsigned-num positive-fixnum)
1383 (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1386 (inst adc carry carry)))
1388 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1389 ;;; borrow and 0 for a borrow.
1390 (define-vop (sub-w/borrow)
1391 (:translate sb!bignum:%subtract-with-borrow)
1392 (:policy :fast-safe)
1393 (:args (a :scs (unsigned-reg) :to :eval :target result)
1394 (b :scs (unsigned-reg unsigned-stack) :to :result)
1395 (c :scs (any-reg control-stack)))
1396 (:arg-types unsigned-num unsigned-num positive-fixnum)
1397 (:results (result :scs (unsigned-reg) :from :eval)
1398 (borrow :scs (unsigned-reg)))
1399 (:result-types unsigned-num positive-fixnum)
1401 (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1405 (inst adc borrow borrow)
1406 (inst xor borrow 1)))
1409 (define-vop (bignum-mult-and-add-3-arg)
1410 (:translate sb!bignum:%multiply-and-add)
1411 (:policy :fast-safe)
1412 (:args (x :scs (unsigned-reg) :target eax)
1413 (y :scs (unsigned-reg unsigned-stack))
1414 (carry-in :scs (unsigned-reg unsigned-stack)))
1415 (:arg-types unsigned-num unsigned-num unsigned-num)
1416 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1417 :to (:result 1) :target lo) eax)
1418 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1419 :to (:result 0) :target hi) edx)
1420 (:results (hi :scs (unsigned-reg))
1421 (lo :scs (unsigned-reg)))
1422 (:result-types unsigned-num unsigned-num)
1426 (inst add eax carry-in)
1431 (define-vop (bignum-mult-and-add-4-arg)
1432 (:translate sb!bignum:%multiply-and-add)
1433 (:policy :fast-safe)
1434 (:args (x :scs (unsigned-reg) :target eax)
1435 (y :scs (unsigned-reg unsigned-stack))
1436 (prev :scs (unsigned-reg unsigned-stack))
1437 (carry-in :scs (unsigned-reg unsigned-stack)))
1438 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1439 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1440 :to (:result 1) :target lo) eax)
1441 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1442 :to (:result 0) :target hi) edx)
1443 (:results (hi :scs (unsigned-reg))
1444 (lo :scs (unsigned-reg)))
1445 (:result-types unsigned-num unsigned-num)
1451 (inst add eax carry-in)
1457 (define-vop (bignum-mult)
1458 (:translate sb!bignum:%multiply)
1459 (:policy :fast-safe)
1460 (:args (x :scs (unsigned-reg) :target eax)
1461 (y :scs (unsigned-reg unsigned-stack)))
1462 (:arg-types unsigned-num unsigned-num)
1463 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1464 :to (:result 1) :target lo) eax)
1465 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1466 :to (:result 0) :target hi) edx)
1467 (:results (hi :scs (unsigned-reg))
1468 (lo :scs (unsigned-reg)))
1469 (:result-types unsigned-num unsigned-num)
1476 (define-vop (bignum-lognot lognot-mod64/unsigned=>unsigned)
1477 (:translate sb!bignum:%lognot))
1479 (define-vop (fixnum-to-digit)
1480 (:translate sb!bignum:%fixnum-to-digit)
1481 (:policy :fast-safe)
1482 (:args (fixnum :scs (any-reg control-stack) :target digit))
1483 (:arg-types tagged-num)
1484 (:results (digit :scs (unsigned-reg)
1485 :load-if (not (and (sc-is fixnum control-stack)
1486 (sc-is digit unsigned-stack)
1487 (location= fixnum digit)))))
1488 (:result-types unsigned-num)
1491 (inst sar digit 3)))
1493 (define-vop (bignum-floor)
1494 (:translate sb!bignum:%floor)
1495 (:policy :fast-safe)
1496 (:args (div-high :scs (unsigned-reg) :target edx)
1497 (div-low :scs (unsigned-reg) :target eax)
1498 (divisor :scs (unsigned-reg unsigned-stack)))
1499 (:arg-types unsigned-num unsigned-num unsigned-num)
1500 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1501 :to (:result 0) :target quo) eax)
1502 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1503 :to (:result 1) :target rem) edx)
1504 (:results (quo :scs (unsigned-reg))
1505 (rem :scs (unsigned-reg)))
1506 (:result-types unsigned-num unsigned-num)
1510 (inst div eax divisor)
1514 (define-vop (signify-digit)
1515 (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1516 (:policy :fast-safe)
1517 (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1518 (:arg-types unsigned-num)
1519 (:results (res :scs (any-reg signed-reg)
1520 :load-if (not (and (sc-is digit unsigned-stack)
1521 (sc-is res control-stack signed-stack)
1522 (location= digit res)))))
1523 (:result-types signed-num)
1526 (when (sc-is res any-reg control-stack)
1529 (define-vop (digit-ashr)
1530 (:translate sb!bignum:%ashr)
1531 (:policy :fast-safe)
1532 (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1533 (count :scs (unsigned-reg) :target ecx))
1534 (:arg-types unsigned-num positive-fixnum)
1535 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1536 (:results (result :scs (unsigned-reg) :from (:argument 0)
1537 :load-if (not (and (sc-is result unsigned-stack)
1538 (location= digit result)))))
1539 (:result-types unsigned-num)
1543 (inst sar result :cl)))
1545 (define-vop (digit-lshr digit-ashr)
1546 (:translate sb!bignum:%digit-logical-shift-right)
1550 (inst shr result :cl)))
1552 (define-vop (digit-ashl digit-ashr)
1553 (:translate sb!bignum:%ashl)
1557 (inst shl result :cl)))
1559 ;;;; static functions
1561 (define-static-fun two-arg-/ (x y) :translate /)
1563 (define-static-fun two-arg-gcd (x y) :translate gcd)
1564 (define-static-fun two-arg-lcm (x y) :translate lcm)
1566 (define-static-fun two-arg-and (x y) :translate logand)
1567 (define-static-fun two-arg-ior (x y) :translate logior)
1568 (define-static-fun two-arg-xor (x y) :translate logxor)
1573 ;;; This is essentially a straight implementation of the algorithm in
1574 ;;; "Strength Reduction of Multiplications by Integer Constants",
1575 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1576 (defun basic-decompose-multiplication (arg num n-bits condensed)
1577 (case (aref condensed 0)
1579 (let ((tmp (min 3 (aref condensed 1))))
1580 (decf (aref condensed 1) tmp)
1583 ,(decompose-multiplication
1584 arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1587 (let ((r0 (aref condensed 0)))
1588 (incf (aref condensed 1) r0)
1590 (%lea ,(decompose-multiplication
1591 arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1594 (t (let ((r0 (aref condensed 0)))
1595 (setf (aref condensed 0) 0)
1597 (ash ,(decompose-multiplication
1598 arg (ash num (- r0)) n-bits condensed)
1601 (defun decompose-multiplication (arg num n-bits condensed)
1606 `(logand #xffffffff (ash ,arg ,(1- (integer-length num)))))
1607 ((let ((max 0) (end 0))
1608 (loop for i from 2 to (length condensed)
1609 for j = (reduce #'+ (subseq condensed 0 i))
1610 when (and (> (- (* 2 i) 3 j) max)
1611 (< (+ (ash 1 (1+ j))
1612 (ash (ldb (byte (- 64 (1+ j)) (1+ j)) num)
1615 do (setq max (- (* 2 i) 3 j)
1618 (let ((j (reduce #'+ (subseq condensed 0 end))))
1619 (let ((n2 (+ (ash 1 (1+ j))
1620 (ash (ldb (byte (- 64 (1+ j)) (1+ j)) num) (1+ j))))
1621 (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1623 (- ,(optimize-multiply arg n2) ,(optimize-multiply arg n1))))))))
1624 ((dolist (i '(9 5 3))
1625 (when (integerp (/ num i))
1626 (when (< (logcount (/ num i)) (logcount num))
1628 (return `(let ((,x ,(optimize-multiply arg (/ num i))))
1630 (%lea ,x ,x (1- ,i) 0)))))))))
1631 (t (basic-decompose-multiplication arg num n-bits condensed))))
1633 (defun optimize-multiply (arg x)
1634 (let* ((n-bits (logcount x))
1635 (condensed (make-array n-bits)))
1636 (let ((count 0) (bit 0))
1638 (cond ((logbitp i x)
1639 (setf (aref condensed bit) count)
1643 (decompose-multiplication arg x n-bits condensed)))
1645 (defun *-transformer (y)
1647 (t (give-up-ir1-transform))
1648 ((= y (ash 1 (integer-length y)))
1649 ;; there's a generic transform for y = 2^k
1650 (give-up-ir1-transform))
1651 ((member y '(3 5 9))
1652 ;; we can do these multiplications directly using LEA
1653 `(%lea x x ,(1- y) 0))
1654 ((member :pentium4 *backend-subfeatures*)
1655 ;; the pentium4's multiply unit is reportedly very good
1656 (give-up-ir1-transform))
1657 ;; FIXME: should make this more fine-grained. If nothing else,
1658 ;; there should probably be a cutoff of about 9 instructions on
1659 ;; pentium-class machines.
1660 (t (optimize-multiply 'x y))))
1662 (deftransform * ((x y)
1663 ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1665 "recode as leas, shifts and adds"
1666 (let ((y (lvar-value y)))
1669 (deftransform sb!vm::*-mod64
1670 ((x y) ((unsigned-byte 64) (constant-arg (unsigned-byte 64)))
1672 "recode as leas, shifts and adds"
1673 (let ((y (lvar-value y)))
1676 ;;; FIXME: we should also be able to write an optimizer or two to
1677 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.