frontend: pandora: work around r20a vs r21 versioning
[pcsx_rearmed.git] / libpcsxcore / gte.c
index 9909e18..62fc7f3 100644 (file)
 
 #ifndef FLAGLESS
 
-static inline s64 BOUNDS_(psxCP2Regs *regs, s64 n_value, s64 n_max, int n_maxflag, s64 n_min, int n_minflag) {
+static inline s32 BOUNDS_(psxCP2Regs *regs, s64 n_value, s64 n_max, int n_maxflag, s64 n_min, int n_minflag) {
        if (n_value > n_max) {
                gteFLAG |= n_maxflag;
        } else if (n_value < n_min) {
@@ -243,12 +243,23 @@ static inline u32 limE_(psxCP2Regs *regs, u32 result) {
 #define limG2(a) LIM((a), 0x3ff, -0x400, (1 << 31) | (1 << 13))
 #define limH(a) LIM((a), 0x1000, 0x0000, (1 << 12))
 
+#ifndef __arm__
+#define A1U A1
+#define A2U A2
+#define A3U A3
+#else
+/* these are unlikely to be hit and usually waste cycles, don't want them on ARM */
+#define A1U(x) (x)
+#define A2U(x) (x)
+#define A3U(x) (x)
+#endif
+
 #include "gte_divider.h"
 
 #ifndef FLAGLESS
 
 static inline u32 MFC2(int reg) {
-       psxCP2Regs *regs = (psxCP2Regs *)&psxRegs.CP2D;
+       psxCP2Regs *regs = &psxRegs.CP2;
        switch (reg) {
                case 1:
                case 3:
@@ -283,7 +294,7 @@ static inline u32 MFC2(int reg) {
 }
 
 static inline void MTC2(u32 value, int reg) {
-       psxCP2Regs *regs = (psxCP2Regs *)&psxRegs.CP2D;
+       psxCP2Regs *regs = &psxRegs.CP2;
        switch (reg) {
                case 15:
                        gteSXY0 = gteSXY1;
@@ -490,7 +501,7 @@ void gteAVSZ3(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC0 = F((s64)(gteZSF3 * gteSZ1) + (gteZSF3 * gteSZ2) + (gteZSF3 * gteSZ3));
+       gteMAC0 = F((s64)gteZSF3 * (gteSZ1 + gteSZ2 + gteSZ3));
        gteOTZ = limD(gteMAC0 >> 12);
 }
 
@@ -500,7 +511,7 @@ void gteAVSZ4(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC0 = F((s64)(gteZSF4 * (gteSZ0 + gteSZ1 + gteSZ2 + gteSZ3)));
+       gteMAC0 = F((s64)gteZSF4 * (gteSZ0 + gteSZ1 + gteSZ2 + gteSZ3));
        gteOTZ = limD(gteMAC0 >> 12);
 }
 
@@ -513,9 +524,9 @@ void gteSQR(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = A1((gteIR1 * gteIR1) >> shift);
-       gteMAC2 = A2((gteIR2 * gteIR2) >> shift);
-       gteMAC3 = A3((gteIR3 * gteIR3) >> shift);
+       gteMAC1 = (gteIR1 * gteIR1) >> shift;
+       gteMAC2 = (gteIR2 * gteIR2) >> shift;
+       gteMAC3 = (gteIR3 * gteIR3) >> shift;
        gteIR1 = limB1(gteMAC1, lm);
        gteIR2 = limB2(gteMAC2, lm);
        gteIR3 = limB3(gteMAC3, lm);
@@ -527,9 +538,9 @@ void gteNCCS(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = A1((((s64)gteL11 * gteVX0) + (gteL12 * gteVY0) + (gteL13 * gteVZ0)) >> 12);
-       gteMAC2 = A2((((s64)gteL21 * gteVX0) + (gteL22 * gteVY0) + (gteL23 * gteVZ0)) >> 12);
-       gteMAC3 = A3((((s64)gteL31 * gteVX0) + (gteL32 * gteVY0) + (gteL33 * gteVZ0)) >> 12);
+       gteMAC1 = ((s64)(gteL11 * gteVX0) + (gteL12 * gteVY0) + (gteL13 * gteVZ0)) >> 12;
+       gteMAC2 = ((s64)(gteL21 * gteVX0) + (gteL22 * gteVY0) + (gteL23 * gteVZ0)) >> 12;
+       gteMAC3 = ((s64)(gteL31 * gteVX0) + (gteL32 * gteVY0) + (gteL33 * gteVZ0)) >> 12;
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
@@ -539,12 +550,12 @@ void gteNCCS(psxCP2Regs *regs) {
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
-       gteMAC1 = A1(((s64)gteR * gteIR1) >> 8);
-       gteMAC2 = A2(((s64)gteG * gteIR2) >> 8);
-       gteMAC3 = A3(((s64)gteB * gteIR3) >> 8);
-       gteIR1 = limB1(gteMAC1, 1);
-       gteIR2 = limB2(gteMAC2, 1);
-       gteIR3 = limB3(gteMAC3, 1);
+       gteMAC1 = ((s32)gteR * gteIR1) >> 8;
+       gteMAC2 = ((s32)gteG * gteIR2) >> 8;
+       gteMAC3 = ((s32)gteB * gteIR3) >> 8;
+       gteIR1 = gteMAC1;
+       gteIR2 = gteMAC2;
+       gteIR3 = gteMAC3;
 
        gteRGB0 = gteRGB1;
        gteRGB1 = gteRGB2;
@@ -567,9 +578,9 @@ void gteNCCT(psxCP2Regs *regs) {
                vx = VX(v);
                vy = VY(v);
                vz = VZ(v);
-               gteMAC1 = A1((((s64)gteL11 * vx) + (gteL12 * vy) + (gteL13 * vz)) >> 12);
-               gteMAC2 = A2((((s64)gteL21 * vx) + (gteL22 * vy) + (gteL23 * vz)) >> 12);
-               gteMAC3 = A3((((s64)gteL31 * vx) + (gteL32 * vy) + (gteL33 * vz)) >> 12);
+               gteMAC1 = ((s64)(gteL11 * vx) + (gteL12 * vy) + (gteL13 * vz)) >> 12;
+               gteMAC2 = ((s64)(gteL21 * vx) + (gteL22 * vy) + (gteL23 * vz)) >> 12;
+               gteMAC3 = ((s64)(gteL31 * vx) + (gteL32 * vy) + (gteL33 * vz)) >> 12;
                gteIR1 = limB1(gteMAC1, 1);
                gteIR2 = limB2(gteMAC2, 1);
                gteIR3 = limB3(gteMAC3, 1);
@@ -579,9 +590,9 @@ void gteNCCT(psxCP2Regs *regs) {
                gteIR1 = limB1(gteMAC1, 1);
                gteIR2 = limB2(gteMAC2, 1);
                gteIR3 = limB3(gteMAC3, 1);
-               gteMAC1 = A1(((s64)gteR * gteIR1) >> 8);
-               gteMAC2 = A2(((s64)gteG * gteIR2) >> 8);
-               gteMAC3 = A3(((s64)gteB * gteIR3) >> 8);
+               gteMAC1 = ((s32)gteR * gteIR1) >> 8;
+               gteMAC2 = ((s32)gteG * gteIR2) >> 8;
+               gteMAC3 = ((s32)gteB * gteIR3) >> 8;
 
                gteRGB0 = gteRGB1;
                gteRGB1 = gteRGB2;
@@ -590,9 +601,9 @@ void gteNCCT(psxCP2Regs *regs) {
                gteG2 = limC2(gteMAC2 >> 4);
                gteB2 = limC3(gteMAC3 >> 4);
        }
-       gteIR1 = limB1(gteMAC1, 1);
-       gteIR2 = limB2(gteMAC2, 1);
-       gteIR3 = limB3(gteMAC3, 1);
+       gteIR1 = gteMAC1;
+       gteIR2 = gteMAC2;
+       gteIR3 = gteMAC3;
 }
 
 void gteNCDS(psxCP2Regs *regs) {
@@ -601,9 +612,9 @@ void gteNCDS(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = A1((((s64)gteL11 * gteVX0) + (gteL12 * gteVY0) + (gteL13 * gteVZ0)) >> 12);
-       gteMAC2 = A2((((s64)gteL21 * gteVX0) + (gteL22 * gteVY0) + (gteL23 * gteVZ0)) >> 12);
-       gteMAC3 = A3((((s64)gteL31 * gteVX0) + (gteL32 * gteVY0) + (gteL33 * gteVZ0)) >> 12);
+       gteMAC1 = ((s64)(gteL11 * gteVX0) + (gteL12 * gteVY0) + (gteL13 * gteVZ0)) >> 12;
+       gteMAC2 = ((s64)(gteL21 * gteVX0) + (gteL22 * gteVY0) + (gteL23 * gteVZ0)) >> 12;
+       gteMAC3 = ((s64)(gteL31 * gteVX0) + (gteL32 * gteVY0) + (gteL33 * gteVZ0)) >> 12;
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
@@ -613,9 +624,9 @@ void gteNCDS(psxCP2Regs *regs) {
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
-       gteMAC1 = A1(((((s64)gteR << 4) * gteIR1) + (gteIR0 * limB1(gteRFC - ((gteR * gteIR1) >> 8), 0))) >> 12);
-       gteMAC2 = A2(((((s64)gteG << 4) * gteIR2) + (gteIR0 * limB2(gteGFC - ((gteG * gteIR2) >> 8), 0))) >> 12);
-       gteMAC3 = A3(((((s64)gteB << 4) * gteIR3) + (gteIR0 * limB3(gteBFC - ((gteB * gteIR3) >> 8), 0))) >> 12);
+       gteMAC1 = (((gteR << 4) * gteIR1) + (gteIR0 * limB1(A1U((s64)gteRFC - ((gteR * gteIR1) >> 8)), 0))) >> 12;
+       gteMAC2 = (((gteG << 4) * gteIR2) + (gteIR0 * limB2(A2U((s64)gteGFC - ((gteG * gteIR2) >> 8)), 0))) >> 12;
+       gteMAC3 = (((gteB << 4) * gteIR3) + (gteIR0 * limB3(A3U((s64)gteBFC - ((gteB * gteIR3) >> 8)), 0))) >> 12;
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
@@ -641,9 +652,9 @@ void gteNCDT(psxCP2Regs *regs) {
                vx = VX(v);
                vy = VY(v);
                vz = VZ(v);
-               gteMAC1 = A1((((s64)gteL11 * vx) + (gteL12 * vy) + (gteL13 * vz)) >> 12);
-               gteMAC2 = A2((((s64)gteL21 * vx) + (gteL22 * vy) + (gteL23 * vz)) >> 12);
-               gteMAC3 = A3((((s64)gteL31 * vx) + (gteL32 * vy) + (gteL33 * vz)) >> 12);
+               gteMAC1 = ((s64)(gteL11 * vx) + (gteL12 * vy) + (gteL13 * vz)) >> 12;
+               gteMAC2 = ((s64)(gteL21 * vx) + (gteL22 * vy) + (gteL23 * vz)) >> 12;
+               gteMAC3 = ((s64)(gteL31 * vx) + (gteL32 * vy) + (gteL33 * vz)) >> 12;
                gteIR1 = limB1(gteMAC1, 1);
                gteIR2 = limB2(gteMAC2, 1);
                gteIR3 = limB3(gteMAC3, 1);
@@ -653,9 +664,9 @@ void gteNCDT(psxCP2Regs *regs) {
                gteIR1 = limB1(gteMAC1, 1);
                gteIR2 = limB2(gteMAC2, 1);
                gteIR3 = limB3(gteMAC3, 1);
-               gteMAC1 = A1(((((s64)gteR << 4) * gteIR1) + (gteIR0 * limB1(gteRFC - ((gteR * gteIR1) >> 8), 0))) >> 12);
-               gteMAC2 = A2(((((s64)gteG << 4) * gteIR2) + (gteIR0 * limB2(gteGFC - ((gteG * gteIR2) >> 8), 0))) >> 12);
-               gteMAC3 = A3(((((s64)gteB << 4) * gteIR3) + (gteIR0 * limB3(gteBFC - ((gteB * gteIR3) >> 8), 0))) >> 12);
+               gteMAC1 = (((gteR << 4) * gteIR1) + (gteIR0 * limB1(A1U((s64)gteRFC - ((gteR * gteIR1) >> 8)), 0))) >> 12;
+               gteMAC2 = (((gteG << 4) * gteIR2) + (gteIR0 * limB2(A2U((s64)gteGFC - ((gteG * gteIR2) >> 8)), 0))) >> 12;
+               gteMAC3 = (((gteB << 4) * gteIR3) + (gteIR0 * limB3(A3U((s64)gteBFC - ((gteB * gteIR3) >> 8)), 0))) >> 12;
 
                gteRGB0 = gteRGB1;
                gteRGB1 = gteRGB2;
@@ -678,9 +689,9 @@ void gteOP(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = A1(((s64)(gteR22 * gteIR3) - (gteR33 * gteIR2)) >> shift);
-       gteMAC2 = A2(((s64)(gteR33 * gteIR1) - (gteR11 * gteIR3)) >> shift);
-       gteMAC3 = A3(((s64)(gteR11 * gteIR2) - (gteR22 * gteIR1)) >> shift);
+       gteMAC1 = ((gteR22 * gteIR3) - (gteR33 * gteIR2)) >> shift;
+       gteMAC2 = ((gteR33 * gteIR1) - (gteR11 * gteIR3)) >> shift;
+       gteMAC3 = ((gteR11 * gteIR2) - (gteR22 * gteIR1)) >> shift;
        gteIR1 = limB1(gteMAC1, lm);
        gteIR2 = limB2(gteMAC2, lm);
        gteIR3 = limB3(gteMAC3, lm);
@@ -689,18 +700,18 @@ void gteOP(psxCP2Regs *regs) {
 void gteDCPL(psxCP2Regs *regs) {
        int lm = GTE_LM(gteop);
 
-       s64 RIR1 = ((s64)gteR * gteIR1) >> 8;
-       s64 GIR2 = ((s64)gteG * gteIR2) >> 8;
-       s64 BIR3 = ((s64)gteB * gteIR3) >> 8;
+       s32 RIR1 = ((s32)gteR * gteIR1) >> 8;
+       s32 GIR2 = ((s32)gteG * gteIR2) >> 8;
+       s32 BIR3 = ((s32)gteB * gteIR3) >> 8;
 
 #ifdef GTE_LOG
        GTE_LOG("GTE DCPL\n");
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = A1(RIR1 + ((gteIR0 * limB1(gteRFC - RIR1, 0)) >> 12));
-       gteMAC2 = A2(GIR2 + ((gteIR0 * limB1(gteGFC - GIR2, 0)) >> 12));
-       gteMAC3 = A3(BIR3 + ((gteIR0 * limB1(gteBFC - BIR3, 0)) >> 12));
+       gteMAC1 = RIR1 + ((gteIR0 * limB1(A1U((s64)gteRFC - RIR1), 0)) >> 12);
+       gteMAC2 = GIR2 + ((gteIR0 * limB1(A2U((s64)gteGFC - GIR2), 0)) >> 12);
+       gteMAC3 = BIR3 + ((gteIR0 * limB1(A3U((s64)gteBFC - BIR3), 0)) >> 12);
 
        gteIR1 = limB1(gteMAC1, lm);
        gteIR2 = limB2(gteMAC2, lm);
@@ -722,9 +733,9 @@ void gteGPF(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = A1(((s64)gteIR0 * gteIR1) >> shift);
-       gteMAC2 = A2(((s64)gteIR0 * gteIR2) >> shift);
-       gteMAC3 = A3(((s64)gteIR0 * gteIR3) >> shift);
+       gteMAC1 = (gteIR0 * gteIR1) >> shift;
+       gteMAC2 = (gteIR0 * gteIR2) >> shift;
+       gteMAC3 = (gteIR0 * gteIR3) >> shift;
        gteIR1 = limB1(gteMAC1, 0);
        gteIR2 = limB2(gteMAC2, 0);
        gteIR3 = limB3(gteMAC3, 0);
@@ -768,9 +779,9 @@ void gteDPCS(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = A1(((gteR << 16) + (gteIR0 * limB1(A1((s64)gteRFC - (gteR << 4)) << (12 - shift), 0))) >> 12);
-       gteMAC2 = A2(((gteG << 16) + (gteIR0 * limB2(A2((s64)gteGFC - (gteG << 4)) << (12 - shift), 0))) >> 12);
-       gteMAC3 = A3(((gteB << 16) + (gteIR0 * limB3(A3((s64)gteBFC - (gteB << 4)) << (12 - shift), 0))) >> 12);
+       gteMAC1 = ((gteR << 16) + (gteIR0 * limB1(A1U(((s64)gteRFC - (gteR << 4)) << (12 - shift)), 0))) >> 12;
+       gteMAC2 = ((gteG << 16) + (gteIR0 * limB2(A2U(((s64)gteGFC - (gteG << 4)) << (12 - shift)), 0))) >> 12;
+       gteMAC3 = ((gteB << 16) + (gteIR0 * limB3(A3U(((s64)gteBFC - (gteB << 4)) << (12 - shift)), 0))) >> 12;
 
        gteIR1 = limB1(gteMAC1, 0);
        gteIR2 = limB2(gteMAC2, 0);
@@ -792,9 +803,9 @@ void gteDPCT(psxCP2Regs *regs) {
        gteFLAG = 0;
 
        for (v = 0; v < 3; v++) {
-               gteMAC1 = A1((((s64)gteR0 << 16) + ((s64)gteIR0 * (limB1(gteRFC - (gteR0 << 4), 0)))) >> 12);
-               gteMAC2 = A2((((s64)gteG0 << 16) + ((s64)gteIR0 * (limB1(gteGFC - (gteG0 << 4), 0)))) >> 12);
-               gteMAC3 = A3((((s64)gteB0 << 16) + ((s64)gteIR0 * (limB1(gteBFC - (gteB0 << 4), 0)))) >> 12);
+               gteMAC1 = ((gteR0 << 16) + (gteIR0 * limB1(A1U((s64)gteRFC - (gteR0 << 4)), 0))) >> 12;
+               gteMAC2 = ((gteG0 << 16) + (gteIR0 * limB1(A2U((s64)gteGFC - (gteG0 << 4)), 0))) >> 12;
+               gteMAC3 = ((gteB0 << 16) + (gteIR0 * limB1(A3U((s64)gteBFC - (gteB0 << 4)), 0))) >> 12;
 
                gteRGB0 = gteRGB1;
                gteRGB1 = gteRGB2;
@@ -814,9 +825,9 @@ void gteNCS(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = A1((((s64)gteL11 * gteVX0) + (gteL12 * gteVY0) + (gteL13 * gteVZ0)) >> 12);
-       gteMAC2 = A2((((s64)gteL21 * gteVX0) + (gteL22 * gteVY0) + (gteL23 * gteVZ0)) >> 12);
-       gteMAC3 = A3((((s64)gteL31 * gteVX0) + (gteL32 * gteVY0) + (gteL33 * gteVZ0)) >> 12);
+       gteMAC1 = ((s64)(gteL11 * gteVX0) + (gteL12 * gteVY0) + (gteL13 * gteVZ0)) >> 12;
+       gteMAC2 = ((s64)(gteL21 * gteVX0) + (gteL22 * gteVY0) + (gteL23 * gteVZ0)) >> 12;
+       gteMAC3 = ((s64)(gteL31 * gteVX0) + (gteL32 * gteVY0) + (gteL33 * gteVZ0)) >> 12;
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
@@ -848,9 +859,9 @@ void gteNCT(psxCP2Regs *regs) {
                vx = VX(v);
                vy = VY(v);
                vz = VZ(v);
-               gteMAC1 = A1((((s64)gteL11 * vx) + (gteL12 * vy) + (gteL13 * vz)) >> 12);
-               gteMAC2 = A2((((s64)gteL21 * vx) + (gteL22 * vy) + (gteL23 * vz)) >> 12);
-               gteMAC3 = A3((((s64)gteL31 * vx) + (gteL32 * vy) + (gteL33 * vz)) >> 12);
+               gteMAC1 = ((s64)(gteL11 * vx) + (gteL12 * vy) + (gteL13 * vz)) >> 12;
+               gteMAC2 = ((s64)(gteL21 * vx) + (gteL22 * vy) + (gteL23 * vz)) >> 12;
+               gteMAC3 = ((s64)(gteL31 * vx) + (gteL32 * vy) + (gteL33 * vz)) >> 12;
                gteIR1 = limB1(gteMAC1, 1);
                gteIR2 = limB2(gteMAC2, 1);
                gteIR3 = limB3(gteMAC3, 1);
@@ -881,9 +892,9 @@ void gteCC(psxCP2Regs *regs) {
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
-       gteMAC1 = A1(((s64)gteR * gteIR1) >> 8);
-       gteMAC2 = A2(((s64)gteG * gteIR2) >> 8);
-       gteMAC3 = A3(((s64)gteB * gteIR3) >> 8);
+       gteMAC1 = ((s32)gteR * gteIR1) >> 8;
+       gteMAC2 = ((s32)gteG * gteIR2) >> 8;
+       gteMAC3 = ((s32)gteB * gteIR3) >> 8;
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
@@ -905,9 +916,9 @@ void gteINTPL(psxCP2Regs *regs) {
 #endif
        gteFLAG = 0;
 
-       gteMAC1 = A1(((gteIR1 << 12) + (gteIR0 * limB1(((s64)gteRFC - gteIR1), 0))) >> shift);
-       gteMAC2 = A2(((gteIR2 << 12) + (gteIR0 * limB2(((s64)gteGFC - gteIR2), 0))) >> shift);
-       gteMAC3 = A3(((gteIR3 << 12) + (gteIR0 * limB3(((s64)gteBFC - gteIR3), 0))) >> shift);
+       gteMAC1 = ((gteIR1 << 12) + (gteIR0 * limB1(A1U((s64)gteRFC - gteIR1), 0))) >> shift;
+       gteMAC2 = ((gteIR2 << 12) + (gteIR0 * limB2(A2U((s64)gteGFC - gteIR2), 0))) >> shift;
+       gteMAC3 = ((gteIR3 << 12) + (gteIR0 * limB3(A3U((s64)gteBFC - gteIR3), 0))) >> shift;
        gteIR1 = limB1(gteMAC1, lm);
        gteIR2 = limB2(gteMAC2, lm);
        gteIR3 = limB3(gteMAC3, lm);
@@ -931,9 +942,9 @@ void gteCDP(psxCP2Regs *regs) {
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
-       gteMAC1 = A1(((((s64)gteR << 4) * gteIR1) + (gteIR0 * limB1(gteRFC - ((gteR * gteIR1) >> 8), 0))) >> 12);
-       gteMAC2 = A2(((((s64)gteG << 4) * gteIR2) + (gteIR0 * limB2(gteGFC - ((gteG * gteIR2) >> 8), 0))) >> 12);
-       gteMAC3 = A3(((((s64)gteB << 4) * gteIR3) + (gteIR0 * limB3(gteBFC - ((gteB * gteIR3) >> 8), 0))) >> 12);
+       gteMAC1 = (((gteR << 4) * gteIR1) + (gteIR0 * limB1(A1U((s64)gteRFC - ((gteR * gteIR1) >> 8)), 0))) >> 12;
+       gteMAC2 = (((gteG << 4) * gteIR2) + (gteIR0 * limB2(A2U((s64)gteGFC - ((gteG * gteIR2) >> 8)), 0))) >> 12;
+       gteMAC3 = (((gteB << 4) * gteIR3) + (gteIR0 * limB3(A3U((s64)gteBFC - ((gteB * gteIR3) >> 8)), 0))) >> 12;
        gteIR1 = limB1(gteMAC1, 1);
        gteIR2 = limB2(gteMAC2, 1);
        gteIR3 = limB3(gteMAC3, 1);
@@ -945,3 +956,131 @@ void gteCDP(psxCP2Regs *regs) {
        gteG2 = limC2(gteMAC2 >> 4);
        gteB2 = limC3(gteMAC3 >> 4);
 }
+
+/* decomposed/parametrized versions for the recompiler */
+
+#ifndef FLAGLESS
+
+void gteSQR_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = gteIR1 * gteIR1;
+       gteMAC2 = gteIR2 * gteIR2;
+       gteMAC3 = gteIR3 * gteIR3;
+}
+
+void gteSQR_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = (gteIR1 * gteIR1) >> 12;
+       gteMAC2 = (gteIR2 * gteIR2) >> 12;
+       gteMAC3 = (gteIR3 * gteIR3) >> 12;
+}
+
+void gteOP_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = (gteR22 * gteIR3) - (gteR33 * gteIR2);
+       gteMAC2 = (gteR33 * gteIR1) - (gteR11 * gteIR3);
+       gteMAC3 = (gteR11 * gteIR2) - (gteR22 * gteIR1);
+}
+
+void gteOP_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteR22 * gteIR3) - (gteR33 * gteIR2)) >> 12;
+       gteMAC2 = ((gteR33 * gteIR1) - (gteR11 * gteIR3)) >> 12;
+       gteMAC3 = ((gteR11 * gteIR2) - (gteR22 * gteIR1)) >> 12;
+}
+
+void gteDCPL_part(psxCP2Regs *regs) {
+       s32 RIR1 = ((s32)gteR * gteIR1) >> 8;
+       s32 GIR2 = ((s32)gteG * gteIR2) >> 8;
+       s32 BIR3 = ((s32)gteB * gteIR3) >> 8;
+
+       gteFLAG = 0;
+
+       gteMAC1 = RIR1 + ((gteIR0 * limB1(A1U((s64)gteRFC - RIR1), 0)) >> 12);
+       gteMAC2 = GIR2 + ((gteIR0 * limB1(A2U((s64)gteGFC - GIR2), 0)) >> 12);
+       gteMAC3 = BIR3 + ((gteIR0 * limB1(A3U((s64)gteBFC - BIR3), 0)) >> 12);
+}
+
+void gteGPF_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = gteIR0 * gteIR1;
+       gteMAC2 = gteIR0 * gteIR2;
+       gteMAC3 = gteIR0 * gteIR3;
+}
+
+void gteGPF_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = (gteIR0 * gteIR1) >> 12;
+       gteMAC2 = (gteIR0 * gteIR2) >> 12;
+       gteMAC3 = (gteIR0 * gteIR3) >> 12;
+}
+
+#endif // !FLAGLESS
+
+void gteGPL_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = A1((s64)gteMAC1 + (gteIR0 * gteIR1));
+       gteMAC2 = A2((s64)gteMAC2 + (gteIR0 * gteIR2));
+       gteMAC3 = A3((s64)gteMAC3 + (gteIR0 * gteIR3));
+}
+
+void gteGPL_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = A1((s64)gteMAC1 + ((gteIR0 * gteIR1) >> 12));
+       gteMAC2 = A2((s64)gteMAC2 + ((gteIR0 * gteIR2) >> 12));
+       gteMAC3 = A3((s64)gteMAC3 + ((gteIR0 * gteIR3) >> 12));
+}
+
+void gteDPCS_part_noshift(psxCP2Regs *regs) {
+       int shift = 0;
+
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteR << 16) + (gteIR0 * limB1(A1U((s64)gteRFC - (gteR << 4)) << (12 - shift), 0))) >> 12;
+       gteMAC2 = ((gteG << 16) + (gteIR0 * limB2(A2U((s64)gteGFC - (gteG << 4)) << (12 - shift), 0))) >> 12;
+       gteMAC3 = ((gteB << 16) + (gteIR0 * limB3(A3U((s64)gteBFC - (gteB << 4)) << (12 - shift), 0))) >> 12;
+}
+
+void gteDPCS_part_shift(psxCP2Regs *regs) {
+       int shift = 12;
+
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteR << 16) + (gteIR0 * limB1(A1U((s64)gteRFC - (gteR << 4)) << (12 - shift), 0))) >> 12;
+       gteMAC2 = ((gteG << 16) + (gteIR0 * limB2(A2U((s64)gteGFC - (gteG << 4)) << (12 - shift), 0))) >> 12;
+       gteMAC3 = ((gteB << 16) + (gteIR0 * limB3(A3U((s64)gteBFC - (gteB << 4)) << (12 - shift), 0))) >> 12;
+}
+
+void gteINTPL_part_noshift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteIR1 << 12) + (gteIR0 * limB1(A1U((s64)gteRFC - gteIR1), 0)));
+       gteMAC2 = ((gteIR2 << 12) + (gteIR0 * limB2(A2U((s64)gteGFC - gteIR2), 0)));
+       gteMAC3 = ((gteIR3 << 12) + (gteIR0 * limB3(A3U((s64)gteBFC - gteIR3), 0)));
+}
+
+void gteINTPL_part_shift(psxCP2Regs *regs) {
+       gteFLAG = 0;
+
+       gteMAC1 = ((gteIR1 << 12) + (gteIR0 * limB1(A1U((s64)gteRFC - gteIR1), 0))) >> 12;
+       gteMAC2 = ((gteIR2 << 12) + (gteIR0 * limB2(A2U((s64)gteGFC - gteIR2), 0))) >> 12;
+       gteMAC3 = ((gteIR3 << 12) + (gteIR0 * limB3(A3U((s64)gteBFC - gteIR3), 0))) >> 12;
+}
+
+void gteMACtoRGB(psxCP2Regs *regs) {
+       gteRGB0 = gteRGB1;
+       gteRGB1 = gteRGB2;
+       gteCODE2 = gteCODE;
+       gteR2 = limC1(gteMAC1 >> 4);
+       gteG2 = limC2(gteMAC2 >> 4);
+       gteB2 = limC3(gteMAC3 >> 4);
+}
+