1 ;;;; the VM definition of arithmetic VOPs for the x86
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
16 (define-vop (fast-safe-arith-op)
21 (define-vop (fixnum-unop fast-safe-arith-op)
22 (:args (x :scs (any-reg) :target res))
23 (:results (res :scs (any-reg)))
24 (:note "inline fixnum arithmetic")
25 (:arg-types tagged-num)
26 (:result-types tagged-num))
28 (define-vop (signed-unop fast-safe-arith-op)
29 (:args (x :scs (signed-reg) :target res))
30 (:results (res :scs (signed-reg)))
31 (:note "inline (signed-byte 32) arithmetic")
32 (:arg-types signed-num)
33 (:result-types signed-num))
35 (define-vop (fast-negate/fixnum fixnum-unop)
41 (define-vop (fast-negate/signed signed-unop)
47 (define-vop (fast-lognot/fixnum fixnum-unop)
51 (inst xor res (fixnumize -1))))
53 (define-vop (fast-lognot/signed signed-unop)
59 ;;;; binary fixnum operations
61 ;;; Assume that any constant operand is the second arg...
63 (define-vop (fast-fixnum-binop fast-safe-arith-op)
64 (:args (x :target r :scs (any-reg)
65 :load-if (not (and (sc-is x control-stack)
67 (sc-is r control-stack)
69 (y :scs (any-reg control-stack)))
70 (:arg-types tagged-num tagged-num)
71 (:results (r :scs (any-reg) :from (:argument 0)
72 :load-if (not (and (sc-is x control-stack)
74 (sc-is r control-stack)
76 (:result-types tagged-num)
77 (:note "inline fixnum arithmetic"))
79 (define-vop (fast-unsigned-binop fast-safe-arith-op)
80 (:args (x :target r :scs (unsigned-reg)
81 :load-if (not (and (sc-is x unsigned-stack)
82 (sc-is y unsigned-reg)
83 (sc-is r unsigned-stack)
85 (y :scs (unsigned-reg unsigned-stack)))
86 (:arg-types unsigned-num unsigned-num)
87 (:results (r :scs (unsigned-reg) :from (:argument 0)
88 :load-if (not (and (sc-is x unsigned-stack)
89 (sc-is y unsigned-reg)
90 (sc-is r unsigned-stack)
92 (:result-types unsigned-num)
93 (:note "inline (unsigned-byte 32) arithmetic"))
95 (define-vop (fast-signed-binop fast-safe-arith-op)
96 (:args (x :target r :scs (signed-reg)
97 :load-if (not (and (sc-is x signed-stack)
99 (sc-is r signed-stack)
101 (y :scs (signed-reg signed-stack)))
102 (:arg-types signed-num signed-num)
103 (:results (r :scs (signed-reg) :from (:argument 0)
104 :load-if (not (and (sc-is x signed-stack)
106 (sc-is r signed-stack)
108 (:result-types signed-num)
109 (:note "inline (signed-byte 32) arithmetic"))
111 (define-vop (fast-fixnum-binop-c fast-safe-arith-op)
112 (:args (x :target r :scs (any-reg control-stack)))
114 (:arg-types tagged-num (:constant (signed-byte 30)))
115 (:results (r :scs (any-reg)
116 :load-if (not (location= x r))))
117 (:result-types tagged-num)
118 (:note "inline fixnum arithmetic"))
120 (define-vop (fast-unsigned-binop-c fast-safe-arith-op)
121 (:args (x :target r :scs (unsigned-reg unsigned-stack)))
123 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
124 (:results (r :scs (unsigned-reg)
125 :load-if (not (location= x r))))
126 (:result-types unsigned-num)
127 (:note "inline (unsigned-byte 32) arithmetic"))
129 (define-vop (fast-signed-binop-c fast-safe-arith-op)
130 (:args (x :target r :scs (signed-reg signed-stack)))
132 (:arg-types signed-num (:constant (signed-byte 32)))
133 (:results (r :scs (signed-reg)
134 :load-if (not (location= x r))))
135 (:result-types signed-num)
136 (:note "inline (signed-byte 32) arithmetic"))
138 (macrolet ((define-binop (translate untagged-penalty op)
140 (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
142 (:translate ,translate)
146 (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
148 (:translate ,translate)
151 (inst ,op r (fixnumize y))))
152 (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
154 (:translate ,translate)
155 (:generator ,(1+ untagged-penalty)
158 (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
160 (:translate ,translate)
161 (:generator ,untagged-penalty
164 (define-vop (,(symbolicate "FAST-"
166 "/UNSIGNED=>UNSIGNED")
168 (:translate ,translate)
169 (:generator ,(1+ untagged-penalty)
172 (define-vop (,(symbolicate 'fast-
174 '-c/unsigned=>unsigned)
175 fast-unsigned-binop-c)
176 (:translate ,translate)
177 (:generator ,untagged-penalty
179 ,(if (eq translate 'logand)
180 ;; for the -C/UNSIGNED=>UNSIGNED VOP, this case
181 ;; is optimized away as an identity somewhere
182 ;; along the lines. However, this VOP is used in
183 ;; -C/SIGNED=>UNSIGNED, below, when the
184 ;; higher-level lisp code can't optimize away the
185 ;; non-trivial identity.
186 `(unless (= y #.(1- (ash 1 n-word-bits)))
188 `(inst ,op r y)))))))
189 (define-binop - 4 sub)
190 (define-binop logand 2 and)
191 (define-binop logior 2 or)
192 (define-binop logxor 2 xor))
194 ;;; Special handling of add on the x86; can use lea to avoid a
195 ;;; register load, otherwise it uses add.
196 (define-vop (fast-+/fixnum=>fixnum fast-safe-arith-op)
198 (:args (x :scs (any-reg) :target r
199 :load-if (not (and (sc-is x control-stack)
201 (sc-is r control-stack)
203 (y :scs (any-reg control-stack)))
204 (:arg-types tagged-num tagged-num)
205 (:results (r :scs (any-reg) :from (:argument 0)
206 :load-if (not (and (sc-is x control-stack)
208 (sc-is r control-stack)
210 (:result-types tagged-num)
211 (:note "inline fixnum arithmetic")
213 (cond ((and (sc-is x any-reg) (sc-is y any-reg) (sc-is r any-reg)
214 (not (location= x r)))
215 (inst lea r (make-ea :dword :base x :index y :scale 1)))
220 (define-vop (fast-+-c/fixnum=>fixnum fast-safe-arith-op)
222 (:args (x :target r :scs (any-reg control-stack)))
224 (:arg-types tagged-num (:constant (signed-byte 30)))
225 (:results (r :scs (any-reg)
226 :load-if (not (location= x r))))
227 (:result-types tagged-num)
228 (:note "inline fixnum arithmetic")
230 (cond ((and (sc-is x any-reg) (sc-is r any-reg) (not (location= x r)))
231 (inst lea r (make-ea :dword :base x :disp (fixnumize y))))
234 (inst add r (fixnumize y))))))
236 (define-vop (fast-+/signed=>signed fast-safe-arith-op)
238 (:args (x :scs (signed-reg) :target r
239 :load-if (not (and (sc-is x signed-stack)
241 (sc-is r signed-stack)
243 (y :scs (signed-reg signed-stack)))
244 (:arg-types signed-num signed-num)
245 (:results (r :scs (signed-reg) :from (:argument 0)
246 :load-if (not (and (sc-is x signed-stack)
249 (:result-types signed-num)
250 (:note "inline (signed-byte 32) arithmetic")
252 (cond ((and (sc-is x signed-reg) (sc-is y signed-reg) (sc-is r signed-reg)
253 (not (location= x r)))
254 (inst lea r (make-ea :dword :base x :index y :scale 1)))
259 ;;;; Special logand cases: (logand signed unsigned) => unsigned
261 (define-vop (fast-logand/signed-unsigned=>unsigned
262 fast-logand/unsigned=>unsigned)
263 (:args (x :target r :scs (signed-reg)
264 :load-if (not (and (sc-is x signed-stack)
265 (sc-is y unsigned-reg)
266 (sc-is r unsigned-stack)
268 (y :scs (unsigned-reg unsigned-stack)))
269 (:arg-types signed-num unsigned-num))
271 (define-vop (fast-logand-c/signed-unsigned=>unsigned
272 fast-logand-c/unsigned=>unsigned)
273 (:args (x :target r :scs (signed-reg signed-stack)))
274 (:arg-types signed-num (:constant (unsigned-byte 32))))
276 (define-vop (fast-logand/unsigned-signed=>unsigned
277 fast-logand/unsigned=>unsigned)
278 (:args (x :target r :scs (unsigned-reg)
279 :load-if (not (and (sc-is x unsigned-stack)
281 (sc-is r unsigned-stack)
283 (y :scs (signed-reg signed-stack)))
284 (:arg-types unsigned-num signed-num))
287 (define-vop (fast-+-c/signed=>signed fast-safe-arith-op)
289 (:args (x :target r :scs (signed-reg signed-stack)))
291 (:arg-types signed-num (:constant (signed-byte 32)))
292 (:results (r :scs (signed-reg)
293 :load-if (not (location= x r))))
294 (:result-types signed-num)
295 (:note "inline (signed-byte 32) arithmetic")
297 (cond ((and (sc-is x signed-reg) (sc-is r signed-reg)
298 (not (location= x r)))
299 (inst lea r (make-ea :dword :base x :disp y)))
306 (define-vop (fast-+/unsigned=>unsigned fast-safe-arith-op)
308 (:args (x :scs (unsigned-reg) :target r
309 :load-if (not (and (sc-is x unsigned-stack)
310 (sc-is y unsigned-reg)
311 (sc-is r unsigned-stack)
313 (y :scs (unsigned-reg unsigned-stack)))
314 (:arg-types unsigned-num unsigned-num)
315 (:results (r :scs (unsigned-reg) :from (:argument 0)
316 :load-if (not (and (sc-is x unsigned-stack)
317 (sc-is y unsigned-reg)
318 (sc-is r unsigned-stack)
320 (:result-types unsigned-num)
321 (:note "inline (unsigned-byte 32) arithmetic")
323 (cond ((and (sc-is x unsigned-reg) (sc-is y unsigned-reg)
324 (sc-is r unsigned-reg) (not (location= x r)))
325 (inst lea r (make-ea :dword :base x :index y :scale 1)))
330 (define-vop (fast-+-c/unsigned=>unsigned fast-safe-arith-op)
332 (:args (x :target r :scs (unsigned-reg unsigned-stack)))
334 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
335 (:results (r :scs (unsigned-reg)
336 :load-if (not (location= x r))))
337 (:result-types unsigned-num)
338 (:note "inline (unsigned-byte 32) arithmetic")
340 (cond ((and (sc-is x unsigned-reg) (sc-is r unsigned-reg)
341 (not (location= x r)))
342 (inst lea r (make-ea :dword :base x :disp y)))
349 ;;;; multiplication and division
351 (define-vop (fast-*/fixnum=>fixnum fast-safe-arith-op)
353 ;; We need different loading characteristics.
354 (:args (x :scs (any-reg) :target r)
355 (y :scs (any-reg control-stack)))
356 (:arg-types tagged-num tagged-num)
357 (:results (r :scs (any-reg) :from (:argument 0)))
358 (:result-types tagged-num)
359 (:note "inline fixnum arithmetic")
365 (define-vop (fast-*-c/fixnum=>fixnum fast-safe-arith-op)
367 ;; We need different loading characteristics.
368 (:args (x :scs (any-reg control-stack)))
370 (:arg-types tagged-num (:constant (signed-byte 30)))
371 (:results (r :scs (any-reg)))
372 (:result-types tagged-num)
373 (:note "inline fixnum arithmetic")
377 (define-vop (fast-*/signed=>signed fast-safe-arith-op)
379 ;; We need different loading characteristics.
380 (:args (x :scs (signed-reg) :target r)
381 (y :scs (signed-reg signed-stack)))
382 (:arg-types signed-num signed-num)
383 (:results (r :scs (signed-reg) :from (:argument 0)))
384 (:result-types signed-num)
385 (:note "inline (signed-byte 32) arithmetic")
390 (define-vop (fast-*-c/signed=>signed fast-safe-arith-op)
392 ;; We need different loading characteristics.
393 (:args (x :scs (signed-reg signed-stack)))
395 (:arg-types signed-num (:constant (signed-byte 32)))
396 (:results (r :scs (signed-reg)))
397 (:result-types signed-num)
398 (:note "inline (signed-byte 32) arithmetic")
402 (define-vop (fast-*/unsigned=>unsigned fast-safe-arith-op)
404 (:args (x :scs (unsigned-reg) :target eax)
405 (y :scs (unsigned-reg unsigned-stack)))
406 (:arg-types unsigned-num unsigned-num)
407 (:temporary (:sc unsigned-reg :offset eax-offset :target result
408 :from (:argument 0) :to :result) eax)
409 (:temporary (:sc unsigned-reg :offset edx-offset
410 :from :eval :to :result) edx)
412 (:results (result :scs (unsigned-reg)))
413 (:result-types unsigned-num)
414 (:note "inline (unsigned-byte 32) arithmetic")
416 (:save-p :compute-only)
423 (define-vop (fast-truncate/fixnum=>fixnum fast-safe-arith-op)
424 (:translate truncate)
425 (:args (x :scs (any-reg) :target eax)
426 (y :scs (any-reg control-stack)))
427 (:arg-types tagged-num tagged-num)
428 (:temporary (:sc signed-reg :offset eax-offset :target quo
429 :from (:argument 0) :to (:result 0)) eax)
430 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
431 :from (:argument 0) :to (:result 1)) edx)
432 (:results (quo :scs (any-reg))
433 (rem :scs (any-reg)))
434 (:result-types tagged-num tagged-num)
435 (:note "inline fixnum arithmetic")
437 (:save-p :compute-only)
439 (let ((zero (generate-error-code vop division-by-zero-error x y)))
440 (if (sc-is y any-reg)
441 (inst test y y) ; smaller instruction
447 (if (location= quo eax)
449 (inst lea quo (make-ea :dword :index eax :scale 4)))
452 (define-vop (fast-truncate-c/fixnum=>fixnum fast-safe-arith-op)
453 (:translate truncate)
454 (:args (x :scs (any-reg) :target eax))
456 (:arg-types tagged-num (:constant (signed-byte 30)))
457 (:temporary (:sc signed-reg :offset eax-offset :target quo
458 :from :argument :to (:result 0)) eax)
459 (:temporary (:sc any-reg :offset edx-offset :target rem
460 :from :eval :to (:result 1)) edx)
461 (:temporary (:sc any-reg :from :eval :to :result) y-arg)
462 (:results (quo :scs (any-reg))
463 (rem :scs (any-reg)))
464 (:result-types tagged-num tagged-num)
465 (:note "inline fixnum arithmetic")
467 (:save-p :compute-only)
471 (inst mov y-arg (fixnumize y))
472 (inst idiv eax y-arg)
473 (if (location= quo eax)
475 (inst lea quo (make-ea :dword :index eax :scale 4)))
478 (define-vop (fast-truncate/unsigned=>unsigned fast-safe-arith-op)
479 (:translate truncate)
480 (:args (x :scs (unsigned-reg) :target eax)
481 (y :scs (unsigned-reg signed-stack)))
482 (:arg-types unsigned-num unsigned-num)
483 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
484 :from (:argument 0) :to (:result 0)) eax)
485 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
486 :from (:argument 0) :to (:result 1)) edx)
487 (:results (quo :scs (unsigned-reg))
488 (rem :scs (unsigned-reg)))
489 (:result-types unsigned-num unsigned-num)
490 (:note "inline (unsigned-byte 32) arithmetic")
492 (:save-p :compute-only)
494 (let ((zero (generate-error-code vop division-by-zero-error x y)))
495 (if (sc-is y unsigned-reg)
496 (inst test y y) ; smaller instruction
505 (define-vop (fast-truncate-c/unsigned=>unsigned fast-safe-arith-op)
506 (:translate truncate)
507 (:args (x :scs (unsigned-reg) :target eax))
509 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
510 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
511 :from :argument :to (:result 0)) eax)
512 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
513 :from :eval :to (:result 1)) edx)
514 (:temporary (:sc unsigned-reg :from :eval :to :result) y-arg)
515 (:results (quo :scs (unsigned-reg))
516 (rem :scs (unsigned-reg)))
517 (:result-types unsigned-num unsigned-num)
518 (:note "inline (unsigned-byte 32) arithmetic")
520 (:save-p :compute-only)
529 (define-vop (fast-truncate/signed=>signed fast-safe-arith-op)
530 (:translate truncate)
531 (:args (x :scs (signed-reg) :target eax)
532 (y :scs (signed-reg signed-stack)))
533 (:arg-types signed-num signed-num)
534 (:temporary (:sc signed-reg :offset eax-offset :target quo
535 :from (:argument 0) :to (:result 0)) eax)
536 (:temporary (:sc signed-reg :offset edx-offset :target rem
537 :from (:argument 0) :to (:result 1)) edx)
538 (:results (quo :scs (signed-reg))
539 (rem :scs (signed-reg)))
540 (:result-types signed-num signed-num)
541 (:note "inline (signed-byte 32) arithmetic")
543 (:save-p :compute-only)
545 (let ((zero (generate-error-code vop division-by-zero-error x y)))
546 (if (sc-is y signed-reg)
547 (inst test y y) ; smaller instruction
556 (define-vop (fast-truncate-c/signed=>signed fast-safe-arith-op)
557 (:translate truncate)
558 (:args (x :scs (signed-reg) :target eax))
560 (:arg-types signed-num (:constant (signed-byte 32)))
561 (:temporary (:sc signed-reg :offset eax-offset :target quo
562 :from :argument :to (:result 0)) eax)
563 (:temporary (:sc signed-reg :offset edx-offset :target rem
564 :from :eval :to (:result 1)) edx)
565 (:temporary (:sc signed-reg :from :eval :to :result) y-arg)
566 (:results (quo :scs (signed-reg))
567 (rem :scs (signed-reg)))
568 (:result-types signed-num signed-num)
569 (:note "inline (signed-byte 32) arithmetic")
571 (:save-p :compute-only)
576 (inst idiv eax y-arg)
583 (define-vop (fast-ash-c/fixnum=>fixnum)
586 (:args (number :scs (any-reg) :target result
587 :load-if (not (and (sc-is number any-reg control-stack)
588 (sc-is result any-reg control-stack)
589 (location= number result)))))
591 (:arg-types tagged-num (:constant integer))
592 (:results (result :scs (any-reg)
593 :load-if (not (and (sc-is number control-stack)
594 (sc-is result control-stack)
595 (location= number result)))))
596 (:result-types tagged-num)
599 (cond ((and (= amount 1) (not (location= number result)))
600 (inst lea result (make-ea :dword :index number :scale 2)))
601 ((and (= amount 2) (not (location= number result)))
602 (inst lea result (make-ea :dword :index number :scale 4)))
603 ((and (= amount 3) (not (location= number result)))
604 (inst lea result (make-ea :dword :index number :scale 8)))
607 (cond ((plusp amount)
608 ;; We don't have to worry about overflow because of the
609 ;; result type restriction.
610 (inst shl result amount))
612 ;; If the amount is greater than 31, only shift by 31. We
613 ;; have to do this because the shift instructions only look
614 ;; at the low five bits of the result.
615 (inst sar result (min 31 (- amount)))
616 ;; Fixnum correction.
617 (inst and result #xfffffffc)))))))
619 (define-vop (fast-ash-left/fixnum=>fixnum)
621 (:args (number :scs (any-reg) :target result
622 :load-if (not (and (sc-is number control-stack)
623 (sc-is result control-stack)
624 (location= number result))))
625 (amount :scs (unsigned-reg) :target ecx))
626 (:arg-types tagged-num positive-fixnum)
627 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
628 (:results (result :scs (any-reg) :from (:argument 0)
629 :load-if (not (and (sc-is number control-stack)
630 (sc-is result control-stack)
631 (location= number result)))))
632 (:result-types tagged-num)
638 ;; The result-type ensures us that this shift will not overflow.
639 (inst shl result :cl)))
641 (define-vop (fast-ash-c/signed=>signed)
644 (:args (number :scs (signed-reg) :target result
645 :load-if (not (and (sc-is number signed-stack)
646 (sc-is result signed-stack)
647 (location= number result)))))
649 (:arg-types signed-num (:constant integer))
650 (:results (result :scs (signed-reg)
651 :load-if (not (and (sc-is number signed-stack)
652 (sc-is result signed-stack)
653 (location= number result)))))
654 (:result-types signed-num)
657 (cond ((and (= amount 1) (not (location= number result)))
658 (inst lea result (make-ea :dword :index number :scale 2)))
659 ((and (= amount 2) (not (location= number result)))
660 (inst lea result (make-ea :dword :index number :scale 4)))
661 ((and (= amount 3) (not (location= number result)))
662 (inst lea result (make-ea :dword :index number :scale 8)))
665 (cond ((plusp amount) (inst shl result amount))
666 (t (inst sar result (min 31 (- amount)))))))))
668 (define-vop (fast-ash-c/unsigned=>unsigned)
671 (:args (number :scs (unsigned-reg) :target result
672 :load-if (not (and (sc-is number unsigned-stack)
673 (sc-is result unsigned-stack)
674 (location= number result)))))
676 (:arg-types unsigned-num (:constant integer))
677 (:results (result :scs (unsigned-reg)
678 :load-if (not (and (sc-is number unsigned-stack)
679 (sc-is result unsigned-stack)
680 (location= number result)))))
681 (:result-types unsigned-num)
684 (cond ((and (= amount 1) (not (location= number result)))
685 (inst lea result (make-ea :dword :index number :scale 2)))
686 ((and (= amount 2) (not (location= number result)))
687 (inst lea result (make-ea :dword :index number :scale 4)))
688 ((and (= amount 3) (not (location= number result)))
689 (inst lea result (make-ea :dword :index number :scale 8)))
692 (cond ((< -32 amount 32)
693 ;; this code is used both in ASH and ASH-MOD32, so
696 (inst shl result amount)
697 (inst shr result (- amount))))
698 (t (if (sc-is result unsigned-reg)
699 (inst xor result result)
700 (inst mov result 0))))))))
702 (define-vop (fast-ash-left/signed=>signed)
704 (:args (number :scs (signed-reg) :target result
705 :load-if (not (and (sc-is number signed-stack)
706 (sc-is result signed-stack)
707 (location= number result))))
708 (amount :scs (unsigned-reg) :target ecx))
709 (:arg-types signed-num positive-fixnum)
710 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
711 (:results (result :scs (signed-reg) :from (:argument 0)
712 :load-if (not (and (sc-is number signed-stack)
713 (sc-is result signed-stack)
714 (location= number result)))))
715 (:result-types signed-num)
721 (inst shl result :cl)))
723 (define-vop (fast-ash-left/unsigned=>unsigned)
725 (:args (number :scs (unsigned-reg) :target result
726 :load-if (not (and (sc-is number unsigned-stack)
727 (sc-is result unsigned-stack)
728 (location= number result))))
729 (amount :scs (unsigned-reg) :target ecx))
730 (:arg-types unsigned-num positive-fixnum)
731 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
732 (:results (result :scs (unsigned-reg) :from (:argument 0)
733 :load-if (not (and (sc-is number unsigned-stack)
734 (sc-is result unsigned-stack)
735 (location= number result)))))
736 (:result-types unsigned-num)
742 (inst shl result :cl)))
744 (define-vop (fast-ash/signed=>signed)
747 (:args (number :scs (signed-reg) :target result)
748 (amount :scs (signed-reg) :target ecx))
749 (:arg-types signed-num signed-num)
750 (:results (result :scs (signed-reg) :from (:argument 0)))
751 (:result-types signed-num)
752 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
758 (inst jmp :ns positive)
764 (inst sar result :cl)
768 ;; The result-type ensures us that this shift will not overflow.
769 (inst shl result :cl)
773 (define-vop (fast-ash/unsigned=>unsigned)
776 (:args (number :scs (unsigned-reg) :target result)
777 (amount :scs (signed-reg) :target ecx))
778 (:arg-types unsigned-num signed-num)
779 (:results (result :scs (unsigned-reg) :from (:argument 0)))
780 (:result-types unsigned-num)
781 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
787 (inst jmp :ns positive)
791 (inst xor result result)
794 (inst shr result :cl)
798 ;; The result-type ensures us that this shift will not overflow.
799 (inst shl result :cl)
805 (defknown %lea (integer integer (member 1 2 4 8) (signed-byte 32))
807 (foldable flushable movable))
809 (defoptimizer (%lea derive-type) ((base index scale disp))
810 (when (and (constant-lvar-p scale)
811 (constant-lvar-p disp))
812 (let ((scale (lvar-value scale))
813 (disp (lvar-value disp))
814 (base-type (lvar-type base))
815 (index-type (lvar-type index)))
816 (when (and (numeric-type-p base-type)
817 (numeric-type-p index-type))
818 (let ((base-lo (numeric-type-low base-type))
819 (base-hi (numeric-type-high base-type))
820 (index-lo (numeric-type-low index-type))
821 (index-hi (numeric-type-high index-type)))
822 (make-numeric-type :class 'integer
824 :low (when (and base-lo index-lo)
825 (+ base-lo (* index-lo scale) disp))
826 :high (when (and base-hi index-hi)
827 (+ base-hi (* index-hi scale) disp))))))))
829 (defun %lea (base index scale disp)
830 (+ base (* index scale) disp))
834 (define-vop (%lea/unsigned=>unsigned)
837 (:args (base :scs (unsigned-reg))
838 (index :scs (unsigned-reg)))
840 (:arg-types unsigned-num unsigned-num
841 (:constant (member 1 2 4 8))
842 (:constant (signed-byte 32)))
843 (:results (r :scs (unsigned-reg)))
844 (:result-types unsigned-num)
846 (inst lea r (make-ea :dword :base base :index index
847 :scale scale :disp disp))))
849 (define-vop (%lea/signed=>signed)
852 (:args (base :scs (signed-reg))
853 (index :scs (signed-reg)))
855 (:arg-types signed-num signed-num
856 (:constant (member 1 2 4 8))
857 (:constant (signed-byte 32)))
858 (:results (r :scs (signed-reg)))
859 (:result-types signed-num)
861 (inst lea r (make-ea :dword :base base :index index
862 :scale scale :disp disp))))
864 (define-vop (%lea/fixnum=>fixnum)
867 (:args (base :scs (any-reg))
868 (index :scs (any-reg)))
870 (:arg-types tagged-num tagged-num
871 (:constant (member 1 2 4 8))
872 (:constant (signed-byte 32)))
873 (:results (r :scs (any-reg)))
874 (:result-types tagged-num)
876 (inst lea r (make-ea :dword :base base :index index
877 :scale scale :disp disp))))
879 ;;; FIXME: before making knowledge of this too public, it needs to be
880 ;;; fixed so that it's actually _faster_ than the non-CMOV version; at
881 ;;; least on my Celeron-XXX laptop, this version is marginally slower
882 ;;; than the above version with branches. -- CSR, 2003-09-04
883 (define-vop (fast-cmov-ash/unsigned=>unsigned)
886 (:args (number :scs (unsigned-reg) :target result)
887 (amount :scs (signed-reg) :target ecx))
888 (:arg-types unsigned-num signed-num)
889 (:results (result :scs (unsigned-reg) :from (:argument 0)))
890 (:result-types unsigned-num)
891 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
892 (:temporary (:sc any-reg :from (:eval 0) :to (:eval 1)) zero)
894 (:guard (member :cmov *backend-subfeatures*))
899 (inst jmp :ns positive)
902 (inst shr result :cl)
904 (inst cmov :nbe result zero)
908 ;; The result-type ensures us that this shift will not overflow.
909 (inst shl result :cl)
913 ;;; Note: documentation for this function is wrong - rtfm
914 (define-vop (signed-byte-32-len)
915 (:translate integer-length)
916 (:note "inline (signed-byte 32) integer-length")
918 (:args (arg :scs (signed-reg) :target res))
919 (:arg-types signed-num)
920 (:results (res :scs (unsigned-reg)))
921 (:result-types unsigned-num)
936 (define-vop (unsigned-byte-32-len)
937 (:translate integer-length)
938 (:note "inline (unsigned-byte 32) integer-length")
940 (:args (arg :scs (unsigned-reg)))
941 (:arg-types unsigned-num)
942 (:results (res :scs (unsigned-reg)))
943 (:result-types unsigned-num)
953 (define-vop (unsigned-byte-32-count)
954 (:translate logcount)
955 (:note "inline (unsigned-byte 32) logcount")
957 (:args (arg :scs (unsigned-reg)))
958 (:arg-types unsigned-num)
959 (:results (result :scs (unsigned-reg)))
960 (:result-types positive-fixnum)
961 (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
965 (inst mov temp result)
967 (inst and result #x55555555)
968 (inst and temp #x55555555)
969 (inst add result temp)
971 (inst mov temp result)
973 (inst and result #x33333333)
974 (inst and temp #x33333333)
975 (inst add result temp)
977 (inst mov temp result)
979 (inst and result #x0f0f0f0f)
980 (inst and temp #x0f0f0f0f)
981 (inst add result temp)
983 (inst mov temp result)
985 (inst and result #x00ff00ff)
986 (inst and temp #x00ff00ff)
987 (inst add result temp)
989 (inst mov temp result)
991 (inst and result #x0000ffff)
992 (inst and temp #x0000ffff)
993 (inst add result temp)))
995 ;;;; binary conditional VOPs
997 (define-vop (fast-conditional)
1002 (:policy :fast-safe))
1004 (define-vop (fast-conditional/fixnum fast-conditional)
1005 (:args (x :scs (any-reg)
1006 :load-if (not (and (sc-is x control-stack)
1007 (sc-is y any-reg))))
1008 (y :scs (any-reg control-stack)))
1009 (:arg-types tagged-num tagged-num)
1010 (:note "inline fixnum comparison"))
1012 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
1013 (:args (x :scs (any-reg control-stack)))
1014 (:arg-types tagged-num (:constant (signed-byte 30)))
1015 (:info target not-p y))
1017 (define-vop (fast-conditional/signed fast-conditional)
1018 (:args (x :scs (signed-reg)
1019 :load-if (not (and (sc-is x signed-stack)
1020 (sc-is y signed-reg))))
1021 (y :scs (signed-reg signed-stack)))
1022 (:arg-types signed-num signed-num)
1023 (:note "inline (signed-byte 32) comparison"))
1025 (define-vop (fast-conditional-c/signed fast-conditional/signed)
1026 (:args (x :scs (signed-reg signed-stack)))
1027 (:arg-types signed-num (:constant (signed-byte 32)))
1028 (:info target not-p y))
1030 (define-vop (fast-conditional/unsigned fast-conditional)
1031 (:args (x :scs (unsigned-reg)
1032 :load-if (not (and (sc-is x unsigned-stack)
1033 (sc-is y unsigned-reg))))
1034 (y :scs (unsigned-reg unsigned-stack)))
1035 (:arg-types unsigned-num unsigned-num)
1036 (:note "inline (unsigned-byte 32) comparison"))
1038 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
1039 (:args (x :scs (unsigned-reg unsigned-stack)))
1040 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
1041 (:info target not-p y))
1044 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
1047 (lambda (suffix cost signed)
1048 `(define-vop (;; FIXME: These could be done more
1049 ;; cleanly with SYMBOLICATE.
1050 ,(intern (format nil "~:@(FAST-IF-~A~A~)"
1053 (format nil "~:@(FAST-CONDITIONAL~A~)"
1058 ,(if (eq suffix '-c/fixnum)
1069 '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
1071 '(t t t t nil nil)))))
1073 (define-conditional-vop < :l :b :ge :ae)
1074 (define-conditional-vop > :g :a :le :be))
1076 (define-vop (fast-if-eql/signed fast-conditional/signed)
1080 (inst jmp (if not-p :ne :e) target)))
1082 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
1085 (cond ((and (sc-is x signed-reg) (zerop y))
1086 (inst test x x)) ; smaller instruction
1089 (inst jmp (if not-p :ne :e) target)))
1091 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1095 (inst jmp (if not-p :ne :e) target)))
1097 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1100 (cond ((and (sc-is x unsigned-reg) (zerop y))
1101 (inst test x x)) ; smaller instruction
1104 (inst jmp (if not-p :ne :e) target)))
1106 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1109 ;;; These versions specify a fixnum restriction on their first arg. We have
1110 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1111 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1112 ;;; fixnum specific operations from being used on word integers, spuriously
1113 ;;; consing the argument.
1115 (define-vop (fast-eql/fixnum fast-conditional)
1116 (:args (x :scs (any-reg)
1117 :load-if (not (and (sc-is x control-stack)
1118 (sc-is y any-reg))))
1119 (y :scs (any-reg control-stack)))
1120 (:arg-types tagged-num tagged-num)
1121 (:note "inline fixnum comparison")
1125 (inst jmp (if not-p :ne :e) target)))
1126 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1127 (:args (x :scs (any-reg descriptor-reg)
1128 :load-if (not (and (sc-is x control-stack)
1129 (sc-is y any-reg))))
1130 (y :scs (any-reg control-stack)))
1131 (:arg-types * tagged-num)
1134 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1135 (:args (x :scs (any-reg control-stack)))
1136 (:arg-types tagged-num (:constant (signed-byte 30)))
1137 (:info target not-p y)
1140 (cond ((and (sc-is x any-reg) (zerop y))
1141 (inst test x x)) ; smaller instruction
1143 (inst cmp x (fixnumize y))))
1144 (inst jmp (if not-p :ne :e) target)))
1145 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1146 (:args (x :scs (any-reg descriptor-reg control-stack)))
1147 (:arg-types * (:constant (signed-byte 30)))
1150 ;;;; 32-bit logical operations
1152 (define-vop (merge-bits)
1153 (:translate merge-bits)
1154 (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1155 (prev :scs (unsigned-reg) :target result)
1156 (next :scs (unsigned-reg)))
1157 (:arg-types tagged-num unsigned-num unsigned-num)
1158 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1159 (:results (result :scs (unsigned-reg) :from (:argument 1)))
1160 (:result-types unsigned-num)
1161 (:policy :fast-safe)
1165 (inst shrd result next :cl)))
1167 ;;; Only the lower 5 bits of the shift amount are significant.
1168 (define-vop (shift-towards-someplace)
1169 (:policy :fast-safe)
1170 (:args (num :scs (unsigned-reg) :target r)
1171 (amount :scs (signed-reg) :target ecx))
1172 (:arg-types unsigned-num tagged-num)
1173 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1174 (:results (r :scs (unsigned-reg) :from (:argument 0)))
1175 (:result-types unsigned-num))
1177 (define-vop (shift-towards-start shift-towards-someplace)
1178 (:translate shift-towards-start)
1179 (:note "SHIFT-TOWARDS-START")
1185 (define-vop (shift-towards-end shift-towards-someplace)
1186 (:translate shift-towards-end)
1187 (:note "SHIFT-TOWARDS-END")
1193 ;;;; Modular functions
1195 (macrolet ((def (name -c-p)
1196 (let ((fun32 (intern (format nil "~S-MOD32" name)))
1197 (vopu (intern (format nil "FAST-~S/UNSIGNED=>UNSIGNED" name)))
1198 (vopcu (intern (format nil "FAST-~S-C/UNSIGNED=>UNSIGNED" name)))
1199 (vopf (intern (format nil "FAST-~S/FIXNUM=>FIXNUM" name)))
1200 (vopcf (intern (format nil "FAST-~S-C/FIXNUM=>FIXNUM" name)))
1201 (vop32u (intern (format nil "FAST-~S-MOD32/UNSIGNED=>UNSIGNED" name)))
1202 (vop32f (intern (format nil "FAST-~S-MOD32/FIXNUM=>FIXNUM" name)))
1203 (vop32cu (intern (format nil "FAST-~S-MOD32-C/UNSIGNED=>UNSIGNED" name)))
1204 (vop32cf (intern (format nil "FAST-~S-MOD32-C/FIXNUM=>FIXNUM" name)))
1205 (sfun30 (intern (format nil "~S-SMOD30" name)))
1206 (svop30f (intern (format nil "FAST-~S-SMOD30/FIXNUM=>FIXNUM" name)))
1207 (svop30cf (intern (format nil "FAST-~S-SMOD30-C/FIXNUM=>FIXNUM" name))))
1209 (define-modular-fun ,fun32 (x y) ,name :unsigned 32)
1210 (define-modular-fun ,sfun30 (x y) ,name :signed 30)
1211 (define-vop (,vop32u ,vopu) (:translate ,fun32))
1212 (define-vop (,vop32f ,vopf) (:translate ,fun32))
1213 (define-vop (,svop30f ,vopf) (:translate ,sfun30))
1215 `((define-vop (,vop32cu ,vopcu) (:translate ,fun32))
1216 (define-vop (,svop30cf ,vopcf) (:translate ,sfun30))))))))
1219 ;; (no -C variant as x86 MUL instruction doesn't take an immediate)
1223 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
1224 fast-ash-c/unsigned=>unsigned)
1225 (:translate ash-left-mod32))
1227 (define-vop (fast-ash-left-mod32/unsigned=>unsigned
1228 fast-ash-left/unsigned=>unsigned))
1229 (deftransform ash-left-mod32 ((integer count)
1230 ((unsigned-byte 32) (unsigned-byte 5)))
1231 (when (sb!c::constant-lvar-p count)
1232 (sb!c::give-up-ir1-transform))
1233 '(%primitive fast-ash-left-mod32/unsigned=>unsigned integer count))
1235 (define-vop (fast-ash-left-smod30-c/fixnum=>fixnum
1236 fast-ash-c/fixnum=>fixnum)
1237 (:translate ash-left-smod30))
1239 (define-vop (fast-ash-left-smod30/fixnum=>fixnum
1240 fast-ash-left/fixnum=>fixnum))
1241 (deftransform ash-left-smod30 ((integer count)
1242 ((signed-byte 30) (unsigned-byte 5)))
1243 (when (sb!c::constant-lvar-p count)
1244 (sb!c::give-up-ir1-transform))
1245 '(%primitive fast-ash-left-smod30/fixnum=>fixnum integer count))
1249 (defknown sb!vm::%lea-mod32 (integer integer (member 1 2 4 8) (signed-byte 32))
1251 (foldable flushable movable))
1252 (defknown sb!vm::%lea-smod30 (integer integer (member 1 2 4 8) (signed-byte 32))
1254 (foldable flushable movable))
1256 (define-modular-fun-optimizer %lea ((base index scale disp) :unsigned :width width)
1257 (when (and (<= width 32)
1258 (constant-lvar-p scale)
1259 (constant-lvar-p disp))
1260 (cut-to-width base :unsigned width)
1261 (cut-to-width index :unsigned width)
1262 'sb!vm::%lea-mod32))
1263 (define-modular-fun-optimizer %lea ((base index scale disp) :signed :width width)
1264 (when (and (<= width 30)
1265 (constant-lvar-p scale)
1266 (constant-lvar-p disp))
1267 (cut-to-width base :signed width)
1268 (cut-to-width index :signed width)
1269 'sb!vm::%lea-smod30))
1273 (defun sb!vm::%lea-mod32 (base index scale disp)
1274 (ldb (byte 32 0) (%lea base index scale disp)))
1275 (defun sb!vm::%lea-smod30 (base index scale disp)
1276 (mask-signed-field 30 (%lea base index scale disp))))
1279 (defun sb!vm::%lea-mod32 (base index scale disp)
1280 (let ((base (logand base #xffffffff))
1281 (index (logand index #xffffffff)))
1282 ;; can't use modular version of %LEA, as we only have VOPs for
1283 ;; constant SCALE and DISP.
1284 (ldb (byte 32 0) (+ base (* index scale) disp))))
1285 (defun sb!vm::%lea-smod30 (base index scale disp)
1286 (let ((base (mask-signed-field 30 base))
1287 (index (mask-signed-field 30 index)))
1288 ;; can't use modular version of %LEA, as we only have VOPs for
1289 ;; constant SCALE and DISP.
1290 (mask-signed-field 30 (+ base (* index scale) disp)))))
1292 (in-package "SB!VM")
1294 (define-vop (%lea-mod32/unsigned=>unsigned
1295 %lea/unsigned=>unsigned)
1296 (:translate %lea-mod32))
1297 (define-vop (%lea-smod30/fixnum=>fixnum
1298 %lea/fixnum=>fixnum)
1299 (:translate %lea-smod30))
1301 ;;; logical operations
1302 (define-modular-fun lognot-mod32 (x) lognot :unsigned 32)
1303 (define-vop (lognot-mod32/unsigned=>unsigned)
1304 (:translate lognot-mod32)
1305 (:args (x :scs (unsigned-reg unsigned-stack) :target r
1306 :load-if (not (and (sc-is x unsigned-stack)
1307 (sc-is r unsigned-stack)
1309 (:arg-types unsigned-num)
1310 (:results (r :scs (unsigned-reg)
1311 :load-if (not (and (sc-is x unsigned-stack)
1312 (sc-is r unsigned-stack)
1314 (:result-types unsigned-num)
1315 (:policy :fast-safe)
1320 (define-modular-fun logxor-mod32 (x y) logxor :unsigned 32)
1321 (define-vop (fast-logxor-mod32/unsigned=>unsigned
1322 fast-logxor/unsigned=>unsigned)
1323 (:translate logxor-mod32))
1324 (define-vop (fast-logxor-mod32-c/unsigned=>unsigned
1325 fast-logxor-c/unsigned=>unsigned)
1326 (:translate logxor-mod32))
1327 (define-vop (fast-logxor-mod32/fixnum=>fixnum
1328 fast-logxor/fixnum=>fixnum)
1329 (:translate logxor-mod32))
1330 (define-vop (fast-logxor-mod32-c/fixnum=>fixnum
1331 fast-logxor-c/fixnum=>fixnum)
1332 (:translate logxor-mod32))
1334 (define-source-transform logeqv (&rest args)
1335 (if (oddp (length args))
1337 `(lognot (logxor ,@args))))
1338 (define-source-transform logandc1 (x y)
1339 `(logand (lognot ,x) ,y))
1340 (define-source-transform logandc2 (x y)
1341 `(logand ,x (lognot ,y)))
1342 (define-source-transform logorc1 (x y)
1343 `(logior (lognot ,x) ,y))
1344 (define-source-transform logorc2 (x y)
1345 `(logior ,x (lognot ,y)))
1346 (define-source-transform lognor (x y)
1347 `(lognot (logior ,x ,y)))
1348 (define-source-transform lognand (x y)
1349 `(lognot (logand ,x ,y)))
1353 (define-vop (bignum-length get-header-data)
1354 (:translate sb!bignum:%bignum-length)
1355 (:policy :fast-safe))
1357 (define-vop (bignum-set-length set-header-data)
1358 (:translate sb!bignum:%bignum-set-length)
1359 (:policy :fast-safe))
1361 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1362 (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1364 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1365 (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1367 (define-vop (digit-0-or-plus)
1368 (:translate sb!bignum:%digit-0-or-plusp)
1369 (:policy :fast-safe)
1370 (:args (digit :scs (unsigned-reg)))
1371 (:arg-types unsigned-num)
1373 (:info target not-p)
1375 (inst or digit digit)
1376 (inst jmp (if not-p :s :ns) target)))
1379 ;;; For add and sub with carry the sc of carry argument is any-reg so
1380 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1381 ;;; 4. This is easy to deal with and may save a fixnum-word
1383 (define-vop (add-w/carry)
1384 (:translate sb!bignum:%add-with-carry)
1385 (:policy :fast-safe)
1386 (:args (a :scs (unsigned-reg) :target result)
1387 (b :scs (unsigned-reg unsigned-stack) :to :eval)
1388 (c :scs (any-reg) :target temp))
1389 (:arg-types unsigned-num unsigned-num positive-fixnum)
1390 (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1391 (:results (result :scs (unsigned-reg) :from (:argument 0))
1392 (carry :scs (unsigned-reg)))
1393 (:result-types unsigned-num positive-fixnum)
1397 (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1400 (inst adc carry carry)))
1402 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1403 ;;; borrow and 0 for a borrow.
1404 (define-vop (sub-w/borrow)
1405 (:translate sb!bignum:%subtract-with-borrow)
1406 (:policy :fast-safe)
1407 (:args (a :scs (unsigned-reg) :to :eval :target result)
1408 (b :scs (unsigned-reg unsigned-stack) :to :result)
1409 (c :scs (any-reg control-stack)))
1410 (:arg-types unsigned-num unsigned-num positive-fixnum)
1411 (:results (result :scs (unsigned-reg) :from :eval)
1412 (borrow :scs (unsigned-reg)))
1413 (:result-types unsigned-num positive-fixnum)
1415 (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1419 (inst adc borrow borrow)
1420 (inst xor borrow 1)))
1423 (define-vop (bignum-mult-and-add-3-arg)
1424 (:translate sb!bignum:%multiply-and-add)
1425 (:policy :fast-safe)
1426 (:args (x :scs (unsigned-reg) :target eax)
1427 (y :scs (unsigned-reg unsigned-stack))
1428 (carry-in :scs (unsigned-reg unsigned-stack)))
1429 (:arg-types unsigned-num unsigned-num unsigned-num)
1430 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1431 :to (:result 1) :target lo) eax)
1432 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1433 :to (:result 0) :target hi) edx)
1434 (:results (hi :scs (unsigned-reg))
1435 (lo :scs (unsigned-reg)))
1436 (:result-types unsigned-num unsigned-num)
1440 (inst add eax carry-in)
1445 (define-vop (bignum-mult-and-add-4-arg)
1446 (:translate sb!bignum:%multiply-and-add)
1447 (:policy :fast-safe)
1448 (:args (x :scs (unsigned-reg) :target eax)
1449 (y :scs (unsigned-reg unsigned-stack))
1450 (prev :scs (unsigned-reg unsigned-stack))
1451 (carry-in :scs (unsigned-reg unsigned-stack)))
1452 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1453 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1454 :to (:result 1) :target lo) eax)
1455 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1456 :to (:result 0) :target hi) edx)
1457 (:results (hi :scs (unsigned-reg))
1458 (lo :scs (unsigned-reg)))
1459 (:result-types unsigned-num unsigned-num)
1465 (inst add eax carry-in)
1471 (define-vop (bignum-mult)
1472 (:translate sb!bignum:%multiply)
1473 (:policy :fast-safe)
1474 (:args (x :scs (unsigned-reg) :target eax)
1475 (y :scs (unsigned-reg unsigned-stack)))
1476 (:arg-types unsigned-num unsigned-num)
1477 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1478 :to (:result 1) :target lo) eax)
1479 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1480 :to (:result 0) :target hi) edx)
1481 (:results (hi :scs (unsigned-reg))
1482 (lo :scs (unsigned-reg)))
1483 (:result-types unsigned-num unsigned-num)
1490 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
1491 (:translate sb!bignum:%lognot))
1493 (define-vop (fixnum-to-digit)
1494 (:translate sb!bignum:%fixnum-to-digit)
1495 (:policy :fast-safe)
1496 (:args (fixnum :scs (any-reg control-stack) :target digit))
1497 (:arg-types tagged-num)
1498 (:results (digit :scs (unsigned-reg)
1499 :load-if (not (and (sc-is fixnum control-stack)
1500 (sc-is digit unsigned-stack)
1501 (location= fixnum digit)))))
1502 (:result-types unsigned-num)
1505 (inst sar digit 2)))
1507 (define-vop (bignum-floor)
1508 (:translate sb!bignum:%floor)
1509 (:policy :fast-safe)
1510 (:args (div-high :scs (unsigned-reg) :target edx)
1511 (div-low :scs (unsigned-reg) :target eax)
1512 (divisor :scs (unsigned-reg unsigned-stack)))
1513 (:arg-types unsigned-num unsigned-num unsigned-num)
1514 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1515 :to (:result 0) :target quo) eax)
1516 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1517 :to (:result 1) :target rem) edx)
1518 (:results (quo :scs (unsigned-reg))
1519 (rem :scs (unsigned-reg)))
1520 (:result-types unsigned-num unsigned-num)
1524 (inst div eax divisor)
1528 (define-vop (signify-digit)
1529 (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1530 (:policy :fast-safe)
1531 (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1532 (:arg-types unsigned-num)
1533 (:results (res :scs (any-reg signed-reg)
1534 :load-if (not (and (sc-is digit unsigned-stack)
1535 (sc-is res control-stack signed-stack)
1536 (location= digit res)))))
1537 (:result-types signed-num)
1540 (when (sc-is res any-reg control-stack)
1543 (define-vop (digit-ashr)
1544 (:translate sb!bignum:%ashr)
1545 (:policy :fast-safe)
1546 (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1547 (count :scs (unsigned-reg) :target ecx))
1548 (:arg-types unsigned-num positive-fixnum)
1549 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1550 (:results (result :scs (unsigned-reg) :from (:argument 0)
1551 :load-if (not (and (sc-is result unsigned-stack)
1552 (location= digit result)))))
1553 (:result-types unsigned-num)
1557 (inst sar result :cl)))
1559 (define-vop (digit-lshr digit-ashr)
1560 (:translate sb!bignum:%digit-logical-shift-right)
1564 (inst shr result :cl)))
1566 (define-vop (digit-ashl digit-ashr)
1567 (:translate sb!bignum:%ashl)
1571 (inst shl result :cl)))
1573 ;;;; static functions
1575 (define-static-fun two-arg-/ (x y) :translate /)
1577 (define-static-fun two-arg-gcd (x y) :translate gcd)
1578 (define-static-fun two-arg-lcm (x y) :translate lcm)
1580 (define-static-fun two-arg-and (x y) :translate logand)
1581 (define-static-fun two-arg-ior (x y) :translate logior)
1582 (define-static-fun two-arg-xor (x y) :translate logxor)
1585 ;;; Support for the Mersenne Twister, MT19937, random number generator
1586 ;;; due to Matsumoto and Nishimura.
1588 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
1589 ;;; 623-dimensionally equidistributed uniform pseudorandom number
1590 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
1591 ;;; 1997, to appear.
1594 ;;; 0-1: Constant matrix A. [0, #x9908b0df] (not used here)
1595 ;;; 2: Index; init. to 1.
1597 (defknown random-mt19937 ((simple-array (unsigned-byte 32) (*)))
1598 (unsigned-byte 32) ())
1599 (define-vop (random-mt19937)
1600 (:policy :fast-safe)
1601 (:translate random-mt19937)
1602 (:args (state :scs (descriptor-reg) :to :result))
1603 (:arg-types simple-array-unsigned-byte-32)
1604 (:temporary (:sc unsigned-reg :from (:eval 0) :to :result) k)
1605 (:temporary (:sc unsigned-reg :offset eax-offset
1606 :from (:eval 0) :to :result) tmp)
1607 (:results (y :scs (unsigned-reg) :from (:eval 0)))
1608 (:result-types unsigned-num)
1610 (inst mov k (make-ea :dword :base state
1611 :disp (- (* (+ 2 vector-data-offset)
1613 other-pointer-lowtag)))
1615 (inst jmp :ne no-update)
1616 (inst mov tmp state) ; The state is passed in EAX.
1617 (inst call (make-fixup 'random-mt19937-update :assembly-routine))
1618 ;; Restore k, and set to 0.
1622 (inst mov y (make-ea :dword :base state :index k :scale 4
1623 :disp (- (* (+ 3 vector-data-offset)
1625 other-pointer-lowtag)))
1628 (inst xor y (make-ea :dword :base state :index k :scale 4
1629 :disp (- (* (+ 3 vector-data-offset)
1631 other-pointer-lowtag)))
1632 ;; y ^= (y << 7) & #x9d2c5680
1636 (inst mov (make-ea :dword :base state
1637 :disp (- (* (+ 2 vector-data-offset)
1639 other-pointer-lowtag))
1641 (inst and tmp #x9d2c5680)
1643 ;; y ^= (y << 15) & #xefc60000
1646 (inst and tmp #xefc60000)
1655 (defun mask-result (class width result)
1658 `(logand ,result ,(1- (ash 1 width))))
1660 `(mask-signed-field ,width ,result))))
1662 ;;; This is essentially a straight implementation of the algorithm in
1663 ;;; "Strength Reduction of Multiplications by Integer Constants",
1664 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1665 (defun basic-decompose-multiplication (class width arg num n-bits condensed)
1666 (case (aref condensed 0)
1668 (let ((tmp (min 3 (aref condensed 1))))
1669 (decf (aref condensed 1) tmp)
1670 (mask-result class width
1672 ,(decompose-multiplication class width
1673 arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1676 (let ((r0 (aref condensed 0)))
1677 (incf (aref condensed 1) r0)
1678 (mask-result class width
1679 `(%lea ,(decompose-multiplication class width
1680 arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1683 (t (let ((r0 (aref condensed 0)))
1684 (setf (aref condensed 0) 0)
1685 (mask-result class width
1686 `(ash ,(decompose-multiplication class width
1687 arg (ash num (- r0)) n-bits condensed)
1690 (defun decompose-multiplication (class width arg num n-bits condensed)
1695 (mask-result class width `(ash ,arg ,(1- (integer-length num)))))
1696 ((let ((max 0) (end 0))
1697 (loop for i from 2 to (length condensed)
1698 for j = (reduce #'+ (subseq condensed 0 i))
1699 when (and (> (- (* 2 i) 3 j) max)
1700 (< (+ (ash 1 (1+ j))
1701 (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num)
1704 do (setq max (- (* 2 i) 3 j)
1707 (let ((j (reduce #'+ (subseq condensed 0 end))))
1708 (let ((n2 (+ (ash 1 (1+ j))
1709 (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num) (1+ j))))
1710 (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1711 (mask-result class width
1712 `(- ,(optimize-multiply class width arg n2)
1713 ,(optimize-multiply class width arg n1))))))))
1714 ((dolist (i '(9 5 3))
1715 (when (integerp (/ num i))
1716 (when (< (logcount (/ num i)) (logcount num))
1718 (return `(let ((,x ,(optimize-multiply class width arg (/ num i))))
1719 ,(mask-result class width
1720 `(%lea ,x ,x (1- ,i) 0)))))))))
1721 (t (basic-decompose-multiplication class width arg num n-bits condensed))))
1723 (defun optimize-multiply (class width arg x)
1724 (let* ((n-bits (logcount x))
1725 (condensed (make-array n-bits)))
1726 (let ((count 0) (bit 0))
1728 (cond ((logbitp i x)
1729 (setf (aref condensed bit) count)
1733 (decompose-multiplication class width arg x n-bits condensed)))
1735 (defun *-transformer (class width y)
1737 ((= y (ash 1 (integer-length y)))
1738 ;; there's a generic transform for y = 2^k
1739 (give-up-ir1-transform))
1740 ((member y '(3 5 9))
1741 ;; we can do these multiplications directly using LEA
1742 `(%lea x x ,(1- y) 0))
1743 ((member :pentium4 *backend-subfeatures*)
1744 ;; the pentium4's multiply unit is reportedly very good
1745 (give-up-ir1-transform))
1746 ;; FIXME: should make this more fine-grained. If nothing else,
1747 ;; there should probably be a cutoff of about 9 instructions on
1748 ;; pentium-class machines.
1749 (t (optimize-multiply class width 'x y))))
1751 (deftransform * ((x y)
1752 ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1754 "recode as leas, shifts and adds"
1755 (let ((y (lvar-value y)))
1756 (*-transformer :unsigned 32 y)))
1757 (deftransform sb!vm::*-mod32
1758 ((x y) ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1760 "recode as leas, shifts and adds"
1761 (let ((y (lvar-value y)))
1762 (*-transformer :unsigned 32 y)))
1764 (deftransform * ((x y)
1765 ((signed-byte 30) (constant-arg (unsigned-byte 32)))
1767 "recode as leas, shifts and adds"
1768 (let ((y (lvar-value y)))
1769 (*-transformer :signed 30 y)))
1770 (deftransform sb!vm::*-smod30
1771 ((x y) ((signed-byte 30) (constant-arg (unsigned-byte 32)))
1773 "recode as leas, shifts and adds"
1774 (let ((y (lvar-value y)))
1775 (*-transformer :signed 30 y)))
1777 ;;; FIXME: we should also be able to write an optimizer or two to
1778 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.