nonce iteration optimization

efficient and correct nonce iteration without duplicates
This commit is contained in:
cohcho 2020-09-05 08:46:56 +00:00
parent 0f09883429
commit b826985d05
3 changed files with 39 additions and 69 deletions

View file

@ -66,14 +66,12 @@ public:
inline bool nextRound(uint32_t rounds, uint32_t roundSize)
{
bool ok = true;
m_rounds[index()]++;
if ((m_rounds[index()] % rounds) == 0) {
for (size_t i = 0; i < N; ++i) {
*nonce(i) = Nonce::next(index(), *nonce(i), rounds * roundSize, currentJob().isNicehash(), &ok);
if (!ok) {
break;
if (!Nonce::next(index(), nonce(i), rounds * roundSize, currentJob().isNicehash(), nonceSize())) {
return false;
}
}
}
@ -83,7 +81,7 @@ public:
}
}
return ok;
return true;
}
@ -102,7 +100,7 @@ private:
for (size_t i = 0; i < N; ++i) {
memcpy(m_blobs[index()] + (i * size), job.blob(), size);
*nonce(i) = Nonce::next(index(), *nonce(i), reserveCount, job.isNicehash());
Nonce::next(index(), nonce(i), reserveCount, job.isNicehash(), nonceSize());
}
}
@ -125,41 +123,23 @@ inline uint32_t *xmrig::WorkerJob<1>::nonce(size_t)
template<>
inline bool xmrig::WorkerJob<1>::nextRound(uint32_t rounds, uint32_t roundSize)
{
bool ok = true;
m_rounds[index()]++;
uint32_t* n = nonce();
const uint32_t prev_nonce = *n;
if ((m_rounds[index()] % rounds) == 0) {
*n = Nonce::next(index(), *n, rounds * roundSize, currentJob().isNicehash(), &ok);
if (!Nonce::next(index(), n, rounds * roundSize, currentJob().isNicehash(), nonceSize())) {
return false;
}
if (nonceSize() == sizeof(uint64_t)) {
m_jobs[index()].nonce()[1] = n[1];
}
}
else {
*n += roundSize;
}
// Increment higher 32 bits of a 64-bit nonce when lower 32 bits overflow
if (!currentJob().isNicehash() && (nonceSize() == sizeof(uint64_t))) {
const bool wrapped = (*n < prev_nonce);
const bool wraps_this_round = (static_cast<uint64_t>(*n) + roundSize > (1ULL << 32));
// Account for the case when starting nonce hasn't wrapped yet, but some nonces in the current round will wrap
if (wrapped || wraps_this_round) {
// Set lower 32 bits to 0 when higher 32 bits change
Nonce::reset(index());
// Sets *n to 0 and Nonce::m_nonce[index] to the correct next value
*n = 0;
Nonce::next(index(), *n, rounds * roundSize, currentJob().isNicehash(), &ok);
++n[1];
Job& job = m_jobs[index()];
memcpy(job.blob(), blob(), job.size());
}
}
return ok;
return true;
}
@ -173,7 +153,7 @@ inline void xmrig::WorkerJob<1>::save(const Job &job, uint32_t reserveCount, Non
m_jobs[index()].setBackend(backend);
memcpy(blob(), job.blob(), job.size());
*nonce() = Nonce::next(index(), *nonce(), reserveCount, currentJob().isNicehash());
Nonce::next(index(), nonce(), reserveCount, currentJob().isNicehash(), nonceSize());
}