From 3470ed855d244100f37b0b7ab4c912d2b9365a93 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 27 Mar 2018 19:02:14 +0200 Subject: [PATCH] Critical fix for large primes search A lot of amicable candidates were skipped due to data format mismatch in CandidatesData vector. --- Amicable/OpenCL.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Amicable/OpenCL.cpp b/Amicable/OpenCL.cpp index 31c6d30..f6be22d 100644 --- a/Amicable/OpenCL.cpp +++ b/Amicable/OpenCL.cpp @@ -934,8 +934,32 @@ bool OpenCL::ProcessLargePrimes() } const num64 LargestCandidate = LowWord(SearchLimit::value / FirstPrime); - auto it = std::lower_bound(CandidatesData.begin(), CandidatesData.end(), LargestCandidate, [](const AmicableCandidate& a, num64 b) { return a.value <= b; }); - const num64 CandidatesCount = static_cast(it - CandidatesData.begin()); + num64 CandidatesCount; + { + const std::pair* packedCandidates = reinterpret_cast*>(CandidatesData.data()); + int a = 0; + int b = static_cast(CandidatesData.size()); + while (a < b) + { + const int c = (a + b) >> 1; + + num64 value = packedCandidates[c].first; + if (c >= CandidatesDataHighBitOffsets.first) + { + value |= 0x100000000ULL; + } + + if (value <= LargestCandidate) + { + a = c + 1; + } + else + { + b = c; + } + } + CandidatesCount = static_cast(a); + } const num64 TotalNumbersToCheck = CandidatesCount * LargePrimesCount;