@@ -788,30 +788,33 @@ struct Factorizer {
788788 BigInteger batchRange;
789789 BigInteger batchNumber;
790790 BigInteger batchOffset;
791+ BigInteger batchTotal;
791792 size_t wheelEntryCount;
792793 size_t smoothPartsLimit;
793794 bool isIncomplete;
794795 std::vector<uint16_t > primes;
795796 ForwardFn forwardFn;
796797
797- Factorizer (const BigInteger &tfsqr, const BigInteger &tf, const BigInteger &tfsqrt, const BigInteger &range, size_t nodeId , size_t w , size_t spl, const std::vector< uint16_t > &p ,
798- ForwardFn fn)
799- : rng({}), toFactorSqr(tfsqr), toFactor(tf), toFactorSqrt(tfsqrt), batchRange(range), batchNumber(0U ), batchOffset(nodeId * range), wheelEntryCount(w ),
800- smoothPartsLimit (spl), isIncomplete(true ), primes(p), forwardFn(fn) {}
798+ Factorizer (const BigInteger &tfsqr, const BigInteger &tf, const BigInteger &tfsqrt, const BigInteger &range, size_t nodeCount , size_t nodeId , size_t w, size_t spl ,
799+ const std::vector< uint16_t > &p, ForwardFn fn)
800+ : rng({}), toFactorSqr(tfsqr), toFactor(tf), toFactorSqrt(tfsqrt), batchRange(range), batchNumber(0U ), batchOffset(nodeId * range), batchTotal(nodeCount * range ),
801+ wheelEntryCount (w), smoothPartsLimit(spl), isIncomplete(true ), primes(p), forwardFn(fn) {}
801802
802- BigInteger getNextBatch () {
803+ BigInteger getNextAltBatch () {
803804 std::lock_guard<std::mutex> lock (batchMutex);
804805
805806 if (batchNumber >= batchRange) {
806807 isIncomplete = false ;
807808 }
808809
809- return batchOffset + batchRange - ++batchNumber;
810+ const BigInteger halfIndex = batchOffset + (batchNumber++ >> 1U ) + 1U ;
811+
812+ return ((batchNumber & 1U ) ? batchTotal - halfIndex : halfIndex);
810813 }
811814
812815 BigInteger bruteForce (std::vector<boost::dynamic_bitset<size_t >> *inc_seqs) {
813816 // Up to wheel factorization, try all batches up to the square root of toFactor.
814- for (BigInteger batchNum = getNextBatch (); isIncomplete; batchNum = getNextBatch ()) {
817+ for (BigInteger batchNum = getNextAltBatch (); isIncomplete; batchNum = getNextAltBatch ()) {
815818 const BigInteger batchStart = batchNum * wheelEntryCount;
816819 const BigInteger batchEnd = batchStart + wheelEntryCount;
817820 for (BigInteger p = batchStart; p < batchEnd;) {
@@ -832,7 +835,7 @@ struct Factorizer {
832835 // Up to wheel factorization, try all batches up to the square root of toFactor.
833836 // Since the largest prime factors of these numbers is relatively small,
834837 // use the "exhaust" of brute force to produce smooth numbers for Quadratic Sieve.
835- for (BigInteger batchNum = getNextBatch (); isIncomplete; batchNum = getNextBatch ()) {
838+ for (BigInteger batchNum = getNextAltBatch (); isIncomplete; batchNum = getNextAltBatch ()) {
836839 const BigInteger batchStart = batchNum * wheelEntryCount;
837840 const BigInteger batchEnd = batchStart + wheelEntryCount;
838841 for (BigInteger p = batchStart; p < batchEnd;) {
@@ -1077,7 +1080,7 @@ std::string find_a_factor(const std::string &toFactorStr, const bool &isConOfSqr
10771080 batchSize = 1U ;
10781081 std::cout << " Warning: batch multiplier would lead to a batch size of 0, but it must be at least 1. (Defaulting to 1.)" ;
10791082 }
1080- Factorizer worker (toFactor * toFactor, toFactor, fullMaxBase, nodeRange, nodeId, wheelEntryCount, batchSize, primes, forward (SMALLEST_WHEEL));
1083+ Factorizer worker (toFactor * toFactor, toFactor, fullMaxBase, nodeRange, nodeCount, nodeId, wheelEntryCount, batchSize, primes, forward (SMALLEST_WHEEL));
10811084
10821085 const auto workerFn = [&toFactor, &inc_seqs, &isConOfSqr, &worker] {
10831086 // inc_seq needs to be independent per thread.
0 commit comments