mips: eefloat.lisp

the Emotion Engine's single precision as rules around the IEEE primitives: the arithmetic of its FPU (COP1) and of the VUs.

Other definitions

(defconstant +ee-max+) source
(defconstant +ee-max+ #x7FFFFFFF "the largest magnitude")
(defconstant +ef-o+) source
(defconstant +ef-o+ 1)
(defconstant +ef-u+) source
(defconstant +ef-u+ 2)
(defconstant +ef-d+) source
(defconstant +ef-d+ 4)
(defconstant +ef-i+) source
(defconstant +ef-i+ 8)
(defmacro ef-sign) source
(defmacro ef-sign (x) `(bit-at ,x 31))
(defmacro ef-bexp) source
(defmacro ef-bexp (x) `(bit-field ,x 23 8))
(defmacro ef-mant) source
(defmacro ef-mant (x) `(if (= (bit-field ,x 23 8) 0) 0 (logior (bit-field ,x 0 23) #x800000)))
(defmacro ef-zero-p) source
(defmacro ef-zero-p (x) `(= (bit-field ,x 23 8) 0))
(defun ee-f64) source
(defun ee-f64 (x)
  "the IEEE double of the EE single X, exactly: exponent 255 is a normal
   exponent, exponent 0 a zero of X's sign"
  (declare (type (unsigned-byte 32) x))
  (if (ef-zero-p x)
      (ash (ef-sign x) 63)
      (logior (ash (ef-sign x) 63) (ash (+ (ef-bexp x) 896) 52) (ash (bit-field x 0 23) 29))))
(defun ee-pack) source
(defun ee-pack (r)
  "the double R (finite, not below 2^-1022 unless zero) as an EE single
   truncated to 24 bits, overflow +-MAX with EF-O, below 2^-126 a zero of
   its sign with EF-U; the flags at bit 32"
  (declare (type (unsigned-byte 64) r))
  (let ((s (bit-at r 63)) (e (- (bit-field r 52 11) 896)))
    (cond ((fzero-p :f64 r) (ash s 31))
          ((> e 255) (logior (ash s 31) +ee-max+ (ash +ef-o+ 32)))
          ((< e 1) (logior (ash s 31) (ash +ef-u+ 32)))
          (t (logior (ash s 31) (ash e 23) (bit-field r 29 23))))))
(defun ee-fadd) source
(defun ee-fadd (a b)
  "A + B"
  (declare (type (unsigned-byte 32) a b))
  (ee-pack (fadd :f64 :rtz (ee-f64 a) (ee-f64 b))))
(defun ee-fneg) source
(defun ee-fneg (a)
  (declare (type (unsigned-byte 32) a))
  (fneg :f32 a))
(defun ee-fsub) source
(defun ee-fsub (a b)
  "A - B"
  (declare (type (unsigned-byte 32) a b))
  (ee-pack (fsub :f64 :rtz (ee-f64 a) (ee-f64 b))))
(defun ee-fmul) source
(defun ee-fmul (a b)
  "A * B (exact as a double: truncated once, by EE-PACK)"
  (declare (type (unsigned-byte 32) a b))
  (ee-pack (fmul :f64 :rtz (ee-f64 a) (ee-f64 b))))
(defun ee-fdiv) source
(defun ee-fdiv (a b)
  "A / B; by zero +-MAX with EF-D, or EF-I for 0 / 0"
  (declare (type (unsigned-byte 32) a b))
  (if (ef-zero-p b)
      (logior (ash (logxor (ef-sign a) (ef-sign b)) 31) +ee-max+ (ash (if (ef-zero-p a) +ef-i+ +ef-d+) 32))
      (ee-pack (fdiv :f64 :rtz (ee-f64 a) (ee-f64 b)))))
(defun ee-fsqrt) source
(defun ee-fsqrt (b)
  "the square root of B; of the magnitude with EF-I when B is negative; a
   zero is kept with its sign"
  (declare (type (unsigned-byte 32) b))
  (if (ef-zero-p b)
      (ash (ef-sign b) 31)
      (logior (ee-pack (fsqrt :f64 :rtz (fabs :f64 (ee-f64 b))))
              (if (= (ef-sign b) 1) (ash +ef-i+ 32) 0))))
(defun ee-frsqrt) source
(defun ee-frsqrt (a b)
  "A / sqrt(B), truncated once (as sqrt(A * A / B), see above); B zero:
   +-MAX (B's sign) with EF-D; B negative: of its magnitude with EF-I (as
   PCSX2's RSQRT_S)"
  (declare (type (unsigned-byte 32) a b))
  (let ((flag (if (= (ef-sign b) 1) (ash +ef-i+ 32) 0)))
    (cond ((ef-zero-p b) (logior (ash (ef-sign b) 31) +ee-max+ (ash +ef-d+ 32)))
          ((ef-zero-p a) (logior (ash (ef-sign a) 31) flag))
          (t (let* ((x (ee-f64 a))
                    (q (fdiv :f64 :rtz (fmul :f64 :rtz x x) (fabs :f64 (ee-f64 b)))))
               (logior (ee-pack (fcopysign :f64 (fsqrt :f64 :rtz q) x)) flag))))))
(defun ee-fcvt-sw) source
(defun ee-fcvt-sw (x)
  "the 32-bit signed integer X as a single, truncated (always an IEEE
   normal single: no flags)"
  (declare (type (unsigned-byte 32) x))
  (int-to-f :f32 :rtz (sext 32 x)))
(defun ee-fcvt-ws) source
(defun ee-fcvt-ws (x)
  "single X as a 32-bit signed integer, truncated toward zero; beyond the
   range #x7FFFFFFF or #x80000000 by its sign"
  (declare (type (unsigned-byte 32) x))
  (wrap 32 (fcvt-to-int :f64 :rtz (ee-f64 x) -2147483648 2147483647)))
(defmacro ee-feq) source
(defmacro ee-feq (a b) `(feq :f64 (ee-f64 ,a) (ee-f64 ,b)))
(defmacro ee-flt) source
(defmacro ee-flt (a b) `(flt :f64 (ee-f64 ,a) (ee-f64 ,b)))
(defmacro ee-fle) source
(defmacro ee-fle (a b) `(fle :f64 (ee-f64 ,a) (ee-f64 ,b)))
(defun ee-fmax) source
(defun ee-fmax (a b)
  "the larger of A and B as the EE compares them (sign and magnitude, on
   the bits as they are; PCSX2 fp_max)"
  (declare (type (unsigned-byte 32) a b))
  (let ((x (sext 32 a)) (y (sext 32 b)))
    (if (and (< x 0) (< y 0))
        (if (< x y) a b)
        (if (> x y) a b))))
(defun ee-fmin) source
(defun ee-fmin (a b)
  (declare (type (unsigned-byte 32) a b))
  (let ((x (sext 32 a)) (y (sext 32 b)))
    (if (and (< x 0) (< y 0))
        (if (> x y) a b)
        (if (< x y) a b))))
(defun ee-ftoi) source
(defun ee-ftoi (x n)
  (declare (type (unsigned-byte 32) x) (type (unsigned-byte 4) n))
  (wrap 32 (fcvt-to-int :f64 :rtz (fmul :f64 :rne (ee-f64 x) (ash (+ 1023 n) 52)) -2147483648 2147483647)))
(defun ee-itof) source
(defun ee-itof (x n)
  (declare (type (unsigned-byte 32) x) (type (unsigned-byte 4) n))
  (wrap 32 (ee-pack (fmul :f64 :rne (int-to-f :f64 :rne (sext 32 x)) (ash (- 1023 n) 52)))))

Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.