1 ;;;; the VM definition 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 30)))
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 (define-vop (fast-signed-binop-c fast-safe-arith-op)
130 (:args (x :target r :scs (signed-reg signed-stack)))
132 (:arg-types signed-num (:constant (signed-byte 32)))
133 (:results (r :scs (signed-reg)
134 :load-if (not (location= x r))))
135 (:result-types signed-num)
136 (:note "inline (signed-byte 32) arithmetic"))
138 (macrolet ((define-binop (translate untagged-penalty op)
140 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
142 (:translate ,translate)
146 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
148 (:translate ,translate)
151 (inst ,op r (fixnumize y))))
152 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
154 (:translate ,translate)
155 (:generator ,(1+ untagged-penalty)
158 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
160 (:translate ,translate)
161 (:generator ,untagged-penalty
164 (define-vop (,(symbolicate "FAST-"
166 "/UNSIGNED=>UNSIGNED")
168 (:translate ,translate)
169 (:generator ,(1+ untagged-penalty)
172 (define-vop (,(symbolicate 'fast-
174 '-c/unsigned=>unsigned)
175 fast-unsigned-binop-c)
176 (:translate ,translate)
177 (:generator ,untagged-penalty
181 ;;(define-binop + 4 add)
182 (define-binop - 4 sub)
183 (define-binop logand 2 and)
184 (define-binop logior 2 or)
185 (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 :dword :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 30)))
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 :dword :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 :dword :base x :index y :scale 1)))
253 (define-vop (fast-+-c/signed=>signed fast-safe-arith-op)
255 (:args (x :target r :scs (signed-reg signed-stack)))
257 (:arg-types signed-num (:constant (signed-byte 32)))
258 (:results (r :scs (signed-reg)
259 :load-if (not (location= x r))))
260 (:result-types signed-num)
261 (:note "inline (signed-byte 32) arithmetic")
263 (cond ((and (sc-is x signed-reg) (sc-is r signed-reg)
264 (not (location= x r)))
265 (inst lea r (make-ea :dword :base x :disp y)))
272 (define-vop (fast-+/unsigned=>unsigned fast-safe-arith-op)
274 (:args (x :scs (unsigned-reg) :target r
275 :load-if (not (and (sc-is x unsigned-stack)
276 (sc-is y unsigned-reg)
277 (sc-is r unsigned-stack)
279 (y :scs (unsigned-reg unsigned-stack)))
280 (:arg-types unsigned-num unsigned-num)
281 (:results (r :scs (unsigned-reg) :from (:argument 0)
282 :load-if (not (and (sc-is x unsigned-stack)
283 (sc-is y unsigned-reg)
284 (sc-is r unsigned-stack)
286 (:result-types unsigned-num)
287 (:note "inline (unsigned-byte 32) arithmetic")
289 (cond ((and (sc-is x unsigned-reg) (sc-is y unsigned-reg)
290 (sc-is r unsigned-reg) (not (location= x r)))
291 (inst lea r (make-ea :dword :base x :index y :scale 1)))
296 (define-vop (fast-+-c/unsigned=>unsigned fast-safe-arith-op)
298 (:args (x :target r :scs (unsigned-reg unsigned-stack)))
300 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
301 (:results (r :scs (unsigned-reg)
302 :load-if (not (location= x r))))
303 (:result-types unsigned-num)
304 (:note "inline (unsigned-byte 32) arithmetic")
306 (cond ((and (sc-is x unsigned-reg) (sc-is r unsigned-reg)
307 (not (location= x r)))
308 (inst lea r (make-ea :dword :base x :disp y)))
315 ;;;; multiplication and division
317 (define-vop (fast-*/fixnum=>fixnum fast-safe-arith-op)
319 ;; We need different loading characteristics.
320 (:args (x :scs (any-reg) :target r)
321 (y :scs (any-reg control-stack)))
322 (:arg-types tagged-num tagged-num)
323 (:results (r :scs (any-reg) :from (:argument 0)))
324 (:result-types tagged-num)
325 (:note "inline fixnum arithmetic")
331 (define-vop (fast-*-c/fixnum=>fixnum fast-safe-arith-op)
333 ;; We need different loading characteristics.
334 (:args (x :scs (any-reg control-stack)))
336 (:arg-types tagged-num (:constant (signed-byte 30)))
337 (:results (r :scs (any-reg)))
338 (:result-types tagged-num)
339 (:note "inline fixnum arithmetic")
343 (define-vop (fast-*/signed=>signed fast-safe-arith-op)
345 ;; We need different loading characteristics.
346 (:args (x :scs (signed-reg) :target r)
347 (y :scs (signed-reg signed-stack)))
348 (:arg-types signed-num signed-num)
349 (:results (r :scs (signed-reg) :from (:argument 0)))
350 (:result-types signed-num)
351 (:note "inline (signed-byte 32) arithmetic")
356 (define-vop (fast-*-c/signed=>signed fast-safe-arith-op)
358 ;; We need different loading characteristics.
359 (:args (x :scs (signed-reg signed-stack)))
361 (:arg-types signed-num (:constant (signed-byte 32)))
362 (:results (r :scs (signed-reg)))
363 (:result-types signed-num)
364 (:note "inline (signed-byte 32) arithmetic")
368 (define-vop (fast-*/unsigned=>unsigned fast-safe-arith-op)
370 (:args (x :scs (unsigned-reg) :target eax)
371 (y :scs (unsigned-reg unsigned-stack)))
372 (:arg-types unsigned-num unsigned-num)
373 (:temporary (:sc unsigned-reg :offset eax-offset :target result
374 :from (:argument 0) :to :result) eax)
375 (:temporary (:sc unsigned-reg :offset edx-offset
376 :from :eval :to :result) edx)
378 (:results (result :scs (unsigned-reg)))
379 (:result-types unsigned-num)
380 (:note "inline (unsigned-byte 32) arithmetic")
382 (:save-p :compute-only)
389 (define-vop (fast-truncate/fixnum=>fixnum fast-safe-arith-op)
390 (:translate truncate)
391 (:args (x :scs (any-reg) :target eax)
392 (y :scs (any-reg control-stack)))
393 (:arg-types tagged-num tagged-num)
394 (:temporary (:sc signed-reg :offset eax-offset :target quo
395 :from (:argument 0) :to (:result 0)) eax)
396 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
397 :from (:argument 0) :to (:result 1)) edx)
398 (:results (quo :scs (any-reg))
399 (rem :scs (any-reg)))
400 (:result-types tagged-num tagged-num)
401 (:note "inline fixnum arithmetic")
403 (:save-p :compute-only)
405 (let ((zero (generate-error-code vop division-by-zero-error x y)))
406 (if (sc-is y any-reg)
407 (inst test y y) ; smaller instruction
413 (if (location= quo eax)
415 (inst lea quo (make-ea :dword :index eax :scale 4)))
418 (define-vop (fast-truncate-c/fixnum=>fixnum fast-safe-arith-op)
419 (:translate truncate)
420 (:args (x :scs (any-reg) :target eax))
422 (:arg-types tagged-num (:constant (signed-byte 30)))
423 (:temporary (:sc signed-reg :offset eax-offset :target quo
424 :from :argument :to (:result 0)) eax)
425 (:temporary (:sc any-reg :offset edx-offset :target rem
426 :from :eval :to (:result 1)) edx)
427 (:temporary (:sc any-reg :from :eval :to :result) y-arg)
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)
437 (inst mov y-arg (fixnumize y))
438 (inst idiv eax y-arg)
439 (if (location= quo eax)
441 (inst lea quo (make-ea :dword :index eax :scale 4)))
444 (define-vop (fast-truncate/unsigned=>unsigned fast-safe-arith-op)
445 (:translate truncate)
446 (:args (x :scs (unsigned-reg) :target eax)
447 (y :scs (unsigned-reg signed-stack)))
448 (:arg-types unsigned-num unsigned-num)
449 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
450 :from (:argument 0) :to (:result 0)) eax)
451 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
452 :from (:argument 0) :to (:result 1)) edx)
453 (:results (quo :scs (unsigned-reg))
454 (rem :scs (unsigned-reg)))
455 (:result-types unsigned-num unsigned-num)
456 (:note "inline (unsigned-byte 32) arithmetic")
458 (:save-p :compute-only)
460 (let ((zero (generate-error-code vop division-by-zero-error x y)))
461 (if (sc-is y unsigned-reg)
462 (inst test y y) ; smaller instruction
471 (define-vop (fast-truncate-c/unsigned=>unsigned fast-safe-arith-op)
472 (:translate truncate)
473 (:args (x :scs (unsigned-reg) :target eax))
475 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
476 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
477 :from :argument :to (:result 0)) eax)
478 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
479 :from :eval :to (:result 1)) edx)
480 (:temporary (:sc unsigned-reg :from :eval :to :result) y-arg)
481 (:results (quo :scs (unsigned-reg))
482 (rem :scs (unsigned-reg)))
483 (:result-types unsigned-num unsigned-num)
484 (:note "inline (unsigned-byte 32) arithmetic")
486 (:save-p :compute-only)
495 (define-vop (fast-truncate/signed=>signed fast-safe-arith-op)
496 (:translate truncate)
497 (:args (x :scs (signed-reg) :target eax)
498 (y :scs (signed-reg signed-stack)))
499 (:arg-types signed-num signed-num)
500 (:temporary (:sc signed-reg :offset eax-offset :target quo
501 :from (:argument 0) :to (:result 0)) eax)
502 (:temporary (:sc signed-reg :offset edx-offset :target rem
503 :from (:argument 0) :to (:result 1)) edx)
504 (:results (quo :scs (signed-reg))
505 (rem :scs (signed-reg)))
506 (:result-types signed-num signed-num)
507 (:note "inline (signed-byte 32) arithmetic")
509 (:save-p :compute-only)
511 (let ((zero (generate-error-code vop division-by-zero-error x y)))
512 (if (sc-is y signed-reg)
513 (inst test y y) ; smaller instruction
522 (define-vop (fast-truncate-c/signed=>signed fast-safe-arith-op)
523 (:translate truncate)
524 (:args (x :scs (signed-reg) :target eax))
526 (:arg-types signed-num (:constant (signed-byte 32)))
527 (:temporary (:sc signed-reg :offset eax-offset :target quo
528 :from :argument :to (:result 0)) eax)
529 (:temporary (:sc signed-reg :offset edx-offset :target rem
530 :from :eval :to (:result 1)) edx)
531 (:temporary (:sc signed-reg :from :eval :to :result) y-arg)
532 (:results (quo :scs (signed-reg))
533 (rem :scs (signed-reg)))
534 (:result-types signed-num signed-num)
535 (:note "inline (signed-byte 32) arithmetic")
537 (:save-p :compute-only)
542 (inst idiv eax y-arg)
549 (define-vop (fast-ash-c/fixnum=>fixnum)
552 (:args (number :scs (any-reg) :target result
553 :load-if (not (and (sc-is number any-reg control-stack)
554 (sc-is result any-reg control-stack)
555 (location= number result)))))
557 (:arg-types tagged-num (:constant integer))
558 (:results (result :scs (any-reg)
559 :load-if (not (and (sc-is number control-stack)
560 (sc-is result control-stack)
561 (location= number result)))))
562 (:result-types tagged-num)
565 (cond ((and (= amount 1) (not (location= number result)))
566 (inst lea result (make-ea :dword :index number :scale 2)))
567 ((and (= amount 2) (not (location= number result)))
568 (inst lea result (make-ea :dword :index number :scale 4)))
569 ((and (= amount 3) (not (location= number result)))
570 (inst lea result (make-ea :dword :index number :scale 8)))
573 (cond ((plusp amount)
574 ;; We don't have to worry about overflow because of the
575 ;; result type restriction.
576 (inst shl result amount))
578 ;; If the amount is greater than 31, only shift by 31. We
579 ;; have to do this because the shift instructions only look
580 ;; at the low five bits of the result.
581 (inst sar result (min 31 (- amount)))
582 ;; Fixnum correction.
583 (inst and result #xfffffffc)))))))
585 (define-vop (fast-ash-left/fixnum=>fixnum)
587 (:args (number :scs (any-reg) :target result
588 :load-if (not (and (sc-is number control-stack)
589 (sc-is result control-stack)
590 (location= number result))))
591 (amount :scs (unsigned-reg) :target ecx))
592 (:arg-types tagged-num positive-fixnum)
593 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
594 (:results (result :scs (any-reg) :from (:argument 0)
595 :load-if (not (and (sc-is number control-stack)
596 (sc-is result control-stack)
597 (location= number result)))))
598 (:result-types tagged-num)
604 ;; The result-type ensures us that this shift will not overflow.
605 (inst shl result :cl)))
607 (define-vop (fast-ash-c)
610 (:args (number :scs (signed-reg unsigned-reg) :target result
611 :load-if (not (and (sc-is number signed-stack unsigned-stack)
612 (sc-is result signed-stack unsigned-stack)
613 (location= number result)))))
615 (:arg-types (:or signed-num unsigned-num) (:constant integer))
616 (:results (result :scs (signed-reg unsigned-reg)
618 (and (sc-is number signed-stack unsigned-stack)
619 (sc-is result signed-stack unsigned-stack)
620 (location= number result)))))
621 (:result-types (:or signed-num unsigned-num))
624 (cond ((and (= amount 1) (not (location= number result)))
625 (inst lea result (make-ea :dword :index number :scale 2)))
626 ((and (= amount 2) (not (location= number result)))
627 (inst lea result (make-ea :dword :index number :scale 4)))
628 ((and (= amount 3) (not (location= number result)))
629 (inst lea result (make-ea :dword :index number :scale 8)))
632 (cond ((plusp amount)
633 ;; We don't have to worry about overflow because of the
634 ;; result type restriction.
635 (inst shl result amount))
636 ((sc-is number signed-reg signed-stack)
637 ;; If the amount is greater than 31, only shift by 31. We
638 ;; have to do this because the shift instructions only look
639 ;; at the low five bits of the result.
640 (inst sar result (min 31 (- amount))))
642 (inst shr result (min 31 (- amount)))))))))
644 (define-vop (fast-ash-left)
646 (:args (number :scs (signed-reg unsigned-reg) :target result
647 :load-if (not (and (sc-is number signed-stack unsigned-stack)
648 (sc-is result signed-stack unsigned-stack)
649 (location= number result))))
650 (amount :scs (unsigned-reg) :target ecx))
651 (:arg-types (:or signed-num unsigned-num) positive-fixnum)
652 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
653 (:results (result :scs (signed-reg unsigned-reg) :from (:argument 0)
655 (and (sc-is number signed-stack unsigned-stack)
656 (sc-is result signed-stack unsigned-stack)
657 (location= number result)))))
658 (:result-types (:or signed-num unsigned-num))
664 ;; The result-type ensures us that this shift will not overflow.
665 (inst shl result :cl)))
667 (define-vop (fast-ash)
670 (:args (number :scs (signed-reg unsigned-reg) :target result)
671 (amount :scs (signed-reg) :target ecx))
672 (:arg-types (:or signed-num unsigned-num) signed-num)
673 (:results (result :scs (signed-reg unsigned-reg) :from (:argument 0)))
674 (:result-types (:or signed-num unsigned-num))
675 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
681 (inst jmp :ns positive)
688 (signed-reg (inst sar result :cl))
689 (unsigned-reg (inst shr result :cl)))
693 ;; The result-type ensures us that this shift will not overflow.
694 (inst shl result :cl)
698 ;;; Note: documentation for this function is wrong - rtfm
699 (define-vop (signed-byte-32-len)
700 (:translate integer-length)
701 (:note "inline (signed-byte 32) integer-length")
703 (:args (arg :scs (signed-reg) :target res))
704 (:arg-types signed-num)
705 (:results (res :scs (any-reg)))
706 (:result-types positive-fixnum)
722 (define-vop (unsigned-byte-32-count)
723 (:translate logcount)
724 (:note "inline (unsigned-byte 32) logcount")
726 (:args (arg :scs (unsigned-reg)))
727 (:arg-types unsigned-num)
728 (:results (result :scs (unsigned-reg)))
729 (:result-types positive-fixnum)
730 (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
734 (inst mov temp result)
736 (inst and result #x55555555)
737 (inst and temp #x55555555)
738 (inst add result temp)
740 (inst mov temp result)
742 (inst and result #x33333333)
743 (inst and temp #x33333333)
744 (inst add result temp)
746 (inst mov temp result)
748 (inst and result #x0f0f0f0f)
749 (inst and temp #x0f0f0f0f)
750 (inst add result temp)
752 (inst mov temp result)
754 (inst and result #x00ff00ff)
755 (inst and temp #x00ff00ff)
756 (inst add result temp)
758 (inst mov temp result)
760 (inst and result #x0000ffff)
761 (inst and temp #x0000ffff)
762 (inst add result temp)))
764 ;;;; binary conditional VOPs
766 (define-vop (fast-conditional)
771 (:policy :fast-safe))
773 (define-vop (fast-conditional/fixnum fast-conditional)
774 (:args (x :scs (any-reg)
775 :load-if (not (and (sc-is x control-stack)
777 (y :scs (any-reg control-stack)))
778 (:arg-types tagged-num tagged-num)
779 (:note "inline fixnum comparison"))
781 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
782 (:args (x :scs (any-reg control-stack)))
783 (:arg-types tagged-num (:constant (signed-byte 30)))
784 (:info target not-p y))
786 (define-vop (fast-conditional/signed fast-conditional)
787 (:args (x :scs (signed-reg)
788 :load-if (not (and (sc-is x signed-stack)
789 (sc-is y signed-reg))))
790 (y :scs (signed-reg signed-stack)))
791 (:arg-types signed-num signed-num)
792 (:note "inline (signed-byte 32) comparison"))
794 (define-vop (fast-conditional-c/signed fast-conditional/signed)
795 (:args (x :scs (signed-reg signed-stack)))
796 (:arg-types signed-num (:constant (signed-byte 32)))
797 (:info target not-p y))
799 (define-vop (fast-conditional/unsigned fast-conditional)
800 (:args (x :scs (unsigned-reg)
801 :load-if (not (and (sc-is x unsigned-stack)
802 (sc-is y unsigned-reg))))
803 (y :scs (unsigned-reg unsigned-stack)))
804 (:arg-types unsigned-num unsigned-num)
805 (:note "inline (unsigned-byte 32) comparison"))
807 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
808 (:args (x :scs (unsigned-reg unsigned-stack)))
809 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
810 (:info target not-p y))
813 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
816 #'(lambda (suffix cost signed)
817 `(define-vop (;; FIXME: These could be done more
818 ;; cleanly with SYMBOLICATE.
819 ,(intern (format nil "~:@(FAST-IF-~A~A~)"
822 (format nil "~:@(FAST-CONDITIONAL~A~)"
827 ,(if (eq suffix '-c/fixnum)
838 '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
840 '(t t t t nil nil)))))
842 (define-conditional-vop < :l :b :ge :ae)
843 (define-conditional-vop > :g :a :le :be))
845 (define-vop (fast-if-eql/signed fast-conditional/signed)
849 (inst jmp (if not-p :ne :e) target)))
851 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
854 (cond ((and (sc-is x signed-reg) (zerop y))
855 (inst test x x)) ; smaller instruction
858 (inst jmp (if not-p :ne :e) target)))
860 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
864 (inst jmp (if not-p :ne :e) target)))
866 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
869 (cond ((and (sc-is x unsigned-reg) (zerop y))
870 (inst test x x)) ; smaller instruction
873 (inst jmp (if not-p :ne :e) target)))
875 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
878 ;;; These versions specify a fixnum restriction on their first arg. We have
879 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
880 ;;; the first arg and a higher cost. The reason for doing this is to prevent
881 ;;; fixnum specific operations from being used on word integers, spuriously
882 ;;; consing the argument.
884 (define-vop (fast-eql/fixnum fast-conditional)
885 (:args (x :scs (any-reg)
886 :load-if (not (and (sc-is x control-stack)
888 (y :scs (any-reg control-stack)))
889 (:arg-types tagged-num tagged-num)
890 (:note "inline fixnum comparison")
894 (inst jmp (if not-p :ne :e) target)))
895 (define-vop (generic-eql/fixnum fast-eql/fixnum)
896 (:args (x :scs (any-reg descriptor-reg)
897 :load-if (not (and (sc-is x control-stack)
899 (y :scs (any-reg control-stack)))
900 (:arg-types * tagged-num)
903 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
904 (:args (x :scs (any-reg control-stack)))
905 (:arg-types tagged-num (:constant (signed-byte 30)))
906 (:info target not-p y)
909 (cond ((and (sc-is x any-reg) (zerop y))
910 (inst test x x)) ; smaller instruction
912 (inst cmp x (fixnumize y))))
913 (inst jmp (if not-p :ne :e) target)))
914 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
915 (:args (x :scs (any-reg descriptor-reg control-stack)))
916 (:arg-types * (:constant (signed-byte 30)))
919 ;;;; 32-bit logical operations
921 (define-vop (merge-bits)
922 (:translate merge-bits)
923 (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
924 (prev :scs (unsigned-reg) :target result)
925 (next :scs (unsigned-reg)))
926 (:arg-types tagged-num unsigned-num unsigned-num)
927 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
928 (:results (result :scs (unsigned-reg) :from (:argument 1)))
929 (:result-types unsigned-num)
934 (inst shrd result next :cl)))
936 (define-vop (32bit-logical)
937 (:args (x :scs (unsigned-reg) :target r
938 :load-if (not (and (sc-is x unsigned-stack)
939 (sc-is r unsigned-stack)
941 (y :scs (unsigned-reg)
942 :load-if (or (not (sc-is y unsigned-stack))
943 (and (sc-is x unsigned-stack)
944 (sc-is y unsigned-stack)
946 (:arg-types unsigned-num unsigned-num)
947 (:results (r :scs (unsigned-reg)
949 :load-if (not (and (sc-is x unsigned-stack)
950 (sc-is r unsigned-stack)
952 (:result-types unsigned-num)
953 (:policy :fast-safe))
955 (define-vop (32bit-logical-not)
956 (:translate 32bit-logical-not)
957 (:args (x :scs (unsigned-reg) :target r
958 :load-if (not (and (sc-is x unsigned-stack)
959 (sc-is r unsigned-stack)
961 (:arg-types unsigned-num)
962 (:results (r :scs (unsigned-reg)
963 :load-if (not (and (sc-is x unsigned-stack)
964 (sc-is r unsigned-stack)
966 (:result-types unsigned-num)
972 (define-vop (32bit-logical-and 32bit-logical)
973 (:translate 32bit-logical-and)
978 (def-source-transform 32bit-logical-nand (x y)
979 `(32bit-logical-not (32bit-logical-and ,x ,y)))
981 (define-vop (32bit-logical-or 32bit-logical)
982 (:translate 32bit-logical-or)
987 (def-source-transform 32bit-logical-nor (x y)
988 `(32bit-logical-not (32bit-logical-or ,x ,y)))
990 (define-vop (32bit-logical-xor 32bit-logical)
991 (:translate 32bit-logical-xor)
996 (def-source-transform 32bit-logical-eqv (x y)
997 `(32bit-logical-not (32bit-logical-xor ,x ,y)))
999 (def-source-transform 32bit-logical-orc1 (x y)
1000 `(32bit-logical-or (32bit-logical-not ,x) ,y))
1002 (def-source-transform 32bit-logical-orc2 (x y)
1003 `(32bit-logical-or ,x (32bit-logical-not ,y)))
1005 (def-source-transform 32bit-logical-andc1 (x y)
1006 `(32bit-logical-and (32bit-logical-not ,x) ,y))
1008 (def-source-transform 32bit-logical-andc2 (x y)
1009 `(32bit-logical-and ,x (32bit-logical-not ,y)))
1011 ;;; Only the lower 5 bits of the shift amount are significant.
1012 (define-vop (shift-towards-someplace)
1013 (:policy :fast-safe)
1014 (:args (num :scs (unsigned-reg) :target r)
1015 (amount :scs (signed-reg) :target ecx))
1016 (:arg-types unsigned-num tagged-num)
1017 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1018 (:results (r :scs (unsigned-reg) :from (:argument 0)))
1019 (:result-types unsigned-num))
1021 (define-vop (shift-towards-start shift-towards-someplace)
1022 (:translate shift-towards-start)
1023 (:note "SHIFT-TOWARDS-START")
1029 (define-vop (shift-towards-end shift-towards-someplace)
1030 (:translate shift-towards-end)
1031 (:note "SHIFT-TOWARDS-END")
1039 (define-vop (bignum-length get-header-data)
1040 (:translate sb!bignum::%bignum-length)
1041 (:policy :fast-safe))
1043 (define-vop (bignum-set-length set-header-data)
1044 (:translate sb!bignum::%bignum-set-length)
1045 (:policy :fast-safe))
1047 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-type
1048 (unsigned-reg) unsigned-num sb!bignum::%bignum-ref)
1050 (define-full-setter bignum-set * bignum-digits-offset other-pointer-type
1051 (unsigned-reg) unsigned-num sb!bignum::%bignum-set)
1053 (define-vop (digit-0-or-plus)
1054 (:translate sb!bignum::%digit-0-or-plusp)
1055 (:policy :fast-safe)
1056 (:args (digit :scs (unsigned-reg)))
1057 (:arg-types unsigned-num)
1059 (:info target not-p)
1061 (inst or digit digit)
1062 (inst jmp (if not-p :s :ns) target)))
1065 ;;; For add and sub with carry the sc of carry argument is any-reg so
1066 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1067 ;;; 4. This is easy to deal with and may save a fixnum-word
1069 (define-vop (add-w/carry)
1070 (:translate sb!bignum::%add-with-carry)
1071 (:policy :fast-safe)
1072 (:args (a :scs (unsigned-reg) :target result)
1073 (b :scs (unsigned-reg unsigned-stack) :to :eval)
1074 (c :scs (any-reg) :target temp))
1075 (:arg-types unsigned-num unsigned-num positive-fixnum)
1076 (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1077 (:results (result :scs (unsigned-reg) :from (:argument 0))
1078 (carry :scs (unsigned-reg)))
1079 (:result-types unsigned-num positive-fixnum)
1083 (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1086 (inst adc carry carry)))
1088 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1089 ;;; borrow and 0 for a borrow.
1090 (define-vop (sub-w/borrow)
1091 (:translate sb!bignum::%subtract-with-borrow)
1092 (:policy :fast-safe)
1093 (:args (a :scs (unsigned-reg) :to :eval :target result)
1094 (b :scs (unsigned-reg unsigned-stack) :to :result)
1095 (c :scs (any-reg control-stack)))
1096 (:arg-types unsigned-num unsigned-num positive-fixnum)
1097 (:results (result :scs (unsigned-reg) :from :eval)
1098 (borrow :scs (unsigned-reg)))
1099 (:result-types unsigned-num positive-fixnum)
1101 (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1105 (inst adc borrow borrow)
1106 (inst xor borrow 1)))
1109 (define-vop (bignum-mult-and-add-3-arg)
1110 (:translate sb!bignum::%multiply-and-add)
1111 (:policy :fast-safe)
1112 (:args (x :scs (unsigned-reg) :target eax)
1113 (y :scs (unsigned-reg unsigned-stack))
1114 (carry-in :scs (unsigned-reg unsigned-stack)))
1115 (:arg-types unsigned-num unsigned-num unsigned-num)
1116 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1117 :to (:result 1) :target lo) eax)
1118 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1119 :to (:result 0) :target hi) edx)
1120 (:results (hi :scs (unsigned-reg))
1121 (lo :scs (unsigned-reg)))
1122 (:result-types unsigned-num unsigned-num)
1126 (inst add eax carry-in)
1131 (define-vop (bignum-mult-and-add-4-arg)
1132 (:translate sb!bignum::%multiply-and-add)
1133 (:policy :fast-safe)
1134 (:args (x :scs (unsigned-reg) :target eax)
1135 (y :scs (unsigned-reg unsigned-stack))
1136 (prev :scs (unsigned-reg unsigned-stack))
1137 (carry-in :scs (unsigned-reg unsigned-stack)))
1138 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1139 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1140 :to (:result 1) :target lo) eax)
1141 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1142 :to (:result 0) :target hi) edx)
1143 (:results (hi :scs (unsigned-reg))
1144 (lo :scs (unsigned-reg)))
1145 (:result-types unsigned-num unsigned-num)
1151 (inst add eax carry-in)
1157 (define-vop (bignum-mult)
1158 (:translate sb!bignum::%multiply)
1159 (:policy :fast-safe)
1160 (:args (x :scs (unsigned-reg) :target eax)
1161 (y :scs (unsigned-reg unsigned-stack)))
1162 (:arg-types unsigned-num unsigned-num)
1163 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1164 :to (:result 1) :target lo) eax)
1165 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1166 :to (:result 0) :target hi) edx)
1167 (:results (hi :scs (unsigned-reg))
1168 (lo :scs (unsigned-reg)))
1169 (:result-types unsigned-num unsigned-num)
1176 (define-vop (bignum-lognot)
1177 (:translate sb!bignum::%lognot)
1178 (:policy :fast-safe)
1179 (:args (x :scs (unsigned-reg unsigned-stack) :target r))
1180 (:arg-types unsigned-num)
1181 (:results (r :scs (unsigned-reg)
1182 :load-if (not (location= x r))))
1183 (:result-types unsigned-num)
1188 (define-vop (fixnum-to-digit)
1189 (:translate sb!bignum::%fixnum-to-digit)
1190 (:policy :fast-safe)
1191 (:args (fixnum :scs (any-reg control-stack) :target digit))
1192 (:arg-types tagged-num)
1193 (:results (digit :scs (unsigned-reg)
1194 :load-if (not (and (sc-is fixnum control-stack)
1195 (sc-is digit unsigned-stack)
1196 (location= fixnum digit)))))
1197 (:result-types unsigned-num)
1200 (inst sar digit 2)))
1202 (define-vop (bignum-floor)
1203 (:translate sb!bignum::%floor)
1204 (:policy :fast-safe)
1205 (:args (div-high :scs (unsigned-reg) :target edx)
1206 (div-low :scs (unsigned-reg) :target eax)
1207 (divisor :scs (unsigned-reg unsigned-stack)))
1208 (:arg-types unsigned-num unsigned-num unsigned-num)
1209 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1210 :to (:result 0) :target quo) eax)
1211 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1212 :to (:result 1) :target rem) edx)
1213 (:results (quo :scs (unsigned-reg))
1214 (rem :scs (unsigned-reg)))
1215 (:result-types unsigned-num unsigned-num)
1219 (inst div eax divisor)
1223 (define-vop (signify-digit)
1224 (:translate sb!bignum::%fixnum-digit-with-correct-sign)
1225 (:policy :fast-safe)
1226 (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1227 (:arg-types unsigned-num)
1228 (:results (res :scs (any-reg signed-reg)
1229 :load-if (not (and (sc-is digit unsigned-stack)
1230 (sc-is res control-stack signed-stack)
1231 (location= digit res)))))
1232 (:result-types signed-num)
1235 (when (sc-is res any-reg control-stack)
1238 (define-vop (digit-ashr)
1239 (:translate sb!bignum::%ashr)
1240 (:policy :fast-safe)
1241 (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1242 (count :scs (unsigned-reg) :target ecx))
1243 (:arg-types unsigned-num positive-fixnum)
1244 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1245 (:results (result :scs (unsigned-reg) :from (:argument 0)
1246 :load-if (not (and (sc-is result unsigned-stack)
1247 (location= digit result)))))
1248 (:result-types unsigned-num)
1252 (inst sar result :cl)))
1254 (define-vop (digit-lshr digit-ashr)
1255 (:translate sb!bignum::%digit-logical-shift-right)
1259 (inst shr result :cl)))
1261 (define-vop (digit-ashl digit-ashr)
1262 (:translate sb!bignum::%ashl)
1266 (inst shl result :cl)))
1268 ;;;; static functions
1270 (define-static-function two-arg-/ (x y) :translate /)
1272 (define-static-function two-arg-gcd (x y) :translate gcd)
1273 (define-static-function two-arg-lcm (x y) :translate lcm)
1275 (define-static-function two-arg-and (x y) :translate logand)
1276 (define-static-function two-arg-ior (x y) :translate logior)
1277 (define-static-function two-arg-xor (x y) :translate logxor)
1280 ;;; Support for the Mersenne Twister, MT19937, random number generator
1281 ;;; due to Matsumoto and Nishimura.
1283 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
1284 ;;; 623-dimensionally equidistributed uniform pseudorandom number
1285 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
1286 ;;; 1997, to appear.
1289 ;;; 0-1: Constant matrix A. [0, #x9908b0df] (not used here)
1290 ;;; 2: Index; init. to 1.
1292 (defknown random-mt19937 ((simple-array (unsigned-byte 32) (*)))
1293 (unsigned-byte 32) ())
1294 (define-vop (random-mt19937)
1295 (:policy :fast-safe)
1296 (:translate random-mt19937)
1297 (:args (state :scs (descriptor-reg) :to :result))
1298 (:arg-types simple-array-unsigned-byte-32)
1299 (:temporary (:sc unsigned-reg :from (:eval 0) :to :result) k)
1300 (:temporary (:sc unsigned-reg :offset eax-offset
1301 :from (:eval 0) :to :result) tmp)
1302 (:results (y :scs (unsigned-reg) :from (:eval 0)))
1303 (:result-types unsigned-num)
1305 (inst mov k (make-ea :dword :base state
1306 :disp (- (* (+ 2 sb!vm:vector-data-offset)
1308 sb!vm:other-pointer-type)))
1310 (inst jmp :ne no-update)
1311 (inst mov tmp state) ; The state is passed in EAX.
1312 (inst call (make-fixup 'random-mt19937-update :assembly-routine))
1313 ;; Restore k, and set to 0.
1317 (inst mov y (make-ea :dword :base state :index k :scale 4
1318 :disp (- (* (+ 3 sb!vm:vector-data-offset)
1320 sb!vm:other-pointer-type)))
1323 (inst xor y (make-ea :dword :base state :index k :scale 4
1324 :disp (- (* (+ 3 sb!vm:vector-data-offset)
1326 sb!vm:other-pointer-type)))
1327 ;; y ^= (y << 7) & #x9d2c5680
1331 (inst mov (make-ea :dword :base state
1332 :disp (- (* (+ 2 sb!vm:vector-data-offset)
1334 sb!vm:other-pointer-type))
1336 (inst and tmp #x9d2c5680)
1338 ;; y ^= (y << 15) & #xefc60000
1341 (inst and tmp #xefc60000)