From a449c4f749e82f4dd326fc3a73462834ee48c05f Mon Sep 17 00:00:00 2001 From: Madeline Busig Date: Mon, 7 Oct 2024 23:49:01 -0700 Subject: [PATCH] Add conditional test compilation and split matrix instantiations This commit adds the ability to conditionally compile tests. To implement this, common source files needed to be moved to a subdirectory `common`. Additionally, this commit splits each `mat` template instantiation into a separate source file. This enables the linker to discard unused instantations. If each instantiation is placed in the same file and also into the .iwram section, the linker will include every symbol in the resulting binary, leading to an extremely high IWRAM usage. To introduce this split, a new header file "mat_impl.hpp" was added containing implementation details for the template instantiations. "mat_impl.hpp" should ONLY be included by the source files containing explicit instantiations of `mat`. --- CMakeLists.txt | 11 ++++++++++- src/mat.cpp => include/mtl/mat_impl.hpp | 22 +++++----------------- src/{ => common}/log.cpp | 0 src/common/mat_1x2.cpp | 8 ++++++++ src/common/mat_1x3.cpp | 8 ++++++++ src/common/mat_1x4.cpp | 8 ++++++++ src/common/mat_2x2.cpp | 11 +++++++++++ src/common/mat_3x3.cpp | 11 +++++++++++ src/common/mat_4x4.cpp | 12 ++++++++++++ src/{ => common}/string.cpp | 1 + src/{ => common}/string_view.cpp | 0 11 files changed, 74 insertions(+), 18 deletions(-) rename src/mat.cpp => include/mtl/mat_impl.hpp (79%) rename src/{ => common}/log.cpp (100%) create mode 100644 src/common/mat_1x2.cpp create mode 100644 src/common/mat_1x3.cpp create mode 100644 src/common/mat_1x4.cpp create mode 100644 src/common/mat_2x2.cpp create mode 100644 src/common/mat_3x3.cpp create mode 100644 src/common/mat_4x4.cpp rename src/{ => common}/string.cpp (99%) rename src/{ => common}/string_view.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 366925e..049b6fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,15 +3,18 @@ cmake_minimum_required(VERSION 3.5) project(mtl LANGUAGES CXX C ASM) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +option(BUILD_TESTS "Build tests" ON) + message("Compiling MTL for ${CMAKE_SYSTEM_PROCESSOR}") # CONFIGURE_DEPENDS option was added in CMake v3.12. This option allows the # build system to automatically re-run CMake if the glob changes, solving the # major issue with globbing. May have a performance impact, but it should be # negligible compared to the time spent building. -file(GLOB mtl_sources_common LIST_DIRECTORIES false CONFIGURE_DEPENDS src/*.cpp src/*.c src/*.s src/tests/*.cpp) +file(GLOB mtl_sources_common LIST_DIRECTORIES false CONFIGURE_DEPENDS src/common/*.cpp src/common/*.c src/common/*.s) file(GLOB mtl_sources_armv4t LIST_DIRECTORIES false CONFIGURE_DEPENDS src/armv4t/*.cpp src/armv4t/*.c src/armv4t/*.s) file(GLOB mtl_sources_gba LIST_DIRECTORIES false CONFIGURE_DEPENDS src/gba/*.cpp src/gba/*.c src/gba/*.s) +file(GLOB mtl_sources_test LIST_DIRECTORIES false CONFIGURE_DEPENDS src/tests/*.cpp) set(mtl_include_common "include") set(mtl_include_gba "include/gba") @@ -39,6 +42,12 @@ if (CMAKE_SYSTEM_PROCESSOR STREQUAL "armv4t") target_link_libraries(${PROJECT_NAME} PUBLIC ${PROJECT_NAME}_armv4t) endif() +if (BUILD_TESTS STREQUAL "ON") + add_library(${PROJECT_NAME}_test OBJECT) + target_sources(${PROJECT_NAME}_test PRIVATE "${mtl_sources_test}") + target_link_libraries(${PROJECT_NAME} PUBLIC ${PROJECT_NAME}_test) +endif() + target_sources(${PROJECT_NAME}_common PRIVATE "${mtl_sources_common}") target_include_directories(${PROJECT_NAME}_common PUBLIC "${mtl_include_common}") target_link_libraries(${PROJECT_NAME} PUBLIC ${PROJECT_NAME}_common) diff --git a/src/mat.cpp b/include/mtl/mat_impl.hpp similarity index 79% rename from src/mat.cpp rename to include/mtl/mat_impl.hpp index 2a6c1f2..53df001 100644 --- a/src/mat.cpp +++ b/include/mtl/mat_impl.hpp @@ -1,3 +1,8 @@ +/* + * This file should only be included by files that explicitly instantiate + * matrix classes. + */ +#pragma once #pragma GCC push_options #pragma GCC optimize("unroll-loops") @@ -125,23 +130,6 @@ mat<4, 4> mat<4, 4>::translation(const vec& v) { return r; } -template class mat<2, 2>; -template class mat<3, 3>; -template class mat<4, 4>; -template class mat<1, 2>; -template class mat<1, 3>; -template class mat<1, 4>; - -template mat<2, 2> operator*(const vec<2>&, const mat<1, 2>&); -template mat<3, 3> operator*(const vec<3>&, const mat<1, 3>&); -template mat<4, 4> operator*(const vec<4>&, const mat<1, 4>&); - -template mat<4, 4> mat<4, 4>::operator*(const mat<4, 4>&) const; -template mat<3, 3> mat<3, 3>::operator*(const mat<3, 3>&) const; -template mat<2, 2> mat<2, 2>::operator*(const mat<2, 2>&) const; - -template mat<4, 4> mat<4, 4>::translation<4, true>(const vec<3>&); - } // namespace mtl #pragma GCC pop_options diff --git a/src/log.cpp b/src/common/log.cpp similarity index 100% rename from src/log.cpp rename to src/common/log.cpp diff --git a/src/common/mat_1x2.cpp b/src/common/mat_1x2.cpp new file mode 100644 index 0000000..99c47e6 --- /dev/null +++ b/src/common/mat_1x2.cpp @@ -0,0 +1,8 @@ +#include "mtl/mat_impl.hpp" + +namespace mtl { + +template class mat<1, 2>; + +} + diff --git a/src/common/mat_1x3.cpp b/src/common/mat_1x3.cpp new file mode 100644 index 0000000..fdfd43a --- /dev/null +++ b/src/common/mat_1x3.cpp @@ -0,0 +1,8 @@ +#include "mtl/mat_impl.hpp" + +namespace mtl { + +template class mat<1, 3>; + +} + diff --git a/src/common/mat_1x4.cpp b/src/common/mat_1x4.cpp new file mode 100644 index 0000000..a9a5c5e --- /dev/null +++ b/src/common/mat_1x4.cpp @@ -0,0 +1,8 @@ +#include "mtl/mat_impl.hpp" + +namespace mtl { + +template class mat<1, 4>; + +} + diff --git a/src/common/mat_2x2.cpp b/src/common/mat_2x2.cpp new file mode 100644 index 0000000..1592fde --- /dev/null +++ b/src/common/mat_2x2.cpp @@ -0,0 +1,11 @@ +#include "mtl/mat_impl.hpp" + +namespace mtl { + +template class mat<2, 2>; + +template mat<2, 2> operator*(const vec<2>&, const mat<1, 2>&); +template mat<2, 2> mat<2, 2>::operator*(const mat<2, 2>&) const; + +} + diff --git a/src/common/mat_3x3.cpp b/src/common/mat_3x3.cpp new file mode 100644 index 0000000..a08a43c --- /dev/null +++ b/src/common/mat_3x3.cpp @@ -0,0 +1,11 @@ +#include "mtl/mat_impl.hpp" + +namespace mtl { + +template class mat<3, 3>; + +template mat<3, 3> operator*(const vec<3>&, const mat<1, 3>&); +template mat<3, 3> mat<3, 3>::operator*(const mat<3, 3>&) const; + +} + diff --git a/src/common/mat_4x4.cpp b/src/common/mat_4x4.cpp new file mode 100644 index 0000000..cb7fd42 --- /dev/null +++ b/src/common/mat_4x4.cpp @@ -0,0 +1,12 @@ +#include "mtl/mat_impl.hpp" + +namespace mtl { + +template class mat<4, 4>; + +template mat<4, 4> operator*(const vec<4>&, const mat<1, 4>&); +template mat<4, 4> mat<4, 4>::operator*(const mat<4, 4>&) const; + +template mat<4, 4> mat<4, 4>::translation<4, true>(const vec<3>&); + +} diff --git a/src/string.cpp b/src/common/string.cpp similarity index 99% rename from src/string.cpp rename to src/common/string.cpp index dfe9a2a..c90697f 100644 --- a/src/string.cpp +++ b/src/common/string.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "mtl/utility.hpp" #include "mtl/string_view.hpp" diff --git a/src/string_view.cpp b/src/common/string_view.cpp similarity index 100% rename from src/string_view.cpp rename to src/common/string_view.cpp