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)))
766 ;;;; binary conditional VOPs
768 (define-vop (fast-conditional)
773 (:policy :fast-safe))
775 (define-vop (fast-conditional/fixnum fast-conditional)
776 (:args (x :scs (any-reg)
777 :load-if (not (and (sc-is x control-stack)
779 (y :scs (any-reg control-stack)))
780 (:arg-types tagged-num tagged-num)
781 (:note "inline fixnum comparison"))
783 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
784 (:args (x :scs (any-reg control-stack)))
785 (:arg-types tagged-num (:constant (signed-byte 30)))
786 (:info target not-p y))
788 (define-vop (fast-conditional/signed fast-conditional)
789 (:args (x :scs (signed-reg)
790 :load-if (not (and (sc-is x signed-stack)
791 (sc-is y signed-reg))))
792 (y :scs (signed-reg signed-stack)))
793 (:arg-types signed-num signed-num)
794 (:note "inline (signed-byte 32) comparison"))
796 (define-vop (fast-conditional-c/signed fast-conditional/signed)
797 (:args (x :scs (signed-reg signed-stack)))
798 (:arg-types signed-num (:constant (signed-byte 32)))
799 (:info target not-p y))
801 (define-vop (fast-conditional/unsigned fast-conditional)
802 (:args (x :scs (unsigned-reg)
803 :load-if (not (and (sc-is x unsigned-stack)
804 (sc-is y unsigned-reg))))
805 (y :scs (unsigned-reg unsigned-stack)))
806 (:arg-types unsigned-num unsigned-num)
807 (:note "inline (unsigned-byte 32) comparison"))
809 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
810 (:args (x :scs (unsigned-reg unsigned-stack)))
811 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
812 (:info target not-p y))
815 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
818 #'(lambda (suffix cost signed)
819 `(define-vop (;; FIXME: These could be done more
820 ;; cleanly with SYMBOLICATE.
821 ,(intern (format nil "~:@(FAST-IF-~A~A~)"
824 (format nil "~:@(FAST-CONDITIONAL~A~)"
829 ,(if (eq suffix '-c/fixnum)
840 '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
842 '(t t t t nil nil)))))
844 (define-conditional-vop < :l :b :ge :ae)
845 (define-conditional-vop > :g :a :le :be))
847 (define-vop (fast-if-eql/signed fast-conditional/signed)
851 (inst jmp (if not-p :ne :e) target)))
853 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
856 (cond ((and (sc-is x signed-reg) (zerop y))
857 (inst test x x)) ; smaller instruction
860 (inst jmp (if not-p :ne :e) target)))
862 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
866 (inst jmp (if not-p :ne :e) target)))
868 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
871 (cond ((and (sc-is x unsigned-reg) (zerop y))
872 (inst test x x)) ; smaller instruction
875 (inst jmp (if not-p :ne :e) target)))
877 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
880 ;;; These versions specify a fixnum restriction on their first arg. We have
881 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
882 ;;; the first arg and a higher cost. The reason for doing this is to prevent
883 ;;; fixnum specific operations from being used on word integers, spuriously
884 ;;; consing the argument.
886 (define-vop (fast-eql/fixnum fast-conditional)
887 (:args (x :scs (any-reg)
888 :load-if (not (and (sc-is x control-stack)
890 (y :scs (any-reg control-stack)))
891 (:arg-types tagged-num tagged-num)
892 (:note "inline fixnum comparison")
896 (inst jmp (if not-p :ne :e) target)))
897 (define-vop (generic-eql/fixnum fast-eql/fixnum)
898 (:args (x :scs (any-reg descriptor-reg)
899 :load-if (not (and (sc-is x control-stack)
901 (y :scs (any-reg control-stack)))
902 (:arg-types * tagged-num)
905 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
906 (:args (x :scs (any-reg control-stack)))
907 (:arg-types tagged-num (:constant (signed-byte 30)))
908 (:info target not-p y)
911 (cond ((and (sc-is x any-reg) (zerop y))
912 (inst test x x)) ; smaller instruction
914 (inst cmp x (fixnumize y))))
915 (inst jmp (if not-p :ne :e) target)))
916 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
917 (:args (x :scs (any-reg descriptor-reg control-stack)))
918 (:arg-types * (:constant (signed-byte 30)))
921 ;;;; 32-bit logical operations
923 (define-vop (merge-bits)
924 (:translate merge-bits)
925 (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
926 (prev :scs (unsigned-reg) :target result)
927 (next :scs (unsigned-reg)))
928 (:arg-types tagged-num unsigned-num unsigned-num)
929 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
930 (:results (result :scs (unsigned-reg) :from (:argument 1)))
931 (:result-types unsigned-num)
936 (inst shrd result next :cl)))
938 (define-vop (32bit-logical)
939 (:args (x :scs (unsigned-reg) :target r
940 :load-if (not (and (sc-is x unsigned-stack)
941 (sc-is r unsigned-stack)
943 (y :scs (unsigned-reg)
944 :load-if (or (not (sc-is y unsigned-stack))
945 (and (sc-is x unsigned-stack)
946 (sc-is y unsigned-stack)
948 (:arg-types unsigned-num unsigned-num)
949 (:results (r :scs (unsigned-reg)
951 :load-if (not (and (sc-is x unsigned-stack)
952 (sc-is r unsigned-stack)
954 (:result-types unsigned-num)
955 (:policy :fast-safe))
957 (define-vop (32bit-logical-not)
958 (:translate 32bit-logical-not)
959 (:args (x :scs (unsigned-reg) :target r
960 :load-if (not (and (sc-is x unsigned-stack)
961 (sc-is r unsigned-stack)
963 (:arg-types unsigned-num)
964 (:results (r :scs (unsigned-reg)
965 :load-if (not (and (sc-is x unsigned-stack)
966 (sc-is r unsigned-stack)
968 (:result-types unsigned-num)
974 (define-vop (32bit-logical-and 32bit-logical)
975 (:translate 32bit-logical-and)
980 (def-source-transform 32bit-logical-nand (x y)
981 `(32bit-logical-not (32bit-logical-and ,x ,y)))
983 (define-vop (32bit-logical-or 32bit-logical)
984 (:translate 32bit-logical-or)
989 (def-source-transform 32bit-logical-nor (x y)
990 `(32bit-logical-not (32bit-logical-or ,x ,y)))
992 (define-vop (32bit-logical-xor 32bit-logical)
993 (:translate 32bit-logical-xor)
998 (def-source-transform 32bit-logical-eqv (x y)
999 `(32bit-logical-not (32bit-logical-xor ,x ,y)))
1001 (def-source-transform 32bit-logical-orc1 (x y)
1002 `(32bit-logical-or (32bit-logical-not ,x) ,y))
1004 (def-source-transform 32bit-logical-orc2 (x y)
1005 `(32bit-logical-or ,x (32bit-logical-not ,y)))
1007 (def-source-transform 32bit-logical-andc1 (x y)
1008 `(32bit-logical-and (32bit-logical-not ,x) ,y))
1010 (def-source-transform 32bit-logical-andc2 (x y)
1011 `(32bit-logical-and ,x (32bit-logical-not ,y)))
1013 ;;; Only the lower 5 bits of the shift amount are significant.
1014 (define-vop (shift-towards-someplace)
1015 (:policy :fast-safe)
1016 (:args (num :scs (unsigned-reg) :target r)
1017 (amount :scs (signed-reg) :target ecx))
1018 (:arg-types unsigned-num tagged-num)
1019 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1020 (:results (r :scs (unsigned-reg) :from (:argument 0)))
1021 (:result-types unsigned-num))
1023 (define-vop (shift-towards-start shift-towards-someplace)
1024 (:translate shift-towards-start)
1025 (:note "SHIFT-TOWARDS-START")
1031 (define-vop (shift-towards-end shift-towards-someplace)
1032 (:translate shift-towards-end)
1033 (:note "SHIFT-TOWARDS-END")
1041 (define-vop (bignum-length get-header-data)
1042 (:translate sb!bignum::%bignum-length)
1043 (:policy :fast-safe))
1045 (define-vop (bignum-set-length set-header-data)
1046 (:translate sb!bignum::%bignum-set-length)
1047 (:policy :fast-safe))
1049 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-type
1050 (unsigned-reg) unsigned-num sb!bignum::%bignum-ref)
1052 (define-full-setter bignum-set * bignum-digits-offset other-pointer-type
1053 (unsigned-reg) unsigned-num sb!bignum::%bignum-set)
1055 (define-vop (digit-0-or-plus)
1056 (:translate sb!bignum::%digit-0-or-plusp)
1057 (:policy :fast-safe)
1058 (:args (digit :scs (unsigned-reg)))
1059 (:arg-types unsigned-num)
1061 (:info target not-p)
1063 (inst or digit digit)
1064 (inst jmp (if not-p :s :ns) target)))
1067 ;;; For add and sub with carry the sc of carry argument is any-reg so
1068 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1069 ;;; 4. This is easy to deal with and may save a fixnum-word
1071 (define-vop (add-w/carry)
1072 (:translate sb!bignum::%add-with-carry)
1073 (:policy :fast-safe)
1074 (:args (a :scs (unsigned-reg) :target result)
1075 (b :scs (unsigned-reg unsigned-stack) :to :eval)
1076 (c :scs (any-reg) :target temp))
1077 (:arg-types unsigned-num unsigned-num positive-fixnum)
1078 (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1079 (:results (result :scs (unsigned-reg) :from (:argument 0))
1080 (carry :scs (unsigned-reg)))
1081 (:result-types unsigned-num positive-fixnum)
1085 (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1088 (inst adc carry carry)))
1090 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1091 ;;; borrow and 0 for a borrow.
1092 (define-vop (sub-w/borrow)
1093 (:translate sb!bignum::%subtract-with-borrow)
1094 (:policy :fast-safe)
1095 (:args (a :scs (unsigned-reg) :to :eval :target result)
1096 (b :scs (unsigned-reg unsigned-stack) :to :result)
1097 (c :scs (any-reg control-stack)))
1098 (:arg-types unsigned-num unsigned-num positive-fixnum)
1099 (:results (result :scs (unsigned-reg) :from :eval)
1100 (borrow :scs (unsigned-reg)))
1101 (:result-types unsigned-num positive-fixnum)
1103 (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1107 (inst adc borrow borrow)
1108 (inst xor borrow 1)))
1111 (define-vop (bignum-mult-and-add-3-arg)
1112 (:translate sb!bignum::%multiply-and-add)
1113 (:policy :fast-safe)
1114 (:args (x :scs (unsigned-reg) :target eax)
1115 (y :scs (unsigned-reg unsigned-stack))
1116 (carry-in :scs (unsigned-reg unsigned-stack)))
1117 (:arg-types unsigned-num unsigned-num unsigned-num)
1118 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1119 :to (:result 1) :target lo) eax)
1120 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1121 :to (:result 0) :target hi) edx)
1122 (:results (hi :scs (unsigned-reg))
1123 (lo :scs (unsigned-reg)))
1124 (:result-types unsigned-num unsigned-num)
1128 (inst add eax carry-in)
1133 (define-vop (bignum-mult-and-add-4-arg)
1134 (:translate sb!bignum::%multiply-and-add)
1135 (:policy :fast-safe)
1136 (:args (x :scs (unsigned-reg) :target eax)
1137 (y :scs (unsigned-reg unsigned-stack))
1138 (prev :scs (unsigned-reg unsigned-stack))
1139 (carry-in :scs (unsigned-reg unsigned-stack)))
1140 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1141 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1142 :to (:result 1) :target lo) eax)
1143 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1144 :to (:result 0) :target hi) edx)
1145 (:results (hi :scs (unsigned-reg))
1146 (lo :scs (unsigned-reg)))
1147 (:result-types unsigned-num unsigned-num)
1153 (inst add eax carry-in)
1159 (define-vop (bignum-mult)
1160 (:translate sb!bignum::%multiply)
1161 (:policy :fast-safe)
1162 (:args (x :scs (unsigned-reg) :target eax)
1163 (y :scs (unsigned-reg unsigned-stack)))
1164 (:arg-types unsigned-num unsigned-num)
1165 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1166 :to (:result 1) :target lo) eax)
1167 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1168 :to (:result 0) :target hi) edx)
1169 (:results (hi :scs (unsigned-reg))
1170 (lo :scs (unsigned-reg)))
1171 (:result-types unsigned-num unsigned-num)
1178 (define-vop (bignum-lognot)
1179 (:translate sb!bignum::%lognot)
1180 (:policy :fast-safe)
1181 (:args (x :scs (unsigned-reg unsigned-stack) :target r))
1182 (:arg-types unsigned-num)
1183 (:results (r :scs (unsigned-reg)
1184 :load-if (not (location= x r))))
1185 (:result-types unsigned-num)
1190 (define-vop (fixnum-to-digit)
1191 (:translate sb!bignum::%fixnum-to-digit)
1192 (:policy :fast-safe)
1193 (:args (fixnum :scs (any-reg control-stack) :target digit))
1194 (:arg-types tagged-num)
1195 (:results (digit :scs (unsigned-reg)
1196 :load-if (not (and (sc-is fixnum control-stack)
1197 (sc-is digit unsigned-stack)
1198 (location= fixnum digit)))))
1199 (:result-types unsigned-num)
1202 (inst sar digit 2)))
1204 (define-vop (bignum-floor)
1205 (:translate sb!bignum::%floor)
1206 (:policy :fast-safe)
1207 (:args (div-high :scs (unsigned-reg) :target edx)
1208 (div-low :scs (unsigned-reg) :target eax)
1209 (divisor :scs (unsigned-reg unsigned-stack)))
1210 (:arg-types unsigned-num unsigned-num unsigned-num)
1211 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1212 :to (:result 0) :target quo) eax)
1213 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1214 :to (:result 1) :target rem) edx)
1215 (:results (quo :scs (unsigned-reg))
1216 (rem :scs (unsigned-reg)))
1217 (:result-types unsigned-num unsigned-num)
1221 (inst div eax divisor)
1225 (define-vop (signify-digit)
1226 (:translate sb!bignum::%fixnum-digit-with-correct-sign)
1227 (:policy :fast-safe)
1228 (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1229 (:arg-types unsigned-num)
1230 (:results (res :scs (any-reg signed-reg)
1231 :load-if (not (and (sc-is digit unsigned-stack)
1232 (sc-is res control-stack signed-stack)
1233 (location= digit res)))))
1234 (:result-types signed-num)
1237 (when (sc-is res any-reg control-stack)
1240 (define-vop (digit-ashr)
1241 (:translate sb!bignum::%ashr)
1242 (:policy :fast-safe)
1243 (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1244 (count :scs (unsigned-reg) :target ecx))
1245 (:arg-types unsigned-num positive-fixnum)
1246 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1247 (:results (result :scs (unsigned-reg) :from (:argument 0)
1248 :load-if (not (and (sc-is result unsigned-stack)
1249 (location= digit result)))))
1250 (:result-types unsigned-num)
1254 (inst sar result :cl)))
1256 (define-vop (digit-lshr digit-ashr)
1257 (:translate sb!bignum::%digit-logical-shift-right)
1261 (inst shr result :cl)))
1263 (define-vop (digit-ashl digit-ashr)
1264 (:translate sb!bignum::%ashl)
1268 (inst shl result :cl)))
1270 ;;;; static functions
1272 (define-static-function two-arg-/ (x y) :translate /)
1274 (define-static-function two-arg-gcd (x y) :translate gcd)
1275 (define-static-function two-arg-lcm (x y) :translate lcm)
1277 (define-static-function two-arg-and (x y) :translate logand)
1278 (define-static-function two-arg-ior (x y) :translate logior)
1279 (define-static-function two-arg-xor (x y) :translate logxor)
1282 ;;; Support for the Mersenne Twister, MT19937, random number generator
1283 ;;; due to Matsumoto and Nishimura.
1285 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
1286 ;;; 623-dimensionally equidistributed uniform pseudorandom number
1287 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
1288 ;;; 1997, to appear.
1291 ;;; 0-1: Constant matrix A. [0, #x9908b0df] (not used here)
1292 ;;; 2: Index; init. to 1.
1294 (defknown random-mt19937 ((simple-array (unsigned-byte 32) (*)))
1295 (unsigned-byte 32) ())
1296 (define-vop (random-mt19937)
1297 (:policy :fast-safe)
1298 (:translate random-mt19937)
1299 (:args (state :scs (descriptor-reg) :to :result))
1300 (:arg-types simple-array-unsigned-byte-32)
1301 (:temporary (:sc unsigned-reg :from (:eval 0) :to :result) k)
1302 (:temporary (:sc unsigned-reg :offset eax-offset
1303 :from (:eval 0) :to :result) tmp)
1304 (:results (y :scs (unsigned-reg) :from (:eval 0)))
1305 (:result-types unsigned-num)
1307 (inst mov k (make-ea :dword :base state
1308 :disp (- (* (+ 2 sb!vm:vector-data-offset)
1310 sb!vm:other-pointer-type)))
1312 (inst jmp :ne no-update)
1313 (inst mov tmp state) ; The state is passed in EAX.
1314 (inst call (make-fixup 'random-mt19937-update :assembly-routine))
1315 ;; Restore k, and set to 0.
1319 (inst mov y (make-ea :dword :base state :index k :scale 4
1320 :disp (- (* (+ 3 sb!vm:vector-data-offset)
1322 sb!vm:other-pointer-type)))
1325 (inst xor y (make-ea :dword :base state :index k :scale 4
1326 :disp (- (* (+ 3 sb!vm:vector-data-offset)
1328 sb!vm:other-pointer-type)))
1329 ;; y ^= (y << 7) & #x9d2c5680
1333 (inst mov (make-ea :dword :base state
1334 :disp (- (* (+ 2 sb!vm:vector-data-offset)
1336 sb!vm:other-pointer-type))
1338 (inst and tmp #x9d2c5680)
1340 ;; y ^= (y << 15) & #xefc60000
1343 (inst and tmp #xefc60000)