Commit Graph
9055 Commits
Author SHA1 Message Date
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 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
Paul Eggert 88cca5d5fc factor: remove wide_int
* src/factor.c (wide_int): Remove, since it gets in the
way of using mp_limb_t for words.  All uses removed.
(submod2, HIGHBIT_TO_MASK, divexact_21):
Rewrite without using wide_int.
This shouldn't change the machine code these days,
as compilers are pretty smart about isolating the
top bit of an unsigned int.
2025-07-09 17:12:39 -07:00
Paul Eggert 397d9b02a3 factor: add platform sanity check
* src/factor.c: Check against theoretical platform.
2025-07-09 17:12:39 -07:00
Paul Eggert 8ff11d626b factor: squfof cleanup
* src/factor.c (USE_SQUFOF, STAT_SQUFOF):
Assume these are always false, and simplify the code accordingly.
We can bring it back later if needed be.
2025-07-09 17:12:39 -07:00
Paul Eggert d7fd90d22d factor: don’t used uninitialized uu[0]
In practice there’s no bug but we might as well avoid the
undefined behavior.
* src/factor.c (hi_is_set): New static function.
(factor_insert_large, prime2_p, print_factors_single): Use it.
2025-07-09 17:12:39 -07:00
Pádraig Brady fd59e4a955 build: avoid make distcheck error
* src/local.mk: Similarly to commit v8.22-156-g09937e9d0
track speedlist.h with nodist_src_stty_SOURCES and DISTCLEANFILES
to ensure the make distcheck manifest comparison passes.
Addresses https://bug.gnu.org/78960
2025-07-05 22:14:08 +01:00
Pádraig Brady 1485eabfdd build: fix non parallel VPATH builds
* src/local.mk: Use the coarser BUILT_SOURCES mechanism
to generate speedlist.h, rather than a specific dependency
(which did seem to work for parallel builds).
Fixes https://bugs.gnu.org/78960
2025-07-05 12:49:02 +01:00
Collin Funk e34a79a034 maint: prefer endian.h macros to WORDS_BIGENDIAN, part 2
* src/blake2/blake2-impl.h: Include endian.h. Use BYTE_ORDER and
LITTLE_ENDIAN instead of WORDS_BIGENDIAN.
2025-07-04 20:52:37 -07:00
Collin Funk 991934d9c9 maint: prefer endian.h macros to WORDS_BIGENDIAN
* src/od.c: Include endian.h.
(WORDS_BIGENDIAN): Remove definition.
(main): Use BYTE_ORDER, BIG_ENDIAN, LITTLE_ENDIAN instead of
WORDS_BIGENDIAN. Fix formatting.
2025-07-04 10:59:39 -07:00
Paul Eggert 48281e56a3 od: port to Apple clang 14
* src/od.c (print_function_type): New type.  Use it for convenience.
(width_bytes): Omit duplicate entries, such as ‘double’ vs ‘long
double’ on macOS.  Problem reported by Bruno Haible
<https://bugs.gnu.org/78933>.
(decode_one_format): Cast null pointer to print_function_type
to pacify Apple clang-1400.0.29.202.
2025-06-30 15:41:47 -07:00
Pádraig Brady 017fc50bb6 build: fix VPATH builds with --enable-single-binary
* src/local.mk: Adjust the dependency so that speedlist.h
is built irrespective of the object file name.
Note we could use BUILT_SOURCES for this,
but it's better to have this more accurate dependency.
2025-06-30 16:03:44 +01:00
Pádraig Brady 87dcd447b1 od: reinstate half float validation check
Reinstate check removed in commit 56aa549a0 so that we
disallow -f2 when configured with utils_cv_ieee_16_bit_supported=no.
Otherwise the output routines will consume floats,
i.e. 4 bytes at a time.  Without this extra check
the tests/od/od-endian.sh will fail with this configuration.

* src/od.c (decode_one_format): Reinstate the explicit check
for this configuration edge case.
2025-06-30 14:34:55 +01:00
Pádraig Brady 9075b2b8d2 maint: avoid sc_prohibit-quotearg failure
* src/od.c (parse_old_offset): s/quotearg_colon/quotef/.
2025-06-30 13:10:59 +01:00
Paul Eggert 9002c04ccc od: more minor fixes for offsets
* src/od.c (parse_old_offset): Reject invalid offsets like "++0".
Treat overflowing offsets as errors, not as file names.
2025-06-29 22:26:14 -07:00
Paul Eggert 617220e970 od: refactor parse_old_offset
* src/od.c (parse_old_offset): Refactor for brevity and clarity.
2025-06-29 22:26:14 -07:00
Paul Eggert dcdb2550c4 od: pacify gcc -Wduplicated-cond
Problem reported by Pádraig Brady <https://bugs.gnu.org/78880#43>.
This patch doesn’t fix any bugs; it merely pacifies GCC.
* src/od.c (ispec_to_format): New function, replacing
the old ISPEC_TO_FORMAT macro.  All uses changed.
This part of the change is just refactoring.
(decode_one_format): Pacify à la ispec_to_format.
2025-06-29 17:14:48 -07:00
Paul Eggert 25454fa34d od: be more consistent re sizeof
* src/od.c (width_bytes, decode_one_format): Don’t assume a signed
type has the same size as the corresponding unsigned type.
This has no effect on practical platforms; it’s just for
consistency there.
2025-06-28 21:00:41 -07:00
Paul Eggert d5ea5e8aed od: fix integer overflow with large pseudos
* src/od.c (format_address_label): Diagnose overflow.
2025-06-28 21:00:41 -07:00
Paul Eggert 667be269ed od: speed up -S
* src/od.c (read_char): Use getc, not fgetc.
2025-06-28 21:00:41 -07:00
Paul Eggert 984dcc37f2 od: check sign bit more often
* src/od.c (read_char, dump_strings, main):
Instead of testing for an exact negative number,
just look at the sign bit.  This is a very minor tweak.
2025-06-28 21:00:41 -07:00
Paul Eggert 9aab4fb287 od: simplify away one loop copy
* src/od.c (dump): Coalesce two loops into one.
2025-06-28 21:00:41 -07:00
Paul Eggert 34b5043c6e od: simpler static initialization
* src/od.c (address_base, address_pad_len, format_address):
Initialize statically rather than dynamically.
(limit_bytes_to_format): Remove.  All uses replaced by
checking sign of end_offset.
(max_bytes_to_format): Remove static var.  Now local to ‘main’.
(end_offset): -1 now means no limit.  All uses changed.
2025-06-28 21:00:41 -07:00
Paul Eggert 0e104647a7 od: minor lcm tuning
* src/od.c (dump, main): Redo lcm calcuations to avoid a multiply.
2025-06-28 21:00:41 -07:00
Paul Eggert 56aa549a06 od: omit some duplicate code
On x86-64 (for example) print_long, print_long_long, and
print_intmax all behave identically, so give GCC enough info so
that it generates code for just one of these functions.
* src/od.c (enum size_spec): Arrange for enum values to
be the same if they represent types that behave the same.
(width_bytes, ISPEC_TO_FORMAT, decode_one_format):
Match the enum size_spec changes.
2025-06-28 21:00:41 -07:00
Paul Eggert 3ad59e19bc od: replace lookup tables with simple arithmetic
* src/od.c (FMT_BYTES_ALLOCATED): Use a simpler formula.
Although slightly too generous, the storage wasted is very small
and it pacifies gcc -Wformat-overflow=2.
(bytes_to_oct_digits, bytes_to_signed_dec_digits)
(bytes_to_unsigned_dec_digits, bytes_to_hex_digoits): Remove.
All uses replaced by algorithmic calculations, which are good
enough: they are valid for integers up to 2620 bits (!) and might
be slightly conservative for wider integers.  Remove related
static_asserts, which are no longer needed.
2025-06-28 21:00:41 -07:00
Paul Eggert 274226dbff od: support uintmax_t too
This has practical effect only on hypothetical platforms where
uintmax_t is wider than unsigned long long int.
* src/od.c (enum size_spec): New constant INTMAX.
(MAX_INTEGRAL_TYPE_WIDTH): Now equals UINTMAX_WIDTH.
(FMT_BYTES_ALLOCATED): Allow for the extra "l" in "%lld".
Also, fix off-by-two error in size calculation.
(width_bytes, integral_type_size): Add entries for uintmax_t.
(print_intmax): New function.
(decode_one_function): Use it.
(ISPEC_TO_FORMAT): New arg Max_fmt. All uses changed.
2025-06-28 21:00:41 -07:00