0.8.17.28:
[sbcl.git] / src / compiler / mips / arith.lisp
1 ;;;; the VM definition arithmetic VOPs for MIPS
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 32) 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 subu res zero-tn x)))
36
37 (define-vop (fast-negate/signed signed-unop)
38   (:translate %negate)
39   (:generator 2
40     (inst subu res zero-tn x)))
41
42 (define-vop (fast-lognot/fixnum fixnum-unop)
43   (:temporary (:scs (any-reg) :type fixnum :to (:result 0))
44               temp)
45   (:translate lognot)
46   (:generator 2
47     (inst li temp (fixnumize -1))
48     (inst xor res x temp)))
49
50 (define-vop (fast-lognot/signed signed-unop)
51   (:translate lognot)
52   (:generator 1
53     (inst nor res x zero-tn)))
54
55
56 \f
57 ;;;; Binary fixnum operations.
58
59 ;;; Assume that any constant operand is the second arg...
60
61 (define-vop (fast-fixnum-binop)
62   (:args (x :target r :scs (any-reg))
63          (y :target r :scs (any-reg)))
64   (:arg-types tagged-num tagged-num)
65   (:results (r :scs (any-reg)))
66   (:result-types tagged-num)
67   (:note "inline fixnum arithmetic")
68   (:effects)
69   (:affected)
70   (:policy :fast-safe))
71
72 (define-vop (fast-unsigned-binop)
73   (:args (x :target r :scs (unsigned-reg))
74          (y :target r :scs (unsigned-reg)))
75   (:arg-types unsigned-num unsigned-num)
76   (:results (r :scs (unsigned-reg)))
77   (:result-types unsigned-num)
78   (:note "inline (unsigned-byte 32) arithmetic")
79   (:effects)
80   (:affected)
81   (:policy :fast-safe))
82
83 (define-vop (fast-signed-binop)
84   (:args (x :target r :scs (signed-reg))
85          (y :target r :scs (signed-reg)))
86   (:arg-types signed-num signed-num)
87   (:results (r :scs (signed-reg)))
88   (:result-types signed-num)
89   (:note "inline (signed-byte 32) arithmetic")
90   (:effects)
91   (:affected)
92   (:policy :fast-safe))
93
94 (define-vop (fast-fixnum-c-binop fast-fixnum-binop)
95   (:args (x :target r :scs (any-reg)))
96   (:info y)
97   (:arg-types tagged-num (:constant integer)))
98
99 (define-vop (fast-signed-c-binop fast-signed-binop)
100   (:args (x :target r :scs (signed-reg)))
101   (:info y)
102   (:arg-types tagged-num (:constant integer)))
103
104 (define-vop (fast-unsigned-c-binop fast-unsigned-binop)
105   (:args (x :target r :scs (unsigned-reg)))
106   (:info y)
107   (:arg-types tagged-num (:constant integer)))
108
109 (defmacro define-binop (translate cost untagged-cost op
110                                   tagged-type untagged-type)
111   `(progn
112      (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
113                   fast-fixnum-binop)
114        (:args (x :target r :scs (any-reg))
115               (y :target r :scs (any-reg)))
116        (:translate ,translate)
117        (:generator ,(1+ cost)
118          (inst ,op r x y)))
119      (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
120                   fast-signed-binop)
121        (:args (x :target r :scs (signed-reg))
122               (y :target r :scs (signed-reg)))
123        (:translate ,translate)
124        (:generator ,(1+ untagged-cost)
125          (inst ,op r x y)))
126      (define-vop (,(symbolicate "FAST-" translate "/UNSIGNED=>UNSIGNED")
127                   fast-unsigned-binop)
128        (:args (x :target r :scs (unsigned-reg))
129               (y :target r :scs (unsigned-reg)))
130        (:translate ,translate)
131        (:generator ,(1+ untagged-cost)
132          (inst ,op r x y)))
133      ,@(when tagged-type
134          `((define-vop (,(symbolicate "FAST-" translate "-C/FIXNUM=>FIXNUM")
135                         fast-fixnum-c-binop)
136                        (:arg-types tagged-num (:constant ,tagged-type))
137              (:translate ,translate)
138              (:generator ,cost
139                          (inst ,op r x (fixnumize y))))))
140      ,@(when untagged-type
141          `((define-vop (,(symbolicate "FAST-" translate "-C/SIGNED=>SIGNED")
142                         fast-signed-c-binop)
143                        (:arg-types signed-num (:constant ,untagged-type))
144              (:translate ,translate)
145              (:generator ,untagged-cost
146                          (inst ,op r x y)))
147            (define-vop (,(symbolicate "FAST-" translate
148                                       "-C/UNSIGNED=>UNSIGNED")
149                         fast-unsigned-c-binop)
150                        (:arg-types unsigned-num (:constant ,untagged-type))
151              (:translate ,translate)
152              (:generator ,untagged-cost
153                          (inst ,op r x y)))))))
154
155 (define-binop + 1 5 addu (signed-byte 14) (signed-byte 16))
156 (define-binop - 1 5 subu
157   (integer #.(- (1- (ash 1 14))) #.(ash 1 14))
158   (integer #.(- (1- (ash 1 16))) #.(ash 1 16)))
159 (define-binop logior 1 3 or (unsigned-byte 14) (unsigned-byte 16))
160 (define-binop logand 1 3 and (unsigned-byte 14) (unsigned-byte 16))
161 (define-binop logxor 1 3 xor (unsigned-byte 14) (unsigned-byte 16))
162
163 ;;; KLUDGE: no FIXNUM VOP for LOGNOR, because there's no efficient way
164 ;;; of restoring the tag bits.  (No -C/ VOPs for LOGNOR because the
165 ;;; NOR instruction doesn't take immediate args).  -- CSR, 2003-09-11
166 (define-vop (fast-lognor/signed=>signed fast-signed-binop)
167   (:translate lognor)
168   (:args (x :target r :scs (signed-reg))
169          (y :target r :scs (signed-reg)))
170   (:generator 4
171     (inst nor r x y)))
172 (define-vop (fast-lognor/unsigned=>unsigned fast-unsigned-binop)
173   (:translate lognor)
174   (:args (x :target r :scs (unsigned-reg))
175          (y :target r :scs (unsigned-reg)))
176   (:generator 4
177     (inst nor r x y)))
178
179 ;;; Special case fixnum + and - that trap on overflow.  Useful when we don't
180 ;;; know that the result is going to be a fixnum.
181 #+nil
182 (progn
183   (define-vop (fast-+/fixnum fast-+/fixnum=>fixnum)
184       (:results (r :scs (any-reg descriptor-reg)))
185     (:result-types (:or signed-num unsigned-num))
186     (:note nil)
187     (:generator 4
188                 (inst add r x y)))
189
190   (define-vop (fast-+-c/fixnum fast-+-c/fixnum=>fixnum)
191       (:results (r :scs (any-reg descriptor-reg)))
192     (:result-types (:or signed-num unsigned-num))
193     (:note nil)
194     (:generator 3
195                 (inst add r x (fixnumize y))))
196
197   (define-vop (fast--/fixnum fast--/fixnum=>fixnum)
198       (:results (r :scs (any-reg descriptor-reg)))
199     (:result-types (:or signed-num unsigned-num))
200     (:note nil)
201     (:generator 4
202                 (inst sub r x y)))
203
204   (define-vop (fast---c/fixnum fast---c/fixnum=>fixnum)
205       (:results (r :scs (any-reg descriptor-reg)))
206     (:result-types (:or signed-num unsigned-num))
207     (:note nil)
208     (:generator 3
209                 (inst sub r x (fixnumize y))))
210 ) ; bogus trap-to-c-land +/-
211
212 ;;; Shifting
213
214 (define-vop (fast-ash/unsigned=>unsigned)
215   (:note "inline ASH")
216   (:args (number :scs (unsigned-reg) :to :save)
217          (amount :scs (signed-reg) :to :save))
218   (:arg-types unsigned-num signed-num)
219   (:results (result :scs (unsigned-reg)))
220   (:result-types unsigned-num)
221   (:translate ash)
222   (:policy :fast-safe)
223   (:temporary (:sc non-descriptor-reg) ndesc)
224   (:temporary (:sc non-descriptor-reg :to :eval) temp)
225   (:generator 3
226     (inst bgez amount positive)
227     (inst subu ndesc zero-tn amount)
228     (inst slt temp ndesc 32)
229     (inst bne temp zero-tn done)
230     (inst srl result number ndesc)
231     (inst b done)
232     (inst move result zero-tn)
233
234     POSITIVE
235     ;; The result-type assures us that this shift will not overflow.
236     (inst sll result number amount)
237
238     DONE))
239
240 (define-vop (fast-ash/signed=>signed)
241   (:note "inline ASH")
242   (:args (number :scs (signed-reg) :to :save)
243          (amount :scs (signed-reg)))
244   (:arg-types signed-num signed-num)
245   (:results (result :scs (signed-reg)))
246   (:result-types signed-num)
247   (:translate ash)
248   (:policy :fast-safe)
249   (:temporary (:sc non-descriptor-reg) ndesc)
250   (:temporary (:sc non-descriptor-reg :to :eval) temp)
251   (:generator 3
252     (inst bgez amount positive)
253     (inst subu ndesc zero-tn amount)
254     (inst slt temp ndesc 31)
255     (inst bne temp zero-tn done)
256     (inst sra result number ndesc)
257     (inst b done)
258     (inst sra result number 31)
259
260     POSITIVE
261     ;; The result-type assures us that this shift will not overflow.
262     (inst sll result number amount)
263
264     DONE))
265
266
267 (define-vop (fast-ash-c/unsigned=>unsigned)
268   (:policy :fast-safe)
269   (:translate ash)
270   (:note "inline ASH")
271   (:args (number :scs (unsigned-reg)))
272   (:info count)
273   (:arg-types unsigned-num (:constant integer))
274   (:results (result :scs (unsigned-reg)))
275   (:result-types unsigned-num)
276   (:generator 1
277     (cond 
278       ((< count -31) (move result zero-tn))
279       ((< count 0) (inst srl result number (min (- count) 31)))
280       ((> count 0) (inst sll result number (min count 31)))
281       (t (bug "identity ASH not transformed away")))))
282
283 (define-vop (fast-ash-c/signed=>signed)
284   (:policy :fast-safe)
285   (:translate ash)
286   (:note "inline ASH")
287   (:args (number :scs (signed-reg)))
288   (:info count)
289   (:arg-types signed-num (:constant integer))
290   (:results (result :scs (signed-reg)))
291   (:result-types signed-num)
292   (:generator 1
293     (cond 
294       ((< count 0) (inst sra result number (min (- count) 31)))
295       ((> count 0) (inst sll result number (min count 31)))
296       (t (bug "identity ASH not transformed away")))))
297
298 (macrolet ((def (name sc-type type result-type cost)
299              `(define-vop (,name)
300                 (:note "inline ASH")
301                 (:translate ash)
302                 (:args (number :scs (,sc-type))
303                        (amount :scs (signed-reg unsigned-reg immediate)))
304                 (:arg-types ,type positive-fixnum)
305                 (:results (result :scs (,result-type)))
306                 (:result-types ,type)
307                 (:policy :fast-safe)
308                 (:generator ,cost
309                    (sc-case amount
310                      ((signed-reg unsigned-reg)
311                       (inst sll result number amount))
312                      (immediate
313                       (let ((amount (tn-value amount)))
314                         (aver (> amount 0))
315                         (inst sll result number amount))))))))
316   (def fast-ash-left/fixnum=>fixnum any-reg tagged-num any-reg 2)
317   (def fast-ash-left/signed=>signed signed-reg signed-num signed-reg 3)
318   (def fast-ash-left/unsigned=>unsigned unsigned-reg unsigned-num unsigned-reg 3))
319
320 (define-vop (signed-byte-32-len)
321   (:translate integer-length)
322   (:note "inline (signed-byte 32) integer-length")
323   (:policy :fast-safe)
324   (:args (arg :scs (signed-reg) :target shift))
325   (:arg-types signed-num)
326   (:results (res :scs (any-reg)))
327   (:result-types positive-fixnum)
328   (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) shift)
329   (:generator 30
330     (let ((loop (gen-label))
331           (test (gen-label)))
332       (move shift arg)
333       (inst bgez shift test)
334       (move res zero-tn)
335       (inst b test)
336       (inst nor shift shift)
337
338       (emit-label loop)
339       (inst add res (fixnumize 1))
340       
341       (emit-label test)
342       (inst bne shift loop)
343       (inst srl shift 1))))
344
345 (define-vop (unsigned-byte-32-count)
346   (:translate logcount)
347   (:note "inline (unsigned-byte 32) logcount")
348   (:policy :fast-safe)
349   (:args (arg :scs (unsigned-reg) :target num))
350   (:arg-types unsigned-num)
351   (:results (res :scs (unsigned-reg)))
352   (:result-types positive-fixnum)
353   (:temporary (:scs (non-descriptor-reg) :from (:argument 0) :to (:result 0)
354                     :target res) num)
355   (:temporary (:scs (non-descriptor-reg)) mask temp)
356   (:generator 30
357     (inst li mask #x55555555)
358     (inst srl temp arg 1)
359     (inst and num arg mask)
360     (inst and temp mask)
361     (inst addu num temp)
362     (inst li mask #x33333333)
363     (inst srl temp num 2)
364     (inst and num mask)
365     (inst and temp mask)
366     (inst addu num temp)
367     (inst li mask #x0f0f0f0f)
368     (inst srl temp num 4)
369     (inst and num mask)
370     (inst and temp mask)
371     (inst addu num temp)
372     (inst li mask #x00ff00ff)
373     (inst srl temp num 8)
374     (inst and num mask)
375     (inst and temp mask)
376     (inst addu num temp)
377     (inst li mask #x0000ffff)
378     (inst srl temp num 16)
379     (inst and num mask)
380     (inst and temp mask)
381     (inst addu res num temp)))
382
383
384 ;;; Multiply and Divide.
385
386 (define-vop (fast-*/fixnum=>fixnum fast-fixnum-binop)
387   (:temporary (:scs (non-descriptor-reg)) temp)
388   (:translate *)
389   (:generator 4
390     (inst sra temp y 2)
391     (inst mult x temp)
392     (inst mflo r)))
393
394 (define-vop (fast-*/signed=>signed fast-signed-binop)
395   (:translate *)
396   (:generator 3
397     (inst mult x y)
398     (inst mflo r)))
399
400 (define-vop (fast-*/unsigned=>unsigned fast-unsigned-binop)
401   (:translate *)
402   (:generator 3
403     (inst multu x y)
404     (inst mflo r)))
405
406
407
408 (define-vop (fast-truncate/fixnum fast-fixnum-binop)
409   (:translate truncate)
410   (:results (q :scs (any-reg))
411             (r :scs (any-reg)))
412   (:result-types tagged-num tagged-num)
413   (:temporary (:scs (non-descriptor-reg) :to :eval) temp)
414   (:vop-var vop)
415   (:save-p :compute-only)
416   (:generator 11
417     (let ((zero (generate-error-code vop division-by-zero-error x y)))
418       (inst beq y zero-tn zero))
419     (inst nop)
420     (inst div x y)
421     (inst mflo temp)
422     (inst sll q temp 2)
423     (inst mfhi r)))
424
425 (define-vop (fast-truncate/unsigned fast-unsigned-binop)
426   (:translate truncate)
427   (:results (q :scs (unsigned-reg))
428             (r :scs (unsigned-reg)))
429   (:result-types unsigned-num unsigned-num)
430   (:vop-var vop)
431   (:save-p :compute-only)
432   (:generator 12
433     (let ((zero (generate-error-code vop division-by-zero-error x y)))
434       (inst beq y zero-tn zero))
435     (inst nop)
436     (inst divu x y)
437     (inst mflo q)
438     (inst mfhi r)))
439
440 (define-vop (fast-truncate/signed fast-signed-binop)
441   (:translate truncate)
442   (:results (q :scs (signed-reg))
443             (r :scs (signed-reg)))
444   (:result-types signed-num signed-num)
445   (:vop-var vop)
446   (:save-p :compute-only)
447   (:generator 12
448     (let ((zero (generate-error-code vop division-by-zero-error x y)))
449       (inst beq y zero-tn zero))
450     (inst nop)
451     (inst div x y)
452     (inst mflo q)
453     (inst mfhi r)))
454
455
456 \f
457 ;;;; Binary conditional VOPs:
458
459 (define-vop (fast-conditional)
460   (:conditional)
461   (:info target not-p)
462   (:effects)
463   (:affected)
464   (:temporary (:scs (non-descriptor-reg)) temp)
465   (:policy :fast-safe))
466
467 (define-vop (fast-conditional/fixnum fast-conditional)
468   (:args (x :scs (any-reg))
469          (y :scs (any-reg)))
470   (:arg-types tagged-num tagged-num)
471   (:note "inline fixnum comparison"))
472
473 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
474   (:args (x :scs (any-reg)))
475   (:arg-types tagged-num (:constant (signed-byte-with-a-bite-out 14 4)))
476   (:info target not-p y))
477
478 (define-vop (fast-conditional/signed fast-conditional)
479   (:args (x :scs (signed-reg))
480          (y :scs (signed-reg)))
481   (:arg-types signed-num signed-num)
482   (:note "inline (signed-byte 32) comparison"))
483
484 (define-vop (fast-conditional-c/signed fast-conditional/signed)
485   (:args (x :scs (signed-reg)))
486   (:arg-types signed-num (:constant (signed-byte-with-a-bite-out 16 1)))
487   (:info target not-p y))
488
489 (define-vop (fast-conditional/unsigned fast-conditional)
490   (:args (x :scs (unsigned-reg))
491          (y :scs (unsigned-reg)))
492   (:arg-types unsigned-num unsigned-num)
493   (:note "inline (unsigned-byte 32) comparison"))
494
495 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
496   (:args (x :scs (unsigned-reg)))
497   (:arg-types unsigned-num (:constant (and (signed-byte-with-a-bite-out 16 1)
498                                            unsigned-byte)))
499   (:info target not-p y))
500
501
502 (defmacro define-conditional-vop (translate &rest generator)
503   `(progn
504      ,@(mapcar #'(lambda (suffix cost signed)
505                    (unless (and (member suffix '(/fixnum -c/fixnum))
506                                 (eq translate 'eql))
507                      `(define-vop (,(intern (format nil "~:@(FAST-IF-~A~A~)"
508                                                     translate suffix))
509                                    ,(intern
510                                      (format nil "~:@(FAST-CONDITIONAL~A~)"
511                                              suffix)))
512                         (:translate ,translate)
513                         (:generator ,cost
514                           (let* ((signed ,signed)
515                                  (-c/fixnum ,(eq suffix '-c/fixnum))
516                                  (y (if -c/fixnum (fixnumize y) y)))
517                             (declare (ignorable signed -c/fixnum y))
518                             ,@generator)))))
519                '(/fixnum -c/fixnum /signed -c/signed /unsigned -c/unsigned)
520                '(3 2 5 4 5 4)
521                '(t t t t nil nil))))
522
523 (define-conditional-vop <
524   (cond ((and signed (eql y 0))
525          (if not-p
526              (inst bgez x target)
527              (inst bltz x target)))
528         (t
529          (if signed
530              (inst slt temp x y)
531              (inst sltu temp x y))
532          (if not-p
533              (inst beq temp zero-tn target)
534              (inst bne temp zero-tn target))))
535   (inst nop))
536
537 (define-conditional-vop >
538   (cond ((and signed (eql y 0))
539          (if not-p
540              (inst blez x target)
541              (inst bgtz x target)))
542         ((integerp y)
543          (let ((y (+ y (if -c/fixnum (fixnumize 1) 1))))
544            (if signed
545                (inst slt temp x y)
546                (inst sltu temp x y))
547            (if not-p
548                (inst bne temp zero-tn target)
549                (inst beq temp zero-tn target))))
550         (t
551          (if signed
552              (inst slt temp y x)
553              (inst sltu temp y x))
554          (if not-p
555              (inst beq temp zero-tn target)
556              (inst bne temp zero-tn target))))
557   (inst nop))
558
559 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
560 ;;; known fixnum.
561
562 (define-conditional-vop eql
563   (declare (ignore signed))
564   (when (integerp y)
565     (inst li temp y)
566     (setf y temp))
567   (if not-p
568       (inst bne x y target)
569       (inst beq x y target))
570   (inst nop))
571
572 ;;; These versions specify a fixnum restriction on their first arg.  We have
573 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
574 ;;; the first arg and a higher cost.  The reason for doing this is to prevent
575 ;;; fixnum specific operations from being used on word integers, spuriously
576 ;;; consing the argument.
577 ;;;
578 (define-vop (fast-eql/fixnum fast-conditional)
579   (:args (x :scs (any-reg))
580          (y :scs (any-reg)))
581   (:arg-types tagged-num tagged-num)
582   (:note "inline fixnum comparison")
583   (:translate eql)
584   (:ignore temp)
585   (:generator 3
586     (if not-p
587         (inst bne x y target)
588         (inst beq x y target))
589     (inst nop)))
590 ;;;
591 (define-vop (generic-eql/fixnum fast-eql/fixnum)
592   (:args (x :scs (any-reg descriptor-reg))
593          (y :scs (any-reg)))
594   (:arg-types * tagged-num)
595   (:variant-cost 7))
596
597 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
598   (:args (x :scs (any-reg)))
599   (:arg-types tagged-num (:constant (signed-byte 14)))
600   (:info target not-p y)
601   (:translate eql)
602   (:generator 2
603     (let ((y (cond ((eql y 0) zero-tn)
604                    (t
605                     (inst li temp (fixnumize y))
606                     temp))))
607       (if not-p
608           (inst bne x y target)
609           (inst beq x y target))
610       (inst nop))))
611 ;;;
612 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
613   (:args (x :scs (any-reg descriptor-reg)))
614   (:arg-types * (:constant (signed-byte 14)))
615   (:variant-cost 6))
616   
617 \f
618 ;;;; 32-bit logical operations
619
620 (define-vop (merge-bits)
621   (:translate merge-bits)
622   (:args (shift :scs (signed-reg unsigned-reg))
623          (prev :scs (unsigned-reg))
624          (next :scs (unsigned-reg)))
625   (:arg-types tagged-num unsigned-num unsigned-num)
626   (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
627   (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
628   (:results (result :scs (unsigned-reg)))
629   (:result-types unsigned-num)
630   (:policy :fast-safe)
631   (:generator 4
632     (let ((done (gen-label)))
633       (inst beq shift done)
634       (inst srl res next shift)
635       (inst subu temp zero-tn shift)
636       (inst sll temp prev temp)
637       (inst or res res temp)
638       (emit-label done)
639       (move result res))))
640
641 (define-vop (shift-towards-someplace)
642   (:policy :fast-safe)
643   (:args (num :scs (unsigned-reg))
644          (amount :scs (signed-reg)))
645   (:arg-types unsigned-num tagged-num)
646   (:results (r :scs (unsigned-reg)))
647   (:result-types unsigned-num))
648
649 (define-vop (shift-towards-start shift-towards-someplace)
650   (:translate shift-towards-start)
651   (:note "SHIFT-TOWARDS-START")
652   (:generator 1
653     (ecase *backend-byte-order*
654       (:big-endian
655        (inst sll r num amount))
656       (:little-endian
657        (inst srl r num amount)))))
658
659 (define-vop (shift-towards-end shift-towards-someplace)
660   (:translate shift-towards-end)
661   (:note "SHIFT-TOWARDS-END")
662   (:generator 1
663     (ecase *backend-byte-order*
664       (:big-endian
665        (inst srl r num amount))
666       (:little-endian
667        (inst sll r num amount)))))
668 \f
669 ;;;; Modular arithmetic
670 (define-modular-fun +-mod32 (x y) + 32)
671 (define-vop (fast-+-mod32/unsigned=>unsigned fast-+/unsigned=>unsigned)
672   (:translate +-mod32))
673 (define-vop (fast-+-mod32-c/unsigned=>unsigned fast-+-c/unsigned=>unsigned)
674   (:translate +-mod32))
675 (define-modular-fun --mod32 (x y) - 32)
676 (define-vop (fast---mod32/unsigned=>unsigned fast--/unsigned=>unsigned)
677   (:translate --mod32))
678 (define-vop (fast---mod32-c/unsigned=>unsigned fast---c/unsigned=>unsigned)
679   (:translate --mod32))
680
681 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
682              fast-ash-c/unsigned=>unsigned)
683   (:translate ash-left-mod32))
684
685 (define-vop (fast-ash-left-mod32/unsigned=>unsigned
686              fast-ash-left/unsigned=>unsigned))
687 (deftransform ash-left-mod32 ((integer count)
688                               ((unsigned-byte 32) (unsigned-byte 5)))
689   (when (sb!c::constant-lvar-p count)
690     (sb!c::give-up-ir1-transform))
691   '(%primitive fast-ash-left-mod32/unsigned=>unsigned integer count))
692
693 ;;; logical operations
694 (define-modular-fun lognot-mod32 (x) lognot 32)
695 (define-vop (lognot-mod32/unsigned=>unsigned)
696   (:translate lognot-mod32)
697   (:args (x :scs (unsigned-reg)))
698   (:arg-types unsigned-num)
699   (:results (r :scs (unsigned-reg)))
700   (:result-types unsigned-num)
701   (:policy :fast-safe)
702   (:generator 1
703     (inst nor r x zero-tn)))
704
705 (define-modular-fun logxor-mod32 (x y) logxor 32)
706 (define-vop (fast-logxor-mod32/unsigned=>unsigned
707              fast-logxor/unsigned=>unsigned)
708   (:translate logxor-mod32))
709 (define-vop (fast-logxor-mod32-c/unsigned=>unsigned
710              fast-logxor-c/unsigned=>unsigned)
711   (:translate logxor-mod32))
712
713 (define-modular-fun lognor-mod32 (x y) lognor 32)
714 (define-vop (fast-lognor-mod32/unsigned=>unsigned
715              fast-lognor/unsigned=>unsigned)
716   (:translate lognor-mod32))
717
718 (define-source-transform logeqv (&rest args)
719   (if (oddp (length args))
720       `(logxor ,@args)
721       `(lognot (logxor ,@args))))
722 (define-source-transform logandc1 (x y)
723   `(logand (lognot ,x) ,y))
724 (define-source-transform logandc2 (x y)
725   `(logand ,x (lognot ,y)))
726 (define-source-transform logorc1 (x y)
727   `(logior (lognot ,x) ,y))
728 (define-source-transform logorc2 (x y)
729   `(logior ,x (lognot ,y)))
730 (define-source-transform lognand (x y)
731   `(lognot (logand ,x ,y)))
732 ;;;; Bignum stuff.
733
734 (define-vop (bignum-length get-header-data)
735   (:translate sb!bignum:%bignum-length)
736   (:policy :fast-safe))
737
738 (define-vop (bignum-set-length set-header-data)
739   (:translate sb!bignum:%bignum-set-length)
740   (:policy :fast-safe))
741
742 (define-full-reffer bignum-ref * bignum-digits-offset other-pointer-lowtag
743   (unsigned-reg) unsigned-num sb!bignum:%bignum-ref)
744
745 (define-full-setter bignum-set * bignum-digits-offset other-pointer-lowtag
746   (unsigned-reg) unsigned-num sb!bignum:%bignum-set)
747
748 (define-vop (digit-0-or-plus)
749   (:translate sb!bignum:%digit-0-or-plusp)
750   (:policy :fast-safe)
751   (:args (digit :scs (unsigned-reg)))
752   (:arg-types unsigned-num)
753   (:conditional)
754   (:info target not-p)
755   (:generator 2
756     (if not-p
757         (inst bltz digit target)
758         (inst bgez digit target))
759     (inst nop)))
760
761 (define-vop (add-w/carry)
762   (:translate sb!bignum:%add-with-carry)
763   (:policy :fast-safe)
764   (:args (a :scs (unsigned-reg))
765          (b :scs (unsigned-reg))
766          (c :scs (any-reg)))
767   (:arg-types unsigned-num unsigned-num positive-fixnum)
768   (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
769   (:results (result :scs (unsigned-reg))
770             (carry :scs (unsigned-reg) :from :eval))
771   (:result-types unsigned-num positive-fixnum)
772   (:temporary (:scs (non-descriptor-reg)) temp)
773   (:generator 5
774     (let ((carry-in (gen-label))
775           (done (gen-label)))
776       (inst bne c carry-in)
777       (inst addu res a b)
778
779       (inst b done)
780       (inst sltu carry res b)
781
782       (emit-label carry-in)
783       (inst addu res 1)
784       (inst nor temp a zero-tn)
785       (inst sltu carry b temp)
786       (inst xor carry 1)
787
788       (emit-label done)
789       (move result res))))
790
791 (define-vop (sub-w/borrow)
792   (:translate sb!bignum:%subtract-with-borrow)
793   (:policy :fast-safe)
794   (:args (a :scs (unsigned-reg))
795          (b :scs (unsigned-reg))
796          (c :scs (any-reg)))
797   (:arg-types unsigned-num unsigned-num positive-fixnum)
798   (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
799   (:results (result :scs (unsigned-reg))
800             (borrow :scs (unsigned-reg) :from :eval))
801   (:result-types unsigned-num positive-fixnum)
802   (:generator 4
803     (let ((no-borrow-in (gen-label))
804           (done (gen-label)))
805
806       (inst bne c no-borrow-in)
807       (inst subu res a b)
808
809       (inst subu res 1)
810       (inst b done)
811       (inst sltu borrow b a)
812
813       (emit-label no-borrow-in)
814       (inst sltu borrow a b)
815       (inst xor borrow 1)
816
817       (emit-label done)
818       (move result res))))
819
820 (define-vop (bignum-mult-and-add-3-arg)
821   (:translate sb!bignum:%multiply-and-add)
822   (:policy :fast-safe)
823   (:args (x :scs (unsigned-reg))
824          (y :scs (unsigned-reg))
825          (carry-in :scs (unsigned-reg) :to :save))
826   (:arg-types unsigned-num unsigned-num unsigned-num)
827   (:temporary (:scs (unsigned-reg) :from (:argument 1)) temp)
828   (:results (hi :scs (unsigned-reg))
829             (lo :scs (unsigned-reg)))
830   (:result-types unsigned-num unsigned-num)
831   (:generator 6
832     (inst multu x y)
833     (inst mflo temp)
834     (inst addu lo temp carry-in)
835     (inst sltu temp lo carry-in)
836     (inst mfhi hi)
837     (inst addu hi temp)))
838
839 (define-vop (bignum-mult-and-add-4-arg)
840   (:translate sb!bignum:%multiply-and-add)
841   (:policy :fast-safe)
842   (:args (x :scs (unsigned-reg))
843          (y :scs (unsigned-reg))
844          (prev :scs (unsigned-reg))
845          (carry-in :scs (unsigned-reg) :to :save))
846   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
847   (:temporary (:scs (unsigned-reg) :from (:argument 2)) temp)
848   (:results (hi :scs (unsigned-reg))
849             (lo :scs (unsigned-reg)))
850   (:result-types unsigned-num unsigned-num)
851   (:generator 9
852     (inst multu x y)
853     (inst addu lo prev carry-in)
854     (inst sltu temp lo carry-in)
855     (inst mfhi hi)
856     (inst addu hi temp)
857     (inst mflo temp)
858     (inst addu lo temp)
859     (inst sltu temp lo temp)
860     (inst addu hi temp)))
861
862 (define-vop (bignum-mult)
863   (:translate sb!bignum:%multiply)
864   (:policy :fast-safe)
865   (:args (x :scs (unsigned-reg))
866          (y :scs (unsigned-reg)))
867   (:arg-types unsigned-num unsigned-num)
868   (:results (hi :scs (unsigned-reg))
869             (lo :scs (unsigned-reg)))
870   (:result-types unsigned-num unsigned-num)
871   (:generator 3
872     (inst multu x y)
873     (inst mflo lo)
874     (inst mfhi hi)))
875
876 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
877   (:translate sb!bignum:%lognot))
878
879 (define-vop (fixnum-to-digit)
880   (:translate sb!bignum:%fixnum-to-digit)
881   (:policy :fast-safe)
882   (:args (fixnum :scs (any-reg)))
883   (:arg-types tagged-num)
884   (:results (digit :scs (unsigned-reg)))
885   (:result-types unsigned-num)
886   (:generator 1
887     (inst sra digit fixnum 2)))
888
889 (define-vop (bignum-floor)
890   (:translate sb!bignum:%floor)
891   (:policy :fast-safe)
892   (:args (num-high :scs (unsigned-reg) :target rem)
893          (num-low :scs (unsigned-reg) :target rem-low)
894          (denom :scs (unsigned-reg) :to (:eval 1)))
895   (:arg-types unsigned-num unsigned-num unsigned-num)
896   (:temporary (:scs (unsigned-reg) :from (:argument 1)) rem-low)
897   (:temporary (:scs (unsigned-reg) :from (:eval 0)) temp)
898   (:results (quo :scs (unsigned-reg) :from (:eval 0))
899             (rem :scs (unsigned-reg) :from (:argument 0)))
900   (:result-types unsigned-num unsigned-num)
901   (:generator 325 ; number of inst assuming targeting works.
902     (move rem num-high)
903     (move rem-low num-low)
904     (flet ((maybe-subtract (&optional (guess temp))
905              (inst subu temp guess 1)
906              (inst and temp denom)
907              (inst subu rem temp)))
908       (inst sltu quo rem denom)
909       (maybe-subtract quo)
910       (dotimes (i 32)
911         (inst sll rem 1)
912         (inst srl temp rem-low 31)
913         (inst or rem temp)
914         (inst sll rem-low 1)
915         (inst sltu temp rem denom)
916         (inst sll quo 1)
917         (inst or quo temp)
918         (maybe-subtract)))
919     (inst nor quo zero-tn)))
920
921 (define-vop (signify-digit)
922   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
923   (:policy :fast-safe)
924   (:args (digit :scs (unsigned-reg) :target res))
925   (:arg-types unsigned-num)
926   (:results (res :scs (any-reg signed-reg)))
927   (:result-types signed-num)
928   (:generator 1
929     (sc-case res
930       (any-reg
931        (inst sll res digit 2))
932       (signed-reg
933        (move res digit)))))
934
935
936 (define-vop (digit-ashr)
937   (:translate sb!bignum:%ashr)
938   (:policy :fast-safe)
939   (:args (digit :scs (unsigned-reg))
940          (count :scs (unsigned-reg)))
941   (:arg-types unsigned-num positive-fixnum)
942   (:results (result :scs (unsigned-reg)))
943   (:result-types unsigned-num)
944   (:generator 1
945     (inst sra result digit count)))
946
947 (define-vop (digit-lshr digit-ashr)
948   (:translate sb!bignum:%digit-logical-shift-right)
949   (:generator 1
950     (inst srl result digit count)))
951
952 (define-vop (digit-ashl digit-ashr)
953   (:translate sb!bignum:%ashl)
954   (:generator 1
955     (inst sll result digit count)))
956
957 \f
958 ;;;; Static functions.
959
960 (define-static-fun two-arg-gcd (x y) :translate gcd)
961 (define-static-fun two-arg-lcm (x y) :translate lcm)
962                   
963 (define-static-fun two-arg-+ (x y) :translate +)
964 (define-static-fun two-arg-- (x y) :translate -)
965 (define-static-fun two-arg-* (x y) :translate *)
966 (define-static-fun two-arg-/ (x y) :translate /)
967                   
968 (define-static-fun two-arg-< (x y) :translate <)
969 (define-static-fun two-arg-<= (x y) :translate <=)
970 (define-static-fun two-arg-> (x y) :translate >)
971 (define-static-fun two-arg->= (x y) :translate >=)
972 (define-static-fun two-arg-= (x y) :translate =)
973 (define-static-fun two-arg-/= (x y) :translate /=)
974                   
975 (define-static-fun %negate (x) :translate %negate)
976
977 (define-static-fun two-arg-and (x y) :translate logand)
978 (define-static-fun two-arg-ior (x y) :translate logior)
979 (define-static-fun two-arg-xor (x y) :translate logxor)