5a20c341a29a7a8215c2f9fc0e6cdc4415a951ba
[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-binop30-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 (signed-byte 30) (not (integer 0 0)))))
97   (:results (r :scs (any-reg)))
98   (:result-types tagged-num)
99   (:note "inline fixnum arithmetic"))
100
101 (define-vop (fast-fixnum-logop-c fast-safe-arith-op)
102   (:args (x :target r :scs (any-reg zero)))
103   (:info y)
104   (:arg-types tagged-num
105               (:constant (and (unsigned-byte 14) (not (integer 0 0)))))
106   (:results (r :scs (any-reg)))
107   (:result-types tagged-num)
108   (:note "inline fixnum logical op"))
109
110 (define-vop (fast-fixnum-logop30-c fast-safe-arith-op)
111   (:args (x :target r :scs (any-reg zero)))
112   (:info y)
113   (:arg-types tagged-num
114               (:constant (and (unsigned-byte 16) (not (integer 0 0)))))
115   (:results (r :scs (any-reg)))
116   (:result-types tagged-num)
117   (:note "inline fixnum logical op"))
118
119 (define-vop (fast-unsigned-binop-c fast-safe-arith-op)
120   (:args (x :target r :scs (unsigned-reg zero)))
121   (:info y)
122   (:arg-types unsigned-num
123               (:constant (and (signed-byte 16) (not (integer 0 0)))))
124   (:results (r :scs (unsigned-reg)))
125   (:result-types unsigned-num)
126   (:note "inline (unsigned-byte 32) arithmetic"))
127
128 (define-vop (fast-unsigned-binop32-c fast-safe-arith-op)
129   (:args (x :target r :scs (unsigned-reg zero)))
130   (:info y)
131   (:arg-types unsigned-num
132               (:constant (and (unsigned-byte 32) (not (integer 0 0)))))
133   (:results (r :scs (unsigned-reg)))
134   (:result-types unsigned-num)
135   (:note "inline (unsigned-byte 32) arithmetic"))
136
137 (define-vop (fast-signed-binop32-c fast-safe-arith-op)
138   (:args (x :target r :scs (signed-reg zero)))
139   (:info y)
140   (:arg-types signed-num
141               (:constant (and (signed-byte 32) (not (integer 0 0)))))
142   (:results (r :scs (signed-reg)))
143   (:result-types signed-num)
144   (:note "inline (signed-byte 32) arithmetic"))
145
146 (define-vop (fast-unsigned-logop-c fast-safe-arith-op)
147   (:args (x :target r :scs (unsigned-reg zero)))
148   (:info y)
149   (:arg-types unsigned-num
150               (:constant (and (unsigned-byte 16) (not (integer 0 0)))))
151   (:results (r :scs (unsigned-reg)))
152   (:result-types unsigned-num)
153   (:note "inline (unsigned-byte 32) logical op"))
154
155 (define-vop (fast-unsigned-logop32-c fast-safe-arith-op)
156   (:args (x :target r :scs (unsigned-reg zero)))
157   (:info y)
158   (:arg-types unsigned-num
159               (:constant (and (unsigned-byte 32) (not (integer 0 0)))))
160   (:results (r :scs (unsigned-reg)))
161   (:result-types unsigned-num)
162   (:note "inline (unsigned-byte 32) logical op"))
163
164 (define-vop (fast-signed-logop32-c fast-safe-arith-op)
165   (:args (x :target r :scs (signed-reg zero)))
166   (:info y)
167   (:arg-types signed-num
168               (:constant (and (unsigned-byte 32) (not (integer 0 0)))))
169   (:results (r :scs (signed-reg)))
170   (:result-types signed-num)
171   (:note "inline (signed-byte 32) logical op"))
172
173 (define-vop (fast-signed-binop-c fast-safe-arith-op)
174   (:args (x :target r :scs (signed-reg zero)))
175   (:info y)
176   (:arg-types signed-num
177               (:constant (and (signed-byte 16) (not (integer 0 0)))))
178   (:results (r :scs (signed-reg)))
179   (:result-types signed-num)
180   (:note "inline (signed-byte 32) arithmetic"))
181
182 (define-vop (fast-signed-logop-c fast-safe-arith-op)
183   (:args (x :target r :scs (signed-reg zero)))
184   (:info y)
185   (:arg-types signed-num
186               (:constant (and (unsigned-byte 16) (not (integer 0 0)))))
187   (:results (r :scs (signed-reg)))
188   (:result-types signed-num)
189   (:note "inline (signed-byte 32) logical op"))
190
191 (eval-when (:compile-toplevel :load-toplevel :execute)
192
193 (defmacro !define-var-binop (translate untagged-penalty op
194                              &optional arg-swap restore-fixnum-mask)
195   `(progn
196      (define-vop (,(symbolicate "FAST-" translate "/FIXNUM=>FIXNUM")
197                   fast-fixnum-binop)
198        ,@(when restore-fixnum-mask
199            `((:temporary (:sc non-descriptor-reg) temp)))
200        (:translate ,translate)
201        (:generator 2
202          ,(if arg-swap
203              `(inst ,op ,(if restore-fixnum-mask 'temp 'r) y x)
204              `(inst ,op ,(if restore-fixnum-mask 'temp 'r) x y))
205          ;; FIXME: remind me what convention we used for 64bitizing
206          ;; stuff?  -- CSR, 2003-08-27
207          ,@(when restore-fixnum-mask
208              `((inst clrrwi r temp (1- n-lowtag-bits))))))
209      (define-vop (,(symbolicate "FAST-" translate "/SIGNED=>SIGNED")
210                   fast-signed-binop)
211        (:translate ,translate)
212        (:generator ,(1+ untagged-penalty)
213          ,(if arg-swap
214              `(inst ,op r y x)
215              `(inst ,op r x y))))
216      (define-vop (,(symbolicate "FAST-" translate "/UNSIGNED=>UNSIGNED")
217                   fast-unsigned-binop)
218        (:translate ,translate)
219        (:generator ,(1+ untagged-penalty)
220          ,(if arg-swap
221              `(inst ,op r y x)
222              `(inst ,op r x y))))))
223
224 ;;; FIXME: the code has really only been checked for adds; we could do
225 ;;; subtracts, too, but my brain is not up to the task of figuring out
226 ;;; signs and borrows.
227 (defmacro !define-const-binop (translate untagged-penalty op &optional (shifted-op nil))
228   `(progn
229      (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
230                   ,(if shifted-op
231                        'fast-fixnum-binop30-c
232                        'fast-fixnum-binop-c))
233        (:translate ,translate)
234        ,@(when shifted-op
235           `((:temporary (:sc any-reg :target r) temp)))
236        (:generator 1
237         ,(if shifted-op
238              `(let* ((y (fixnumize y))
239                      (high-half (ldb (byte 16 16) y))
240                      (low-half (ldb (byte 16 0) y)))
241                ;; Compare %LR in insts.lisp.
242                (cond
243                  ((and (logbitp 15 low-half) (= high-half #xffff))
244                   ;; Let sign-extension do the work for us, but make sure
245                   ;; to turn LOW-HALF into a signed integer.
246                   (inst ,op r x (dpb low-half (byte 16 0) -1)))
247                  ((and (not (logbitp 15 low-half)) (zerop high-half))
248                   (inst ,op r x low-half))
249                  ((zerop low-half)
250                   (inst ,shifted-op r x (if (logbitp 15 high-half)
251                                             (dpb high-half (byte 16 0) -1)
252                                             high-half)))
253                  (t
254                   ;; Check to see whether compensating for the sign bit
255                   ;; of LOW-HALF is necessary.
256                   (let ((high-half (let ((top (if (logbitp 15 low-half)
257                                                   (ldb (byte 16 0)
258                                                        (1+ high-half))
259                                                   high-half)))
260                                      (if (logbitp 15 top)
261                                          (dpb top (byte 16 0) -1)
262                                          top))))
263                     (inst ,shifted-op temp x high-half)
264                     (inst ,op r temp low-half)))))
265              `(inst ,op r x (fixnumize y)))))
266      (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
267                   ,(if shifted-op
268                        'fast-signed-binop32-c
269                        'fast-signed-binop-c))
270        (:translate ,translate)
271        ,@(when shifted-op
272           `((:temporary (:sc non-descriptor-reg :target r) temp)))
273        (:generator ,untagged-penalty
274         ,(if shifted-op
275              `(let ((high-half (ldb (byte 16 16) y))
276                     (low-half (ldb (byte 16 0) y)))
277                ;; Compare %LR in insts.lisp.
278                (cond
279                  ((and (logbitp 15 low-half) (= high-half #xffff))
280                   ;; Let sign-extension do the work for us, but make sure
281                   ;; to turn LOW-HALF into a signed integer.
282                   (inst ,op r x (dpb low-half (byte 16 0) -1)))
283                  ((and (not (logbitp 15 low-half)) (zerop high-half))
284                   (inst ,op r x low-half))
285                  ((zerop low-half)
286                   (inst ,shifted-op r x (if (logbitp 15 high-half)
287                                             (dpb high-half (byte 16 0) -1)
288                                             high-half)))
289                  (t
290                   ;; Check to see whether compensating for the sign bit
291                   ;; of LOW-HALF is necessary.
292                   (let ((high-half (let ((top (if (logbitp 15 low-half)
293                                                   (ldb (byte 16 0)
294                                                        (1+ high-half))
295                                                   high-half)))
296                                      (if (logbitp 15 top)
297                                          (dpb top (byte 16 0) -1)
298                                          top))))
299                     (inst ,shifted-op temp x high-half)
300                     (inst ,op r temp low-half)))))
301              `(inst ,op r x y))))
302      (define-vop (,(symbolicate 'fast- translate '-c/unsigned=>unsigned)
303                   ,(if shifted-op
304                        'fast-unsigned-binop32-c
305                        'fast-unsigned-binop-c))
306        (:translate ,translate)
307        ,@(when shifted-op
308           `((:temporary (:sc non-descriptor-reg :target r) temp)))
309        (:generator ,untagged-penalty
310         ,(if shifted-op
311              `(let ((high-half (ldb (byte 16 16) y))
312                     (low-half (ldb (byte 16 0) y)))
313                ;; Compare %LR in insts.lisp.
314                (cond
315                  ((and (logbitp 15 low-half) (= high-half #xffff))
316                   ;; Let sign-extension do the work for us, but make sure
317                   ;; to turn LOW-HALF into a signed integer.
318                   (inst ,op r x (dpb low-half (byte 16 0) -1)))
319                  ((and (not (logbitp 15 low-half)) (zerop high-half))
320                   (inst ,op r x low-half))
321                  ((zerop low-half)
322                   (inst ,shifted-op r x (if (logbitp 15 high-half)
323                                             (dpb high-half (byte 16 0) -1)
324                                             high-half)))
325                  (t
326                   ;; Check to see whether compensating for the sign bit
327                   ;; of LOW-HALF is necessary.
328                   (let ((high-half (let ((top (if (logbitp 15 low-half)
329                                                   (ldb (byte 16 0)
330                                                        (1+ high-half))
331                                                   high-half)))
332                                      (if (logbitp 15 top)
333                                          (dpb top (byte 16 0) -1)
334                                          top))))
335                     (inst ,shifted-op temp x high-half)
336                     (inst ,op r temp low-half)))))
337              `(inst ,op r x y))))))
338
339 ;;; For logical operations, we don't have to worry about signed bit
340 ;;; propagation from the lower half of a 32-bit operand.
341 (defmacro !define-const-logop (translate untagged-penalty op &optional (shifted-op nil))
342   `(progn
343      (define-vop (,(symbolicate 'fast- translate '-c/fixnum=>fixnum)
344                   ,(if shifted-op
345                        'fast-fixnum-logop30-c
346                        'fast-fixnum-logop-c))
347        (:translate ,translate)
348        ,@(when shifted-op
349           `((:temporary (:sc any-reg :target r) temp)))
350        (:generator 1
351         ,(if shifted-op
352              `(let* ((y (fixnumize y))
353                      (high-half (ldb (byte 16 16) y))
354                      (low-half (ldb (byte 16 0) y)))
355                (cond
356                  ((zerop high-half) (inst ,op r x low-half))
357                  ((zerop low-half) (inst ,shifted-op r x high-half))
358                  (t
359                   (inst ,shifted-op temp x high-half)
360                   (inst ,op r temp low-half))))
361              `(inst ,op r x (fixnumize y)))))
362      (define-vop (,(symbolicate 'fast- translate '-c/signed=>signed)
363                   ,(if shifted-op
364                        'fast-signed-logop32-c
365                        'fast-signed-logop-c))
366        (:translate ,translate)
367        ,@(when shifted-op
368           `((:temporary (:sc non-descriptor-reg :target r) temp)))
369        (:generator ,untagged-penalty
370         ,(if shifted-op
371              `(let ((high-half (ldb (byte 16 16) y))
372                     (low-half (ldb (byte 16 0) y)))
373                (cond
374                  ((zerop high-half) (inst ,op r x low-half))
375                  ((zerop low-half) (inst ,shifted-op r x high-half))
376                  (t
377                   (inst ,shifted-op temp x high-half)
378                   (inst ,op r temp low-half))))
379              `(inst ,op r x y))))
380      (define-vop (,(symbolicate 'fast- translate '-c/unsigned=>unsigned)
381                   ,(if shifted-op
382                        'fast-unsigned-logop32-c
383                        'fast-unsigned-logop-c))
384        (:translate ,translate)
385        ,@(when shifted-op
386           `((:temporary (:sc non-descriptor-reg :target r) temp)))
387        (:generator ,untagged-penalty
388         ,(if shifted-op
389              `(let ((high-half (ldb (byte 16 16) y))
390                     (low-half (ldb (byte 16 0) y)))
391                (cond
392                  ((zerop high-half) (inst ,op r x low-half))
393                  ((zerop low-half) (inst ,shifted-op r x high-half))
394                  (t
395                   (inst ,shifted-op temp x high-half)
396                   (inst ,op r temp low-half))))
397              `(inst ,op r x y))))))
398
399 ); eval-when
400
401 (!define-var-binop + 4 add)
402 (!define-var-binop - 4 sub)
403 (!define-var-binop logand 2 and)
404 (!define-var-binop logandc1 2 andc t)
405 (!define-var-binop logandc2 2 andc)
406 (!define-var-binop logior 2 or)
407 (!define-var-binop logorc1 2 orc t t)
408 (!define-var-binop logorc2 2 orc nil t)
409 (!define-var-binop logxor 2 xor)
410 (!define-var-binop logeqv 2 eqv nil t)
411 (!define-var-binop lognand 2 nand nil t)
412 (!define-var-binop lognor 2 nor nil t)
413
414 (!define-const-binop + 4 addi addis)
415 (!define-const-binop - 4 subi)
416 ;;; Implementing a 32-bit immediate version of LOGAND wouldn't be any
417 ;;; better than loading the 32-bit constant via LR and then performing
418 ;;; an /AND/.  So don't bother.  (It would be better in some cases, such
419 ;;; as when one half of the word is zeros--we save a register--but we
420 ;;; would have specified one temporary register in the VOP, so we lose
421 ;;; any possible advantage.)
422 (!define-const-logop logand 2 andi.)
423 (!define-const-logop logior 2 ori oris)
424 (!define-const-logop logxor 2 xori xoris)
425
426
427 ;;; Special case fixnum + and - that trap on overflow.  Useful when we
428 ;;; don't know that the output type is a fixnum.
429 ;;;
430 (define-vop (+/fixnum fast-+/fixnum=>fixnum)
431   (:policy :safe)
432   (:results (r :scs (any-reg descriptor-reg)))
433   (:result-types tagged-num)
434   (:note "safe inline fixnum arithmetic")
435   (:generator 4
436     (let* ((no-overflow (gen-label)))
437       (inst mtxer zero-tn)
438       (inst addo. r x y)
439       (inst bns no-overflow)
440       (inst unimp (logior (ash (reg-tn-encoding r) 5)
441                           fixnum-additive-overflow-trap))
442       (emit-label no-overflow))))
443
444 (define-vop (-/fixnum fast--/fixnum=>fixnum)
445   (:policy :safe)
446   (:results (r :scs (any-reg descriptor-reg)))
447   (:result-types tagged-num)
448   (:note "safe inline fixnum arithmetic")
449   (:generator 4
450     (let* ((no-overflow (gen-label)))
451       (inst mtxer zero-tn)
452       (inst subo. r x y)
453       (inst bns no-overflow)
454       (inst unimp (logior (ash (reg-tn-encoding r) 5)
455                           fixnum-additive-overflow-trap))
456       (emit-label no-overflow))))
457
458 (define-vop (fast-*/fixnum=>fixnum fast-fixnum-binop)
459   (:temporary (:scs (non-descriptor-reg)) temp)
460   (:translate *)
461   (:generator 2
462     (inst srawi temp y 2)
463     (inst mullw r x temp)))
464
465 (define-vop (fast-*-c/fixnum=>fixnum fast-fixnum-binop-c)
466   (:translate *)
467   (:arg-types tagged-num
468               (:constant (and (signed-byte 16) (not (integer 0 0)))))
469   (:generator 1
470     (inst mulli r x y)))
471
472 (define-vop (fast-*-bigc/fixnum=>fixnum fast-fixnum-binop-c)
473   (:translate *)
474   (:arg-types tagged-num
475               (:constant (and fixnum (not (signed-byte 16)))))
476   (:temporary (:scs (non-descriptor-reg)) temp)
477   (:generator 1
478     (inst lr temp y)
479     (inst mullw r x temp)))
480
481 (define-vop (fast-*/signed=>signed fast-signed-binop)
482   (:translate *)
483   (:generator 4
484     (inst mullw r x y)))
485
486 (define-vop (fast-*-c/signed=>signed fast-signed-binop-c)
487   (:translate *)
488   (:generator 3
489     (inst mulli r x y)))
490
491 (define-vop (fast-*/unsigned=>unsigned fast-unsigned-binop)
492   (:translate *)
493   (:generator 4
494     (inst mullw r x y)))
495
496 (define-vop (fast-*-c/unsigned=>unsigned fast-unsigned-binop-c)
497   (:translate *)
498   (:generator 3
499     (inst mulli r x y)))
500 \f
501 ;;; Shifting
502
503 (macrolet ((def (name sc-type type result-type cost)
504              `(define-vop (,name)
505                 (:note "inline ASH")
506                 (:translate ash)
507                 (:args (number :scs (,sc-type))
508                        (amount :scs (signed-reg unsigned-reg immediate)))
509                 (:arg-types ,type positive-fixnum)
510                 (:results (result :scs (,result-type)))
511                 (:result-types ,type)
512                 (:policy :fast-safe)
513                 (:generator ,cost
514                    (sc-case amount
515                      ((signed-reg unsigned-reg)
516                       (inst slw result number amount))
517                      (immediate
518                       (let ((amount (tn-value amount)))
519                         (aver (> amount 0))
520                         (inst slwi result number amount))))))))
521   ;; FIXME: There's the opportunity for a sneaky optimization here, I
522   ;; think: a FAST-ASH-LEFT-C/FIXNUM=>SIGNED vop.  -- CSR, 2003-09-03
523   (def fast-ash-left/fixnum=>fixnum any-reg tagged-num any-reg 2)
524   (def fast-ash-left/signed=>signed signed-reg signed-num signed-reg 3)
525   (def fast-ash-left/unsigned=>unsigned unsigned-reg unsigned-num unsigned-reg 3))
526
527 (define-vop (fast-ash/unsigned=>unsigned)
528   (:note "inline ASH")
529   (:args (number :scs (unsigned-reg) :to :save)
530          (amount :scs (signed-reg)))
531   (:arg-types (:or unsigned-num) signed-num)
532   (:results (result :scs (unsigned-reg)))
533   (:result-types unsigned-num)
534   (:translate ash)
535   (:policy :fast-safe)
536   (:temporary (:sc non-descriptor-reg) ndesc)
537   (:generator 5
538     (let ((positive (gen-label))
539           (done (gen-label)))
540       (inst cmpwi amount 0)
541       (inst neg ndesc amount)
542       (inst bge positive)
543       (inst cmpwi ndesc 31)
544       (inst srw result number ndesc)
545       (inst ble done)
546       (move result zero-tn)
547       (inst b done)
548
549       (emit-label positive)
550       ;; The result-type assures us that this shift will not overflow.
551       (inst slw result number amount)
552
553       (emit-label done))))
554
555 (define-vop (fast-ash-c/unsigned=>unsigned)
556   (:note "inline constant ASH")
557   (:args (number :scs (unsigned-reg)))
558   (:info amount)
559   (:arg-types unsigned-num (:constant integer))
560   (:results (result :scs (unsigned-reg)))
561   (:result-types unsigned-num)
562   (:translate ash)
563   (:policy :fast-safe)
564   (:generator 4
565     (cond
566       ((and (minusp amount) (< amount -31)) (move result zero-tn))
567       ((minusp amount) (inst srwi result number (- amount)))
568       ;; possible because this is used in the modular version too
569       ((> amount 31) (move result zero-tn))
570       (t (inst slwi result number amount)))))
571
572 (define-vop (fast-ash/signed=>signed)
573   (:note "inline ASH")
574   (:args (number :scs (signed-reg) :to :save)
575          (amount :scs (signed-reg immediate)))
576   (:arg-types (:or signed-num) signed-num)
577   (:results (result :scs (signed-reg)))
578   (:result-types (:or signed-num))
579   (:translate ash)
580   (:policy :fast-safe)
581   (:temporary (:sc non-descriptor-reg) ndesc)
582   (:generator 3
583     (sc-case amount
584       (signed-reg
585        (let ((positive (gen-label))
586              (done (gen-label)))
587          (inst cmpwi amount 0)
588          (inst neg ndesc amount)
589          (inst bge positive)
590          (inst cmpwi ndesc 31)
591          (inst sraw result number ndesc)
592          (inst ble done)
593          (inst srawi result number 31)
594          (inst b done)
595
596          (emit-label positive)
597          ;; The result-type assures us that this shift will not overflow.
598          (inst slw result number amount)
599
600          (emit-label done)))
601
602       (immediate
603        (let ((amount (tn-value amount)))
604          (if (minusp amount)
605              (let ((amount (min 31 (- amount))))
606                (inst srawi result number amount))
607              (inst slwi result number amount)))))))
608
609 (define-vop (signed-byte-32-len)
610   (:translate integer-length)
611   (:note "inline (signed-byte 32) integer-length")
612   (:policy :fast-safe)
613   (:args (arg :scs (signed-reg)))
614   (:arg-types signed-num)
615   (:results (res :scs (unsigned-reg) :from :load))
616   (:result-types unsigned-num)
617   (:generator 6
618     ; (integer-length arg) = (- 32 (cntlz (if (>= arg 0) arg (lognot arg))))
619     (let ((nonneg (gen-label)))
620       (inst cntlzw. res arg)
621       (inst bne nonneg)
622       (inst not res arg)
623       (inst cntlzw res res)
624       (emit-label nonneg)
625       (inst subfic res res 32))))
626
627 (define-vop (unsigned-byte-32-len)
628   (:translate integer-length)
629   (:note "inline (unsigned-byte 32) integer-length")
630   (:policy :fast-safe)
631   (:args (arg :scs (unsigned-reg)))
632   (:arg-types unsigned-num)
633   (:results (res :scs (unsigned-reg)))
634   (:result-types unsigned-num)
635   (:generator 4
636     (inst cntlzw res arg)
637     (inst subfic res res 32)))
638
639 (define-vop (unsigned-byte-32-count)
640   (:translate logcount)
641   (:note "inline (unsigned-byte 32) logcount")
642   (:policy :fast-safe)
643   (:args (arg :scs (unsigned-reg) :target shift))
644   (:arg-types unsigned-num)
645   (:results (res :scs (any-reg)))
646   (:result-types positive-fixnum)
647   (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) shift temp)
648   (:generator 30
649     (let ((loop (gen-label))
650           (done (gen-label)))
651       (inst add. shift zero-tn arg)
652       (move res zero-tn)
653       (inst beq done)
654
655       (emit-label loop)
656       (inst subi temp shift 1)
657       (inst and. shift shift temp)
658       (inst addi res res (fixnumize 1))
659       (inst bne loop)
660
661       (emit-label done))))
662
663 \f
664 ;;;; Modular functions:
665 (define-modular-fun lognot-mod32 (x) lognot :unsigned 32)
666 (define-vop (lognot-mod32/unsigned=>unsigned)
667   (:translate lognot-mod32)
668   (:args (x :scs (unsigned-reg)))
669   (:arg-types unsigned-num)
670   (:results (res :scs (unsigned-reg)))
671   (:result-types unsigned-num)
672   (:policy :fast-safe)
673   (:generator 1
674     (inst not res x)))
675
676 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
677              fast-ash-c/unsigned=>unsigned)
678   (:translate ash-left-mod32))
679
680 (define-vop (fast-ash-left-mod32/unsigned=>unsigned
681              fast-ash-left/unsigned=>unsigned))
682 (deftransform ash-left-mod32 ((integer count)
683                               ((unsigned-byte 32) (unsigned-byte 5)))
684   (when (sb!c::constant-lvar-p count)
685     (sb!c::give-up-ir1-transform))
686   '(%primitive fast-ash-left-mod32/unsigned=>unsigned integer count))
687
688 (macrolet
689     ((define-modular-backend (fun &optional constantp)
690        (let ((mfun-name (symbolicate fun '-mod32))
691              (modvop (symbolicate 'fast- fun '-mod32/unsigned=>unsigned))
692              (modcvop (symbolicate 'fast- fun 'mod32-c/unsigned=>unsigned))
693              (vop (symbolicate 'fast- fun '/unsigned=>unsigned))
694              (cvop (symbolicate 'fast- fun '-c/unsigned=>unsigned)))
695          `(progn
696             (define-modular-fun ,mfun-name (x y) ,fun :unsigned 32)
697             (define-vop (,modvop ,vop)
698               (:translate ,mfun-name))
699             ,@(when constantp
700                 `((define-vop (,modcvop ,cvop)
701                     (:translate ,mfun-name))))))))
702   (define-modular-backend + t)
703   (define-modular-backend - t)
704   (define-modular-backend * t)
705   (define-modular-backend logxor t)
706   (define-modular-backend logeqv)
707   (define-modular-backend lognand)
708   (define-modular-backend lognor)
709   (define-modular-backend logandc1)
710   (define-modular-backend logandc2)
711   (define-modular-backend logorc1)
712   (define-modular-backend logorc2))
713 \f
714 ;;;; Binary conditional VOPs:
715
716 (define-vop (fast-conditional)
717   (:conditional)
718   (:info target not-p)
719   (:effects)
720   (:affected)
721   (:policy :fast-safe))
722
723 (define-vop (fast-conditional/fixnum fast-conditional)
724   (:args (x :scs (any-reg zero))
725          (y :scs (any-reg zero)))
726   (:arg-types tagged-num tagged-num)
727   (:note "inline fixnum comparison"))
728
729 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
730   (:args (x :scs (any-reg zero)))
731   (:arg-types tagged-num (:constant (signed-byte 14)))
732   (:info target not-p y))
733
734 (define-vop (fast-conditional/signed fast-conditional)
735   (:args (x :scs (signed-reg zero))
736          (y :scs (signed-reg zero)))
737   (:arg-types signed-num signed-num)
738   (:note "inline (signed-byte 32) comparison"))
739
740 (define-vop (fast-conditional-c/signed fast-conditional/signed)
741   (:args (x :scs (signed-reg zero)))
742   (:arg-types signed-num (:constant (signed-byte 16)))
743   (:info target not-p y))
744
745 (define-vop (fast-conditional/unsigned fast-conditional)
746   (:args (x :scs (unsigned-reg zero))
747          (y :scs (unsigned-reg zero)))
748   (:arg-types unsigned-num unsigned-num)
749   (:note "inline (unsigned-byte 32) comparison"))
750
751 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
752   (:args (x :scs (unsigned-reg zero)))
753   (:arg-types unsigned-num (:constant (unsigned-byte 16)))
754   (:info target not-p y))
755
756 (define-vop (fast-if-</fixnum fast-conditional/fixnum)
757   (:translate <)
758   (:generator 4
759     (inst cmpw x y)
760     (inst b? (if not-p :ge :lt) target)))
761
762 (define-vop (fast-if-<-c/fixnum fast-conditional-c/fixnum)
763   (:translate <)
764   (:generator 3
765     (inst cmpwi x (fixnumize y))
766     (inst b? (if not-p :ge :lt) target)))
767
768 (define-vop (fast-if-</signed fast-conditional/signed)
769   (:translate <)
770   (:generator 6
771     (inst cmpw x y)
772     (inst b? (if not-p :ge :lt) target)))
773
774 (define-vop (fast-if-<-c/signed fast-conditional-c/signed)
775   (:translate <)
776   (:generator 5
777     (inst cmpwi x y)
778     (inst b? (if not-p :ge :lt) target)))
779
780 (define-vop (fast-if-</unsigned fast-conditional/unsigned)
781   (:translate <)
782   (:generator 6
783     (inst cmplw x y)
784     (inst b? (if not-p :ge :lt) target)))
785
786 (define-vop (fast-if-<-c/unsigned fast-conditional-c/unsigned)
787   (:translate <)
788   (:generator 5
789     (inst cmplwi x y)
790     (inst b? (if not-p :ge :lt) target)))
791
792 (define-vop (fast-if->/fixnum fast-conditional/fixnum)
793   (:translate >)
794   (:generator 4
795     (inst cmpw x y)
796     (inst b? (if not-p :le :gt) target)))
797
798 (define-vop (fast-if->-c/fixnum fast-conditional-c/fixnum)
799   (:translate >)
800   (:generator 3
801     (inst cmpwi x (fixnumize y))
802     (inst b? (if not-p :le :gt) target)))
803
804 (define-vop (fast-if->/signed fast-conditional/signed)
805   (:translate >)
806   (:generator 6
807     (inst cmpw x y)
808     (inst b? (if not-p :le :gt) target)))
809
810 (define-vop (fast-if->-c/signed fast-conditional-c/signed)
811   (:translate >)
812   (:generator 5
813     (inst cmpwi x y)
814     (inst b? (if not-p :le :gt) target)))
815
816 (define-vop (fast-if->/unsigned fast-conditional/unsigned)
817   (:translate >)
818   (:generator 6
819     (inst cmplw x y)
820     (inst b? (if not-p :le :gt) target)))
821
822 (define-vop (fast-if->-c/unsigned fast-conditional-c/unsigned)
823   (:translate >)
824   (:generator 5
825     (inst cmplwi x y)
826     (inst b? (if not-p :le :gt) target)))
827
828 (define-vop (fast-if-eql/signed fast-conditional/signed)
829   (:translate eql)
830   (:generator 6
831     (inst cmpw x y)
832     (inst b? (if not-p :ne :eq) target)))
833
834 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
835   (:translate eql)
836   (:generator 5
837     (inst cmpwi x y)
838     (inst b? (if not-p :ne :eq) target)))
839
840 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
841   (:translate eql)
842   (:generator 6
843     (inst cmplw x y)
844     (inst b? (if not-p :ne :eq) target)))
845
846 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
847   (:translate eql)
848   (:generator 5
849     (inst cmplwi x y)
850     (inst b? (if not-p :ne :eq) target)))
851
852
853 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
854 ;;; known fixnum.
855
856 ;;; These versions specify a fixnum restriction on their first arg.  We have
857 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
858 ;;; the first arg and a higher cost.  The reason for doing this is to prevent
859 ;;; fixnum specific operations from being used on word integers, spuriously
860 ;;; consing the argument.
861 ;;;
862
863 (define-vop (fast-eql/fixnum fast-conditional)
864   (:args (x :scs (any-reg descriptor-reg zero))
865          (y :scs (any-reg zero)))
866   (:arg-types tagged-num tagged-num)
867   (:note "inline fixnum comparison")
868   (:translate eql)
869   (:generator 4
870     (inst cmpw x y)
871     (inst b? (if not-p :ne :eq) target)))
872 ;;;
873 (define-vop (generic-eql/fixnum fast-eql/fixnum)
874   (:arg-types * tagged-num)
875   (:variant-cost 7))
876
877 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
878   (:args (x :scs (any-reg descriptor-reg zero)))
879   (:arg-types tagged-num (:constant (signed-byte 14)))
880   (:info target not-p y)
881   (:translate eql)
882   (:generator 2
883     (inst cmpwi x (fixnumize y))
884     (inst b? (if not-p :ne :eq) target)))
885 ;;;
886 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
887   (:arg-types * (:constant (signed-byte 11)))
888   (:variant-cost 6))
889
890 \f
891 ;;;; 32-bit logical operations
892
893 (define-vop (merge-bits)
894   (:translate merge-bits)
895   (:args (shift :scs (signed-reg unsigned-reg))
896          (prev :scs (unsigned-reg))
897          (next :scs (unsigned-reg)))
898   (:arg-types tagged-num unsigned-num unsigned-num)
899   (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
900   (:temporary (:scs (unsigned-reg) :to (:result 0) :target result) res)
901   (:results (result :scs (unsigned-reg)))
902   (:result-types unsigned-num)
903   (:policy :fast-safe)
904   (:generator 4
905     (let ((done (gen-label)))
906       (inst cmpwi shift 0)
907       (inst beq done)
908       (inst srw res next shift)
909       (inst sub temp zero-tn shift)
910       (inst slw temp prev temp)
911       (inst or res res temp)
912       (emit-label done)
913       (move result res))))
914
915 (define-vop (shift-towards-someplace)
916   (:policy :fast-safe)
917   (:args (num :scs (unsigned-reg))
918          (amount :scs (signed-reg)))
919   (:arg-types unsigned-num tagged-num)
920   (:results (r :scs (unsigned-reg)))
921   (:result-types unsigned-num))
922
923 (define-vop (shift-towards-start shift-towards-someplace)
924   (:translate shift-towards-start)
925   (:note "shift-towards-start")
926   (:generator 1
927     (inst rlwinm amount amount 0 27 31)
928     (inst slw r num amount)))
929
930 (define-vop (shift-towards-end shift-towards-someplace)
931   (:translate shift-towards-end)
932   (:note "shift-towards-end")
933   (:generator 1
934     (inst rlwinm amount amount 0 27 31)
935     (inst srw r num amount)))
936 \f
937 ;;;; Bignum stuff.
938
939 (define-vop (bignum-length get-header-data)
940   (:translate sb!bignum:%bignum-length)
941   (:policy :fast-safe))
942
943 (define-vop (bignum-set-length set-header-data)
944   (:translate sb!bignum:%bignum-set-length)
945   (:policy :fast-safe))
946
947 (define-vop (bignum-ref word-index-ref)
948   (:variant bignum-digits-offset other-pointer-lowtag)
949   (:translate sb!bignum:%bignum-ref)
950   (:results (value :scs (unsigned-reg)))
951   (:result-types unsigned-num))
952
953 (define-vop (bignum-set word-index-set)
954   (:variant bignum-digits-offset other-pointer-lowtag)
955   (:translate sb!bignum:%bignum-set)
956   (:args (object :scs (descriptor-reg))
957          (index :scs (any-reg immediate zero))
958          (value :scs (unsigned-reg)))
959   (:arg-types t positive-fixnum unsigned-num)
960   (:results (result :scs (unsigned-reg)))
961   (:result-types unsigned-num))
962
963 (define-vop (digit-0-or-plus)
964   (:translate sb!bignum:%digit-0-or-plusp)
965   (:policy :fast-safe)
966   (:args (digit :scs (unsigned-reg)))
967   (:arg-types unsigned-num)
968   (:results (result :scs (descriptor-reg)))
969   (:generator 3
970     (let ((done (gen-label)))
971       (inst cmpwi digit 0)
972       (move result null-tn)
973       (inst blt done)
974       (load-symbol result t)
975       (emit-label done))))
976
977 (define-vop (add-w/carry)
978   (:translate sb!bignum:%add-with-carry)
979   (:policy :fast-safe)
980   (:args (a :scs (unsigned-reg))
981          (b :scs (unsigned-reg))
982          (c :scs (any-reg)))
983   (:arg-types unsigned-num unsigned-num positive-fixnum)
984   (:temporary (:scs (unsigned-reg)) temp)
985   (:results (result :scs (unsigned-reg))
986             (carry :scs (unsigned-reg)))
987   (:result-types unsigned-num positive-fixnum)
988   (:generator 3
989     (inst addic temp c -1)
990     (inst adde result a b)
991     (inst addze carry zero-tn)))
992
993 (define-vop (sub-w/borrow)
994   (:translate sb!bignum:%subtract-with-borrow)
995   (:policy :fast-safe)
996   (:args (a :scs (unsigned-reg))
997          (b :scs (unsigned-reg))
998          (c :scs (any-reg)))
999   (:arg-types unsigned-num unsigned-num positive-fixnum)
1000   (:temporary (:scs (unsigned-reg)) temp)
1001   (:results (result :scs (unsigned-reg))
1002             (borrow :scs (unsigned-reg)))
1003   (:result-types unsigned-num positive-fixnum)
1004   (:generator 4
1005     (inst addic temp c -1)
1006     (inst sube result a b)
1007     (inst addze borrow zero-tn)))
1008
1009 (define-vop (bignum-mult-and-add-3-arg)
1010   (:translate sb!bignum:%multiply-and-add)
1011   (:policy :fast-safe)
1012   (:args (x :scs (unsigned-reg))
1013          (y :scs (unsigned-reg))
1014          (carry-in :scs (unsigned-reg) :to (:eval 1)))
1015   (:arg-types unsigned-num unsigned-num unsigned-num)
1016   (:temporary (:scs (unsigned-reg) :to (:result 0) :target hi) hi-temp)
1017   (:temporary (:scs (unsigned-reg) :from (:eval 0) :to (:result 1)
1018                     :target lo) lo-temp)
1019   (:results (hi :scs (unsigned-reg))
1020             (lo :scs (unsigned-reg)))
1021   (:result-types unsigned-num unsigned-num)
1022   (:generator 40
1023     (inst mulhwu hi-temp x y)
1024     (inst mullw lo-temp x y)
1025     (inst addc lo lo-temp carry-in)
1026     (inst addze hi hi-temp)))
1027
1028 (define-vop (bignum-mult-and-add-4-arg)
1029   (:translate sb!bignum:%multiply-and-add)
1030   (:policy :fast-safe)
1031   (:args (x :scs (unsigned-reg))
1032          (y :scs (unsigned-reg))
1033          (prev :scs (unsigned-reg) :to (:eval 1))
1034          (carry-in :scs (unsigned-reg) :to (:eval 1)))
1035   (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1036   (:temporary (:scs (unsigned-reg) :to (:result 0) :target hi) hi-temp)
1037   (:temporary (:scs (unsigned-reg) :from (:eval 0) :to (:result 1)
1038                     :target lo) lo-temp)
1039   (:results (hi :scs (unsigned-reg))
1040             (lo :scs (unsigned-reg)))
1041   (:result-types unsigned-num unsigned-num)
1042   (:generator 40
1043     (inst mulhwu hi-temp x y)
1044     (inst mullw lo-temp x y)
1045     (inst addc lo-temp lo-temp carry-in)
1046     (inst addze hi-temp hi-temp)
1047     (inst addc lo lo-temp prev)
1048     (inst addze hi hi-temp)))
1049
1050 (define-vop (bignum-mult)
1051   (:translate sb!bignum:%multiply)
1052   (:policy :fast-safe)
1053   (:args (x :scs (unsigned-reg) :to (:eval 1))
1054          (y :scs (unsigned-reg) :to (:eval 1)))
1055   (:arg-types unsigned-num unsigned-num)
1056   (:results (hi :scs (unsigned-reg) :from (:eval 1))
1057             (lo :scs (unsigned-reg) :from (:eval 0)))
1058   (:result-types unsigned-num unsigned-num)
1059   (:generator 40
1060     (inst mullw lo x y)
1061     (inst mulhwu hi x y)))
1062
1063 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
1064   (:translate sb!bignum:%lognot))
1065
1066 (define-vop (fixnum-to-digit)
1067   (:translate sb!bignum:%fixnum-to-digit)
1068   (:policy :fast-safe)
1069   (:args (fixnum :scs (any-reg)))
1070   (:arg-types tagged-num)
1071   (:results (digit :scs (unsigned-reg)))
1072   (:result-types unsigned-num)
1073   (:generator 1
1074     (inst srawi digit fixnum 2)))
1075
1076
1077 (define-vop (bignum-floor)
1078   (:translate sb!bignum:%floor)
1079   (:policy :fast-safe)
1080   (:args (num-high :scs (unsigned-reg) :target rem)
1081          (num-low :scs (unsigned-reg) :target rem-low)
1082          (denom :scs (unsigned-reg) :to (:eval 1)))
1083   (:arg-types unsigned-num unsigned-num unsigned-num)
1084   (:temporary (:scs (unsigned-reg) :from (:argument 1)) rem-low)
1085   (:temporary (:scs (unsigned-reg) :from (:eval 0)) temp)
1086   (:results (quo :scs (unsigned-reg) :from (:eval 0))
1087             (rem :scs (unsigned-reg) :from (:argument 0)))
1088   (:result-types unsigned-num unsigned-num)
1089   (:generator 325 ; number of inst assuming targeting works.
1090     (move rem num-high)
1091     (move rem-low num-low)
1092     (flet ((maybe-subtract (&optional (guess temp))
1093              (inst subi temp guess 1)
1094              (inst and temp temp denom)
1095              (inst sub rem rem temp))
1096            (sltu (res x y)
1097              (inst subfc res y x)
1098              (inst subfe res res res)
1099              (inst neg res res)))
1100       (sltu quo rem denom)
1101       (maybe-subtract quo)
1102       (dotimes (i 32)
1103         (inst slwi rem rem 1)
1104         (inst srwi temp rem-low 31)
1105         (inst or rem rem temp)
1106         (inst slwi rem-low rem-low 1)
1107         (sltu temp rem denom)
1108         (inst slwi quo quo 1)
1109         (inst or quo quo temp)
1110         (maybe-subtract)))
1111     (inst not quo quo)))
1112
1113 #|
1114
1115 (define-vop (bignum-floor)
1116   (:translate sb!bignum:%floor)
1117   (:policy :fast-safe)
1118   (:args (div-high :scs (unsigned-reg) :target rem)
1119          (div-low :scs (unsigned-reg) :target quo)
1120          (divisor :scs (unsigned-reg)))
1121   (:arg-types unsigned-num unsigned-num unsigned-num)
1122   (:results (quo :scs (unsigned-reg) :from (:argument 1))
1123             (rem :scs (unsigned-reg) :from (:argument 0)))
1124   (:result-types unsigned-num unsigned-num)
1125   (:generator 300
1126     (inst mtmq div-low)
1127     (inst div quo div-high divisor)
1128     (inst mfmq rem)))
1129 |#
1130
1131 (define-vop (signify-digit)
1132   (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1133   (:policy :fast-safe)
1134   (:args (digit :scs (unsigned-reg) :target res))
1135   (:arg-types unsigned-num)
1136   (:results (res :scs (any-reg signed-reg)))
1137   (:result-types signed-num)
1138   (:generator 1
1139     (sc-case res
1140       (any-reg
1141        (inst slwi res digit 2))
1142       (signed-reg
1143        (move res digit)))))
1144
1145
1146 (define-vop (digit-ashr)
1147   (:translate sb!bignum:%ashr)
1148   (:policy :fast-safe)
1149   (:args (digit :scs (unsigned-reg))
1150          (count :scs (unsigned-reg)))
1151   (:arg-types unsigned-num positive-fixnum)
1152   (:results (result :scs (unsigned-reg)))
1153   (:result-types unsigned-num)
1154   (:generator 1
1155     (inst sraw result digit count)))
1156
1157 (define-vop (digit-lshr digit-ashr)
1158   (:translate sb!bignum:%digit-logical-shift-right)
1159   (:generator 1
1160     (inst srw result digit count)))
1161
1162 (define-vop (digit-ashl digit-ashr)
1163   (:translate sb!bignum:%ashl)
1164   (:generator 1
1165     (inst slw result digit count)))
1166
1167 \f
1168 ;;;; Static funs.
1169
1170 (define-static-fun two-arg-gcd (x y) :translate gcd)
1171 (define-static-fun two-arg-lcm (x y) :translate lcm)
1172
1173 (define-static-fun two-arg-+ (x y) :translate +)
1174 (define-static-fun two-arg-- (x y) :translate -)
1175 (define-static-fun two-arg-* (x y) :translate *)
1176 (define-static-fun two-arg-/ (x y) :translate /)
1177
1178 (define-static-fun two-arg-< (x y) :translate <)
1179 (define-static-fun two-arg-<= (x y) :translate <=)
1180 (define-static-fun two-arg-> (x y) :translate >)
1181 (define-static-fun two-arg->= (x y) :translate >=)
1182 (define-static-fun two-arg-= (x y) :translate =)
1183 (define-static-fun two-arg-/= (x y) :translate /=)
1184
1185 (define-static-fun %negate (x) :translate %negate)
1186
1187 (define-static-fun two-arg-and (x y) :translate logand)
1188 (define-static-fun two-arg-ior (x y) :translate logior)
1189 (define-static-fun two-arg-xor (x y) :translate logxor)
1190 (define-static-fun two-arg-eqv (x y) :translate logeqv)
1191 \f
1192 (in-package "SB!C")
1193
1194 (deftransform * ((x y)
1195                  ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1196                  (unsigned-byte 32))
1197   "recode as shifts and adds"
1198   (let ((y (lvar-value y)))
1199     (multiple-value-bind (result adds shifts)
1200         (ub32-strength-reduce-constant-multiply 'x y)
1201       (cond
1202        ((typep y '(signed-byte 16))
1203         ;; a mulli instruction has a latency of 5.
1204         (when (> (+ adds shifts) 4)
1205           (give-up-ir1-transform)))
1206        (t
1207         ;; a mullw instruction also has a latency of 5, plus two
1208         ;; instructions (in general) to load the immediate into a
1209         ;; register.
1210         (when (> (+ adds shifts) 6)
1211           (give-up-ir1-transform))))
1212       (or result 0))))