abba8aa4fd4e1304a5e62857a1fdb129778cf1b4
[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 immediate)))
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 3
344     (sc-case amount
345       (signed-reg
346        (let ((positive (gen-label))
347              (done (gen-label)))
348          (inst cmpwi amount 0)
349          (inst neg ndesc amount)
350          (inst bge positive)
351          (inst cmpwi ndesc 31)
352          (inst srw result number ndesc)
353          (inst ble done)
354          (move result zero-tn)
355          (inst b done)
356
357          (emit-label positive)
358          ;; The result-type assures us that this shift will not overflow.
359          (inst slw result number amount)
360
361          (emit-label done)))
362       (immediate
363        (let ((amount (tn-value amount)))
364          (cond
365           ((and (minusp amount) (< amount -31)) (move result zero-tn))
366           ((minusp amount) (inst srwi result number (- amount)))
367           (t (inst slwi result number amount))))))))
368
369 (define-vop (fast-ash/signed=>signed)
370   (:note "inline ASH")
371   (:args (number :scs (signed-reg) :to :save)
372          (amount :scs (signed-reg immediate)))
373   (:arg-types (:or signed-num) signed-num)
374   (:results (result :scs (signed-reg)))
375   (:result-types (:or signed-num))
376   (:translate ash)
377   (:policy :fast-safe)
378   (:temporary (:sc non-descriptor-reg) ndesc)
379   (:generator 3
380     (sc-case amount
381       (signed-reg
382        (let ((positive (gen-label))
383              (done (gen-label)))
384          (inst cmpwi amount 0)
385          (inst neg ndesc amount)
386          (inst bge positive)
387          (inst cmpwi ndesc 31)
388          (inst sraw result number ndesc)
389          (inst ble done)
390          (inst srawi result number 31)
391          (inst b done)
392
393          (emit-label positive)
394          ;; The result-type assures us that this shift will not overflow.
395          (inst slw result number amount)
396
397          (emit-label done)))
398
399       (immediate
400        (let ((amount (tn-value amount)))
401          (if (minusp amount)
402              (let ((amount (min 31 (- amount))))
403                (inst srawi result number amount))
404              (inst slwi result number amount)))))))
405
406
407
408 (define-vop (signed-byte-32-len)
409   (:translate integer-length)
410   (:note "inline (signed-byte 32) integer-length")
411   (:policy :fast-safe)
412   (:args (arg :scs (signed-reg)))
413   (:arg-types signed-num)
414   (:results (res :scs (any-reg)))
415   (:result-types positive-fixnum)
416   (:temporary (:scs (non-descriptor-reg) :to (:argument 0)) shift)
417   (:generator 6
418     ; (integer-length arg) = (- 32 (cntlz (if (>= arg 0) arg (lognot arg))))
419     (let ((nonneg (gen-label)))
420       (inst cntlzw. shift arg)
421       (inst bne nonneg)
422       (inst not shift arg)
423       (inst cntlzw shift shift)
424       (emit-label nonneg)
425       (inst slwi shift shift 2)
426       (inst subfic res  shift (fixnumize 32)))))
427
428 (define-vop (unsigned-byte-32-count)
429   (:translate logcount)
430   (:note "inline (unsigned-byte 32) logcount")
431   (:policy :fast-safe)
432   (:args (arg :scs (unsigned-reg) :target shift))
433   (:arg-types unsigned-num)
434   (:results (res :scs (any-reg)))
435   (:result-types positive-fixnum)
436   (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) shift temp)
437   (:generator 30
438     (let ((loop (gen-label))
439           (done (gen-label)))
440       (inst add. shift zero-tn arg)
441       (move res zero-tn)
442       (inst beq done)
443
444       (emit-label loop)
445       (inst subi temp shift 1)
446       (inst and. shift shift temp)
447       (inst addi res res (fixnumize 1))
448       (inst bne loop)
449
450       (emit-label done))))
451
452 \f
453 ;;;; Modular functions:
454 (define-modular-fun lognot-mod32 (x) lognot 32)
455 (define-vop (lognot-mod32/unsigned=>unsigned)
456   (:translate lognot-mod32)
457   (:args (x :scs (unsigned-reg)))
458   (:arg-types unsigned-num)
459   (:results (res :scs (unsigned-reg)))
460   (:result-types unsigned-num)
461   (:policy :fast-safe)
462   (:generator 1
463     (inst not res x)))
464
465 (macrolet 
466     ((define-modular-backend (fun &optional constantp)
467        (let ((mfun-name (symbolicate fun '-mod32))
468              (modvop (symbolicate 'fast- fun '-mod32/unsigned=>unsigned))
469              (modcvop (symbolicate 'fast- fun 'mod32-c/unsigned=>unsigned))
470              (vop (symbolicate 'fast- fun '/unsigned=>unsigned))
471              (cvop (symbolicate 'fast- fun '-c/unsigned=>unsigned)))
472          `(progn
473             (define-modular-fun ,mfun-name (x y) ,fun 32)
474             (define-vop (,modvop ,vop)
475               (:translate ,mfun-name))
476             ,@(when constantp
477                 `((define-vop (,modcvop ,cvop)
478                     (:translate ,mfun-name))))))))
479   (define-modular-backend + t)
480   (define-modular-backend logxor t)
481   (define-modular-backend logeqv)
482   (define-modular-backend lognand)
483   (define-modular-backend lognor)
484   (define-modular-backend logandc1)
485   (define-modular-backend logandc2)
486   (define-modular-backend logorc1)
487   (define-modular-backend logorc2))
488 \f
489 ;;;; Binary conditional VOPs:
490
491 (define-vop (fast-conditional)
492   (:conditional)
493   (:info target not-p)
494   (:effects)
495   (:affected)
496   (:policy :fast-safe))
497
498 (define-vop (fast-conditional/fixnum fast-conditional)
499   (:args (x :scs (any-reg zero))
500          (y :scs (any-reg zero)))
501   (:arg-types tagged-num tagged-num)
502   (:note "inline fixnum comparison"))
503
504 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
505   (:args (x :scs (any-reg zero)))
506   (:arg-types tagged-num (:constant (signed-byte 14)))
507   (:info target not-p y))
508
509 (define-vop (fast-conditional/signed fast-conditional)
510   (:args (x :scs (signed-reg zero))
511          (y :scs (signed-reg zero)))
512   (:arg-types signed-num signed-num)
513   (:note "inline (signed-byte 32) comparison"))
514
515 (define-vop (fast-conditional-c/signed fast-conditional/signed)
516   (:args (x :scs (signed-reg zero)))
517   (:arg-types signed-num (:constant (signed-byte 16)))
518   (:info target not-p y))
519
520 (define-vop (fast-conditional/unsigned fast-conditional)
521   (:args (x :scs (unsigned-reg zero))
522          (y :scs (unsigned-reg zero)))
523   (:arg-types unsigned-num unsigned-num)
524   (:note "inline (unsigned-byte 32) comparison"))
525
526 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
527   (:args (x :scs (unsigned-reg zero)))
528   (:arg-types unsigned-num (:constant (unsigned-byte 16)))
529   (:info target not-p y))
530
531
532 (define-vop (fast-if-</fixnum fast-conditional/fixnum)
533   (:translate <)
534   (:generator 4
535     (inst cmpw x y)
536     (inst b? (if not-p :ge :lt) target)))
537
538 (define-vop (fast-if-<-c/fixnum fast-conditional-c/fixnum)
539   (:translate <)
540   (:generator 3
541     (inst cmpwi x (fixnumize y))
542     (inst b? (if not-p :ge :lt) target)))
543
544 (define-vop (fast-if-</signed fast-conditional/signed)
545   (:translate <)
546   (:generator 6
547     (inst cmpw x y)
548     (inst b? (if not-p :ge :lt) target)))
549
550 (define-vop (fast-if-<-c/signed fast-conditional-c/signed)
551   (:translate <)
552   (:generator 5
553     (inst cmpwi x y)
554     (inst b? (if not-p :ge :lt) target)))
555
556 (define-vop (fast-if-</unsigned fast-conditional/unsigned)
557   (:translate <)
558   (:generator 6
559     (inst cmplw x y)
560     (inst b? (if not-p :ge :lt) target)))
561
562 (define-vop (fast-if-<-c/unsigned fast-conditional-c/unsigned)
563   (:translate <)
564   (:generator 5
565     (inst cmplwi x y)
566     (inst b? (if not-p :ge :lt) target)))
567
568 (define-vop (fast-if->/fixnum fast-conditional/fixnum)
569   (:translate >)
570   (:generator 4
571     (inst cmpw x y)
572     (inst b? (if not-p :le :gt) target)))
573
574 (define-vop (fast-if->-c/fixnum fast-conditional-c/fixnum)
575   (:translate >)
576   (:generator 3
577     (inst cmpwi x (fixnumize y))
578     (inst b? (if not-p :le :gt) target)))
579
580 (define-vop (fast-if->/signed fast-conditional/signed)
581   (:translate >)
582   (:generator 6
583     (inst cmpw x y)
584     (inst b? (if not-p :le :gt) target)))
585
586 (define-vop (fast-if->-c/signed fast-conditional-c/signed)
587   (:translate >)
588   (:generator 5
589     (inst cmpwi x y)
590     (inst b? (if not-p :le :gt) target)))
591
592 (define-vop (fast-if->/unsigned fast-conditional/unsigned)
593   (:translate >)
594   (:generator 6
595     (inst cmplw x y)
596     (inst b? (if not-p :le :gt) target)))
597
598 (define-vop (fast-if->-c/unsigned fast-conditional-c/unsigned)
599   (:translate >)
600   (:generator 5
601     (inst cmplwi x y)
602     (inst b? (if not-p :le :gt) target)))
603
604 (define-vop (fast-if-eql/signed fast-conditional/signed)
605   (:translate eql)
606   (:generator 6
607     (inst cmpw x y)
608     (inst b? (if not-p :ne :eq) target)))
609
610 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
611   (:translate eql)
612   (:generator 5
613     (inst cmpwi x y)
614     (inst b? (if not-p :ne :eq) target)))
615
616 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
617   (:translate eql)
618   (:generator 6
619     (inst cmplw x y)
620     (inst b? (if not-p :ne :eq) target)))
621
622 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
623   (:translate eql)
624   (:generator 5
625     (inst cmplwi x y)
626     (inst b? (if not-p :ne :eq) target)))
627
628
629 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
630 ;;; known fixnum.
631
632 ;;; These versions specify a fixnum restriction on their first arg.  We have
633 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
634 ;;; the first arg and a higher cost.  The reason for doing this is to prevent
635 ;;; fixnum specific operations from being used on word integers, spuriously
636 ;;; consing the argument.
637 ;;;
638
639 (define-vop (fast-eql/fixnum fast-conditional)
640   (:args (x :scs (any-reg descriptor-reg zero))
641          (y :scs (any-reg zero)))
642   (:arg-types tagged-num tagged-num)
643   (:note "inline fixnum comparison")
644   (:translate eql)
645   (:generator 4
646     (inst cmpw x y)
647     (inst b? (if not-p :ne :eq) target)))
648 ;;;
649 (define-vop (generic-eql/fixnum fast-eql/fixnum)
650   (:arg-types * tagged-num)
651   (:variant-cost 7))
652
653 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
654   (:args (x :scs (any-reg descriptor-reg zero)))
655   (:arg-types tagged-num (:constant (signed-byte 14)))
656   (:info target not-p y)
657   (:translate eql)
658   (:generator 2
659     (inst cmpwi x (fixnumize y))
660     (inst b? (if not-p :ne :eq) target)))
661 ;;;
662 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
663   (:arg-types * (:constant (signed-byte 11)))
664   (:variant-cost 6))
665
666 \f
667 ;;;; 32-bit logical operations
668
669 (define-vop (merge-bits)
670   (:translate merge-bits)
671   (:args (shift :scs (signed-reg unsigned-reg))
672          (prev :scs (unsigned-reg))
673          (next :scs (unsigned-reg)))
674   (:arg-types tagged-num unsigned-num unsigned-num)
675   (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
676   (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
677   (:results (result :scs (unsigned-reg)))
678   (:result-types unsigned-num)
679   (:policy :fast-safe)
680   (:generator 4
681     (let ((done (gen-label)))
682       (inst cmpwi shift 0)
683       (inst beq done)
684       (inst srw res next shift)
685       (inst sub temp zero-tn shift)
686       (inst slw temp prev temp)
687       (inst or res res temp)
688       (emit-label done)
689       (move result res))))
690
691 (define-source-transform 32bit-logical-not (x)
692   `(logand (lognot (the (unsigned-byte 32) ,x)) #.(1- (ash 1 32))))
693
694 (deftransform 32bit-logical-and ((x y))
695   '(logand x y))
696
697 (deftransform 32bit-logical-nand ((x y))
698   '(logand (lognand x y) #.(1- (ash 1 32))))
699
700 (deftransform 32bit-logical-or ((x y))
701   '(logior x y))
702
703 (deftransform 32bit-logical-nor ((x y))
704   '(logand (lognor x y) #.(1- (ash 1 32))))
705
706 (deftransform 32bit-logical-xor ((x y))
707   '(logxor x y))
708
709 (deftransform 32bit-logical-eqv ((x y))
710   '(logand (logeqv x y) #.(1- (ash 1 32))))
711
712 (deftransform 32bit-logical-orc1 ((x y))
713   '(logand (logorc1 x y) #.(1- (ash 1 32))))
714
715 (deftransform 32bit-logical-orc2 ((x y))
716   '(logand (logorc2 x y) #.(1- (ash 1 32))))
717
718 (deftransform 32bit-logical-andc1 ((x y))
719   '(logand (logandc1 x y) #.(1- (ash 1 32))))
720
721 (deftransform 32bit-logical-andc2 ((x y))
722   '(logand (logandc2 x y) #.(1- (ash 1 32))))
723
724 (define-vop (shift-towards-someplace)
725   (:policy :fast-safe)
726   (:args (num :scs (unsigned-reg))
727          (amount :scs (signed-reg)))
728   (:arg-types unsigned-num tagged-num)
729   (:results (r :scs (unsigned-reg)))
730   (:result-types unsigned-num))
731
732 (define-vop (shift-towards-start shift-towards-someplace)
733   (:translate shift-towards-start)
734   (:note "shift-towards-start")
735   (:generator 1
736     (inst rlwinm amount amount 0 27 31)
737     (inst slw r num amount)))
738
739 (define-vop (shift-towards-end shift-towards-someplace)
740   (:translate shift-towards-end)
741   (:note "shift-towards-end")
742   (:generator 1
743     (inst rlwinm amount amount 0 27 31)
744     (inst srw r num amount)))
745 \f
746 ;;;; Bignum stuff.
747
748 (define-vop (bignum-length get-header-data)
749   (:translate sb!bignum::%bignum-length)
750   (:policy :fast-safe))
751
752 (define-vop (bignum-set-length set-header-data)
753   (:translate sb!bignum::%bignum-set-length)
754   (:policy :fast-safe))
755
756 (define-vop (bignum-ref word-index-ref)
757   (:variant sb!vm:bignum-digits-offset sb!vm:other-pointer-lowtag)
758   (:translate sb!bignum::%bignum-ref)
759   (:results (value :scs (unsigned-reg)))
760   (:result-types unsigned-num))
761
762 (define-vop (bignum-set word-index-set)
763   (:variant sb!vm:bignum-digits-offset sb!vm:other-pointer-lowtag)
764   (:translate sb!bignum::%bignum-set)
765   (:args (object :scs (descriptor-reg))
766          (index :scs (any-reg immediate zero))
767          (value :scs (unsigned-reg)))
768   (:arg-types t positive-fixnum unsigned-num)
769   (:results (result :scs (unsigned-reg)))
770   (:result-types unsigned-num))
771
772 (define-vop (digit-0-or-plus)
773   (:translate sb!bignum::%digit-0-or-plusp)
774   (:policy :fast-safe)
775   (:args (digit :scs (unsigned-reg)))
776   (:arg-types unsigned-num)
777   (:results (result :scs (descriptor-reg)))
778   (:generator 3
779     (let ((done (gen-label)))
780       (inst cmpwi digit 0)
781       (move result null-tn)
782       (inst blt done)
783       (load-symbol result t)
784       (emit-label done))))
785
786 (define-vop (add-w/carry)
787   (:translate sb!bignum::%add-with-carry)
788   (:policy :fast-safe)
789   (:args (a :scs (unsigned-reg))
790          (b :scs (unsigned-reg))
791          (c :scs (any-reg)))
792   (:arg-types unsigned-num unsigned-num positive-fixnum)
793   (:temporary (:scs (unsigned-reg)) temp)
794   (:results (result :scs (unsigned-reg))
795             (carry :scs (unsigned-reg)))
796   (:result-types unsigned-num positive-fixnum)
797   (:generator 3
798     (inst addic temp c -1)
799     (inst adde result a b)
800     (inst addze carry zero-tn)))
801
802 (define-vop (sub-w/borrow)
803   (:translate sb!bignum::%subtract-with-borrow)
804   (:policy :fast-safe)
805   (:args (a :scs (unsigned-reg))
806          (b :scs (unsigned-reg))
807          (c :scs (any-reg)))
808   (:arg-types unsigned-num unsigned-num positive-fixnum)
809   (:temporary (:scs (unsigned-reg)) temp)
810   (:results (result :scs (unsigned-reg))
811             (borrow :scs (unsigned-reg)))
812   (:result-types unsigned-num positive-fixnum)
813   (:generator 4
814     (inst addic temp c -1)
815     (inst sube result a b)
816     (inst addze borrow zero-tn)))
817
818 (define-vop (bignum-mult-and-add-3-arg)
819   (:translate sb!bignum::%multiply-and-add)
820   (:policy :fast-safe)
821   (:args (x :scs (unsigned-reg))
822          (y :scs (unsigned-reg))
823          (carry-in :scs (unsigned-reg) :to (:eval 1)))
824   (:arg-types unsigned-num unsigned-num unsigned-num)
825   (:temporary (:scs (unsigned-reg) :to (:result 0) :target hi) hi-temp)
826   (:temporary (:scs (unsigned-reg) :from (:eval 0) :to (:result 1)
827                     :target lo) lo-temp)
828   (:results (hi :scs (unsigned-reg))
829             (lo :scs (unsigned-reg)))
830   (:result-types unsigned-num unsigned-num)
831   (:generator 40
832     (inst mulhwu hi-temp x y)
833     (inst mullw lo-temp x y)
834     (inst addc lo lo-temp carry-in)
835     (inst addze hi hi-temp)))
836
837 (define-vop (bignum-mult-and-add-4-arg)
838   (:translate sb!bignum::%multiply-and-add)
839   (:policy :fast-safe)
840   (:args (x :scs (unsigned-reg))
841          (y :scs (unsigned-reg))
842          (prev :scs (unsigned-reg) :to (:eval 1))
843          (carry-in :scs (unsigned-reg) :to (:eval 1)))
844   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
845   (:temporary (:scs (unsigned-reg) :to (:result 0) :target hi) hi-temp)
846   (:temporary (:scs (unsigned-reg) :from (:eval 0) :to (:result 1)
847                     :target lo) lo-temp)
848   (:results (hi :scs (unsigned-reg))
849             (lo :scs (unsigned-reg)))
850   (:result-types unsigned-num unsigned-num)
851   (:generator 40
852     (inst mulhwu hi-temp x y)
853     (inst mullw lo-temp x y)
854     (inst addc lo-temp lo-temp carry-in)
855     (inst addze hi-temp hi-temp)
856     (inst addc lo lo-temp prev)
857     (inst addze hi hi-temp)))
858
859 (define-vop (bignum-mult)
860   (:translate sb!bignum::%multiply)
861   (:policy :fast-safe)
862   (:args (x :scs (unsigned-reg) :to (:eval 1))
863          (y :scs (unsigned-reg) :to (:eval 1)))
864   (:arg-types unsigned-num unsigned-num)
865   (:results (hi :scs (unsigned-reg) :from (:eval 1))
866             (lo :scs (unsigned-reg) :from (:eval 0)))
867   (:result-types unsigned-num unsigned-num)
868   (:generator 40
869     (inst mullw lo x y)
870     (inst mulhwu hi x y)))
871
872 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
873   (:translate sb!bignum::%lognot))
874
875 (define-vop (fixnum-to-digit)
876   (:translate sb!bignum::%fixnum-to-digit)
877   (:policy :fast-safe)
878   (:args (fixnum :scs (any-reg)))
879   (:arg-types tagged-num)
880   (:results (digit :scs (unsigned-reg)))
881   (:result-types unsigned-num)
882   (:generator 1
883     (inst srawi digit fixnum 2)))
884
885
886 (define-vop (bignum-floor)
887   (:translate sb!bignum::%floor)
888   (:policy :fast-safe)
889   (:args (num-high :scs (unsigned-reg) :target rem)
890          (num-low :scs (unsigned-reg) :target rem-low)
891          (denom :scs (unsigned-reg) :to (:eval 1)))
892   (:arg-types unsigned-num unsigned-num unsigned-num)
893   (:temporary (:scs (unsigned-reg) :from (:argument 1)) rem-low)
894   (:temporary (:scs (unsigned-reg) :from (:eval 0)) temp)
895   (:results (quo :scs (unsigned-reg) :from (:eval 0))
896             (rem :scs (unsigned-reg) :from (:argument 0)))
897   (:result-types unsigned-num unsigned-num)
898   (:generator 325 ; number of inst assuming targeting works.
899     (move rem num-high)
900     (move rem-low num-low)
901     (flet ((maybe-subtract (&optional (guess temp))
902              (inst subi temp guess 1)
903              (inst and temp temp denom)
904              (inst sub rem rem temp))
905            (sltu (res x y)
906              (inst subfc res y x)
907              (inst subfe res res res)
908              (inst neg res res)))
909       (sltu quo rem denom)
910       (maybe-subtract quo)
911       (dotimes (i 32)
912         (inst slwi rem rem 1)
913         (inst srwi temp rem-low 31)
914         (inst or rem rem temp)
915         (inst slwi rem-low rem-low 1)
916         (sltu temp rem denom)
917         (inst slwi quo quo 1)
918         (inst or quo quo temp)
919         (maybe-subtract)))
920     (inst not quo quo)))
921
922 #|
923
924 (define-vop (bignum-floor)
925   (:translate sb!bignum::%floor)
926   (:policy :fast-safe)
927   (:args (div-high :scs (unsigned-reg) :target rem)
928          (div-low :scs (unsigned-reg) :target quo)
929          (divisor :scs (unsigned-reg)))
930   (:arg-types unsigned-num unsigned-num unsigned-num)
931   (:results (quo :scs (unsigned-reg) :from (:argument 1))
932             (rem :scs (unsigned-reg) :from (:argument 0)))
933   (:result-types unsigned-num unsigned-num)
934   (:generator 300
935     (inst mtmq div-low)
936     (inst div quo div-high divisor)
937     (inst mfmq rem)))
938 |#
939
940 (define-vop (signify-digit)
941   (:translate sb!bignum::%fixnum-digit-with-correct-sign)
942   (:policy :fast-safe)
943   (:args (digit :scs (unsigned-reg) :target res))
944   (:arg-types unsigned-num)
945   (:results (res :scs (any-reg signed-reg)))
946   (:result-types signed-num)
947   (:generator 1
948     (sc-case res
949       (any-reg
950        (inst slwi res digit 2))
951       (signed-reg
952        (move res digit)))))
953
954
955 (define-vop (digit-ashr)
956   (:translate sb!bignum::%ashr)
957   (:policy :fast-safe)
958   (:args (digit :scs (unsigned-reg))
959          (count :scs (unsigned-reg)))
960   (:arg-types unsigned-num positive-fixnum)
961   (:results (result :scs (unsigned-reg)))
962   (:result-types unsigned-num)
963   (:generator 1
964     (inst sraw result digit count)))
965
966 (define-vop (digit-lshr digit-ashr)
967   (:translate sb!bignum::%digit-logical-shift-right)
968   (:generator 1
969     (inst srw result digit count)))
970
971 (define-vop (digit-ashl digit-ashr)
972   (:translate sb!bignum::%ashl)
973   (:generator 1
974     (inst slw result digit count)))
975
976 \f
977 ;;;; Static funs.
978
979 (define-static-fun two-arg-gcd (x y) :translate gcd)
980 (define-static-fun two-arg-lcm (x y) :translate lcm)
981
982 (define-static-fun two-arg-+ (x y) :translate +)
983 (define-static-fun two-arg-- (x y) :translate -)
984 (define-static-fun two-arg-* (x y) :translate *)
985 (define-static-fun two-arg-/ (x y) :translate /)
986
987 (define-static-fun two-arg-< (x y) :translate <)
988 (define-static-fun two-arg-<= (x y) :translate <=)
989 (define-static-fun two-arg-> (x y) :translate >)
990 (define-static-fun two-arg->= (x y) :translate >=)
991 (define-static-fun two-arg-= (x y) :translate =)
992 (define-static-fun two-arg-/= (x y) :translate /=)
993
994 (define-static-fun %negate (x) :translate %negate)
995
996 (define-static-fun two-arg-and (x y) :translate logand)
997 (define-static-fun two-arg-ior (x y) :translate logior)
998 (define-static-fun two-arg-xor (x y) :translate logxor)
999 (define-static-fun two-arg-eqv (x y) :translate logeqv)
1000 \f
1001 (in-package "SB!C")
1002
1003 (deftransform * ((x y)
1004                  ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1005                  (unsigned-byte 32))
1006   "recode as shifts and adds"
1007   (let ((y (continuation-value y)))
1008     (multiple-value-bind (result adds shifts)
1009         (ub32-strength-reduce-constant-multiply 'x y)
1010       (cond
1011        ((typep y '(signed-byte 16))
1012         ;; a mulli instruction has a latency of 5.
1013         (when (> (+ adds shifts) 4)
1014           (give-up-ir1-transform)))
1015        (t
1016         ;; a mullw instruction also has a latency of 5, plus two
1017         ;; instructions (in general) to load the immediate into a
1018         ;; register.
1019         (when (> (+ adds shifts) 6)
1020           (give-up-ir1-transform))))
1021       (or result 0))))