Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

Vectorize ITOF and FTOI in compiled VU1 programs

Samir Nuri6194c83e66c6
diff --git a/recomp/vurecomp.cpp b/recomp/vurecomp.cpp
index e50dbc4..b48ea19 100644
--- a/recomp/vurecomp.cpp
+++ b/recomp/vurecomp.cpp
@@ -156,7 +156,7 @@ Upper upper(std::uint32_t hi, bool flags_live) {
}
};
const std::string dst = target == kAcc ? "r.acc" : std::format("r.vf[{}]", target);
- const bool vector = kind == Add || kind == Sub || kind == Mul || kind == Madd || kind == Msub || kind == Max || kind == Min || kind == Abs;
+ const bool vector = kind == Add || kind == Sub || kind == Mul || kind == Madd || kind == Msub || kind == Max || kind == Min || kind == Abs || kind == Ftoi || kind == Itof;
const bool arithmetic = kind == Add || kind == Sub || kind == Mul || kind == Madd || kind == Msub;
if (target != 0) {
u.target = target;
@@ -203,6 +203,8 @@ Upper upper(std::uint32_t hi, bool flags_live) {
case Max: e = std::format("vmax({}, {})", vs, vb); break;
case Min: e = std::format("vmin({}, {})", vs, vb); break;
case Abs: e = std::format("vabs({})", vs); break;
+ case Ftoi: e = std::format("vftoi({}, {})", vs, frac); break;
+ case Itof: e = std::format("vitof({}, {})", vs, frac); break;
default: break;
}
if (!e.empty()) u.pre += std::format("const v4u uv = {}; ", e);
diff --git a/runtime/include/ps2/vu_ops.hpp b/runtime/include/ps2/vu_ops.hpp
index b1cd663..c6fabf1 100644
--- a/runtime/include/ps2/vu_ops.hpp
+++ b/runtime/include/ps2/vu_ops.hpp
@@ -168,6 +168,25 @@ PS2_VU_INLINE v4u vmin(v4u a, v4u b) {
return (a & le) | (b & ~le);
}
PS2_VU_INLINE v4u vabs(v4u a) { return a & 0x7FFFFFFFu; }
+PS2_VU_INLINE v4u vftoi(v4u x, int fraction) {
+ const v4f f = (v4f)vfix(x) * static_cast<float>(1u << fraction);
+ const v4i big = f >= 2147483648.0f;
+ const v4i small = f < -2147483648.0f;
+ const v4i out = big | small;
+ const v4i t = __builtin_convertvector((v4f)((v4i)f & ~out), v4i);
+ return (v4u)((t & ~out) | (big & 0x7FFFFFFF) | (small & (v4i)vsplat(0x80000000u)));
+}
+PS2_VU_INLINE v4u vitof(v4u x, int fraction) {
+ const v4i v = (v4i)x;
+ v4f f = __builtin_convertvector(v, v4f);
+ if (__builtin_reduce_or((v4i)(x + 0x01000000u > 0x02000000u))) {
+ const v4i big = f >= 2147483648.0f;
+ const v4i back = __builtin_convertvector((v4f)((v4i)f & ~big), v4i);
+ const v4i over = big | ((v >= 0) & (back > v)) | ((v < 0) & (back < v));
+ f = (v4f)((v4i)f + over);
+ }
+ return (v4u)(f * (1.0f / static_cast<float>(1u << fraction)));
+}
PS2_VU_INLINE void flags(Vu0Regs &r, const Reg128 &v, unsigned dest) {
std::uint32_t mac = 0;