PPU scalar floating point and the FPSCR.
0/1 a fixed bit, name a field, a bit the decoder ignores
fres frt frb
Floating Reciprocal Estimate Single
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 0 | 1 | 1 | frt | 0 | 0 | 0 | 0 | 0 | frb | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | rc | ||||||||
(defppu fres ((59 6) "frt:5 00000 frb:5 00000" (24 5) "rc:1") (:asm "fres" frt frb)
(:doc "Floating Reciprocal Estimate Single")
(fp-arith 1 (let ((b (fpr frb)))
(if (or (fnan-p :f64 b) (finf-p :f64 b) (fzero-p :f64 b))
(fp-div-op cpu 1 #x3FF0000000000000 b)
(ppc-model-feature-case
(:estimate-tables (fres-estimate cpu b))
(t (fp-div-op cpu 1 #x3FF0000000000000 b)))))))
frsqrte frt frb
Floating Reciprocal Square Root Estimate
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | frt | 0 | 0 | 0 | 0 | 0 | frb | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | rc | ||||||||
(defppu frsqrte ((63 6) "frt:5 00000 frb:5 00000" (26 5) "rc:1") (:asm "frsqrte" frt frb)
(:doc "Floating Reciprocal Square Root Estimate")
(fp-arith 0 (let ((b (fpr frb)))
(cond ((fnan-p :f64 b) (fp-nan cpu 0 b b b))
((fzero-p :f64 b) (fp-raise cpu +zx+) (setf (ppu-fpx cpu) 0)
(logior +f64-inf+ (ash (fsign :f64 b) 63)))
((= (fsign :f64 b) 1) (fp-invalid cpu +vxsqrt+))
((finf-p :f64 b) (setf (ppu-fpx cpu) 0) 0)
(t (ppc-model-feature-case
(:estimate-tables (frsqrte-estimate cpu b))
(t (fp-div-op cpu 0 #x3FF0000000000000 (fp-sqrt-op cpu 0 b)))))))))
fsel frt fra frc frb
Floating Select
frC if frA >= 0 (-0 included), else frB; a NaN frA selects frB (5.2.2, p. 126)
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | frt | fra | frb | frc | 1 | 0 | 1 | 1 | 1 | rc | ||||||||||||||||
(defppu fsel ((63 6) "frt:5 fra:5 frb:5 frc:5" (23 5) "rc:1") (:asm "fsel" frt fra frc frb)
(:doc "Floating Select")
(setf (fpr frt) (if (= (fle :f64 0 (fpr fra)) 1) (fpr frc) (fpr frb)))
(rc-cr1))
frsp frt frb
Floating Round to Single-Precision
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | frt | 0 | 0 | 0 | 0 | 0 | frb | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | rc | ||||||||
(defppu frsp ((63 6) "frt:5 00000 frb:5" (12 10) "rc:1") (:asm "frsp" frt frb)
(:doc "Floating Round to Single-Precision")
(fp-arith 1 (let ((b (fpr frb)))
(if (fnan-p :f64 b)
(fp-nan cpu 1 b b b)
(fp-to-single cpu (fpscr-rm) b 0)))))
fctiw frt frb
Floating Convert To Integer Word
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | frt | 0 | 0 | 0 | 0 | 0 | frb | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | rc | ||||||||
(defppu fctiw ((63 6) "frt:5 00000 frb:5" (14 10) "rc:1") (:asm "fctiw" frt frb)
(:doc "Floating Convert To Integer Word")
(fp-to-int 32 (fpscr-rm)))
fctiwz frt frb
Floating Convert To Integer Word with round toward Zero
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | frt | 0 | 0 | 0 | 0 | 0 | frb | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | rc | ||||||||
(defppu fctiwz ((63 6) "frt:5 00000 frb:5" (15 10) "rc:1") (:asm "fctiwz" frt frb)
(:doc "Floating Convert To Integer Word with round toward Zero")
(fp-to-int 32 :rtz))
fctid frt frb
Floating Convert To Integer Doubleword
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | frt | 0 | 0 | 0 | 0 | 0 | frb | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 0 | rc | ||||||||
(defppu fctid ((63 6) "frt:5 00000 frb:5" (814 10) "rc:1") (:asm "fctid" frt frb)
(:doc "Floating Convert To Integer Doubleword")
(fp-to-int 64 (fpscr-rm)))
fctidz frt frb
Floating Convert To Integer Doubleword with round toward Zero
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | frt | 0 | 0 | 0 | 0 | 0 | frb | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | rc | ||||||||
(defppu fctidz ((63 6) "frt:5 00000 frb:5" (815 10) "rc:1") (:asm "fctidz" frt frb)
(:doc "Floating Convert To Integer Doubleword with round toward Zero")
(fp-to-int 64 :rtz))
fcfid frt frb
Floating Convert From Integer Doubleword
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | frt | 0 | 0 | 0 | 0 | 0 | frb | 1 | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | rc | ||||||||
(defppu fcfid ((63 6) "frt:5 00000 frb:5" (846 10) "rc:1") (:asm "fcfid" frt frb)
(:doc "Floating Convert From Integer Doubleword")
(fp-arith 0 (fp-round-op 0 int-to-f (sext 64 (fpr frb)))))
fcmpu bf fra frb
Floating Compare Unordered
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | bf | fra | frb | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |||||||||||||
(defppu fcmpu ((63 6) "bf:3 .. fra:5 frb:5" (0 10) ".") (:asm "fcmpu" bf fra frb)
(:doc "Floating Compare Unordered")
(let ((a (fpr fra)) (b (fpr frb)))
(fp-compare a b)
(when (or (fsnan-p :f64 a) (fsnan-p :f64 b)) (fp-raise cpu +vxsnan+))
(fpscr-summary cpu)))
fcmpo bf fra frb
Floating Compare Ordered
VXVC for any NaN; with a signalling one only when VE = 0
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | bf | fra | frb | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | |||||||||||||
(defppu fcmpo ((63 6) "bf:3 .. fra:5 frb:5" (32 10) ".") (:asm "fcmpo" bf fra frb)
(:doc "Floating Compare Ordered")
(let ((a (fpr fra)) (b (fpr frb)))
(fp-compare a b)
(cond ((or (fsnan-p :f64 a) (fsnan-p :f64 b))
(fp-raise cpu +vxsnan+)
(when (= (fpscr-bit +ve+) 0) (fp-raise cpu +vxvc+)))
((any-nan a b) (fp-raise cpu +vxvc+)))
(fpscr-summary cpu)))
mffs frt
Move From FPSCR
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | frt | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | rc | ||||||||||||||
(defppu mffs ((63 6) "frt:5 ..... ....." (583 10) "rc:1") (:asm "mffs" frt)
(:doc "Move From FPSCR")
(setf (fpr frt) (ppu-fpscr cpu))
(rc-cr1))
mtfsf flm frb
Move To FPSCR Fields
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | flm | frb | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | rc | |||||||||||||
(defppu mtfsf ((63 6) ". flm:8 . frb:5" (711 10) "rc:1") (:asm "mtfsf" flm frb)
(:doc "Move To FPSCR Fields")
(let ((v (wrap 32 (fpr frb))))
(dotimes (i 8)
(when (= (msb-bit 8 flm i) 1)
(set-fpscr-field i (msb-field 32 v (* 4 i) 4))))
(fpscr-summary cpu)
(rc-cr1)))
mtfsfi bf u
Move To FPSCR Field Immediate
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | bf | u | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | rc | |||||||||||||
(defppu mtfsfi ((63 6) "bf:3 ....... u:4 ." (134 10) "rc:1") (:asm "mtfsfi" bf u)
(:doc "Move To FPSCR Field Immediate")
(set-fpscr-field bf u)
(fpscr-summary cpu)
(rc-cr1))
mtfsb0 bt
Move To FPSCR Bit 0
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | bt | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | rc | ||||||||||||||
(defppu mtfsb0 ((63 6) "bt:5 ..... ....." (70 10) "rc:1") (:asm "mtfsb0" bt)
(:doc "Move To FPSCR Bit 0")
(set-fpscr-bit bt 0)
(fpscr-summary cpu)
(rc-cr1))
mtfsb1 bt
Move To FPSCR Bit 1
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | bt | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | rc | ||||||||||||||
(defppu mtfsb1 ((63 6) "bt:5 ..... ....." (38 10) "rc:1") (:asm "mtfsb1" bt)
(:doc "Move To FPSCR Bit 1")
(if (= (msb-bit 32 +fpscr-exc-mask+ bt) 1) (fp-raise cpu bt) (set-fpscr-bit bt 1))
(fpscr-summary cpu)
(rc-cr1))
mcrfs bf bfa
Move to Condition Register from FPSCR
CR field bf <- FPSCR field bfa; the exception bits copied are cleared
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | bf | bfa | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | ||||||||||||||
(defppu mcrfs ((63 6) "bf:3 .. bfa:3 .. ....." (64 10) ".") (:asm "mcrfs" bf bfa)
(:doc "Move to Condition Register from FPSCR")
(set-cr-field bf (msb-field 32 (ppu-fpscr cpu) (* 4 bfa) 4))
(setf (ppu-fpscr cpu)
(logand (ppu-fpscr cpu)
(wrap 32 (lognot (logand (ash #xF (* 4 (- 7 bfa)))
(logior +fpscr-exc-mask+ #x80000000))))))
(fpscr-summary cpu))
(defconstant +fx+) source(defconstant +fx+ 0)(defconstant +fex+) source(defconstant +fex+ 1)(defconstant +vx+) source(defconstant +vx+ 2)(defconstant +ox+) source(defconstant +ox+ 3)(defconstant +ux+) source(defconstant +ux+ 4)(defconstant +zx+) source(defconstant +zx+ 5)(defconstant +xx+) source(defconstant +xx+ 6)(defconstant +vxsnan+) source(defconstant +vxsnan+ 7)(defconstant +vxisi+) source(defconstant +vxisi+ 8)(defconstant +vxidi+) source(defconstant +vxidi+ 9)(defconstant +vxzdz+) source(defconstant +vxzdz+ 10)(defconstant +vximz+) source(defconstant +vximz+ 11)(defconstant +vxvc+) source(defconstant +vxvc+ 12)(defconstant +fr+) source(defconstant +fr+ 13)(defconstant +fi+) source(defconstant +fi+ 14)(defconstant +vxsoft+) source(defconstant +vxsoft+ 21)(defconstant +vxsqrt+) source(defconstant +vxsqrt+ 22)(defconstant +vxcvi+) source(defconstant +vxcvi+ 23)(defconstant +ve+) source(defconstant +ve+ 24)(defconstant +oe+) source(defconstant +oe+ 25)(defconstant +ue+) source(defconstant +ue+ 26)(defconstant +ze+) source(defconstant +ze+ 27)(defconstant +xe+) source(defconstant +xe+ 28)(defmacro fpscr-bit) source(defmacro fpscr-bit (i) `(msb-bit 32 (ppu-fpscr cpu) ,i))(defmacro set-fpscr-bit) source(defmacro set-fpscr-bit (i v) `(setf (ppu-fpscr cpu) (dpb ,v (byte 1 (- 31 ,i)) (ppu-fpscr cpu))))(defmacro fpscr-rn) source(defmacro fpscr-rn () `(bit-field (ppu-fpscr cpu) 0 2))(defconstant +fpscr-exc-mask+) source(defconstant +fpscr-exc-mask+ #x1FF80700)(defmacro fpscr-rm) source(defmacro fpscr-rm () `(let ((rn (fpscr-rn))) (if (= rn 0) 0 (+ rn 1))))(defconstant +f64-qnan+) source(defconstant +f64-qnan+ #x7FF8000000000000)(defconstant +f64-inf+) source(defconstant +f64-inf+ #x7FF0000000000000)(defconstant +fpx-fr+) source(defconstant +fpx-fr+ 32)(defconstant +fpx-cvi+) source(defconstant +fpx-cvi+ 64)(defmacro set-fpx) source(defmacro set-fpx (flags fr) `(setf (ppu-fpx cpu) (logior ,flags (ash ,fr 5))))(defun fpscr-summary) source(defun fpscr-summary (cpu)
(declare (type ppu cpu))
;; VX: any VX* bit. FEX: (VX&VE) | (OX&OE) | (UX&UE) | (ZX&ZE) | (XX&XE);
;; the enables VE..XE (bits 24..28) are in the order of VX..XX (2..6)
(let* ((f (ppu-fpscr cpu))
(vx (b2i (/= (logand f #x01F80700) 0)))
(fex (b2i (/= (logand (bit-field f 3 5) (logior (ash vx 4) (bit-field f 25 4))) 0))))
(setf (ppu-fpscr cpu) (dpb vx (byte 1 29) (dpb fex (byte 1 30) f)))))(defun fp-raise) source(defun fp-raise (cpu i)
(declare (type ppu cpu) (type (unsigned-byte 5) i))
(when (= (fpscr-bit i) 0)
(set-fpscr-bit i 1)
(set-fpscr-bit +fx+ 1)))(defun fprf-class) source(defun fprf-class (sgl d)
(declare (type (unsigned-byte 1) sgl) (type (unsigned-byte 64) d))
(let ((neg (= (fsign :f64 d) 1)))
(cond ((fnan-p :f64 d) #b10001)
((finf-p :f64 d) (if neg #b01001 #b00101))
((fzero-p :f64 d) (if neg #b10010 #b00010))
((if (= sgl 1) (< (bit-field d 52 11) 897) (fsubnormal-p :f64 d))
(if neg #b11000 #b10100))
(t (if neg #b01000 #b00100)))))(defun fp-commit) source(defun fp-commit (cpu sgl r)
(declare (type ppu cpu) (type (unsigned-byte 1) sgl) (type (unsigned-byte 64) r))
(let* ((x (ppu-fpx cpu)) (fi (b2i (/= (logand x +fflag-inexact+) 0))))
(set-fpscr-bit +fr+ (b2i (/= (logand x +fpx-fr+) 0)))
(set-fpscr-bit +fi+ fi)
(when (= fi 1) (fp-raise cpu +xx+))
(when (/= (logand x +fflag-overflow+) 0) (fp-raise cpu +ox+))
(when (/= (logand x +fflag-underflow+) 0) (fp-raise cpu +ux+))
(when (/= (logand x +fflag-divzero+) 0) (fp-raise cpu +zx+))
(setf (ppu-fpscr cpu) (dpb (fprf-class sgl r) (byte 5 12) (ppu-fpscr cpu)))
(fpscr-summary cpu)
r))(defmacro rc-cr1) source(defmacro rc-cr1 () `(when (= rc 1) (set-cr-field 1 (msb-field 32 (ppu-fpscr cpu) 0 4))))(defun fp-invalid) source(defun fp-invalid (cpu i)
(declare (type ppu cpu))
(fp-raise cpu i)
(setf (ppu-fpx cpu) 0)
+f64-qnan+)(defun fp-nan) source(defun fp-nan (cpu sgl a b c)
(declare (type ppu cpu) (type (unsigned-byte 64) a b c))
(when (or (fsnan-p :f64 a) (fsnan-p :f64 b) (fsnan-p :f64 c)) (fp-raise cpu +vxsnan+))
(setf (ppu-fpx cpu) 0)
(let ((n (fquiet :f64 (cond ((fnan-p :f64 a) a) ((fnan-p :f64 b) b) (t c)))))
(if (= sgl 1) (logand n #xFFFFFFFFE0000000) n)))(defmacro any-nan) source(defmacro any-nan (&rest xs) `(or ,@(mapcar (lambda (x) `(fnan-p :f64 ,x)) xs)))(defun fp-to-single) source(defun fp-to-single (cpu rm d prior)
(declare (type ppu cpu) (type (unsigned-byte 3) rm) (type (unsigned-byte 64) d))
(let ((s (fcvt :f32 :f64 rm d)))
(set-fpx (logior (logand prior (logior +fflag-invalid+ +fflag-divzero+))
(fflags fcvt :f32 :f64 rm d :tiny :before))
(b2i (/= s (fcvt :f32 :f64 :rtz d))))
(fcvt :f64 :f32 :rne s)))(defmacro fp-round-op) source(defmacro fp-round-op (sgl op &rest args)
(let ((vars (loop repeat (length args) collect (gensym "X"))))
`(let ((rm (fpscr-rm)) ,@(mapcar #'list vars args))
(if (= ,sgl 1)
(let* ((z (,op :f64 :rtz ,@vars))
(zf (fflags ,op :f64 :rtz ,@vars))
(odd (cond ((/= (logand zf +fflag-inexact+) 0) (logior z 1))
((fzero-p :f64 z) (,op :f64 rm ,@vars))
(t z))))
(fp-to-single cpu rm odd zf))
(let* ((r (,op :f64 rm ,@vars))
(f (fflags ,op :f64 rm ,@vars :tiny :before)))
(set-fpx f (if (= (logand f +fflag-inexact+) 0) 0 (b2i (/= r (,op :f64 :rtz ,@vars)))))
r)))))(defun fp-add-op) source(defun fp-add-op (cpu sgl a b sub)
(declare (type ppu cpu) (type (unsigned-byte 64) a b) (type (unsigned-byte 1) sub))
(cond ((any-nan a b) (fp-nan cpu sgl a b b))
((and (finf-p :f64 a) (finf-p :f64 b) (/= (fsign :f64 a) (logxor (fsign :f64 b) sub)))
(fp-invalid cpu +vxisi+))
((= sub 1) (fp-round-op sgl fsub a b))
(t (fp-round-op sgl fadd a b))))(defun fp-mul-op) source(defun fp-mul-op (cpu sgl a c)
(declare (type ppu cpu) (type (unsigned-byte 64) a c))
(cond ((any-nan a c) (fp-nan cpu sgl a c c))
((or (and (finf-p :f64 a) (fzero-p :f64 c)) (and (fzero-p :f64 a) (finf-p :f64 c)))
(fp-invalid cpu +vximz+))
(t (fp-round-op sgl fmul a c))))(defun fp-div-op) source(defun fp-div-op (cpu sgl a b)
(declare (type ppu cpu) (type (unsigned-byte 64) a b))
(cond ((any-nan a b) (fp-nan cpu sgl a b b))
((and (finf-p :f64 a) (finf-p :f64 b)) (fp-invalid cpu +vxidi+))
((and (fzero-p :f64 a) (fzero-p :f64 b)) (fp-invalid cpu +vxzdz+))
(t (fp-round-op sgl fdiv a b))))(defun fp-fma-op) source(defun fp-fma-op (cpu sgl a c b nb)
(declare (type ppu cpu) (type (unsigned-byte 64) a c b nb))
(cond ((any-nan a nb c) (fp-nan cpu sgl a nb c))
((or (and (finf-p :f64 a) (fzero-p :f64 c)) (and (fzero-p :f64 a) (finf-p :f64 c)))
(fp-invalid cpu +vximz+))
((and (or (finf-p :f64 a) (finf-p :f64 c)) (finf-p :f64 b)
(/= (logxor (fsign :f64 a) (fsign :f64 c)) (fsign :f64 b)))
(fp-invalid cpu +vxisi+))
(t (fp-round-op sgl ffma a c b))))(defun fp-sqrt-op) source(defun fp-sqrt-op (cpu sgl b)
(declare (type ppu cpu) (type (unsigned-byte 64) b))
(cond ((fnan-p :f64 b) (fp-nan cpu sgl b b b))
((and (= (fsign :f64 b) 1) (not (fzero-p :f64 b))) (fp-invalid cpu +vxsqrt+))
(t (fp-round-op sgl fsqrt b))))(defmacro fp-arith) source(defmacro fp-arith (sgl expr)
`(progn (setf (fpr frt) (fp-commit cpu ,sgl ,expr)) (rc-cr1)))(defmacro def-fp-arith) source(defmacro def-fp-arith (name dname op xo fields asm-ops doc expr)
`(progn
(defppu ,name ((63 6) ,fields (,xo 5) "rc:1") (:asm ,(ppu-mnemonic name) ,@asm-ops) (:doc ,doc)
(let ((sgl 0)) (fp-arith sgl ,expr)))
,@(when dname
`((defppu ,dname ((59 6) ,fields (,xo 5) "rc:1") (:asm ,(ppu-mnemonic dname) ,@asm-ops)
(:doc ,(concatenate 'string doc " Single"))
(let ((sgl 1)) (fp-arith sgl ,expr)))))))(defmacro neg-result) source(defmacro neg-result (r)
(let ((v (gensym "R"))) `(let ((,v ,r)) (if (fnan-p :f64 ,v) ,v (fneg :f64 ,v)))))(defparameter *fres-mantissas*) source(defparameter *fres-mantissas*
(make-array 128 :element-type '(unsigned-byte 32) :initial-contents
'(#x7F0000 #x7D0800 #x7B1800 #x793000 #x775000 #x757000 #x73A000 #x71E000
#x700000 #x6E4000 #x6CA000 #x6AE000 #x694000 #x678000 #x660000 #x646000
#x62C000 #x614000 #x5FC000 #x5E4000 #x5CC000 #x5B4000 #x59C000 #x584000
#x570000 #x558000 #x540000 #x52C000 #x518000 #x500000 #x4EC000 #x4D8000
#x4C0000 #x4B0000 #x498000 #x488000 #x474000 #x460000 #x44C000 #x438000
#x428000 #x418000 #x400000 #x3F0000 #x3E0000 #x3D0000 #x3BC000 #x3AC000
#x398000 #x388000 #x378000 #x368000 #x358000 #x348000 #x338000 #x328000
#x318000 #x308000 #x2F8000 #x2EC000 #x2E0000 #x2D0000 #x2C0000 #x2B0000
#x2A0000 #x298000 #x288000 #x278000 #x26C000 #x260000 #x250000 #x244000
#x238000 #x228000 #x220000 #x210000 #x200000 #x1F8000 #x1E8000 #x1E0000
#x1D0000 #x1C8000 #x1B8000 #x1B0000 #x1A0000 #x198000 #x190000 #x180000
#x178000 #x168000 #x160000 #x158000 #x148000 #x140000 #x138000 #x128000
#x120000 #x118000 #x108000 #x100000 #x0F8000 #x0F0000 #x0E0000 #x0D8000
#x0D0000 #x0C8000 #x0B8000 #x0B0000 #x0A8000 #x0A0000 #x098000 #x090000
#x080000 #x078000 #x070000 #x068000 #x060000 #x058000 #x050000 #x048000
#x040000 #x038000 #x030000 #x028000 #x020000 #x018000 #x010000 #x000000)))(defparameter *frsqrte-mantissas*) source(defparameter *frsqrte-mantissas*
(make-array 16 :element-type '(unsigned-byte 32) :initial-contents
'(#xF1000 #xD8000 #xC0000 #xA8000 #x98000 #x88000 #x80000 #x70000
#x60000 #x4C000 #x3C000 #x30000 #x20000 #x18000 #x10000 #x08000)))(defmacro with-normal-double) source(defmacro with-normal-double ((exp sig) b &body body)
(let ((s (gensym "S")) (n (gensym "N")) (e (gensym "E")) (v (gensym "B")))
`(let* ((,v ,b) (,e (bit-field ,v 52 11))
(,s (if (= ,e 0) (bit-field ,v 0 52) (logior (bit-field ,v 0 52) (ash 1 52))))
(,n (integer-length ,s))
(,exp (+ (if (= ,e 0) -1074 (- ,e 1075)) ,n -1))
(,sig (ash ,s (- 53 ,n))))
,@body)))(defun fres-estimate) source(defun fres-estimate (cpu b)
(declare (type ppu cpu) (type (unsigned-byte 64) b))
(setf (ppu-fpx cpu) 0)
(with-normal-double (e sig) b
(let ((s (ash (fsign :f64 b) 63)))
(cond ((>= e 128) s)
((<= e -129) (logior s +f64-inf+))
(t (fcvt :f64 :f32 :rne
(fcvt :f32 :f64 :rne
(logior s (ash (- 1022 e) 52)
(ash (aref *fres-mantissas* (bit-field sig 45 7)) 29)))))))))(defun frsqrte-estimate) source(defun frsqrte-estimate (cpu b)
(declare (type ppu cpu) (type (unsigned-byte 64) b))
(setf (ppu-fpx cpu) 0)
(with-normal-double (e sig) b
(logior (ash (- 1022 (ash e -1)) 52)
(ash (aref *frsqrte-mantissas* (logior (ash (logand e 1) 3) (bit-field sig 49 3))) 32))))(defmacro def-fp-move) source(defmacro def-fp-move (name xo doc expr)
`(defppu ,name ((63 6) "frt:5 00000 frb:5" (,xo 10) "rc:1") (:asm ,(ppu-mnemonic name) frt frb)
(:doc ,doc)
(let ((b (fpr frb))) (setf (fpr frt) ,expr) (rc-cr1))))(defmacro fp-to-int) source(defmacro fp-to-int (width rm)
(let ((lo (- (ash 1 (- width 1)))) (hi (- (ash 1 (- width 1)) 1)))
`(let* ((b (fpr frb))
(rm ,(if (keywordp rm) (position rm *rounding-modes*) rm))
(r (if (fnan-p :f64 b)
(progn (when (fsnan-p :f64 b) (fp-raise cpu +vxsnan+))
(setf (ppu-fpx cpu) +fpx-cvi+)
,lo)
(let ((v (fcvt-to-int :f64 rm b ,lo ,hi))
(f (fflags fcvt-to-int :f64 rm b ,lo ,hi)))
(if (/= (logand f +fflag-invalid+) 0)
(setf (ppu-fpx cpu) +fpx-cvi+)
(set-fpx f (b2i (/= v (fcvt-to-int :f64 :rtz b ,lo ,hi)))))
v))))
(if (/= (logand (ppu-fpx cpu) +fpx-cvi+) 0)
(progn (fp-raise cpu +vxcvi+) (set-fpscr-bit +fr+ 0) (set-fpscr-bit +fi+ 0))
(let ((fi (b2i (/= (logand (ppu-fpx cpu) +fflag-inexact+) 0))))
(set-fpscr-bit +fr+ (b2i (/= (logand (ppu-fpx cpu) +fpx-fr+) 0)))
(set-fpscr-bit +fi+ fi)
(when (= fi 1) (fp-raise cpu +xx+))))
(fpscr-summary cpu)
;; fctiw's high word, which Book I leaves undefined, is the model's:
;; with :fctiw-sext (the Cell) the sign extension of the word (seen
;; on a PS3: [PS3AUTO] ppu_float_conversion), else zero
(setf (fpr frt) ,(if (= width 32)
'(ppc-model-feature-case
(:fctiw-sext (wrap 64 r))
(t (wrap 32 r)))
'(wrap 64 r)))
(rc-cr1))))(defmacro fp-compare) source(defmacro fp-compare (a b)
`(let* ((x ,a) (y ,b)
(c (cond ((= (funord :f64 x y) 1) #b0001)
((= (flt :f64 x y) 1) #b1000)
((= (flt :f64 y x) 1) #b0100)
(t #b0010))))
(setf (ppu-fpscr cpu) (dpb c (byte 4 12) (ppu-fpscr cpu)))
(set-cr-field bf c)
c))(defmacro set-fpscr-field) source(defmacro set-fpscr-field (n v)
`(setf (ppu-fpscr cpu) (dpb ,v (byte 4 (* 4 (- 7 ,n))) (ppu-fpscr cpu))))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.