From 3631c5d6986fd807effd934009c23a57129fa206 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 11 Apr 2025 23:10:02 +0300 Subject: [PATCH 1/6] replaced with STL relization --- include/omath/mat.hpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index 6b53821..d8cf403 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -9,6 +9,7 @@ #include #include #include "omath/vector3.hpp" +#include #ifdef near @@ -141,12 +142,7 @@ namespace omath [[nodiscard]] constexpr Type Sum() const noexcept { - Type sum = 0; - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - sum += At(i, j); - - return sum; + return std::accumulate(m_data.begin(), m_data.end(), Type(0)); } constexpr void Clear() noexcept From 466d8f7becc9ad6e903c76dcec9fce7bf995eef0 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 11 Apr 2025 23:20:16 +0300 Subject: [PATCH 2/6] improvement --- include/omath/mat.hpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index d8cf403..78737d2 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -198,9 +198,7 @@ namespace omath constexpr Mat& operator/=(const Type& f) noexcept { - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - At(i, j) /= f; + std::ranges::for_each(m_data,[&f](auto& val) {val /= f;}); return *this; } From 1b47f45af940098d62ecb97f8d4240e0cdaa9bc4 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 11 Apr 2025 23:30:07 +0300 Subject: [PATCH 3/6] improved naming --- include/omath/mat.hpp | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index 78737d2..55f61ee 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -175,9 +175,7 @@ namespace omath constexpr Mat& operator*=(const Type& f) noexcept { - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - At(i, j) *= f; + std::ranges::for_each(m_data,[&f](auto& val) {val *= f;}); return *this; } @@ -189,45 +187,39 @@ namespace omath } [[nodiscard]] - constexpr Mat operator*(const Type& f) const noexcept + constexpr Mat operator*(const Type& value) const noexcept { Mat result(*this); - result *= f; + result *= value; return result; } - constexpr Mat& operator/=(const Type& f) noexcept + constexpr Mat& operator/=(const Type& value) noexcept { - std::ranges::for_each(m_data,[&f](auto& val) {val /= f;}); + std::ranges::for_each(m_data,[&value](auto& val) {val /= value;}); return *this; } [[nodiscard]] - constexpr Mat operator/(const Type& f) const noexcept + constexpr Mat operator/(const Type& value) const noexcept { Mat result(*this); - result /= f; + result /= value; return result; } constexpr Mat& operator=(const Mat& other) noexcept { - if (this == &other) - return *this; - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - At(i, j) = other.At(i, j); + if (this != &other) + m_data = other.m_data; + return *this; } constexpr Mat& operator=(Mat&& other) noexcept { - if (this == &other) - return *this; - - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - At(i, j) = other.At(i, j); + if (this != &other) + m_data = std::move(other.m_data); return *this; } From e08c22f6047ebea81cadf767a3dcfa28d53f3d31 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 11 Apr 2025 23:54:56 +0300 Subject: [PATCH 4/6] added new build option --- CMakeLists.txt | 6 ++++++ include/omath/mat.hpp | 9 +++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a693502..f5684e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,8 @@ option(OMATH_USE_AVX2 "Omath will use AVX2 to boost performance" ON) option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types to imgui types" OFF) option(OMATH_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF) option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF) +option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON) + if (OMATH_BUILD_AS_SHARED_LIBRARY) add_library(omath SHARED source/matrix.cpp) @@ -45,6 +47,10 @@ if (OMATH_USE_AVX2) target_compile_definitions(omath PUBLIC OMATH_USE_AVX2) endif() +if (OMATH_SUPRESS_SAFETY_CHECKS) + target_compile_definitions(omath PUBLIC OMATH_SUPRESS_SAFETY_CHECKS) +endif() + set_target_properties(omath PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index 55f61ee..f73fb20 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -118,9 +118,10 @@ namespace omath [[nodiscard]] constexpr const Type& At(const size_t rowIndex, const size_t columnIndex) const { +#if !defined(NDEBUG) && defined(OMATH_SUPRESS_SAFETY_CHECKS) if (rowIndex >= Rows || columnIndex >= Columns) throw std::out_of_range("Index out of range"); - +#endif if constexpr (StoreType == MatStoreType::ROW_MAJOR) return m_data[rowIndex * Columns + columnIndex]; @@ -175,7 +176,7 @@ namespace omath constexpr Mat& operator*=(const Type& f) noexcept { - std::ranges::for_each(m_data,[&f](auto& val) {val *= f;}); + std::ranges::for_each(m_data, [&f](auto& val) {val *= f;}); return *this; } @@ -196,7 +197,7 @@ namespace omath constexpr Mat& operator/=(const Type& value) noexcept { - std::ranges::for_each(m_data,[&value](auto& val) {val /= value;}); + std::ranges::for_each(m_data, [&value](auto& val) {val /= value;}); return *this; } @@ -243,7 +244,7 @@ namespace omath if constexpr (Rows == 1) return At(0, 0); - else if constexpr (Rows == 2) + if constexpr (Rows == 2) return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0); else { From 4a7a63193208ff23fb61b81f9074718ee3c09024 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 11 Apr 2025 23:57:56 +0300 Subject: [PATCH 5/6] added const method to mat --- include/omath/mat.hpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index f73fb20..8b89a52 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -92,6 +92,12 @@ namespace omath return At(row, col); } + [[nodiscard]] + constexpr Type& operator[](const size_t row, const size_t col) const + { + return At(row, col); + } + constexpr Mat(Mat&& other) noexcept { m_data = std::move(other.m_data); From 14acebad5febeab9e515816485e1fecf01c9e04d Mon Sep 17 00:00:00 2001 From: Orange Date: Sat, 12 Apr 2025 00:04:07 +0300 Subject: [PATCH 6/6] fixed tests --- tests/general/unit_test_mat.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/general/unit_test_mat.cpp b/tests/general/unit_test_mat.cpp index 97b94d1..43b25c1 100644 --- a/tests/general/unit_test_mat.cpp +++ b/tests/general/unit_test_mat.cpp @@ -166,8 +166,10 @@ TEST_F(UnitTestMat, StaticMethod_ToScreenMat) // Test exception handling in At() method TEST_F(UnitTestMat, Method_At_OutOfRange) { +#if !defined(NDEBUG) && defined(OMATH_SUPRESS_SAFETY_CHECKS) EXPECT_THROW(std::ignore = m2.At(2, 0), std::out_of_range); EXPECT_THROW(std::ignore = m2.At(0, 2), std::out_of_range); +#endif } // Test Determinant for 3x3 matrix