The reductions by a reciprocal and of the row indices #
The high half of the product of two 64-bit words from the products of their 32-bit halves
(Ratpoints.Sieve.mulHi) is the high half of the product of natural numbers
(Ratpoints.Sieve.toNat_mulHi), so that the reductions by a reciprocal on 64-bit words
(Ratpoints.Sieve.mulMod, Ratpoints.Sieve.mulDiv) compute the remainder and the quotient where
the C code relies on them: with the reciprocal Ratpoints.Sieve.magic p, which exceeds 2 ^ 64 / p
by less than 1, for every u with u p < 2 ^ 64, and so for the values at most
Ratpoints.mulModLimit and the moduli up to 2 ^ 32; the quotient for p ≥ 2, since the
reciprocal 2 ^ 64 of 1 wraps to 0 on a word, as in the C code. So does the remainder of a
value of either sign (Ratpoints.Sieve.modMul), and the helper mod of the C code
(Ratpoints.Sieve.modLong) is the Euclidean % for every integer. The first two stages of the
sieve reduce the sums of a word number and the offset of a row modulo the moduli of their sieve
specs, by a multiplication with the reciprocal when the test small says that the word numbers of
the piece allow it and by the helper mod otherwise; for a sieve spec whose offset lies in the
range that the packing gives it (Ratpoints.Sieve.SieveSpec.Fits), both give the Euclidean
remainder.
Main definitions #
Ratpoints.Sieve.SieveSpec.Wraps: the row of a sieve spec repeats its first bit arrays after the firstp, where the chunked first stage reads it.Ratpoints.Sieve.SieveSpec.Fits: a sieve spec of a modulus0 < p ≤ Ratpoints.maxPrimeEvenwith an offset fromRatpoints.rowBiasto belowRatpoints.rowBias + 2 p, the endpand a row that repeats its first bit arrays.Ratpoints.Sieve.SmallFits: when the testsmallholds, the word numbers lie between-Ratpoints.rowBiasandRatpoints.rowBias - 2 Ratpoints.maxPrimeEven.Ratpoints.Sieve.StageFits: what the stages of the sieve need of their moduli, the reciprocals and the testsmall.
Main statements #
Ratpoints.maxPrimeEven_le_two_pow_10,Ratpoints.maxPrimeEven_le_two_pow_32: the moduli are below2 ^ 10, and so below2 ^ 32;Ratpoints.maxPrimeEven_le_sixty_four_mul_modWords: they fit the bit arrays ofRatpoints.modWordswords.Ratpoints.rowBias_le_two_pow_31:Ratpoints.rowBias, the lower bound of the bias of a modulus (the multiple of the modulus in an offset), is at most2 ^ 31.Ratpoints.mul_lt_two_pow_64_of_le_mulModLimit:u p < 2 ^ 64foru ≤ Ratpoints.mulModLimitandp ≤ 2 ^ 32.Ratpoints.mul_le_mulModLimit_of_le_div,Ratpoints.mul_lt_two_pow_63: the products offill_bp_listand ofclass_offsetsarelongs, those of the first branch offill_bp_listat mostRatpoints.mulModLimit.Ratpoints.rbaLength_eq,Ratpoints.rbaLength_pos,Ratpoints.natCast_rbaLength_pos,Ratpoints.natCast_rbaLength_dvd_two_pow: the widthRatpoints.rbaLengthof the bit arrays is64 Ratpoints.rbaPack, positive, and divides2 ^ nforn ≥ Ratpoints.rbaShift.Ratpoints.Sieve.toNat_mulHi:Ratpoints.Sieve.mulHi a bis⌊a b / 2 ^ 64⌋;Ratpoints.Sieve.toNat_hi_mul_add_lo,Ratpoints.Sieve.toNat_ite_bne_zero: the 128-bit suma m + rofexact_scalein its two words, and the quotient on two words capped at2 ^ 64 - 1.Ratpoints.Sieve.mulMod_eq,Ratpoints.Sieve.mulDiv_eq: the reductions on 64-bit words are those of the macros on natural numbers reduced modulo2 ^ 64.Ratpoints.Sieve.magic_mul_eq:magic p · p = 2 ^ 64 + δwithδ < p.Ratpoints.Sieve.mulMod_magic,Ratpoints.Sieve.mulDiv_magic: with the reciprocalRatpoints.Sieve.magic p, the reductions giveu % pand, forp ≥ 2,u / pforu p < 2 ^ 64;Ratpoints.Sieve.mulMod_magic_of_le_mulModLimit: the remainder of the values at mostRatpoints.mulModLimitmodulop ≤ 2 ^ 32.Ratpoints.Sieve.modMul_magic:mod_mulgives the Euclideana % pfor|a| p < 2 ^ 64.Ratpoints.Sieve.modLong_eq_emod: the helpermodof the C code is the Euclidean%.Ratpoints.Sieve.SieveSpec.Fits.ite_modMul_modLong_eq_emod_of_natAbs_le: the reduction bymod_mulunder the testsmalland by the helpermodotherwise is the Euclidean remainder of a value at mostRatpoints.mulModLimitin absolute value undersmall.Ratpoints.Sieve.SieveSpec.Fits.ite_modMul_modLong_eq_emod,Ratpoints.Sieve.SieveSpec.Fits.ite_modMul_modLong_add_eq_emod,Ratpoints.Sieve.SieveSpec.Fits.ite_mulMod_modLong_eq_toNat_emod: the first row index of the first stage one row at a time, the start of a row of the chunked first stage and the row index of the second stage are the Euclidean remainders of-w0 - offset, ofw0 + offsetand ofi + offset.
Implementation notes #
The file uses Lean core and Std only, so that the contracts of the sieve, in files without
Mathlib, can use it. The proofs use only the facts on the constants that decide checks next to
them, that Ratpoints.rbaPack > 0, that Ratpoints.mulModLimit < 2 ^ 32, that
Ratpoints.maxBitsInPrime ≤ 10 and that 2 Ratpoints.rowBias - 1 ≤ Ratpoints.mulModLimit, never
their values; Ratpoints.rowBias is generalized before lia, which otherwise fails on its value
with a deep recursion of the kernel. Ratpoints.mul_lt_two_pow_63 and
Ratpoints.rowBias_le_two_pow_31 (through Ratpoints.Sieve.SieveEntry.PackedFor.lt_two_pow_32)
discharge range obligations of the longs that the Deviation paragraphs of the model cite, and no
contract uses them.
The moduli #
The moduli below Ratpoints.maxPrimeEven are below 2 ^ 10, since maxBitsInPrime ≤ 10
(Ratpoints.maxBitsInPrime_le_ten), as the comment on Horner's rule in examine_power assumes
(find_points.c:2286-2287).
The moduli, below Ratpoints.maxPrimeEven, are below 2 ^ 32, the bound under which the
reductions by a reciprocal of the values at most Ratpoints.mulModLimit are exact
(Ratpoints.mul_lt_two_pow_64_of_le_mulModLimit) and the helper mod keeps its values longs
(Ratpoints.fitsLong_sixteen_mul, Ratpoints.fitsLong_add_sixteen_mul).
A prime power below Ratpoints.maxPrimeEven, the moduli of the parameters, fits the
Ratpoints.modWords words of the bit arrays of its tables and of the patterns of its rows.
Ratpoints.rowBias is at most 2 ^ 31, since 2 rowBias - 1 ≤ Ratpoints.mulModLimit < 2 ^ 32
(Ratpoints.two_mul_rowBias_sub_one_le_mulModLimit, Ratpoints.mulModLimit_lt_two_pow_32): the
bound of the offsets of the rows (Ratpoints.Sieve.SieveEntry.PackedFor.lt_two_pow_32).
A value at most Ratpoints.mulModLimit times a modulus p ≤ 2 ^ 32 is below 2 ^ 64, where
the reductions by a reciprocal are exact (Ratpoints.Sieve.mulMod_magic,
Ratpoints.Sieve.mulDiv_magic); this reads off only Ratpoints.mulModLimit_lt_two_pow_32.
The products x d p of fill_bp_list and class_offsets that a reduction by the reciprocal
of a modulus p ≤ Ratpoints.maxPrimeEven takes, for x ≤ 2 ^ 10 and d < p, are below 2 ^ 64,
where it is exact.
The products b dinv of the first branch of fill_bp_list, for
b ≤ Ratpoints.mulModLimit / Ratpoints.maxPrimeEven and dinv < p ≤ Ratpoints.maxPrimeEven, are at
most Ratpoints.mulModLimit, so longs that the reduction by the reciprocal of p reduces
exactly.
The products x d of the other branches of fill_bp_list, (b mod p) dinv, and of
class_offsets, a₀ rinv, for x ≤ 2 ^ 10 and d < p ≤ Ratpoints.maxPrimeEven, are longs.
The width of the bit arrays #
The width Ratpoints.rbaLength = 2 ^ Ratpoints.rbaShift of the bit arrays divides 2 ^ n for
n ≥ Ratpoints.rbaShift.
The high half of a product #
The reductions by a reciprocal #
RP_MULMOD on 64-bit words is the macro on natural numbers: the product m u reduced modulo
2 ^ 64, as the product of unsigned longs wraps, times p as an unsigned long, shifted right
by 64 bits.
RP_MULMOD with the reciprocal of a modulus 0 < p ≤ 2 ^ 32 computes the remainder of every
u ≤ Ratpoints.mulModLimit (Ratpoints.mul_lt_two_pow_64_of_le_mulModLimit).
The helper mod #
The helper mod of the C code is the Euclidean %, for every integer a and every b ≥ 0;
that its values stay longs is Ratpoints.fitsLong_sixteen_mul, Ratpoints.fitsLong_neg and
Ratpoints.fitsLong_add_sixteen_mul.
The row indices of the first two stages #
The row of the sieve spec s repeats its first p bit arrays in the Ratpoints.chunk - 1 bit
arrays after them: word κ < Ratpoints.rbaPack of bit array x < p + chunk - 1 is word κ of bit
array x mod p, the copies that the chunked first stage reads past the first p bit arrays from a
start below p (Ratpoints.Sieve.andRowsChunked).
Equations
Instances For
A sieve spec whose modulus p and offset lie in the range that the packing of the numerators
gives them (Ratpoints.Sieve.classOffsets) and whose row has the copies of its first bit arrays
with the end p, as the run sets them up (Ratpoints.Sieve.setRows): 0 < p ≤ Ratpoints.maxPrimeEven, the offset a residue below 2 p plus Ratpoints.rowBias, so that under
the test small the sums of a word number and the offset that the first two stages reduce are at
most Ratpoints.mulModLimit in absolute value
(Ratpoints.Sieve.SieveSpec.Fits.ite_modMul_modLong_eq_emod,
Ratpoints.Sieve.SieveSpec.Fits.ite_modMul_modLong_add_eq_emod,
Ratpoints.Sieve.SieveSpec.Fits.ite_mulMod_modLong_eq_toNat_emod), and the row repeating its first
bit arrays (Ratpoints.Sieve.SieveSpec.Wraps) where the chunked first stage reads it.
The modulus is positive.
The modulus is at most
Ratpoints.maxPrimeEven.The offset is at least
Ratpoints.rowBias.The offset is below
Ratpoints.rowBias + 2 p.The end of the walk of the chunked first stage through the row is the modulus.
- wraps : s.Wraps
The row repeats its first bit arrays after the first
p.
Instances For
When the test small of Ratpoints.Sieve.siftPiece holds, the word numbers from lo to hi
lie between -Ratpoints.rowBias and Ratpoints.rowBias - 2 Ratpoints.maxPrimeEven: the hypothesis
under which the first two stages may reduce by a multiplication.
Equations
- Ratpoints.Sieve.SmallFits small lo hi = (small = true → -↑Ratpoints.rowBias ≤ lo ∧ hi ≤ ↑Ratpoints.rowBias - 2 * ↑Ratpoints.maxPrimeEven)
Instances For
The test small of a piece, as Ratpoints.Sieve.siftPiece computes it, says what
Ratpoints.Sieve.SmallFits asks of its word numbers.
What the stages of the sieve need of the moduli specs with their reciprocals mgs, for the
word numbers from lo to hi: the sieve specs in the range of the packing, the reciprocals of
their moduli in the same order, and the test small of Ratpoints.Sieve.SmallFits.
The sieve specs are in the range of the packing.
The reciprocals are those of the moduli, in the same order.
- smallFits : SmallFits small lo hi
The test
smallfits the word numbers.
Instances For
The first k of the moduli, with their reciprocals.
The moduli after the first k, with their reciprocals.
For a word number -Ratpoints.rowBias ≤ w0 ≤ Ratpoints.rowBias - 2 Ratpoints.maxPrimeEven, the
value -w0 - offset that the first stage reduces is at most Ratpoints.mulModLimit in absolute
value.
For a word number -Ratpoints.rowBias ≤ i < Ratpoints.rowBias - 2 Ratpoints.maxPrimeEven, the
value i + offset that the second stage reduces lies between 0 and Ratpoints.mulModLimit.
The reduction of a modulo the modulus of a sieve spec in the range of the packing, by
mod_mul under the test small and by the helper mod otherwise, is the Euclidean remainder, for
|a| ≤ Ratpoints.mulModLimit under small.
The index of the first row of the first stage over a piece from the word number w0 on, by
mod_mul under the test small and by the helper mod otherwise, is the Euclidean remainder of
-w0 - offset.
The start of the row of the chunked first stage at the first word number w0 of a piece, by
mod_mul under the test small and by the helper mod otherwise, is the Euclidean remainder of
w0 + offset.
The row index of the second stage at the word number i, by a multiplication with the
reciprocal under the test small and by the helper mod otherwise, is the Euclidean remainder of
i + offset.