Commit Graph
30568 Commits
Author SHA1 Message Date
Paul Eggert 37fbbc5373 factor: faster gcd_odd since 2nd is odd
* src/factor.c (gcd_odd, gcd2_odd): Speed up, given that the
second argument is always odd.
(gcd_odd): Avoid recomputing a temporary.
(gcd2_odd): Test for zero only if a multiple of B.
This saves an ‘assume’.
2025-07-09 17:12:40 -07:00
Paul Eggert 2ce3fc0c1b doc: update ‘factor’ benchmarks 2025-07-09 17:12:40 -07:00
Paul Eggert 3ee25abfd3 factor: prefer exact division
* src/factor.c (mp_factor_using_division, mp_factor_using_pollard_rho):
Use exact division when it suffices, as it’s typically faster.
2025-07-09 17:12:40 -07:00
Paul Eggert 73bb62f7df factor: tune submod2
* src/factor.c (submod2): Use ckd_sub to subtract by hand rather
than using sub_ddmmss plus a compare.  This speeds things up a
bit, on x86-64 with GCC 15 anyway.
2025-07-09 17:12:40 -07:00
Paul Eggert b0c58a8b29 factor: DELIM and DIVBLOCK no longer macros
* src/factor.c (DELIM): Now a constant, not a macro.
(divblock): Now a static inline function, not a macro DIVBLOCK.
All uses changed.
2025-07-09 17:12:40 -07:00
Paul Eggert d4bc32a5ff factor: simplify by assuming !PROVE_PRIMALITY
* src/factor.c (PROVE_PRIMALITY, mp_millerrabin): Remove.
All uses removed, and surrounding code simplified.
2025-07-09 17:12:40 -07:00
Paul Eggert a29d128162 factor: simplify by assuming USE_BAILLIE_PSW
* src/factor.c (make_uuint2, powm, powm2, millerrabin)
(millerrabin2, USE_BAILLIE_PSW): Remove; no longer used.
(prime_p, prime2_p): Simplify by assuming USE_BAILLIE_PSW.
2025-07-09 17:12:40 -07:00
Paul Eggert 5bab674646 factor: prime comments
* src/factor.c: Add comments re prime functions.
2025-07-09 17:12:40 -07:00
Paul Eggert fb20a7d3e4 factor: no ferror+fclose
* src/make-prime-list.c (main): Avoid parallel calls
to ferror and fclose.
2025-07-09 17:12:40 -07:00
Paul Eggert 86bddb0d53 factor: define SQUARE_OF_FIRST_OMITTED_PRIME
* src/make-prime-list.c (output_primes): Output
SQUARE_OF_FIRST_OMITTED_PRIME, not FIRST_OMITTED_PRIME.  All uses
changed.  This way, the uses don’t need to worry about casts to
avoid overflow.
2025-07-09 17:12:40 -07:00
Paul Eggert a9a8e0db47 factor: simplify add_ssaaaa default
* src/factor.c (add_ssaaaa): Simplify default implementation.
2025-07-09 17:12:40 -07:00
Paul Eggert 7cbc6c55cc factor: speed up ge2, gt2
* src/factor.c (ge2, gt2): Now branchless static functions.
2025-07-09 17:12:40 -07:00
Paul Eggert 097760c3de factor: improve millerrabin2 API
* src/factor.c (make_uuint2): New function.
(powm2, millerrabin2): Pass two-word args as uuints,
not as mp_limb_t const [2] pointers.  All uses changed.
(prime2_p): Rework to use the new API, fixing a FIXME.
2025-07-09 17:12:40 -07:00
Paul Eggert d3db83b879 factor: put FACTORS first
* src/factor.c (factor_using_division, mp_finish_up_in_single)
(mp_finish_in_single, factor_using_pollard_rho)
(factor_using_pollard_rho2, factor_up, factor):
Put FACTORS arg first, for consistency.
This is just a refactoring and should not affect speed.
2025-07-09 17:12:40 -07:00
Paul Eggert 4e2aeb31e0 factor: use 1-word code only when tested
* src/factor.c (print_factors): Use single-precision code only
when word size is 32 or 64, as it isn't tested for other sizes and
is known to not work when it the size is 128.  This change does
not affect any known practical platforms.
2025-07-09 17:12:40 -07:00
Paul Eggert 8eb114f080 factor: decrement Miller-Rabin tests from 25 to 24
This avoids some seemingly-unnecessary code in mpz_probab_prime_p.
* src/factor.c (MR_REPS): Shrink from 25 to 24, and add commentary.
2025-07-09 17:12:40 -07:00
Paul Eggert 5037ce930c factor: don’t prove primality
Suggested for consideration by Torbjörn Granlund in:
https://lists.gnu.org/r/coreutils/2025-01/msg00000.html
* src/factor.c (PROVE_PRIMALITY): Now defaults to false.
(mp_prime_p): Help the compiler by telling it mpz_prob_prime_p
returns nonnegative.
* tests/factor/create-test.sh (bigprime): Test 2^400 - 593,
since that’s now practical.
* tests/local.mk (factor_tests): Add new test.
2025-07-09 17:12:40 -07:00
Paul Eggert f726827e1f factor: add comments to factor.c 2025-07-09 17:12:39 -07:00
Paul Eggert 30b7851cdf factor: always use Baillie-PSW
* src/factor.c (USE_BAILLIE_PSW): New constant.
(prime_p, prime2_p): Use it, i.e., always use Baillie-PSW.
Do so by using mp_prime_p.  Do not tell GCC these functions
are pure; the pure mark was present only to pacify GCC
and is no longer needed now that thes functions call mp_prime_p.
2025-07-09 17:12:39 -07:00
Paul Eggert 3d442fd824 build: update gnulib submodule to latest 2025-07-09 17:12:39 -07:00
Paul Eggert a76600cda2 factor: use mpz_probab_prime_p
Inspired by a proposal by Torbjörn Granlund in:
https://lists.gnu.org/r/coreutils/2025-01/msg00000.html
* src/factor.c (mp_prime_p): Use mpz_probab_prime_p rather than doing
it by hand, as mpz_probab_prime_p uses Baillie-PSW which is a win.
This removes the need for the ret2 label and goto.
2025-07-09 17:12:39 -07:00
Paul Eggert 5d3fdfd04b factor: don’t give up before last prime in table
* src/factor.c (prime_p, prime2_p, mp_prime_p): Do not skip the
flag_prove_primality test for the last prime in the table, i.e.,
when r == PRIMES_PTAB_ENTRIES - 1.
(mp_prime_p): There is no longer a need for the ret1 label or goto.
2025-07-09 17:12:39 -07:00
Paul Eggert 38c63d58d0 factor: move prime_p local in
* src/factor.c (mp_prime_p): Refactor by moving local further in.
2025-07-09 17:12:39 -07:00
Paul Eggert bdd13ff8c0 factor: port back to mini-gmp
mini-gmp lacs mpn_tdiv_qr, so supply an emulation of it
when using mini-gmp.
* src/factor.c (copy_mpn_from_mpz, mpn_tdiv_qr) [!mpn_tdiv_qr]:
New functions.
2025-07-09 17:12:39 -07:00
Paul Eggert 442c164ae0 factor: speed up multiprecision Pollard’s rho
These changes are taken from a proposal by Torbjörn Granlund in:
https://lists.gnu.org/r/coreutils/2025-01/msg00000.html
On my x86-64 platform, they improve speed by more than 8× when
factoring 340282366920938463463374607431768211457.
* src/factor.c (mp_modadd, mp_modsub, mp_modadd_1, mp_mulredc):
New functions.
(MP_FACTOR_USING_POLLARD_RHO_N_MAX): New macro.
(mp_factor_using_pollard_rho): Act on mpn not mpz, and on
mp_limb_t not unsigned long int.  Reorder args.  All uses changed.
2025-07-09 17:12:39 -07:00
Paul Eggert 54a2d18b1f factor: use function for binv
* src/factor.c (binv_limb): New function, replacing the old
binv macro.  All uses changed.
2025-07-09 17:12:39 -07:00
Paul Eggert 20beab12df factor: use a more functional style
This is mostly to make the code a bit easier to read.
It shrinks the size of factor.o by 0.7% on my x86-64 platform
though it doesn’t affect CPU performance significantly.
* src/factor.c (mp_no_factors): Rename from mp_factor_init.
(mp_no_factors, mp_factor_using_division, mp_factor):
Return struct mp_factors rather than modifying one passed by reference.
All uses changed.
2025-07-09 17:12:39 -07:00
Paul Eggert d16059e375 factor: use single vector in struct mp_factors
* src/factor.c (struct mp_factor): New type.
(struct mp_factors): Use it to replace P and E with F.
All uses changed.
2025-07-09 17:12:39 -07:00
Paul Eggert 6e87c9b3a3 factor: omit unnecessary divisions by small primes
* src/factor.c (mp_factor_using_division):
When continuing in single precision, don’t divide by primes that
were already cast out in multiple precision.
On my platform this gave a 2.5% speedup when factoring
2**128 + 172261 = 4999 * 68070087401668026297934508388031283,
as W_TYPE_SIZE == 64 and 4999 is the last prime in the primes table.
2025-07-09 17:12:39 -07:00
Paul Eggert f7d295d941 factor: use primes_diff more consistently
* src/factor.c (mp_factor_using_division): Use index into
primes_diff consistently with other uses.  This is mostly just a
style change, and likely doesn’t change the generated machine code.
2025-07-09 17:12:39 -07:00
Paul Eggert 748d03ca12 factor: refactor to for later performance speedup
This does not affect performance much, but it should allow future
performance improvements.
* src/factor.c (factor_using_division): Two new args I and P,
which generalize this function.  All uses changed.
(mp_finish_up_in_single, factor_up): New functions, like the
non-*_up* versions but with two new args PRIME_IDX and PRIME.
They mostly just have the old body of the old non-*_up_ versions.
(mp_finish_in_single, factor): Rewrite in terms of the new functions.
2025-07-09 17:12:39 -07:00
Paul Eggert 831623ba66 factor: switch from mp to single when doable
This significantly improves performance when a number exceeds
2**(W_TYPE_SIZE - 1) and is the product of a prime less than
FIRST_OMITTED_PRIME and another prime less than 2**(W_TYPE_SIZE - 1).
On my platform, for example, it doubled the speed of factoring
4999 * (2**128 - 159).
* src/factor.c (mp_size, mp_finish_in_single): New functions.
(mp_factor_using_division, mp_factor_using_pollard_rho):
Finish using single precision when possible.
* tests/factor/factor.pl (lt-5000-times-128-bit): New test.
2025-07-09 17:12:39 -07:00
Paul Eggert 845f3a6158 factor: primes_diff idx type consistency
* src/factor.c (factor_insert_refind):
Use idx_t for indexes into primes_diff,
for consistency with other indexes into primes_diff.
This has no practical effect unless the primes_diff
table becomes unreasonably large.
2025-07-09 17:12:39 -07:00
Paul Eggert fd458798fe factor: omit q from mp_factor_using_division
* src/factor.c (mp_factor_using_division): Remove unused local.
2025-07-09 17:12:39 -07:00
Paul Eggert 64b860c2f7 factor: mp insert multiplicity too
Support a multiplicity argument in the mp case, too.
This helps keeps the two cases in sync, for maintenance.
* src/factor.c (mp_factor_insert, mp_factor_insert_ui):
New arg M, for multiplicity.  All callers changed.
2025-07-09 17:12:39 -07:00
Paul Eggert 258392808c factor: prefer non-macros
Use something other than a macro when that is easy and won’t hurt
performance.
* src/factor.c (__ll_B, __ll_lowpart, _ll_highpart) [!USE_LONGLONG_H]:
(MAX_NFACTS, highbit_to_mask, factor_insert, PRIMES_PTAB_ENTRIES):
Make these enums, or constants, or static functions instead of macros.
(highbit_to_mask): Rename from HIGHBIT_TO_MASK.  All uses changed.
2025-07-09 17:12:39 -07:00
Paul Eggert fe136a9800 factor: let builder specify MR_REPS
* src/factor.c (MR_REPS): Let the builder specify it
with -DMR_REPS=whatever.
2025-07-09 17:12:39 -07:00
Paul Eggert 3e438e369c factor: add comments 2025-07-09 17:12:39 -07:00
Paul Eggert 77717f597e factor: factor insertion simplifications
* src/factor.c (factor_insert_multiplicity):
Adjust to keep in sync with mp_factor_insert changes below,
by adding 1 to the index and using memmove to move.
(mp_factor_insert): Omit redundant call to mpz_cmp.
Prefer idx_t (always nonnegative) to ptrdiff_t,
by adding 1 to the indexes.
Prefer mpz_init_set to mpz_init+mpz_set.
Use memmove to move, rather than doing it by hand.
2025-07-09 17:12:39 -07:00
Paul Eggert 374f5fdcc1 factor: check unsigned char counts
* src/factor.c (MAX_NFACTS): Allow word size of 128 bits,
even if this is only theoretical now.
Check that struct factors’s unsigned char counts won’t overflow.
2025-07-09 17:12:39 -07:00
Paul Eggert 5ae141ae14 factor: fix comment 2025-07-09 17:12:39 -07:00
Paul Eggert a25d188098 factor: paren cleanup
* src/factor.c (umul_ppmm, udiv_qrnnd, addmod, addmod2, submod2)
(binv, divexact_21, redcify2, div_smallq): Omit unnecessary parens.
2025-07-09 17:12:39 -07:00
Paul Eggert f14ddcb1ef factor: simplify longlong.h setup
* src/factor.c (USE_LONGLONG_H):
Default to false on unusual (but standard-conforming)
platforms that lack int64_t etc.
(UWtype, UHWtype): Now typedefs, not macros.
(UQItype): Remove.
(SItype, USItype, DItype, UDItype): Use standard C types.
2025-07-09 17:12:39 -07:00
Paul Eggert 0a5d071967 factor: prefer uuint to two words in a couple of places
This simplifies things slightly by using uuint for
some two-word integers.
* src/factor.c (strtouuint): Accept uuint *, not two mp_limb_t *.
All callers changed.
(print_factors_single): Accept uuint, not two limbs.
All callers changed.
(print_factors): Use simpler test for high bit,
one that need not worry about promoting to int.
2025-07-09 17:12:39 -07:00
Paul Eggert 59421d373b factor: FALLTHROUGH instead of break
* src/factor.c (print_factors): Use FALLTHROUGH
to make it clear that the "Use GMP" comment also applies here.
2025-07-09 17:12:39 -07:00
Paul Eggert e7c1abfe79 factor: remove wide_uuint
Simplify by using GMP’s word type instead of pretending to roll our own.
* src/factor.c (wide_uuint): Remove.  All uses replaced by mp_limb_t.
(umul_ppmm) [!umul_ppmm]: Don’t assume unsigned long is at least half
as wide as mp_limb_t.  This simpler anyway.
(strtouuint): Rename from strto2wide_uint.  All uses changed.
2025-07-09 17:12:39 -07:00
Paul Eggert d6ee61e407 factor: use same word size as GMP
Remove experimental code for 128-bit words as it does not work and
we lack time to figure out why.  Instead, ensure that words are
the same size as with GMP.
* src/factor.c (USE_INT128): Remove.  All uses removed.
(wide_uint, W_TYPE_SIZE): Define to be the same as GMP.
(MP_LIMB_MAX): New macro.  Check that it matches W_TYPE_SIZE.
(USE_LONGLONG_H): Default to true.
(UHWtype) [USE_LONGLONG_H]: Define to unsigned int, same as GMP.
(prime_p): Go back to not worrying about 128-bit words,
since GMP doesn’t worry and doesn’t use them.
(lbuf_putbitcnt): New function, since we cannot assume
that bitcnt_t fits into mp_limb_t.
(print_factors): Use it.
* src/make-prime-list.c (output_primes):
Don’t assume that wide_uint’s maximum is UINTMAX_MAX.
2025-07-09 17:12:39 -07:00
Paul Eggert c8af57b5b8 factor: unsigned long → mp_bitcnt_t
* src/factor.c (struct mp_factors): e (multiplicity) member
is now of type mp_bitcnt_t, not unsigned long int, since
its value is at most a bit count.  All uses changed.
2025-07-09 17:12:39 -07:00
Paul Eggert badc24c30c factor: fix test case name
* tests/factor/factor.pl (bug-gmp-plus_2_sup_127_plus_1):
Rename from bug-gmp-plus_2_sup_128_plus_1, so that
it accurately reflects the test case.
2025-07-09 17:12:39 -07:00
Paul Eggert bdccb27ca6 factor: generalize BIG_POWER_OF_10
* src/factor.c (BIG_POWER_OF_10, LOG_BIG_POWER_OF_10):
Place fewer restrictions on BIG_POWER_OF_10.
This is only for currently-theoretical hosts;
it shouldn’t affect machine code on practical platforms.
2025-07-09 17:12:39 -07:00