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<int, N>, 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
This commit is contained in:
1 parent
0bc5f1e6b8
commit
505fe8af86
3 files changed
+16
-4
No files matched your search
@@ -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。
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
@@ -65,7 +66,9 @@ template <int N, int K> struct engine {
|
||||
};
|
||||
|
||||
// cost of one block on the tape (same model as r8veritas::solve_cost)
|
||||
template <int N>
|
||||
// N has to be size_t here: std::array<int, N> deduces N as size_t, and clang
|
||||
// refuses to convert that to an int non-type parameter (MSVC lets it pass)
|
||||
template <std::size_t N>
|
||||
inline int block_cost(const std::array<int, N> &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<N, K>::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<std::thread> 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();
|
||||
|
||||
@@ -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 ";
|
||||
|
||||
Reference in new issue
Block a user