GNU MP the GNU Multiple Precision Arithmetic Library Edition 6.0.0 24 March 2014
Total Page:16
File Type:pdf, Size:1020Kb
GNU MP The GNU Multiple Precision Arithmetic Library Edition 6.0.0 24 March 2014 by Torbj¨orn Granlund and the GMP development team This manual describes how to install and use the GNU multiple precision arithmetic library, version 6.0.0. Copyright 1991, 1993-2014 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 127. 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.0.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 ................................. 69 9.1 Random State Initialization ...................................................... 69 9.2 Random State Seeding ........................................................... 70 9.3 Random State Miscellaneous ..................................................... 70 10 Formatted Output .......................................... 71 10.1 Format Strings .................................................................. 71 10.2 Functions ....................................................................... 73 10.3 C++ Formatted Output......................................................... 74 11 Formatted Input ............................................ 76 11.1 Formatted Input Strings......................................................... 76 11.2 Formatted Input Functions ...................................................... 78 11.3 C++ Formatted Input .......................................................... 78 12 C++ Class Interface ........................................ 80 12.1 C++ Interface General .......................................................... 80 12.2 C++ Interface Integers .......................................................... 81 12.3 C++ Interface Rationals ........................................................ 82 12.4 C++ Interface Floats ........................................................... 84 12.5 C++ Interface Random Numbers................................................ 86 12.6 C++ Interface Limitations ...................................................... 87 13 Custom Allocation .......................................... 89 iii 14 Language Bindings .......................................... 91 15 Algorithms .................................................. 93 15.1 Multiplication ................................................................... 93 15.1.1 Basecase Multiplication..................................................... 93 15.1.2 Karatsuba Multiplication ................................................... 94 15.1.3 Toom 3-Way Multiplication................................................. 95 15.1.4 Toom 4-Way Multiplication................................................. 97 15.1.5 Higher degree Toom’n’half .................................................. 97 15.1.6 FFT Multiplication......................................................... 97 15.1.7 Other Multiplication ....................................................... 99 15.1.8 Unbalanced Multiplication.................................................. 99 15.2 Division Algorithms ............................................................ 100 15.2.1 Single Limb Division ...................................................... 100 15.2.2 Basecase Division ......................................................... 100 15.2.3 Divide and Conquer Division .............................................. 101 15.2.4 Block-Wise Barrett Division ............................................... 101 15.2.5 Exact Division ............................................................ 101 15.2.6 Exact Remainder.......................................................... 102 15.2.7 Small Quotient Division ................................................... 103 15.3 Greatest Common Divisor ...................................................... 103 15.3.1