7f2a0bd87423c56bc91401bbe135d3b5fe7c38cb
[sbcl.git] / src / compiler / alpha / arith.lisp
1 ;;;; the VM definition arithmetic VOPs for the Alpha
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; unary operations
15
16 (define-vop (fixnum-unop)
17   (:args (x :scs (any-reg)))
18   (:results (res :scs (any-reg)))
19   (:note "inline fixnum arithmetic")
20   (:arg-types tagged-num)
21   (:result-types tagged-num)
22   (:policy :fast-safe))
23
24 (define-vop (signed-unop)
25   (:args (x :scs (signed-reg)))
26   (:results (res :scs (signed-reg)))
27   (:note "inline (signed-byte 64) arithmetic")
28   (:arg-types signed-num)
29   (:result-types signed-num)
30   (:policy :fast-safe))
31
32 (define-vop (fast-negate/fixnum fixnum-unop)
33   (:translate %negate)
34   (:generator 1
35     (inst subq zero-tn x res)))
36
37 (define-vop (fast-negate/signed signed-unop)
38   (:translate %negate)
39   (:generator 2
40     (inst subq zero-tn x res)))
41
42 (define-vop (fast-lognot/fixnum fixnum-unop)
43   (:translate lognot)
44   (:generator 2
45     (inst eqv x zero-tn res)))
46
47 (define-vop (fast-lognot/signed signed-unop)
48   (:translate lognot)
49   (:generator 1
50     (inst not x res)))
51 \f
52 ;;;; binary fixnum operations
53
54 ;;; Assume that any constant operand is the second arg...
55
56 (define-vop (fast-fixnum-binop)
57   (:args (x :target r :scs (any-reg))
58          (y :target r :scs (any-reg)))
59   (:arg-types tagged-num tagged-num)
60   (:results (r :scs (any-reg)))
61   (:result-types tagged-num)
62   (:note "inline fixnum arithmetic")
63   (:effects)
64   (:affected)
65   (:policy :fast-safe))
66
67 (define-vop (fast-unsigned-binop)
68   (:args (x :target r :scs (unsigned-reg))
69          (y :target r :scs (unsigned-reg)))
70   (:arg-types unsigned-num unsigned-num)
71   (:results (r :scs (unsigned-reg)))
72   (:result-types unsigned-num)
73   (:note "inline (unsigned-byte 64) arithmetic")
74   (:effects)
75   (:affected)
76   (:policy :fast-safe))
77
78 (define-vop (fast-signed-binop)
79   (:args (x :target r :scs (signed-reg))
80          (y :target r :scs (signed-reg)))
81   (:arg-types signed-num signed-num)
82   (:results (r :scs (signed-reg)))
83   (:result-types signed-num)
84   (:note "inline (signed-byte 64) arithmetic")
85   (:effects)
86   (:affected)
87   (:policy :fast-safe))
88
89 (define-vop (fast-fixnum-c-binop fast-fixnum-binop)
90   (:args (x :target r :scs (any-reg)))
91   (:info y)
92   (:arg-types tagged-num (:constant integer)))
93
94 (define-vop (fast-signed-c-binop fast-signed-binop)
95   (:args (x :target r :scs (signed-reg)))
96   (:info y)
97   (:arg-types tagged-num (:constant integer)))
98
99 (define-vop (fast-unsigned-c-binop fast-unsigned-binop)
100   (:args (x :target r :scs (unsigned-reg)))
101   (:info y)
102   (:arg-types tagged-num (:constant integer)))
103
104 (defmacro define-binop (translate cost untagged-cost op 
105                         tagged-type untagged-type
106                         &optional arg-swap restore-fixnum-mask)
107   `(progn
108      (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
109                   fast-fixnum-binop)
110        ,@(when restore-fixnum-mask
111            `((:temporary (:sc non-descriptor-reg) temp)))
112        (:args (x ,@(unless restore-fixnum-mask `(:target r)) :scs (any-reg))
113               (y ,@(unless restore-fixnum-mask `(:target r)) :scs (any-reg)))
114        (:translate ,translate)
115        (:generator ,(1+ cost)
116          ,(if arg-swap
117               `(inst ,op y x ,(if restore-fixnum-mask 'temp 'r))
118               `(inst ,op x y ,(if restore-fixnum-mask 'temp 'r)))
119          ,@(when restore-fixnum-mask
120              `((inst bic temp #.(ash lowtag-mask -1) r)))))
121      (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
122                   fast-signed-binop)
123        (:args (x :target r :scs (signed-reg))
124               (y :target r :scs (signed-reg)))
125        (:translate ,translate)
126        (:generator ,(1+ untagged-cost)
127          ,(if arg-swap
128               `(inst ,op y x r)
129               `(inst ,op x y r))))
130      (define-vop (,(symbolicate "FAST-" translate "/UNSIGNED=>UNSIGNED")
131                   fast-unsigned-binop)
132        (:args (x :target r :scs (unsigned-reg))
133               (y :target r :scs (unsigned-reg)))
134        (:translate ,translate)
135        (:generator ,(1+ untagged-cost)
136          ,(if arg-swap
137               `(inst ,op y x r)
138               `(inst ,op x y r))))
139      ,@(when (and tagged-type (not arg-swap))
140          `((define-vop (,(symbolicate "FAST-" translate "-C/FIXNUM=>FIXNUM")
141                         fast-fixnum-c-binop)
142              (:arg-types tagged-num (:constant ,tagged-type))
143              ,@(when restore-fixnum-mask
144                  `((:temporary (:sc non-descriptor-reg) temp)))
145              (:translate ,translate)
146              (:generator ,cost
147                 (inst ,op x (fixnumize y) ,(if restore-fixnum-mask 'temp 'r))
148                 ,@(when restore-fixnum-mask
149                     `((inst bic temp #.(ash lowtag-mask -1) r)))))))
150      ,@(when (and untagged-type (not arg-swap))
151          `((define-vop (,(symbolicate "FAST-" translate "-C/SIGNED=>SIGNED")
152                         fast-signed-c-binop)
153              (:arg-types signed-num (:constant ,untagged-type))
154              (:translate ,translate)
155              (:generator ,untagged-cost
156                 (inst ,op x y r)))
157            (define-vop (,(symbolicate "FAST-" translate
158                                       "-C/UNSIGNED=>UNSIGNED")
159                         fast-unsigned-c-binop)
160              (:arg-types unsigned-num (:constant ,untagged-type))
161              (:translate ,translate)
162              (:generator ,untagged-cost
163                 (inst ,op x y r)))))))
164
165 (define-binop + 1 5 addq (unsigned-byte 6) (unsigned-byte 8))
166 (define-binop - 1 5 subq (unsigned-byte 6) (unsigned-byte 8))
167 (define-binop logand 1 3 and (unsigned-byte 6) (unsigned-byte 8))
168 (define-binop logandc1 1 3 bic (unsigned-byte 6) (unsigned-byte 8) t)
169 (define-binop logandc2 1 3 bic (unsigned-byte 6) (unsigned-byte 8))
170 (define-binop logior 1 3 bis (unsigned-byte 6) (unsigned-byte 8))
171 (define-binop logorc1 1 3 ornot (unsigned-byte 6) (unsigned-byte 8) t t)
172 (define-binop logorc2 1 3 ornot (unsigned-byte 6) (unsigned-byte 8) nil t)
173 (define-binop logxor 1 3 xor (unsigned-byte 6) (unsigned-byte 8))
174 (define-binop logeqv 1 3 eqv (unsigned-byte 6) (unsigned-byte 8) nil t)
175
176 ;;; special cases for LOGAND where we can use a mask operation
177 (define-vop (fast-logand-c-mask/unsigned=>unsigned fast-unsigned-c-binop)
178   (:translate logand)
179   (:arg-types unsigned-num
180               (:constant (or (integer #xffffffff #xffffffff)
181                              (integer #xffffffff00000000 #xffffffff00000000))))
182   (:generator 1
183     (ecase y
184       (#xffffffff (inst mskll x 4 r))
185       (#xffffffff00000000 (inst mskll x 0 r)))))
186 \f
187 ;;;; shifting
188
189 (define-vop (fast-ash/unsigned=>unsigned)
190   (:note "inline ASH")
191   (:args (number :scs (unsigned-reg) :to :save)
192          (amount :scs (signed-reg)))
193   (:arg-types unsigned-num signed-num)
194   (:results (result :scs (unsigned-reg)))
195   (:result-types unsigned-num)
196   (:translate ash)
197   (:policy :fast-safe)
198   (:temporary (:sc non-descriptor-reg) ndesc)
199   (:temporary (:sc non-descriptor-reg :to :eval) temp)
200   (:generator 3
201     (inst bge amount positive)
202     (inst subq zero-tn amount ndesc)
203     (inst cmplt ndesc 64 temp)
204     (inst srl number ndesc result)
205     ;; FIXME: this looks like a candidate for a conditional move --
206     ;; CSR, 2003-09-10
207     (inst bne temp done)
208     (move zero-tn result)
209     (inst br zero-tn done)
210       
211     POSITIVE
212     (inst sll number amount result)
213       
214     DONE))
215
216 (define-vop (fast-ash/signed=>signed)
217   (:note "inline ASH")
218   (:args (number :scs (signed-reg) :to :save)
219          (amount :scs (signed-reg)))
220   (:arg-types signed-num signed-num)
221   (:results (result :scs (signed-reg)))
222   (:result-types signed-num)
223   (:translate ash)
224   (:policy :fast-safe)
225   (:temporary (:sc non-descriptor-reg) ndesc)
226   (:temporary (:sc non-descriptor-reg :to :eval) temp)
227   (:generator 3
228     (inst bge amount positive)
229     (inst subq zero-tn amount ndesc)
230     (inst cmplt ndesc 63 temp)
231     (inst sra number ndesc result)
232     (inst bne temp done)
233     (inst sra number 63 result)
234     (inst br zero-tn done)
235       
236     POSITIVE
237     (inst sll number amount result)
238       
239     DONE))
240
241 (define-vop (fast-ash-c/signed=>signed)
242   (:policy :fast-safe)
243   (:translate ash)
244   (:note nil)
245   (:args (number :scs (signed-reg)))
246   (:info count)
247   (:arg-types signed-num (:constant integer))
248   (:results (result :scs (signed-reg)))
249   (:result-types signed-num)
250   (:generator 1
251     (cond
252       ((< count 0) (inst sra number (- count) result))
253       ((> count 0) (inst sll number count result))
254       (t (bug "identity ASH not transformed away")))))
255
256 (define-vop (fast-ash-c/unsigned=>unsigned)
257   (:policy :fast-safe)
258   (:translate ash)
259   (:note nil)
260   (:args (number :scs (unsigned-reg)))
261   (:info count)
262   (:arg-types unsigned-num (:constant integer))
263   (:results (result :scs (unsigned-reg)))
264   (:result-types unsigned-num)
265   (:generator 1
266     (cond
267       ((< count -63) (move zero-tn result))
268       ((< count 0) (inst sra number (- count) result))
269       ((> count 0) (inst sll number count result))
270       (t (bug "identity ASH not transformed away")))))
271
272 (define-vop (signed-byte-64-len)
273   (:translate integer-length)
274   (:note "inline (signed-byte 64) integer-length")
275   (:policy :fast-safe)
276   (:args (arg :scs (signed-reg) :to (:argument 1)))
277   (:arg-types signed-num)
278   (:results (res :scs (any-reg)))
279   (:result-types positive-fixnum)
280   (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) shift)
281   (:generator 30
282     (inst not arg shift)
283     (inst cmovge arg arg shift)
284     (inst subq zero-tn (fixnumize 1) res)
285     (inst sll shift 1 shift)
286     LOOP
287     (inst addq res (fixnumize 1) res)
288     (inst srl shift 1 shift)
289     (inst bne shift loop)))
290
291 (define-vop (unsigned-byte-64-count)
292   (:translate logcount)
293   (:note "inline (unsigned-byte 64) logcount")
294   (:policy :fast-safe)
295   (:args (arg :scs (unsigned-reg) :target num))
296   (:arg-types unsigned-num)
297   (:results (res :scs (unsigned-reg)))
298   (:result-types positive-fixnum)
299   (:temporary (:scs (non-descriptor-reg) :from (:argument 0) :to (:result 0)
300                     :target res) num)
301   (:temporary (:scs (non-descriptor-reg)) mask temp)
302   (:generator 60
303     ;; FIXME: now this looks expensive, what with these 64bit loads.
304     ;; Maybe a loop and count would be faster?  -- CSR, 2003-09-10
305     (inst li #x5555555555555555 mask)
306     (inst srl arg 1 temp)
307     (inst and arg mask num)
308     (inst and temp mask temp)
309     (inst addq num temp num)
310     (inst li #x3333333333333333 mask)
311     (inst srl num 2 temp)
312     (inst and num mask num)
313     (inst and temp mask temp)
314     (inst addq num temp num)
315     (inst li #x0f0f0f0f0f0f0f0f mask)
316     (inst srl num 4 temp)
317     (inst and num mask num)
318     (inst and temp mask temp)
319     (inst addq num temp num)
320     (inst li #x00ff00ff00ff00ff mask)
321     (inst srl num 8 temp)
322     (inst and num mask num)
323     (inst and temp mask temp)
324     (inst addq num temp num)
325     (inst li #x0000ffff0000ffff mask)
326     (inst srl num 16 temp)
327     (inst and num mask num)
328     (inst and temp mask temp)
329     (inst addq num temp num)
330     (inst li #x00000000ffffffff mask)
331     (inst srl num 32 temp)
332     (inst and num mask num)
333     (inst and temp mask temp)
334     (inst addq num temp res)))
335 \f
336 ;;;; multiplying
337
338 (define-vop (fast-*/fixnum=>fixnum fast-fixnum-binop)
339   (:temporary (:scs (non-descriptor-reg)) temp)
340   (:translate *)
341   (:generator 4
342     (inst sra y 2 temp)
343     (inst mulq x temp r)))
344
345 (define-vop (fast-*/signed=>signed fast-signed-binop)
346   (:translate *)
347   (:generator 3
348     (inst mulq x y r)))
349
350 (define-vop (fast-*/unsigned=>unsigned fast-unsigned-binop)
351   (:translate *)
352   (:generator 3
353     (inst mulq x y r)))
354 \f
355 ;;;; Modular functions:
356 (define-modular-fun lognot-mod64 (x) lognot 64)
357 (define-vop (lognot-mod64/unsigned=>unsigned)
358   (:translate lognot-mod64)
359   (:args (x :scs (unsigned-reg)))
360   (:arg-types unsigned-num)
361   (:results (res :scs (unsigned-reg)))
362   (:result-types unsigned-num)
363   (:policy :fast-safe)
364   (:generator 1
365     (inst not x res)))
366
367 (macrolet
368     ((define-modular-backend (fun &optional constantp)
369        (let ((mfun-name (symbolicate fun '-mod64))
370              (modvop (symbolicate 'fast- fun '-mod64/unsigned=>unsigned))
371              (modcvop (symbolicate 'fast- fun 'mod64-c/unsigned=>unsigned))
372              (vop (symbolicate 'fast- fun '/unsigned=>unsigned))
373              (cvop (symbolicate 'fast- fun '-c/unsigned=>unsigned)))
374          `(progn
375             (define-modular-fun ,mfun-name (x y) ,fun 64)
376             (define-vop (,modvop ,vop)
377               (:translate ,mfun-name))
378             ,@(when constantp
379                 `((define-vop (,modcvop ,cvop)
380                     (:translate ,mfun-name))))))))
381   (define-modular-backend + t)
382   (define-modular-backend logxor t)
383   (define-modular-backend logeqv t)
384   (define-modular-backend logandc1)
385   (define-modular-backend logandc2 t)
386   (define-modular-backend logorc1)
387   (define-modular-backend logorc2 t))
388
389 (define-source-transform lognand (x y)
390   `(lognot (logand ,x ,y)))
391 (define-source-transform lognor (x y)
392   `(lognot (logior ,x ,y)))
393 \f
394 ;;;; binary conditional VOPs
395
396 (define-vop (fast-conditional)
397   (:conditional)
398   (:info target not-p)
399   (:effects)
400   (:affected)
401   (:temporary (:scs (non-descriptor-reg)) temp)
402   (:policy :fast-safe))
403
404 (define-vop (fast-conditional/fixnum fast-conditional)
405   (:args (x :scs (any-reg))
406          (y :scs (any-reg)))
407   (:arg-types tagged-num tagged-num)
408   (:note "inline fixnum comparison"))
409
410 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
411   (:args (x :scs (any-reg)))
412   (:arg-types tagged-num (:constant (unsigned-byte-with-a-bite-out 6 4)))
413   (:info target not-p y))
414
415 (define-vop (fast-conditional/signed fast-conditional)
416   (:args (x :scs (signed-reg))
417          (y :scs (signed-reg)))
418   (:arg-types signed-num signed-num)
419   (:note "inline (signed-byte 64) comparison"))
420
421 (define-vop (fast-conditional-c/signed fast-conditional/signed)
422   (:args (x :scs (signed-reg)))
423   (:arg-types signed-num (:constant (unsigned-byte-with-a-bite-out 8 1)))
424   (:info target not-p y))
425
426 (define-vop (fast-conditional/unsigned fast-conditional)
427   (:args (x :scs (unsigned-reg))
428          (y :scs (unsigned-reg)))
429   (:arg-types unsigned-num unsigned-num)
430   (:note "inline (unsigned-byte 64) comparison"))
431
432 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
433   (:args (x :scs (unsigned-reg)))
434   (:arg-types unsigned-num (:constant (unsigned-byte-with-a-bite-out 8 1)))
435   (:info target not-p y))
436
437
438 (defmacro define-conditional-vop (translate &rest generator)
439   `(progn
440      ,@(mapcar (lambda (suffix cost signed)
441                  (unless (and (member suffix '(/fixnum -c/fixnum))
442                               (eq translate 'eql))
443                    `(define-vop (,(intern (format nil "~:@(FAST-IF-~A~A~)"
444                                                   translate suffix))
445                                  ,(intern
446                                    (format nil "~:@(FAST-CONDITIONAL~A~)"
447                                            suffix)))
448                       (:translate ,translate)
449                       (:generator ,cost
450                                   (let* ((signed ,signed)
451                                          (-c/fixnum ,(eq suffix '-c/fixnum))
452                                          (y (if -c/fixnum (fixnumize y) y)))
453                                     ,@generator)))))
454                '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
455                '(3 2 5 4 5 4)
456                '(t t t t nil nil))))
457
458 (define-conditional-vop <
459   (cond ((and signed (eql y 0))
460          (if not-p
461              (inst bge x target)
462              (inst blt x target)))
463         (t
464          (if signed
465              (inst cmplt x y temp)
466              (inst cmpult x y temp))
467          (if not-p
468              (inst beq temp target)
469              (inst bne temp target)))))
470
471 (define-conditional-vop >
472   (cond ((and signed (eql y 0))
473          (if not-p
474              (inst ble x target)
475              (inst bgt x target)))
476         ((integerp y)
477          (let ((y (+ y (if -c/fixnum (fixnumize 1) 1))))
478            (if signed
479                (inst cmplt x y temp)
480                (inst cmpult x y temp))
481            (if not-p
482                (inst bne temp target)
483                (inst beq temp target))))
484         (t
485          (if signed
486              (inst cmplt y x temp)
487              (inst cmpult y x temp))
488          (if not-p
489              (inst beq temp target)
490              (inst bne temp target)))))
491
492 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not
493 ;;; just a known fixnum.
494
495 (define-conditional-vop eql
496   (declare (ignore signed))
497   (when (integerp y)
498     (inst li y temp)
499     (setf y temp))
500   (inst cmpeq x y temp)
501   (if not-p
502       (inst beq temp target)
503       (inst bne temp target)))
504
505 ;;; These versions specify a fixnum restriction on their first arg. We
506 ;;; have also generic-eql/fixnum VOPs which are the same, but have no
507 ;;; restriction on the first arg and a higher cost. The reason for
508 ;;; doing this is to prevent fixnum specific operations from being
509 ;;; used on word integers, spuriously consing the argument.
510 (define-vop (fast-eql/fixnum fast-conditional)
511   (:args (x :scs (any-reg))
512          (y :scs (any-reg)))
513   (:arg-types tagged-num tagged-num)
514   (:note "inline fixnum comparison")
515   (:translate eql)
516   (:generator 3
517     (cond ((equal y zero-tn)
518            (if not-p
519                (inst bne x target)
520                (inst beq x target)))
521           (t
522            (inst cmpeq x y temp)
523            (if not-p
524                (inst beq temp target)
525                (inst bne temp target))))))
526
527 ;;;
528 (define-vop (generic-eql/fixnum fast-eql/fixnum)
529   (:args (x :scs (any-reg descriptor-reg))
530          (y :scs (any-reg)))
531   (:arg-types * tagged-num)
532   (:variant-cost 7))
533
534 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
535   (:args (x :scs (any-reg)))
536   (:arg-types tagged-num (:constant (signed-byte 6)))
537   (:temporary (:scs (non-descriptor-reg)) temp)
538   (:info target not-p y)
539   (:translate eql)
540   (:generator 2
541     (let ((y (cond ((eql y 0) zero-tn)
542                    (t
543                     (inst li (fixnumize y) temp)
544                     temp))))
545       (inst cmpeq x y temp)
546       (if not-p
547           (inst beq temp target)
548           (inst bne temp target)))))
549 ;;;
550 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
551   (:args (x :scs (any-reg descriptor-reg)))
552   (:arg-types * (:constant (signed-byte 6)))
553   (:variant-cost 6))
554   
555 \f
556 ;;;; 32-bit logical operations
557
558 (define-vop (merge-bits)
559   (:translate merge-bits)
560   (:args (shift :scs (signed-reg unsigned-reg))
561          (prev :scs (unsigned-reg))
562          (next :scs (unsigned-reg)))
563   (:arg-types tagged-num unsigned-num unsigned-num)
564   (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
565   (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
566   (:results (result :scs (unsigned-reg)))
567   (:result-types unsigned-num)
568   (:policy :fast-safe)
569   (:generator 4
570     (let ((done (gen-label)))
571       (inst srl next shift res)
572       (inst beq shift done)
573       (inst subq zero-tn shift temp)
574       (inst sll prev temp temp)
575       (inst bis res temp res)
576       (emit-label done)
577       (move res result))))
578
579 (define-source-transform 32bit-logical-not (x)
580   `(logand (lognot (the (unsigned-byte 32) ,x)) #.(1- (ash 1 32))))
581
582 (deftransform 32bit-logical-and ((x y))
583   '(logand x y))
584
585 (define-source-transform 32bit-logical-nand (x y)
586   `(32bit-logical-not (32bit-logical-and ,x ,y)))
587
588 (deftransform 32bit-logical-or ((x y))
589   '(logior x y))
590
591 (define-source-transform 32bit-logical-nor (x y)
592   `(logand (lognor (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y))
593            #.(1- (ash 1 32))))
594
595 (deftransform 32bit-logical-xor ((x y))
596   '(logxor x y))
597
598 (define-source-transform 32bit-logical-eqv (x y)
599   `(logand (logeqv (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y))
600            #.(1- (ash 1 32))))
601
602 (define-source-transform 32bit-logical-orc1 (x y)
603   `(logand (logorc1 (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y))
604            #.(1- (ash 1 32))))
605
606 (define-source-transform 32bit-logical-orc2 (x y)
607   `(logand (logorc2 (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y))
608            #.(1- (ash 1 32))))
609
610 (define-source-transform 32bit-logical-andc1 (x y)
611   `(logandc1 (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y)))
612
613 (define-source-transform 32bit-logical-andc2 (x y)
614   `(logandc2 (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y)))
615
616 (define-vop (shift-towards-someplace)
617   (:policy :fast-safe)
618   (:args (num :scs (unsigned-reg))
619          (amount :scs (signed-reg)))
620   (:arg-types unsigned-num tagged-num)
621   (:results (r :scs (unsigned-reg)))
622   (:result-types unsigned-num))
623
624 (define-vop (shift-towards-start shift-towards-someplace)
625   (:translate shift-towards-start)
626   (:note "SHIFT-TOWARDS-START")
627   (:temporary (:sc non-descriptor-reg) temp)
628   (:generator 1
629     (inst and amount #x1f temp)
630     (inst srl num temp r)))
631
632 (define-vop (shift-towards-end shift-towards-someplace)
633   (:translate shift-towards-end)
634   (:note "SHIFT-TOWARDS-END")
635   (:temporary (:sc non-descriptor-reg) temp)
636   (:generator 1
637     (inst and amount #x1f temp)
638     (inst sll num temp r)))
639 \f
640 ;;;; bignum stuff
641
642 (define-vop (bignum-length get-header-data)
643   (:translate sb!bignum::%bignum-length)
644   (:policy :fast-safe))
645
646 (define-vop (bignum-set-length set-header-data)
647   (:translate sb!bignum::%bignum-set-length)
648   (:policy :fast-safe))
649
650 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
651   (unsigned-reg) unsigned-num sb!bignum::%bignum-ref)
652
653 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
654   (unsigned-reg) unsigned-num sb!bignum::%bignum-set #!+gengc nil)
655
656 (define-vop (digit-0-or-plus)
657   (:translate sb!bignum::%digit-0-or-plusp)
658   (:policy :fast-safe)
659   (:args (digit :scs (unsigned-reg)))
660   (:arg-types unsigned-num)
661   (:temporary (:sc non-descriptor-reg) temp)
662   (:conditional)
663   (:info target not-p)
664   (:generator 2
665     (inst sll digit 32 temp)
666     (if not-p
667         (inst blt temp target)
668         (inst bge temp target))))
669
670 (define-vop (add-w/carry)
671   (:translate sb!bignum::%add-with-carry)
672   (:policy :fast-safe)
673   (:args (a :scs (unsigned-reg))
674          (b :scs (unsigned-reg))
675          (c :scs (unsigned-reg)))
676   (:arg-types unsigned-num unsigned-num positive-fixnum)
677   (:results (result :scs (unsigned-reg) :from :load)
678             (carry :scs (unsigned-reg) :from :eval))
679   (:result-types unsigned-num positive-fixnum)
680   (:generator 5
681     (inst addq a b result)
682     (inst addq result c result)
683     (inst sra result 32 carry)
684     (inst mskll result 4 result)))
685
686 (define-vop (sub-w/borrow)
687   (:translate sb!bignum::%subtract-with-borrow)
688   (:policy :fast-safe)
689   (:args (a :scs (unsigned-reg))
690          (b :scs (unsigned-reg))
691          (c :scs (unsigned-reg)))
692   (:arg-types unsigned-num unsigned-num positive-fixnum)
693   (:results (result :scs (unsigned-reg) :from :load)
694             (borrow :scs (unsigned-reg) :from :eval))
695   (:result-types unsigned-num positive-fixnum)
696   (:generator 4
697     (inst xor c 1 result)
698     (inst subq a result result)
699     (inst subq result b result)
700     (inst srl result 63 borrow)
701     (inst xor borrow 1 borrow)
702     (inst mskll result 4 result)))
703
704 (define-vop (bignum-mult-and-add-3-arg)
705   (:translate sb!bignum::%multiply-and-add)
706   (:policy :fast-safe)
707   (:args (x :scs (unsigned-reg))
708          (y :scs (unsigned-reg))
709          (carry-in :scs (unsigned-reg) :to :save))
710   (:arg-types unsigned-num unsigned-num unsigned-num)
711   (:results (hi :scs (unsigned-reg))
712             (lo :scs (unsigned-reg)))
713   (:result-types unsigned-num unsigned-num)
714   (:generator 6
715     (inst mulq x y lo)
716     (inst addq lo carry-in lo)
717     (inst srl lo 32 hi)
718     (inst mskll lo 4 lo)))
719
720
721 (define-vop (bignum-mult-and-add-4-arg)
722   (:translate sb!bignum::%multiply-and-add)
723   (:policy :fast-safe)
724   (:args (x :scs (unsigned-reg))
725          (y :scs (unsigned-reg))
726          (prev :scs (unsigned-reg))
727          (carry-in :scs (unsigned-reg) :to :save))
728   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
729   (:results (hi :scs (unsigned-reg))
730             (lo :scs (unsigned-reg)))
731   (:result-types unsigned-num unsigned-num)
732   (:generator 9
733     (inst mulq x y lo)
734     (inst addq lo prev lo)
735     (inst addq lo carry-in lo)
736     (inst srl lo 32 hi)
737     (inst mskll lo 4 lo)))
738
739 (define-vop (bignum-mult)
740   (:translate sb!bignum::%multiply)
741   (:policy :fast-safe)
742   (:args (x :scs (unsigned-reg))
743          (y :scs (unsigned-reg)))
744   (:arg-types unsigned-num unsigned-num)
745   (:results (hi :scs (unsigned-reg))
746             (lo :scs (unsigned-reg)))
747   (:result-types unsigned-num unsigned-num)
748   (:generator 3
749     (inst mulq x y lo)
750     (inst srl lo 32 hi)
751     (inst mskll lo 4 lo)))
752
753 (define-vop (bignum-lognot)
754   (:translate sb!bignum::%lognot)
755   (:policy :fast-safe)
756   (:args (x :scs (unsigned-reg)))
757   (:arg-types unsigned-num)
758   (:results (r :scs (unsigned-reg)))
759   (:result-types unsigned-num)
760   (:generator 1
761     (inst not x r)
762     (inst mskll r 4 r)))
763
764 (define-vop (fixnum-to-digit)
765   (:translate sb!bignum::%fixnum-to-digit)
766   (:policy :fast-safe)
767   (:args (fixnum :scs (any-reg)))
768   (:arg-types tagged-num)
769   (:results (digit :scs (unsigned-reg)))
770   (:result-types unsigned-num)
771   (:generator 1
772     (inst sra fixnum 2 digit)))
773
774 (define-vop (bignum-floor)
775   (:translate sb!bignum::%floor)
776   (:policy :fast-safe)
777   (:args (num-high :scs (unsigned-reg))
778          (num-low :scs (unsigned-reg))
779          (denom-arg :scs (unsigned-reg) :target denom))
780   (:arg-types unsigned-num unsigned-num unsigned-num)
781   (:temporary (:scs (unsigned-reg) :from (:argument 2)) denom)
782   (:temporary (:scs (unsigned-reg) :from (:eval 0)) temp)
783   (:results (quo :scs (unsigned-reg) :from (:eval 0))
784             (rem :scs (unsigned-reg) :from (:argument 0)))
785   (:result-types unsigned-num unsigned-num)
786   (:generator 325 ; number of inst assuming targeting works.
787     (inst sll num-high 32 rem)
788     (inst bis rem num-low rem)
789     (inst sll denom-arg 32 denom)
790     (inst cmpule denom rem quo)
791     (inst beq quo shift1)
792     (inst subq rem denom rem)
793     SHIFT1
794     (dotimes (i 32)
795       (let ((shift2 (gen-label)))
796         (inst srl denom 1 denom)
797         (inst cmpule denom rem temp)
798         (inst sll quo 1 quo)
799         (inst beq temp shift2)
800         (inst subq rem denom rem)
801         (inst bis quo 1 quo)
802         (emit-label shift2)))))
803
804 (define-vop (signify-digit)
805   (:translate sb!bignum::%fixnum-digit-with-correct-sign)
806   (:policy :fast-safe)
807   (:args (digit :scs (unsigned-reg) :target res))
808   (:arg-types unsigned-num)
809   (:results (res :scs (any-reg signed-reg)))
810   (:result-types signed-num)
811   (:generator 2
812     (sc-case res
813       (any-reg
814        (inst sll digit 34 res)
815        (inst sra res 32 res))
816       (signed-reg
817        (inst sll digit 32 res)
818        (inst sra res 32 res)))))
819
820
821 (define-vop (digit-ashr)
822   (:translate sb!bignum::%ashr)
823   (:policy :fast-safe)
824   (:args (digit :scs (unsigned-reg))
825          (count :scs (unsigned-reg)))
826   (:arg-types unsigned-num positive-fixnum)
827   (:results (result :scs (unsigned-reg) :from (:argument 0)))
828   (:result-types unsigned-num)
829   (:generator 1
830     (inst sll digit 32 result)
831     (inst sra result count result)
832     (inst srl result 32 result)))
833
834 (define-vop (digit-lshr digit-ashr)
835   (:translate sb!bignum::%digit-logical-shift-right)
836   (:generator 1
837     (inst srl digit count result)))
838
839 (define-vop (digit-ashl digit-ashr)
840   (:translate sb!bignum::%ashl)
841   (:generator 1
842     (inst sll digit count result)))
843 \f
844 ;;;; static functions
845
846 (define-static-fun two-arg-gcd (x y) :translate gcd)
847 (define-static-fun two-arg-lcm (x y) :translate lcm)
848
849 (define-static-fun two-arg-+ (x y) :translate +)
850 (define-static-fun two-arg-- (x y) :translate -)
851 (define-static-fun two-arg-* (x y) :translate *)
852 (define-static-fun two-arg-/ (x y) :translate /)
853
854 (define-static-fun two-arg-< (x y) :translate <)
855 (define-static-fun two-arg-<= (x y) :translate <=)
856 (define-static-fun two-arg-> (x y) :translate >)
857 (define-static-fun two-arg->= (x y) :translate >=)
858 (define-static-fun two-arg-= (x y) :translate =)
859 (define-static-fun two-arg-/= (x y) :translate /=)
860
861 (define-static-fun %negate (x) :translate %negate)
862
863 (define-static-fun two-arg-and (x y) :translate logand)
864 (define-static-fun two-arg-ior (x y) :translate logior)
865 (define-static-fun two-arg-xor (x y) :translate logxor)
866 (define-static-fun two-arg-eqv (x y) :translate logeqv)