From be8573d4f52be92967434de9f9e22985f9257160 Mon Sep 17 00:00:00 2001 From: Aaron Boxer Date: Sat, 4 May 2024 18:12:29 -0400 Subject: [PATCH] convert most of core to header only --- CMakeLists.txt | 5 +++-- hwy/abort.cc | 4 ++++ hwy/abort.h | 4 ++++ hwy/abort_test.cc | 2 ++ hwy/aligned_allocator.cc | 4 ++++ hwy/aligned_allocator.h | 5 +++++ hwy/aligned_allocator_test.cc | 3 +++ hwy/base_test.cc | 3 +++ hwy/examples/profiler_example.cc | 3 +++ hwy/nanobenchmark.cc | 3 +++ hwy/nanobenchmark.h | 4 ++++ hwy/print.cc | 9 +++++++++ hwy/print.h | 4 ++++ hwy/targets.cc | 12 ++++++++++++ hwy/targets.h | 4 ++++ hwy/timer.cc | 13 ++++++++++++- hwy/timer.h | 6 ++++++ 17 files changed, 85 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f1ced8176..031153b4fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,7 @@ if(POLICY CMP0128) cmake_policy(SET CMP0128 NEW) endif() -project(hwy VERSION 1.2.0) # Keep in sync with highway.h version +project(hwy VERSION 1.2.0 LANGUAGES CXX) # Keep in sync with highway.h version # `hwy` is lowercase to handle find_package() in Config mode: set(namespace "${PROJECT_NAME}::") @@ -179,13 +179,14 @@ if (NOT HWY_CMAKE_HEADER_ONLY) hwy/abort.cc hwy/aligned_allocator.cc hwy/nanobenchmark.cc - hwy/per_target.cc hwy/print.cc hwy/targets.cc hwy/timer.cc ) endif() +list(APPEND HWY_SOURCES hwy/per_target.cc) + set(HWY_TEST_SOURCES hwy/tests/hwy_gtest.h hwy/tests/test_util-inl.h diff --git a/hwy/abort.cc b/hwy/abort.cc index ce4d5016e5..a7efa18c78 100644 --- a/hwy/abort.cc +++ b/hwy/abort.cc @@ -20,6 +20,10 @@ namespace hwy { namespace { + +#ifdef HWY_HEADER_ONLY +inline +#endif std::string GetBaseName(std::string const& file_name) { auto last_slash = file_name.find_last_of("/\\"); return file_name.substr(last_slash + 1); diff --git a/hwy/abort.h b/hwy/abort.h index aaa707765d..88cd884a85 100644 --- a/hwy/abort.h +++ b/hwy/abort.h @@ -23,4 +23,8 @@ AbortFunc SetAbortFunc(AbortFunc func); } // namespace hwy +#ifdef HWY_HEADER_ONLY +#include "abort.cc" +#endif + #endif // HIGHWAY_HWY_ABORT_H_ diff --git a/hwy/abort_test.cc b/hwy/abort_test.cc index fbf5886b4d..d8fbd57749 100644 --- a/hwy/abort_test.cc +++ b/hwy/abort_test.cc @@ -12,12 +12,14 @@ namespace hwy { #ifdef GTEST_HAS_DEATH_TEST +#ifndef HWY_HEADER_ONLY namespace { std::string GetBaseName(std::string const& file_name) { auto last_slash = file_name.find_last_of("/\\"); return file_name.substr(last_slash + 1); } } // namespace +#endif TEST(AbortDeathTest, AbortDefault) { std::string expected = std::string("Abort at ") + GetBaseName(__FILE__) + diff --git a/hwy/aligned_allocator.cc b/hwy/aligned_allocator.cc index b88a64e24e..6a7b745a9b 100644 --- a/hwy/aligned_allocator.cc +++ b/hwy/aligned_allocator.cc @@ -23,6 +23,10 @@ #include #include "hwy/base.h" +#ifdef HWY_HEADER_ONLY +#include "hwy/abort.h" +#endif + namespace hwy { namespace { diff --git a/hwy/aligned_allocator.h b/hwy/aligned_allocator.h index 6274c5d192..ca7f751935 100644 --- a/hwy/aligned_allocator.h +++ b/hwy/aligned_allocator.h @@ -420,4 +420,9 @@ class AlignedNDArray { }; } // namespace hwy + +#ifdef HWY_HEADER_ONLY +#include "aligned_allocator.cc" +#endif + #endif // HIGHWAY_HWY_ALIGNED_ALLOCATOR_H_ diff --git a/hwy/aligned_allocator_test.cc b/hwy/aligned_allocator_test.cc index d634eca40c..d45e42df93 100644 --- a/hwy/aligned_allocator_test.cc +++ b/hwy/aligned_allocator_test.cc @@ -25,6 +25,9 @@ #include #include "hwy/base.h" +#ifdef HWY_HEADER_ONLY +#include "hwy/abort.h" +#endif #include "hwy/per_target.h" #include "hwy/tests/hwy_gtest.h" #include "hwy/tests/test_util-inl.h" // HWY_ASSERT_EQ diff --git a/hwy/base_test.cc b/hwy/base_test.cc index d017b24474..852ecef124 100644 --- a/hwy/base_test.cc +++ b/hwy/base_test.cc @@ -14,6 +14,9 @@ // limitations under the License. #include "hwy/base.h" +#ifdef HWY_HEADER_ONLY +#include "hwy/abort.h" +#endif #include diff --git a/hwy/examples/profiler_example.cc b/hwy/examples/profiler_example.cc index a4a37ed9c7..ae195a38bc 100644 --- a/hwy/examples/profiler_example.cc +++ b/hwy/examples/profiler_example.cc @@ -17,6 +17,9 @@ #include "hwy/base.h" // Abort #include "hwy/profiler.h" #include "hwy/timer.h" +#ifdef HWY_HEADER_ONLY +#include "hwy/abort.h" +#endif namespace hwy { namespace { diff --git a/hwy/nanobenchmark.cc b/hwy/nanobenchmark.cc index ea5549f3d1..5a8eeb8538 100644 --- a/hwy/nanobenchmark.cc +++ b/hwy/nanobenchmark.cc @@ -27,6 +27,9 @@ #include "hwy/robust_statistics.h" #include "hwy/timer-inl.h" #include "hwy/timer.h" +#ifdef HWY_HEADER_ONLY +#include "hwy/abort.h" +#endif namespace hwy { namespace { diff --git a/hwy/nanobenchmark.h b/hwy/nanobenchmark.h index 46bfc4b0a8..d544a337ea 100644 --- a/hwy/nanobenchmark.h +++ b/hwy/nanobenchmark.h @@ -168,4 +168,8 @@ static inline size_t MeasureClosure(const Closure& closure, } // namespace hwy +#ifdef HWY_HEADER_ONLY +#include "nanobenchmark.cc" +#endif + #endif // HIGHWAY_HWY_NANOBENCHMARK_H_ diff --git a/hwy/print.cc b/hwy/print.cc index b2c68b1444..2c295fc4f5 100644 --- a/hwy/print.cc +++ b/hwy/print.cc @@ -23,6 +23,9 @@ namespace hwy { namespace detail { +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT void TypeName(const TypeInfo& info, size_t N, char* string100) { const char prefix = info.is_float ? 'f' : (info.is_signed ? 'i' : 'u'); // Omit the xN suffix for scalars. @@ -37,6 +40,9 @@ HWY_DLLEXPORT void TypeName(const TypeInfo& info, size_t N, char* string100) { } } +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT void ToString(const TypeInfo& info, const void* ptr, char* string100) { if (info.sizeof_t == 1) { @@ -105,6 +111,9 @@ HWY_DLLEXPORT void ToString(const TypeInfo& info, const void* ptr, } } +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT void PrintArray(const TypeInfo& info, const char* caption, const void* array_void, size_t N, size_t lane_u, size_t max_lanes) { diff --git a/hwy/print.h b/hwy/print.h index e61631e650..aa3ad8414f 100644 --- a/hwy/print.h +++ b/hwy/print.h @@ -72,4 +72,8 @@ HWY_NOINLINE void PrintArray(const T* value, size_t count) { } // namespace hwy +#ifdef HWY_HEADER_ONLY +#include "print.cc" +#endif + #endif // HWY_PRINT_H_ diff --git a/hwy/targets.cc b/hwy/targets.cc index decf6bdd13..abb11ee25e 100644 --- a/hwy/targets.cc +++ b/hwy/targets.cc @@ -689,6 +689,9 @@ int64_t DetectTargets() { } // namespace +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT void DisableTargets(int64_t disabled_targets) { supported_mask_ = static_cast(~disabled_targets); // This will take effect on the next call to SupportedTargets, which is @@ -699,11 +702,17 @@ HWY_DLLEXPORT void DisableTargets(int64_t disabled_targets) { GetChosenTarget().DeInit(); } +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT void SetSupportedTargetsForTest(int64_t targets) { supported_targets_for_test_ = targets; GetChosenTarget().DeInit(); // see comment above } +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT int64_t SupportedTargets() { int64_t targets = supported_targets_for_test_; if (HWY_LIKELY(targets == 0)) { @@ -738,6 +747,9 @@ HWY_DLLEXPORT int64_t SupportedTargets() { return targets == 0 ? HWY_STATIC_TARGET : targets; } +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT ChosenTarget& GetChosenTarget() { static ChosenTarget chosen_target; return chosen_target; diff --git a/hwy/targets.h b/hwy/targets.h index 5fe67f698d..00fd383550 100644 --- a/hwy/targets.h +++ b/hwy/targets.h @@ -341,4 +341,8 @@ HWY_DLLEXPORT ChosenTarget& GetChosenTarget(); } // namespace hwy +#ifdef HWY_HEADER_ONLY +#include "targets.cc" +#endif + #endif // HIGHWAY_HWY_TARGETS_H_ diff --git a/hwy/timer.cc b/hwy/timer.cc index 73e8ae39fa..54da31686b 100644 --- a/hwy/timer.cc +++ b/hwy/timer.cc @@ -116,11 +116,17 @@ void GetBrandString(char* cpu100) { } // namespace +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT double Now() { static const double mul = 1.0 / InvariantTicksPerSecond(); return static_cast(timer::Start()) * mul; } +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT bool HaveTimerStop(char* cpu100) { #if HWY_ARCH_X86 if (!HasRDTSCP()) { @@ -132,7 +138,9 @@ HWY_DLLEXPORT bool HaveTimerStop(char* cpu100) { cpu100[1] = '\0'; return true; } - +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT double InvariantTicksPerSecond() { #if HWY_ARCH_PPC && defined(__GLIBC__) && defined(__powerpc64__) return static_cast(__ppc_get_timebase_freq()); @@ -154,6 +162,9 @@ HWY_DLLEXPORT double InvariantTicksPerSecond() { #endif } +#ifdef HWY_HEADER_ONLY +inline +#endif HWY_DLLEXPORT uint64_t TimerResolution() { char cpu100[100]; bool can_use_stop = HaveTimerStop(cpu100); diff --git a/hwy/timer.h b/hwy/timer.h index 5cb7236521..05a997e8bd 100644 --- a/hwy/timer.h +++ b/hwy/timer.h @@ -63,4 +63,10 @@ static inline double SecondsSince(const Timestamp& t0) { } // namespace hwy +#ifdef HWY_HEADER_ONLY +#include "timer.cc" +#endif + #endif // HIGHWAY_HWY_TIMER_H_ + +