cleanup in vec4

This commit is contained in:
h4570
2022-08-15 01:36:43 +02:00
parent ae0d5c52fd
commit 3af2493068
2 changed files with 82 additions and 55 deletions
+25 -9
View File
@@ -50,18 +50,34 @@ class Vec4 {
/** Initialize Vec4 with float[4] values */
explicit Vec4(const float* v) { copy(this, v); }
Vec4 operator+(const Vec4& v) const;
Vec4 operator-(const Vec4& v) const;
/** Cross product */
Vec4 operator*(const Vec4& v) const;
Vec4 operator*(const float& v) const;
Vec4 operator/(const float& v) const;
Vec4 operator/(const Vec4& v) const;
Vec4 operator-(void) const;
/** copy */
void operator=(const Vec4& v);
/** negate xyz */
Vec4 operator-(void) const;
/** xyz + xyz */
Vec4 operator+(const Vec4& v) const;
void operator+=(const Vec4& v);
void operator*=(const float& v);
/** xyz - xyz */
Vec4 operator-(const Vec4& v) const;
void operator-=(const Vec4& v);
/** xyzw * xyzw */
Vec4 operator*(const Vec4& v) const;
void operator*=(const Vec4& v);
/** xyzw / xyzw */
Vec4 operator/(const Vec4& v) const;
void operator/=(const Vec4& v);
/** xyz * v */
Vec4 operator*(const float& v) const;
void operator*=(const float& v);
/** xyz / v */
Vec4 operator/(const float& v) const;
void operator/=(const float& v);
/** (0,0,0,1) */
+57 -46
View File
@@ -23,6 +23,10 @@ void Vec4::set(const float& t_x, const float& t_y, const float& t_z,
w = t_w;
}
void Vec4::operator=(const Vec4& v) { copy(this, v); }
Vec4 Vec4::operator-(void) const { return Vec4(-x, -y, -z, w); }
Vec4 Vec4::operator+(const Vec4& v) const {
Vec4 res;
asm volatile(
@@ -35,6 +39,16 @@ Vec4 Vec4::operator+(const Vec4& v) const {
return res;
}
void Vec4::operator+=(const Vec4& v) {
asm volatile(
"lqc2 $vf4, 0x0(%0) \n\t"
"lqc2 $vf5, 0x0(%1) \n\t"
"vadd.xyz $vf4, $vf4, $vf5 \n\t"
"sqc2 $vf4, 0x0(%0) \n\t"
:
: "r"(this->xyzw), "r"(v.xyzw));
}
Vec4 Vec4::operator-(const Vec4& v) const {
Vec4 res;
asm volatile(
@@ -47,6 +61,16 @@ Vec4 Vec4::operator-(const Vec4& v) const {
return res;
}
void Vec4::operator-=(const Vec4& v) {
asm volatile(
"lqc2 $vf4, 0x0(%0) \n\t"
"lqc2 $vf5, 0x0(%1) \n\t"
"vsub.xyz $vf4, $vf4, $vf5 \n\t"
"sqc2 $vf4, 0x0(%0) \n\t"
:
: "r"(this->xyzw), "r"(v.xyzw));
}
Vec4 Vec4::operator*(const Vec4& v) const {
Vec4 res;
asm volatile(
@@ -59,38 +83,6 @@ Vec4 Vec4::operator*(const Vec4& v) const {
return res;
}
Vec4 Vec4::operator*(const float& v) const {
Vec4 res;
asm volatile(
"lqc2 $vf4, 0x0(%1) \n\t"
"mfc1 $8, %2 \n\t"
"qmtc2 $8, $vf5 \n\t"
"vmulx.xyz $vf6, $vf4, $vf5 \n\t"
"sqc2 $vf6, 0x0(%0) \n\t"
:
: "r"(res.xyzw), "r"(this->xyzw), "f"(v));
res.w = w;
return res;
}
Vec4 Vec4::operator/(const float& v) const {
return Vec4(x / v, y / v, z / v, w);
}
Vec4 Vec4::operator/(const Vec4& v) const {
return Vec4(x / v.x, y / v.y, z / v.z, w);
}
void Vec4::operator+=(const Vec4& v) {
asm volatile(
"lqc2 $vf4, 0x0(%0) \n\t"
"lqc2 $vf5, 0x0(%1) \n\t"
"vadd.xyz $vf4, $vf4, $vf5 \n\t"
"sqc2 $vf4, 0x0(%0) \n\t"
:
: "r"(this->xyzw), "r"(v.xyzw));
}
void Vec4::operator*=(const Vec4& v) {
asm volatile(
"lqc2 $vf4, 0x0(%0) \n\t"
@@ -101,21 +93,44 @@ void Vec4::operator*=(const Vec4& v) {
: "r"(this->xyzw), "r"(v.xyzw));
}
void Vec4::operator*=(const float& v) {
// Hmm... we don't want to modify W in most of the cases
// It should be touched only during rendering process
auto tempW = w;
Vec4 Vec4::operator/(const Vec4& v) const {
return Vec4(x / v.x, y / v.y, z / v.z, w / v.w);
}
void Vec4::operator/=(const Vec4& v) {
x /= v.x;
y /= v.y;
z /= v.z;
w /= v.w;
}
Vec4 Vec4::operator*(const float& v) const {
Vec4 res;
asm volatile(
"lqc2 $vf4, 0x0(%0) \n\t"
"mfc1 $8, %1 \n\t"
"qmtc2 $8, $vf5 \n\t"
"vmulx.xyz $vf4, $vf4, $vf5 \n\t"
"sqc2 $vf4, 0x0(%0) \n\t"
"lqc2 $vf4, 0x0(%1) \n\t"
"mfc1 $8, %2 \n\t"
"qmtc2 $8, $vf5 \n\t"
"vmulx.xyz $vf6, $vf4, $vf5 \n\t"
"vmove.w $vf6, $vf4 \n\t"
"sqc2 $vf6, 0x0(%0) \n\t"
:
: "r"(res.xyzw), "r"(this->xyzw), "f"(v));
return res;
}
void Vec4::operator*=(const float& v) {
asm volatile(
"lqc2 $vf4, 0x0(%0) \n\t"
"mfc1 $8, %1 \n\t"
"qmtc2 $8, $vf5 \n\t"
"vmulx.xyz $vf4, $vf4, $vf5 \n\t"
"sqc2 $vf4, 0x0(%0) \n\t"
:
: "r"(this->xyzw), "f"(v));
}
w = tempW;
Vec4 Vec4::operator/(const float& v) const {
return Vec4(x / v, y / v, z / v, w);
}
void Vec4::operator/=(const float& v) {
@@ -124,10 +139,6 @@ void Vec4::operator/=(const float& v) {
z /= v;
}
void Vec4::operator=(const Vec4& v) { copy(this, v); }
Vec4 Vec4::operator-(void) const { return Vec4(-x, -y, -z); }
void Vec4::unit() {
asm volatile("sqc2 $vf0, 0x00(%0) \n" : : "r"(this->xyzw));
}