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