GNU MP the GNU Multiple Precision Arithmetic Library Edition 5.0.1 6 February 2010
Total Page:16
File Type:pdf, Size:1020Kb
GNU MP The GNU Multiple Precision Arithmetic Library Edition 5.0.1 6 February 2010 by Torbj¨orn Granlund and the GMP development team This manual describes how to install and use the GNU multiple precision arithmetic library, version 5.0.1. Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 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 124. 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 .................................................. 16 3.1 Headers and Libraries ............................................................ 16 3.2 Nomenclature and Types ......................................................... 16 3.3 Function Classes ................................................................. 17 3.4 Variable Conventions ............................................................. 17 3.5 Parameter Conventions ........................................................... 18 3.6 Memory Management ............................................................ 19 3.7 Reentrancy....................................................................... 19 3.8 Useful Macros and Constants ..................................................... 20 3.9 Compatibility with older versions ................................................. 20 3.10 Demonstration programs ........................................................ 20 3.11 Efficiency ....................................................................... 21 3.12 Debugging ...................................................................... 23 3.13 Profiling ........................................................................ 25 3.14 Autoconf........................................................................ 26 3.15 Emacs .......................................................................... 27 4 Reporting Bugs............................................... 28 5 Integer Functions............................................. 29 5.1 Initialization Functions ........................................................... 29 5.2 Assignment Functions ............................................................ 30 5.3 Combined Initialization and Assignment Functions ................................ 30 5.4 Conversion Functions ............................................................. 31 5.5 Arithmetic Functions ............................................................. 32 5.6 Division Functions ............................................................... 32 5.7 Exponentiation Functions ........................................................ 34 5.8 Root Extraction Functions ....................................................... 35 5.9 Number Theoretic Functions ..................................................... 35 5.10 Comparison Functions........................................................... 37 5.11 Logical and Bit Manipulation Functions ......................................... 38 5.12 Input and Output Functions..................................................... 39 5.13 Random Number Functions ..................................................... 40 ii GNU MP 5.0.1 5.14 Integer Import and Export ...................................................... 40 5.15 Miscellaneous Functions ......................................................... 42 5.16 Special Functions ............................................................... 42 6 Rational Number Functions ................................. 44 6.1 Initialization and Assignment Functions .......................................... 44 6.2 Conversion Functions ............................................................. 45 6.3 Arithmetic Functions ............................................................. 45 6.4 Comparison Functions ............................................................ 46 6.5 Applying Integer Functions to Rationals .......................................... 46 6.6 Input and Output Functions ...................................................... 47 7 Floating-point Functions..................................... 48 7.1 Initialization Functions ........................................................... 48 7.2 Assignment Functions ............................................................ 50 7.3 Combined Initialization and Assignment Functions ................................ 51 7.4 Conversion Functions ............................................................. 51 7.5 Arithmetic Functions ............................................................. 52 7.6 Comparison Functions ............................................................ 53 7.7 Input and Output Functions ...................................................... 53 7.8 Miscellaneous Functions .......................................................... 54 8 Low-level Functions .......................................... 56 8.1 Nails ............................................................................. 63 9 Random Number Functions ................................. 65 9.1 Random State Initialization ...................................................... 65 9.2 Random State Seeding ........................................................... 66 9.3 Random State Miscellaneous ..................................................... 66 10 Formatted Output .......................................... 67 10.1 Format Strings .................................................................. 67 10.2 Functions ....................................................................... 69 10.3 C++ Formatted Output......................................................... 70 11 Formatted Input ............................................ 72 11.1 Formatted Input Strings......................................................... 72 11.2 Formatted Input Functions ...................................................... 74 11.3 C++ Formatted Input .......................................................... 74 12 C++ Class Interface ........................................ 76 12.1 C++ Interface General .......................................................... 76 12.2 C++ Interface Integers .......................................................... 77 12.3 C++ Interface Rationals ........................................................ 78 12.4 C++ Interface Floats ........................................................... 80 12.5 C++ Interface Random Numbers................................................ 81 12.6 C++ Interface Limitations ...................................................... 82 13 Berkeley MP Compatible Functions ....................... 84 iii 14 Custom Allocation .......................................... 86 15 Language Bindings .......................................... 88 16 Algorithms .................................................. 90 16.1 Multiplication ................................................................... 90 16.1.1 Basecase Multiplication..................................................... 90 16.1.2 Karatsuba Multiplication ................................................... 91 16.1.3 Toom 3-Way Multiplication................................................. 92 16.1.4 Toom 4-Way Multiplication................................................. 94 16.1.5 FFT Multiplication......................................................... 94 16.1.6 Other Multiplication ....................................................... 95 16.1.7 Unbalanced Multiplication.................................................. 96 16.2 Division Algorithms ............................................................. 96 16.2.1 Single Limb Division ....................................................... 96 16.2.2 Basecase Division .......................................................... 97 16.2.3 Divide and Conquer Division ............................................... 97 16.2.4 Block-Wise Barrett Division ................................................ 98 16.2.5 Exact Division ............................................................. 98 16.2.6 Exact Remainder ........................................................... 99 16.2.7 Small Quotient Division .................................................... 99 16.3 Greatest Common Divisor ...................................................... 100 16.3.1 Binary GCD