GNU MP the GNU Multiple Precision Arithmetic Library Edition 6.2.0 17 January 2020
Total Page:16
File Type:pdf, Size:1020Kb
GNU MP The GNU Multiple Precision Arithmetic Library Edition 6.2.0 17 January 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.0. Copyright 1991, 1993-2016, 2018 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 130. 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 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 33 5.7 Exponentiation Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 35 5.8 Root Extraction Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 36 5.9 Number Theoretic Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::: 37 5.10 Comparison Functions::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 39 5.11 Logical and Bit Manipulation Functions ::::::::::::::::::::::::::::::::::::::::: 39 5.12 Input and Output Functions::::::::::::::::::::::::::::::::::::::::::::::::::::: 40 5.13 Random Number Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::: 41 ii GNU MP 6.2.0 5.14 Integer Import and Export :::::::::::::::::::::::::::::::::::::::::::::::::::::: 42 5.15 Miscellaneous Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 43 5.16 Special Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 44 6 Rational Number Functions ::::::::::::::::::::::::::::::::: 46 6.1 Initialization and Assignment Functions :::::::::::::::::::::::::::::::::::::::::: 46 6.2 Conversion Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 47 6.3 Arithmetic Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 47 6.4 Comparison Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 48 6.5 Applying Integer Functions to Rationals :::::::::::::::::::::::::::::::::::::::::: 48 6.6 Input and Output Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::: 49 7 Floating-point Functions::::::::::::::::::::::::::::::::::::: 50 7.1 Initialization Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 50 7.2 Assignment Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 52 7.3 Combined Initialization and Assignment Functions :::::::::::::::::::::::::::::::: 53 7.4 Conversion Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 53 7.5 Arithmetic Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 54 7.6 Comparison Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 55 7.7 Input and Output Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::: 55 7.8 Miscellaneous Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 56 8 Low-level Functions :::::::::::::::::::::::::::::::::::::::::: 58 8.1 Low-level functions for cryptography:::::::::::::::::::::::::::::::::::::::::::::: 65 8.2 Nails ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 68 9 Random Number Functions ::::::::::::::::::::::::::::::::: 70 9.1 Random State Initialization :::::::::::::::::::::::::::::::::::::::::::::::::::::: 70 9.2 Random State Seeding ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 71 9.3 Random State Miscellaneous ::::::::::::::::::::::::::::::::::::::::::::::::::::: 71 10 Formatted Output :::::::::::::::::::::::::::::::::::::::::: 72 10.1 Format Strings :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 72 10.2 Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 74 10.3 C++ Formatted Output::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 75 11 Formatted Input :::::::::::::::::::::::::::::::::::::::::::: 77 11.1 Formatted Input Strings::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 77 11.2 Formatted Input Functions :::::::::::::::::::::::::::::::::::::::::::::::::::::: 79 11.3 C++ Formatted Input :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 79 12 C++ Class Interface :::::::::::::::::::::::::::::::::::::::: 81 12.1 C++ Interface General :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 81 12.2 C++ Interface Integers :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 82 12.3 C++ Interface Rationals :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 84 12.4 C++ Interface Floats ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 85 12.5 C++ Interface Random Numbers:::::::::::::::::::::::::::::::::::::::::::::::: 87 12.6 C++ Interface Limitations :::::::::::::::::::::::::::::::::::::::::::::::::::::: 88 13 Custom Allocation :::::::::::::::::::::::::::::::::::::::::: 90 iii 14 Language Bindings :::::::::::::::::::::::::::::::::::::::::: 92 15 Algorithms :::::::::::::::::::::::::::::::::::::::::::::::::: 94 15.1 Multiplication ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 94 15.1.1 Basecase Multiplication::::::::::::::::::::::::::::::::::::::::::::::::::::: 94 15.1.2 Karatsuba Multiplication ::::::::::::::::::::::::::::::::::::::::::::::::::: 95 15.1.3 Toom 3-Way Multiplication::::::::::::::::::::::::::::::::::::::::::::::::: 96 15.1.4 Toom 4-Way Multiplication::::::::::::::::::::::::::::::::::::::::::::::::: 98 15.1.5 Higher degree Toom'n'half :::::::::::::::::::::::::::::::::::::::::::::::::: 98 15.1.6 FFT Multiplication::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 98 15.1.7 Other Multiplication :::::::::::::::::::::::::::::::::::::::::::::::::::::: 100 15.1.8 Unbalanced Multiplication::::::::::::::::::::::::::::::::::::::::::::::::: 100 15.2 Division Algorithms :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 101 15.2.1 Single Limb Division :::::::::::::::::::::::::::::::::::::::::::::::::::::: 101 15.2.2 Basecase Division ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 101 15.2.3 Divide and Conquer Division :::::::::::::::::::::::::::::::::::::::::::::: 102 15.2.4 Block-Wise Barrett Division ::::::::::::::::::::::::::::::::::::::::::::::: 102 15.2.5 Exact Division :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 102 15.2.6 Exact Remainder:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 103 15.2.7 Small Quotient Division ::::::::::::::::::::::::::::::::::::::::::::::::::: 104 15.3 Greatest Common Divisor ::::::::::::::::::::::::::::::::::::::::::::::::::::::