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
181 ;;(define-binop + 4 add)
182 (define-binop - 4 sub)
183 (define-binop logand 2 and)
184 (define-binop logior 2 or)
185 (define-binop logxor 2 xor))
187 ;;; Special handling of add on the x86; can use lea to avoid a
188 ;;; register load, otherwise it uses add.
189 (define-vop (fast-+/fixnum=>fixnum fast-safe-arith-op)
191 (:args (x :scs (any-reg) :target r
192 :load-if (not (and (sc-is x control-stack)
194 (sc-is r control-stack)
196 (y :scs (any-reg control-stack)))
197 (:arg-types tagged-num tagged-num)
198 (:results (r :scs (any-reg) :from (:argument 0)
199 :load-if (not (and (sc-is x control-stack)
201 (sc-is r control-stack)
203 (:result-types tagged-num)
204 (:note "inline fixnum arithmetic")
206 (cond ((and (sc-is x any-reg) (sc-is y any-reg) (sc-is r any-reg)
207 (not (location= x r)))
208 (inst lea r (make-ea :dword :base x :index y :scale 1)))
213 (define-vop (fast-+-c/fixnum=>fixnum fast-safe-arith-op)
215 (:args (x :target r :scs (any-reg control-stack)))
217 (:arg-types tagged-num (:constant (signed-byte 30)))
218 (:results (r :scs (any-reg)
219 :load-if (not (location= x r))))
220 (:result-types tagged-num)
221 (:note "inline fixnum arithmetic")
223 (cond ((and (sc-is x any-reg) (sc-is r any-reg) (not (location= x r)))
224 (inst lea r (make-ea :dword :base x :disp (fixnumize y))))
227 (inst add r (fixnumize y))))))
229 (define-vop (fast-+/signed=>signed fast-safe-arith-op)
231 (:args (x :scs (signed-reg) :target r
232 :load-if (not (and (sc-is x signed-stack)
234 (sc-is r signed-stack)
236 (y :scs (signed-reg signed-stack)))
237 (:arg-types signed-num signed-num)
238 (:results (r :scs (signed-reg) :from (:argument 0)
239 :load-if (not (and (sc-is x signed-stack)
242 (:result-types signed-num)
243 (:note "inline (signed-byte 32) arithmetic")
245 (cond ((and (sc-is x signed-reg) (sc-is y signed-reg) (sc-is r signed-reg)
246 (not (location= x r)))
247 (inst lea r (make-ea :dword :base x :index y :scale 1)))
253 ;;;; Special logand cases: (logand signed unsigned) => unsigned
255 (define-vop (fast-logand/signed-unsigned=>unsigned
256 fast-logand/unsigned=>unsigned)
257 (:args (x :target r :scs (signed-reg)
258 :load-if (not (and (sc-is x signed-stack)
259 (sc-is y unsigned-reg)
260 (sc-is r unsigned-stack)
262 (y :scs (unsigned-reg unsigned-stack)))
263 (:arg-types signed-num unsigned-num))
265 (define-vop (fast-logand-c/signed-unsigned=>unsigned
266 fast-logand-c/unsigned=>unsigned)
267 (:args (x :target r :scs (signed-reg signed-stack)))
268 (:arg-types signed-num (:constant (unsigned-byte 32))))
270 (define-vop (fast-logand/unsigned-signed=>unsigned
271 fast-logand/unsigned=>unsigned)
272 (:args (x :target r :scs (unsigned-reg)
273 :load-if (not (and (sc-is x unsigned-stack)
275 (sc-is r unsigned-stack)
277 (y :scs (signed-reg signed-stack)))
278 (:arg-types unsigned-num signed-num))
281 (define-vop (fast-+-c/signed=>signed fast-safe-arith-op)
283 (:args (x :target r :scs (signed-reg signed-stack)))
285 (:arg-types signed-num (:constant (signed-byte 32)))
286 (:results (r :scs (signed-reg)
287 :load-if (not (location= x r))))
288 (:result-types signed-num)
289 (:note "inline (signed-byte 32) arithmetic")
291 (cond ((and (sc-is x signed-reg) (sc-is r signed-reg)
292 (not (location= x r)))
293 (inst lea r (make-ea :dword :base x :disp y)))
300 (define-vop (fast-+/unsigned=>unsigned fast-safe-arith-op)
302 (:args (x :scs (unsigned-reg) :target r
303 :load-if (not (and (sc-is x unsigned-stack)
304 (sc-is y unsigned-reg)
305 (sc-is r unsigned-stack)
307 (y :scs (unsigned-reg unsigned-stack)))
308 (:arg-types unsigned-num unsigned-num)
309 (:results (r :scs (unsigned-reg) :from (:argument 0)
310 :load-if (not (and (sc-is x unsigned-stack)
311 (sc-is y unsigned-reg)
312 (sc-is r unsigned-stack)
314 (:result-types unsigned-num)
315 (:note "inline (unsigned-byte 32) arithmetic")
317 (cond ((and (sc-is x unsigned-reg) (sc-is y unsigned-reg)
318 (sc-is r unsigned-reg) (not (location= x r)))
319 (inst lea r (make-ea :dword :base x :index y :scale 1)))
324 (define-vop (fast-+-c/unsigned=>unsigned fast-safe-arith-op)
326 (:args (x :target r :scs (unsigned-reg unsigned-stack)))
328 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
329 (:results (r :scs (unsigned-reg)
330 :load-if (not (location= x r))))
331 (:result-types unsigned-num)
332 (:note "inline (unsigned-byte 32) arithmetic")
334 (cond ((and (sc-is x unsigned-reg) (sc-is r unsigned-reg)
335 (not (location= x r)))
336 (inst lea r (make-ea :dword :base x :disp y)))
343 ;;;; multiplication and division
345 (define-vop (fast-*/fixnum=>fixnum fast-safe-arith-op)
347 ;; We need different loading characteristics.
348 (:args (x :scs (any-reg) :target r)
349 (y :scs (any-reg control-stack)))
350 (:arg-types tagged-num tagged-num)
351 (:results (r :scs (any-reg) :from (:argument 0)))
352 (:result-types tagged-num)
353 (:note "inline fixnum arithmetic")
359 (define-vop (fast-*-c/fixnum=>fixnum fast-safe-arith-op)
361 ;; We need different loading characteristics.
362 (:args (x :scs (any-reg control-stack)))
364 (:arg-types tagged-num (:constant (signed-byte 30)))
365 (:results (r :scs (any-reg)))
366 (:result-types tagged-num)
367 (:note "inline fixnum arithmetic")
371 (define-vop (fast-*/signed=>signed fast-safe-arith-op)
373 ;; We need different loading characteristics.
374 (:args (x :scs (signed-reg) :target r)
375 (y :scs (signed-reg signed-stack)))
376 (:arg-types signed-num signed-num)
377 (:results (r :scs (signed-reg) :from (:argument 0)))
378 (:result-types signed-num)
379 (:note "inline (signed-byte 32) arithmetic")
384 (define-vop (fast-*-c/signed=>signed fast-safe-arith-op)
386 ;; We need different loading characteristics.
387 (:args (x :scs (signed-reg signed-stack)))
389 (:arg-types signed-num (:constant (signed-byte 32)))
390 (:results (r :scs (signed-reg)))
391 (:result-types signed-num)
392 (:note "inline (signed-byte 32) arithmetic")
396 (define-vop (fast-*/unsigned=>unsigned fast-safe-arith-op)
398 (:args (x :scs (unsigned-reg) :target eax)
399 (y :scs (unsigned-reg unsigned-stack)))
400 (:arg-types unsigned-num unsigned-num)
401 (:temporary (:sc unsigned-reg :offset eax-offset :target result
402 :from (:argument 0) :to :result) eax)
403 (:temporary (:sc unsigned-reg :offset edx-offset
404 :from :eval :to :result) edx)
406 (:results (result :scs (unsigned-reg)))
407 (:result-types unsigned-num)
408 (:note "inline (unsigned-byte 32) arithmetic")
410 (:save-p :compute-only)
417 (define-vop (fast-truncate/fixnum=>fixnum fast-safe-arith-op)
418 (:translate truncate)
419 (:args (x :scs (any-reg) :target eax)
420 (y :scs (any-reg control-stack)))
421 (:arg-types tagged-num tagged-num)
422 (:temporary (:sc signed-reg :offset eax-offset :target quo
423 :from (:argument 0) :to (:result 0)) eax)
424 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
425 :from (:argument 0) :to (:result 1)) edx)
426 (:results (quo :scs (any-reg))
427 (rem :scs (any-reg)))
428 (:result-types tagged-num tagged-num)
429 (:note "inline fixnum arithmetic")
431 (:save-p :compute-only)
433 (let ((zero (generate-error-code vop division-by-zero-error x y)))
434 (if (sc-is y any-reg)
435 (inst test y y) ; smaller instruction
441 (if (location= quo eax)
443 (inst lea quo (make-ea :dword :index eax :scale 4)))
446 (define-vop (fast-truncate-c/fixnum=>fixnum fast-safe-arith-op)
447 (:translate truncate)
448 (:args (x :scs (any-reg) :target eax))
450 (:arg-types tagged-num (:constant (signed-byte 30)))
451 (:temporary (:sc signed-reg :offset eax-offset :target quo
452 :from :argument :to (:result 0)) eax)
453 (:temporary (:sc any-reg :offset edx-offset :target rem
454 :from :eval :to (:result 1)) edx)
455 (:temporary (:sc any-reg :from :eval :to :result) y-arg)
456 (:results (quo :scs (any-reg))
457 (rem :scs (any-reg)))
458 (:result-types tagged-num tagged-num)
459 (:note "inline fixnum arithmetic")
461 (:save-p :compute-only)
465 (inst mov y-arg (fixnumize y))
466 (inst idiv eax y-arg)
467 (if (location= quo eax)
469 (inst lea quo (make-ea :dword :index eax :scale 4)))
472 (define-vop (fast-truncate/unsigned=>unsigned fast-safe-arith-op)
473 (:translate truncate)
474 (:args (x :scs (unsigned-reg) :target eax)
475 (y :scs (unsigned-reg signed-stack)))
476 (:arg-types unsigned-num unsigned-num)
477 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
478 :from (:argument 0) :to (:result 0)) eax)
479 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
480 :from (:argument 0) :to (:result 1)) edx)
481 (:results (quo :scs (unsigned-reg))
482 (rem :scs (unsigned-reg)))
483 (:result-types unsigned-num unsigned-num)
484 (:note "inline (unsigned-byte 32) arithmetic")
486 (:save-p :compute-only)
488 (let ((zero (generate-error-code vop division-by-zero-error x y)))
489 (if (sc-is y unsigned-reg)
490 (inst test y y) ; smaller instruction
499 (define-vop (fast-truncate-c/unsigned=>unsigned fast-safe-arith-op)
500 (:translate truncate)
501 (:args (x :scs (unsigned-reg) :target eax))
503 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
504 (:temporary (:sc unsigned-reg :offset eax-offset :target quo
505 :from :argument :to (:result 0)) eax)
506 (:temporary (:sc unsigned-reg :offset edx-offset :target rem
507 :from :eval :to (:result 1)) edx)
508 (:temporary (:sc unsigned-reg :from :eval :to :result) y-arg)
509 (:results (quo :scs (unsigned-reg))
510 (rem :scs (unsigned-reg)))
511 (:result-types unsigned-num unsigned-num)
512 (:note "inline (unsigned-byte 32) arithmetic")
514 (:save-p :compute-only)
523 (define-vop (fast-truncate/signed=>signed fast-safe-arith-op)
524 (:translate truncate)
525 (:args (x :scs (signed-reg) :target eax)
526 (y :scs (signed-reg signed-stack)))
527 (:arg-types signed-num signed-num)
528 (:temporary (:sc signed-reg :offset eax-offset :target quo
529 :from (:argument 0) :to (:result 0)) eax)
530 (:temporary (:sc signed-reg :offset edx-offset :target rem
531 :from (:argument 0) :to (:result 1)) edx)
532 (:results (quo :scs (signed-reg))
533 (rem :scs (signed-reg)))
534 (:result-types signed-num signed-num)
535 (:note "inline (signed-byte 32) arithmetic")
537 (:save-p :compute-only)
539 (let ((zero (generate-error-code vop division-by-zero-error x y)))
540 (if (sc-is y signed-reg)
541 (inst test y y) ; smaller instruction
550 (define-vop (fast-truncate-c/signed=>signed fast-safe-arith-op)
551 (:translate truncate)
552 (:args (x :scs (signed-reg) :target eax))
554 (:arg-types signed-num (:constant (signed-byte 32)))
555 (:temporary (:sc signed-reg :offset eax-offset :target quo
556 :from :argument :to (:result 0)) eax)
557 (:temporary (:sc signed-reg :offset edx-offset :target rem
558 :from :eval :to (:result 1)) edx)
559 (:temporary (:sc signed-reg :from :eval :to :result) y-arg)
560 (:results (quo :scs (signed-reg))
561 (rem :scs (signed-reg)))
562 (:result-types signed-num signed-num)
563 (:note "inline (signed-byte 32) arithmetic")
565 (:save-p :compute-only)
570 (inst idiv eax y-arg)
577 (define-vop (fast-ash-c/fixnum=>fixnum)
580 (:args (number :scs (any-reg) :target result
581 :load-if (not (and (sc-is number any-reg control-stack)
582 (sc-is result any-reg control-stack)
583 (location= number result)))))
585 (:arg-types tagged-num (:constant integer))
586 (:results (result :scs (any-reg)
587 :load-if (not (and (sc-is number control-stack)
588 (sc-is result control-stack)
589 (location= number result)))))
590 (:result-types tagged-num)
593 (cond ((and (= amount 1) (not (location= number result)))
594 (inst lea result (make-ea :dword :index number :scale 2)))
595 ((and (= amount 2) (not (location= number result)))
596 (inst lea result (make-ea :dword :index number :scale 4)))
597 ((and (= amount 3) (not (location= number result)))
598 (inst lea result (make-ea :dword :index number :scale 8)))
601 (cond ((plusp amount)
602 ;; We don't have to worry about overflow because of the
603 ;; result type restriction.
604 (inst shl result amount))
606 ;; If the amount is greater than 31, only shift by 31. We
607 ;; have to do this because the shift instructions only look
608 ;; at the low five bits of the result.
609 (inst sar result (min 31 (- amount)))
610 ;; Fixnum correction.
611 (inst and result #xfffffffc)))))))
613 (define-vop (fast-ash-left/fixnum=>fixnum)
615 (:args (number :scs (any-reg) :target result
616 :load-if (not (and (sc-is number control-stack)
617 (sc-is result control-stack)
618 (location= number result))))
619 (amount :scs (unsigned-reg) :target ecx))
620 (:arg-types tagged-num positive-fixnum)
621 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
622 (:results (result :scs (any-reg) :from (:argument 0)
623 :load-if (not (and (sc-is number control-stack)
624 (sc-is result control-stack)
625 (location= number result)))))
626 (:result-types tagged-num)
632 ;; The result-type ensures us that this shift will not overflow.
633 (inst shl result :cl)))
635 (define-vop (fast-ash-c/signed=>signed)
638 (:args (number :scs (signed-reg) :target result
639 :load-if (not (and (sc-is number signed-stack)
640 (sc-is result signed-stack)
641 (location= number result)))))
643 (:arg-types signed-num (:constant integer))
644 (:results (result :scs (signed-reg)
645 :load-if (not (and (sc-is number signed-stack)
646 (sc-is result signed-stack)
647 (location= number result)))))
648 (:result-types signed-num)
651 (cond ((and (= amount 1) (not (location= number result)))
652 (inst lea result (make-ea :dword :index number :scale 2)))
653 ((and (= amount 2) (not (location= number result)))
654 (inst lea result (make-ea :dword :index number :scale 4)))
655 ((and (= amount 3) (not (location= number result)))
656 (inst lea result (make-ea :dword :index number :scale 8)))
659 (cond ((plusp amount) (inst shl result amount))
660 (t (inst sar result (min 31 (- amount)))))))))
662 (define-vop (fast-ash-c/unsigned=>unsigned)
665 (:args (number :scs (unsigned-reg) :target result
666 :load-if (not (and (sc-is number unsigned-stack)
667 (sc-is result unsigned-stack)
668 (location= number result)))))
670 (:arg-types unsigned-num (:constant integer))
671 (:results (result :scs (unsigned-reg)
672 :load-if (not (and (sc-is number unsigned-stack)
673 (sc-is result unsigned-stack)
674 (location= number result)))))
675 (:result-types unsigned-num)
678 (cond ((and (= amount 1) (not (location= number result)))
679 (inst lea result (make-ea :dword :index number :scale 2)))
680 ((and (= amount 2) (not (location= number result)))
681 (inst lea result (make-ea :dword :index number :scale 4)))
682 ((and (= amount 3) (not (location= number result)))
683 (inst lea result (make-ea :dword :index number :scale 8)))
686 (cond ((< -32 amount 32)
687 ;; this code is used both in ASH and ASH-MOD32, so
690 (inst shl result amount)
691 (inst shr result (- amount))))
692 (t (if (sc-is result unsigned-reg)
693 (inst xor result result)
694 (inst mov result 0))))))))
696 (define-vop (fast-ash-left/signed=>signed)
698 (:args (number :scs (signed-reg) :target result
699 :load-if (not (and (sc-is number signed-stack)
700 (sc-is result signed-stack)
701 (location= number result))))
702 (amount :scs (unsigned-reg) :target ecx))
703 (:arg-types signed-num positive-fixnum)
704 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
705 (:results (result :scs (signed-reg) :from (:argument 0)
706 :load-if (not (and (sc-is number signed-stack)
707 (sc-is result signed-stack)
708 (location= number result)))))
709 (:result-types signed-num)
715 (inst shl result :cl)))
717 (define-vop (fast-ash-left/unsigned=>unsigned)
719 (:args (number :scs (unsigned-reg) :target result
720 :load-if (not (and (sc-is number unsigned-stack)
721 (sc-is result unsigned-stack)
722 (location= number result))))
723 (amount :scs (unsigned-reg) :target ecx))
724 (:arg-types unsigned-num positive-fixnum)
725 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
726 (:results (result :scs (unsigned-reg) :from (:argument 0)
727 :load-if (not (and (sc-is number unsigned-stack)
728 (sc-is result unsigned-stack)
729 (location= number result)))))
730 (:result-types unsigned-num)
736 (inst shl result :cl)))
738 (define-vop (fast-ash/signed=>signed)
741 (:args (number :scs (signed-reg) :target result)
742 (amount :scs (signed-reg) :target ecx))
743 (:arg-types signed-num signed-num)
744 (:results (result :scs (signed-reg) :from (:argument 0)))
745 (:result-types signed-num)
746 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
752 (inst jmp :ns positive)
758 (inst sar result :cl)
762 ;; The result-type ensures us that this shift will not overflow.
763 (inst shl result :cl)
767 (define-vop (fast-ash/unsigned=>unsigned)
770 (:args (number :scs (unsigned-reg) :target result)
771 (amount :scs (signed-reg) :target ecx))
772 (:arg-types unsigned-num signed-num)
773 (:results (result :scs (unsigned-reg) :from (:argument 0)))
774 (:result-types unsigned-num)
775 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
781 (inst jmp :ns positive)
785 (inst xor result result)
788 (inst shr result :cl)
792 ;; The result-type ensures us that this shift will not overflow.
793 (inst shl result :cl)
797 ;;; FIXME: before making knowledge of this too public, it needs to be
798 ;;; fixed so that it's actually _faster_ than the non-CMOV version; at
799 ;;; least on my Celeron-XXX laptop, this version is marginally slower
800 ;;; than the above version with branches. -- CSR, 2003-09-04
801 (define-vop (fast-cmov-ash/unsigned=>unsigned)
804 (:args (number :scs (unsigned-reg) :target result)
805 (amount :scs (signed-reg) :target ecx))
806 (:arg-types unsigned-num signed-num)
807 (:results (result :scs (unsigned-reg) :from (:argument 0)))
808 (:result-types unsigned-num)
809 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
810 (:temporary (:sc any-reg :from (:eval 0) :to (:eval 1)) zero)
812 (:guard (member :cmov *backend-subfeatures*))
817 (inst jmp :ns positive)
820 (inst shr result :cl)
822 (inst cmov :nbe result zero)
826 ;; The result-type ensures us that this shift will not overflow.
827 (inst shl result :cl)
831 ;;; Note: documentation for this function is wrong - rtfm
832 (define-vop (signed-byte-32-len)
833 (:translate integer-length)
834 (:note "inline (signed-byte 32) integer-length")
836 (:args (arg :scs (signed-reg) :target res))
837 (:arg-types signed-num)
838 (:results (res :scs (unsigned-reg)))
839 (:result-types unsigned-num)
854 (define-vop (unsigned-byte-32-len)
855 (:translate integer-length)
856 (:note "inline (unsigned-byte 32) integer-length")
858 (:args (arg :scs (unsigned-reg)))
859 (:arg-types unsigned-num)
860 (:results (res :scs (unsigned-reg)))
861 (:result-types unsigned-num)
871 (define-vop (unsigned-byte-32-count)
872 (:translate logcount)
873 (:note "inline (unsigned-byte 32) logcount")
875 (:args (arg :scs (unsigned-reg)))
876 (:arg-types unsigned-num)
877 (:results (result :scs (unsigned-reg)))
878 (:result-types positive-fixnum)
879 (:temporary (:sc unsigned-reg :from (:argument 0)) temp)
883 (inst mov temp result)
885 (inst and result #x55555555)
886 (inst and temp #x55555555)
887 (inst add result temp)
889 (inst mov temp result)
891 (inst and result #x33333333)
892 (inst and temp #x33333333)
893 (inst add result temp)
895 (inst mov temp result)
897 (inst and result #x0f0f0f0f)
898 (inst and temp #x0f0f0f0f)
899 (inst add result temp)
901 (inst mov temp result)
903 (inst and result #x00ff00ff)
904 (inst and temp #x00ff00ff)
905 (inst add result temp)
907 (inst mov temp result)
909 (inst and result #x0000ffff)
910 (inst and temp #x0000ffff)
911 (inst add result temp)))
913 ;;;; binary conditional VOPs
915 (define-vop (fast-conditional)
920 (:policy :fast-safe))
922 (define-vop (fast-conditional/fixnum fast-conditional)
923 (:args (x :scs (any-reg)
924 :load-if (not (and (sc-is x control-stack)
926 (y :scs (any-reg control-stack)))
927 (:arg-types tagged-num tagged-num)
928 (:note "inline fixnum comparison"))
930 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
931 (:args (x :scs (any-reg control-stack)))
932 (:arg-types tagged-num (:constant (signed-byte 30)))
933 (:info target not-p y))
935 (define-vop (fast-conditional/signed fast-conditional)
936 (:args (x :scs (signed-reg)
937 :load-if (not (and (sc-is x signed-stack)
938 (sc-is y signed-reg))))
939 (y :scs (signed-reg signed-stack)))
940 (:arg-types signed-num signed-num)
941 (:note "inline (signed-byte 32) comparison"))
943 (define-vop (fast-conditional-c/signed fast-conditional/signed)
944 (:args (x :scs (signed-reg signed-stack)))
945 (:arg-types signed-num (:constant (signed-byte 32)))
946 (:info target not-p y))
948 (define-vop (fast-conditional/unsigned fast-conditional)
949 (:args (x :scs (unsigned-reg)
950 :load-if (not (and (sc-is x unsigned-stack)
951 (sc-is y unsigned-reg))))
952 (y :scs (unsigned-reg unsigned-stack)))
953 (:arg-types unsigned-num unsigned-num)
954 (:note "inline (unsigned-byte 32) comparison"))
956 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
957 (:args (x :scs (unsigned-reg unsigned-stack)))
958 (:arg-types unsigned-num (:constant (unsigned-byte 32)))
959 (:info target not-p y))
962 (macrolet ((define-conditional-vop (tran cond unsigned not-cond not-unsigned)
965 (lambda (suffix cost signed)
966 `(define-vop (;; FIXME: These could be done more
967 ;; cleanly with SYMBOLICATE.
968 ,(intern (format nil "~:@(FAST-IF-~A~A~)"
971 (format nil "~:@(FAST-CONDITIONAL~A~)"
976 ,(if (eq suffix '-c/fixnum)
987 '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
989 '(t t t t nil nil)))))
991 (define-conditional-vop < :l :b :ge :ae)
992 (define-conditional-vop > :g :a :le :be))
994 (define-vop (fast-if-eql/signed fast-conditional/signed)
998 (inst jmp (if not-p :ne :e) target)))
1000 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
1003 (cond ((and (sc-is x signed-reg) (zerop y))
1004 (inst test x x)) ; smaller instruction
1007 (inst jmp (if not-p :ne :e) target)))
1009 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
1013 (inst jmp (if not-p :ne :e) target)))
1015 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
1018 (cond ((and (sc-is x unsigned-reg) (zerop y))
1019 (inst test x x)) ; smaller instruction
1022 (inst jmp (if not-p :ne :e) target)))
1024 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
1027 ;;; These versions specify a fixnum restriction on their first arg. We have
1028 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
1029 ;;; the first arg and a higher cost. The reason for doing this is to prevent
1030 ;;; fixnum specific operations from being used on word integers, spuriously
1031 ;;; consing the argument.
1033 (define-vop (fast-eql/fixnum fast-conditional)
1034 (:args (x :scs (any-reg)
1035 :load-if (not (and (sc-is x control-stack)
1036 (sc-is y any-reg))))
1037 (y :scs (any-reg control-stack)))
1038 (:arg-types tagged-num tagged-num)
1039 (:note "inline fixnum comparison")
1043 (inst jmp (if not-p :ne :e) target)))
1044 (define-vop (generic-eql/fixnum fast-eql/fixnum)
1045 (:args (x :scs (any-reg descriptor-reg)
1046 :load-if (not (and (sc-is x control-stack)
1047 (sc-is y any-reg))))
1048 (y :scs (any-reg control-stack)))
1049 (:arg-types * tagged-num)
1052 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
1053 (:args (x :scs (any-reg control-stack)))
1054 (:arg-types tagged-num (:constant (signed-byte 30)))
1055 (:info target not-p y)
1058 (cond ((and (sc-is x any-reg) (zerop y))
1059 (inst test x x)) ; smaller instruction
1061 (inst cmp x (fixnumize y))))
1062 (inst jmp (if not-p :ne :e) target)))
1063 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
1064 (:args (x :scs (any-reg descriptor-reg control-stack)))
1065 (:arg-types * (:constant (signed-byte 30)))
1068 ;;;; 32-bit logical operations
1070 (define-vop (merge-bits)
1071 (:translate merge-bits)
1072 (:args (shift :scs (signed-reg unsigned-reg) :target ecx)
1073 (prev :scs (unsigned-reg) :target result)
1074 (next :scs (unsigned-reg)))
1075 (:arg-types tagged-num unsigned-num unsigned-num)
1076 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 0)) ecx)
1077 (:results (result :scs (unsigned-reg) :from (:argument 1)))
1078 (:result-types unsigned-num)
1079 (:policy :fast-safe)
1083 (inst shrd result next :cl)))
1085 (define-source-transform 32bit-logical-not (x)
1086 `(logand (lognot (the (unsigned-byte 32) ,x)) #.(1- (ash 1 32))))
1088 (deftransform 32bit-logical-and ((x y))
1091 (define-source-transform 32bit-logical-nand (x y)
1092 `(32bit-logical-not (32bit-logical-and ,x ,y)))
1094 (deftransform 32bit-logical-or ((x y))
1097 (define-source-transform 32bit-logical-nor (x y)
1098 `(32bit-logical-not (32bit-logical-or ,x ,y)))
1100 (deftransform 32bit-logical-xor ((x y))
1103 (define-source-transform 32bit-logical-eqv (x y)
1104 `(32bit-logical-not (32bit-logical-xor ,x ,y)))
1106 (define-source-transform 32bit-logical-orc1 (x y)
1107 `(32bit-logical-or (32bit-logical-not ,x) ,y))
1109 (define-source-transform 32bit-logical-orc2 (x y)
1110 `(32bit-logical-or ,x (32bit-logical-not ,y)))
1112 (define-source-transform 32bit-logical-andc1 (x y)
1113 `(32bit-logical-and (32bit-logical-not ,x) ,y))
1115 (define-source-transform 32bit-logical-andc2 (x y)
1116 `(32bit-logical-and ,x (32bit-logical-not ,y)))
1118 ;;; Only the lower 5 bits of the shift amount are significant.
1119 (define-vop (shift-towards-someplace)
1120 (:policy :fast-safe)
1121 (:args (num :scs (unsigned-reg) :target r)
1122 (amount :scs (signed-reg) :target ecx))
1123 (:arg-types unsigned-num tagged-num)
1124 (:temporary (:sc signed-reg :offset ecx-offset :from (:argument 1)) ecx)
1125 (:results (r :scs (unsigned-reg) :from (:argument 0)))
1126 (:result-types unsigned-num))
1128 (define-vop (shift-towards-start shift-towards-someplace)
1129 (:translate shift-towards-start)
1130 (:note "SHIFT-TOWARDS-START")
1136 (define-vop (shift-towards-end shift-towards-someplace)
1137 (:translate shift-towards-end)
1138 (:note "SHIFT-TOWARDS-END")
1144 ;;;; Modular functions
1146 (define-modular-fun +-mod32 (x y) + 32)
1147 (define-vop (fast-+-mod32/unsigned=>unsigned fast-+/unsigned=>unsigned)
1148 (:translate +-mod32))
1149 (define-vop (fast-+-mod32-c/unsigned=>unsigned fast-+-c/unsigned=>unsigned)
1150 (:translate +-mod32))
1151 (define-modular-fun --mod32 (x y) - 32)
1152 (define-vop (fast---mod32/unsigned=>unsigned fast--/unsigned=>unsigned)
1153 (:translate --mod32))
1154 (define-vop (fast---mod32-c/unsigned=>unsigned fast---c/unsigned=>unsigned)
1155 (:translate --mod32))
1157 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
1158 fast-ash-c/unsigned=>unsigned)
1159 (:translate ash-left-mod32))
1161 ;;; logical operations
1162 (define-modular-fun lognot-mod32 (x) lognot 32)
1163 (define-vop (lognot-mod32/unsigned=>unsigned)
1164 (:translate lognot-mod32)
1165 (:args (x :scs (unsigned-reg unsigned-stack) :target r
1166 :load-if (not (and (sc-is x unsigned-stack)
1167 (sc-is r unsigned-stack)
1169 (:arg-types unsigned-num)
1170 (:results (r :scs (unsigned-reg)
1171 :load-if (not (and (sc-is x unsigned-stack)
1172 (sc-is r unsigned-stack)
1174 (:result-types unsigned-num)
1175 (:policy :fast-safe)
1180 (define-modular-fun logxor-mod32 (x y) logxor 32)
1181 (define-vop (fast-logxor-mod32/unsigned=>unsigned
1182 fast-logxor/unsigned=>unsigned)
1183 (:translate logxor-mod32))
1184 (define-vop (fast-logxor-mod32-c/unsigned=>unsigned
1185 fast-logxor-c/unsigned=>unsigned)
1186 (:translate logxor-mod32))
1188 (define-source-transform logeqv (&rest args)
1189 (if (oddp (length args))
1191 `(lognot (logxor ,@args))))
1192 (define-source-transform logandc1 (x y)
1193 `(logand (lognot ,x) ,y))
1194 (define-source-transform logandc2 (x y)
1195 `(logand ,x (lognot ,y)))
1196 (define-source-transform logorc1 (x y)
1197 `(logior (lognot ,x) ,y))
1198 (define-source-transform logorc2 (x y)
1199 `(logior ,x (lognot ,y)))
1200 (define-source-transform lognor (x y)
1201 `(lognot (logior ,x ,y)))
1202 (define-source-transform lognand (x y)
1203 `(lognot (logand ,x ,y)))
1207 (define-vop (bignum-length get-header-data)
1208 (:translate sb!bignum:%bignum-length)
1209 (:policy :fast-safe))
1211 (define-vop (bignum-set-length set-header-data)
1212 (:translate sb!bignum:%bignum-set-length)
1213 (:policy :fast-safe))
1215 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
1216 (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
1218 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
1219 (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
1221 (define-vop (digit-0-or-plus)
1222 (:translate sb!bignum:%digit-0-or-plusp)
1223 (:policy :fast-safe)
1224 (:args (digit :scs (unsigned-reg)))
1225 (:arg-types unsigned-num)
1227 (:info target not-p)
1229 (inst or digit digit)
1230 (inst jmp (if not-p :s :ns) target)))
1233 ;;; For add and sub with carry the sc of carry argument is any-reg so
1234 ;;; the it may be passed as a fixnum or word and thus may be 0, 1, or
1235 ;;; 4. This is easy to deal with and may save a fixnum-word
1237 (define-vop (add-w/carry)
1238 (:translate sb!bignum:%add-with-carry)
1239 (:policy :fast-safe)
1240 (:args (a :scs (unsigned-reg) :target result)
1241 (b :scs (unsigned-reg unsigned-stack) :to :eval)
1242 (c :scs (any-reg) :target temp))
1243 (:arg-types unsigned-num unsigned-num positive-fixnum)
1244 (:temporary (:sc any-reg :from (:argument 2) :to :eval) temp)
1245 (:results (result :scs (unsigned-reg) :from (:argument 0))
1246 (carry :scs (unsigned-reg)))
1247 (:result-types unsigned-num positive-fixnum)
1251 (inst neg temp) ; Set the carry flag to 0 if c=0 else to 1
1254 (inst adc carry carry)))
1256 ;;; Note: the borrow is the oppostite of the x86 convention - 1 for no
1257 ;;; borrow and 0 for a borrow.
1258 (define-vop (sub-w/borrow)
1259 (:translate sb!bignum:%subtract-with-borrow)
1260 (:policy :fast-safe)
1261 (:args (a :scs (unsigned-reg) :to :eval :target result)
1262 (b :scs (unsigned-reg unsigned-stack) :to :result)
1263 (c :scs (any-reg control-stack)))
1264 (:arg-types unsigned-num unsigned-num positive-fixnum)
1265 (:results (result :scs (unsigned-reg) :from :eval)
1266 (borrow :scs (unsigned-reg)))
1267 (:result-types unsigned-num positive-fixnum)
1269 (inst cmp c 1) ; Set the carry flag to 1 if c=0 else to 0
1273 (inst adc borrow borrow)
1274 (inst xor borrow 1)))
1277 (define-vop (bignum-mult-and-add-3-arg)
1278 (:translate sb!bignum:%multiply-and-add)
1279 (:policy :fast-safe)
1280 (:args (x :scs (unsigned-reg) :target eax)
1281 (y :scs (unsigned-reg unsigned-stack))
1282 (carry-in :scs (unsigned-reg unsigned-stack)))
1283 (:arg-types unsigned-num unsigned-num unsigned-num)
1284 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1285 :to (:result 1) :target lo) eax)
1286 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1287 :to (:result 0) :target hi) edx)
1288 (:results (hi :scs (unsigned-reg))
1289 (lo :scs (unsigned-reg)))
1290 (:result-types unsigned-num unsigned-num)
1294 (inst add eax carry-in)
1299 (define-vop (bignum-mult-and-add-4-arg)
1300 (:translate sb!bignum:%multiply-and-add)
1301 (:policy :fast-safe)
1302 (:args (x :scs (unsigned-reg) :target eax)
1303 (y :scs (unsigned-reg unsigned-stack))
1304 (prev :scs (unsigned-reg unsigned-stack))
1305 (carry-in :scs (unsigned-reg unsigned-stack)))
1306 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1307 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1308 :to (:result 1) :target lo) eax)
1309 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1310 :to (:result 0) :target hi) edx)
1311 (:results (hi :scs (unsigned-reg))
1312 (lo :scs (unsigned-reg)))
1313 (:result-types unsigned-num unsigned-num)
1319 (inst add eax carry-in)
1325 (define-vop (bignum-mult)
1326 (:translate sb!bignum:%multiply)
1327 (:policy :fast-safe)
1328 (:args (x :scs (unsigned-reg) :target eax)
1329 (y :scs (unsigned-reg unsigned-stack)))
1330 (:arg-types unsigned-num unsigned-num)
1331 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 0)
1332 :to (:result 1) :target lo) eax)
1333 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 1)
1334 :to (:result 0) :target hi) edx)
1335 (:results (hi :scs (unsigned-reg))
1336 (lo :scs (unsigned-reg)))
1337 (:result-types unsigned-num unsigned-num)
1344 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
1345 (:translate sb!bignum:%lognot))
1347 (define-vop (fixnum-to-digit)
1348 (:translate sb!bignum:%fixnum-to-digit)
1349 (:policy :fast-safe)
1350 (:args (fixnum :scs (any-reg control-stack) :target digit))
1351 (:arg-types tagged-num)
1352 (:results (digit :scs (unsigned-reg)
1353 :load-if (not (and (sc-is fixnum control-stack)
1354 (sc-is digit unsigned-stack)
1355 (location= fixnum digit)))))
1356 (:result-types unsigned-num)
1359 (inst sar digit 2)))
1361 (define-vop (bignum-floor)
1362 (:translate sb!bignum:%floor)
1363 (:policy :fast-safe)
1364 (:args (div-high :scs (unsigned-reg) :target edx)
1365 (div-low :scs (unsigned-reg) :target eax)
1366 (divisor :scs (unsigned-reg unsigned-stack)))
1367 (:arg-types unsigned-num unsigned-num unsigned-num)
1368 (:temporary (:sc unsigned-reg :offset eax-offset :from (:argument 1)
1369 :to (:result 0) :target quo) eax)
1370 (:temporary (:sc unsigned-reg :offset edx-offset :from (:argument 0)
1371 :to (:result 1) :target rem) edx)
1372 (:results (quo :scs (unsigned-reg))
1373 (rem :scs (unsigned-reg)))
1374 (:result-types unsigned-num unsigned-num)
1378 (inst div eax divisor)
1382 (define-vop (signify-digit)
1383 (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1384 (:policy :fast-safe)
1385 (:args (digit :scs (unsigned-reg unsigned-stack) :target res))
1386 (:arg-types unsigned-num)
1387 (:results (res :scs (any-reg signed-reg)
1388 :load-if (not (and (sc-is digit unsigned-stack)
1389 (sc-is res control-stack signed-stack)
1390 (location= digit res)))))
1391 (:result-types signed-num)
1394 (when (sc-is res any-reg control-stack)
1397 (define-vop (digit-ashr)
1398 (:translate sb!bignum:%ashr)
1399 (:policy :fast-safe)
1400 (:args (digit :scs (unsigned-reg unsigned-stack) :target result)
1401 (count :scs (unsigned-reg) :target ecx))
1402 (:arg-types unsigned-num positive-fixnum)
1403 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 1)) ecx)
1404 (:results (result :scs (unsigned-reg) :from (:argument 0)
1405 :load-if (not (and (sc-is result unsigned-stack)
1406 (location= digit result)))))
1407 (:result-types unsigned-num)
1411 (inst sar result :cl)))
1413 (define-vop (digit-lshr digit-ashr)
1414 (:translate sb!bignum:%digit-logical-shift-right)
1418 (inst shr result :cl)))
1420 (define-vop (digit-ashl digit-ashr)
1421 (:translate sb!bignum:%ashl)
1425 (inst shl result :cl)))
1427 ;;;; static functions
1429 (define-static-fun two-arg-/ (x y) :translate /)
1431 (define-static-fun two-arg-gcd (x y) :translate gcd)
1432 (define-static-fun two-arg-lcm (x y) :translate lcm)
1434 (define-static-fun two-arg-and (x y) :translate logand)
1435 (define-static-fun two-arg-ior (x y) :translate logior)
1436 (define-static-fun two-arg-xor (x y) :translate logxor)
1439 ;;; Support for the Mersenne Twister, MT19937, random number generator
1440 ;;; due to Matsumoto and Nishimura.
1442 ;;; Makoto Matsumoto and T. Nishimura, "Mersenne twister: A
1443 ;;; 623-dimensionally equidistributed uniform pseudorandom number
1444 ;;; generator.", ACM Transactions on Modeling and Computer Simulation,
1445 ;;; 1997, to appear.
1448 ;;; 0-1: Constant matrix A. [0, #x9908b0df] (not used here)
1449 ;;; 2: Index; init. to 1.
1451 (defknown random-mt19937 ((simple-array (unsigned-byte 32) (*)))
1452 (unsigned-byte 32) ())
1453 (define-vop (random-mt19937)
1454 (:policy :fast-safe)
1455 (:translate random-mt19937)
1456 (:args (state :scs (descriptor-reg) :to :result))
1457 (:arg-types simple-array-unsigned-byte-32)
1458 (:temporary (:sc unsigned-reg :from (:eval 0) :to :result) k)
1459 (:temporary (:sc unsigned-reg :offset eax-offset
1460 :from (:eval 0) :to :result) tmp)
1461 (:results (y :scs (unsigned-reg) :from (:eval 0)))
1462 (:result-types unsigned-num)
1464 (inst mov k (make-ea :dword :base state
1465 :disp (- (* (+ 2 vector-data-offset)
1467 other-pointer-lowtag)))
1469 (inst jmp :ne no-update)
1470 (inst mov tmp state) ; The state is passed in EAX.
1471 (inst call (make-fixup 'random-mt19937-update :assembly-routine))
1472 ;; Restore k, and set to 0.
1476 (inst mov y (make-ea :dword :base state :index k :scale 4
1477 :disp (- (* (+ 3 vector-data-offset)
1479 other-pointer-lowtag)))
1482 (inst xor y (make-ea :dword :base state :index k :scale 4
1483 :disp (- (* (+ 3 vector-data-offset)
1485 other-pointer-lowtag)))
1486 ;; y ^= (y << 7) & #x9d2c5680
1490 (inst mov (make-ea :dword :base state
1491 :disp (- (* (+ 2 vector-data-offset)
1493 other-pointer-lowtag))
1495 (inst and tmp #x9d2c5680)
1497 ;; y ^= (y << 15) & #xefc60000
1500 (inst and tmp #xefc60000)
1509 (defknown %lea ((or (signed-byte 32) (unsigned-byte 32))
1510 (or (signed-byte 32) (unsigned-byte 32))
1511 (member 1 2 4 8) (signed-byte 32))
1512 (or (signed-byte 32) (unsigned-byte 32))
1513 (foldable flushable))
1515 (defoptimizer (%lea derive-type) ((base index scale disp))
1516 (when (and (constant-lvar-p scale)
1517 (constant-lvar-p disp))
1518 (let ((scale (lvar-value scale))
1519 (disp (lvar-value disp))
1520 (base-type (lvar-type base))
1521 (index-type (lvar-type index)))
1522 (when (and (numeric-type-p base-type)
1523 (numeric-type-p index-type))
1524 (let ((base-lo (numeric-type-low base-type))
1525 (base-hi (numeric-type-high base-type))
1526 (index-lo (numeric-type-low index-type))
1527 (index-hi (numeric-type-high index-type)))
1528 (make-numeric-type :class 'integer
1530 :low (when (and base-lo index-lo)
1531 (+ base-lo (* index-lo scale) disp))
1532 :high (when (and base-hi index-hi)
1533 (+ base-hi (* index-hi scale) disp))))))))
1535 (defun %lea (base index scale disp)
1536 (+ base (* index scale) disp))
1538 (in-package "SB!VM")
1540 (define-vop (%lea/unsigned=>unsigned)
1542 (:policy :fast-safe)
1543 (:args (base :scs (unsigned-reg))
1544 (index :scs (unsigned-reg)))
1546 (:arg-types unsigned-num unsigned-num
1547 (:constant (member 1 2 4 8))
1548 (:constant (signed-byte 32)))
1549 (:results (r :scs (unsigned-reg)))
1550 (:result-types unsigned-num)
1552 (inst lea r (make-ea :dword :base base :index index
1553 :scale scale :disp disp))))
1555 (define-vop (%lea/signed=>signed)
1557 (:policy :fast-safe)
1558 (:args (base :scs (signed-reg))
1559 (index :scs (signed-reg)))
1561 (:arg-types signed-num signed-num
1562 (:constant (member 1 2 4 8))
1563 (:constant (signed-byte 32)))
1564 (:results (r :scs (signed-reg)))
1565 (:result-types signed-num)
1567 (inst lea r (make-ea :dword :base base :index index
1568 :scale scale :disp disp))))
1570 (define-vop (%lea/fixnum=>fixnum)
1572 (:policy :fast-safe)
1573 (:args (base :scs (any-reg))
1574 (index :scs (any-reg)))
1576 (:arg-types tagged-num tagged-num
1577 (:constant (member 1 2 4 8))
1578 (:constant (signed-byte 32)))
1579 (:results (r :scs (any-reg)))
1580 (:result-types tagged-num)
1582 (inst lea r (make-ea :dword :base base :index index
1583 :scale scale :disp disp))))
1587 ;;; This is essentially a straight implementation of the algorithm in
1588 ;;; "Strength Reduction of Multiplications by Integer Constants",
1589 ;;; Youfeng Wu, ACM SIGPLAN Notices, Vol. 30, No.2, February 1995.
1590 (defun basic-decompose-multiplication (arg num n-bits condensed)
1591 (case (aref condensed 0)
1593 (let ((tmp (min 3 (aref condensed 1))))
1594 (decf (aref condensed 1) tmp)
1597 ,(decompose-multiplication
1598 arg (ash (1- num) (- tmp)) (1- n-bits) (subseq condensed 1))
1601 (let ((r0 (aref condensed 0)))
1602 (incf (aref condensed 1) r0)
1604 (%lea ,(decompose-multiplication
1605 arg (- num (ash 1 r0)) (1- n-bits) (subseq condensed 1))
1608 (t (let ((r0 (aref condensed 0)))
1609 (setf (aref condensed 0) 0)
1611 (ash ,(decompose-multiplication
1612 arg (ash num (- r0)) n-bits condensed)
1615 (defun decompose-multiplication (arg num n-bits condensed)
1620 `(logand #xffffffff (ash ,arg ,(1- (integer-length num)))))
1621 ((let ((max 0) (end 0))
1622 (loop for i from 2 to (length condensed)
1623 for j = (reduce #'+ (subseq condensed 0 i))
1624 when (and (> (- (* 2 i) 3 j) max)
1625 (< (+ (ash 1 (1+ j))
1626 (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num)
1629 do (setq max (- (* 2 i) 3 j)
1632 (let ((j (reduce #'+ (subseq condensed 0 end))))
1633 (let ((n2 (+ (ash 1 (1+ j))
1634 (ash (ldb (byte (- 32 (1+ j)) (1+ j)) num) (1+ j))))
1635 (n1 (1+ (ldb (byte (1+ j) 0) (lognot num)))))
1637 (- ,(optimize-multiply arg n2) ,(optimize-multiply arg n1))))))))
1638 ((dolist (i '(9 5 3))
1639 (when (integerp (/ num i))
1640 (when (< (logcount (/ num i)) (logcount num))
1642 (return `(let ((,x ,(optimize-multiply arg (/ num i))))
1644 (%lea ,x ,x (1- ,i) 0)))))))))
1645 (t (basic-decompose-multiplication arg num n-bits condensed))))
1647 (defun optimize-multiply (arg x)
1648 (let* ((n-bits (logcount x))
1649 (condensed (make-array n-bits)))
1650 (let ((count 0) (bit 0))
1652 (cond ((logbitp i x)
1653 (setf (aref condensed bit) count)
1657 (decompose-multiplication arg x n-bits condensed)))
1659 (deftransform * ((x y)
1660 ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1662 "recode as leas, shifts and adds"
1663 (let ((y (lvar-value y)))
1665 ((= y (ash 1 (integer-length y)))
1666 ;; there's a generic transform for y = 2^k
1667 (give-up-ir1-transform))
1668 ((member y '(3 5 9))
1669 ;; we can do these multiplications directly using LEA
1670 `(%lea x x ,(1- y) 0))
1671 ((member :pentium4 *backend-subfeatures*)
1672 ;; the pentium4's multiply unit is reportedly very good
1673 (give-up-ir1-transform))
1674 ;; FIXME: should make this more fine-grained. If nothing else,
1675 ;; there should probably be a cutoff of about 9 instructions on
1676 ;; pentium-class machines.
1677 (t (optimize-multiply 'x y)))))
1679 ;;; FIXME: we should also be able to write an optimizer or two to
1680 ;;; convert (+ (* x 2) 17), (- (* x 9) 5) to a %LEA.