1 ;;;; the VM definition arithmetic VOPs for the PPC
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 xori 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"))
83 (define-vop (fast-fixnum-binop-c fast-safe-arith-op)
84 (:args (x :target r :scs (any-reg zero)))
86 (:arg-types tagged-num
87 (:constant (and (signed-byte 14) (not (integer 0 0)))))
88 (:results (r :scs (any-reg)))
89 (:result-types tagged-num)
90 (:note "inline fixnum arithmetic"))
92 (define-vop (fast-fixnum-binop30-c fast-safe-arith-op)
93 (:args (x :target r :scs (any-reg zero)))
95 (:arg-types tagged-num
96 (:constant (and (signed-byte 30) (not (integer 0 0)))))
97 (:results (r :scs (any-reg)))
98 (:result-types tagged-num)
99 (:note "inline fixnum arithmetic"))
101 (define-vop (fast-fixnum-logop-c fast-safe-arith-op)
102 (:args (x :target r :scs (any-reg zero)))
104 (:arg-types tagged-num
105 (:constant (and (unsigned-byte 14) (not (integer 0 0)))))
106 (:results (r :scs (any-reg)))
107 (:result-types tagged-num)
108 (:note "inline fixnum logical op"))
110 (define-vop (fast-fixnum-logop30-c fast-safe-arith-op)
111 (:args (x :target r :scs (any-reg zero)))
113 (:arg-types tagged-num
114 (:constant (and (unsigned-byte 16) (not (integer 0 0)))))
115 (:results (r :scs (any-reg)))
116 (:result-types tagged-num)
117 (:note "inline fixnum logical op"))
119 (define-vop (fast-unsigned-binop-c fast-safe-arith-op)
120 (:args (x :target r :scs (unsigned-reg zero)))
122 (:arg-types unsigned-num
123 (:constant (and (signed-byte 16) (not (integer 0 0)))))
124 (:results (r :scs (unsigned-reg)))
125 (:result-types unsigned-num)
126 (:note "inline (unsigned-byte 32) arithmetic"))
128 (define-vop (fast-unsigned-binop32-c fast-safe-arith-op)
129 (:args (x :target r :scs (unsigned-reg zero)))
131 (:arg-types unsigned-num
132 (:constant (and (unsigned-byte 32) (not (integer 0 0)))))
133 (:results (r :scs (unsigned-reg)))
134 (:result-types unsigned-num)
135 (:note "inline (unsigned-byte 32) arithmetic"))
137 (define-vop (fast-signed-binop32-c fast-safe-arith-op)
138 (:args (x :target r :scs (signed-reg zero)))
140 (:arg-types signed-num
141 (:constant (and (signed-byte 32) (not (integer 0 0)))))
142 (:results (r :scs (signed-reg)))
143 (:result-types signed-num)
144 (:note "inline (signed-byte 32) arithmetic"))
146 (define-vop (fast-unsigned-logop-c fast-safe-arith-op)
147 (:args (x :target r :scs (unsigned-reg zero)))
149 (:arg-types unsigned-num
150 (:constant (and (unsigned-byte 16) (not (integer 0 0)))))
151 (:results (r :scs (unsigned-reg)))
152 (:result-types unsigned-num)
153 (:note "inline (unsigned-byte 32) logical op"))
155 (define-vop (fast-unsigned-logop32-c fast-safe-arith-op)
156 (:args (x :target r :scs (unsigned-reg zero)))
158 (:arg-types unsigned-num
159 (:constant (and (unsigned-byte 32) (not (integer 0 0)))))
160 (:results (r :scs (unsigned-reg)))
161 (:result-types unsigned-num)
162 (:note "inline (unsigned-byte 32) logical op"))
164 (define-vop (fast-signed-logop32-c fast-safe-arith-op)
165 (:args (x :target r :scs (signed-reg zero)))
167 (:arg-types signed-num
168 (:constant (and (unsigned-byte 32) (not (integer 0 0)))))
169 (:results (r :scs (signed-reg)))
170 (:result-types signed-num)
171 (:note "inline (signed-byte 32) logical op"))
173 (define-vop (fast-signed-binop-c fast-safe-arith-op)
174 (:args (x :target r :scs (signed-reg zero)))
176 (:arg-types signed-num
177 (:constant (and (signed-byte 16) (not (integer 0 0)))))
178 (:results (r :scs (signed-reg)))
179 (:result-types signed-num)
180 (:note "inline (signed-byte 32) arithmetic"))
182 (define-vop (fast-signed-logop-c fast-safe-arith-op)
183 (:args (x :target r :scs (signed-reg zero)))
185 (:arg-types signed-num
186 (:constant (and (unsigned-byte 16) (not (integer 0 0)))))
187 (:results (r :scs (signed-reg)))
188 (:result-types signed-num)
189 (:note "inline (signed-byte 32) logical op"))
191 (eval-when (:compile-toplevel :load-toplevel :execute)
193 (defmacro !define-var-binop (translate untagged-penalty op
194 &optional arg-swap restore-fixnum-mask)
196 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
198 ,@(when restore-fixnum-mask
199 `((:temporary (:sc non-descriptor-reg) temp)))
200 (:translate ,translate)
203 `(inst ,op ,(if restore-fixnum-mask 'temp 'r) y x)
204 `(inst ,op ,(if restore-fixnum-mask 'temp 'r) x y))
205 ;; FIXME: remind me what convention we used for 64bitizing
206 ;; stuff? -- CSR, 2003-08-27
207 ,@(when restore-fixnum-mask
208 `((inst clrrwi r temp (1- n-lowtag-bits))))))
209 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
211 (:translate ,translate)
212 (:generator ,(1+ untagged-penalty)
216 (define-vop (,(symbolicate "FAST-" translate "/UNSIGNED=>UNSIGNED")
218 (:translate ,translate)
219 (:generator ,(1+ untagged-penalty)
222 `(inst ,op r x y))))))
224 ;;; FIXME: the code has really only been checked for adds; we could do
225 ;;; subtracts, too, but my brain is not up to the task of figuring out
226 ;;; signs and borrows.
227 (defmacro !define-const-binop (translate untagged-penalty op &optional (shifted-op nil))
229 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
231 'fast-fixnum-binop30-c
232 'fast-fixnum-binop-c))
233 (:translate ,translate)
235 `((:temporary (:sc any-reg :target r) temp)))
238 `(let* ((y (fixnumize y))
239 (high-half (ldb (byte 16 16) y))
240 (low-half (ldb (byte 16 0) y)))
241 ;; Compare %LR in insts.lisp.
243 ((and (logbitp 15 low-half) (= high-half #xffff))
244 ;; Let sign-extension do the work for us, but make sure
245 ;; to turn LOW-HALF into a signed integer.
246 (inst ,op r x (dpb low-half (byte 16 0) -1)))
247 ((and (not (logbitp 15 low-half)) (zerop high-half))
248 (inst ,op r x low-half))
250 (inst ,shifted-op r x (if (logbitp 15 high-half)
251 (dpb high-half (byte 16 0) -1)
254 ;; Check to see whether compensating for the sign bit
255 ;; of LOW-HALF is necessary.
256 (let ((high-half (let ((top (if (logbitp 15 low-half)
261 (dpb top (byte 16 0) -1)
263 (inst ,shifted-op temp x high-half)
264 (inst ,op r temp low-half)))))
265 `(inst ,op r x (fixnumize y)))))
266 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
268 'fast-signed-binop32-c
269 'fast-signed-binop-c))
270 (:translate ,translate)
272 `((:temporary (:sc non-descriptor-reg :target r) temp)))
273 (:generator ,untagged-penalty
275 `(let ((high-half (ldb (byte 16 16) y))
276 (low-half (ldb (byte 16 0) y)))
277 ;; Compare %LR in insts.lisp.
279 ((and (logbitp 15 low-half) (= high-half #xffff))
280 ;; Let sign-extension do the work for us, but make sure
281 ;; to turn LOW-HALF into a signed integer.
282 (inst ,op r x (dpb low-half (byte 16 0) -1)))
283 ((and (not (logbitp 15 low-half)) (zerop high-half))
284 (inst ,op r x low-half))
286 (inst ,shifted-op r x (if (logbitp 15 high-half)
287 (dpb high-half (byte 16 0) -1)
290 ;; Check to see whether compensating for the sign bit
291 ;; of LOW-HALF is necessary.
292 (let ((high-half (let ((top (if (logbitp 15 low-half)
297 (dpb top (byte 16 0) -1)
299 (inst ,shifted-op temp x high-half)
300 (inst ,op r temp low-half)))))
302 (define-vop (,(symbolicate 'fast- translate '-c/unsigned=>unsigned)
304 'fast-unsigned-binop32-c
305 'fast-unsigned-binop-c))
306 (:translate ,translate)
308 `((:temporary (:sc non-descriptor-reg :target r) temp)))
309 (:generator ,untagged-penalty
311 `(let ((high-half (ldb (byte 16 16) y))
312 (low-half (ldb (byte 16 0) y)))
313 ;; Compare %LR in insts.lisp.
315 ((and (logbitp 15 low-half) (= high-half #xffff))
316 ;; Let sign-extension do the work for us, but make sure
317 ;; to turn LOW-HALF into a signed integer.
318 (inst ,op r x (dpb low-half (byte 16 0) -1)))
319 ((and (not (logbitp 15 low-half)) (zerop high-half))
320 (inst ,op r x low-half))
322 (inst ,shifted-op r x (if (logbitp 15 high-half)
323 (dpb high-half (byte 16 0) -1)
326 ;; Check to see whether compensating for the sign bit
327 ;; of LOW-HALF is necessary.
328 (let ((high-half (let ((top (if (logbitp 15 low-half)
333 (dpb top (byte 16 0) -1)
335 (inst ,shifted-op temp x high-half)
336 (inst ,op r temp low-half)))))
337 `(inst ,op r x y))))))
339 ;;; For logical operations, we don't have to worry about signed bit
340 ;;; propagation from the lower half of a 32-bit operand.
341 (defmacro !define-const-logop (translate untagged-penalty op &optional (shifted-op nil))
343 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
345 'fast-fixnum-logop30-c
346 'fast-fixnum-logop-c))
347 (:translate ,translate)
349 `((:temporary (:sc any-reg :target r) temp)))
352 `(let* ((y (fixnumize y))
353 (high-half (ldb (byte 16 16) y))
354 (low-half (ldb (byte 16 0) y)))
356 ((zerop high-half) (inst ,op r x low-half))
357 ((zerop low-half) (inst ,shifted-op r x high-half))
359 (inst ,shifted-op temp x high-half)
360 (inst ,op r temp low-half))))
361 `(inst ,op r x (fixnumize y)))))
362 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
364 'fast-signed-logop32-c
365 'fast-signed-logop-c))
366 (:translate ,translate)
368 `((:temporary (:sc non-descriptor-reg :target r) temp)))
369 (:generator ,untagged-penalty
371 `(let ((high-half (ldb (byte 16 16) y))
372 (low-half (ldb (byte 16 0) y)))
374 ((zerop high-half) (inst ,op r x low-half))
375 ((zerop low-half) (inst ,shifted-op r x high-half))
377 (inst ,shifted-op temp x high-half)
378 (inst ,op r temp low-half))))
380 (define-vop (,(symbolicate 'fast- translate '-c/unsigned=>unsigned)
382 'fast-unsigned-logop32-c
383 'fast-unsigned-logop-c))
384 (:translate ,translate)
386 `((:temporary (:sc non-descriptor-reg :target r) temp)))
387 (:generator ,untagged-penalty
389 `(let ((high-half (ldb (byte 16 16) y))
390 (low-half (ldb (byte 16 0) y)))
392 ((zerop high-half) (inst ,op r x low-half))
393 ((zerop low-half) (inst ,shifted-op r x high-half))
395 (inst ,shifted-op temp x high-half)
396 (inst ,op r temp low-half))))
397 `(inst ,op r x y))))))
401 (!define-var-binop + 4 add)
402 (!define-var-binop - 4 sub)
403 (!define-var-binop logand 2 and)
404 (!define-var-binop logandc1 2 andc t)
405 (!define-var-binop logandc2 2 andc)
406 (!define-var-binop logior 2 or)
407 (!define-var-binop logorc1 2 orc t t)
408 (!define-var-binop logorc2 2 orc nil t)
409 (!define-var-binop logxor 2 xor)
410 (!define-var-binop logeqv 2 eqv nil t)
411 (!define-var-binop lognand 2 nand nil t)
412 (!define-var-binop lognor 2 nor nil t)
414 (!define-const-binop + 4 addi addis)
415 (!define-const-binop - 4 subi)
416 ;;; Implementing a 32-bit immediate version of LOGAND wouldn't be any
417 ;;; better than loading the 32-bit constant via LR and then performing
418 ;;; an /AND/. So don't bother. (It would be better in some cases, such
419 ;;; as when one half of the word is zeros--we save a register--but we
420 ;;; would have specified one temporary register in the VOP, so we lose
421 ;;; any possible advantage.)
422 (!define-const-logop logand 2 andi.)
423 (!define-const-logop logior 2 ori oris)
424 (!define-const-logop logxor 2 xori xoris)
427 ;;; Special case fixnum + and - that trap on overflow. Useful when we
428 ;;; don't know that the output type is a fixnum.
430 (define-vop (+/fixnum fast-+/fixnum=>fixnum)
432 (:results (r :scs (any-reg descriptor-reg)))
433 (:result-types tagged-num)
434 (:note "safe inline fixnum arithmetic")
436 (let* ((no-overflow (gen-label)))
439 (inst bns no-overflow)
440 (inst unimp (logior (ash (reg-tn-encoding r) 5)
441 fixnum-additive-overflow-trap))
442 (emit-label no-overflow))))
444 (define-vop (-/fixnum fast--/fixnum=>fixnum)
446 (:results (r :scs (any-reg descriptor-reg)))
447 (:result-types tagged-num)
448 (:note "safe inline fixnum arithmetic")
450 (let* ((no-overflow (gen-label)))
453 (inst bns no-overflow)
454 (inst unimp (logior (ash (reg-tn-encoding r) 5)
455 fixnum-additive-overflow-trap))
456 (emit-label no-overflow))))
458 (define-vop (fast-*/fixnum=>fixnum fast-fixnum-binop)
459 (:temporary (:scs (non-descriptor-reg)) temp)
462 (inst srawi temp y 2)
463 (inst mullw r x temp)))
465 (define-vop (fast-*-c/fixnum=>fixnum fast-fixnum-binop-c)
467 (:arg-types tagged-num
468 (:constant (and (signed-byte 16) (not (integer 0 0)))))
472 (define-vop (fast-*-bigc/fixnum=>fixnum fast-fixnum-binop-c)
474 (:arg-types tagged-num
475 (:constant (and fixnum (not (signed-byte 16)))))
476 (:temporary (:scs (non-descriptor-reg)) temp)
479 (inst mullw r x temp)))
481 (define-vop (fast-*/signed=>signed fast-signed-binop)
486 (define-vop (fast-*-c/signed=>signed fast-signed-binop-c)
491 (define-vop (fast-*/unsigned=>unsigned fast-unsigned-binop)
496 (define-vop (fast-*-c/unsigned=>unsigned fast-unsigned-binop-c)
503 (macrolet ((def (name sc-type type result-type cost)
507 (:args (number :scs (,sc-type))
508 (amount :scs (signed-reg unsigned-reg immediate)))
509 (:arg-types ,type positive-fixnum)
510 (:results (result :scs (,result-type)))
511 (:result-types ,type)
515 ((signed-reg unsigned-reg)
516 (inst slw result number amount))
518 (let ((amount (tn-value amount)))
520 (inst slwi result number amount))))))))
521 ;; FIXME: There's the opportunity for a sneaky optimization here, I
522 ;; think: a FAST-ASH-LEFT-C/FIXNUM=>SIGNED vop. -- CSR, 2003-09-03
523 (def fast-ash-left/fixnum=>fixnum any-reg tagged-num any-reg 2)
524 (def fast-ash-left/signed=>signed signed-reg signed-num signed-reg 3)
525 (def fast-ash-left/unsigned=>unsigned unsigned-reg unsigned-num unsigned-reg 3))
527 (define-vop (fast-ash/unsigned=>unsigned)
529 (:args (number :scs (unsigned-reg) :to :save)
530 (amount :scs (signed-reg)))
531 (:arg-types (:or unsigned-num) signed-num)
532 (:results (result :scs (unsigned-reg)))
533 (:result-types unsigned-num)
536 (:temporary (:sc non-descriptor-reg) ndesc)
538 (let ((positive (gen-label))
540 (inst cmpwi amount 0)
541 (inst neg ndesc amount)
543 (inst cmpwi ndesc 31)
544 (inst srw result number ndesc)
546 (move result zero-tn)
549 (emit-label positive)
550 ;; The result-type assures us that this shift will not overflow.
551 (inst slw result number amount)
555 (define-vop (fast-ash-c/unsigned=>unsigned)
556 (:note "inline constant ASH")
557 (:args (number :scs (unsigned-reg)))
559 (:arg-types unsigned-num (:constant integer))
560 (:results (result :scs (unsigned-reg)))
561 (:result-types unsigned-num)
566 ((and (minusp amount) (< amount -31)) (move result zero-tn))
567 ((minusp amount) (inst srwi result number (- amount)))
568 ;; possible because this is used in the modular version too
569 ((> amount 31) (move result zero-tn))
570 (t (inst slwi result number amount)))))
572 (define-vop (fast-ash/signed=>signed)
574 (:args (number :scs (signed-reg) :to :save)
575 (amount :scs (signed-reg immediate)))
576 (:arg-types (:or signed-num) signed-num)
577 (:results (result :scs (signed-reg)))
578 (:result-types (:or signed-num))
581 (:temporary (:sc non-descriptor-reg) ndesc)
585 (let ((positive (gen-label))
587 (inst cmpwi amount 0)
588 (inst neg ndesc amount)
590 (inst cmpwi ndesc 31)
591 (inst sraw result number ndesc)
593 (inst srawi result number 31)
596 (emit-label positive)
597 ;; The result-type assures us that this shift will not overflow.
598 (inst slw result number amount)
603 (let ((amount (tn-value amount)))
605 (let ((amount (min 31 (- amount))))
606 (inst srawi result number amount))
607 (inst slwi result number amount)))))))
611 (define-vop (signed-byte-32-len)
612 (:translate integer-length)
613 (:note "inline (signed-byte 32) integer-length")
615 (:args (arg :scs (signed-reg)))
616 (:arg-types signed-num)
617 (:results (res :scs (unsigned-reg)))
618 (:result-types unsigned-num)
620 ; (integer-length arg) = (- 32 (cntlz (if (>= arg 0) arg (lognot arg))))
621 (let ((nonneg (gen-label)))
622 (inst cntlzw. res arg)
625 (inst cntlzw res res)
627 (inst subfic res res 32))))
629 (define-vop (unsigned-byte-32-len)
630 (:translate integer-length)
631 (:note "inline (unsigned-byte 32) integer-length")
633 (:args (arg :scs (unsigned-reg)))
634 (:arg-types unsigned-num)
635 (:results (res :scs (unsigned-reg)))
636 (:result-types unsigned-num)
638 (inst cntlzw res arg)
639 (inst subfic res res 32)))
641 (define-vop (unsigned-byte-32-count)
642 (:translate logcount)
643 (:note "inline (unsigned-byte 32) logcount")
645 (:args (arg :scs (unsigned-reg) :target shift))
646 (:arg-types unsigned-num)
647 (:results (res :scs (any-reg)))
648 (:result-types positive-fixnum)
649 (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) shift temp)
651 (let ((loop (gen-label))
653 (inst add. shift zero-tn arg)
658 (inst subi temp shift 1)
659 (inst and. shift shift temp)
660 (inst addi res res (fixnumize 1))
666 ;;;; Modular functions:
667 (define-modular-fun lognot-mod32 (x) lognot :unsigned 32)
668 (define-vop (lognot-mod32/unsigned=>unsigned)
669 (:translate lognot-mod32)
670 (:args (x :scs (unsigned-reg)))
671 (:arg-types unsigned-num)
672 (:results (res :scs (unsigned-reg)))
673 (:result-types unsigned-num)
678 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
679 fast-ash-c/unsigned=>unsigned)
680 (:translate ash-left-mod32))
682 (define-vop (fast-ash-left-mod32/unsigned=>unsigned
683 fast-ash-left/unsigned=>unsigned))
684 (deftransform ash-left-mod32 ((integer count)
685 ((unsigned-byte 32) (unsigned-byte 5)))
686 (when (sb!c::constant-lvar-p count)
687 (sb!c::give-up-ir1-transform))
688 '(%primitive fast-ash-left-mod32/unsigned=>unsigned integer count))
691 ((define-modular-backend (fun &optional constantp)
692 (let ((mfun-name (symbolicate fun '-mod32))
693 (modvop (symbolicate 'fast- fun '-mod32/unsigned=>unsigned))
694 (modcvop (symbolicate 'fast- fun 'mod32-c/unsigned=>unsigned))
695 (vop (symbolicate 'fast- fun '/unsigned=>unsigned))
696 (cvop (symbolicate 'fast- fun '-c/unsigned=>unsigned)))
698 (define-modular-fun ,mfun-name (x y) ,fun :unsigned 32)
699 (define-vop (,modvop ,vop)
700 (:translate ,mfun-name))
702 `((define-vop (,modcvop ,cvop)
703 (:translate ,mfun-name))))))))
704 (define-modular-backend + t)
705 (define-modular-backend - t)
706 (define-modular-backend * t)
707 (define-modular-backend logxor t)
708 (define-modular-backend logeqv)
709 (define-modular-backend lognand)
710 (define-modular-backend lognor)
711 (define-modular-backend logandc1)
712 (define-modular-backend logandc2)
713 (define-modular-backend logorc1)
714 (define-modular-backend logorc2))
716 ;;;; Binary conditional VOPs:
718 (define-vop (fast-conditional)
723 (:policy :fast-safe))
725 (define-vop (fast-conditional/fixnum fast-conditional)
726 (:args (x :scs (any-reg zero))
727 (y :scs (any-reg zero)))
728 (:arg-types tagged-num tagged-num)
729 (:note "inline fixnum comparison"))
731 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
732 (:args (x :scs (any-reg zero)))
733 (:arg-types tagged-num (:constant (signed-byte 14)))
734 (:info target not-p y))
736 (define-vop (fast-conditional/signed fast-conditional)
737 (:args (x :scs (signed-reg zero))
738 (y :scs (signed-reg zero)))
739 (:arg-types signed-num signed-num)
740 (:note "inline (signed-byte 32) comparison"))
742 (define-vop (fast-conditional-c/signed fast-conditional/signed)
743 (:args (x :scs (signed-reg zero)))
744 (:arg-types signed-num (:constant (signed-byte 16)))
745 (:info target not-p y))
747 (define-vop (fast-conditional/unsigned fast-conditional)
748 (:args (x :scs (unsigned-reg zero))
749 (y :scs (unsigned-reg zero)))
750 (:arg-types unsigned-num unsigned-num)
751 (:note "inline (unsigned-byte 32) comparison"))
753 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
754 (:args (x :scs (unsigned-reg zero)))
755 (:arg-types unsigned-num (:constant (unsigned-byte 16)))
756 (:info target not-p y))
758 (define-vop (fast-if-</fixnum fast-conditional/fixnum)
762 (inst b? (if not-p :ge :lt) target)))
764 (define-vop (fast-if-<-c/fixnum fast-conditional-c/fixnum)
767 (inst cmpwi x (fixnumize y))
768 (inst b? (if not-p :ge :lt) target)))
770 (define-vop (fast-if-</signed fast-conditional/signed)
774 (inst b? (if not-p :ge :lt) target)))
776 (define-vop (fast-if-<-c/signed fast-conditional-c/signed)
780 (inst b? (if not-p :ge :lt) target)))
782 (define-vop (fast-if-</unsigned fast-conditional/unsigned)
786 (inst b? (if not-p :ge :lt) target)))
788 (define-vop (fast-if-<-c/unsigned fast-conditional-c/unsigned)
792 (inst b? (if not-p :ge :lt) target)))
794 (define-vop (fast-if->/fixnum fast-conditional/fixnum)
798 (inst b? (if not-p :le :gt) target)))
800 (define-vop (fast-if->-c/fixnum fast-conditional-c/fixnum)
803 (inst cmpwi x (fixnumize y))
804 (inst b? (if not-p :le :gt) target)))
806 (define-vop (fast-if->/signed fast-conditional/signed)
810 (inst b? (if not-p :le :gt) target)))
812 (define-vop (fast-if->-c/signed fast-conditional-c/signed)
816 (inst b? (if not-p :le :gt) target)))
818 (define-vop (fast-if->/unsigned fast-conditional/unsigned)
822 (inst b? (if not-p :le :gt) target)))
824 (define-vop (fast-if->-c/unsigned fast-conditional-c/unsigned)
828 (inst b? (if not-p :le :gt) target)))
830 (define-vop (fast-if-eql/signed fast-conditional/signed)
834 (inst b? (if not-p :ne :eq) target)))
836 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
840 (inst b? (if not-p :ne :eq) target)))
842 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
846 (inst b? (if not-p :ne :eq) target)))
848 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
852 (inst b? (if not-p :ne :eq) target)))
855 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
858 ;;; These versions specify a fixnum restriction on their first arg. We have
859 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
860 ;;; the first arg and a higher cost. The reason for doing this is to prevent
861 ;;; fixnum specific operations from being used on word integers, spuriously
862 ;;; consing the argument.
865 (define-vop (fast-eql/fixnum fast-conditional)
866 (:args (x :scs (any-reg descriptor-reg zero))
867 (y :scs (any-reg zero)))
868 (:arg-types tagged-num tagged-num)
869 (:note "inline fixnum comparison")
873 (inst b? (if not-p :ne :eq) target)))
875 (define-vop (generic-eql/fixnum fast-eql/fixnum)
876 (:arg-types * tagged-num)
879 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
880 (:args (x :scs (any-reg descriptor-reg zero)))
881 (:arg-types tagged-num (:constant (signed-byte 14)))
882 (:info target not-p y)
885 (inst cmpwi x (fixnumize y))
886 (inst b? (if not-p :ne :eq) target)))
888 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
889 (:arg-types * (:constant (signed-byte 11)))
893 ;;;; 32-bit logical operations
895 (define-vop (merge-bits)
896 (:translate merge-bits)
897 (:args (shift :scs (signed-reg unsigned-reg))
898 (prev :scs (unsigned-reg))
899 (next :scs (unsigned-reg)))
900 (:arg-types tagged-num unsigned-num unsigned-num)
901 (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
902 (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
903 (:results (result :scs (unsigned-reg)))
904 (:result-types unsigned-num)
907 (let ((done (gen-label)))
910 (inst srw res next shift)
911 (inst sub temp zero-tn shift)
912 (inst slw temp prev temp)
913 (inst or res res temp)
917 (define-vop (shift-towards-someplace)
919 (:args (num :scs (unsigned-reg))
920 (amount :scs (signed-reg)))
921 (:arg-types unsigned-num tagged-num)
922 (:results (r :scs (unsigned-reg)))
923 (:result-types unsigned-num))
925 (define-vop (shift-towards-start shift-towards-someplace)
926 (:translate shift-towards-start)
927 (:note "shift-towards-start")
929 (inst rlwinm amount amount 0 27 31)
930 (inst slw r num amount)))
932 (define-vop (shift-towards-end shift-towards-someplace)
933 (:translate shift-towards-end)
934 (:note "shift-towards-end")
936 (inst rlwinm amount amount 0 27 31)
937 (inst srw r num amount)))
941 (define-vop (bignum-length get-header-data)
942 (:translate sb!bignum:%bignum-length)
943 (:policy :fast-safe))
945 (define-vop (bignum-set-length set-header-data)
946 (:translate sb!bignum:%bignum-set-length)
947 (:policy :fast-safe))
949 (define-vop (bignum-ref word-index-ref)
950 (:variant bignum-digits-offset other-pointer-lowtag)
951 (:translate sb!bignum:%bignum-ref)
952 (:results (value :scs (unsigned-reg)))
953 (:result-types unsigned-num))
955 (define-vop (bignum-set word-index-set)
956 (:variant bignum-digits-offset other-pointer-lowtag)
957 (:translate sb!bignum:%bignum-set)
958 (:args (object :scs (descriptor-reg))
959 (index :scs (any-reg immediate zero))
960 (value :scs (unsigned-reg)))
961 (:arg-types t positive-fixnum unsigned-num)
962 (:results (result :scs (unsigned-reg)))
963 (:result-types unsigned-num))
965 (define-vop (digit-0-or-plus)
966 (:translate sb!bignum:%digit-0-or-plusp)
968 (:args (digit :scs (unsigned-reg)))
969 (:arg-types unsigned-num)
970 (:results (result :scs (descriptor-reg)))
972 (let ((done (gen-label)))
974 (move result null-tn)
976 (load-symbol result t)
979 (define-vop (add-w/carry)
980 (:translate sb!bignum:%add-with-carry)
982 (:args (a :scs (unsigned-reg))
983 (b :scs (unsigned-reg))
985 (:arg-types unsigned-num unsigned-num positive-fixnum)
986 (:temporary (:scs (unsigned-reg)) temp)
987 (:results (result :scs (unsigned-reg))
988 (carry :scs (unsigned-reg)))
989 (:result-types unsigned-num positive-fixnum)
991 (inst addic temp c -1)
992 (inst adde result a b)
993 (inst addze carry zero-tn)))
995 (define-vop (sub-w/borrow)
996 (:translate sb!bignum:%subtract-with-borrow)
998 (:args (a :scs (unsigned-reg))
999 (b :scs (unsigned-reg))
1001 (:arg-types unsigned-num unsigned-num positive-fixnum)
1002 (:temporary (:scs (unsigned-reg)) temp)
1003 (:results (result :scs (unsigned-reg))
1004 (borrow :scs (unsigned-reg)))
1005 (:result-types unsigned-num positive-fixnum)
1007 (inst addic temp c -1)
1008 (inst sube result a b)
1009 (inst addze borrow zero-tn)))
1011 (define-vop (bignum-mult-and-add-3-arg)
1012 (:translate sb!bignum:%multiply-and-add)
1013 (:policy :fast-safe)
1014 (:args (x :scs (unsigned-reg))
1015 (y :scs (unsigned-reg))
1016 (carry-in :scs (unsigned-reg) :to (:eval 1)))
1017 (:arg-types unsigned-num unsigned-num unsigned-num)
1018 (:temporary (:scs (unsigned-reg) :to (:result 0) :target hi) hi-temp)
1019 (:temporary (:scs (unsigned-reg) :from (:eval 0) :to (:result 1)
1020 :target lo) lo-temp)
1021 (:results (hi :scs (unsigned-reg))
1022 (lo :scs (unsigned-reg)))
1023 (:result-types unsigned-num unsigned-num)
1025 (inst mulhwu hi-temp x y)
1026 (inst mullw lo-temp x y)
1027 (inst addc lo lo-temp carry-in)
1028 (inst addze hi hi-temp)))
1030 (define-vop (bignum-mult-and-add-4-arg)
1031 (:translate sb!bignum:%multiply-and-add)
1032 (:policy :fast-safe)
1033 (:args (x :scs (unsigned-reg))
1034 (y :scs (unsigned-reg))
1035 (prev :scs (unsigned-reg) :to (:eval 1))
1036 (carry-in :scs (unsigned-reg) :to (:eval 1)))
1037 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1038 (:temporary (:scs (unsigned-reg) :to (:result 0) :target hi) hi-temp)
1039 (:temporary (:scs (unsigned-reg) :from (:eval 0) :to (:result 1)
1040 :target lo) lo-temp)
1041 (:results (hi :scs (unsigned-reg))
1042 (lo :scs (unsigned-reg)))
1043 (:result-types unsigned-num unsigned-num)
1045 (inst mulhwu hi-temp x y)
1046 (inst mullw lo-temp x y)
1047 (inst addc lo-temp lo-temp carry-in)
1048 (inst addze hi-temp hi-temp)
1049 (inst addc lo lo-temp prev)
1050 (inst addze hi hi-temp)))
1052 (define-vop (bignum-mult)
1053 (:translate sb!bignum:%multiply)
1054 (:policy :fast-safe)
1055 (:args (x :scs (unsigned-reg) :to (:eval 1))
1056 (y :scs (unsigned-reg) :to (:eval 1)))
1057 (:arg-types unsigned-num unsigned-num)
1058 (:results (hi :scs (unsigned-reg) :from (:eval 1))
1059 (lo :scs (unsigned-reg) :from (:eval 0)))
1060 (:result-types unsigned-num unsigned-num)
1063 (inst mulhwu hi x y)))
1065 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
1066 (:translate sb!bignum:%lognot))
1068 (define-vop (fixnum-to-digit)
1069 (:translate sb!bignum:%fixnum-to-digit)
1070 (:policy :fast-safe)
1071 (:args (fixnum :scs (any-reg)))
1072 (:arg-types tagged-num)
1073 (:results (digit :scs (unsigned-reg)))
1074 (:result-types unsigned-num)
1076 (inst srawi digit fixnum 2)))
1079 (define-vop (bignum-floor)
1080 (:translate sb!bignum:%floor)
1081 (:policy :fast-safe)
1082 (:args (num-high :scs (unsigned-reg) :target rem)
1083 (num-low :scs (unsigned-reg) :target rem-low)
1084 (denom :scs (unsigned-reg) :to (:eval 1)))
1085 (:arg-types unsigned-num unsigned-num unsigned-num)
1086 (:temporary (:scs (unsigned-reg) :from (:argument 1)) rem-low)
1087 (:temporary (:scs (unsigned-reg) :from (:eval 0)) temp)
1088 (:results (quo :scs (unsigned-reg) :from (:eval 0))
1089 (rem :scs (unsigned-reg) :from (:argument 0)))
1090 (:result-types unsigned-num unsigned-num)
1091 (:generator 325 ; number of inst assuming targeting works.
1093 (move rem-low num-low)
1094 (flet ((maybe-subtract (&optional (guess temp))
1095 (inst subi temp guess 1)
1096 (inst and temp temp denom)
1097 (inst sub rem rem temp))
1099 (inst subfc res y x)
1100 (inst subfe res res res)
1101 (inst neg res res)))
1102 (sltu quo rem denom)
1103 (maybe-subtract quo)
1105 (inst slwi rem rem 1)
1106 (inst srwi temp rem-low 31)
1107 (inst or rem rem temp)
1108 (inst slwi rem-low rem-low 1)
1109 (sltu temp rem denom)
1110 (inst slwi quo quo 1)
1111 (inst or quo quo temp)
1113 (inst not quo quo)))
1117 (define-vop (bignum-floor)
1118 (:translate sb!bignum:%floor)
1119 (:policy :fast-safe)
1120 (:args (div-high :scs (unsigned-reg) :target rem)
1121 (div-low :scs (unsigned-reg) :target quo)
1122 (divisor :scs (unsigned-reg)))
1123 (:arg-types unsigned-num unsigned-num unsigned-num)
1124 (:results (quo :scs (unsigned-reg) :from (:argument 1))
1125 (rem :scs (unsigned-reg) :from (:argument 0)))
1126 (:result-types unsigned-num unsigned-num)
1129 (inst div quo div-high divisor)
1133 (define-vop (signify-digit)
1134 (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1135 (:policy :fast-safe)
1136 (:args (digit :scs (unsigned-reg) :target res))
1137 (:arg-types unsigned-num)
1138 (:results (res :scs (any-reg signed-reg)))
1139 (:result-types signed-num)
1143 (inst slwi res digit 2))
1145 (move res digit)))))
1148 (define-vop (digit-ashr)
1149 (:translate sb!bignum:%ashr)
1150 (:policy :fast-safe)
1151 (:args (digit :scs (unsigned-reg))
1152 (count :scs (unsigned-reg)))
1153 (:arg-types unsigned-num positive-fixnum)
1154 (:results (result :scs (unsigned-reg)))
1155 (:result-types unsigned-num)
1157 (inst sraw result digit count)))
1159 (define-vop (digit-lshr digit-ashr)
1160 (:translate sb!bignum:%digit-logical-shift-right)
1162 (inst srw result digit count)))
1164 (define-vop (digit-ashl digit-ashr)
1165 (:translate sb!bignum:%ashl)
1167 (inst slw result digit count)))
1172 (define-static-fun two-arg-gcd (x y) :translate gcd)
1173 (define-static-fun two-arg-lcm (x y) :translate lcm)
1175 (define-static-fun two-arg-+ (x y) :translate +)
1176 (define-static-fun two-arg-- (x y) :translate -)
1177 (define-static-fun two-arg-* (x y) :translate *)
1178 (define-static-fun two-arg-/ (x y) :translate /)
1180 (define-static-fun two-arg-< (x y) :translate <)
1181 (define-static-fun two-arg-<= (x y) :translate <=)
1182 (define-static-fun two-arg-> (x y) :translate >)
1183 (define-static-fun two-arg->= (x y) :translate >=)
1184 (define-static-fun two-arg-= (x y) :translate =)
1185 (define-static-fun two-arg-/= (x y) :translate /=)
1187 (define-static-fun %negate (x) :translate %negate)
1189 (define-static-fun two-arg-and (x y) :translate logand)
1190 (define-static-fun two-arg-ior (x y) :translate logior)
1191 (define-static-fun two-arg-xor (x y) :translate logxor)
1192 (define-static-fun two-arg-eqv (x y) :translate logeqv)
1196 (deftransform * ((x y)
1197 ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1199 "recode as shifts and adds"
1200 (let ((y (lvar-value y)))
1201 (multiple-value-bind (result adds shifts)
1202 (ub32-strength-reduce-constant-multiply 'x y)
1204 ((typep y '(signed-byte 16))
1205 ;; a mulli instruction has a latency of 5.
1206 (when (> (+ adds shifts) 4)
1207 (give-up-ir1-transform)))
1209 ;; a mullw instruction also has a latency of 5, plus two
1210 ;; instructions (in general) to load the immediate into a
1212 (when (> (+ adds shifts) 6)
1213 (give-up-ir1-transform))))