GNU MP the GNU Multiple Precision Arithmetic Library Edition 6.2.1 14 November 2020
Total Page:16
File Type:pdf, Size:1020Kb
GNU MP The GNU Multiple Precision Arithmetic Library Edition 6.2.1 14 November 2020 by Torbj¨ornGranlund and the GMP development team This manual describes how to install and use the GNU multiple precision arithmetic library, version 6.2.1. Copyright 1991, 1993-2016, 2018-2020 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover Texts being \A GNU Manual", and with the Back-Cover Texts being \You have freedom to copy and modify this GNU Manual, like GNU software". A copy of the license is included in Appendix C [GNU Free Documentation License], page 132. i Table of Contents GNU MP Copying Conditions :::::::::::::::::::::::::::::::::::: 1 1 Introduction to GNU MP ::::::::::::::::::::::::::::::::::::: 2 1.1 How to use this Manual :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 2 2 Installing GMP ::::::::::::::::::::::::::::::::::::::::::::::::: 3 2.1 Build Options:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 3 2.2 ABI and ISA :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 8 2.3 Notes for Package Builds:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 2.4 Notes for Particular Systems :::::::::::::::::::::::::::::::::::::::::::::::::::::: 12 2.5 Known Build Problems ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 14 2.6 Performance optimization ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 15 3 GMP Basics ::::::::::::::::::::::::::::::::::::::::::::::::::: 17 3.1 Headers and Libraries::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 17 3.2 Nomenclature and Types ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 17 3.3 Function Classes :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 18 3.4 Variable Conventions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 18 3.5 Parameter Conventions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 19 3.6 Memory Management ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 20 3.7 Reentrancy ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 20 3.8 Useful Macros and Constants ::::::::::::::::::::::::::::::::::::::::::::::::::::: 21 3.9 Compatibility with older versions ::::::::::::::::::::::::::::::::::::::::::::::::: 21 3.10 Demonstration programs::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 21 3.11 Efficiency:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 22 3.12 Debugging::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 24 3.13 Profiling ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 26 3.14 Autoconf :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 27 3.15 Emacs::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 28 4 Reporting Bugs ::::::::::::::::::::::::::::::::::::::::::::::: 29 5 Integer Functions ::::::::::::::::::::::::::::::::::::::::::::: 30 5.1 Initialization Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 30 5.2 Assignment Functions::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 31 5.3 Combined Initialization and Assignment Functions :::::::::::::::::::::::::::::::: 31 5.4 Conversion Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 32 5.5 Arithmetic Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 33 5.6 Division Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 34 5.7 Exponentiation Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 36 5.8 Root Extraction Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 36 5.9 Number Theoretic Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::: 37 5.10 Comparison Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 39 5.11 Logical and Bit Manipulation Functions:::::::::::::::::::::::::::::::::::::::::: 40 5.12 Input and Output Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::: 41 5.13 Random Number Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::: 42 ii GNU MP 6.2.1 5.14 Integer Import and Export::::::::::::::::::::::::::::::::::::::::::::::::::::::: 42 5.15 Miscellaneous Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 44 5.16 Special Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 44 6 Rational Number Functions :::::::::::::::::::::::::::::::::: 47 6.1 Initialization and Assignment Functions ::::::::::::::::::::::::::::::::::::::::::: 47 6.2 Conversion Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 48 6.3 Arithmetic Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 48 6.4 Comparison Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 49 6.5 Applying Integer Functions to Rationals::::::::::::::::::::::::::::::::::::::::::: 50 6.6 Input and Output Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::: 50 7 Floating-point Functions ::::::::::::::::::::::::::::::::::::: 52 7.1 Initialization Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 52 7.2 Assignment Functions::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 54 7.3 Combined Initialization and Assignment Functions :::::::::::::::::::::::::::::::: 55 7.4 Conversion Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 55 7.5 Arithmetic Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 56 7.6 Comparison Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 57 7.7 Input and Output Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::: 57 7.8 Miscellaneous Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 58 8 Low-level Functions ::::::::::::::::::::::::::::::::::::::::::: 60 8.1 Low-level functions for cryptography :::::::::::::::::::::::::::::::::::::::::::::: 67 8.2 Nails ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 70 9 Random Number Functions :::::::::::::::::::::::::::::::::: 72 9.1 Random State Initialization ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 72 9.2 Random State Seeding :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 73 9.3 Random State Miscellaneous :::::::::::::::::::::::::::::::::::::::::::::::::::::: 73 10 Formatted Output ::::::::::::::::::::::::::::::::::::::::::: 74 10.1 Format Strings :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 74 10.2 Functions:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 76 10.3 C++ Formatted Output ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 77 11 Formatted Input ::::::::::::::::::::::::::::::::::::::::::::: 79 11.1 Formatted Input Strings ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 79 11.2 Formatted Input Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::: 81 11.3 C++ Formatted Input ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 81 12 C++ Class Interface :::::::::::::::::::::::::::::::::::::::: 83 12.1 C++ Interface General :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 83 12.2 C++ Interface Integers :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 84 12.3 C++ Interface Rationals::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 86 12.4 C++ Interface Floats :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 87 12.5 C++ Interface Random Numbers :::::::::::::::::::::::::::::::::::::::::::::::: 89 12.6 C++ Interface Limitations::::::::::::::::::::::::::::::::::::::::::::::::::::::: 90 13 Custom Allocation::::::::::::::::::::::::::::::::::::::::::: 92 iii 14 Language Bindings :::::::::::::::::::::::::::::::::::::::::: 94 15 Algorithms ::::::::::::::::::::::::::::::::::::::::::::::::::: 96 15.1 Multiplication ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 96 15.1.1 Basecase Multiplication ::::::::::::::::::::::::::::::::::::::::::::::::::::: 96 15.1.2 Karatsuba Multiplication ::::::::::::::::::::::::::::::::::::::::::::::::::: 97 15.1.3 Toom 3-Way Multiplication ::::::::::::::::::::::::::::::::::::::::::::::::: 98 15.1.4 Toom 4-Way Multiplication :::::::::::::::::::::::::::::::::::::::::::::::: 100 15.1.5 Higher degree Toom'n'half ::::::::::::::::::::::::::::::::::::::::::::::::: 100 15.1.6 FFT Multiplication :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 100 15.1.7 Other Multiplication ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 102 15.1.8 Unbalanced Multiplication ::::::::::::::::::::::::::::::::::::::::::::::::: 102 15.2 Division Algorithms :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 103 15.2.1 Single Limb Division::::::::::::::::::::::::::::::::::::::::::::::::::::::: 103 15.2.2 Basecase Division :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 103 15.2.3 Divide and Conquer Division::::::::::::::::::::::::::::::::::::::::::::::: 104 15.2.4 Block-Wise Barrett Division ::::::::::::::::::::::::::::::::::::::::::::::: 104 15.2.5 Exact Division::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 104 15.2.6 Exact Remainder :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 105 15.2.7 Small Quotient Division ::::::::::::::::::::::::::::::::::::::::::::::::::: 106 15.3 Greatest Common Divisor ::::::::::::::::::::::::::::::::::::::::::::::::::::::