1 ;;;; the VM definition arithmetic VOPs for MIPS
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.
14 ;;;; Unary operations.
16 (define-vop (fixnum-unop)
17 (:args (x :scs (any-reg)))
18 (:results (res :scs (any-reg)))
19 (:note "inline fixnum arithmetic")
20 (:arg-types tagged-num)
21 (:result-types tagged-num)
24 (define-vop (signed-unop)
25 (:args (x :scs (signed-reg)))
26 (:results (res :scs (signed-reg)))
27 (:note "inline (signed-byte 32) arithmetic")
28 (:arg-types signed-num)
29 (:result-types signed-num)
32 (define-vop (fast-negate/fixnum fixnum-unop)
35 (inst subu res zero-tn x)))
37 (define-vop (fast-negate/signed signed-unop)
40 (inst subu res zero-tn x)))
42 (define-vop (fast-lognot/fixnum fixnum-unop)
43 (:temporary (:scs (any-reg) :type fixnum :to (:result 0))
47 (inst li temp (fixnumize -1))
48 (inst xor res x temp)))
50 (define-vop (fast-lognot/signed signed-unop)
53 (inst nor res x zero-tn)))
57 ;;;; Binary fixnum operations.
59 ;;; Assume that any constant operand is the second arg...
61 (define-vop (fast-fixnum-binop)
62 (:args (x :target r :scs (any-reg))
63 (y :target r :scs (any-reg)))
64 (:arg-types tagged-num tagged-num)
65 (:results (r :scs (any-reg)))
66 (:result-types tagged-num)
67 (:note "inline fixnum arithmetic")
72 (define-vop (fast-unsigned-binop)
73 (:args (x :target r :scs (unsigned-reg))
74 (y :target r :scs (unsigned-reg)))
75 (:arg-types unsigned-num unsigned-num)
76 (:results (r :scs (unsigned-reg)))
77 (:result-types unsigned-num)
78 (:note "inline (unsigned-byte 32) arithmetic")
83 (define-vop (fast-signed-binop)
84 (:args (x :target r :scs (signed-reg))
85 (y :target r :scs (signed-reg)))
86 (:arg-types signed-num signed-num)
87 (:results (r :scs (signed-reg)))
88 (:result-types signed-num)
89 (:note "inline (signed-byte 32) arithmetic")
94 (define-vop (fast-fixnum-c-binop fast-fixnum-binop)
95 (:args (x :target r :scs (any-reg)))
97 (:arg-types tagged-num (:constant integer)))
99 (define-vop (fast-signed-c-binop fast-signed-binop)
100 (:args (x :target r :scs (signed-reg)))
102 (:arg-types tagged-num (:constant integer)))
104 (define-vop (fast-unsigned-c-binop fast-unsigned-binop)
105 (:args (x :target r :scs (unsigned-reg)))
107 (:arg-types tagged-num (:constant integer)))
109 (defmacro define-binop (translate cost untagged-cost op
110 tagged-type untagged-type)
112 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
114 (:args (x :target r :scs (any-reg))
115 (y :target r :scs (any-reg)))
116 (:translate ,translate)
117 (:generator ,(1+ cost)
119 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
121 (:args (x :target r :scs (signed-reg))
122 (y :target r :scs (signed-reg)))
123 (:translate ,translate)
124 (:generator ,(1+ untagged-cost)
126 (define-vop (,(symbolicate "FAST-" translate "/UNSIGNED=>UNSIGNED")
128 (:args (x :target r :scs (unsigned-reg))
129 (y :target r :scs (unsigned-reg)))
130 (:translate ,translate)
131 (:generator ,(1+ untagged-cost)
134 `((define-vop (,(symbolicate "FAST-" translate "-C/FIXNUM=>FIXNUM")
136 (:arg-types tagged-num (:constant ,tagged-type))
137 (:translate ,translate)
139 (inst ,op r x (fixnumize y))))))
140 ,@(when untagged-type
141 `((define-vop (,(symbolicate "FAST-" translate "-C/SIGNED=>SIGNED")
143 (:arg-types signed-num (:constant ,untagged-type))
144 (:translate ,translate)
145 (:generator ,untagged-cost
147 (define-vop (,(symbolicate "FAST-" translate
148 "-C/UNSIGNED=>UNSIGNED")
149 fast-unsigned-c-binop)
150 (:arg-types unsigned-num (:constant ,untagged-type))
151 (:translate ,translate)
152 (:generator ,untagged-cost
153 (inst ,op r x y)))))))
155 (define-binop + 1 5 addu (signed-byte 14) (signed-byte 16))
156 (define-binop - 1 5 subu
157 (integer #.(- (1- (ash 1 14))) #.(ash 1 14))
158 (integer #.(- (1- (ash 1 16))) #.(ash 1 16)))
159 (define-binop logior 1 3 or (unsigned-byte 14) (unsigned-byte 16))
160 (define-binop logand 1 3 and (unsigned-byte 14) (unsigned-byte 16))
161 (define-binop logxor 1 3 xor (unsigned-byte 14) (unsigned-byte 16))
163 ;;; KLUDGE: no FIXNUM VOP for LOGNOR, because there's no efficient way
164 ;;; of restoring the tag bits. (No -C/ VOPs for LOGNOR because the
165 ;;; NOR instruction doesn't take immediate args). -- CSR, 2003-09-11
166 (define-vop (fast-lognor/signed=>signed fast-signed-binop)
168 (:args (x :target r :scs (signed-reg))
169 (y :target r :scs (signed-reg)))
172 (define-vop (fast-lognor/unsigned=>unsigned fast-unsigned-binop)
174 (:args (x :target r :scs (unsigned-reg))
175 (y :target r :scs (unsigned-reg)))
179 ;;; Special case fixnum + and - that trap on overflow. Useful when we don't
180 ;;; know that the result is going to be a fixnum.
183 (define-vop (fast-+/fixnum fast-+/fixnum=>fixnum)
184 (:results (r :scs (any-reg descriptor-reg)))
185 (:result-types (:or signed-num unsigned-num))
190 (define-vop (fast-+-c/fixnum fast-+-c/fixnum=>fixnum)
191 (:results (r :scs (any-reg descriptor-reg)))
192 (:result-types (:or signed-num unsigned-num))
195 (inst add r x (fixnumize y))))
197 (define-vop (fast--/fixnum fast--/fixnum=>fixnum)
198 (:results (r :scs (any-reg descriptor-reg)))
199 (:result-types (:or signed-num unsigned-num))
204 (define-vop (fast---c/fixnum fast---c/fixnum=>fixnum)
205 (:results (r :scs (any-reg descriptor-reg)))
206 (:result-types (:or signed-num unsigned-num))
209 (inst sub r x (fixnumize y))))
210 ) ; bogus trap-to-c-land +/-
214 (define-vop (fast-ash/unsigned=>unsigned)
216 (:args (number :scs (unsigned-reg) :to :save)
217 (amount :scs (signed-reg) :to :save))
218 (:arg-types unsigned-num signed-num)
219 (:results (result :scs (unsigned-reg)))
220 (:result-types unsigned-num)
223 (:temporary (:sc non-descriptor-reg) ndesc)
224 (:temporary (:sc non-descriptor-reg :to :eval) temp)
226 (inst bgez amount positive)
227 (inst subu ndesc zero-tn amount)
228 (inst slt temp ndesc 32)
229 (inst bne temp zero-tn done)
230 (inst srl result number ndesc)
232 (inst move result zero-tn)
235 ;; The result-type assures us that this shift will not overflow.
236 (inst sll result number amount)
240 (define-vop (fast-ash/signed=>signed)
242 (:args (number :scs (signed-reg) :to :save)
243 (amount :scs (signed-reg)))
244 (:arg-types signed-num signed-num)
245 (:results (result :scs (signed-reg)))
246 (:result-types signed-num)
249 (:temporary (:sc non-descriptor-reg) ndesc)
250 (:temporary (:sc non-descriptor-reg :to :eval) temp)
252 (inst bgez amount positive)
253 (inst subu ndesc zero-tn amount)
254 (inst slt temp ndesc 31)
255 (inst bne temp zero-tn done)
256 (inst sra result number ndesc)
258 (inst sra result number 31)
261 ;; The result-type assures us that this shift will not overflow.
262 (inst sll result number amount)
267 (define-vop (fast-ash-c/unsigned=>unsigned)
271 (:args (number :scs (unsigned-reg)))
273 (:arg-types unsigned-num (:constant integer))
274 (:results (result :scs (unsigned-reg)))
275 (:result-types unsigned-num)
278 ((< count -31) (move result zero-tn))
279 ((< count 0) (inst srl result number (min (- count) 31)))
280 ((> count 0) (inst sll result number (min count 31)))
281 (t (bug "identity ASH not transformed away")))))
283 (define-vop (fast-ash-c/signed=>signed)
287 (:args (number :scs (signed-reg)))
289 (:arg-types signed-num (:constant integer))
290 (:results (result :scs (signed-reg)))
291 (:result-types signed-num)
294 ((< count 0) (inst sra result number (min (- count) 31)))
295 ((> count 0) (inst sll result number (min count 31)))
296 (t (bug "identity ASH not transformed away")))))
298 (define-vop (signed-byte-32-len)
299 (:translate integer-length)
300 (:note "inline (signed-byte 32) integer-length")
302 (:args (arg :scs (signed-reg) :target shift))
303 (:arg-types signed-num)
304 (:results (res :scs (any-reg)))
305 (:result-types positive-fixnum)
306 (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) shift)
308 (let ((loop (gen-label))
311 (inst bgez shift test)
314 (inst nor shift shift)
317 (inst add res (fixnumize 1))
320 (inst bne shift loop)
321 (inst srl shift 1))))
323 (define-vop (unsigned-byte-32-count)
324 (:translate logcount)
325 (:note "inline (unsigned-byte 32) logcount")
327 (:args (arg :scs (unsigned-reg) :target num))
328 (:arg-types unsigned-num)
329 (:results (res :scs (unsigned-reg)))
330 (:result-types positive-fixnum)
331 (:temporary (:scs (non-descriptor-reg) :from (:argument 0) :to (:result 0)
333 (:temporary (:scs (non-descriptor-reg)) mask temp)
335 (inst li mask #x55555555)
336 (inst srl temp arg 1)
337 (inst and num arg mask)
340 (inst li mask #x33333333)
341 (inst srl temp num 2)
345 (inst li mask #x0f0f0f0f)
346 (inst srl temp num 4)
350 (inst li mask #x00ff00ff)
351 (inst srl temp num 8)
355 (inst li mask #x0000ffff)
356 (inst srl temp num 16)
359 (inst addu res num temp)))
362 ;;; Multiply and Divide.
364 (define-vop (fast-*/fixnum=>fixnum fast-fixnum-binop)
365 (:temporary (:scs (non-descriptor-reg)) temp)
372 (define-vop (fast-*/signed=>signed fast-signed-binop)
378 (define-vop (fast-*/unsigned=>unsigned fast-unsigned-binop)
386 (define-vop (fast-truncate/fixnum fast-fixnum-binop)
387 (:translate truncate)
388 (:results (q :scs (any-reg))
390 (:result-types tagged-num tagged-num)
391 (:temporary (:scs (non-descriptor-reg) :to :eval) temp)
393 (:save-p :compute-only)
395 (let ((zero (generate-error-code vop division-by-zero-error x y)))
396 (inst beq y zero-tn zero))
403 (define-vop (fast-truncate/unsigned fast-unsigned-binop)
404 (:translate truncate)
405 (:results (q :scs (unsigned-reg))
406 (r :scs (unsigned-reg)))
407 (:result-types unsigned-num unsigned-num)
409 (:save-p :compute-only)
411 (let ((zero (generate-error-code vop division-by-zero-error x y)))
412 (inst beq y zero-tn zero))
418 (define-vop (fast-truncate/signed fast-signed-binop)
419 (:translate truncate)
420 (:results (q :scs (signed-reg))
421 (r :scs (signed-reg)))
422 (:result-types signed-num signed-num)
424 (:save-p :compute-only)
426 (let ((zero (generate-error-code vop division-by-zero-error x y)))
427 (inst beq y zero-tn zero))
435 ;;;; Binary conditional VOPs:
437 (define-vop (fast-conditional)
442 (:temporary (:scs (non-descriptor-reg)) temp)
443 (:policy :fast-safe))
445 (define-vop (fast-conditional/fixnum fast-conditional)
446 (:args (x :scs (any-reg))
448 (:arg-types tagged-num tagged-num)
449 (:note "inline fixnum comparison"))
451 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
452 (:args (x :scs (any-reg)))
453 (:arg-types tagged-num (:constant (signed-byte-with-a-bite-out 14 4)))
454 (:info target not-p y))
456 (define-vop (fast-conditional/signed fast-conditional)
457 (:args (x :scs (signed-reg))
458 (y :scs (signed-reg)))
459 (:arg-types signed-num signed-num)
460 (:note "inline (signed-byte 32) comparison"))
462 (define-vop (fast-conditional-c/signed fast-conditional/signed)
463 (:args (x :scs (signed-reg)))
464 (:arg-types signed-num (:constant (signed-byte-with-a-bite-out 16 1)))
465 (:info target not-p y))
467 (define-vop (fast-conditional/unsigned fast-conditional)
468 (:args (x :scs (unsigned-reg))
469 (y :scs (unsigned-reg)))
470 (:arg-types unsigned-num unsigned-num)
471 (:note "inline (unsigned-byte 32) comparison"))
473 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
474 (:args (x :scs (unsigned-reg)))
475 (:arg-types unsigned-num (:constant (and (signed-byte-with-a-bite-out 16 1)
477 (:info target not-p y))
480 (defmacro define-conditional-vop (translate &rest generator)
482 ,@(mapcar #'(lambda (suffix cost signed)
483 (unless (and (member suffix '(/fixnum -c/fixnum))
485 `(define-vop (,(intern (format nil "~:@(FAST-IF-~A~A~)"
488 (format nil "~:@(FAST-CONDITIONAL~A~)"
490 (:translate ,translate)
492 (let* ((signed ,signed)
493 (-c/fixnum ,(eq suffix '-c/fixnum))
494 (y (if -c/fixnum (fixnumize y) y)))
495 (declare (ignorable signed -c/fixnum y))
497 '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
499 '(t t t t nil nil))))
501 (define-conditional-vop <
502 (cond ((and signed (eql y 0))
505 (inst bltz x target)))
509 (inst sltu temp x y))
511 (inst beq temp zero-tn target)
512 (inst bne temp zero-tn target))))
515 (define-conditional-vop >
516 (cond ((and signed (eql y 0))
519 (inst bgtz x target)))
521 (let ((y (+ y (if -c/fixnum (fixnumize 1) 1))))
524 (inst sltu temp x y))
526 (inst bne temp zero-tn target)
527 (inst beq temp zero-tn target))))
531 (inst sltu temp y x))
533 (inst beq temp zero-tn target)
534 (inst bne temp zero-tn target))))
537 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
540 (define-conditional-vop eql
541 (declare (ignore signed))
546 (inst bne x y target)
547 (inst beq x y target))
550 ;;; These versions specify a fixnum restriction on their first arg. We have
551 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
552 ;;; the first arg and a higher cost. The reason for doing this is to prevent
553 ;;; fixnum specific operations from being used on word integers, spuriously
554 ;;; consing the argument.
556 (define-vop (fast-eql/fixnum fast-conditional)
557 (:args (x :scs (any-reg))
559 (:arg-types tagged-num tagged-num)
560 (:note "inline fixnum comparison")
565 (inst bne x y target)
566 (inst beq x y target))
569 (define-vop (generic-eql/fixnum fast-eql/fixnum)
570 (:args (x :scs (any-reg descriptor-reg))
572 (:arg-types * tagged-num)
575 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
576 (:args (x :scs (any-reg)))
577 (:arg-types tagged-num (:constant (signed-byte 14)))
578 (:info target not-p y)
581 (let ((y (cond ((eql y 0) zero-tn)
583 (inst li temp (fixnumize y))
586 (inst bne x y target)
587 (inst beq x y target))
590 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
591 (:args (x :scs (any-reg descriptor-reg)))
592 (:arg-types * (:constant (signed-byte 14)))
596 ;;;; 32-bit logical operations
598 (define-vop (merge-bits)
599 (:translate merge-bits)
600 (:args (shift :scs (signed-reg unsigned-reg))
601 (prev :scs (unsigned-reg))
602 (next :scs (unsigned-reg)))
603 (:arg-types tagged-num unsigned-num unsigned-num)
604 (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
605 (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
606 (:results (result :scs (unsigned-reg)))
607 (:result-types unsigned-num)
610 (let ((done (gen-label)))
611 (inst beq shift done)
612 (inst srl res next shift)
613 (inst subu temp zero-tn shift)
614 (inst sll temp prev temp)
615 (inst or res res temp)
619 (define-vop (shift-towards-someplace)
621 (:args (num :scs (unsigned-reg))
622 (amount :scs (signed-reg)))
623 (:arg-types unsigned-num tagged-num)
624 (:results (r :scs (unsigned-reg)))
625 (:result-types unsigned-num))
627 (define-vop (shift-towards-start shift-towards-someplace)
628 (:translate shift-towards-start)
629 (:note "SHIFT-TOWARDS-START")
631 (ecase *backend-byte-order*
633 (inst sll r num amount))
635 (inst srl r num amount)))))
637 (define-vop (shift-towards-end shift-towards-someplace)
638 (:translate shift-towards-end)
639 (:note "SHIFT-TOWARDS-END")
641 (ecase *backend-byte-order*
643 (inst srl r num amount))
645 (inst sll r num amount)))))
647 ;;;; Modular arithmetic
648 (define-modular-fun +-mod32 (x y) + 32)
649 (define-vop (fast-+-mod32/unsigned=>unsigned fast-+/unsigned=>unsigned)
650 (:translate +-mod32))
651 (define-vop (fast-+-mod32-c/unsigned=>unsigned fast-+-c/unsigned=>unsigned)
652 (:translate +-mod32))
653 (define-modular-fun --mod32 (x y) - 32)
654 (define-vop (fast---mod32/unsigned=>unsigned fast--/unsigned=>unsigned)
655 (:translate --mod32))
656 (define-vop (fast---mod32-c/unsigned=>unsigned fast---c/unsigned=>unsigned)
657 (:translate --mod32))
659 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
660 fast-ash-c/unsigned=>unsigned)
661 (:translate ash-left-mod32))
663 ;;; logical operations
664 (define-modular-fun lognot-mod32 (x) lognot 32)
665 (define-vop (lognot-mod32/unsigned=>unsigned)
666 (:translate lognot-mod32)
667 (:args (x :scs (unsigned-reg)))
668 (:arg-types unsigned-num)
669 (:results (r :scs (unsigned-reg)))
670 (:result-types unsigned-num)
673 (inst nor r x zero-tn)))
675 (define-modular-fun logxor-mod32 (x y) logxor 32)
676 (define-vop (fast-logxor-mod32/unsigned=>unsigned
677 fast-logxor/unsigned=>unsigned)
678 (:translate logxor-mod32))
679 (define-vop (fast-logxor-mod32-c/unsigned=>unsigned
680 fast-logxor-c/unsigned=>unsigned)
681 (:translate logxor-mod32))
683 (define-modular-fun lognor-mod32 (x y) lognor 32)
684 (define-vop (fast-lognor-mod32/unsigned=>unsigned
685 fast-lognor/unsigned=>unsigned)
686 (:translate lognor-mod32))
688 (define-source-transform logeqv (&rest args)
689 (if (oddp (length args))
691 `(lognot (logxor ,@args))))
692 (define-source-transform logandc1 (x y)
693 `(logand (lognot ,x) ,y))
694 (define-source-transform logandc2 (x y)
695 `(logand ,x (lognot ,y)))
696 (define-source-transform logorc1 (x y)
697 `(logior (lognot ,x) ,y))
698 (define-source-transform logorc2 (x y)
699 `(logior ,x (lognot ,y)))
700 (define-source-transform lognand (x y)
701 `(lognot (logand ,x ,y)))
704 (define-vop (bignum-length get-header-data)
705 (:translate sb!bignum:%bignum-length)
706 (:policy :fast-safe))
708 (define-vop (bignum-set-length set-header-data)
709 (:translate sb!bignum:%bignum-set-length)
710 (:policy :fast-safe))
712 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
713 (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
715 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
716 (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
718 (define-vop (digit-0-or-plus)
719 (:translate sb!bignum:%digit-0-or-plusp)
721 (:args (digit :scs (unsigned-reg)))
722 (:arg-types unsigned-num)
727 (inst bltz digit target)
728 (inst bgez digit target))
731 (define-vop (add-w/carry)
732 (:translate sb!bignum:%add-with-carry)
734 (:args (a :scs (unsigned-reg))
735 (b :scs (unsigned-reg))
737 (:arg-types unsigned-num unsigned-num positive-fixnum)
738 (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
739 (:results (result :scs (unsigned-reg))
740 (carry :scs (unsigned-reg) :from :eval))
741 (:result-types unsigned-num positive-fixnum)
742 (:temporary (:scs (non-descriptor-reg)) temp)
744 (let ((carry-in (gen-label))
746 (inst bne c carry-in)
750 (inst sltu carry res b)
752 (emit-label carry-in)
754 (inst nor temp a zero-tn)
755 (inst sltu carry b temp)
761 (define-vop (sub-w/borrow)
762 (:translate sb!bignum:%subtract-with-borrow)
764 (:args (a :scs (unsigned-reg))
765 (b :scs (unsigned-reg))
767 (:arg-types unsigned-num unsigned-num positive-fixnum)
768 (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
769 (:results (result :scs (unsigned-reg))
770 (borrow :scs (unsigned-reg) :from :eval))
771 (:result-types unsigned-num positive-fixnum)
773 (let ((no-borrow-in (gen-label))
776 (inst bne c no-borrow-in)
781 (inst sltu borrow b a)
783 (emit-label no-borrow-in)
784 (inst sltu borrow a b)
790 (define-vop (bignum-mult-and-add-3-arg)
791 (:translate sb!bignum:%multiply-and-add)
793 (:args (x :scs (unsigned-reg))
794 (y :scs (unsigned-reg))
795 (carry-in :scs (unsigned-reg) :to :save))
796 (:arg-types unsigned-num unsigned-num unsigned-num)
797 (:temporary (:scs (unsigned-reg) :from (:argument 1)) temp)
798 (:results (hi :scs (unsigned-reg))
799 (lo :scs (unsigned-reg)))
800 (:result-types unsigned-num unsigned-num)
804 (inst addu lo temp carry-in)
805 (inst sltu temp lo carry-in)
807 (inst addu hi temp)))
809 (define-vop (bignum-mult-and-add-4-arg)
810 (:translate sb!bignum:%multiply-and-add)
812 (:args (x :scs (unsigned-reg))
813 (y :scs (unsigned-reg))
814 (prev :scs (unsigned-reg))
815 (carry-in :scs (unsigned-reg) :to :save))
816 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
817 (:temporary (:scs (unsigned-reg) :from (:argument 2)) temp)
818 (:results (hi :scs (unsigned-reg))
819 (lo :scs (unsigned-reg)))
820 (:result-types unsigned-num unsigned-num)
823 (inst addu lo prev carry-in)
824 (inst sltu temp lo carry-in)
829 (inst sltu temp lo temp)
830 (inst addu hi temp)))
832 (define-vop (bignum-mult)
833 (:translate sb!bignum:%multiply)
835 (:args (x :scs (unsigned-reg))
836 (y :scs (unsigned-reg)))
837 (:arg-types unsigned-num unsigned-num)
838 (:results (hi :scs (unsigned-reg))
839 (lo :scs (unsigned-reg)))
840 (:result-types unsigned-num unsigned-num)
846 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
847 (:translate sb!bignum:%lognot))
849 (define-vop (fixnum-to-digit)
850 (:translate sb!bignum:%fixnum-to-digit)
852 (:args (fixnum :scs (any-reg)))
853 (:arg-types tagged-num)
854 (:results (digit :scs (unsigned-reg)))
855 (:result-types unsigned-num)
857 (inst sra digit fixnum 2)))
859 (define-vop (bignum-floor)
860 (:translate sb!bignum:%floor)
862 (:args (num-high :scs (unsigned-reg) :target rem)
863 (num-low :scs (unsigned-reg) :target rem-low)
864 (denom :scs (unsigned-reg) :to (:eval 1)))
865 (:arg-types unsigned-num unsigned-num unsigned-num)
866 (:temporary (:scs (unsigned-reg) :from (:argument 1)) rem-low)
867 (:temporary (:scs (unsigned-reg) :from (:eval 0)) temp)
868 (:results (quo :scs (unsigned-reg) :from (:eval 0))
869 (rem :scs (unsigned-reg) :from (:argument 0)))
870 (:result-types unsigned-num unsigned-num)
871 (:generator 325 ; number of inst assuming targeting works.
873 (move rem-low num-low)
874 (flet ((maybe-subtract (&optional (guess temp))
875 (inst subu temp guess 1)
876 (inst and temp denom)
877 (inst subu rem temp)))
878 (inst sltu quo rem denom)
882 (inst srl temp rem-low 31)
885 (inst sltu temp rem denom)
889 (inst nor quo zero-tn)))
891 (define-vop (signify-digit)
892 (:translate sb!bignum:%fixnum-digit-with-correct-sign)
894 (:args (digit :scs (unsigned-reg) :target res))
895 (:arg-types unsigned-num)
896 (:results (res :scs (any-reg signed-reg)))
897 (:result-types signed-num)
901 (inst sll res digit 2))
906 (define-vop (digit-ashr)
907 (:translate sb!bignum:%ashr)
909 (:args (digit :scs (unsigned-reg))
910 (count :scs (unsigned-reg)))
911 (:arg-types unsigned-num positive-fixnum)
912 (:results (result :scs (unsigned-reg)))
913 (:result-types unsigned-num)
915 (inst sra result digit count)))
917 (define-vop (digit-lshr digit-ashr)
918 (:translate sb!bignum:%digit-logical-shift-right)
920 (inst srl result digit count)))
922 (define-vop (digit-ashl digit-ashr)
923 (:translate sb!bignum:%ashl)
925 (inst sll result digit count)))
928 ;;;; Static functions.
930 (define-static-fun two-arg-gcd (x y) :translate gcd)
931 (define-static-fun two-arg-lcm (x y) :translate lcm)
933 (define-static-fun two-arg-+ (x y) :translate +)
934 (define-static-fun two-arg-- (x y) :translate -)
935 (define-static-fun two-arg-* (x y) :translate *)
936 (define-static-fun two-arg-/ (x y) :translate /)
938 (define-static-fun two-arg-< (x y) :translate <)
939 (define-static-fun two-arg-<= (x y) :translate <=)
940 (define-static-fun two-arg-> (x y) :translate >)
941 (define-static-fun two-arg->= (x y) :translate >=)
942 (define-static-fun two-arg-= (x y) :translate =)
943 (define-static-fun two-arg-/= (x y) :translate /=)
945 (define-static-fun %negate (x) :translate %negate)
947 (define-static-fun two-arg-and (x y) :translate logand)
948 (define-static-fun two-arg-ior (x y) :translate logior)
949 (define-static-fun two-arg-xor (x y) :translate logxor)