From a7d6efc5201960b351fee4760663388dd946ab8e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 7 Aug 2018 13:31:57 -0400 Subject: common: Convert type traits templates over to variable template versions where applicable Uses the C++17 inline variable variants --- src/common/alignment.h | 4 ++-- src/common/bit_set.h | 2 +- src/common/file_util.h | 10 +++++----- src/common/hash.h | 4 ++-- src/common/x64/xbyak_util.h | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/common') diff --git a/src/common/alignment.h b/src/common/alignment.h index b77da4a92..b9dd38746 100644 --- a/src/common/alignment.h +++ b/src/common/alignment.h @@ -9,13 +9,13 @@ namespace Common { template constexpr T AlignUp(T value, size_t size) { - static_assert(std::is_unsigned::value, "T must be an unsigned value."); + static_assert(std::is_unsigned_v, "T must be an unsigned value."); return static_cast(value + (size - value % size) % size); } template constexpr T AlignDown(T value, size_t size) { - static_assert(std::is_unsigned::value, "T must be an unsigned value."); + static_assert(std::is_unsigned_v, "T must be an unsigned value."); return static_cast(value - value % size); } diff --git a/src/common/bit_set.h b/src/common/bit_set.h index 84e3cbe58..5a197d8c1 100644 --- a/src/common/bit_set.h +++ b/src/common/bit_set.h @@ -96,7 +96,7 @@ static inline int LeastSignificantSetBit(u64 val) { template class BitSet { - static_assert(!std::is_signed::value, "BitSet should not be used with signed types"); + static_assert(!std::is_signed_v, "BitSet should not be used with signed types"); public: // A reference to a particular bit, returned from operator[]. diff --git a/src/common/file_util.h b/src/common/file_util.h index 28697d527..7f2a5cb63 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -207,7 +207,7 @@ public: template size_t ReadArray(T* data, size_t length) const { - static_assert(std::is_trivially_copyable(), + static_assert(std::is_trivially_copyable_v, "Given array does not consist of trivially copyable objects"); if (!IsOpen()) @@ -218,7 +218,7 @@ public: template size_t WriteArray(const T* data, size_t length) { - static_assert(std::is_trivially_copyable(), + static_assert(std::is_trivially_copyable_v, "Given array does not consist of trivially copyable objects"); if (!IsOpen()) return -1; @@ -227,19 +227,19 @@ public: template size_t ReadBytes(T* data, size_t length) const { - static_assert(std::is_trivially_copyable(), "T must be trivially copyable"); + static_assert(std::is_trivially_copyable_v, "T must be trivially copyable"); return ReadArray(reinterpret_cast(data), length); } template size_t WriteBytes(const T* data, size_t length) { - static_assert(std::is_trivially_copyable(), "T must be trivially copyable"); + static_assert(std::is_trivially_copyable_v, "T must be trivially copyable"); return WriteArray(reinterpret_cast(data), length); } template size_t WriteObject(const T& object) { - static_assert(!std::is_pointer::value, "Given object is a pointer"); + static_assert(!std::is_pointer_v, "WriteObject arguments must not be a pointer"); return WriteArray(&object, 1); } diff --git a/src/common/hash.h b/src/common/hash.h index 73c326980..2c761e545 100644 --- a/src/common/hash.h +++ b/src/common/hash.h @@ -28,7 +28,7 @@ static inline u64 ComputeHash64(const void* data, size_t len) { */ template static inline u64 ComputeStructHash64(const T& data) { - static_assert(std::is_trivially_copyable(), + static_assert(std::is_trivially_copyable_v, "Type passed to ComputeStructHash64 must be trivially copyable"); return ComputeHash64(&data, sizeof(data)); } @@ -38,7 +38,7 @@ template struct HashableStruct { // In addition to being trivially copyable, T must also have a trivial default constructor, // because any member initialization would be overridden by memset - static_assert(std::is_trivial(), "Type passed to HashableStruct must be trivial"); + static_assert(std::is_trivial_v, "Type passed to HashableStruct must be trivial"); /* * We use a union because "implicitly-defined copy/move constructor for a union X copies the * object representation of X." and "implicitly-defined copy assignment operator for a union X diff --git a/src/common/x64/xbyak_util.h b/src/common/x64/xbyak_util.h index 0f52f704b..ec76e0a47 100644 --- a/src/common/x64/xbyak_util.h +++ b/src/common/x64/xbyak_util.h @@ -34,7 +34,7 @@ inline bool IsWithin2G(const Xbyak::CodeGenerator& code, uintptr_t target) { template inline void CallFarFunction(Xbyak::CodeGenerator& code, const T f) { - static_assert(std::is_pointer(), "Argument must be a (function) pointer."); + static_assert(std::is_pointer_v, "Argument must be a (function) pointer."); size_t addr = reinterpret_cast(f); if (IsWithin2G(code, addr)) { code.call(f); -- cgit v1.2.3 From 4e3bc377911d941b67911434140ecce1e398f5ca Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 7 Aug 2018 14:15:56 -0400 Subject: vector_math: Convert typedefs to type aliases --- src/common/vector_math.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/vector_math.h b/src/common/vector_math.h index cca43bd4c..108399ae8 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h @@ -175,7 +175,7 @@ Vec2 operator*(const V& f, const Vec2& vec) { return Vec2(f * vec.x, f * vec.y); } -typedef Vec2 Vec2f; +using Vec2f = Vec2; template <> inline float Vec2::Length() const { @@ -387,7 +387,7 @@ inline float Vec3::Normalize() { return length; } -typedef Vec3 Vec3f; +using Vec3f = Vec3; template class Vec4 { @@ -583,7 +583,7 @@ Vec4 operator*(const V& f, const Vec4& vec) { return MakeVec(f * vec.x, f * vec.y, f * vec.z, f * vec.w); } -typedef Vec4 Vec4f; +using Vec4f = Vec4; template static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec2& a, const Vec2& b) { -- cgit v1.2.3 From 5c323d96e034c49e60dccb966a1ab753239b1ce6 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 7 Aug 2018 21:32:05 -0400 Subject: vector_math: Make functions constexpr where applicable --- src/common/vector_math.h | 333 +++++++++++++++++++++++++---------------------- 1 file changed, 179 insertions(+), 154 deletions(-) (limited to 'src/common') diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 108399ae8..7e5af651a 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h @@ -42,71 +42,72 @@ class Vec3; template class Vec4; -template -static inline Vec2 MakeVec(const T& x, const T& y); -template -static inline Vec3 MakeVec(const T& x, const T& y, const T& z); -template -static inline Vec4 MakeVec(const T& x, const T& y, const T& z, const T& w); - template class Vec2 { public: T x{}; T y{}; - Vec2() = default; - Vec2(const T& _x, const T& _y) : x(_x), y(_y) {} + constexpr Vec2() = default; + constexpr Vec2(const T& x_, const T& y_) : x(x_), y(y_) {} template - Vec2 Cast() const { - return Vec2((T2)x, (T2)y); + constexpr Vec2 Cast() const { + return Vec2(static_cast(x), static_cast(y)); } - static Vec2 AssignToAll(const T& f) { - return Vec2(f, f); + static constexpr Vec2 AssignToAll(const T& f) { + return Vec2{f, f}; } - Vec2 operator+(const Vec2& other) const { - return MakeVec(x + other.x, y + other.y); + constexpr Vec2 operator+(const Vec2& other) const { + return {x + other.x, y + other.y}; } - void operator+=(const Vec2& other) { + constexpr Vec2& operator+=(const Vec2& other) { x += other.x; y += other.y; + return *this; } - Vec2 operator-(const Vec2& other) const { - return MakeVec(x - other.x, y - other.y); + constexpr Vec2 operator-(const Vec2& other) const { + return {x - other.x, y - other.y}; } - void operator-=(const Vec2& other) { + constexpr Vec2& operator-=(const Vec2& other) { x -= other.x; y -= other.y; + return *this; } template - Vec2::value, U>> operator-() const { - return MakeVec(-x, -y); + constexpr Vec2::value, U>> operator-() const { + return {-x, -y}; } - Vec2 operator*(const Vec2& other) const { - return MakeVec(x * other.x, y * other.y); + constexpr Vec2 operator*(const Vec2& other) const { + return {x * other.x, y * other.y}; } + template - Vec2 operator*(const V& f) const { - return MakeVec(x * f, y * f); + constexpr Vec2 operator*(const V& f) const { + return {x * f, y * f}; } + template - void operator*=(const V& f) { + constexpr Vec2& operator*=(const V& f) { *this = *this * f; + return *this; } + template - Vec2 operator/(const V& f) const { - return MakeVec(x / f, y / f); + constexpr Vec2 operator/(const V& f) const { + return {x / f, y / f}; } + template - void operator/=(const V& f) { + constexpr Vec2& operator/=(const V& f) { *this = *this / f; + return *this; } - T Length2() const { + constexpr T Length2() const { return x * x + y * y; } @@ -118,60 +119,59 @@ public: Vec2 Normalized() const; float Normalize(); // returns the previous length, which is often useful - T& operator[](int i) // allow vector[1] = 3 (vector.y=3) - { + constexpr T& operator[](std::size_t i) { return *((&x) + i); } - T operator[](const int i) const { + constexpr const T& operator[](std::size_t i) const { return *((&x) + i); } - void SetZero() { + constexpr void SetZero() { x = 0; y = 0; } // Common aliases: UV (texel coordinates), ST (texture coordinates) - T& u() { + constexpr T& u() { return x; } - T& v() { + constexpr T& v() { return y; } - T& s() { + constexpr T& s() { return x; } - T& t() { + constexpr T& t() { return y; } - const T& u() const { + constexpr const T& u() const { return x; } - const T& v() const { + constexpr const T& v() const { return y; } - const T& s() const { + constexpr const T& s() const { return x; } - const T& t() const { + constexpr const T& t() const { return y; } // swizzlers - create a subvector of specific components - const Vec2 yx() const { + constexpr Vec2 yx() const { return Vec2(y, x); } - const Vec2 vu() const { + constexpr Vec2 vu() const { return Vec2(y, x); } - const Vec2 ts() const { + constexpr Vec2 ts() const { return Vec2(y, x); } }; template -Vec2 operator*(const V& f, const Vec2& vec) { +constexpr Vec2 operator*(const V& f, const Vec2& vec) { return Vec2(f * vec.x, f * vec.y); } @@ -196,64 +196,75 @@ public: T y{}; T z{}; - Vec3() = default; - Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {} + constexpr Vec3() = default; + constexpr Vec3(const T& x_, const T& y_, const T& z_) : x(x_), y(y_), z(z_) {} template - Vec3 Cast() const { - return MakeVec((T2)x, (T2)y, (T2)z); + constexpr Vec3 Cast() const { + return Vec3(static_cast(x), static_cast(y), static_cast(z)); } // Only implemented for T=int and T=float static Vec3 FromRGB(unsigned int rgb); unsigned int ToRGB() const; // alpha bits set to zero - static Vec3 AssignToAll(const T& f) { - return MakeVec(f, f, f); + static constexpr Vec3 AssignToAll(const T& f) { + return Vec3(f, f, f); } - Vec3 operator+(const Vec3& other) const { - return MakeVec(x + other.x, y + other.y, z + other.z); + constexpr Vec3 operator+(const Vec3& other) const { + return {x + other.x, y + other.y, z + other.z}; } - void operator+=(const Vec3& other) { + + constexpr Vec3& operator+=(const Vec3& other) { x += other.x; y += other.y; z += other.z; + return *this; } - Vec3 operator-(const Vec3& other) const { - return MakeVec(x - other.x, y - other.y, z - other.z); + + constexpr Vec3 operator-(const Vec3& other) const { + return {x - other.x, y - other.y, z - other.z}; } - void operator-=(const Vec3& other) { + + constexpr Vec3& operator-=(const Vec3& other) { x -= other.x; y -= other.y; z -= other.z; + return *this; } template - Vec3::value, U>> operator-() const { - return MakeVec(-x, -y, -z); + constexpr Vec3::value, U>> operator-() const { + return {-x, -y, -z}; } - Vec3 operator*(const Vec3& other) const { - return MakeVec(x * other.x, y * other.y, z * other.z); + + constexpr Vec3 operator*(const Vec3& other) const { + return {x * other.x, y * other.y, z * other.z}; } + template - Vec3 operator*(const V& f) const { - return MakeVec(x * f, y * f, z * f); + constexpr Vec3 operator*(const V& f) const { + return {x * f, y * f, z * f}; } + template - void operator*=(const V& f) { + constexpr Vec3& operator*=(const V& f) { *this = *this * f; + return *this; } template - Vec3 operator/(const V& f) const { - return MakeVec(x / f, y / f, z / f); + constexpr Vec3 operator/(const V& f) const { + return {x / f, y / f, z / f}; } + template - void operator/=(const V& f) { + constexpr Vec3& operator/=(const V& f) { *this = *this / f; + return *this; } - T Length2() const { + constexpr T Length2() const { return x * x + y * y + z * z; } @@ -265,78 +276,78 @@ public: Vec3 Normalized() const; float Normalize(); // returns the previous length, which is often useful - T& operator[](int i) // allow vector[2] = 3 (vector.z=3) - { + constexpr T& operator[](std::size_t i) { return *((&x) + i); } - T operator[](const int i) const { + + constexpr const T& operator[](std::size_t i) const { return *((&x) + i); } - void SetZero() { + constexpr void SetZero() { x = 0; y = 0; z = 0; } // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) - T& u() { + constexpr T& u() { return x; } - T& v() { + constexpr T& v() { return y; } - T& w() { + constexpr T& w() { return z; } - T& r() { + constexpr T& r() { return x; } - T& g() { + constexpr T& g() { return y; } - T& b() { + constexpr T& b() { return z; } - T& s() { + constexpr T& s() { return x; } - T& t() { + constexpr T& t() { return y; } - T& q() { + constexpr T& q() { return z; } - const T& u() const { + constexpr const T& u() const { return x; } - const T& v() const { + constexpr const T& v() const { return y; } - const T& w() const { + constexpr const T& w() const { return z; } - const T& r() const { + constexpr const T& r() const { return x; } - const T& g() const { + constexpr const T& g() const { return y; } - const T& b() const { + constexpr const T& b() const { return z; } - const T& s() const { + constexpr const T& s() const { return x; } - const T& t() const { + constexpr const T& t() const { return y; } - const T& q() const { + constexpr const T& q() const { return z; } @@ -345,7 +356,7 @@ public: // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all // component names (x<->r) and permutations (xy<->yx) #define _DEFINE_SWIZZLER2(a, b, name) \ - const Vec2 name() const { \ + constexpr Vec2 name() const { \ return Vec2(a, b); \ } #define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ @@ -366,7 +377,7 @@ public: }; template -Vec3 operator*(const V& f, const Vec3& vec) { +constexpr Vec3 operator*(const V& f, const Vec3& vec) { return Vec3(f * vec.x, f * vec.y, f * vec.z); } @@ -397,66 +408,80 @@ public: T z{}; T w{}; - Vec4() = default; - Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {} + constexpr Vec4() = default; + constexpr Vec4(const T& x_, const T& y_, const T& z_, const T& w_) + : x(x_), y(y_), z(z_), w(w_) {} template - Vec4 Cast() const { - return Vec4((T2)x, (T2)y, (T2)z, (T2)w); + constexpr Vec4 Cast() const { + return Vec4(static_cast(x), static_cast(y), static_cast(z), + static_cast(w)); } // Only implemented for T=int and T=float static Vec4 FromRGBA(unsigned int rgba); unsigned int ToRGBA() const; - static Vec4 AssignToAll(const T& f) { - return Vec4(f, f, f, f); + static constexpr Vec4 AssignToAll(const T& f) { + return Vec4(f, f, f, f); } - Vec4 operator+(const Vec4& other) const { - return MakeVec(x + other.x, y + other.y, z + other.z, w + other.w); + constexpr Vec4 operator+(const Vec4& other) const { + return {x + other.x, y + other.y, z + other.z, w + other.w}; } - void operator+=(const Vec4& other) { + + constexpr Vec4& operator+=(const Vec4& other) { x += other.x; y += other.y; z += other.z; w += other.w; + return *this; } - Vec4 operator-(const Vec4& other) const { - return MakeVec(x - other.x, y - other.y, z - other.z, w - other.w); + + constexpr Vec4 operator-(const Vec4& other) const { + return {x - other.x, y - other.y, z - other.z, w - other.w}; } - void operator-=(const Vec4& other) { + + constexpr Vec4& operator-=(const Vec4& other) { x -= other.x; y -= other.y; z -= other.z; w -= other.w; + return *this; } template - Vec4::value, U>> operator-() const { - return MakeVec(-x, -y, -z, -w); + constexpr Vec4::value, U>> operator-() const { + return {-x, -y, -z, -w}; } - Vec4 operator*(const Vec4& other) const { - return MakeVec(x * other.x, y * other.y, z * other.z, w * other.w); + + constexpr Vec4 operator*(const Vec4& other) const { + return {x * other.x, y * other.y, z * other.z, w * other.w}; } + template - Vec4 operator*(const V& f) const { - return MakeVec(x * f, y * f, z * f, w * f); + constexpr Vec4 operator*(const V& f) const { + return {x * f, y * f, z * f, w * f}; } + template - void operator*=(const V& f) { + constexpr Vec4& operator*=(const V& f) { *this = *this * f; + return *this; } + template - Vec4 operator/(const V& f) const { - return MakeVec(x / f, y / f, z / f, w / f); + constexpr Vec4 operator/(const V& f) const { + return {x / f, y / f, z / f, w / f}; } + template - void operator/=(const V& f) { + constexpr Vec4& operator/=(const V& f) { *this = *this / f; + return *this; } - T Length2() const { + constexpr T Length2() const { return x * x + y * y + z * z + w * w; } @@ -468,15 +493,15 @@ public: Vec4 Normalized() const; float Normalize(); // returns the previous length, which is often useful - T& operator[](int i) // allow vector[2] = 3 (vector.z=3) - { + constexpr T& operator[](std::size_t i) { return *((&x) + i); } - T operator[](const int i) const { + + constexpr const T& operator[](std::size_t i) const { return *((&x) + i); } - void SetZero() { + constexpr void SetZero() { x = 0; y = 0; z = 0; @@ -484,29 +509,29 @@ public: } // Common alias: RGBA (colors) - T& r() { + constexpr T& r() { return x; } - T& g() { + constexpr T& g() { return y; } - T& b() { + constexpr T& b() { return z; } - T& a() { + constexpr T& a() { return w; } - const T& r() const { + constexpr const T& r() const { return x; } - const T& g() const { + constexpr const T& g() const { return y; } - const T& b() const { + constexpr const T& b() const { return z; } - const T& a() const { + constexpr const T& a() const { return w; } @@ -518,7 +543,7 @@ public: // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and // permutations (xy<->yx) #define _DEFINE_SWIZZLER2(a, b, name) \ - const Vec2 name() const { \ + constexpr Vec2 name() const { \ return Vec2(a, b); \ } #define DEFINE_SWIZZLER2_COMP1(a, a2) \ @@ -545,7 +570,7 @@ public: #undef _DEFINE_SWIZZLER2 #define _DEFINE_SWIZZLER3(a, b, c, name) \ - const Vec3 name() const { \ + constexpr Vec3 name() const { \ return Vec3(a, b, c); \ } #define DEFINE_SWIZZLER3_COMP1(a, a2) \ @@ -579,51 +604,51 @@ public: }; template -Vec4 operator*(const V& f, const Vec4& vec) { - return MakeVec(f * vec.x, f * vec.y, f * vec.z, f * vec.w); +constexpr Vec4 operator*(const V& f, const Vec4& vec) { + return {f * vec.x, f * vec.y, f * vec.z, f * vec.w}; } using Vec4f = Vec4; template -static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec2& a, const Vec2& b) { +constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec2& a, const Vec2& b) { return a.x * b.x + a.y * b.y; } template -static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec3& a, const Vec3& b) { +constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3& a, const Vec3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; } template -static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec4& a, const Vec4& b) { +constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4& a, const Vec4& b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } template -static inline Vec3 Cross(const Vec3& a, const Vec3& b) { - return MakeVec(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); +constexpr Vec3 Cross(const Vec3& a, const Vec3& b) { + return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; } // linear interpolation via float: 0.0=begin, 1.0=end template -static inline decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end, - const float t) { +constexpr decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end, + const float t) { return begin * (1.f - t) + end * t; } // linear interpolation via int: 0=begin, base=end template -static inline decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, const X& end, - const int t) { +constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, const X& end, + const int t) { return (begin * (base - t) + end * t) / base; } // bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second // interpolation. template -inline auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s, - const float t) { +constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s, + const float t) { auto y0 = Lerp(x00, x01, s); auto y1 = Lerp(x10, x11, s); return Lerp(y0, y1, t); @@ -631,42 +656,42 @@ inline auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x1 // Utility vector factories template -static inline Vec2 MakeVec(const T& x, const T& y) { +constexpr Vec2 MakeVec(const T& x, const T& y) { return Vec2{x, y}; } template -static inline Vec3 MakeVec(const T& x, const T& y, const T& z) { +constexpr Vec3 MakeVec(const T& x, const T& y, const T& z) { return Vec3{x, y, z}; } template -static inline Vec4 MakeVec(const T& x, const T& y, const Vec2& zw) { +constexpr Vec4 MakeVec(const T& x, const T& y, const Vec2& zw) { return MakeVec(x, y, zw[0], zw[1]); } template -static inline Vec3 MakeVec(const Vec2& xy, const T& z) { +constexpr Vec3 MakeVec(const Vec2& xy, const T& z) { return MakeVec(xy[0], xy[1], z); } template -static inline Vec3 MakeVec(const T& x, const Vec2& yz) { +constexpr Vec3 MakeVec(const T& x, const Vec2& yz) { return MakeVec(x, yz[0], yz[1]); } template -static inline Vec4 MakeVec(const T& x, const T& y, const T& z, const T& w) { +constexpr Vec4 MakeVec(const T& x, const T& y, const T& z, const T& w) { return Vec4{x, y, z, w}; } template -static inline Vec4 MakeVec(const Vec2& xy, const T& z, const T& w) { +constexpr Vec4 MakeVec(const Vec2& xy, const T& z, const T& w) { return MakeVec(xy[0], xy[1], z, w); } template -static inline Vec4 MakeVec(const T& x, const Vec2& yz, const T& w) { +constexpr Vec4 MakeVec(const T& x, const Vec2& yz, const T& w) { return MakeVec(x, yz[0], yz[1], w); } @@ -674,17 +699,17 @@ static inline Vec4 MakeVec(const T& x, const Vec2& yz, const T& w) { // Even if someone wanted to use an odd object like Vec2>, the compiler would error // out soon enough due to misuse of the returned structure. template -static inline Vec4 MakeVec(const Vec2& xy, const Vec2& zw) { +constexpr Vec4 MakeVec(const Vec2& xy, const Vec2& zw) { return MakeVec(xy[0], xy[1], zw[0], zw[1]); } template -static inline Vec4 MakeVec(const Vec3& xyz, const T& w) { +constexpr Vec4 MakeVec(const Vec3& xyz, const T& w) { return MakeVec(xyz[0], xyz[1], xyz[2], w); } template -static inline Vec4 MakeVec(const T& x, const Vec3& yzw) { +constexpr Vec4 MakeVec(const T& x, const Vec3& yzw) { return MakeVec(x, yzw[0], yzw[1], yzw[2]); } -- cgit v1.2.3 From 766c1a2d501966d4e6944cd16b9cee9f35db7a89 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 7 Aug 2018 21:33:48 -0400 Subject: vector_math: Remove unimplemented function prototypes --- src/common/vector_math.h | 23 ----------------------- 1 file changed, 23 deletions(-) (limited to 'src/common') diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 7e5af651a..5c94fcda3 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h @@ -113,10 +113,6 @@ public: // Only implemented for T=float float Length() const; - void SetLength(const float l); - Vec2 WithLength(const float l) const; - float Distance2To(Vec2& other); - Vec2 Normalized() const; float Normalize(); // returns the previous length, which is often useful constexpr T& operator[](std::size_t i) { @@ -204,10 +200,6 @@ public: return Vec3(static_cast(x), static_cast(y), static_cast(z)); } - // Only implemented for T=int and T=float - static Vec3 FromRGB(unsigned int rgb); - unsigned int ToRGB() const; // alpha bits set to zero - static constexpr Vec3 AssignToAll(const T& f) { return Vec3(f, f, f); } @@ -270,9 +262,6 @@ public: // Only implemented for T=float float Length() const; - void SetLength(const float l); - Vec3 WithLength(const float l) const; - float Distance2To(Vec3& other); Vec3 Normalized() const; float Normalize(); // returns the previous length, which is often useful @@ -418,10 +407,6 @@ public: static_cast(w)); } - // Only implemented for T=int and T=float - static Vec4 FromRGBA(unsigned int rgba); - unsigned int ToRGBA() const; - static constexpr Vec4 AssignToAll(const T& f) { return Vec4(f, f, f, f); } @@ -485,14 +470,6 @@ public: return x * x + y * y + z * z + w * w; } - // Only implemented for T=float - float Length() const; - void SetLength(const float l); - Vec4 WithLength(const float l) const; - float Distance2To(Vec4& other); - Vec4 Normalized() const; - float Normalize(); // returns the previous length, which is often useful - constexpr T& operator[](std::size_t i) { return *((&x) + i); } -- cgit v1.2.3 From cc9d7bbf01c407a256436d57e2aca56b399b968a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 8 Aug 2018 15:53:39 -0400 Subject: vector_math: Use variable template version of is_signed in Vec classes Same behavior, less code --- src/common/vector_math.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 5c94fcda3..8feb49941 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h @@ -78,7 +78,7 @@ public: } template - constexpr Vec2::value, U>> operator-() const { + constexpr Vec2, U>> operator-() const { return {-x, -y}; } constexpr Vec2 operator*(const Vec2& other) const { @@ -227,7 +227,7 @@ public: } template - constexpr Vec3::value, U>> operator-() const { + constexpr Vec3, U>> operator-() const { return {-x, -y, -z}; } @@ -436,7 +436,7 @@ public: } template - constexpr Vec4::value, U>> operator-() const { + constexpr Vec4, U>> operator-() const { return {-x, -y, -z, -w}; } -- cgit v1.2.3 From 76197a4be981dfad16793f27ce74bf527d5ef110 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 8 Aug 2018 16:00:07 -0400 Subject: common/color: Get rid of undefined behavior Gets rid of type punning via reinterpret_cast within functions. Instead, we use memcpy to transfer the contents across types. --- src/common/color.h | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'src/common') diff --git a/src/common/color.h b/src/common/color.h index 24a445dac..2e56af5a9 100644 --- a/src/common/color.h +++ b/src/common/color.h @@ -4,6 +4,8 @@ #pragma once +#include + #include "common/common_types.h" #include "common/swap.h" #include "common/vector_math.h" @@ -83,7 +85,8 @@ inline const Math::Vec4 DecodeRG8(const u8* bytes) { * @return Result color decoded as Math::Vec4 */ inline const Math::Vec4 DecodeRGB565(const u8* bytes) { - const u16_le pixel = *reinterpret_cast(bytes); + u16_le pixel; + std::memcpy(&pixel, bytes, sizeof(pixel)); return {Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F), Convert5To8(pixel & 0x1F), 255}; } @@ -94,7 +97,8 @@ inline const Math::Vec4 DecodeRGB565(const u8* bytes) { * @return Result color decoded as Math::Vec4 */ inline const Math::Vec4 DecodeRGB5A1(const u8* bytes) { - const u16_le pixel = *reinterpret_cast(bytes); + u16_le pixel; + std::memcpy(&pixel, bytes, sizeof(pixel)); return {Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F), Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1)}; } @@ -105,7 +109,8 @@ inline const Math::Vec4 DecodeRGB5A1(const u8* bytes) { * @return Result color decoded as Math::Vec4 */ inline const Math::Vec4 DecodeRGBA4(const u8* bytes) { - const u16_le pixel = *reinterpret_cast(bytes); + u16_le pixel; + std::memcpy(&pixel, bytes, sizeof(pixel)); return {Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF), Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF)}; } @@ -116,7 +121,9 @@ inline const Math::Vec4 DecodeRGBA4(const u8* bytes) { * @return Depth value as an u32 */ inline u32 DecodeD16(const u8* bytes) { - return *reinterpret_cast(bytes); + u16_le data; + std::memcpy(&data, bytes, sizeof(data)); + return data; } /** @@ -175,8 +182,10 @@ inline void EncodeRG8(const Math::Vec4& color, u8* bytes) { * @param bytes Destination pointer to store encoded color */ inline void EncodeRGB565(const Math::Vec4& color, u8* bytes) { - *reinterpret_cast(bytes) = + const u16_le data = (Convert8To5(color.r()) << 11) | (Convert8To6(color.g()) << 5) | Convert8To5(color.b()); + + std::memcpy(bytes, &data, sizeof(data)); } /** @@ -185,9 +194,10 @@ inline void EncodeRGB565(const Math::Vec4& color, u8* bytes) { * @param bytes Destination pointer to store encoded color */ inline void EncodeRGB5A1(const Math::Vec4& color, u8* bytes) { - *reinterpret_cast(bytes) = (Convert8To5(color.r()) << 11) | - (Convert8To5(color.g()) << 6) | - (Convert8To5(color.b()) << 1) | Convert8To1(color.a()); + const u16_le data = (Convert8To5(color.r()) << 11) | (Convert8To5(color.g()) << 6) | + (Convert8To5(color.b()) << 1) | Convert8To1(color.a()); + + std::memcpy(bytes, &data, sizeof(data)); } /** @@ -196,9 +206,10 @@ inline void EncodeRGB5A1(const Math::Vec4& color, u8* bytes) { * @param bytes Destination pointer to store encoded color */ inline void EncodeRGBA4(const Math::Vec4& color, u8* bytes) { - *reinterpret_cast(bytes) = (Convert8To4(color.r()) << 12) | - (Convert8To4(color.g()) << 8) | - (Convert8To4(color.b()) << 4) | Convert8To4(color.a()); + const u16 data = (Convert8To4(color.r()) << 12) | (Convert8To4(color.g()) << 8) | + (Convert8To4(color.b()) << 4) | Convert8To4(color.a()); + + std::memcpy(bytes, &data, sizeof(data)); } /** @@ -207,7 +218,8 @@ inline void EncodeRGBA4(const Math::Vec4& color, u8* bytes) { * @param bytes Pointer where to store the encoded value */ inline void EncodeD16(u32 value, u8* bytes) { - *reinterpret_cast(bytes) = value & 0xFFFF; + const u16_le data = static_cast(value); + std::memcpy(bytes, &data, sizeof(data)); } /** -- cgit v1.2.3 From 5a9c00ea04c9010ead9790054d6034306ebbdc92 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 8 Aug 2018 16:17:38 -0400 Subject: common/color: Remove unnecessary const qualifiers on return types These are just superfluous and not necessesary --- src/common/color.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/common') diff --git a/src/common/color.h b/src/common/color.h index 2e56af5a9..0379040be 100644 --- a/src/common/color.h +++ b/src/common/color.h @@ -57,7 +57,7 @@ constexpr u8 Convert8To6(u8 value) { * @param bytes Pointer to encoded source color * @return Result color decoded as Math::Vec4 */ -inline const Math::Vec4 DecodeRGBA8(const u8* bytes) { +inline Math::Vec4 DecodeRGBA8(const u8* bytes) { return {bytes[3], bytes[2], bytes[1], bytes[0]}; } @@ -66,7 +66,7 @@ inline const Math::Vec4 DecodeRGBA8(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Math::Vec4 */ -inline const Math::Vec4 DecodeRGB8(const u8* bytes) { +inline Math::Vec4 DecodeRGB8(const u8* bytes) { return {bytes[2], bytes[1], bytes[0], 255}; } @@ -75,7 +75,7 @@ inline const Math::Vec4 DecodeRGB8(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Math::Vec4 */ -inline const Math::Vec4 DecodeRG8(const u8* bytes) { +inline Math::Vec4 DecodeRG8(const u8* bytes) { return {bytes[1], bytes[0], 0, 255}; } @@ -84,7 +84,7 @@ inline const Math::Vec4 DecodeRG8(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Math::Vec4 */ -inline const Math::Vec4 DecodeRGB565(const u8* bytes) { +inline Math::Vec4 DecodeRGB565(const u8* bytes) { u16_le pixel; std::memcpy(&pixel, bytes, sizeof(pixel)); return {Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F), @@ -96,7 +96,7 @@ inline const Math::Vec4 DecodeRGB565(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Math::Vec4 */ -inline const Math::Vec4 DecodeRGB5A1(const u8* bytes) { +inline Math::Vec4 DecodeRGB5A1(const u8* bytes) { u16_le pixel; std::memcpy(&pixel, bytes, sizeof(pixel)); return {Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F), @@ -108,7 +108,7 @@ inline const Math::Vec4 DecodeRGB5A1(const u8* bytes) { * @param bytes Pointer to encoded source color * @return Result color decoded as Math::Vec4 */ -inline const Math::Vec4 DecodeRGBA4(const u8* bytes) { +inline Math::Vec4 DecodeRGBA4(const u8* bytes) { u16_le pixel; std::memcpy(&pixel, bytes, sizeof(pixel)); return {Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF), @@ -140,7 +140,7 @@ inline u32 DecodeD24(const u8* bytes) { * @param bytes Pointer to encoded source values * @return Resulting values stored as a Math::Vec2 */ -inline const Math::Vec2 DecodeD24S8(const u8* bytes) { +inline Math::Vec2 DecodeD24S8(const u8* bytes) { return {static_cast((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3]}; } -- cgit v1.2.3 From 6e90f0bf6a5c2c64263e0f3be016fd0caf8869ce Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 8 Aug 2018 16:42:06 -0400 Subject: common/logging: Add missing service log categories These weren't added when the services were introduced. --- src/common/logging/backend.cpp | 8 ++++++++ src/common/logging/log.h | 8 ++++++++ 2 files changed, 16 insertions(+) (limited to 'src/common') diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 355abd682..e80784c3c 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -171,15 +171,21 @@ void FileBackend::Write(const Entry& entry) { SUB(Service, ARP) \ SUB(Service, BCAT) \ SUB(Service, BPC) \ + SUB(Service, BTDRV) \ SUB(Service, BTM) \ SUB(Service, Capture) \ + SUB(Service, ERPT) \ + SUB(Service, ETicket) \ + SUB(Service, EUPLD) \ SUB(Service, Fatal) \ SUB(Service, FGM) \ SUB(Service, Friend) \ SUB(Service, FS) \ + SUB(Service, GRC) \ SUB(Service, HID) \ SUB(Service, LBL) \ SUB(Service, LDN) \ + SUB(Service, LDR) \ SUB(Service, LM) \ SUB(Service, Migration) \ SUB(Service, Mii) \ @@ -188,11 +194,13 @@ void FileBackend::Write(const Entry& entry) { SUB(Service, NFC) \ SUB(Service, NFP) \ SUB(Service, NIFM) \ + SUB(Service, NIM) \ SUB(Service, NS) \ SUB(Service, NVDRV) \ SUB(Service, PCIE) \ SUB(Service, PCTL) \ SUB(Service, PCV) \ + SUB(Service, PM) \ SUB(Service, PREPO) \ SUB(Service, PSC) \ SUB(Service, SET) \ diff --git a/src/common/logging/log.h b/src/common/logging/log.h index a889ebefa..e12f47f8f 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h @@ -58,15 +58,21 @@ enum class Class : ClassType { Service_Audio, ///< The Audio (Audio control) service Service_BCAT, ///< The BCAT service Service_BPC, ///< The BPC service + Service_BTDRV, ///< The Bluetooth driver service Service_BTM, ///< The BTM service Service_Capture, ///< The capture service + Service_ERPT, ///< The error reporting service + Service_ETicket, ///< The ETicket service + Service_EUPLD, ///< The error upload service Service_Fatal, ///< The Fatal service Service_FGM, ///< The FGM service Service_Friend, ///< The friend service Service_FS, ///< The FS (Filesystem) service + Service_GRC, ///< The game recording service Service_HID, ///< The HID (Human interface device) service Service_LBL, ///< The LBL (LCD backlight) service Service_LDN, ///< The LDN (Local domain network) service + Service_LDR, ///< The loader service Service_LM, ///< The LM (Logger) service Service_Migration, ///< The migration service Service_Mii, ///< The Mii service @@ -75,11 +81,13 @@ enum class Class : ClassType { Service_NFC, ///< The NFC (Near-field communication) service Service_NFP, ///< The NFP service Service_NIFM, ///< The NIFM (Network interface) service + Service_NIM, ///< The NIM service Service_NS, ///< The NS services Service_NVDRV, ///< The NVDRV (Nvidia driver) service Service_PCIE, ///< The PCIe service Service_PCTL, ///< The PCTL (Parental control) service Service_PCV, ///< The PCV service + Service_PM, ///< The PM service Service_PREPO, ///< The PREPO (Play report) service Service_PSC, ///< The PSC service Service_SET, ///< The SET (Settings) service -- cgit v1.2.3 From 3f82dad1e411130e309074a1547fb2104257f95d Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Fri, 3 Aug 2018 11:47:35 -0400 Subject: file_util: Add platform-specific slash option to SanitizePath --- src/common/file_util.cpp | 16 +++++++++++++--- src/common/file_util.h | 5 +++-- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 7aeda737f..190cac6d9 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -884,11 +884,21 @@ std::string_view RemoveTrailingSlash(std::string_view path) { return path; } -std::string SanitizePath(std::string_view path_) { +std::string SanitizePath(std::string_view path_, bool with_platform_slashes) { std::string path(path_); - std::replace(path.begin(), path.end(), '\\', '/'); + char type1 = '\\'; + char type2 = '/'; + + if (with_platform_slashes) { +#ifdef _WIN32 + type1 = '/'; + type2 = '\\'; +#endif + } + + std::replace(path.begin(), path.end(), type1, type2); path.erase(std::unique(path.begin(), path.end(), - [](char c1, char c2) { return c1 == '/' && c2 == '/'; }), + [type2](char c1, char c2) { return c1 == type2 && c2 == type2; }), path.end()); return std::string(RemoveTrailingSlash(path)); } diff --git a/src/common/file_util.h b/src/common/file_util.h index d0987fb57..ca63d7466 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -182,8 +182,9 @@ std::vector SliceVector(const std::vector& vector, size_t first, size_t la return std::vector(vector.begin() + first, vector.begin() + first + last); } -// Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. -std::string SanitizePath(std::string_view path); +// Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\' +// if windows and with_platform_slashes is true. +std::string SanitizePath(std::string_view path, bool with_platform_slashes = false); // simple wrapper for cstdlib file functions to // hopefully will make error checking easier -- cgit v1.2.3 From 2b6128fe0b8788318a4bbe1fc55ea14aed2981e4 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Mon, 6 Aug 2018 23:21:37 -0400 Subject: file_util: Use enum instead of bool for specifing path behavior --- src/common/file_util.cpp | 8 ++++---- src/common/file_util.h | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 190cac6d9..3ce590062 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -884,12 +884,12 @@ std::string_view RemoveTrailingSlash(std::string_view path) { return path; } -std::string SanitizePath(std::string_view path_, bool with_platform_slashes) { +std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) { std::string path(path_); - char type1 = '\\'; - char type2 = '/'; + char type1 = directory_separator == DirectorySeparator::BackwardSlash ? '/' : '\\'; + char type2 = directory_separator == DirectorySeparator::BackwardSlash ? '\\' : '/'; - if (with_platform_slashes) { + if (directory_separator == DirectorySeparator::PlatformDefault) { #ifdef _WIN32 type1 = '/'; type2 = '\\'; diff --git a/src/common/file_util.h b/src/common/file_util.h index ca63d7466..2711872ae 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -182,9 +182,12 @@ std::vector SliceVector(const std::vector& vector, size_t first, size_t la return std::vector(vector.begin() + first, vector.begin() + first + last); } +enum class DirectorySeparator { ForwardSlash, BackwardSlash, PlatformDefault }; + // Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\' -// if windows and with_platform_slashes is true. -std::string SanitizePath(std::string_view path, bool with_platform_slashes = false); +// depending if directory_separator is BackwardSlash or PlatformDefault and running on windows +std::string SanitizePath(std::string_view path, + DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash); // simple wrapper for cstdlib file functions to // hopefully will make error checking easier -- cgit v1.2.3