Numerical Computation Guide
Sunª ONE Studio 8
Sun Microsystems, Inc. 4150 Network Circle Santa Clara, CA 95054 U.S.A. 650-960-1300
Part No. 817-0932-10 May 2003, Revision A
Send comments about this document to: [email protected] Copyright © 2003 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,California 95054, U.S.A. All rights reserved. U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supplements. This distribution may include materials developed by third parties. Third-party software, including font technology, is copyrighted and licensed from Sun suppliers. Portions of this product are derived in part from Cray90, a product of Cray Research, Inc. libdwarf and libredblack are Copyright 2000 Silicon Graphics, Inc. and are available under the GNU Lesser General Public License from http://www.sgi.com. Parts of the product may be derived from Berkeley BSD systems, licensed from the University of California. UNIX is a registered trademark in the U.S. and in other countries, exclusively licensed through X/Open Company, Ltd. Sun, Sun Microsystems, the Sun logo, Java, Sun ONE Studio, the Solaris logo and the Sun ONE logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. Netscape and Netscape Navigator are trademarks or registered trademarks of Netscape Communications Corporation in the United States and other countries. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the U.S. and other countries. Products bearing SPARC trademarks are based upon architecture developed by Sun Microsystems, Inc. Products covered by and information contained in this service manual are controlled by U.S. Export Control laws and may be subject to the export or import laws in other countries. Nuclear, missile, chemical biological weapons or nuclear maritime end uses or end users, whether direct or indirect, are strictly prohibited. Export or reexport to countries subject to U.S. embargo or to entities identified on U.S. export exclusion lists, including, but not limited to, the denied persons and specially designated nationals lists is strictly prohibited. DOCUMENTATION IS PROVIDED “AS IS” AND ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
Copyright © 2003 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, Etats-Unis. Tous droits reserves. Droits du gouvernement americain, utlisateurs gouvernmentaux logiciel commercial. Les utilisateurs gouvernmentaux sont soumis au contrat de licence standard de Sun Microsystems, Inc., ainsi qu aux dispositions en vigueur de la FAR (Federal Acquisition Regulations) et des supplements a celles-ci. Distribue par des licences qui en restreignent l’utilisation. Cette distribution peut comprendre des composants developpes par des tierces parties. Des parties de ce produit pourront etre derivees Cray CF90, un produit de Cray Inc. Des parties de ce produit pourront etre derivees des systemes Berkeley BSD licencies par l’Universite de Californie.UNIX est une marque deposee aux Etats-Unis et dans d’autres pays et licenciee exclusivement par X/Open Company, Ltd. libdwarf et libredblack sont déposent 2000 Silicon Graphics, Inc. et sont disponible sous le GNU Moins Général Public Permis de http://www.sgi.com. Sun, Sun Microsystems, le logo Sun, Java, Sun ONE Studio, le logo Solaris et le logo Sun ONE sont des marques de fabrique ou des marques deposees de Sun Microsystems, Inc. aux Etats-Unis et dans d’autres pays. Netscape et Netscape Navigator sont des marques de fabrique ou des marques déposées de Netscape Communications Corporation aux Etats- Unis et dans d’autres pays. Toutes les marques SPARC sont utilisees sous licence et sont des marques de fabrique ou des marques deposees de SPARC International, Inc. aux Etats-Unis et dans d’autres pays. Les produits protant les marques SPARC sont bases sur une architecture developpee par Sun Microsystems, Inc. Les produits qui font l’objet de ce manuel d’entretien et les informations qu’il contient sont regis par la legislation americaine en matiere de controle des exportations et peuvent etre soumis au droit d’autres pays dans le domaine des exportations et importations. Les utilisations finales, ou utilisateurs finaux, pour des armes nucleaires, des missiles, des armes biologiques et chimiques ou du nucleaire maritime, directement ou indirectement, sont strictement interdites. Les exportations ou reexportations vers des pays sous embargo des Etats-Unis, ou vers des entites figurant sur les listes d’exclusion d’exportation americaines, y compris, mais de maniere non exclusive, la liste de personnes qui font objet d’un ordre de ne pas participer, d’une facon directe ou indirecte, aux exportations des produits ou des services qui sont regi par la legislation americaine en matiere de controle des exportations et la liste de ressortissants specifiquement designes, sont rigoureusement interdites. LA DOCUMENTATION EST FOURNIE “EN L’ÉTAT” ET TOUTES AUTRES CONDITIONS, DECLARATIONS ET GARANTIES EXPRESSES OU TACITES SONT FORMELLEMENT EXCLUES, DANS LA MESURE AUTORISEE PAR LA LOI APPLICABLE, Y COMPRIS NOTAMMENT TOUTE GARANTIE IMPLICITE RELATIVE A LA QUALITE MARCHANDE, A L’APTITUDE A UNE UTILISATION PARTICULIERE OU A L’ABSENCE DE CONTREFAÇON.
Please Recycle Contents
Before You Begin 13 Who Should Use This Book 13 How This Book Is Organized 13 Typographic Conventions 14 Shell Prompts 15 Accessing Compiler Collection Tools and Man Pages 16 Accessing Compiler Collection Documentation 18 Accessing Related Solaris Documentation 19 Resources for Developers 20 Contacting Sun Technical Support 20 Sun Welcomes Your Comments 20
1. Introduction 21 Floating-Point Environment 21
2. IEEE Arithmetic 23 IEEE Arithmetic Model 23 What Is IEEE Arithmetic? 23 IEEE Formats 25 Storage Formats 25
3 Single Format 25 Double Format 28 Double-Extended Format (SPARC) 30 Double-Extended Format (x86) 33 Ranges and Precisions in Decimal Representation 36 Base Conversion in the Solaris Environment 40 Underflow 41 Underflow Thresholds 41 How Does IEEE Arithmetic Treat Underflow? 42 Why Gradual Underflow? 43 Error Properties of Gradual Underflow 43 Two Examples of Gradual Underflow Versus Store 0 46 Does Underflow Matter? 47
3. The Math Libraries 49 Standard Math Library 49 Additional Math Libraries 50 Sun Math Library 50 Optimized Libraries 52 Vector Math Library (SPARC only) 53 libm9x Math Library 54 Single, Double, and Long Double Precision 56 IEEE Support Functions 57 ieee_functions(3m) and ieee_sun(3m) 57 ieee_values(3m) 59 ieee_flags(3m) 61 ieee_retrospective(3m) 63 nonstandard_arithmetic(3m) 65 C99 Floating Point Environment Functions 66
4 Numerical Computation Guide ¥ May 2003 Exception Flag Functions 66 Rounding Control 67 Environment Functions 67 Implementation Features of libm and libsunmath 69 About the Algorithms 70 Argument Reduction for Trigonometric Functions 70 Data Conversion Routines 71 Random Number Facilities 71
4. Exceptions and Exception Handling 73 What Is an Exception? 73 Notes for Table 4-1 76 Detecting Exceptions 77 ieee_flags(3m) 77 C99 Exception Flag Functions 79 Locating an Exception 81 Using the Debuggers to Locate an Exception 82 Using a Signal Handler to Locate an Exception 90 Using libm9x.so Exception Handling Extensions to Locate an Exception 96 Handling Exceptions 103
A. Examples 119 IEEE Arithmetic 119 The Math Libraries 121 Random Number Generator 121 IEEE Recommended Functions 124 IEEE Special Values 128 ieee_flags — Rounding Direction 130 C99 Floating Point Environment Functions 132
Contents 5 Exceptions and Exception Handling 137 ieee_flags — Accrued Exceptions 137 ieee_handler — Trapping Exceptions 140 ieee_handler — Abort on Exceptions 149 libm9x.so Exception Handling Features 149 Using libm9x.so With Fortran Programs 156 Miscellaneous 160 sigfpe — Trapping Integer Exceptions 160 Calling Fortran From C 161 Useful Debugging Commands 165
B. SPARC Behavior and Implementation 169 Floating-Point Hardware 169 Floating-Point Status Register and Queue 172 Special Cases Requiring Software Support 174 fpversion(1) Function — Finding Information About the FPU 179
C. x86 Behavior and Implementation 181
D. What Every Computer Scientist Should Know About Floating-Point Arithmetic 183 Abstract 183 Introduction 184 Rounding Error 184 Floating-point Formats 185 Relative Error and Ulps 187 Guard Digits 188 Cancellation 190 Exactly Rounded Operations 195 The IEEE Standard 198
6 Numerical Computation Guide ¥ May 2003 Formats and Operations 199 Special Quantities 204 NaNs 205 Exceptions, Flags and Trap Handlers 211 Systems Aspects 216 Instruction Sets 216 Languages and Compilers 218 Exception Handling 226 The Details 227 Rounding Error 228 Binary to Decimal Conversion 237 Errors In Summation 238 Summary 240 Acknowledgments 240 References 241 Theorem 14 and Theorem 8 243 Theorem 14 243 Proof 243 Differences Among IEEE 754 Implementations 247 Current IEEE 754 Implementations 249 Pitfalls in Computations on Extended-Based Systems 250 Programming Language Support for Extended Precision 256 Conclusion 260
E. Standards Compliance 263 SVID History 263 IEEE 754 History 264 SVID Future Directions 264 SVID Implementation 265
Contents 7 General Notes on Exceptional Cases and libm Functions 267 Notes on libm 268 LIA-1 Conformance 268
F. References 273 Chapter 2: “IEEE Arithmetic” 273 Chapter 3: “The Math Libraries” 274 Chapter 4: “Exceptions and Signal Handling” 275 Appendix B: “SPARC Behavior and Implementation” 275 Standards 276 Test Programs 276
Glossary 277
Index 291
8 Numerical Computation Guide ¥ May 2003 Figures
FIGURE 2-1 Single-Storage Format 26
FIGURE 2-2 Double-Storage Format 28
FIGURE 2-3 Double-Extended Format (SPARC) 31
FIGURE 2-4 Double-Extended Format (x86) 34
FIGURE 2-5 Comparison of a Set of Numbers Defined by Digital and Binary Representation 37
FIGURE 2-6 Number Line 44
FIGURE B-1 SPARC Floating-Point Status Register 173 β FIGURE D-1 Normalized Numbers When = 2, p = 3, emin = -1, emax = 2 187 FIGURE D-2 Flush To Zero Compared With Gradual Underflow 210
9 10 Numerical Computation Guide ¥ May 2003 Tables
TABLE 2-1 IEEE Formats and Language Types 25
TABLE 2-2 Values Represented by Bit Patterns in IEEE Single Format 26
TABLE 2-3 Bit Patterns in Single-Storage Format and Their IEEE Values 27
TABLE 2-4 Values Represented by Bit Patterns in IEEE Double Format 29
TABLE 2-5 Bit Patterns in Double-Storage Format and Their IEEE Values 30
TABLE 2-6 Values Represented by Bit Patterns (SPARC) 31
TABLE 2-7 Bit Patterns in Double-Extended Format (SPARC) 32
TABLE 2-8 Values Represented by Bit Patterns (x86) 34
TABLE 2-9 Bit Patterns in Double-Extended Format and Their Values (x86) 36
TABLE 2-10 Range and Precision of Storage Formats 39
TABLE 2-11 Underflow Thresholds 41
TABLE 2-12 ulp(1) in Four Different Precisions 44
TABLE 2-13 Gaps Be(tween Representable Single-Format Floating-Point Numbers 45
TABLE 3-1 Contents of libm 50
TABLE 3-2 Contents of libsunmath 51
TABLE 3-3 Contents of libmvec 54
TABLE 3-4 Contents of libm9x 55
TABLE 3-5 Calling Single, Double, and Quadruple Functions 56
TABLE 3-6 ieee_functions(3m) 57
TABLE 3-8 Calling ieee_functions From Fortran 58
11 TABLE 3-9 Calling ieee_sun From Fortran 58
TABLE 3-7 ieee_sun(3m) 58
TABLE 3-10 IEEE Values: Single Precision 59
TABLE 3-11 IEEE Values: Double Precision 59
TABLE 3-12 IEEE Values: Quadruple Precision (SPARC) 60
TABLE 3-14 Parameter Values for ieee_flags 61
TABLE 3-13 IEEE Values: Double Extended Precision (x86) 61
TABLE 3-15 ieee_flags Input Values for the Rounding Direction 62
TABLE 3-16 C99 Standard Exception Flag Functions 66
TABLE 3-17 libm9x.so Floating Point Environment Functions 68
TABLE 3-18 Intervals for Single-Value Random Number Generators 72
TABLE 4-1 IEEE Floating Point Exceptions 75
TABLE 4-2 Unordered Comparisons 76
TABLE 4-3 Exception Bits 79
TABLE 4-4 Types for Arithmetic Exceptions 93
TABLE 4-5 Exception Codes for fex_set_handling 97
TABLE A-1 Some Debugging Commands (SPARC) 165
TABLE A-2 Some Debugging Commands (x86) 166
TABLE B-1 SPARC Floating-Point Options 170
TABLE B-2 Floating-Point Status Register Fields 173
TABLE B-3 Exception Handling Fields 174
TABLE D-1 IEEE 754 Format Parameters 200
TABLE D-2 IEEE 754 Special Values 205
TABLE D-3 Operations That Produce a NaN 206
TABLE D-4 Exceptions in IEEE 754* 212
TABLE E-1 Exceptional Cases and libm Functions 265
TABLE E-2 LIA-1 Conformance - Notation 270
12 Numerical Computation Guide ¥ May 2003 Before You Begin
This manual describes the floating-point environment supported by software and hardware on SPARC® and x86 platforms running the Solaris™ operating environment. Although this manual discusses some general aspects of the SPARC and Intel architectures, it is primarily a reference manual designed to accompany Sun™ language products.
Certain aspects of the IEEE Standard for Binary Floating-Point Arithmetic are discussed in this manual. To learn about IEEE arithmetic, see the 18-page Standard. See Appendix F for a brief bibliography on IEEE arithmetic.
Who Should Use This Book
This manual is written for those who develop, maintain, and port mathematical and scientific applications or benchmarks. Before using this manual, you should be familiar with the programming language used (Fortran, C, etc.), dbx (the source- level debugger), and the operating system commands and concepts.
How This Book Is Organized
Chapter 1 introduces the floating-point environment.
Chapter 2 describes the IEEE arithmetic model, IEEE formats, and underflow.
Chapter 3 describes the mathematics libraries provided with the Sun™ Open Net Environment (Sun ONE) Studio compilers.
13 Chapter 4 describes exceptions and shows how to detect, locate, and handle them.
Appendix A contains example programs.
Appendix B describes the floating-point hardware options for SPARC workstations.
Appendix C lists x86 and SPARC compatibility issues related to the floating-point units used in Intel systems.
Appendix D is an edited reprint of a tutorial on floating-point arithmetic by David Goldberg.
Appendix E discusses standards compliance.
Appendix F includes a list of references and related documentation.
Glossary contains a definition of terms.
The examples in this manual are in C and Fortran, but the concepts apply to either compiler on a SPARC or Intel system.
Typographic Conventions
TABLE P-1 Typeface Conventions
Typeface Meaning Examples
AaBbCc123 The names of commands, files, Edit your .login file. and directories; on-screen Use ls -a to list all files. computer output % You have mail.
AaBbCc123 What you type, when contrasted % su with on-screen computer output Password:
AaBbCc123 Book titles, new words or terms, Read Chapter 6 in the User’s Guide. words to be emphasized These are called class options. You must be superuser to do this. AaBbCc123 Command-line placeholder text; To delete a file, type rm filename. replace with a real name or value
14 Numerical Computation Guide ¥ May 2003 TABLE P-2 Code Conventions
Code Symbol Meaning Notation Code Example
[ ] Brackets contain arguments O[n] O4, O that are optional. { } Braces contain a set of choices d{y|n} dy for a required option. | The “pipe” or “bar” symbol B{dynamic|static} Bstatic separates arguments, only one of which may be chosen. : The colon, like the comma, is Rdir[:dir] R/local/libs:/U/a sometimes used to separate arguments. … The ellipsis indicates omission xinline=f1[,…fn] xinline=alpha,dos in a series.
Shell Prompts
Shell Prompt
C shell machine-name% C shell superuser machine-name# Bourne shell and Korn shell $ Superuser for Bourne shell and Korn shell #
Before You Begin 15 Accessing Compiler Collection Tools and Man Pages
The compiler collection components and man pages are not installed into the standard /usr/bin/ and /usr/share/man directories. To access the compilers and tools, you must have the compiler collection component directory in your PATH environment variable. To access the man pages, you must have the compiler collection man page directory in your MANPATH environment variable.
For more information about the PATH variable, see the csh(1), sh(1), and ksh(1) man pages. For more information about the MANPATH variable, see the man(1) man page. For more information about setting your PATH variable and MANPATH variables to access this release, see the installation guide or your system administrator.
Note Ð The information in this section assumes that your Sun ONE Studio Compiler Collection components are installed in the /opt directory. If your software is not installed in the /opt directory, ask your system administrator for the equivalent path on your system.
Accessing the Compilers and Tools
Use the steps below to determine whether you need to change your PATH variable to access the compilers and tools.