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