Modern Computer Arithmetic Richard P. Brent and Paul Zimmermann Version 0.3 Copyright c 2003-2009 Richard P. Brent and Paul Zimmermann (cid:13) This electronic version is distributed under the terms and conditions of the Creative Commons license “Attribution-Noncommercial-No Derivative Works 3.0”. You are free to copy, distribute and transmit this book under the following conditions: Attribution. You must attribute the work in the manner specified • by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Noncommercial. Youmaynotusethisworkforcommercialpurposes. • No Derivative Works. You may not alter, transform, or build upon • this work. Forany reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to the web page below. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author’s moral rights. For more information about the license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ Preface This is a book about algorithms for performing arithmetic, and their imple- mentation on modern computers. We are concerned with software more than hardware —we do notcover computer architecture orthe design ofcomputer hardware since good books are already available on these topics. Instead we focus on algorithms for efficiently performing arithmetic operations such as addition, multiplication and division, and their connections to topics such as modular arithmetic, greatest common divisors, the Fast Fourier Transform (FFT), and the computation of special functions. Thealgorithmsthatwepresentaremainlyintendedforarbitrary-precision arithmetic. That is, they are not limited by the computer wordsize of 32 or 64 bits, only by the memory and time available for the computation. We consider both integer and real (floating-point) computations. The bookis divided into four mainchapters, plus anappendix. Chapter 1 covers integer arithmetic. This has, of course, been considered in many other books and papers. However, there has been much recent progress, inspired in part by the application to public key cryptography, so most of the published books are now partly out of date or incomplete. Our aim has been to present the latest developments in a concise manner. Chapter 2 is concerned with the FFT and modular arithmetic, and their applications to computer arithmetic. We consider different number represen- tations, fast algorithms for multiplication, division and exponentiation, and the use of the Chinese Remainder Theorem (CRT). Chapter 3 covers floating-point arithmetic. Our concern is with high- precision floating-point arithmetic, implemented in software if the precision provided by the hardware (typically IEEE standard 64-bit arithmetic) is in- adequate. Thealgorithmsdescribedinthischapterfocusoncorrect rounding, extending the IEEE standard to arbitrary precision. Chapter 4 deals with the computation, to arbitraryprecision, of functions 3 4 Modern Computer Arithmetic, version 0.3 of June 11, 2009 such as sqrt, exp, ln, sin, cos, and more generally functions defined by power series or continued fractions. We also consider the computation of certain constants, such as π and (Euler’s constant) γ. Of course, the computation of special functions is a huge topic so we have had to be selective. In particular, wehave concentrated onmethodsthat areefficient andsuitable forarbitrary- precision computations. For details that are omitted we give pointers in the Notes and References sections of each chapter, and in the bibliography. Finally, the Appendix contains pointers to implementations, useful web sites, mailing lists, and so on. The book is intended for anyone interested in the design and implemen- tation of efficient algorithms for computer arithmetic, and more generally efficient numerical algorithms. We did our best to present algorithms that are ready to implement in your favorite language, while keeping a high-level description. Although the book is not specifically intended as a textbook, it could be used in a graduate course in mathematics or computer science, and for this reason, as well as to cover topics that could not be discussed at length in the text, we have included exercises at the end of each chapter. For solutions to the exercises, please contact the authors. WethanktheFrench NationalInstituteforResearch inComputer Science and Control (INRIA), the Australian National University (ANU), and the Australian Research Council (ARC), for their support. The book could not have been written without the contributions of many friends and colleagues, toonumerous to mention here, but acknowledged inthe text andintheNotes and References sections at the end of each chapter. Finally, we acknowledge Erin Brent, who first suggested writing the book; and thank our wives, Judy-anne and Marie, for their patience and encour- agement. This is a preliminary version — there are still a few exercises to be added. We welcome comments and corrections. Please send them to either of the authors. Richard Brent and Paul Zimmermann [email protected] [email protected] Canberra and Nancy, June 2009 Contents Preface 3 Contents 5 Notation 11 1 Integer Arithmetic 15 1.1 Representation and Notations . . . . . . . . . . . . . . . . . . 15 1.2 Addition and Subtraction . . . . . . . . . . . . . . . . . . . . 16 1.3 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.3.1 Naive Multiplication . . . . . . . . . . . . . . . . . . . 18 1.3.2 Karatsuba’s Algorithm . . . . . . . . . . . . . . . . . . 19 1.3.3 Toom-Cook Multiplication . . . . . . . . . . . . . . . . 20 1.3.4 Fast Fourier Transform (FFT) . . . . . . . . . . . . . . 22 1.3.5 Unbalanced Multiplication . . . . . . . . . . . . . . . . 22 1.3.6 Squaring . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.3.7 Multiplication by a Constant . . . . . . . . . . . . . . 24 1.4 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.4.1 Naive Division . . . . . . . . . . . . . . . . . . . . . . . 26 1.4.2 Divisor Preconditioning . . . . . . . . . . . . . . . . . 28 1.4.3 Divide and Conquer Division . . . . . . . . . . . . . . 29 1.4.4 Newton’s Method . . . . . . . . . . . . . . . . . . . . . 32 1.4.5 Exact Division . . . . . . . . . . . . . . . . . . . . . . 32 1.4.6 Only Quotient or Remainder Wanted . . . . . . . . . . 33 1.4.7 Division by a Constant . . . . . . . . . . . . . . . . . . 34 1.4.8 Hensel’s Division . . . . . . . . . . . . . . . . . . . . . 35 1.5 Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.5.1 Square Root . . . . . . . . . . . . . . . . . . . . . . . . 36 5 6 Modern Computer Arithmetic, version 0.3 of June 11, 2009 1.5.2 k-th Root . . . . . . . . . . . . . . . . . . . . . . . . . 38 1.5.3 Exact Root . . . . . . . . . . . . . . . . . . . . . . . . 39 1.6 Greatest Common Divisor . . . . . . . . . . . . . . . . . . . . 40 1.6.1 Naive GCD . . . . . . . . . . . . . . . . . . . . . . . . 41 1.6.2 Extended GCD . . . . . . . . . . . . . . . . . . . . . . 43 1.6.3 Half GCD, Divide and Conquer GCD . . . . . . . . . . 44 1.7 Base Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . 47 1.7.1 Quadratic Algorithms . . . . . . . . . . . . . . . . . . 47 1.7.2 Subquadratic Algorithms . . . . . . . . . . . . . . . . . 47 1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 1.9 Notes and References . . . . . . . . . . . . . . . . . . . . . . . 53 2 The FFT and Modular Arithmetic 55 2.1 Representation . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2.1.1 Classical Representation . . . . . . . . . . . . . . . . . 55 2.1.2 Montgomery’s Form . . . . . . . . . . . . . . . . . . . 56 2.1.3 Residue Number Systems . . . . . . . . . . . . . . . . 56 2.1.4 MSB vs LSB Algorithms . . . . . . . . . . . . . . . . . 57 2.1.5 Link with Polynomials . . . . . . . . . . . . . . . . . . 57 2.2 Addition and Subtraction . . . . . . . . . . . . . . . . . . . . 58 2.3 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 2.3.1 Barrett’s Algorithm . . . . . . . . . . . . . . . . . . . . 59 2.3.2 Montgomery’s Multiplication . . . . . . . . . . . . . . 60 2.3.3 McLaughlin’s Algorithm . . . . . . . . . . . . . . . . . 64 2.3.4 Special Moduli . . . . . . . . . . . . . . . . . . . . . . 65 2.3.5 Fast Multiplication Over GF(2)[x] . . . . . . . . . . . . 66 2.4 Division and Inversion . . . . . . . . . . . . . . . . . . . . . . 72 2.4.1 Several Inversions at Once . . . . . . . . . . . . . . . . 74 2.5 Exponentiation . . . . . . . . . . . . . . . . . . . . . . . . . . 76 2.5.1 Binary Exponentiation . . . . . . . . . . . . . . . . . . 77 2.5.2 Base 2k Exponentiation . . . . . . . . . . . . . . . . . 78 2.5.3 Sliding Window and Redundant Representation . . . . 79 2.6 Chinese Remainder Theorem . . . . . . . . . . . . . . . . . . . 80 2.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 2.8 Notes and References . . . . . . . . . . . . . . . . . . . . . . . 82 Modern Computer Arithmetic, 0.0 7 § 3 Floating-Point Arithmetic 85 3.1 Representation . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3.1.1 Radix Choice . . . . . . . . . . . . . . . . . . . . . . . 86 3.1.2 Exponent Range . . . . . . . . . . . . . . . . . . . . . 87 3.1.3 Special Values . . . . . . . . . . . . . . . . . . . . . . . 88 3.1.4 Subnormal Numbers . . . . . . . . . . . . . . . . . . . 88 3.1.5 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . 89 3.1.6 Precision: Local, Global, Operation, Operand . . . . . 91 3.1.7 Link to Integers . . . . . . . . . . . . . . . . . . . . . . 92 3.1.8 Ziv’s Algorithm and Error Analysis . . . . . . . . . . . 92 3.1.9 Rounding . . . . . . . . . . . . . . . . . . . . . . . . . 94 3.1.10 Strategies . . . . . . . . . . . . . . . . . . . . . . . . . 97 3.2 Addition, Subtraction, Comparison . . . . . . . . . . . . . . . 98 3.2.1 Floating-Point Addition . . . . . . . . . . . . . . . . . 99 3.2.2 Floating-Point Subtraction . . . . . . . . . . . . . . . . 100 3.3 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 3.3.1 Integer Multiplication via Complex FFT . . . . . . . . 105 3.3.2 The Middle Product . . . . . . . . . . . . . . . . . . . 107 3.4 Reciprocal and Division . . . . . . . . . . . . . . . . . . . . . 109 3.4.1 Reciprocal . . . . . . . . . . . . . . . . . . . . . . . . . 109 3.4.2 Division . . . . . . . . . . . . . . . . . . . . . . . . . . 114 3.5 Square Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 3.5.1 Reciprocal Square Root . . . . . . . . . . . . . . . . . 119 3.6 Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 3.6.1 Floating-Point Output . . . . . . . . . . . . . . . . . . 123 3.6.2 Floating-Point Input . . . . . . . . . . . . . . . . . . . 125 3.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 3.8 Notes and References . . . . . . . . . . . . . . . . . . . . . . . 128 4 Newton’s Method and Function Evaluation 131 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 4.2 Newton’s Method . . . . . . . . . . . . . . . . . . . . . . . . . 132 4.2.1 Newton’s Method for Inverse Roots . . . . . . . . . . . 134 4.2.2 Newton’s Method for Reciprocals . . . . . . . . . . . . 134 4.2.3 Newton’s Method for (Reciprocal) Square Roots . . . . 135 4.2.4 Newton’s Method for Formal Power Series . . . . . . . 136 4.2.5 Newton’s Method for Functional Inverses . . . . . . . . 137 4.2.6 Higher Order Newton-like Methods . . . . . . . . . . . 138 8 Modern Computer Arithmetic, version 0.3 of June 11, 2009 4.3 Argument Reduction . . . . . . . . . . . . . . . . . . . . . . . 139 4.3.1 Repeated Use of a Doubling Formula . . . . . . . . . . 141 4.3.2 Loss of Precision . . . . . . . . . . . . . . . . . . . . . 141 4.3.3 Guard Digits . . . . . . . . . . . . . . . . . . . . . . . 142 4.3.4 Doubling versus Tripling . . . . . . . . . . . . . . . . . 143 4.4 Power Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 4.4.1 Direct Power Series Evaluation . . . . . . . . . . . . . 147 4.4.2 Power Series With Argument Reduction . . . . . . . . 148 4.4.3 Rectangular Series Splitting . . . . . . . . . . . . . . . 149 4.5 Asymptotic Expansions . . . . . . . . . . . . . . . . . . . . . . 152 4.6 Continued Fractions . . . . . . . . . . . . . . . . . . . . . . . 158 4.7 Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . 161 4.7.1 Evaluation of Bessel Functions . . . . . . . . . . . . . . 162 4.7.2 Evaluation of Bernoulli and Tangent numbers . . . . . 163 4.8 Arithmetic-Geometric Mean . . . . . . . . . . . . . . . . . . . 167 4.8.1 Elliptic Integrals . . . . . . . . . . . . . . . . . . . . . 167 4.8.2 First AGM Algorithm for the Logarithm . . . . . . . . 168 4.8.3 Theta Functions . . . . . . . . . . . . . . . . . . . . . . 169 4.8.4 Second AGM Algorithm for the Logarithm . . . . . . . 171 4.8.5 The Complex AGM . . . . . . . . . . . . . . . . . . . . 172 4.9 Binary Splitting . . . . . . . . . . . . . . . . . . . . . . . . . . 173 4.9.1 A Binary Splitting Algorithm for sin,cos . . . . . . . . 176 4.9.2 The Bit-Burst Algorithm . . . . . . . . . . . . . . . . . 176 4.10 Contour Integration . . . . . . . . . . . . . . . . . . . . . . . . 179 4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 4.12 Notes and References . . . . . . . . . . . . . . . . . . . . . . . 188 5 Appendix: Implementations and Pointers 193 5.1 Software Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 5.1.1 CLN . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 5.1.2 GNU MP (GMP) . . . . . . . . . . . . . . . . . . . . . 193 5.1.3 MPFQ . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 5.1.4 MPFR . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 5.2 Mailing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 5.2.1 The BNIS Mailing List . . . . . . . . . . . . . . . . . . 195 5.2.2 The GMP Lists . . . . . . . . . . . . . . . . . . . . . . 195 5.3 On-Line Documents . . . . . . . . . . . . . . . . . . . . . . . . 195 Modern Computer Arithmetic, 0.0 9 § Bibliography 197 Index 213 Summary of Complexities 221 10 Modern Computer Arithmetic, version 0.3 of June 11, 2009
Description: