Version 0.2.1 Copyright C 2003-2009 Richard P
Total Page:16
File Type:pdf, Size:1020Kb
Modern Computer Arithmetic Richard P. Brent and Paul Zimmermann Version 0.2.1 Copyright c 2003-2009 Richard P. Brent and Paul Zimmermann 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. You may not use this work for commercial purposes. • No Derivative Works. You may not alter, transform, or build upon • this work. For any 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 not cover computer architecture or the design of computer 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. The algorithms that we present are mainly intended for arbitrary-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 book is divided into four main chapters, plus an appendix. 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. The algorithms described in this chapter focus on correct rounding, extending the IEEE standard to arbitrary precision. Chapter 4 deals with the computation, to arbitrary precision, of functions 3 4 Modern Computer Arithmetic, version 0.2.1 of March 23, 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, we have concentrated on methods that are efficient and suitable for arbitrary- 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. We thank the French National Institute for Research in Computer 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, too numerous to mention here, but acknowledged in the text and in the Notes and References sections at the end of each chapter. Finally, we thank Erin Brent, who first suggested writing the book, and our wives Judy-anne and Marie, for their patience and encouragement. This is a preliminary version — there are still a few sections to be com- pleted. 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 2008 Contents Preface 3 Contents 5 Notation 11 1 Integer Arithmetic 15 1.1 RepresentationandNotations . 15 1.2 AdditionandSubtraction . 16 1.3 Multiplication........................... 17 1.3.1 NaiveMultiplication . 18 1.3.2 Karatsuba’sAlgorithm . 19 1.3.3 Toom-CookMultiplication . 20 1.3.4 FastFourierTransform(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 NaiveDivision.. .. 26 1.4.2 Divisor Preconditioning . 28 1.4.3 Divide and Conquer Division . 29 1.4.4 Newton’sMethod. 32 1.4.5 ExactDivision ...................... 32 1.4.6 Only Quotient or Remainder Wanted . 33 1.4.7 DivisionbyaConstant . 34 1.4.8 Hensel’sDivision . 35 1.5 Roots ............................... 36 1.5.1 SquareRoot........................ 36 5 6 Modern Computer Arithmetic, version 0.2.1 of March 23, 2009 1.5.2 k-thRoot ......................... 38 1.5.3 ExactRoot ........................ 39 1.6 GreatestCommonDivisor . 40 1.6.1 NaiveGCD ........................ 41 1.6.2 ExtendedGCD ...................... 43 1.6.3 Half GCD, Divide and Conquer GCD . 44 1.7 BaseConversion.......................... 47 1.7.1 QuadraticAlgorithms . 47 1.7.2 SubquadraticAlgorithms. 47 1.8 Exercises.............................. 50 1.9 NotesandReferences . 53 2 The FFT and Modular Arithmetic 55 2.1 Representation .......................... 55 2.1.1 ClassicalRepresentation . 55 2.1.2 Montgomery’sForm . 56 2.1.3 Residue Number Systems . 56 2.1.4 MSBvsLSBAlgorithms . 57 2.1.5 LinkwithPolynomials . 57 2.2 AdditionandSubtraction . 58 2.3 Multiplication........................... 58 2.3.1 Barrett’sAlgorithm. 59 2.3.2 Montgomery’s Multiplication . 60 2.3.3 McLaughlin’sAlgorithm . 64 2.3.4 SpecialModuli ...................... 65 2.3.5 Fast Multiplication Over GF(2)[x]............ 66 2.4 DivisionandInversion . 72 2.4.1 SeveralInversionsatOnce . 74 2.5 Exponentiation .......................... 76 2.5.1 BinaryExponentiation . 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 NotesandReferences . 82 Modern Computer Arithmetic, 0.0 7 § 3 Floating-Point Arithmetic 85 3.1 Representation .......................... 85 3.1.1 RadixChoice ....................... 86 3.1.2 ExponentRange ..................... 87 3.1.3 SpecialValues.. .. 88 3.1.4 SubnormalNumbers . 88 3.1.5 Encoding ......................... 89 3.1.6 Precision: Local, Global, Operation, Operand . 91 3.1.7 LinktoIntegers. .. 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-PointAddition . 99 3.2.2 Floating-PointSubtraction . 100 3.3 Multiplication . .. .. .102 3.3.1 Integer Multiplication via Complex FFT . 105 3.3.2 TheMiddleProduct . .107 3.4 ReciprocalandDivision . .109 3.4.1 Reciprocal.........................109 3.4.2 Division ..........................114 3.5 SquareRoot............................118 3.5.1 ReciprocalSquareRoot . .119 3.6 Conversion.............................122 3.6.1 Floating-PointOutput . .123 3.6.2 Floating-PointInput . .125 3.7 Exercises..............................126 3.8 NotesandReferences . .128 4 Newton’s Method and Function Evaluation 131 4.1 Introduction............................131 4.2 Newton’sMethod . .. .. .132 4.2.1 Newton’s MethodforInverse 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.2.1 of March 23, 2009 4.3 ArgumentReduction . .139 4.3.1 Repeated Use of Doubling Formulæ . 141 4.3.2 LossofPrecision . .141 4.3.3 GuardDigits .......................142 4.3.4 Doubling versus Tripling . 143 4.4 PowerSeries............................143 4.4.1 Direct Power Series Evaluation . 148 4.4.2 Power Series With Argument Reduction . 148 4.4.3 Rectangular Series Splitting . 149 4.5 AsymptoticExpansions. .154 4.6 ContinuedFractions . .158 4.7 RecurrenceRelations . .161