9eece3c675433b79c277c174fe37a050ab4e9d46
[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) 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) 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 (min 63 (- count)) result))
253       ((> count 0) (inst sll number (min 63 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 (min 63 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 (defknown ash-left-constant-mod64 (integer (integer 0)) (unsigned-byte 64)
368   (foldable flushable movable))
369 (define-vop (fast-ash-left-constant-mod64/unsigned=>unsigned
370              fast-ash-c/unsigned=>unsigned)
371   (:translate ash-left-constant-mod64))
372
373 (macrolet
374     ((define-modular-backend (fun &optional constantp)
375        (let ((mfun-name (symbolicate fun '-mod64))
376              (modvop (symbolicate 'fast- fun '-mod64/unsigned=>unsigned))
377              (modcvop (symbolicate 'fast- fun '-mod64-c/unsigned=>unsigned))
378              (vop (symbolicate 'fast- fun '/unsigned=>unsigned))
379              (cvop (symbolicate 'fast- fun '-c/unsigned=>unsigned)))
380          `(progn
381             (define-modular-fun ,mfun-name (x y) ,fun 64)
382             (define-vop (,modvop ,vop)
383               (:translate ,mfun-name))
384             ,@(when constantp
385                 `((define-vop (,modcvop ,cvop)
386                     (:translate ,mfun-name))))))))
387   (define-modular-backend + t)
388   (define-modular-backend - t)
389   (define-modular-backend logxor t)
390   (define-modular-backend logeqv t)
391   (define-modular-backend logandc1)
392   (define-modular-backend logandc2 t)
393   (define-modular-backend logorc1)
394   (define-modular-backend logorc2 t))
395
396 (define-source-transform lognand (x y)
397   `(lognot (logand ,x ,y)))
398 (define-source-transform lognor (x y)
399   `(lognot (logior ,x ,y)))
400 \f
401 ;;;; binary conditional VOPs
402
403 (define-vop (fast-conditional)
404   (:conditional)
405   (:info target not-p)
406   (:effects)
407   (:affected)
408   (:temporary (:scs (non-descriptor-reg)) temp)
409   (:policy :fast-safe))
410
411 (define-vop (fast-conditional/fixnum fast-conditional)
412   (:args (x :scs (any-reg))
413          (y :scs (any-reg)))
414   (:arg-types tagged-num tagged-num)
415   (:note "inline fixnum comparison"))
416
417 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
418   (:args (x :scs (any-reg)))
419   (:arg-types tagged-num (:constant (unsigned-byte-with-a-bite-out 6 4)))
420   (:info target not-p y))
421
422 (define-vop (fast-conditional/signed fast-conditional)
423   (:args (x :scs (signed-reg))
424          (y :scs (signed-reg)))
425   (:arg-types signed-num signed-num)
426   (:note "inline (signed-byte 64) comparison"))
427
428 (define-vop (fast-conditional-c/signed fast-conditional/signed)
429   (:args (x :scs (signed-reg)))
430   (:arg-types signed-num (:constant (unsigned-byte-with-a-bite-out 8 1)))
431   (:info target not-p y))
432
433 (define-vop (fast-conditional/unsigned fast-conditional)
434   (:args (x :scs (unsigned-reg))
435          (y :scs (unsigned-reg)))
436   (:arg-types unsigned-num unsigned-num)
437   (:note "inline (unsigned-byte 64) comparison"))
438
439 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
440   (:args (x :scs (unsigned-reg)))
441   (:arg-types unsigned-num (:constant (unsigned-byte-with-a-bite-out 8 1)))
442   (:info target not-p y))
443
444
445 (defmacro define-conditional-vop (translate &rest generator)
446   `(progn
447      ,@(mapcar (lambda (suffix cost signed)
448                  (unless (and (member suffix '(/fixnum -c/fixnum))
449                               (eq translate 'eql))
450                    `(define-vop (,(intern (format nil "~:@(FAST-IF-~A~A~)"
451                                                   translate suffix))
452                                  ,(intern
453                                    (format nil "~:@(FAST-CONDITIONAL~A~)"
454                                            suffix)))
455                       (:translate ,translate)
456                       (:generator ,cost
457                                   (let* ((signed ,signed)
458                                          (-c/fixnum ,(eq suffix '-c/fixnum))
459                                          (y (if -c/fixnum (fixnumize y) y)))
460                                     ,@generator)))))
461                '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
462                '(3 2 5 4 5 4)
463                '(t t t t nil nil))))
464
465 (define-conditional-vop <
466   (cond ((and signed (eql y 0))
467          (if not-p
468              (inst bge x target)
469              (inst blt x target)))
470         (t
471          (if signed
472              (inst cmplt x y temp)
473              (inst cmpult x y temp))
474          (if not-p
475              (inst beq temp target)
476              (inst bne temp target)))))
477
478 (define-conditional-vop >
479   (cond ((and signed (eql y 0))
480          (if not-p
481              (inst ble x target)
482              (inst bgt x target)))
483         ((integerp y)
484          (let ((y (+ y (if -c/fixnum (fixnumize 1) 1))))
485            (if signed
486                (inst cmplt x y temp)
487                (inst cmpult x y temp))
488            (if not-p
489                (inst bne temp target)
490                (inst beq temp target))))
491         (t
492          (if signed
493              (inst cmplt y x temp)
494              (inst cmpult y x temp))
495          (if not-p
496              (inst beq temp target)
497              (inst bne temp target)))))
498
499 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not
500 ;;; just a known fixnum.
501
502 (define-conditional-vop eql
503   (declare (ignore signed))
504   (when (integerp y)
505     (inst li y temp)
506     (setf y temp))
507   (inst cmpeq x y temp)
508   (if not-p
509       (inst beq temp target)
510       (inst bne temp target)))
511
512 ;;; These versions specify a fixnum restriction on their first arg. We
513 ;;; have also generic-eql/fixnum VOPs which are the same, but have no
514 ;;; restriction on the first arg and a higher cost. The reason for
515 ;;; doing this is to prevent fixnum specific operations from being
516 ;;; used on word integers, spuriously consing the argument.
517 (define-vop (fast-eql/fixnum fast-conditional)
518   (:args (x :scs (any-reg))
519          (y :scs (any-reg)))
520   (:arg-types tagged-num tagged-num)
521   (:note "inline fixnum comparison")
522   (:translate eql)
523   (:generator 3
524     (cond ((equal y zero-tn)
525            (if not-p
526                (inst bne x target)
527                (inst beq x target)))
528           (t
529            (inst cmpeq x y temp)
530            (if not-p
531                (inst beq temp target)
532                (inst bne temp target))))))
533
534 ;;;
535 (define-vop (generic-eql/fixnum fast-eql/fixnum)
536   (:args (x :scs (any-reg descriptor-reg))
537          (y :scs (any-reg)))
538   (:arg-types * tagged-num)
539   (:variant-cost 7))
540
541 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
542   (:args (x :scs (any-reg)))
543   (:arg-types tagged-num (:constant (signed-byte 6)))
544   (:temporary (:scs (non-descriptor-reg)) temp)
545   (:info target not-p y)
546   (:translate eql)
547   (:generator 2
548     (let ((y (cond ((eql y 0) zero-tn)
549                    (t
550                     (inst li (fixnumize y) temp)
551                     temp))))
552       (inst cmpeq x y temp)
553       (if not-p
554           (inst beq temp target)
555           (inst bne temp target)))))
556 ;;;
557 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
558   (:args (x :scs (any-reg descriptor-reg)))
559   (:arg-types * (:constant (signed-byte 6)))
560   (:variant-cost 6))
561   
562 \f
563 ;;;; 32-bit logical operations
564
565 (define-vop (merge-bits)
566   (:translate merge-bits)
567   (:args (shift :scs (signed-reg unsigned-reg))
568          (prev :scs (unsigned-reg))
569          (next :scs (unsigned-reg)))
570   (:arg-types tagged-num unsigned-num unsigned-num)
571   (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
572   (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
573   (:results (result :scs (unsigned-reg)))
574   (:result-types unsigned-num)
575   (:policy :fast-safe)
576   (:generator 4
577     (let ((done (gen-label)))
578       (inst srl next shift res)
579       (inst beq shift done)
580       (inst subq zero-tn shift temp)
581       (inst sll prev temp temp)
582       (inst bis res temp res)
583       (emit-label done)
584       (move res result))))
585
586 (define-source-transform 32bit-logical-not (x)
587   `(logand (lognot (the (unsigned-byte 32) ,x)) #.(1- (ash 1 32))))
588
589 (deftransform 32bit-logical-and ((x y))
590   '(logand x y))
591
592 (define-source-transform 32bit-logical-nand (x y)
593   `(32bit-logical-not (32bit-logical-and ,x ,y)))
594
595 (deftransform 32bit-logical-or ((x y))
596   '(logior x y))
597
598 (define-source-transform 32bit-logical-nor (x y)
599   `(logand (lognor (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y))
600            #.(1- (ash 1 32))))
601
602 (deftransform 32bit-logical-xor ((x y))
603   '(logxor x y))
604
605 (define-source-transform 32bit-logical-eqv (x y)
606   `(logand (logeqv (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y))
607            #.(1- (ash 1 32))))
608
609 (define-source-transform 32bit-logical-orc1 (x y)
610   `(logand (logorc1 (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y))
611            #.(1- (ash 1 32))))
612
613 (define-source-transform 32bit-logical-orc2 (x y)
614   `(logand (logorc2 (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y))
615            #.(1- (ash 1 32))))
616
617 (define-source-transform 32bit-logical-andc1 (x y)
618   `(logandc1 (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y)))
619
620 (define-source-transform 32bit-logical-andc2 (x y)
621   `(logandc2 (the (unsigned-byte 32) ,x) (the (unsigned-byte 32) ,y)))
622
623 (define-vop (shift-towards-someplace)
624   (:policy :fast-safe)
625   (:args (num :scs (unsigned-reg))
626          (amount :scs (signed-reg)))
627   (:arg-types unsigned-num tagged-num)
628   (:results (r :scs (unsigned-reg)))
629   (:result-types unsigned-num))
630
631 (define-vop (shift-towards-start shift-towards-someplace)
632   (:translate shift-towards-start)
633   (:note "SHIFT-TOWARDS-START")
634   (:temporary (:sc non-descriptor-reg) temp)
635   (:generator 1
636     (inst and amount #x1f temp)
637     (inst srl num temp r)))
638
639 (define-vop (shift-towards-end shift-towards-someplace)
640   (:translate shift-towards-end)
641   (:note "SHIFT-TOWARDS-END")
642   (:temporary (:sc non-descriptor-reg) temp)
643   (:generator 1
644     (inst and amount #x1f temp)
645     (inst sll num temp r)))
646 \f
647 ;;;; bignum stuff
648
649 (define-vop (bignum-length get-header-data)
650   (:translate sb!bignum::%bignum-length)
651   (:policy :fast-safe))
652
653 (define-vop (bignum-set-length set-header-data)
654   (:translate sb!bignum::%bignum-set-length)
655   (:policy :fast-safe))
656
657 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
658   (unsigned-reg) unsigned-num sb!bignum::%bignum-ref)
659
660 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
661   (unsigned-reg) unsigned-num sb!bignum::%bignum-set #!+gengc nil)
662
663 (define-vop (digit-0-or-plus)
664   (:translate sb!bignum::%digit-0-or-plusp)
665   (:policy :fast-safe)
666   (:args (digit :scs (unsigned-reg)))
667   (:arg-types unsigned-num)
668   (:temporary (:sc non-descriptor-reg) temp)
669   (:conditional)
670   (:info target not-p)
671   (:generator 2
672     (inst sll digit 32 temp)
673     (if not-p
674         (inst blt temp target)
675         (inst bge temp target))))
676
677 (define-vop (add-w/carry)
678   (:translate sb!bignum::%add-with-carry)
679   (:policy :fast-safe)
680   (:args (a :scs (unsigned-reg))
681          (b :scs (unsigned-reg))
682          (c :scs (unsigned-reg)))
683   (:arg-types unsigned-num unsigned-num positive-fixnum)
684   (:results (result :scs (unsigned-reg) :from :load)
685             (carry :scs (unsigned-reg) :from :eval))
686   (:result-types unsigned-num positive-fixnum)
687   (:generator 5
688     (inst addq a b result)
689     (inst addq result c result)
690     (inst sra result 32 carry)
691     (inst mskll result 4 result)))
692
693 (define-vop (sub-w/borrow)
694   (:translate sb!bignum::%subtract-with-borrow)
695   (:policy :fast-safe)
696   (:args (a :scs (unsigned-reg))
697          (b :scs (unsigned-reg))
698          (c :scs (unsigned-reg)))
699   (:arg-types unsigned-num unsigned-num positive-fixnum)
700   (:results (result :scs (unsigned-reg) :from :load)
701             (borrow :scs (unsigned-reg) :from :eval))
702   (:result-types unsigned-num positive-fixnum)
703   (:generator 4
704     (inst xor c 1 result)
705     (inst subq a result result)
706     (inst subq result b result)
707     (inst srl result 63 borrow)
708     (inst xor borrow 1 borrow)
709     (inst mskll result 4 result)))
710
711 (define-vop (bignum-mult-and-add-3-arg)
712   (:translate sb!bignum::%multiply-and-add)
713   (:policy :fast-safe)
714   (:args (x :scs (unsigned-reg))
715          (y :scs (unsigned-reg))
716          (carry-in :scs (unsigned-reg) :to :save))
717   (:arg-types unsigned-num unsigned-num unsigned-num)
718   (:results (hi :scs (unsigned-reg))
719             (lo :scs (unsigned-reg)))
720   (:result-types unsigned-num unsigned-num)
721   (:generator 6
722     (inst mulq x y lo)
723     (inst addq lo carry-in lo)
724     (inst srl lo 32 hi)
725     (inst mskll lo 4 lo)))
726
727
728 (define-vop (bignum-mult-and-add-4-arg)
729   (:translate sb!bignum::%multiply-and-add)
730   (:policy :fast-safe)
731   (:args (x :scs (unsigned-reg))
732          (y :scs (unsigned-reg))
733          (prev :scs (unsigned-reg))
734          (carry-in :scs (unsigned-reg) :to :save))
735   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
736   (:results (hi :scs (unsigned-reg))
737             (lo :scs (unsigned-reg)))
738   (:result-types unsigned-num unsigned-num)
739   (:generator 9
740     (inst mulq x y lo)
741     (inst addq lo prev lo)
742     (inst addq lo carry-in lo)
743     (inst srl lo 32 hi)
744     (inst mskll lo 4 lo)))
745
746 (define-vop (bignum-mult)
747   (:translate sb!bignum::%multiply)
748   (:policy :fast-safe)
749   (:args (x :scs (unsigned-reg))
750          (y :scs (unsigned-reg)))
751   (:arg-types unsigned-num unsigned-num)
752   (:results (hi :scs (unsigned-reg))
753             (lo :scs (unsigned-reg)))
754   (:result-types unsigned-num unsigned-num)
755   (:generator 3
756     (inst mulq x y lo)
757     (inst srl lo 32 hi)
758     (inst mskll lo 4 lo)))
759
760 (define-vop (bignum-lognot)
761   (:translate sb!bignum::%lognot)
762   (:policy :fast-safe)
763   (:args (x :scs (unsigned-reg)))
764   (:arg-types unsigned-num)
765   (:results (r :scs (unsigned-reg)))
766   (:result-types unsigned-num)
767   (:generator 1
768     (inst not x r)
769     (inst mskll r 4 r)))
770
771 (define-vop (fixnum-to-digit)
772   (:translate sb!bignum::%fixnum-to-digit)
773   (:policy :fast-safe)
774   (:args (fixnum :scs (any-reg)))
775   (:arg-types tagged-num)
776   (:results (digit :scs (unsigned-reg)))
777   (:result-types unsigned-num)
778   (:generator 1
779     (inst sra fixnum 2 digit)))
780
781 (define-vop (bignum-floor)
782   (:translate sb!bignum::%floor)
783   (:policy :fast-safe)
784   (:args (num-high :scs (unsigned-reg))
785          (num-low :scs (unsigned-reg))
786          (denom-arg :scs (unsigned-reg) :target denom))
787   (:arg-types unsigned-num unsigned-num unsigned-num)
788   (:temporary (:scs (unsigned-reg) :from (:argument 2)) denom)
789   (:temporary (:scs (unsigned-reg) :from (:eval 0)) temp)
790   (:results (quo :scs (unsigned-reg) :from (:eval 0))
791             (rem :scs (unsigned-reg) :from (:argument 0)))
792   (:result-types unsigned-num unsigned-num)
793   (:generator 325 ; number of inst assuming targeting works.
794     (inst sll num-high 32 rem)
795     (inst bis rem num-low rem)
796     (inst sll denom-arg 32 denom)
797     (inst cmpule denom rem quo)
798     (inst beq quo shift1)
799     (inst subq rem denom rem)
800     SHIFT1
801     (dotimes (i 32)
802       (let ((shift2 (gen-label)))
803         (inst srl denom 1 denom)
804         (inst cmpule denom rem temp)
805         (inst sll quo 1 quo)
806         (inst beq temp shift2)
807         (inst subq rem denom rem)
808         (inst bis quo 1 quo)
809         (emit-label shift2)))))
810
811 (define-vop (signify-digit)
812   (:translate sb!bignum::%fixnum-digit-with-correct-sign)
813   (:policy :fast-safe)
814   (:args (digit :scs (unsigned-reg) :target res))
815   (:arg-types unsigned-num)
816   (:results (res :scs (any-reg signed-reg)))
817   (:result-types signed-num)
818   (:generator 2
819     (sc-case res
820       (any-reg
821        (inst sll digit 34 res)
822        (inst sra res 32 res))
823       (signed-reg
824        (inst sll digit 32 res)
825        (inst sra res 32 res)))))
826
827
828 (define-vop (digit-ashr)
829   (:translate sb!bignum::%ashr)
830   (:policy :fast-safe)
831   (:args (digit :scs (unsigned-reg))
832          (count :scs (unsigned-reg)))
833   (:arg-types unsigned-num positive-fixnum)
834   (:results (result :scs (unsigned-reg) :from (:argument 0)))
835   (:result-types unsigned-num)
836   (:generator 1
837     (inst sll digit 32 result)
838     (inst sra result count result)
839     (inst srl result 32 result)))
840
841 (define-vop (digit-lshr digit-ashr)
842   (:translate sb!bignum::%digit-logical-shift-right)
843   (:generator 1
844     (inst srl digit count result)))
845
846 (define-vop (digit-ashl digit-ashr)
847   (:translate sb!bignum::%ashl)
848   (:generator 1
849     (inst sll digit count result)))
850 \f
851 ;;;; static functions
852
853 (define-static-fun two-arg-gcd (x y) :translate gcd)
854 (define-static-fun two-arg-lcm (x y) :translate lcm)
855
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 two-arg-< (x y) :translate <)
862 (define-static-fun two-arg-<= (x y) :translate <=)
863 (define-static-fun two-arg-> (x y) :translate >)
864 (define-static-fun two-arg->= (x y) :translate >=)
865 (define-static-fun two-arg-= (x y) :translate =)
866 (define-static-fun two-arg-/= (x y) :translate /=)
867
868 (define-static-fun %negate (x) :translate %negate)
869
870 (define-static-fun two-arg-and (x y) :translate logand)
871 (define-static-fun two-arg-ior (x y) :translate logior)
872 (define-static-fun two-arg-xor (x y) :translate logxor)
873 (define-static-fun two-arg-eqv (x y) :translate logeqv)