From 505fe8af866792f986e5957d512cdceac3e37218 Mon Sep 17 00:00:00 2001 From: dsh Date: Fri, 6 Nov 2026 13:54:46 +0800 Subject: [PATCH] Make it build under clang Found with scoop's llvm 23.1.5 (clang-cl), which is stricter than MSVC about non-type template arguments: candidate template ignored: substitution failure: deduced non-type template argument does not have the same type as the corresponding template parameter ('unsigned long long' vs 'int') block_cost deduced its N from std::array, and that deduces size_t; clang refuses to convert size_t to the int parameter, MSVC allows it. N is now size_t. Every target (r8forge, synth_test, derive, cost_par, cost_search) builds clean with clang-cl and the clang-built binaries assemble test.r8asm to the same 1674 characters, pass the eight combination check and derive the same bit_raw_add table. Two other portability fixes in the same pass: * cost_search used _strtoui64, which is MSVC only -- now std::strtoull * the CMake project now finds Threads and links Threads::Threads, which clang and MinGW need to pull in the thread runtime * if spawning a worker throws (no thread runtime available) the search now carries on with the threads it did get instead of failing --- CMakeLists.txt | 3 +++ include/r8forge/r8veritas_phase.hh | 15 ++++++++++++--- tests/cost_search.cc | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 97233aa..ddf1b16 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) include_directories(include) +find_package(Threads REQUIRED) + add_library(r8forge_core src/r8asm.cc src/synth.cc) +target_link_libraries(r8forge_core PUBLIC Threads::Threads) # 别再 set(CMAKE_CXX_FLAGS ...) 了:那会把默认 flag 整个换掉,MSVC 下丢 /EHsc、 # 还会把 -O3 塞进 Debug 和 /RTC1 打架。Release 配置本来就带 -O3 / /O2 和 NDEBUG。 diff --git a/include/r8forge/r8veritas_phase.hh b/include/r8forge/r8veritas_phase.hh index 7d4f0f7..99513ca 100644 --- a/include/r8forge/r8veritas_phase.hh +++ b/include/r8forge/r8veritas_phase.hh @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -65,7 +66,9 @@ template struct engine { }; // cost of one block on the tape (same model as r8veritas::solve_cost) -template +// N has to be size_t here: std::array deduces N as size_t, and clang +// refuses to convert that to an int non-type parameter (MSVC lets it pass) +template inline int block_cost(const std::array &addr, int sacrifice, int mask, bool or_mode) { int lo = addr[size_t(sacrifice)], hi = lo, cnt = 0; @@ -232,9 +235,15 @@ result_t search(const typename engine::key &start, } } }; + // a stalled or unavailable thread runtime must not lose the search: if + // spawning fails, carry on with however many threads actually started std::vector pool; - for (unsigned t = 1; t < nthreads; t++) - pool.emplace_back(body, t); + try { + for (unsigned t = 1; t < nthreads; t++) + pool.emplace_back(body, t); + } catch (const std::system_error &) { + // fall through: run on the threads we have + } body(0); for (auto &th : pool) th.join(); diff --git a/tests/cost_search.cc b/tests/cost_search.cc index 95ee572..8e8cd00 100644 --- a/tests/cost_search.cc +++ b/tests/cost_search.cc @@ -36,7 +36,7 @@ int main(int argc, char **argv) { opt.max_cost = limit; opt.verbose = true; if (argc > 3) - opt.max_states = _strtoui64(argv[3], nullptr, 10); + opt.max_states = size_t(std::strtoull(argv[3], nullptr, 10)); const auto res = v.solve_cost(compressed, opt); std::cout << "limit " << limit << ", addresses ";