CONTINUE restart for %UNKNOWN-KEY-ARG-ERROR.
[sbcl.git] / src / compiler / ppc / arith.lisp
blob17749fe327fafe52db5a14d2a34d0f919c50a456
1 ;;;; the VM definition arithmetic VOPs for the PPC
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.
12 (in-package "SB!VM")
14 ;;;; Unary operations.
16 (define-vop (fast-safe-arith-op)
17 (:policy :fast-safe)
18 (:effects)
19 (:affected))
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))
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))
35 (define-vop (fast-negate/fixnum fixnum-unop)
36 (:translate %negate)
37 (:generator 1
38 (inst neg res x)))
40 (define-vop (fast-negate/signed signed-unop)
41 (:translate %negate)
42 (:generator 2
43 (inst neg res x)))
45 (define-vop (fast-lognot/fixnum fixnum-unop)
46 (:translate lognot)
47 (:generator 1
48 (inst subfic res x (fixnumize -1))))
50 (define-vop (fast-lognot/signed signed-unop)
51 (:translate lognot)
52 (:generator 2
53 (inst not res x)))
55 ;;;; Binary fixnum operations.
57 ;;; Assume that any constant operand is the second arg...
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
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"))
191 (eval-when (:compile-toplevel :load-toplevel :execute)
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))))))
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)))
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)))
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)))
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))))))
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))
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))
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))
395 (inst ,shifted-op temp x high-half)
396 (inst ,op r temp low-half))))
397 `(inst ,op r x y))))))
399 ); eval-when
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)
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)
426 (define-vop (fast-*/fixnum=>fixnum fast-fixnum-binop)
427 (:temporary (:scs (non-descriptor-reg)) temp)
428 (:translate *)
429 (:generator 2
430 (inst srawi temp y n-fixnum-tag-bits)
431 (inst mullw r x temp)))
433 (define-vop (fast-*-c/fixnum=>fixnum fast-fixnum-binop-c)
434 (:translate *)
435 (:arg-types tagged-num
436 (:constant (and (signed-byte 16) (not (integer 0 0)))))
437 (:generator 1
438 (inst mulli r x y)))
440 (define-vop (fast-*-bigc/fixnum=>fixnum fast-fixnum-binop-c)
441 (:translate *)
442 (:arg-types tagged-num
443 (:constant (and fixnum (not (signed-byte 16)))))
444 (:temporary (:scs (non-descriptor-reg)) temp)
445 (:generator 1
446 (inst lr temp y)
447 (inst mullw r x temp)))
449 (define-vop (fast-*/signed=>signed fast-signed-binop)
450 (:translate *)
451 (:generator 4
452 (inst mullw r x y)))
454 (define-vop (fast-*-c/signed=>signed fast-signed-binop-c)
455 (:translate *)
456 (:generator 3
457 (inst mulli r x y)))
459 (define-vop (fast-*/unsigned=>unsigned fast-unsigned-binop)
460 (:translate *)
461 (:generator 4
462 (inst mullw r x y)))
464 (define-vop (fast-*-c/unsigned=>unsigned fast-unsigned-binop-c)
465 (:translate *)
466 (:generator 3
467 (inst mulli r x y)))
469 ;;; Shifting
471 (macrolet ((def (name sc-type type result-type cost)
472 `(define-vop (,name)
473 (:note "inline ASH")
474 (:translate ash)
475 (:args (number :scs (,sc-type))
476 (amount :scs (signed-reg unsigned-reg immediate)))
477 (:arg-types ,type positive-fixnum)
478 (:results (result :scs (,result-type)))
479 (:result-types ,type)
480 (:policy :fast-safe)
481 (:generator ,cost
482 (sc-case amount
483 ((signed-reg unsigned-reg)
484 (inst slw result number amount))
485 (immediate
486 (let ((amount (tn-value amount)))
487 (aver (> amount 0))
488 (inst slwi result number amount))))))))
489 ;; FIXME: There's the opportunity for a sneaky optimization here, I
490 ;; think: a FAST-ASH-LEFT-C/FIXNUM=>SIGNED vop. -- CSR, 2003-09-03
491 (def fast-ash-left/fixnum=>fixnum any-reg tagged-num any-reg 2)
492 (def fast-ash-left/signed=>signed signed-reg signed-num signed-reg 3)
493 (def fast-ash-left/unsigned=>unsigned unsigned-reg unsigned-num unsigned-reg 3))
495 (define-vop (fast-ash/unsigned=>unsigned)
496 (:note "inline ASH")
497 (:args (number :scs (unsigned-reg) :to :save)
498 (amount :scs (signed-reg)))
499 (:arg-types (:or unsigned-num) signed-num)
500 (:results (result :scs (unsigned-reg)))
501 (:result-types unsigned-num)
502 (:translate ash)
503 (:policy :fast-safe)
504 (:temporary (:sc non-descriptor-reg) ndesc)
505 (:generator 5
506 (let ((positive (gen-label))
507 (done (gen-label)))
508 (inst cmpwi amount 0)
509 (inst neg ndesc amount)
510 (inst bge positive)
511 (inst cmpwi ndesc 31)
512 (inst srw result number ndesc)
513 (inst ble done)
514 (move result zero-tn)
515 (inst b done)
517 (emit-label positive)
518 ;; The result-type assures us that this shift will not overflow.
519 (inst slw result number amount)
521 (emit-label done))))
523 (define-vop (fast-ash-c/unsigned=>unsigned)
524 (:note "inline constant ASH")
525 (:args (number :scs (unsigned-reg)))
526 (:info amount)
527 (:arg-types unsigned-num (:constant integer))
528 (:results (result :scs (unsigned-reg)))
529 (:result-types unsigned-num)
530 (:translate ash)
531 (:policy :fast-safe)
532 (:generator 4
533 (cond
534 ((and (minusp amount) (< amount -31)) (move result zero-tn))
535 ((minusp amount) (inst srwi result number (- amount)))
536 ;; possible because this is used in the modular version too
537 ((> amount 31) (move result zero-tn))
538 (t (inst slwi result number amount)))))
540 (define-vop (fast-ash/signed=>signed)
541 (:note "inline ASH")
542 (:args (number :scs (signed-reg) :to :save)
543 (amount :scs (signed-reg immediate)))
544 (:arg-types (:or signed-num) signed-num)
545 (:results (result :scs (signed-reg)))
546 (:result-types (:or signed-num))
547 (:translate ash)
548 (:policy :fast-safe)
549 (:temporary (:sc non-descriptor-reg) ndesc)
550 (:generator 3
551 (sc-case amount
552 (signed-reg
553 (let ((positive (gen-label))
554 (done (gen-label)))
555 (inst cmpwi amount 0)
556 (inst neg ndesc amount)
557 (inst bge positive)
558 (inst cmpwi ndesc 31)
559 (inst sraw result number ndesc)
560 (inst ble done)
561 (inst srawi result number 31)
562 (inst b done)
564 (emit-label positive)
565 ;; The result-type assures us that this shift will not overflow.
566 (inst slw result number amount)
568 (emit-label done)))
570 (immediate
571 (let ((amount (tn-value amount)))
572 (if (minusp amount)
573 (let ((amount (min 31 (- amount))))
574 (inst srawi result number amount))
575 (inst slwi result number amount)))))))
577 (define-vop (signed-byte-32-len)
578 (:translate integer-length)
579 (:note "inline (signed-byte 32) integer-length")
580 (:policy :fast-safe)
581 (:args (arg :scs (signed-reg)))
582 (:arg-types signed-num)
583 (:results (res :scs (unsigned-reg) :from :load))
584 (:result-types unsigned-num)
585 (:generator 6
586 ; (integer-length arg) = (- 32 (cntlz (if (>= arg 0) arg (lognot arg))))
587 (let ((nonneg (gen-label)))
588 (inst cntlzw. res arg)
589 (inst bne nonneg)
590 (inst not res arg)
591 (inst cntlzw res res)
592 (emit-label nonneg)
593 (inst subfic res res 32))))
595 (define-vop (unsigned-byte-32-len)
596 (:translate integer-length)
597 (:note "inline (unsigned-byte 32) integer-length")
598 (:policy :fast-safe)
599 (:args (arg :scs (unsigned-reg)))
600 (:arg-types unsigned-num)
601 (:results (res :scs (unsigned-reg)))
602 (:result-types unsigned-num)
603 (:generator 4
604 (inst cntlzw res arg)
605 (inst subfic res res 32)))
607 (define-vop (unsigned-byte-32-count)
608 (:translate logcount)
609 (:note "inline (unsigned-byte 32) logcount")
610 (:policy :fast-safe)
611 (:args (arg :scs (unsigned-reg) :target shift))
612 (:arg-types unsigned-num)
613 (:results (res :scs (any-reg)))
614 (:result-types positive-fixnum)
615 (:temporary (:scs (non-descriptor-reg) :from (:argument 0)) shift temp)
616 (:generator 30
617 (let ((loop (gen-label))
618 (done (gen-label)))
619 (inst add. shift zero-tn arg)
620 (move res zero-tn)
621 (inst beq done)
623 (emit-label loop)
624 (inst subi temp shift 1)
625 (inst and. shift shift temp)
626 (inst addi res res (fixnumize 1))
627 (inst bne loop)
629 (emit-label done))))
632 ;;;; %LDB
634 (defknown %%ldb (integer unsigned-byte unsigned-byte) unsigned-byte
635 (movable foldable flushable always-translatable))
637 ;;; only for constant folding within the compiler
638 (defun %%ldb (integer size posn)
639 (%ldb size posn integer))
641 (define-vop (ldb-c/fixnum)
642 (:translate %%ldb)
643 (:args (x :scs (any-reg)))
644 (:arg-types tagged-num (:constant (integer 1 29)) (:constant (integer 0 29)))
645 (:info size posn)
646 (:results (res :scs (any-reg)))
647 (:result-types tagged-num)
648 (:policy :fast-safe)
649 (:generator 2
650 (let ((phantom-bits (- (+ size posn) 30)))
651 (cond
652 ((plusp phantom-bits)
653 ;; The byte to be loaded into RES includes sign bits which are not
654 ;; present in the input X physically. RLWINM as used below would
655 ;; mask these out with 0 even for negative inputs.
656 (inst srawi res x phantom-bits)
657 (inst rlwinm res x
658 (mod (- 32 posn (- phantom-bits)) 32)
659 (- 32 size n-fixnum-tag-bits)
660 (- 31 n-fixnum-tag-bits)))
662 (inst rlwinm res x
663 (mod (- 32 posn) 32) ; effectively rotate right
664 (- 32 size n-fixnum-tag-bits)
665 (- 31 n-fixnum-tag-bits)))))))
667 (define-vop (ldb-c/signed)
668 (:translate %%ldb)
669 (:args (x :scs (signed-reg)))
670 (:arg-types signed-num (:constant (integer 1 29)) (:constant (integer 0 31)))
671 (:info size posn)
672 (:results (res :scs (any-reg)))
673 (:result-types tagged-num)
674 (:policy :fast-safe)
675 (:generator 3
676 (inst rlwinm res x
677 (mod (- (+ 32 n-fixnum-tag-bits) posn) 32)
678 (- 32 size n-fixnum-tag-bits)
679 (- 31 n-fixnum-tag-bits))))
681 (define-vop (ldb-c/unsigned)
682 (:translate %%ldb)
683 (:args (x :scs (unsigned-reg)))
684 (:arg-types unsigned-num (:constant (integer 1 29)) (:constant (integer 0 31)))
685 (:info size posn)
686 (:results (res :scs (any-reg)))
687 (:result-types tagged-num)
688 (:policy :fast-safe)
689 (:generator 3
690 (inst rlwinm res x
691 (mod (- (+ 32 n-fixnum-tag-bits) posn) 32)
692 (- 32 size n-fixnum-tag-bits)
693 (- 31 n-fixnum-tag-bits))))
695 ;;;; Modular functions:
696 (define-modular-fun lognot-mod32 (x) lognot :untagged nil 32)
697 (define-vop (lognot-mod32/unsigned=>unsigned)
698 (:translate lognot-mod32)
699 (:args (x :scs (unsigned-reg)))
700 (:arg-types unsigned-num)
701 (:results (res :scs (unsigned-reg)))
702 (:result-types unsigned-num)
703 (:policy :fast-safe)
704 (:generator 1
705 (inst not res x)))
707 (define-vop (fast-ash-left-mod32-c/unsigned=>unsigned
708 fast-ash-c/unsigned=>unsigned)
709 (:translate ash-left-mod32))
711 (define-vop (fast-ash-left-mod32/unsigned=>unsigned
712 fast-ash-left/unsigned=>unsigned))
713 (deftransform ash-left-mod32 ((integer count)
714 ((unsigned-byte 32) (unsigned-byte 5)))
715 (when (sb!c::constant-lvar-p count)
716 (sb!c::give-up-ir1-transform))
717 '(%primitive fast-ash-left-mod32/unsigned=>unsigned integer count))
719 (macrolet
720 ((define-modular-backend (fun &optional constantp)
721 (let ((mfun-name (symbolicate fun '-mod32))
722 (modvop (symbolicate 'fast- fun '-mod32/unsigned=>unsigned))
723 (modcvop (symbolicate 'fast- fun 'mod32-c/unsigned=>unsigned))
724 (vop (symbolicate 'fast- fun '/unsigned=>unsigned))
725 (cvop (symbolicate 'fast- fun '-c/unsigned=>unsigned)))
726 `(progn
727 (define-modular-fun ,mfun-name (x y) ,fun :untagged nil 32)
728 (define-vop (,modvop ,vop)
729 (:translate ,mfun-name))
730 ,@(when constantp
731 `((define-vop (,modcvop ,cvop)
732 (:translate ,mfun-name))))))))
733 (define-modular-backend + t)
734 (define-modular-backend - t)
735 (define-modular-backend * t)
736 (define-modular-backend logeqv)
737 (define-modular-backend lognand)
738 (define-modular-backend lognor)
739 (define-modular-backend logandc1)
740 (define-modular-backend logandc2)
741 (define-modular-backend logorc1)
742 (define-modular-backend logorc2))
744 ;;;; Binary conditional VOPs:
746 (define-vop (fast-conditional)
747 (:conditional)
748 (:info target not-p)
749 (:effects)
750 (:affected)
751 (:policy :fast-safe))
753 (define-vop (fast-conditional/fixnum fast-conditional)
754 (:args (x :scs (any-reg zero))
755 (y :scs (any-reg zero)))
756 (:arg-types tagged-num tagged-num)
757 (:note "inline fixnum comparison"))
759 (define-vop (fast-conditional-c/fixnum fast-conditional/fixnum)
760 (:args (x :scs (any-reg zero)))
761 (:arg-types tagged-num (:constant (signed-byte 14)))
762 (:info target not-p y))
764 (define-vop (fast-conditional/signed fast-conditional)
765 (:args (x :scs (signed-reg zero))
766 (y :scs (signed-reg zero)))
767 (:arg-types signed-num signed-num)
768 (:note "inline (signed-byte 32) comparison"))
770 (define-vop (fast-conditional-c/signed fast-conditional/signed)
771 (:args (x :scs (signed-reg zero)))
772 (:arg-types signed-num (:constant (signed-byte 16)))
773 (:info target not-p y))
775 (define-vop (fast-conditional/unsigned fast-conditional)
776 (:args (x :scs (unsigned-reg zero))
777 (y :scs (unsigned-reg zero)))
778 (:arg-types unsigned-num unsigned-num)
779 (:note "inline (unsigned-byte 32) comparison"))
781 (define-vop (fast-conditional-c/unsigned fast-conditional/unsigned)
782 (:args (x :scs (unsigned-reg zero)))
783 (:arg-types unsigned-num (:constant (unsigned-byte 16)))
784 (:info target not-p y))
786 (macrolet ((define-logtest-vops ()
787 `(progn
788 ,@(loop for suffix in '(/fixnum -c/fixnum
789 /signed -c/signed
790 /unsigned -c/unsigned)
791 for sc in '(any-reg any-reg
792 signed-reg signed-reg
793 unsigned-reg unsigned-reg)
794 for cost in '(4 3 6 5 6 5)
795 collect
796 `(define-vop (,(symbolicate "FAST-LOGTEST" suffix)
797 ,(symbolicate "FAST-CONDITIONAL" suffix))
798 (:translate logtest)
799 (:temporary (:scs (,sc) :to (:result 0)) test)
800 (:generator ,cost
801 ;; We could be a lot more sophisticated here and
802 ;; check for possibilities with ANDIS..
803 ,(if (string= "-C" suffix :end2 2)
804 `(inst andi. test x ,(if (eq suffix '-c/fixnum)
805 '(fixnumize y)
806 'y))
807 `(inst and. test x y))
808 (inst b? (if not-p :eq :ne) target)))))))
809 (define-logtest-vops))
811 (defknown %logbitp (integer unsigned-byte) boolean
812 (movable foldable flushable always-translatable))
814 ;;; only for constant folding within the compiler
815 (defun %logbitp (integer index)
816 (logbitp index integer))
818 ;;; We only handle the constant cases because those are the only ones
819 ;;; guaranteed to make it past COMBINATION-IMPLEMENTATION-STYLE.
820 ;;; --njf, 06-02-2006
821 (define-vop (fast-logbitp-c/fixnum fast-conditional-c/fixnum)
822 (:translate %logbitp)
823 (:arg-types tagged-num (:constant (integer 0 29)))
824 (:temporary (:scs (any-reg) :to (:result 0)) test)
825 (:generator 4
826 (if (< y 14)
827 (inst andi. test x (ash 1 (+ y n-fixnum-tag-bits)))
828 (inst andis. test x (ash 1 (- y 14))))
829 (inst b? (if not-p :eq :ne) target)))
831 (define-vop (fast-logbitp-c/signed fast-conditional-c/signed)
832 (:translate %logbitp)
833 (:arg-types signed-num (:constant (integer 0 31)))
834 (:temporary (:scs (signed-reg) :to (:result 0)) test)
835 (:generator 4
836 (if (< y 16)
837 (inst andi. test x (ash 1 y))
838 (inst andis. test x (ash 1 (- y 16))))
839 (inst b? (if not-p :eq :ne) target)))
841 (define-vop (fast-logbitp-c/unsigned fast-conditional-c/unsigned)
842 (:translate %logbitp)
843 (:arg-types unsigned-num (:constant (integer 0 31)))
844 (:temporary (:scs (unsigned-reg) :to (:result 0)) test)
845 (:generator 4
846 (if (< y 16)
847 (inst andi. test x (ash 1 y))
848 (inst andis. test x (ash 1 (- y 16))))
849 (inst b? (if not-p :eq :ne) target)))
851 (define-vop (fast-if-</fixnum fast-conditional/fixnum)
852 (:translate <)
853 (:generator 4
854 (inst cmpw x y)
855 (inst b? (if not-p :ge :lt) target)))
857 (define-vop (fast-if-<-c/fixnum fast-conditional-c/fixnum)
858 (:translate <)
859 (:generator 3
860 (inst cmpwi x (fixnumize y))
861 (inst b? (if not-p :ge :lt) target)))
863 (define-vop (fast-if-</signed fast-conditional/signed)
864 (:translate <)
865 (:generator 6
866 (inst cmpw x y)
867 (inst b? (if not-p :ge :lt) target)))
869 (define-vop (fast-if-<-c/signed fast-conditional-c/signed)
870 (:translate <)
871 (:generator 5
872 (inst cmpwi x y)
873 (inst b? (if not-p :ge :lt) target)))
875 (define-vop (fast-if-</unsigned fast-conditional/unsigned)
876 (:translate <)
877 (:generator 6
878 (inst cmplw x y)
879 (inst b? (if not-p :ge :lt) target)))
881 (define-vop (fast-if-<-c/unsigned fast-conditional-c/unsigned)
882 (:translate <)
883 (:generator 5
884 (inst cmplwi x y)
885 (inst b? (if not-p :ge :lt) target)))
887 (define-vop (fast-if->/fixnum fast-conditional/fixnum)
888 (:translate >)
889 (:generator 4
890 (inst cmpw x y)
891 (inst b? (if not-p :le :gt) target)))
893 (define-vop (fast-if->-c/fixnum fast-conditional-c/fixnum)
894 (:translate >)
895 (:generator 3
896 (inst cmpwi x (fixnumize y))
897 (inst b? (if not-p :le :gt) target)))
899 (define-vop (fast-if->/signed fast-conditional/signed)
900 (:translate >)
901 (:generator 6
902 (inst cmpw x y)
903 (inst b? (if not-p :le :gt) target)))
905 (define-vop (fast-if->-c/signed fast-conditional-c/signed)
906 (:translate >)
907 (:generator 5
908 (inst cmpwi x y)
909 (inst b? (if not-p :le :gt) target)))
911 (define-vop (fast-if->/unsigned fast-conditional/unsigned)
912 (:translate >)
913 (:generator 6
914 (inst cmplw x y)
915 (inst b? (if not-p :le :gt) target)))
917 (define-vop (fast-if->-c/unsigned fast-conditional-c/unsigned)
918 (:translate >)
919 (:generator 5
920 (inst cmplwi x y)
921 (inst b? (if not-p :le :gt) target)))
923 (define-vop (fast-if-eql/signed fast-conditional/signed)
924 (:translate eql)
925 (:generator 6
926 (inst cmpw x y)
927 (inst b? (if not-p :ne :eq) target)))
929 (define-vop (fast-if-eql-c/signed fast-conditional-c/signed)
930 (:translate eql)
931 (:generator 5
932 (inst cmpwi x y)
933 (inst b? (if not-p :ne :eq) target)))
935 (define-vop (fast-if-eql/unsigned fast-conditional/unsigned)
936 (:translate eql)
937 (:generator 6
938 (inst cmplw x y)
939 (inst b? (if not-p :ne :eq) target)))
941 (define-vop (fast-if-eql-c/unsigned fast-conditional-c/unsigned)
942 (:translate eql)
943 (:generator 5
944 (inst cmplwi x y)
945 (inst b? (if not-p :ne :eq) target)))
948 ;;; EQL/FIXNUM is funny because the first arg can be of any type, not just a
949 ;;; known fixnum.
951 ;;; These versions specify a fixnum restriction on their first arg. We have
952 ;;; also generic-eql/fixnum VOPs which are the same, but have no restriction on
953 ;;; the first arg and a higher cost. The reason for doing this is to prevent
954 ;;; fixnum specific operations from being used on word integers, spuriously
955 ;;; consing the argument.
958 (define-vop (fast-eql/fixnum fast-conditional)
959 (:args (x :scs (any-reg descriptor-reg zero))
960 (y :scs (any-reg zero)))
961 (:arg-types tagged-num tagged-num)
962 (:note "inline fixnum comparison")
963 (:translate eql)
964 (:generator 4
965 (inst cmpw x y)
966 (inst b? (if not-p :ne :eq) target)))
968 (define-vop (generic-eql/fixnum fast-eql/fixnum)
969 (:arg-types * tagged-num)
970 (:variant-cost 7))
972 (define-vop (fast-eql-c/fixnum fast-conditional/fixnum)
973 (:args (x :scs (any-reg descriptor-reg zero)))
974 (:arg-types tagged-num (:constant (signed-byte 14)))
975 (:info target not-p y)
976 (:translate eql)
977 (:generator 2
978 (inst cmpwi x (fixnumize y))
979 (inst b? (if not-p :ne :eq) target)))
981 (define-vop (generic-eql-c/fixnum fast-eql-c/fixnum)
982 (:arg-types * (:constant (signed-byte 11)))
983 (:variant-cost 6))
986 ;;;; 32-bit logical operations
988 (define-vop (shift-towards-someplace)
989 (:policy :fast-safe)
990 (:args (num :scs (unsigned-reg))
991 (amount :scs (signed-reg)))
992 (:arg-types unsigned-num tagged-num)
993 (:results (r :scs (unsigned-reg)))
994 (:result-types unsigned-num))
996 (define-vop (shift-towards-start shift-towards-someplace)
997 (:translate shift-towards-start)
998 (:note "shift-towards-start")
999 (:generator 1
1000 (inst rlwinm amount amount 0 27 31)
1001 (inst slw r num amount)))
1003 (define-vop (shift-towards-end shift-towards-someplace)
1004 (:translate shift-towards-end)
1005 (:note "shift-towards-end")
1006 (:generator 1
1007 (inst rlwinm amount amount 0 27 31)
1008 (inst srw r num amount)))
1010 ;;;; Bignum stuff.
1012 (define-vop (bignum-length get-header-data)
1013 (:translate sb!bignum:%bignum-length)
1014 (:policy :fast-safe))
1016 (define-vop (bignum-set-length set-header-data)
1017 (:translate sb!bignum:%bignum-set-length)
1018 (:policy :fast-safe))
1020 (define-vop (bignum-ref word-index-ref)
1021 (:variant bignum-digits-offset other-pointer-lowtag)
1022 (:translate sb!bignum:%bignum-ref)
1023 (:results (value :scs (unsigned-reg)))
1024 (:result-types unsigned-num))
1026 (define-vop (bignum-set word-index-set)
1027 (:variant bignum-digits-offset other-pointer-lowtag)
1028 (:translate sb!bignum:%bignum-set)
1029 (:args (object :scs (descriptor-reg))
1030 (index :scs (any-reg immediate zero))
1031 (value :scs (unsigned-reg)))
1032 (:arg-types t positive-fixnum unsigned-num)
1033 (:results (result :scs (unsigned-reg)))
1034 (:result-types unsigned-num))
1036 (define-vop (digit-0-or-plus)
1037 (:translate sb!bignum:%digit-0-or-plusp)
1038 (:policy :fast-safe)
1039 (:args (digit :scs (unsigned-reg)))
1040 (:arg-types unsigned-num)
1041 (:results (result :scs (descriptor-reg)))
1042 (:generator 3
1043 (let ((done (gen-label)))
1044 (inst cmpwi digit 0)
1045 (move result null-tn)
1046 (inst blt done)
1047 (load-symbol result t)
1048 (emit-label done))))
1050 (define-vop (add-w/carry)
1051 (:translate sb!bignum:%add-with-carry)
1052 (:policy :fast-safe)
1053 (:args (a :scs (unsigned-reg))
1054 (b :scs (unsigned-reg))
1055 (c :scs (any-reg)))
1056 (:arg-types unsigned-num unsigned-num positive-fixnum)
1057 (:temporary (:scs (unsigned-reg)) temp)
1058 (:results (result :scs (unsigned-reg))
1059 (carry :scs (unsigned-reg)))
1060 (:result-types unsigned-num positive-fixnum)
1061 (:generator 3
1062 (inst addic temp c -1)
1063 (inst adde result a b)
1064 (inst addze carry zero-tn)))
1066 (define-vop (sub-w/borrow)
1067 (:translate sb!bignum:%subtract-with-borrow)
1068 (:policy :fast-safe)
1069 (:args (a :scs (unsigned-reg))
1070 (b :scs (unsigned-reg))
1071 (c :scs (any-reg)))
1072 (:arg-types unsigned-num unsigned-num positive-fixnum)
1073 (:temporary (:scs (unsigned-reg)) temp)
1074 (:results (result :scs (unsigned-reg))
1075 (borrow :scs (unsigned-reg)))
1076 (:result-types unsigned-num positive-fixnum)
1077 (:generator 4
1078 (inst addic temp c -1)
1079 (inst sube result a b)
1080 (inst addze borrow zero-tn)))
1082 (define-vop (bignum-mult-and-add-3-arg)
1083 (:translate sb!bignum:%multiply-and-add)
1084 (:policy :fast-safe)
1085 (:args (x :scs (unsigned-reg))
1086 (y :scs (unsigned-reg))
1087 (carry-in :scs (unsigned-reg) :to (:eval 1)))
1088 (:arg-types unsigned-num unsigned-num unsigned-num)
1089 (:temporary (:scs (unsigned-reg) :to (:result 0) :target hi) hi-temp)
1090 (:temporary (:scs (unsigned-reg) :from (:eval 0) :to (:result 1)
1091 :target lo) lo-temp)
1092 (:results (hi :scs (unsigned-reg))
1093 (lo :scs (unsigned-reg)))
1094 (:result-types unsigned-num unsigned-num)
1095 (:generator 40
1096 (inst mulhwu hi-temp x y)
1097 (inst mullw lo-temp x y)
1098 (inst addc lo lo-temp carry-in)
1099 (inst addze hi hi-temp)))
1101 (define-vop (bignum-mult-and-add-4-arg)
1102 (:translate sb!bignum:%multiply-and-add)
1103 (:policy :fast-safe)
1104 (:args (x :scs (unsigned-reg))
1105 (y :scs (unsigned-reg))
1106 (prev :scs (unsigned-reg) :to (:eval 1))
1107 (carry-in :scs (unsigned-reg) :to (:eval 1)))
1108 (:arg-types unsigned-num unsigned-num unsigned-num unsigned-num)
1109 (:temporary (:scs (unsigned-reg) :to (:result 0) :target hi) hi-temp)
1110 (:temporary (:scs (unsigned-reg) :from (:eval 0) :to (:result 1)
1111 :target lo) lo-temp)
1112 (:results (hi :scs (unsigned-reg))
1113 (lo :scs (unsigned-reg)))
1114 (:result-types unsigned-num unsigned-num)
1115 (:generator 40
1116 (inst mulhwu hi-temp x y)
1117 (inst mullw lo-temp x y)
1118 (inst addc lo-temp lo-temp carry-in)
1119 (inst addze hi-temp hi-temp)
1120 (inst addc lo lo-temp prev)
1121 (inst addze hi hi-temp)))
1123 (define-vop (bignum-mult)
1124 (:translate sb!bignum:%multiply)
1125 (:policy :fast-safe)
1126 (:args (x :scs (unsigned-reg) :to (:eval 1))
1127 (y :scs (unsigned-reg) :to (:eval 1)))
1128 (:arg-types unsigned-num unsigned-num)
1129 (:results (hi :scs (unsigned-reg) :from (:eval 1))
1130 (lo :scs (unsigned-reg) :from (:eval 0)))
1131 (:result-types unsigned-num unsigned-num)
1132 (:generator 40
1133 (inst mullw lo x y)
1134 (inst mulhwu hi x y)))
1136 #!+multiply-high-vops
1137 (define-vop (mulhi)
1138 (:translate %multiply-high)
1139 (:policy :fast-safe)
1140 (:args (x :scs (unsigned-reg))
1141 (y :scs (unsigned-reg)))
1142 (:arg-types unsigned-num unsigned-num)
1143 (:results (hi :scs (unsigned-reg)))
1144 (:result-types unsigned-num)
1145 (:generator 20
1146 (inst mulhwu hi x y)))
1148 #!+multiply-high-vops
1149 (define-vop (mulhi/fx)
1150 (:translate %multiply-high)
1151 (:policy :fast-safe)
1152 (:args (x :scs (any-reg))
1153 (y :scs (unsigned-reg)))
1154 (:arg-types positive-fixnum unsigned-num)
1155 (:temporary (:sc non-descriptor-reg :from :eval :to :result) temp)
1156 (:temporary (:sc non-descriptor-reg :from :eval :to :result) mask)
1157 (:results (hi :scs (any-reg)))
1158 (:result-types positive-fixnum)
1159 (:generator 15
1160 (inst mulhwu temp x y)
1161 (inst lr mask fixnum-tag-mask)
1162 (inst andc hi temp mask)))
1164 (define-vop (bignum-lognot lognot-mod32/unsigned=>unsigned)
1165 (:translate sb!bignum:%lognot))
1167 (define-vop (fixnum-to-digit)
1168 (:translate sb!bignum:%fixnum-to-digit)
1169 (:policy :fast-safe)
1170 (:args (fixnum :scs (any-reg)))
1171 (:arg-types tagged-num)
1172 (:results (digit :scs (unsigned-reg)))
1173 (:result-types unsigned-num)
1174 (:generator 1
1175 (inst srawi digit fixnum n-fixnum-tag-bits)))
1178 (define-vop (bignum-floor)
1179 (:translate sb!bignum:%bigfloor)
1180 (:policy :fast-safe)
1181 (:args (num-high :scs (unsigned-reg) :target rem)
1182 (num-low :scs (unsigned-reg) :target rem-low)
1183 (denom :scs (unsigned-reg) :to (:eval 1)))
1184 (:arg-types unsigned-num unsigned-num unsigned-num)
1185 (:temporary (:scs (unsigned-reg) :from (:argument 1)) rem-low)
1186 (:temporary (:scs (unsigned-reg) :from (:eval 0)) temp)
1187 (:results (quo :scs (unsigned-reg) :from (:eval 0))
1188 (rem :scs (unsigned-reg) :from (:argument 0)))
1189 (:result-types unsigned-num unsigned-num)
1190 (:generator 325 ; number of inst assuming targeting works.
1191 (move rem num-high)
1192 (move rem-low num-low)
1193 (flet ((maybe-subtract (&optional (guess temp))
1194 (inst subi temp guess 1)
1195 (inst and temp temp denom)
1196 (inst sub rem rem temp))
1197 (sltu (res x y)
1198 (inst subfc res y x)
1199 (inst subfe res res res)
1200 (inst neg res res)))
1201 (sltu quo rem denom)
1202 (maybe-subtract quo)
1203 (dotimes (i 32)
1204 (inst slwi rem rem 1)
1205 (inst srwi temp rem-low 31)
1206 (inst or rem rem temp)
1207 (inst slwi rem-low rem-low 1)
1208 (sltu temp rem denom)
1209 (inst slwi quo quo 1)
1210 (inst or quo quo temp)
1211 (maybe-subtract)))
1212 (inst not quo quo)))
1216 (define-vop (bignum-floor)
1217 (:translate sb!bignum:%bigfloor)
1218 (:policy :fast-safe)
1219 (:args (div-high :scs (unsigned-reg) :target rem)
1220 (div-low :scs (unsigned-reg) :target quo)
1221 (divisor :scs (unsigned-reg)))
1222 (:arg-types unsigned-num unsigned-num unsigned-num)
1223 (:results (quo :scs (unsigned-reg) :from (:argument 1))
1224 (rem :scs (unsigned-reg) :from (:argument 0)))
1225 (:result-types unsigned-num unsigned-num)
1226 (:generator 300
1227 (inst mtmq div-low)
1228 (inst div quo div-high divisor)
1229 (inst mfmq rem)))
1232 (define-vop (signify-digit)
1233 (:translate sb!bignum:%fixnum-digit-with-correct-sign)
1234 (:policy :fast-safe)
1235 (:args (digit :scs (unsigned-reg) :target res))
1236 (:arg-types unsigned-num)
1237 (:results (res :scs (any-reg signed-reg)))
1238 (:result-types signed-num)
1239 (:generator 1
1240 (sc-case res
1241 (any-reg
1242 (inst slwi res digit n-fixnum-tag-bits))
1243 (signed-reg
1244 (move res digit)))))
1247 (define-vop (digit-ashr)
1248 (:translate sb!bignum:%ashr)
1249 (:policy :fast-safe)
1250 (:args (digit :scs (unsigned-reg))
1251 (count :scs (unsigned-reg)))
1252 (:arg-types unsigned-num positive-fixnum)
1253 (:results (result :scs (unsigned-reg)))
1254 (:result-types unsigned-num)
1255 (:generator 1
1256 (inst sraw result digit count)))
1258 (define-vop (digit-lshr digit-ashr)
1259 (:translate sb!bignum:%digit-logical-shift-right)
1260 (:generator 1
1261 (inst srw result digit count)))
1263 (define-vop (digit-ashl digit-ashr)
1264 (:translate sb!bignum:%ashl)
1265 (:generator 1
1266 (inst slw result digit count)))
1269 ;;;; Static funs.
1271 (define-static-fun two-arg-gcd (x y) :translate gcd)
1272 (define-static-fun two-arg-lcm (x y) :translate lcm)
1274 (define-static-fun two-arg-+ (x y) :translate +)
1275 (define-static-fun two-arg-- (x y) :translate -)
1276 (define-static-fun two-arg-* (x y) :translate *)
1277 (define-static-fun two-arg-/ (x y) :translate /)
1279 (define-static-fun two-arg-< (x y) :translate <)
1280 (define-static-fun two-arg-<= (x y) :translate <=)
1281 (define-static-fun two-arg-> (x y) :translate >)
1282 (define-static-fun two-arg->= (x y) :translate >=)
1283 (define-static-fun two-arg-= (x y) :translate =)
1284 (define-static-fun two-arg-/= (x y) :translate /=)
1286 (define-static-fun %negate (x) :translate %negate)
1288 (define-static-fun two-arg-and (x y) :translate logand)
1289 (define-static-fun two-arg-ior (x y) :translate logior)
1290 (define-static-fun two-arg-xor (x y) :translate logxor)
1291 (define-static-fun two-arg-eqv (x y) :translate logeqv)
1293 (in-package "SB!C")
1295 (deftransform * ((x y)
1296 ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
1297 (unsigned-byte 32))
1298 "recode as shifts and adds"
1299 (let ((y (lvar-value y)))
1300 (multiple-value-bind (result adds shifts)
1301 (ub32-strength-reduce-constant-multiply 'x y)
1302 (cond
1303 ((typep y '(signed-byte 16))
1304 ;; a mulli instruction has a latency of 5.
1305 (when (> (+ adds shifts) 4)
1306 (give-up-ir1-transform)))
1308 ;; a mullw instruction also has a latency of 5, plus two
1309 ;; instructions (in general) to load the immediate into a
1310 ;; register.
1311 (when (> (+ adds shifts) 6)
1312 (give-up-ir1-transform))))
1313 (or result 0))))