1 ;;;; the VM definition arithmetic VOPs for the SPARC
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 (fast-safe-arith-op)
21 (define-vop (fixnum-unop fast-safe-arith-op)
22 (:args (x :scs (any-reg)))
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)))
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)
40 (define-vop (fast-negate/signed signed-unop)
45 (define-vop (fast-lognot/fixnum fixnum-unop)
48 (inst xor res x (fixnumize -1))))
50 (define-vop (fast-lognot/signed signed-unop)
55 ;;;; Binary fixnum operations.
57 ;;; Assume that any constant operand is the second arg...
59 (define-vop (fast-fixnum-binop fast-safe-arith-op)
60 (:args (x :target r :scs (any-reg zero))
61 (y :target r :scs (any-reg zero)))
62 (:arg-types tagged-num tagged-num)
63 (:results (r :scs (any-reg)))
64 (:result-types tagged-num)
65 (:note "inline fixnum arithmetic"))
67 (define-vop (fast-unsigned-binop fast-safe-arith-op)
68 (:args (x :target r :scs (unsigned-reg zero))
69 (y :target r :scs (unsigned-reg zero)))
70 (:arg-types unsigned-num unsigned-num)
71 (:results (r :scs (unsigned-reg)))
72 (:result-types unsigned-num)
73 (:note "inline (unsigned-byte 32) arithmetic"))
75 (define-vop (fast-signed-binop fast-safe-arith-op)
76 (:args (x :target r :scs (signed-reg zero))
77 (y :target r :scs (signed-reg zero)))
78 (:arg-types signed-num signed-num)
79 (:results (r :scs (signed-reg)))
80 (:result-types signed-num)
81 (:note "inline (signed-byte 32) arithmetic"))
84 (define-vop (fast-fixnum-binop-c fast-safe-arith-op)
85 (:args (x :target r :scs (any-reg zero)))
87 (:arg-types tagged-num
88 (:constant (and (signed-byte 11) (not (integer 0 0)))))
89 (:results (r :scs (any-reg)))
90 (:result-types tagged-num)
91 (:note "inline fixnum arithmetic"))
93 (define-vop (fast-unsigned-binop-c fast-safe-arith-op)
94 (:args (x :target r :scs (unsigned-reg zero)))
96 (:arg-types unsigned-num
97 (:constant (and (signed-byte 13) (not (integer 0 0)))))
98 (:results (r :scs (unsigned-reg)))
99 (:result-types unsigned-num)
100 (:note "inline (unsigned-byte 32) arithmetic"))
102 (define-vop (fast-signed-binop-c fast-safe-arith-op)
103 (:args (x :target r :scs (signed-reg zero)))
105 (:arg-types signed-num
106 (:constant (and (signed-byte 13) (not (integer 0 0)))))
107 (:results (r :scs (signed-reg)))
108 (:result-types signed-num)
109 (:note "inline (signed-byte 32) arithmetic"))
112 (eval-when (:compile-toplevel :load-toplevel :execute)
114 (defmacro define-binop (translate untagged-penalty op)
116 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
118 (:translate ,translate)
121 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
123 (:translate ,translate)
125 (inst ,op r x (fixnumize y))))
126 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
128 (:translate ,translate)
129 (:generator ,(1+ untagged-penalty)
131 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
133 (:translate ,translate)
134 (:generator ,untagged-penalty
136 (define-vop (,(symbolicate "FAST-" translate "/UNSIGNED=>UNSIGNED")
138 (:translate ,translate)
139 (:generator ,(1+ untagged-penalty)
141 (define-vop (,(symbolicate 'fast- translate '-c/unsigned=>unsigned)
142 fast-unsigned-binop-c)
143 (:translate ,translate)
144 (:generator ,untagged-penalty
149 (define-binop + 4 add)
150 (define-binop - 4 sub)
151 (define-binop logand 2 and)
152 (define-binop logandc2 2 andn)
153 (define-binop logior 2 or)
154 (define-binop logorc2 2 orn)
155 (define-binop logxor 2 xor)
156 (define-binop logeqv 2 xnor)
158 ;;; Special logand cases: (logand signed unsigned) => unsigned
160 (define-vop (fast-logand/signed-unsigned=>unsigned
161 fast-logand/unsigned=>unsigned)
162 (:args (x :scs (signed-reg))
163 (y :target r :scs (unsigned-reg)))
164 (:arg-types signed-num unsigned-num))
166 (define-vop (fast-logand/unsigned-signed=>unsigned
167 fast-logand/unsigned=>unsigned)
168 (:args (x :target r :scs (unsigned-reg))
169 (y :scs (signed-reg)))
170 (:arg-types unsigned-num signed-num))
172 ;;; Special case fixnum + and - that trap on overflow. Useful when we
173 ;;; don't know that the output type is a fixnum.
175 ;;; I (Raymond Toy) took these out. They don't seem to be used anywhere at all.
178 (define-vop (+/fixnum fast-+/fixnum=>fixnum)
180 (:results (r :scs (any-reg descriptor-reg)))
181 (:result-types tagged-num)
182 (:note "safe inline fixnum arithmetic")
184 (inst taddcctv r x y)))
186 (define-vop (+-c/fixnum fast-+-c/fixnum=>fixnum)
188 (:results (r :scs (any-reg descriptor-reg)))
189 (:result-types tagged-num)
190 (:note "safe inline fixnum arithmetic")
192 (inst taddcctv r x (fixnumize y))))
194 (define-vop (-/fixnum fast--/fixnum=>fixnum)
196 (:results (r :scs (any-reg descriptor-reg)))
197 (:result-types tagged-num)
198 (:note "safe inline fixnum arithmetic")
200 (inst tsubcctv r x y)))
202 (define-vop (--c/fixnum fast---c/fixnum=>fixnum)
204 (:results (r :scs (any-reg descriptor-reg)))
205 (:result-types tagged-num)
206 (:note "safe inline fixnum arithmetic")
208 (inst tsubcctv r x (fixnumize y))))
214 ;; This doesn't work for some reason.
216 (define-vop (fast-v8-truncate/fixnum=>fixnum fast-safe-arith-op)
217 (:translate truncate)
218 (:args (x :scs (any-reg))
220 (:arg-types tagged-num tagged-num)
221 (:results (quo :scs (any-reg))
222 (rem :scs (any-reg)))
223 (:result-types tagged-num tagged-num)
224 (:note "inline fixnum arithmetic")
225 (:temporary (:scs (any-reg) :target quo) q)
226 (:temporary (:scs (any-reg)) r)
227 (:temporary (:scs (signed-reg)) y-int)
229 (:save-p :compute-only)
230 (:guard (or (member :sparc-v8 *backend-subfeatures*)
231 (and (member :sparc-v9 *backend-subfeatures*)
232 (not (member :sparc-64 *backend-subfeatures*)))))
234 (let ((zero (generate-error-code vop division-by-zero-error x y)))
237 ;; Extend the sign of X into the Y register
240 ;; Remove tag bits so Q and R will be tagged correctly.
241 (inst sra y-int y n-fixnum-tag-bits)
245 (inst sdiv q x y-int) ; Q is tagged.
246 ;; We have the quotient so we need to compute the remainder
247 (inst smul r q y-int) ; R is tagged
249 (unless (location= quo q)
252 (define-vop (fast-v8-truncate/signed=>signed fast-safe-arith-op)
253 (:translate truncate)
254 (:args (x :scs (signed-reg))
255 (y :scs (signed-reg)))
256 (:arg-types signed-num signed-num)
257 (:results (quo :scs (signed-reg))
258 (rem :scs (signed-reg)))
259 (:result-types signed-num signed-num)
260 (:note "inline (signed-byte 32) arithmetic")
261 (:temporary (:scs (signed-reg) :target quo) q)
262 (:temporary (:scs (signed-reg)) r)
264 (:save-p :compute-only)
265 (:guard (or (member :sparc-v8 *backend-subfeatures*)
266 (and (member :sparc-v9 *backend-subfeatures*)
267 (not (member :sparc-64 *backend-subfeatures*)))))
269 (let ((zero (generate-error-code vop division-by-zero-error x y)))
271 (if (member :sparc-v9 *backend-subfeatures*)
272 (inst b :eq zero :pn)
274 ;; Extend the sign of X into the Y register
282 ;; We have the quotient so we need to compue the remainder
283 (inst smul r q y) ; rem
285 (unless (location= quo q)
288 (define-vop (fast-v8-truncate/unsigned=>unsigned fast-safe-arith-op)
289 (:translate truncate)
290 (:args (x :scs (unsigned-reg))
291 (y :scs (unsigned-reg)))
292 (:arg-types unsigned-num unsigned-num)
293 (:results (quo :scs (unsigned-reg))
294 (rem :scs (unsigned-reg)))
295 (:result-types unsigned-num unsigned-num)
296 (:note "inline (unsigned-byte 32) arithmetic")
297 (:temporary (:scs (unsigned-reg) :target quo) q)
298 (:temporary (:scs (unsigned-reg)) r)
300 (:save-p :compute-only)
301 (:guard (or (member :sparc-v8 *backend-subfeatures*)
302 (and (member :sparc-v9 *backend-subfeatures*)
303 (not (member :sparc-64 *backend-subfeatures*)))))
305 (let ((zero (generate-error-code vop division-by-zero-error x y)))
307 (if (member :sparc-v9 *backend-subfeatures*)
308 (inst b :eq zero :pn)
310 (inst wry zero-tn) ; Clear out high part
319 (unless (location= quo q)
320 (inst move quo q)))))
322 (define-vop (fast-v9-truncate/signed=>signed fast-safe-arith-op)
323 (:translate truncate)
324 (:args (x :scs (signed-reg))
325 (y :scs (signed-reg)))
326 (:arg-types signed-num signed-num)
327 (:results (quo :scs (signed-reg))
328 (rem :scs (signed-reg)))
329 (:result-types signed-num signed-num)
330 (:note "inline (signed-byte 32) arithmetic")
331 (:temporary (:scs (signed-reg) :target quo) q)
332 (:temporary (:scs (signed-reg)) r)
334 (:save-p :compute-only)
335 (:guard (member :sparc-64 *backend-subfeatures*))
337 (let ((zero (generate-error-code vop division-by-zero-error x y)))
339 (inst b :eq zero :pn)
340 ;; Sign extend the numbers, just in case.
347 (unless (location= quo q)
348 (inst move quo q)))))
350 (define-vop (fast-v9-truncate/unsigned=>unsigned fast-safe-arith-op)
351 (:translate truncate)
352 (:args (x :scs (unsigned-reg))
353 (y :scs (unsigned-reg)))
354 (:arg-types unsigned-num unsigned-num)
355 (:results (quo :scs (unsigned-reg))
356 (rem :scs (unsigned-reg)))
357 (:result-types unsigned-num unsigned-num)
358 (:note "inline (unsigned-byte 32) arithmetic")
359 (:temporary (:scs (unsigned-reg) :target quo) q)
360 (:temporary (:scs (unsigned-reg)) r)
362 (:save-p :compute-only)
363 (:guard (member :sparc-64 *backend-subfeatures*))
365 (let ((zero (generate-error-code vop division-by-zero-error x y)))
367 (inst b :eq zero :pn)
368 ;; Zap the higher 32 bits, just in case
375 (unless (location= quo q)
376 (inst move quo q)))))
381 ((frob (name sc-type type shift-right-inst)
384 (:args (number :scs (,sc-type) :to :save)
385 (amount :scs (signed-reg immediate)))
386 (:arg-types ,type signed-num)
387 (:results (result :scs (,sc-type)))
388 (:result-types ,type)
391 (:temporary (:sc non-descriptor-reg) ndesc)
396 ;; FIXME: These two don't look different enough.
397 ((member :sparc-v9 *backend-subfeatures*)
398 (let ((done (gen-label))
399 (positive (gen-label)))
401 (inst b :ge positive)
402 (inst neg ndesc amount)
403 ;; ndesc = max(-amount, 31)
405 (inst cmove :ge ndesc 31)
407 (inst ,shift-right-inst result number ndesc)
408 (emit-label positive)
409 ;; The result-type assures us that this shift will
411 (inst sll result number amount)
412 ;; We want a right shift of the appropriate size.
415 (let ((positive (gen-label))
418 (inst b :ge positive)
419 (inst neg ndesc amount)
422 (inst ,shift-right-inst result number ndesc)
424 (inst ,shift-right-inst result number 31)
425 (emit-label positive)
426 ;; The result-type assures us that this shift will
428 (inst sll result number amount)
429 (emit-label done)))))
431 (let ((amount (tn-value amount)))
433 (let ((amount (min 31 (- amount))))
434 (inst ,shift-right-inst result number amount))
435 (inst sll result number amount)))))))))
436 (frob fast-ash/signed=>signed signed-reg signed-num sra)
437 (frob fast-ash/unsigned=>unsigned unsigned-reg unsigned-num srl))
439 ;; Some special cases where we know we want a left shift. Just do the
440 ;; shift, instead of checking for the sign of the shift.
442 ((frob (name sc-type type result-type cost)
446 (:args (number :scs (,sc-type))
447 (amount :scs (signed-reg unsigned-reg immediate)))
448 (:arg-types ,type positive-fixnum)
449 (:results (result :scs (,result-type)))
450 (:result-types ,type)
453 ;; The result-type assures us that this shift will not
454 ;; overflow. And for fixnum's, the zero bits that get
455 ;; shifted in are just fine for the fixnum tag.
457 ((signed-reg unsigned-reg)
458 (inst sll result number amount))
460 (let ((amount (tn-value amount)))
461 (assert (>= amount 0))
462 (inst sll result number amount))))))))
463 (frob fast-ash-left/signed=>signed signed-reg signed-num signed-reg 3)
464 (frob fast-ash-left/fixnum=>fixnum any-reg tagged-num any-reg 2)
465 (frob fast-ash-left/unsigned=>unsigned unsigned-reg unsigned-num unsigned-reg 3))
467 (defknown ash-right-signed ((signed-byte #.sb!vm:n-word-bits)
468 (and fixnum unsigned-byte))
469 (signed-byte #.sb!vm:n-word-bits)
470 (movable foldable flushable))
472 (defknown ash-right-unsigned ((unsigned-byte #.sb!vm:n-word-bits)
473 (and fixnum unsigned-byte))
474 (unsigned-byte #.sb!vm:n-word-bits)
475 (movable foldable flushable))
477 ;; Some special cases where we want a right shift. Just do the shift.
478 ;; (Needs appropriate deftransforms to call these, though.)
481 ((frob (trans name sc-type type shift-inst cost)
483 (:note "inline right ASH")
485 (:args (number :scs (,sc-type))
486 (amount :scs (signed-reg unsigned-reg immediate)))
487 (:arg-types ,type positive-fixnum)
488 (:results (result :scs (,sc-type)))
489 (:result-types ,type)
493 ((signed-reg unsigned-reg)
494 (inst ,shift-inst result number amount))
496 (let ((amt (tn-value amount)))
497 (inst ,shift-inst result number amt))))))))
498 (frob ash-right-signed fast-ash-right/signed=>signed
499 signed-reg signed-num sra 3)
500 (frob ash-right-unsigned fast-ash-right/unsigned=>unsigned
501 unsigned-reg unsigned-num srl 3))
503 (define-vop (fast-ash-right/fixnum=>fixnum)
504 (:note "inline right ASH")
505 (:translate ash-right-signed)
506 (:args (number :scs (any-reg))
507 (amount :scs (signed-reg unsigned-reg immediate)))
508 (:arg-types tagged-num positive-fixnum)
509 (:results (result :scs (any-reg)))
510 (:result-types tagged-num)
511 (:temporary (:sc non-descriptor-reg :target result) temp)
514 ;; Shift the fixnum right by the desired amount. Then zap out the
515 ;; 2 LSBs to make it a fixnum again. (Those bits are junk.)
517 ((signed-reg unsigned-reg)
518 (inst sra temp number amount))
520 (inst sra temp number (tn-value amount))))
521 (inst andn result temp fixnum-tag-mask)))
526 (define-vop (signed-byte-32-len)
527 (:translate integer-length)
528 (:note "inline (signed-byte 32) integer-length")
530 (:args (arg :scs (signed-reg) :target shift))
531 (:arg-types signed-num)
532 (:results (res :scs (any-reg)))
533 (:result-types positive-fixnum)
534 (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) shift)
536 (let ((loop (gen-label))
538 (inst addcc shift zero-tn arg)
545 (inst add res (fixnumize 1))
550 (inst srl shift 1))))
552 (define-vop (unsigned-byte-32-count)
553 (:translate logcount)
554 (:note "inline (unsigned-byte 32) logcount")
556 (:args (arg :scs (unsigned-reg)))
557 (:arg-types unsigned-num)
558 (:results (res :scs (unsigned-reg)))
559 (:result-types positive-fixnum)
560 (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) mask temp)
564 (dolist (stuff '((1 #x55555555) (2 #x33333333) (4 #x0f0f0f0f)
565 (8 #x00ff00ff) (16 #x0000ffff)))
566 (destructuring-bind (shift bit-mask)
569 (inst sethi mask (ldb (byte 22 10) bit-mask))
570 (inst add mask (ldb (byte 10 0) bit-mask))
572 (inst and temp res mask)
575 (inst add res temp)))))
578 ;;; Multiply and Divide.
580 (define-vop (fast-v8-*/fixnum=>fixnum fast-fixnum-binop)
581 (:temporary (:scs (non-descriptor-reg)) temp)
583 (:guard (or (member :sparc-v8 *backend-subfeatures*)
584 (and (member :sparc-v9 *backend-subfeatures*)
585 (not (member :sparc-64 *backend-subfeatures*)))))
587 ;; The cost here should be less than the cost for
588 ;; */signed=>signed. Why? A fixnum product using signed=>signed
589 ;; has to convert both args to signed-nums. But using this, we
590 ;; don't have to and that saves an instruction.
591 (inst sra temp y n-fixnum-tag-bits)
592 (inst smul r x temp)))
594 (define-vop (fast-v8-*-c/fixnum=>fixnum fast-safe-arith-op)
595 (:args (x :target r :scs (any-reg zero)))
597 (:arg-types tagged-num
598 (:constant (and (signed-byte 13) (not (integer 0 0)))))
599 (:results (r :scs (any-reg)))
600 (:result-types tagged-num)
601 (:note "inline fixnum arithmetic")
603 (:guard (or (member :sparc-v8 *backend-subfeatures*)
604 (and (member :sparc-v9 *backend-subfeatures*)
605 (not (member :sparc-64 *backend-subfeatures*)))))
609 (define-vop (fast-v8-*/signed=>signed fast-signed-binop)
611 (:guard (or (member :sparc-v8 *backend-subfeatures*)
612 (and (member :sparc-v9 *backend-subfeatures*)
613 (not (member :sparc-64 *backend-subfeatures*)))))
617 (define-vop (fast-v8-*-c/signed=>signed fast-signed-binop-c)
619 (:guard (or (member :sparc-v8 *backend-subfeatures*)
620 (and (member :sparc-v9 *backend-subfeatures*)
621 (not (member :sparc-64 *backend-subfeatures*)))))
625 (define-vop (fast-v8-*/unsigned=>unsigned fast-unsigned-binop)
627 (:guard (or (member :sparc-v8 *backend-subfeatures*)
628 (and (member :sparc-v9 *backend-subfeatures*)
629 (not (member :sparc-64 *backend-subfeatures*)))))
633 (define-vop (fast-v8-*-c/unsigned=>unsigned fast-unsigned-binop-c)
635 (:guard (or (member :sparc-v8 *backend-subfeatures*)
636 (and (member :sparc-v9 *backend-subfeatures*)
637 (not (member :sparc-64 *backend-subfeatures*)))))
641 ;; The smul and umul instructions are deprecated on the Sparc V9. Use
643 (define-vop (fast-v9-*/fixnum=>fixnum fast-fixnum-binop)
644 (:temporary (:scs (non-descriptor-reg)) temp)
646 (:guard (member :sparc-64 *backend-subfeatures*))
648 (inst sra temp y n-fixnum-tag-bits)
649 (inst mulx r x temp)))
651 (define-vop (fast-v9-*/signed=>signed fast-signed-binop)
653 (:guard (member :sparc-64 *backend-subfeatures*))
657 (define-vop (fast-v9-*/unsigned=>unsigned fast-unsigned-binop)
659 (:guard (member :sparc-64 *backend-subfeatures*))
664 ;;;; Binary conditional VOPs:
666 (define-vop (fast-conditional)
671 (:policy :fast-safe))
673 (define-vop (fast-conditional/fixnum fast-conditional)
674 (:args (x :scs (any-reg zero))
675 (y :scs (any-reg zero)))
676 (:arg-types tagged-num tagged-num)
677 (:note "inline fixnum comparison"))
679 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
680 (:args (x :scs (any-reg zero)))
681 (:arg-types tagged-num (:constant (signed-byte 11)))
682 (:info target not-p y))
684 (define-vop (fast-conditional/signed fast-conditional)
685 (:args (x :scs (signed-reg zero))
686 (y :scs (signed-reg zero)))
687 (:arg-types signed-num signed-num)
688 (:note "inline (signed-byte 32) comparison"))
690 (define-vop (fast-conditional-c/signed fast-conditional/signed)
691 (:args (x :scs (signed-reg zero)))
692 (:arg-types signed-num (:constant (signed-byte 13)))
693 (:info target not-p y))
695 (define-vop (fast-conditional/unsigned fast-conditional)
696 (:args (x :scs (unsigned-reg zero))
697 (y :scs (unsigned-reg zero)))
698 (:arg-types unsigned-num unsigned-num)
699 (:note "inline (unsigned-byte 32) comparison"))
701 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
702 (:args (x :scs (unsigned-reg zero)))
703 (:arg-types unsigned-num (:constant (unsigned-byte 12)))
704 (:info target not-p y))
707 (defmacro define-conditional-vop (tran cond unsigned not-cond not-unsigned)
709 ,@(mapcar (lambda (suffix cost signed)
710 (unless (and (member suffix '(/fixnum -c/fixnum))
712 `(define-vop (,(intern (format nil "~:@(FAST-IF-~A~A~)"
715 (format nil "~:@(FAST-CONDITIONAL~A~)"
720 ,(if (eq suffix '-c/fixnum) '(fixnumize y) 'y))
722 ,(if signed not-cond not-unsigned)
723 ,(if signed cond unsigned))
726 '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
728 '(t t t t nil nil))))
730 (define-conditional-vop < :lt :ltu :ge :geu)
732 (define-conditional-vop > :gt :gtu :le :leu)
734 (define-conditional-vop eql :eq :eq :ne :ne)
736 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
739 ;;; These versions specify a fixnum restriction on their first arg. We have
740 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
741 ;;; the first arg and a higher cost. The reason for doing this is to prevent
742 ;;; fixnum specific operations from being used on word integers, spuriously
743 ;;; consing the argument.
746 (define-vop (fast-eql/fixnum fast-conditional)
747 (:args (x :scs (any-reg descriptor-reg zero))
748 (y :scs (any-reg zero)))
749 (:arg-types tagged-num tagged-num)
750 (:note "inline fixnum comparison")
754 (inst b (if not-p :ne :eq) target)
757 (define-vop (generic-eql/fixnum fast-eql/fixnum)
758 (:arg-types * tagged-num)
761 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
762 (:args (x :scs (any-reg descriptor-reg zero)))
763 (:arg-types tagged-num (:constant (signed-byte 11)))
764 (:info target not-p y)
767 (inst cmp x (fixnumize y))
768 (inst b (if not-p :ne :eq) target)
771 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
772 (:arg-types * (:constant (signed-byte 11)))
776 ;;;; 32-bit logical operations
778 (define-vop (merge-bits)
779 (:translate merge-bits)
780 (:args (shift :scs (signed-reg unsigned-reg))
781 (prev :scs (unsigned-reg))
782 (next :scs (unsigned-reg)))
783 (:arg-types tagged-num unsigned-num unsigned-num)
784 (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
785 (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
786 (:results (result :scs (unsigned-reg)))
787 (:result-types unsigned-num)
790 (let ((done (gen-label)))
793 (inst srl res next shift)
794 (inst sub temp zero-tn shift)
795 (inst sll temp prev temp)
801 (define-vop (32bit-logical)
802 (:args (x :scs (unsigned-reg zero))
803 (y :scs (unsigned-reg zero)))
804 (:arg-types unsigned-num unsigned-num)
805 (:results (r :scs (unsigned-reg)))
806 (:result-types unsigned-num)
807 (:policy :fast-safe))
809 (define-vop (32bit-logical-not 32bit-logical)
810 (:translate 32bit-logical-not)
811 (:args (x :scs (unsigned-reg zero)))
812 (:arg-types unsigned-num)
816 (define-vop (32bit-logical-and 32bit-logical)
817 (:translate 32bit-logical-and)
821 (deftransform 32bit-logical-nand ((x y) (* *))
822 '(32bit-logical-not (32bit-logical-and x y)))
824 (define-vop (32bit-logical-or 32bit-logical)
825 (:translate 32bit-logical-or)
829 (deftransform 32bit-logical-nor ((x y) (* *))
830 '(32bit-logical-not (32bit-logical-or x y)))
832 (define-vop (32bit-logical-xor 32bit-logical)
833 (:translate 32bit-logical-xor)
837 (define-vop (32bit-logical-eqv 32bit-logical)
838 (:translate 32bit-logical-eqv)
842 (define-vop (32bit-logical-orc2 32bit-logical)
843 (:translate 32bit-logical-orc2)
847 (deftransform 32bit-logical-orc1 ((x y) (* *))
848 '(32bit-logical-orc2 y x))
850 (define-vop (32bit-logical-andc2 32bit-logical)
851 (:translate 32bit-logical-andc2)
855 (deftransform 32bit-logical-andc1 ((x y) (* *))
856 '(32bit-logical-andc2 y x))
859 (define-vop (shift-towards-someplace)
861 (:args (num :scs (unsigned-reg))
862 (amount :scs (signed-reg)))
863 (:arg-types unsigned-num tagged-num)
864 (:results (r :scs (unsigned-reg)))
865 (:result-types unsigned-num))
867 (define-vop (shift-towards-start shift-towards-someplace)
868 (:translate shift-towards-start)
869 (:note "shift-towards-start")
871 (inst sll r num amount)))
873 (define-vop (shift-towards-end shift-towards-someplace)
874 (:translate shift-towards-end)
875 (:note "shift-towards-end")
877 (inst srl r num amount)))
884 (define-vop (bignum-length get-header-data)
885 (:translate sb!bignum::%bignum-length)
886 (:policy :fast-safe))
888 (define-vop (bignum-set-length set-header-data)
889 (:translate sb!bignum::%bignum-set-length)
890 (:policy :fast-safe))
892 (define-vop (bignum-ref word-index-ref)
893 (:variant bignum-digits-offset other-pointer-lowtag)
894 (:translate sb!bignum::%bignum-ref)
895 (:results (value :scs (unsigned-reg)))
896 (:result-types unsigned-num))
898 (define-vop (bignum-set word-index-set)
899 (:variant bignum-digits-offset other-pointer-lowtag)
900 (:translate sb!bignum::%bignum-set)
901 (:args (object :scs (descriptor-reg))
902 (index :scs (any-reg immediate zero))
903 (value :scs (unsigned-reg)))
904 (:arg-types t positive-fixnum unsigned-num)
905 (:results (result :scs (unsigned-reg)))
906 (:result-types unsigned-num))
908 (define-vop (digit-0-or-plus)
909 (:translate sb!bignum::%digit-0-or-plusp)
911 (:args (digit :scs (unsigned-reg)))
912 (:arg-types unsigned-num)
913 (:results (result :scs (descriptor-reg)))
914 (:guard (not (member :sparc-v9 *backend-subfeatures*)))
916 (let ((done (gen-label)))
919 (move result null-tn)
920 (load-symbol result t)
923 (define-vop (v9-digit-0-or-plus-cmove)
924 (:translate sb!bignum::%digit-0-or-plusp)
926 (:args (digit :scs (unsigned-reg)))
927 (:arg-types unsigned-num)
928 (:results (result :scs (descriptor-reg)))
929 (:guard (member :sparc-v9 *backend-subfeatures*))
932 (load-symbol result t)
933 (inst cmove :lt result null-tn)))
935 ;; This doesn't work?
937 (define-vop (v9-digit-0-or-plus-movr)
938 (:translate sb!bignum::%digit-0-or-plusp)
940 (:args (digit :scs (unsigned-reg)))
941 (:arg-types unsigned-num)
942 (:results (result :scs (descriptor-reg)))
943 (:temporary (:scs (descriptor-reg)) temp)
944 (:guard #!+:sparc-v9 t #!-:sparc-v9 nil)
947 (inst movr result null-tn digit :lz)
948 (inst movr result temp digit :gez)))
951 (define-vop (add-w/carry)
952 (:translate sb!bignum::%add-with-carry)
954 (:args (a :scs (unsigned-reg))
955 (b :scs (unsigned-reg))
957 (:arg-types unsigned-num unsigned-num positive-fixnum)
958 (:results (result :scs (unsigned-reg))
959 (carry :scs (unsigned-reg)))
960 (:result-types unsigned-num positive-fixnum)
962 (inst addcc zero-tn c -1)
963 (inst addxcc result a b)
964 (inst addx carry zero-tn zero-tn)))
966 (define-vop (sub-w/borrow)
967 (:translate sb!bignum::%subtract-with-borrow)
969 (:args (a :scs (unsigned-reg))
970 (b :scs (unsigned-reg))
972 (:arg-types unsigned-num unsigned-num positive-fixnum)
973 (:results (result :scs (unsigned-reg))
974 (borrow :scs (unsigned-reg)))
975 (:result-types unsigned-num positive-fixnum)
977 (inst subcc zero-tn c 1)
978 (inst subxcc result a b)
979 (inst addx borrow zero-tn zero-tn)
980 (inst xor borrow 1)))
982 ;;; EMIT-MULTIPLY -- This is used both for bignum stuff and in assembly
985 (defun emit-multiply (multiplier multiplicand result-high result-low)
986 "Emit code to multiply MULTIPLIER with MULTIPLICAND, putting the result
987 in RESULT-HIGH and RESULT-LOW. KIND is either :signed or :unsigned.
988 Note: the lifetimes of MULTIPLICAND and RESULT-HIGH overlap."
989 (declare (type tn multiplier result-high result-low)
990 (type (or tn (signed-byte 13)) multiplicand))
991 ;; It seems that emit-multiply is only used to do an unsigned
992 ;; multiply, so the code only does an unsigned multiply.
994 ((member :sparc-64 *backend-subfeatures*)
995 ;; Take advantage of V9's 64-bit multiplier.
997 ;; Make sure the multiplier and multiplicand are really
998 ;; unsigned 64-bit numbers.
999 (inst srl multiplier 0)
1000 (inst srl multiplicand 0)
1002 ;; Multiply the two numbers and put the result in
1003 ;; result-high. Copy the low 32-bits to result-low. Then
1004 ;; shift result-high so the high 32-bits end up in the low
1006 (inst mulx result-high multiplier multiplicand)
1007 (inst move result-low result-high)
1008 (inst srax result-high 32))
1009 ((or (member :sparc-v8 *backend-subfeatures*)
1010 (member :sparc-v9 *backend-subfeatures*))
1011 ;; V8 has a multiply instruction. This should also work for
1012 ;; the V9, but umul and the Y register is deprecated on the
1014 (inst umul result-low multiplier multiplicand)
1015 (inst rdy result-high))
1017 (let ((label (gen-label)))
1018 (inst wry multiplier)
1019 (inst andcc result-high zero-tn)
1020 ;; Note: we can't use the Y register until three insts
1021 ;; after it's written.
1025 (inst mulscc result-high multiplicand))
1026 (inst mulscc result-high zero-tn)
1027 (inst cmp multiplicand)
1030 (inst add result-high multiplier)
1032 (inst rdy result-low)))))
1034 (define-vop (bignum-mult-and-add-3-arg)
1035 (:translate sb!bignum::%multiply-and-add)
1036 (:policy :fast-safe)
1037 (:args (x :scs (unsigned-reg) :to (:eval 1))
1038 (y :scs (unsigned-reg) :to (:eval 1))
1039 (carry-in :scs (unsigned-reg) :to (:eval 2)))
1040 (:arg-types unsigned-num unsigned-num unsigned-num)
1041 (:results (hi :scs (unsigned-reg) :from (:eval 0))
1042 (lo :scs (unsigned-reg) :from (:eval 1)))
1043 (:result-types unsigned-num unsigned-num)
1045 (emit-multiply x y hi lo)
1046 (inst addcc lo carry-in)
1047 (inst addx hi zero-tn)))
1049 (define-vop (bignum-mult-and-add-4-arg)
1050 (:translate sb!bignum::%multiply-and-add)
1051 (:policy :fast-safe)
1052 (:args (x :scs (unsigned-reg) :to (:eval 1))
1053 (y :scs (unsigned-reg) :to (:eval 1))
1054 (prev :scs (unsigned-reg) :to (:eval 2))
1055 (carry-in :scs (unsigned-reg) :to (:eval 2)))
1056 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1057 (:results (hi :scs (unsigned-reg) :from (:eval 0))
1058 (lo :scs (unsigned-reg) :from (:eval 1)))
1059 (:result-types unsigned-num unsigned-num)
1061 (emit-multiply x y hi lo)
1062 (inst addcc lo carry-in)
1063 (inst addx hi zero-tn)
1064 (inst addcc lo prev)
1065 (inst addx hi zero-tn)))
1067 (define-vop (bignum-mult)
1068 (:translate sb!bignum::%multiply)
1069 (:policy :fast-safe)
1070 (:args (x :scs (unsigned-reg) :to (:result 1))
1071 (y :scs (unsigned-reg) :to (:result 1)))
1072 (:arg-types unsigned-num unsigned-num)
1073 (:results (hi :scs (unsigned-reg))
1074 (lo :scs (unsigned-reg)))
1075 (:result-types unsigned-num unsigned-num)
1077 (emit-multiply x y hi lo)))
1079 (define-vop (bignum-lognot)
1080 (:translate sb!bignum::%lognot)
1081 (:policy :fast-safe)
1082 (:args (x :scs (unsigned-reg)))
1083 (:arg-types unsigned-num)
1084 (:results (r :scs (unsigned-reg)))
1085 (:result-types unsigned-num)
1089 (define-vop (fixnum-to-digit)
1090 (:translate sb!bignum::%fixnum-to-digit)
1091 (:policy :fast-safe)
1092 (:args (fixnum :scs (any-reg)))
1093 (:arg-types tagged-num)
1094 (:results (digit :scs (unsigned-reg)))
1095 (:result-types unsigned-num)
1097 (inst sra digit fixnum n-fixnum-tag-bits)))
1099 (define-vop (bignum-floor)
1100 (:translate sb!bignum::%floor)
1101 (:policy :fast-safe)
1102 (:args (div-high :scs (unsigned-reg) :target rem)
1103 (div-low :scs (unsigned-reg) :target quo)
1104 (divisor :scs (unsigned-reg)))
1105 (:arg-types unsigned-num unsigned-num unsigned-num)
1106 (:results (quo :scs (unsigned-reg) :from (:argument 1))
1107 (rem :scs (unsigned-reg) :from (:argument 0)))
1108 (:result-types unsigned-num unsigned-num)
1113 (let ((label (gen-label)))
1114 (inst cmp rem divisor)
1116 (inst addxcc quo quo)
1117 (inst sub rem divisor)
1120 (inst addx rem rem))))
1123 (define-vop (bignum-floor-v8)
1124 (:translate sb!bignum::%floor)
1125 (:policy :fast-safe)
1126 (:args (div-high :scs (unsigned-reg) :target rem)
1127 (div-low :scs (unsigned-reg) :target quo)
1128 (divisor :scs (unsigned-reg)))
1129 (:arg-types unsigned-num unsigned-num unsigned-num)
1130 (:results (quo :scs (unsigned-reg) :from (:argument 1))
1131 (rem :scs (unsigned-reg) :from (:argument 0)))
1132 (:result-types unsigned-num unsigned-num)
1133 (:temporary (:scs (unsigned-reg) :target quo) q)
1134 ;; This vop is for a v8 or v9, provided we're also not using
1135 ;; sparc-64, for which there a special sparc-64 vop.
1136 (:guard (or (member :sparc-v8 *backend-subfeatures*)
1137 (member :sparc-v9 *backend-subfeatures*)))
1143 ;; Compute the quotient [Y, div-low] / divisor
1144 (inst udiv q div-low divisor)
1145 ;; Compute the remainder. The high part of the result is in the Y
1147 (inst umul rem q divisor)
1148 (inst sub rem div-low rem)
1149 (unless (location= quo q)
1152 (define-vop (bignum-floor-v9)
1153 (:translate sb!bignum::%floor)
1154 (:policy :fast-safe)
1155 (:args (div-high :scs (unsigned-reg))
1156 (div-low :scs (unsigned-reg))
1157 (divisor :scs (unsigned-reg) :to (:result 1)))
1158 (:arg-types unsigned-num unsigned-num unsigned-num)
1159 (:temporary (:sc unsigned-reg :from (:argument 0)) dividend)
1160 (:results (quo :scs (unsigned-reg))
1161 (rem :scs (unsigned-reg)))
1162 (:result-types unsigned-num unsigned-num)
1163 (:guard (member :sparc-64 *backend-subfeatures*))
1165 ;; Set dividend to be div-high and div-low
1166 (inst sllx dividend div-high 32)
1167 (inst add dividend div-low)
1169 (inst udivx quo dividend divisor)
1170 ;; Compute the remainder
1171 (inst mulx rem quo divisor)
1172 (inst sub rem dividend rem)))
1174 (define-vop (signify-digit)
1175 (:translate sb!bignum::%fixnum-digit-with-correct-sign)
1176 (:policy :fast-safe)
1177 (:args (digit :scs (unsigned-reg) :target res))
1178 (:arg-types unsigned-num)
1179 (:results (res :scs (any-reg signed-reg)))
1180 (:result-types signed-num)
1184 (inst sll res digit n-fixnum-tag-bits))
1186 (move res digit)))))
1189 (define-vop (digit-ashr)
1190 (:translate sb!bignum::%ashr)
1191 (:policy :fast-safe)
1192 (:args (digit :scs (unsigned-reg))
1193 (count :scs (unsigned-reg)))
1194 (:arg-types unsigned-num positive-fixnum)
1195 (:results (result :scs (unsigned-reg)))
1196 (:result-types unsigned-num)
1198 (inst sra result digit count)))
1200 (define-vop (digit-lshr digit-ashr)
1201 (:translate sb!bignum::%digit-logical-shift-right)
1203 (inst srl result digit count)))
1205 (define-vop (digit-ashl digit-ashr)
1206 (:translate sb!bignum::%ashl)
1208 (inst sll result digit count)))
1211 ;;;; Static functions.
1213 (define-static-fun two-arg-gcd (x y) :translate gcd)
1214 (define-static-fun two-arg-lcm (x y) :translate lcm)
1216 (define-static-fun two-arg-+ (x y) :translate +)
1217 (define-static-fun two-arg-- (x y) :translate -)
1218 (define-static-fun two-arg-* (x y) :translate *)
1219 (define-static-fun two-arg-/ (x y) :translate /)
1221 (define-static-fun two-arg-< (x y) :translate <)
1222 (define-static-fun two-arg-<= (x y) :translate <=)
1223 (define-static-fun two-arg-> (x y) :translate >)
1224 (define-static-fun two-arg->= (x y) :translate >=)
1225 (define-static-fun two-arg-= (x y) :translate =)
1226 (define-static-fun two-arg-/= (x y) :translate /=)
1228 (define-static-fun %negate (x) :translate %negate)
1230 (define-static-fun two-arg-and (x y) :translate logand)
1231 (define-static-fun two-arg-ior (x y) :translate logior)
1232 (define-static-fun two-arg-xor (x y) :translate logxor)
1235 ;; Need these so constant folding works with the deftransform.
1237 ;; FIXME KLUDGE ew yuk.
1240 (defun ash-right-signed (num shift)
1241 (ash-right-signed num shift))
1243 (defun ash-right-unsigned (num shuft)
1244 (ash-right-unsigned num shift)))
1248 ;;; If both arguments and the result are (UNSIGNED-BYTE 32), try to
1249 ;;; come up with a ``better'' multiplication using multiplier
1250 ;;; recoding. There are two different ways the multiplier can be
1251 ;;; recoded. The more obvious is to shift X by the correct amount for
1252 ;;; each bit set in Y and to sum the results. But if there is a string
1253 ;;; of bits that are all set, you can add X shifted by one more then
1254 ;;; the bit position of the first set bit and subtract X shifted by
1255 ;;; the bit position of the last set bit. We can't use this second
1256 ;;; method when the high order bit is bit 31 because shifting by 32
1257 ;;; doesn't work too well.
1258 (deftransform * ((x y)
1259 ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1261 "recode as shifts and adds"
1262 (let ((y (continuation-value y))
1267 (labels ((tub32 (x) `(truly-the (unsigned-byte 32) ,x))
1272 (progn (incf adds) `(+ ,result ,(tub32 next-factor)))
1274 (declare (inline add))
1275 (dotimes (bitpos 32)
1277 (when (not (logbitp bitpos y))
1278 (add (if (= (1+ first-one) bitpos)
1279 ;; There is only a single bit in the string.
1280 (progn (incf shifts) `(ash x ,first-one))
1281 ;; There are at least two.
1285 `(- ,(tub32 `(ash x ,bitpos))
1286 ,(tub32 `(ash x ,first-one))))))
1287 (setf first-one nil))
1288 (when (logbitp bitpos y)
1289 (setf first-one bitpos))))
1291 (cond ((= first-one 31))
1292 ((= first-one 30) (incf shifts) (add '(ash x 30)))
1296 (add `(- ,(tub32 '(ash x 31)) ,(tub32 `(ash x ,first-one))))))
1301 ;; we assume, perhaps foolishly, that good SPARCs don't have an
1302 ;; issue with multiplications. (Remember that there's a
1303 ;; different transform for converting x*2^k to a shift).
1304 ((member :sparc-64 *backend-subfeatures*) (give-up-ir1-transform))
1305 ((or (member :sparc-v9 *backend-subfeatures*)
1306 (member :sparc-v8 *backend-subfeatures*))
1307 ;; breakeven point as measured by Raymond Toy
1308 (when (> (+ adds shifts) 9)
1309 (give-up-ir1-transform))))
1313 ;; If we can prove that we have a right shift, just do the right shift
1314 ;; instead of calling the inline ASH which has to check for the
1315 ;; direction of the shift at run-time.
1316 (deftransform ash ((num shift) (integer integer))
1317 (let ((num-type (continuation-type num))
1318 (shift-type (continuation-type shift)))
1319 ;; Can only handle right shifts
1320 (unless (csubtypep shift-type (specifier-type '(integer * 0)))
1321 (give-up-ir1-transform))
1323 ;; If we can prove the shift is so large that all bits are shifted
1324 ;; out, return the appropriate constant. If the shift is small
1325 ;; enough, call the VOP. Otherwise, check for the shift size and
1326 ;; do the appropriate thing. (Hmm, could we just leave the IF
1327 ;; s-expr and depend on other parts of the compiler to delete the
1328 ;; unreachable parts, if any?)
1329 (cond ((csubtypep num-type (specifier-type '(signed-byte #.sb!vm:n-word-bits)))
1330 ;; A right shift by 31 is the same as a right shift by
1331 ;; larger amount. We get just the sign.
1332 (if (csubtypep shift-type (specifier-type '(integer #.(- 1 sb!vm:n-word-bits) 0)))
1333 ;; FIXME: ash-right-{un,}signed package problems
1334 `(sb!vm::ash-right-signed num (- shift))
1335 `(sb!vm::ash-right-signed num (min (- shift) #.(1- sb!vm:n-word-bits)))))
1336 ((csubtypep num-type (specifier-type '(unsigned-byte #.sb!vm:n-word-bits)))
1337 (if (csubtypep shift-type (specifier-type '(integer #.(- 1 sb!vm:n-word-bits) 0)))
1338 `(sb!vm::ash-right-unsigned num (- shift))
1339 `(if (<= shift #.(- sb!vm:n-word-bits))
1341 (sb!vm::ash-right-unsigned num (- shift)))))
1343 (give-up-ir1-transform)))))