### GMP Build System Configuration Notes Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Comprehensive notes on configuring the GMP library across various hardware architectures and build tools. Covers issues and solutions related to CPU detection, PIC support, assembly optimizations, and Autoconf/Automake variable usage. ```APIDOC Alpha ev7, ev79 CPU Detection: - Issue: `config.guess` needs updates to detect these CPUs (e.g., "3-1307" for ev7). - Solution: Implement custom detection logic, potentially using `psrinfo` on OSF. Alpha OSF Libtool PIC Support: - Issue: Libtool 1.5 doesn't recognize "pic always" on Alpha OSF, leading to redundant `gcc` calls. - Solution: Await newer Libtool versions or tolerate harmless inefficiency. ARM `umul_ppmm` Optimization: - Issue: `umul_ppmm` in `longlong.h` always uses `umull`, which might be specific to certain ARM series chips. - Solution: Configure `umull` usage based on ARM CPU capabilities. HPPA CPU Detection: - Issue: `config.guess` should recognize 7000, 7100, 7200, and 8x00 series HPPA CPUs. - Solution: Update `config.guess`. HPPA GCC Scheduling Parameters: - Issue: GCC 3.2 introduces `-mschedule=7200` etc., which could be driven by exact HPPA CPU type. - Solution: Integrate CPU type detection with GCC scheduling options. Mips CPU Detection and Endianness: - Issue: `config.guess` should differentiate Mips CPUs (e.g., mipsr3000, mipsr4000, mipsr10000). - Solution: Use `hinv -c processor` on Irix for detailed info; use `AC_C_BIGENDIAN` for endianness instead of `el` suffix. PowerPC AIX Function Descriptors: - Issue: AIX function descriptor handling is driven by `*-*-aix*`, which might be unreliable. - Solution: Implement feature tests (e.g., examining compiler output) for more reliable detection; consider merging `aix.m4` into `powerpc-defs.m4`. Autoconf `config.m4` Generation: - Issue: `config.m4` is not regenerated by `config.status`, causing issues with `make` and `AC_SUBST`. - Solution: Use `AC_CONFIG_COMMANDS` instead of `AC_OUTPUT` for `config.m4` generation; careful m4 quoting might reduce `changequote` needs. Automake Variable Usage (`CCAS`, `CCASFLAGS`, `LDFLAGS`): - Issue: Automake's `CCAS`, `CCASFLAGS` scheme could be used for assembler support; `GMP_LDFLAGS` might be redundant with `LDFLAGS`. - Solution: Align GMP's build system with Automake's standard variables; anticipate `libtool` passing `CFLAGS` to linker to make `GMP_LDFLAGS` unnecessary. `mpn/Makeasm.am` Compilation Rules: - Issue: Using `-c` and `-o` together in `.S` and `.asm` rules is not fully portable (requires `AC_PROG_CC_C_O`). - Solution: Rewrite rules to use temporary `.s` files or `mv` operations; ensure `SUFFIXES` order prevents conflicts with primary sources. ``` -------------------------------- ### Optimizing mpz_add_ui by Sharing Copy Operations Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Suggests optimizing `mpz_add_ui` by opening up `mpn_add_1` and `mpn_sub_1` to share `__GMPN_COPY` operations, leveraging the absence of carry for add and high zero for non-empty sub copies. ```APIDOC mpz_add_ui: - Contains two `__GMPN_COPY` operations (one from `mpn_add_1`, one from `mpn_sub_1`). - Suggests opening up `mpn_add_1` and `mpn_sub_1` to share this code. - When a copy is needed, there's no carry to append for add, and if non-empty, no high zero for sub. ``` -------------------------------- ### Sparc32 udiv_nfp.asm Selection Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Notes that the integer-based `udiv_nfp.asm` for Sparc32, previously selected by `configure --nfp`, is no longer easily included due to the removal of that option with autoconf. Suggests placing it in the `mpn` search path if beneficial for specific chips. ```APIDOC Sparc32 udiv_nfp.asm: - Integer-based division assembly file. - Used to be selected by `configure --nfp`, but that option is gone with autoconf. - Could be placed in the `mpn` search path if beneficial for specific CPU types. ``` -------------------------------- ### GMP Library Prime Testing and Utility Functions Source: https://github.com/gbeauchesne/gmp/blob/master/doc/projects.html Documentation for various functions related to prime testing and number theory utilities within the GMP library, including probabilistic primality tests and potential future additions. ```APIDOC mpz_probab_prime_p (mpz_t n, int reps) - Purpose: Probabilistically tests if 'n' is prime. - Current Behavior: - Always initializes a new `gmp_randstate_t` for randomized tests, which means it's not truly random and runs the same tests for a given input. - The 'reps' argument is specific to Miller-Rabin tests (1/4^reps chance of error). - Suggested Improvements: - Accept an `rstate` parameter to allow successive tests to increase confidence. - Change 'reps' to a parameter asking for a maximum chance 1/2^x of a probable prime being composite, allowing for implementation changes or new proofs. mpn_mod_34lsub1 - Purpose: An obvious and easy improvement to the trial divisions. mpz_nthprime (mpz_t rop, unsigned long n) - Purpose: Computes the nth prime number. - Feasibility: Almost certainly impractical for anything but small 'n' within a bignum library. ``` -------------------------------- ### GMP I/O and String Conversion Enhancements Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Proposed improvements for input/output operations and string conversions in GMP, including support for binary literals and more flexible base handling in `mpf_t` and `mpz_t` functions, as well as `gmp_printf` extensions. ```APIDOC Binary Input Prefixes (0b/0B) - Purpose: Allow `0b` and `0B` prefixes to indicate binary input in string conversion functions (e.g., `mpz_set_str`). mpf_out_raw(stream, op) - Purpose: Output an `mpf_t` number to a stream in a raw, portable binary format. - Parameters: `stream` (output stream), `op` (mpf_t operand). mpf_inp_raw(rop, stream) - Purpose: Input an `mpf_t` number from a stream in a raw, portable binary format. - Parameters: `rop` (mpf_t result), `stream` (input stream). mpz_set_str(rop, str, base, len) - Purpose: Variants of `mpz_set_str` that accept an explicit string length `len` instead of relying on null-termination. - Parameters: `rop` (mpz_t result), `str` (input string), `base` (numeric base), `len` (length of string). mpf_set_str(rop, str, base) - Purpose: Enhance `mpf_set_str` to accept `0x` and `0b` prefixes when `base` is 0. The exponent part could default to decimal, with `0x` or `0b` also allowed for the exponent (e.g., `0xFFAA@0x5A`). - Parameters: `rop` (mpf_t result), `str` (input string), `base` (numeric base). mpf_inp_str(rop, stream, base) - Purpose: Similar enhancements for `mpf_inp_str` to handle `0x` and `0b` prefixes. - Parameters: `rop` (mpf_t result), `stream` (input stream), `base` (numeric base). gmp_printf(format, ...) - Purpose: Extend `gmp_printf` functionality. - Features: - Support `%b` format specifier for binary output (applicable to `mpz_t`, `int`, etc.). - Allow arbitrary base specification within the format string (e.g., `&13b`) or as a parameter using `*`. - Accept `mpq_t` arguments for float conversions (e.g., `"%.4Qf"`), with rounding similar to `mpf_t`. ``` -------------------------------- ### GMP Variable Argument List Handling Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Explains the correct way to pass GMP types like `mpz_t` to `va_arg` functions, requiring pointer types such as `MP_INT*` instead of direct types, and suggests documenting `mpz_ptr` and related types. ```APIDOC va_arg: - Does not work directly with `mpz_t`. - Requires pointer types like `MP_INT*`. - Consider documenting `mpz_ptr` and friends, or using `void*` fallback. ``` -------------------------------- ### Refining mpf_t Exponent Handling for Zero Size Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Suggests allowing the exponent of `mpf_t` to be undefined when `size==0` instead of requiring it to be 0, simplifying zero result handling and avoiding unnecessary exponent setting, though noting current dependencies in `mpz_set_f` and `mpf_cmp_ui`. ```APIDOC mpf_t: - Might allow the exponent to be undefined when size==0, instead of requiring it to be 0. - Simplifies handling of zero results by avoiding setting the exponent. - Note current dependencies in `mpz_set_f` and `mpf_cmp_ui` on `exp==0`. ``` -------------------------------- ### C Optimization for 64-bit Leading Zero Count Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html This C code snippet illustrates an optimization technique for counting leading zeros on 64-bit machines. It employs a binary search-like approach using conditional right and left shifts to efficiently determine the count, progressively narrowing down the search range. This pattern is typically integrated into a larger `count_leading_zeros` function. ```C if ((x >> 32) == 0) { x <<= 32; cnt += 32; } if ((x >> 48) == 0) { x <<= 16; cnt += 16; } ... ``` -------------------------------- ### GMP Application Development Macros Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Proposed macros to assist applications in correctly handling and printing GMP limb types, providing information about their underlying size and format specifiers. ```APIDOC GMP_LONG_LONG_LIMB - Purpose: A documented feature/macro indicating whether a GMP limb is represented by a `long long` type. - Usage: Applications can use this to determine whether to use `%lu` or `%Lu` for printing limbs with `printf`. GMP_PRIdMP_LIMB - Purpose: Defines following C99 `` for printing limbs. - Usage: Provides a portable format specifier for printing GMP limb types, similar to `PRId64`. ``` -------------------------------- ### Clarifying mpz_get_si Return Value Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Notes an undocumented behavior of `mpz_get_si` returning `0x80000000` for `-0x100000000` and suggests clarifying its intended return of the low 31 or 63 bits. ```APIDOC mpz_get_si: - Returns 0x80000000 for -0x100000000. - Expected to return the low 31 (or 63) bits. - Behavior is currently undocumented and may not be critical. ``` -------------------------------- ### Optimizing longlong.h for Inlining and Conditional Assembly Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Proposes reorganizing `longlong.h` to enable inlining of operations even with system compilers, using stub functions where compiler features are absent. Also suggests conditional inclusion of assembly files like `umul.asm`, `udiv.asm`, and `cntlz.asm` based on usage (e.g., `mpn_umul_ppmm`) for minor space savings, and building the header at configure time for CPU-specific macros. ```APIDOC longlong.h: - Reorganize to inline operations for system compilers. - Use stub functions when compiler features are unavailable. - Conditionally include assembly files (umul.asm, udiv.asm, cntlz.asm) based on usage (e.g., mpn_umul_ppmm). - Build at configure time by concatenating fragments for CPU-specific macros, flattening nested conditionals. ``` -------------------------------- ### GMP `mpz_strtoz` String Parsing Function Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Documentation for the proposed `mpz_strtoz` function, intended to parse strings into `mpz_t` integers with behavior similar to the standard C `strtol` function. ```APIDOC mpz_strtoz(const char *nptr, char **endptr, int base): Parses a string to an `mpz_t`. - Purpose: Convert a string representation of a number to an `mpz_t` integer. - Behavior: Mimics the parsing rules and error handling of `strtol`. ``` -------------------------------- ### Optimizing mpq Functions for Common Cases Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Proposes optimizing `mpq` functions by checking for numerator or denominator equal to 1, assuming common occurrence of integers or denominator-only values. ```APIDOC mpq functions: - Check for numerator or denominator equal to 1. - Optimizes for cases where integers or denominator-only values are expected to occur frequently. ``` -------------------------------- ### Direct Handling of VAX D and G Format Doubles Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Suggests directly handling VAX D and G format `double` floats within `__gmp_extract_double` and `mpn_get_d` instead of falling back to generic code, as these formats are straightforward and detected by `configure`. ```APIDOC VAX D and G format double floats: - Straightforward formats, detected by `configure`. - Could be handled directly in `__gmp_extract_double` and `mpn_get_d`. - Avoids falling back on generic code. ``` -------------------------------- ### GMP Input/Output String Conversion Functions Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Documents current ambiguities and desired clarifications for GMP functions related to string input and output, specifically concerning when `mpz_inp_str` stops reading digits and how many digits `mpn_get_str` and `mpz_get_str` produce, especially regarding leading zeros. ```APIDOC mpz_inp_str: - Documentation needed on when it stops reading digits. mpn_get_str: - Clarification needed on the number of digits produced. - Should specify behavior regarding at most one leading zero. mpz_get_str: - Similar to mpn_get_str, regarding leading zeros. ``` -------------------------------- ### Optimizing GMP Memory Allocation for Growth Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Suggests an optional strategy for `mpz_init_set*` and `mpz_realloc` to allocate extra limbs (e.g., 16) to reduce reallocations as `mpz_t` grows. This would need to be an option due to potential memory bloat for applications using many small values. ```APIDOC mpz_init_set*: mpz_realloc: - Consider allocating an extra 16 limbs over what's needed. - Reduces the chance of reallocations as `mpz_t` grows. - Should be an option to avoid memory bloat in applications with many small values. ``` -------------------------------- ### GMP `long long` Conversion Functions Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Documentation for proposed GMP functions (`mpz_get_ull`, `mpz_set_ull`, `mpz_get_sll`) for converting between `mpz_t` and `long long` types. Discusses interoperability, C99/GCC availability, and build considerations like conditional compilation and library content variations. ```APIDOC mpz_get_ull(mpz_t op): Converts an `mpz_t` to an `unsigned long long`. - Purpose: Retrieve the value of an `mpz_t` as an `unsigned long long`. - Considerations: Potential for large inline code, handling of negative values for signed conversions. mpz_set_ull(mpz_t rop, unsigned long long op): Sets an `mpz_t` from an `unsigned long long`. - Purpose: Initialize or set an `mpz_t` from an `unsigned long long` value. - Considerations: May require splitting `long long` into two halves if direct `long long` support is deferred. mpz_get_sll(mpz_t op): Converts an `mpz_t` to a `signed long long`. - Purpose: Retrieve the value of an `mpz_t` as a `signed long long`. - Considerations: Similar to `mpz_get_ull`, with additional complexity for handling negative values and specific edge cases like `-0x10...00` (as seen in `mpz_get_si` correctness). ``` -------------------------------- ### GMP Scanning Functions Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Proposed new functions for scanning bits in `mpz_t` integers, providing counterparts to existing `mpz_scan0` and `mpz_scan1` but searching towards the low end of the integer. ```APIDOC mpz_rscan0(op, starting_bit) or mpz_revscan0(op, starting_bit) - Purpose: Scan for the first zero bit in `op` starting from `starting_bit` and moving towards the least significant bit (low end). - Parameters: `op` (mpz_t operand), `starting_bit` (index of the bit to start scanning from). - Returns: The index of the first zero bit found, or a special value if no zero bit is found. mpz_rscan1(op, starting_bit) or mpz_revscan1(op, starting_bit) - Purpose: Scan for the first one bit in `op` starting from `starting_bit` and moving towards the least significant bit (low end). - Parameters: `op` (mpz_t operand), `starting_bit` (index of the bit to start scanning from). - Returns: The index of the first one bit found, or a special value if no one bit is found. ``` -------------------------------- ### Optimizing count_trailing_zeros for Specific CPUs Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Suggests special handling for the frequently occurring 0 to 2 trailing zeros cases in `count_trailing_zeros` for CPUs where the operation is slow, as it's used on more or less uniformly distributed numbers. ```APIDOC count_trailing_zeros: - Used on more or less uniformly distributed numbers. - Slow on some CPUs. - Consider handling the frequently occurring 0 to 2 trailing zeros cases specially for performance. ``` -------------------------------- ### New GMP Arithmetic and Bitwise Operations Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Proposed new functions for advanced arithmetic, bitwise logical operations, and utility checks within the GMP library, aiming to expand its computational capabilities. ```APIDOC mpz_crr(rop, op1, op2, ...) - Purpose: Chinese Remainder Reconstruction. - Parameters: `rop` (result), `op1`, `op2` (operands). mpz_andn(rop, op1, op2) - Purpose: Bitwise AND-NOT operation (op1 & ~op2). - Parameters: `rop` (result), `op1`, `op2` (operands). mpz_iorn(rop, op1, op2) - Purpose: Bitwise Inclusive OR-NOT operation (op1 | ~op2). - Parameters: `rop` (result), `op1`, `op2` (operands). mpz_nand(rop, op1, op2) - Purpose: Bitwise NAND operation (~(op1 & op2)). - Parameters: `rop` (result), `op1`, `op2` (operands). mpz_nior(rop, op1, op2) - Purpose: Bitwise NOR operation (~(op1 | op2)). - Parameters: `rop` (result), `op1`, `op2` (operands). mpz_xnor(rop, op1, op2) - Purpose: Bitwise XNOR operation (~(op1 ^ op2)). - Parameters: `rop` (result), `op1`, `op2` (operands). mpz_and_ui(rop, op1, op2_ui) - Purpose: Bitwise AND operation with an unsigned long integer. - Parameters: `rop` (result), `op1` (mpz_t operand), `op2_ui` (unsigned long operand). mpz_bit_subset(op1, op2) - Purpose: Test if `op1` is a bitwise subset of `op2`. - Returns: Non-zero if `op1` is a bitwise subset of `op2`, 0 otherwise. May indicate proper/non-proper subset. mpz_sqrt_if_perfect_square(root, op) - Purpose: Calculates the integer square root of `op` if `op` is a perfect square. - Parameters: `root` (mpz_t to store the square root), `op` (mpz_t operand). - Returns: Non-zero if `op` is a perfect square and `root` is set, 0 otherwise. mpn_and_n, mpn_ior_n, mpn_xor_n, mpn_nand_n, mpn_nior_n, mpn_xnor_n, mpn_copyd - Purpose: Proposed to be made publicly available in `gmp.h` as library functions or inlines. These are low-level limb operations. ``` -------------------------------- ### GMP Type Conversions Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Proposed functions for converting between GMP floating-point numbers (`mpf_t`) and the C `long double` type, enhancing interoperability with standard C floating-point arithmetic. ```APIDOC mpf_get_ld(op) - Purpose: Convert an `mpf_t` number to a `long double`. - Parameters: `op` (mpf_t operand). - Returns: The `long double` representation of `op`. mpf_set_ld(rop, val) - Purpose: Convert a `long double` value to an `mpf_t` number. - Parameters: `rop` (mpf_t result), `val` (long double value). ``` -------------------------------- ### Applying GCC malloc Attribute to __gmp_allocate_func Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html Considers applying GCC's `__attribute__ ((malloc))` to `__gmp_allocate_func` for potential compiler optimizations, noting that it currently doesn't support function pointers and would require a new autoconf test and a supporting GCC version. ```APIDOC __gmp_allocate_func: - Could use GCC `__attribute__ ((malloc))`. - GCC 3.0 allows attribute on functions, but not function pointers. - Requires a new autoconf test and a GCC version that supports it. ``` -------------------------------- ### C Macro for Efficient Divisibility Check by 7 Source: https://github.com/gbeauchesne/gmp/blob/master/doc/projects.html This macro provides an optimized way to check if a number `n` is divisible by 7 using multiplication and comparison, leveraging the 'Division by Invariant Integers' technique. It's an improvement over standard division operations for compilers that don't optimize constant divisions. ```C #define MP_LIMB_DIVISIBLE_7_P(n) \ ((n) * MODLIMB_INVERSE_7 <= MP_LIMB_T_MAX/7) ``` -------------------------------- ### Define TMP_ALLOC Macro for Temporary Memory Allocation Source: https://github.com/gbeauchesne/gmp/blob/master/doc/tasks.html This C macro defines a mechanism for temporary memory allocation within GMP. It checks if the current memory chunk has enough space; if not, it calls `__gmp_tmp_increase` to allocate more. It then updates the current pointer and returns the allocated address. This aims to partially inline memory allocation for performance by avoiding full function calls when sufficient space is available. ```C #define TMP_ALLOC(n) ((ROUND_UP(n) > current->end - current->point ? __gmp_tmp_increase (ROUND_UP (n)) : 0), current->point += ROUND_UP (n), current->point - ROUND_UP (n)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.