void Primes_filterStartStep(cncTag_t i, cncTag_t keep, cncTag_t base, cncTag_t count, PrimeFactor *factors, PrimesCtx *ctx) {
    // Filter first batch, using the entire range starting from `base'
    uIntPrime end = base + CANDIDATE_BATCH_COUNT;
    // Fix base so it's not a multiple of 2 or 3
    base |= 1; // add 1 if even
    if (base % 3 == 0) base += 2; // skip multiples of 3
    // Set up memory for storing prime candidate that pass this filter
    uIntPrime *candidates = cncItemCreateVector_candidates(CANDIDATE_BATCH_COUNT);
    CandidatesInfo *info = cncItemCreate_candidatesInfo();
    info->baseValue = base;
    // Start with inc=4 if base%3 is 1, or inc=2 if base%3=2
    uIntPrime startInc = 6-(base%3)*2;
    assert((base+startInc)%3 > 0 && "Increment should skip all multiples of 3");
    // Filter all candidate by the given batch of prime factor
    // Skip primes and multiples of 3 by alternating inc between 2 and 4
    u32 checkedCount = 0, confirmedCount = 0;
    for (uIntPrime n=base, inc=startInc; n<end; n+=inc, inc=6-inc) {
        bool belowBound = false;
        uIntPrime factorBound = ZSQRT(n); // Only need to check factor thru sqrt(n)
        for (u32 i=0; i<FACTOR_BATCH_COUNT && (belowBound = factors[i].prime<factorBound); i++) {
            // If you find a prime factor then n isn't prime
            if (DIVIDES(factors[i], n)) goto lbl_next_candidate;
        }
        // Completely confirmed that n is prime (checked all possible factors)
        if (!belowBound) confirmedCount++;
        // No prime factor, so this candidate passes
        candidates[checkedCount++] = n;
lbl_next_candidate:; // labeled continue (semi-colon needed to avoid empty block)
    }
    // Results out
    info->count = checkedCount;
    info->confirmedCount = confirmedCount;
    info->summarizeFlag = keep;
    scheduleNextFilter(i, 1, info, candidates, factors, ctx);
}
Example #2
0
int main() {
    int y = 2;
    int z = 3;

    printf("%d %d\n", y, z);
    SWAP(y,z);
    printf("%d %d\n", y, z);

    int x = 15;
    if (DIVIDES(y + z, x)) {
        printf("%d divides %d\n", y + z, x);
    } else {
        printf("%d does not divide %d\n", y + z, x);
    }

    printf("%d %d\n", x, y);
    z = MAX(x++, y++);
    printf("%d %d %d -- oops!\n", x, y, z);
}
/**
 * Step function defintion for "filterContinueStep"
 */
void Primes_filterContinueStep(cncTag_t i, cncTag_t j, PrimeFactor *factors, CandidatesInfo *info, uIntPrime *candidates, PrimesCtx *ctx) {
    // Filter all candidate by the given batch of prime factor
    // Skip primes and multiples of 3 by alternating inc between 2 and 4
    u32 confirmedCount = info->confirmedCount;
    u32 checkedCount = confirmedCount;
    for (u32 j=info->confirmedCount; j<info->count; j++) {
        bool belowBound = false;
        uIntPrime n = candidates[j];
        uIntPrime factorBound = ZSQRT(n); // Only need to check factor thru sqrt(n)
        for (u32 i=0; i<FACTOR_BATCH_COUNT && (belowBound = factors[i].prime<factorBound); i++) {
            // If you find a prime factor then n isn't prime
            if (DIVIDES(factors[i], n)) goto lbl_next_candidate;
        }
        // No prime factor, so this candidate passes
        candidates[checkedCount++] = n;
        // Completely confirmed that n is prime (checked all possible factors)
        if (!belowBound) confirmedCount++;
lbl_next_candidate:; // labeled continue (semi-colon needed to avoid empty block)
    }
    // Results out
    info->count = checkedCount;
    info->confirmedCount = confirmedCount;
    scheduleNextFilter(i, j+1, info, candidates, factors, ctx);
}