From 146cf358925c433e1b9af022cab8e915160467f5 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 2 Oct 2017 07:52:41 +0200 Subject: compat/simple-mat: add constexpr --- compat/simple-mat.hpp | 78 +++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/compat/simple-mat.hpp b/compat/simple-mat.hpp index d983f79b..3b2d89eb 100644 --- a/compat/simple-mat.hpp +++ b/compat/simple-mat.hpp @@ -14,7 +14,7 @@ #include #include -namespace { +namespace simple_mat_detail { // last param to fool SFINAE into overloading template struct equals @@ -49,7 +49,6 @@ namespace { { enum { value = h * w == sizeof...(ts) }; }; -} template class Mat @@ -59,54 +58,54 @@ class Mat public: template std::enable_if_t::value, num> - inline operator()(int i) const { return data[i][0]; } + constexpr inline operator()(int i) const { return data[i][0]; } template std::enable_if_t::value, num> - inline operator()(int i) const { return data[0][i]; } + constexpr inline operator()(int i) const { return data[0][i]; } template std::enable_if_t::value, num&> - inline operator()(int i) { return data[i][0]; } + constexpr inline operator()(int i) { return data[i][0]; } template std::enable_if_t::value, num&> - inline operator()(int i) { return data[0][i]; } + constexpr inline operator()(int i) { return data[0][i]; } template std::enable_if_t::value, num> - inline operator()(unsigned i) const { return data[i][0]; } + constexpr inline operator()(unsigned i) const { return data[i][0]; } template std::enable_if_t::value, num> - inline operator()(unsigned i) const { return data[0][i]; } + constexpr inline operator()(unsigned i) const { return data[0][i]; } template std::enable_if_t::value, num&> - inline operator()(unsigned i) { return data[i][0]; } + constexpr inline operator()(unsigned i) { return data[i][0]; } template std::enable_if_t::value, num&> - inline operator()(unsigned i) { return data[0][i]; } + constexpr inline operator()(unsigned i) { return data[0][i]; } #define OPENTRACK_ASSERT_SWIZZLE static_assert(P == h_ && Q == w_, "") template std::enable_if_t::value, num> - x() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(0); } + constexpr inline x() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(0); } template std::enable_if_t::value, num> - y() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(1); } + constexpr inline y() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(1); } template std::enable_if_t::value, num> - z() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(2); } + constexpr inline z() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(2); } template std::enable_if_t::value, num> - w() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(3); } + constexpr inline w() const { OPENTRACK_ASSERT_SWIZZLE; return operator()(3); } template std::enable_if_t::value, num&> - x() { OPENTRACK_ASSERT_SWIZZLE; return operator()(0); } + constexpr inline x() { OPENTRACK_ASSERT_SWIZZLE; return operator()(0); } template std::enable_if_t::value, num&> - y() { OPENTRACK_ASSERT_SWIZZLE; return operator()(1); } + constexpr inline y() { OPENTRACK_ASSERT_SWIZZLE; return operator()(1); } template std::enable_if_t::value, num&> - z() { OPENTRACK_ASSERT_SWIZZLE; return operator()(2); } + constexpr inline z() { OPENTRACK_ASSERT_SWIZZLE; return operator()(2); } template std::enable_if_t::value, num&> - w() { OPENTRACK_ASSERT_SWIZZLE; return operator()(3); } + constexpr inline w() { OPENTRACK_ASSERT_SWIZZLE; return operator()(3); } // parameters w_ and h_ are rebound so that SFINAE occurs // removing them causes a compile-time error -sh 20150811 @@ -119,7 +118,7 @@ public: const num val = dot(*this); - if (std::fabs(val) < num(1e-4)) + if (val < num(1e-4)) return num(0); else return std::sqrt(val); @@ -127,12 +126,12 @@ public: template std::enable_if_t::value, num> - dot(const Mat& p2) const + constexpr dot(const Mat& p2) const { static_assert(P == h_ && Q == w_, ""); num ret = 0; - static constexpr int len = vector_len::value; + constexpr int len = vector_len::value; for (int i = 0; i < len; i++) ret += operator()(i) * p2(i); return ret; @@ -140,17 +139,17 @@ public: template std::enable_if_t::value, Mat::P, is_dim3::Q>> - cross(const Mat& b) const + constexpr cross(const Mat& b) const { static_assert(P == h_ && Q == w_, ""); - decltype(*this)& a = *this; + auto& a = *this; return Mat(a.y()*b.z() - a.z()*b.y(), a.z()*b.x() - a.x()*b.z(), a.x()*b.y() - a.y()*b.x()); } - Mat operator+(const Mat& other) const + constexpr Mat operator+(const Mat& other) const { Mat ret; for (int j = 0; j < h_; j++) @@ -159,7 +158,7 @@ public: return ret; } - Mat operator-(const Mat& other) const + constexpr Mat operator-(const Mat& other) const { Mat ret; for (int j = 0; j < h_; j++) @@ -168,7 +167,7 @@ public: return ret; } - Mat operator+(const num& other) const + constexpr Mat operator+(const num& other) const { Mat ret; for (int j = 0; j < h_; j++) @@ -177,7 +176,7 @@ public: return ret; } - Mat operator-(const num& other) const + constexpr Mat operator-(const num& other) const { Mat ret; for (int j = 0; j < h_; j++) @@ -187,7 +186,7 @@ public: } template - Mat operator*(const Mat& other) const + constexpr Mat operator*(const Mat& other) const { Mat ret; for (int k = 0; k < h_; k++) @@ -200,11 +199,11 @@ public: return ret; } - inline num operator()(int j, int i) const { return data[j][i]; } - inline num& operator()(int j, int i) { return data[j][i]; } + constexpr inline num operator()(int j, int i) const { return data[j][i]; } + constexpr inline num& operator()(int j, int i) { return data[j][i]; } - inline num operator()(unsigned j, unsigned i) const { return data[j][i]; } - inline num& operator()(unsigned j, unsigned i) { return data[j][i]; } + constexpr inline num operator()(unsigned j, unsigned i) const { return data[j][i]; } + constexpr inline num& operator()(unsigned j, unsigned i) { return data[j][i]; } #ifdef __GNUG__ # pragma GCC diagnostic push @@ -213,7 +212,7 @@ public: template::value>> - Mat(const ts... xs) : data{static_cast(xs)...} + constexpr Mat(const ts... xs) : data{static_cast(xs)...} { static_assert(h__ == h_ && w__ == w_, ""); } @@ -222,7 +221,7 @@ public: # pragma GCC diagnostic pop #endif - Mat() + constexpr Mat() { for (int j = 0; j < h_; j++) for (int i = 0; i < w_; i++) @@ -258,7 +257,7 @@ public: return ret; } - Mat t() const + constexpr Mat t() const { Mat ret; @@ -271,13 +270,13 @@ public: }; template -Mat operator*(num scalar, const Mat& mat) +constexpr Mat operator*(num scalar, const Mat& mat) { return mat * scalar; } template -Mat operator*(const Mat& self, num other) +constexpr Mat operator*(const Mat& self, num other) { Mat ret; for (int j = 0; j < h_; j++) @@ -285,3 +284,8 @@ Mat operator*(const Mat& self, num other) ret(j, i) = self(j, i) * other; return ret; } + +} // ns simple_mat_detail + +template +using Mat = simple_mat_detail::Mat; -- cgit v1.2.3