ebook img

Modern Computer Arithmetic - Loria PDF

239 Pages·2009·1.89 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Modern Computer Arithmetic - Loria

Modern Computer Arithmetic Richard P. Brent and Paul Zimmermann Version 0.4 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. Youmaynotusethisworkforcommercialpurposes. • 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—wedonotcovercomputerarchitectureorthedesignofcomputer 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 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 is to present the latest developments in a concise manner. At the same time, we provide a self-contained introduction for the reader who is not an expert in the field. Chapter 2 is concerned with modular arithmetic and the FFT, 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. 3 4 Modern Computer Arithmetic, version 0.4 of November 10, 2009 Chapter 4 deals with the computation, to arbitrary precision, of functions 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, wehaveconcentratedonmethodsthatareefficientandsuitableforarbitrary- 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 and not getting too involved in low-level or machine-dependent details. 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 welcome comments and corrections. Please send them to either of the authors. Richard Brent and Paul Zimmermann [email protected] [email protected] Canberra and Nancy, November 2009 Acknowledgements 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, toonumeroustomentionhere, butacknowledgedinthetextandintheNotes and References sections at the end of each chapter. We also thank people who have sent us comments and corrections on earlier versions of this book: J¨org Arndt, Wolfgang Ehrhardt, Steven Gal- braith, Torbj¨orn Granlund, Guillaume Hanrot, Marc Mezzarobba, Jean- Michel Muller, Sidimohamed Sedjelmaci, Mark Wezelenburg. Two anony- mous reviewers provided very helpful suggestions. We also thank the authors of the LATEX program, which allowed us to produce this book, the authors of the gnuplot program, and the authors of the GNU MP library, which helped us to illustrate several algorithms with concrete figures. Finally, we acknowledge the contribution of Erin Brent, who first sug- gested writing the book; and thank our wives, Judy-anne and Marie, for their patience and encouragement. 5 6 Modern Computer Arithmetic, version 0.4 of November 10, 2009 Contents Preface 3 Acknowledgements 5 Contents 6 Notation 13 1 Integer Arithmetic 17 1.1 Representation and Notations . . . . . . . . . . . . . . . . . . 17 1.2 Addition and Subtraction . . . . . . . . . . . . . . . . . . . . 18 1.3 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.3.1 Naive Multiplication . . . . . . . . . . . . . . . . . . . 20 1.3.2 Karatsuba’s Algorithm . . . . . . . . . . . . . . . . . . 21 1.3.3 Toom-Cook Multiplication . . . . . . . . . . . . . . . . 22 1.3.4 Fast Fourier Transform (FFT) . . . . . . . . . . . . . . 24 1.3.5 Unbalanced Multiplication . . . . . . . . . . . . . . . . 24 1.3.6 Squaring . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.7 Multiplication by a Constant . . . . . . . . . . . . . . 30 1.4 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 1.4.1 Naive Division . . . . . . . . . . . . . . . . . . . . . . . 31 1.4.2 Divisor Preconditioning . . . . . . . . . . . . . . . . . 33 1.4.3 Divide and Conquer Division . . . . . . . . . . . . . . 34 1.4.4 Newton’s Method . . . . . . . . . . . . . . . . . . . . . 38 1.4.5 Exact Division . . . . . . . . . . . . . . . . . . . . . . 38 1.4.6 Only Quotient or Remainder Wanted . . . . . . . . . . 39 1.4.7 Division by a Constant . . . . . . . . . . . . . . . . . . 40 1.4.8 Hensel’s Division . . . . . . . . . . . . . . . . . . . . . 41 7 8 Modern Computer Arithmetic, version 0.4 of November 10, 2009 1.5 Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 1.5.1 Square Root . . . . . . . . . . . . . . . . . . . . . . . . 42 1.5.2 k-th Root . . . . . . . . . . . . . . . . . . . . . . . . . 43 1.5.3 Exact Root . . . . . . . . . . . . . . . . . . . . . . . . 44 1.6 Greatest Common Divisor . . . . . . . . . . . . . . . . . . . . 46 1.6.1 Naive GCD . . . . . . . . . . . . . . . . . . . . . . . . 46 1.6.2 Extended GCD . . . . . . . . . . . . . . . . . . . . . . 49 1.6.3 Half Binary GCD, Divide and Conquer GCD . . . . . . 50 1.7 Base Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . 54 1.7.1 Quadratic Algorithms . . . . . . . . . . . . . . . . . . 54 1.7.2 Subquadratic Algorithms . . . . . . . . . . . . . . . . . 54 1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 1.9 Notes and References . . . . . . . . . . . . . . . . . . . . . . . 60 2 Modular Arithmetic and the FFT 63 2.1 Representation . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.1.1 Classical Representation . . . . . . . . . . . . . . . . . 64 2.1.2 Montgomery’s Form . . . . . . . . . . . . . . . . . . . 64 2.1.3 Residue Number Systems . . . . . . . . . . . . . . . . 65 2.1.4 MSB vs LSB Algorithms . . . . . . . . . . . . . . . . . 65 2.1.5 Link with Polynomials . . . . . . . . . . . . . . . . . . 65 2.2 Addition and Subtraction . . . . . . . . . . . . . . . . . . . . 66 2.3 The Fourier Transform . . . . . . . . . . . . . . . . . . . . . . 66 2.3.1 Theoretical Setting . . . . . . . . . . . . . . . . . . . . 67 2.3.2 The Fast Fourier Transform . . . . . . . . . . . . . . . 68 2.3.3 The Sch¨onhage-Strassen Algorithm . . . . . . . . . . . 71 2.4 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 2.4.1 Barrett’s Algorithm . . . . . . . . . . . . . . . . . . . . 75 2.4.2 Montgomery’s Multiplication . . . . . . . . . . . . . . 76 2.4.3 McLaughlin’s Algorithm . . . . . . . . . . . . . . . . . 80 2.4.4 Special Moduli . . . . . . . . . . . . . . . . . . . . . . 81 2.5 Division and Inversion . . . . . . . . . . . . . . . . . . . . . . 82 2.5.1 Several Inversions at Once . . . . . . . . . . . . . . . . 83 2.6 Exponentiation . . . . . . . . . . . . . . . . . . . . . . . . . . 85 2.6.1 Binary Exponentiation . . . . . . . . . . . . . . . . . . 86 2.6.2 Base 2k Exponentiation . . . . . . . . . . . . . . . . . 87 2.6.3 Sliding Window and Redundant Representation . . . . 88 2.7 Chinese Remainder Theorem . . . . . . . . . . . . . . . . . . . 90 Modern Computer Arithmetic, 0.0 9 § 2.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 2.9 Notes and References . . . . . . . . . . . . . . . . . . . . . . . 93 3 Floating-Point Arithmetic 95 3.1 Representation . . . . . . . . . . . . . . . . . . . . . . . . . . 95 3.1.1 Radix Choice . . . . . . . . . . . . . . . . . . . . . . . 96 3.1.2 Exponent Range . . . . . . . . . . . . . . . . . . . . . 97 3.1.3 Special Values . . . . . . . . . . . . . . . . . . . . . . . 98 3.1.4 Subnormal Numbers . . . . . . . . . . . . . . . . . . . 98 3.1.5 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . 99 3.1.6 Precision: Local, Global, Operation, Operand . . . . . 101 3.1.7 Link to Integers . . . . . . . . . . . . . . . . . . . . . . 102 3.1.8 Ziv’s Algorithm and Error Analysis . . . . . . . . . . . 102 3.1.9 Rounding . . . . . . . . . . . . . . . . . . . . . . . . . 104 3.1.10 Strategies . . . . . . . . . . . . . . . . . . . . . . . . . 107 3.2 Addition, Subtraction, Comparison . . . . . . . . . . . . . . . 108 3.2.1 Floating-Point Addition . . . . . . . . . . . . . . . . . 109 3.2.2 Floating-Point Subtraction . . . . . . . . . . . . . . . . 110 3.3 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 3.3.1 Integer Multiplication via Complex FFT . . . . . . . . 116 3.3.2 The Middle Product . . . . . . . . . . . . . . . . . . . 117 3.4 Reciprocal and Division . . . . . . . . . . . . . . . . . . . . . 119 3.4.1 Reciprocal . . . . . . . . . . . . . . . . . . . . . . . . . 119 3.4.2 Division . . . . . . . . . . . . . . . . . . . . . . . . . . 124 3.5 Square Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 3.5.1 Reciprocal Square Root . . . . . . . . . . . . . . . . . 130 3.6 Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 3.6.1 Floating-Point Output . . . . . . . . . . . . . . . . . . 133 3.6.2 Floating-Point Input . . . . . . . . . . . . . . . . . . . 136 3.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 3.8 Notes and References . . . . . . . . . . . . . . . . . . . . . . . 138 4 Newton’s Method and Function Evaluation 143 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 4.2 Newton’s Method . . . . . . . . . . . . . . . . . . . . . . . . . 144 4.2.1 Newton’s Method for Inverse Roots . . . . . . . . . . . 146 4.2.2 Newton’s Method for Reciprocals . . . . . . . . . . . . 146 4.2.3 Newton’s Method for (Reciprocal) Square Roots . . . . 147 10 Modern Computer Arithmetic, version 0.4 of November 10, 2009 4.2.4 Newton’s Method for Formal Power Series . . . . . . . 148 4.2.5 Newton’s Method for Functional Inverses . . . . . . . . 149 4.2.6 Higher Order Newton-like Methods . . . . . . . . . . . 150 4.3 Argument Reduction . . . . . . . . . . . . . . . . . . . . . . . 151 4.3.1 Repeated Use of a Doubling Formula . . . . . . . . . . 153 4.3.2 Loss of Precision . . . . . . . . . . . . . . . . . . . . . 153 4.3.3 Guard Digits . . . . . . . . . . . . . . . . . . . . . . . 154 4.3.4 Doubling versus Tripling . . . . . . . . . . . . . . . . . 155 4.4 Power Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 4.4.1 Direct Power Series Evaluation . . . . . . . . . . . . . 159 4.4.2 Power Series With Argument Reduction . . . . . . . . 160 4.4.3 Rectangular Series Splitting . . . . . . . . . . . . . . . 161 4.5 Asymptotic Expansions . . . . . . . . . . . . . . . . . . . . . . 164 4.6 Continued Fractions . . . . . . . . . . . . . . . . . . . . . . . 170 4.7 Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . 173 4.7.1 Evaluation of Bessel Functions . . . . . . . . . . . . . . 174 4.7.2 Evaluation of Bernoulli and Tangent numbers . . . . . 175 4.8 Arithmetic-Geometric Mean . . . . . . . . . . . . . . . . . . . 179 4.8.1 Elliptic Integrals . . . . . . . . . . . . . . . . . . . . . 179 4.8.2 First AGM Algorithm for the Logarithm . . . . . . . . 180 4.8.3 Theta Functions . . . . . . . . . . . . . . . . . . . . . . 181 4.8.4 Second AGM Algorithm for the Logarithm . . . . . . . 183 4.8.5 The Complex AGM . . . . . . . . . . . . . . . . . . . . 184 4.9 Binary Splitting . . . . . . . . . . . . . . . . . . . . . . . . . . 185 4.9.1 A Binary Splitting Algorithm for sin,cos . . . . . . . . 188 4.9.2 The Bit-Burst Algorithm . . . . . . . . . . . . . . . . . 188 4.10 Contour Integration . . . . . . . . . . . . . . . . . . . . . . . . 191 4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 4.12 Notes and References . . . . . . . . . . . . . . . . . . . . . . . 201 5 Appendix: Implementations and Pointers 207 5.1 Software Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 5.1.1 CLN . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 5.1.2 GNU MP (GMP) . . . . . . . . . . . . . . . . . . . . . 207 5.1.3 MPFQ . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 5.1.4 MPFR . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 5.2 Mailing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 5.2.1 The BNIS Mailing List . . . . . . . . . . . . . . . . . . 209

Description:
Modern Computer Arithmetic, version 0.4 of November 10, 2009. Chapter 4 deals with the computation, to arbitrary precision, of functions such as sqrt, exp, ln,
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.