0.8.3.62:
[sbcl.git] / src / compiler / ppc / arith.lisp
index aafd5cf..9bbc6cd 100644 (file)
@@ -1,10 +1,15 @@
-;;;
-;;; Converted by William Lott.
-;;; 
-
-(in-package "SB!VM")
+;;;; the VM definition arithmetic VOPs for the PPC
 
+;;;; This software is part of the SBCL system. See the README file for
+;;;; more information.
+;;;;
+;;;; This software is derived from the CMU CL system, which was
+;;;; written at Carnegie Mellon University and released into the
+;;;; public domain. The software is in the public domain and is
+;;;; provided with absolutely no warranty. See the COPYING and CREDITS
+;;;; files for more information.
 
+(in-package "SB!VM")
 \f
 ;;;; Unary operations.
 
@@ -13,7 +18,6 @@
   (:effects)
   (:affected))
 
-
 (define-vop (fixnum-unop fast-safe-arith-op)
   (:args (x :scs (any-reg)))
   (:results (res :scs (any-reg)))
@@ -47,8 +51,6 @@
   (:translate lognot)
   (:generator 1
     (inst not res x)))
-
-
 \f
 ;;;; Binary fixnum operations.
 
@@ -78,7 +80,6 @@
   (:result-types signed-num)
   (:note "inline (signed-byte 32) arithmetic"))
 
-
 (define-vop (fast-fixnum-binop-c fast-safe-arith-op)
   (:args (x :target r :scs (any-reg zero)))
   (:info y)
                          fixnum-additive-overflow-trap))
       (emit-label no-overflow))))
 
+(define-vop (fast-*/fixnum=>fixnum fast-fixnum-binop)
+  (:temporary (:scs (non-descriptor-reg)) temp)
+  (:translate *)
+  (:generator 2
+    (inst srawi temp y 2)
+    (inst mullw r x temp)))
+
+(define-vop (fast-*-c/fixnum=>fixnum fast-fixnum-binop-c)
+  (:translate *)
+  (:arg-types tagged-num 
+             (:constant (and (signed-byte 16) (not (integer 0 0)))))
+  (:generator 1
+    (inst mulli r x y)))
+
+(define-vop (fast-*-bigc/fixnum=>fixnum fast-fixnum-binop-c)
+  (:translate *)
+  (:arg-types tagged-num
+             (:constant (and fixnum (not (signed-byte 16)))))
+  (:temporary (:scs (non-descriptor-reg)) temp)
+  (:generator 1
+    (inst lr temp y)
+    (inst mullw r x temp)))
+
+(define-vop (fast-*/signed=>signed fast-signed-binop)
+  (:translate *)
+  (:generator 4
+    (inst mullw r x y)))
 
+(define-vop (fast-*-c/signed=>signed fast-signed-binop-c)
+  (:translate *)
+  (:generator 3
+    (inst mulli r x y)))
+
+(define-vop (fast-*/unsigned=>unsigned fast-unsigned-binop)
+  (:translate *)
+  (:generator 4
+    (inst mullw r x y)))
+
+(define-vop (fast-*-c/unsigned=>unsigned fast-unsigned-binop-c)
+  (:translate *)
+  (:generator 3
+    (inst mulli r x y)))
+\f
 ;;; Shifting
 
+(macrolet ((def (name sc-type type result-type cost)
+            `(define-vop (,name)
+               (:note "inline ASH")
+               (:translate ash)
+               (:args (number :scs (,sc-type))
+                      (amount :scs (signed-reg unsigned-reg immediate)))
+               (:arg-types ,type positive-fixnum)
+               (:results (result :scs (,result-type)))
+               (:result-types ,type)
+               (:policy :fast-safe)
+               (:generator ,cost
+                  (sc-case amount
+                    ((signed-reg unsigned-reg) 
+                     (inst slw result number amount))
+                    (immediate
+                     (let ((amount (tn-value amount)))
+                       (aver (> amount 0))
+                       (inst slwi result number amount))))))))
+  ;; FIXME: There's the opportunity for a sneaky optimization here, I
+  ;; think: a FAST-ASH-LEFT-C/FIXNUM=>SIGNED vop.  -- CSR, 2003-09-03
+  (def fast-ash-left/fixnum=>fixnum any-reg tagged-num any-reg 2)
+  (def fast-ash-left/signed=>signed signed-reg signed-num signed-reg 3)
+  (def fast-ash-left/unsigned=>unsigned unsigned-reg unsigned-num unsigned-reg 3))
+
 (define-vop (fast-ash/unsigned=>unsigned)
   (:note "inline ASH")
   (:args (number :scs (unsigned-reg) :to :save)
         (inst cmpwi ndesc 31)
         (inst srw result number ndesc)
         (inst ble done)
-        (inst srwi result number 31)
+        (move result zero-tn)
         (inst b done)
 
         (emit-label positive)
         (inst slw result number amount)
 
         (emit-label done)))
-
       (immediate
        (let ((amount (tn-value amount)))
-        (if (minusp amount)
-            (let ((amount (min 31 (- amount))))
-              (inst srwi result number amount))
-            (inst slwi result number amount)))))))
-
+        (cond
+         ((and (minusp amount) (< amount -31)) (move result zero-tn))
+         ((minusp amount) (inst srwi result number (- amount)))
+         (t (inst slwi result number amount))))))))
 
 (define-vop (fast-ash/signed=>signed)
   (:note "inline ASH")
   (:affected)
   (:policy :fast-safe))
 
-(deftype integer-with-a-bite-out (s bite)
-  (cond ((eq s '*) 'integer)
-       ((and (integerp s) (> s 1))
-        (let ((bound (ash 1 (1- s))))
-          `(integer ,(- bound) ,(- bound bite 1))))
-       (t
-        (error "Bad size specified for SIGNED-BYTE type specifier: ~S." s))))
-
 (define-vop (fast-conditional/fixnum fast-conditional)
   (:args (x :scs (any-reg zero))
         (y :scs (any-reg zero)))
 (define-vop (bignum-mult)
   (:translate sb!bignum::%multiply)
   (:policy :fast-safe)
-  (:args (x :scs (unsigned-reg) :to (:result 1))
-        (y :scs (unsigned-reg) :to (:result 1)))
+  (:args (x :scs (unsigned-reg) :to (:eval 1))
+        (y :scs (unsigned-reg) :to (:eval 1)))
   (:arg-types unsigned-num unsigned-num)
-  (:results (hi :scs (unsigned-reg))
-           (lo :scs (unsigned-reg)))
+  (:results (hi :scs (unsigned-reg) :from (:eval 1))
+           (lo :scs (unsigned-reg) :from (:eval 0)))
   (:result-types unsigned-num unsigned-num)
   (:generator 40
     (inst mullw lo x y)
 (define-static-fun two-arg-and (x y) :translate logand)
 (define-static-fun two-arg-ior (x y) :translate logior)
 (define-static-fun two-arg-xor (x y) :translate logxor)
+\f
+(in-package "SB!C")
+
+(deftransform * ((x y)
+                ((unsigned-byte 32) (constant-arg (unsigned-byte 32)))
+                (unsigned-byte 32))
+  "recode as shifts and adds"
+  (let ((y (lvar-value y)))
+    (multiple-value-bind (result adds shifts)
+       (ub32-strength-reduce-constant-multiply 'x y)
+      (cond
+       ((typep y '(signed-byte 16))
+       ;; a mulli instruction has a latency of 5.
+       (when (> (+ adds shifts) 4)
+         (give-up-ir1-transform)))
+       (t
+       ;; a mullw instruction also has a latency of 5, plus two
+       ;; instructions (in general) to load the immediate into a
+       ;; register.
+       (when (> (+ adds shifts) 6)
+         (give-up-ir1-transform))))
+      (or result 0))))