APFloat.h revision 2e2922cd90922eaf3736421bd0ad4331f89f3e75
1b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//== llvm/Support/APFloat.h - Arbitrary Precision Floating Point -*- C++ -*-==// 2b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner// 3b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner// The LLVM Compiler Infrastructure 4b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 7b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner// 8b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//===----------------------------------------------------------------------===// 97f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 107f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// \file 117f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// \brief 127f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// This file declares a class to represent arbitrary precision floating point 137f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// values and provide a variety of arithmetic operations on them. 147f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 15b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner//===----------------------------------------------------------------------===// 16b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner 17674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_ADT_APFLOAT_H 18674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_ADT_APFLOAT_H 19b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner 20b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner#include "llvm/ADT/APInt.h" 21b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner 22b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattnernamespace llvm { 23b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner 247f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// A signed type to represent a floating point numbers unbiased exponent. 25fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmantypedef signed short exponent_t; 26fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 27fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanstruct fltSemantics; 28fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanclass APSInt; 29fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanclass StringRef; 30fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 317f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Enum that represents what fraction of the LSB truncated bits of an fp number 327f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// represent. 337f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 347f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// This essentially combines the roles of guard and sticky bits. 35fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanenum lostFraction { // Example of truncated bits: 36fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman lfExactlyZero, // 000000 37fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman lfLessThanHalf, // 0xxxxx x's not all zero 38fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman lfExactlyHalf, // 100000 39fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman lfMoreThanHalf // 1xxxxx x's not all zero 40fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman}; 41fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 427f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// \brief A self-contained host- and target-independent arbitrary-precision 437f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// floating-point software implementation. 447f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 457f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// APFloat uses bignum integer arithmetic as provided by static functions in 467f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// the APInt class. The library will work with bignum integers whose parts are 477f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// any unsigned type at least 16 bits wide, but 64 bits is recommended. 487f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 497f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Written for clarity rather than speed, in particular with a view to use in 507f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// the front-end of a cross compiler so that target arithmetic can be correctly 517f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// performed on the host. Performance should nonetheless be reasonable, 527f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// particularly for its intended use. It may be useful as a base 537f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// implementation for a run-time library during development of a faster 547f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// target-specific one. 557f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 567f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// All 5 rounding modes in the IEEE-754R draft are handled correctly for all 577f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// implemented operations. Currently implemented operations are add, subtract, 587f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// multiply, divide, fused-multiply-add, conversion-to-float, 597f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// conversion-to-integer and conversion-from-integer. New rounding modes 607f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// (e.g. away from zero) can be added with three or four lines of code. 617f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 627f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Four formats are built-in: IEEE single precision, double precision, 637f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// quadruple precision, and x87 80-bit extended double (when operating with 647f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// full extended precision). Adding a new format that obeys IEEE semantics 657f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// only requires adding two lines of code: a declaration and definition of the 667f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// format. 677f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 687f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// All operations return the status of that operation as an exception bit-mask, 697f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// so multiple operations can be done consecutively with their results or-ed 707f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// together. The returned status can be useful for compiler diagnostics; e.g., 717f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// inexact, underflow and overflow can be easily diagnosed on constant folding, 727f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// and compiler optimizers can determine what exceptions would be raised by 737f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// folding operations and optimize, or perhaps not optimize, accordingly. 747f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 757f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// At present, underflow tininess is detected after rounding; it should be 767f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// straight forward to add support for the before-rounding case too. 777f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 787f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// The library reads hexadecimal floating point numbers as per C99, and 797f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// correctly rounds if necessary according to the specified rounding mode. 807f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Syntax is required to have been validated by the caller. It also converts 817f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// floating point numbers to hexadecimal text as per the C99 %a and %A 827f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// conversions. The output precision (or alternatively the natural minimal 837f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// precision) can be specified; if the requested precision is less than the 847f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// natural precision the output is correctly rounded for the specified rounding 857f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// mode. 867f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 877f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// It also reads decimal floating point numbers and correctly rounds according 887f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// to the specified rounding mode. 897f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 907f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Conversion to decimal text is not currently implemented. 917f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 927f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Non-zero finite numbers are represented internally as a sign bit, a 16-bit 937f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// signed exponent, and the significand as an array of integer parts. After 947f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// normalization of a number of precision P the exponent is within the range of 957f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// the format, and if the number is not denormal the P-th bit of the 967f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// significand is set as an explicit integer bit. For denormals the most 977f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// significant bit is shifted right so that the exponent is maintained at the 987f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// format's minimum, so that the smallest denormal has just the least 997f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// significant bit of the significand set. The sign of zeroes and infinities 1007f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// is significant; the exponent and significand of such numbers is not stored, 1017f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// but has a known implicit (deterministic) value: 0 for the significands, 0 1027f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// for zero exponent, all 1 bits for infinity exponent. For NaNs the sign and 1037f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// significand are deterministic, although not really meaningful, and preserved 1047f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// in non-conversion operations. The exponent is implicitly all 1 bits. 1057f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 1067f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// APFloat does not provide any exception handling beyond default exception 1077f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// handling. We represent Signaling NaNs via IEEE-754R 2008 6.2.1 should clause 1087f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// by encoding Signaling NaNs with the first bit of its trailing significand as 1097f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 0. 1107f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 1117f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// TODO 1127f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// ==== 1137f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 1147f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Some features that may or may not be worth adding: 1157f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 1167f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Binary to decimal conversion (hard). 1177f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 1187f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// Optional ability to detect underflow tininess before rounding. 1197f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 1207f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// New formats: x87 in single and double precision mode (IEEE apart from 1217f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// extended exponent range) (hard). 1227f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 1237f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// New operations: sqrt, IEEE remainder, C90 fmod, nextafter, nexttoward. 1247f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 125fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanclass APFloat { 126fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanpublic: 127fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 1287f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Floating Point Semantics. 1297f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 1307f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 131fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static const fltSemantics IEEEhalf; 132fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static const fltSemantics IEEEsingle; 133fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static const fltSemantics IEEEdouble; 134fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static const fltSemantics IEEEquad; 135fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static const fltSemantics PPCDoubleDouble; 136fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static const fltSemantics x87DoubleExtended; 1377f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 1387f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// A Pseudo fltsemantic used to construct APFloats that cannot conflict with 1397f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// anything real. 140fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static const fltSemantics Bogus; 141fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 1422e2922cd90922eaf3736421bd0ad4331f89f3e75Michael Gottesman /// @} 1432e2922cd90922eaf3736421bd0ad4331f89f3e75Michael Gottesman 144fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static unsigned int semanticsPrecision(const fltSemantics &); 145fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 1467f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// IEEE-754R 5.11: Floating Point Comparison Relations. 147fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman enum cmpResult { 148fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman cmpLessThan, 149fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman cmpEqual, 150fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman cmpGreaterThan, 151fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman cmpUnordered 152b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner }; 153b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner 1547f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// IEEE-754R 4.3: Rounding-direction attributes. 155fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman enum roundingMode { 156fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman rmNearestTiesToEven, 157fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman rmTowardPositive, 158fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman rmTowardNegative, 159fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman rmTowardZero, 160fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman rmNearestTiesToAway 161fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman }; 162e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall 1637f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// IEEE-754R 7: Default exception handling. 1647f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// 1657f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// opUnderflow or opOverflow are always returned or-ed with opInexact. 166fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman enum opStatus { 167fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opOK = 0x00, 168fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opInvalidOp = 0x01, 169fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opDivByZero = 0x02, 170fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opOverflow = 0x04, 171fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opUnderflow = 0x08, 172fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opInexact = 0x10 173fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman }; 174e12b73816b50bbe2cc54b8005d86c95413b4f465John McCall 1757f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Category of internally-represented number. 176fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman enum fltCategory { 177fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman fcInfinity, 178fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman fcNaN, 179fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman fcNormal, 180fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman fcZero 181fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman }; 1823a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 1837f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Convenience enum used to construct an uninitialized APFloat. 184fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman enum uninitializedTag { 185fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman uninitialized 186b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner }; 1873a9b71434cda6f66d65a031effec1bbe58e1dda3Rafael Espindola 1887f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Constructors 1897f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 1907f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 191fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APFloat(const fltSemantics &); // Default construct to 0.0 192fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APFloat(const fltSemantics &, StringRef); 193fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APFloat(const fltSemantics &, integerPart); 194fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APFloat(const fltSemantics &, fltCategory, bool negative); 195fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APFloat(const fltSemantics &, uninitializedTag); 196fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APFloat(const fltSemantics &, const APInt &); 197fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman explicit APFloat(double d); 198fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman explicit APFloat(float f); 199fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APFloat(const APFloat &); 200fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman ~APFloat(); 201fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2027f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 2037f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 2047f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Convenience "constructors" 2057f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 2067f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 2078a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman /// Factory for Positive and Negative Zero. 2088a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman /// 2098a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman /// \param Negative True iff the number should be negative. 210fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat getZero(const fltSemantics &Sem, bool Negative = false) { 211fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman return APFloat(Sem, fcZero, Negative); 212fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman } 2138a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman 2148a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman /// Factory for Positive and Negative Infinity. 2158a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman /// 2168a70f5815dda98d23f0bbf519532703886159cdaMichael Gottesman /// \param Negative True iff the number should be negative. 217fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat getInf(const fltSemantics &Sem, bool Negative = false) { 218fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman return APFloat(Sem, fcInfinity, Negative); 219fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman } 220fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2217f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Factory for QNaN values. 222fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 223fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \param Negative - True iff the NaN generated should be negative. 224fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \param type - The unspecified fill bits for creating the NaN, 0 by 225fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// default. The value is truncated as necessary. 226fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat getNaN(const fltSemantics &Sem, bool Negative = false, 227fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman unsigned type = 0) { 228fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman if (type) { 229fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APInt fill(64, type); 230fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman return getQNaN(Sem, Negative, &fill); 231fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman } else { 232fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman return getQNaN(Sem, Negative, 0); 233fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman } 234fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman } 235fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2367f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Factory for QNaN values. 237fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat getQNaN(const fltSemantics &Sem, bool Negative = false, 238fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman const APInt *payload = 0) { 239fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman return makeNaN(Sem, false, Negative, payload); 240fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman } 241fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2427f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Factory for SNaN values. 243fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat getSNaN(const fltSemantics &Sem, bool Negative = false, 244fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman const APInt *payload = 0) { 245fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman return makeNaN(Sem, true, Negative, payload); 246fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman } 247fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2487f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Returns the largest finite number in the given semantics. 249fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 250fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \param Negative - True iff the number should be negative 251fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat getLargest(const fltSemantics &Sem, bool Negative = false); 252fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2537f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Returns the smallest (by magnitude) finite number in the given semantics. 2547f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Might be denormalized, which implies a relative loss of precision. 255fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 256fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \param Negative - True iff the number should be negative 257fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat getSmallest(const fltSemantics &Sem, bool Negative = false); 258fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2597f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Returns the smallest (by magnitude) normalized finite number in the given 2607f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// semantics. 261fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 262fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \param Negative - True iff the number should be negative 263fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat getSmallestNormalized(const fltSemantics &Sem, 264fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool Negative = false); 265fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2667f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Returns a float which is bitcasted from an all one value int. 267fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 268fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \param BitWidth - Select float type 269fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \param isIEEE - If 128 bit number, select between PPC and IEEE 270fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat getAllOnesValue(unsigned BitWidth, bool isIEEE = false); 271fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2727f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 2737f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 2747f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Used to insert APFloat objects, or objects that contain APFloat objects, 2757f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// into FoldingSets. 276fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void Profile(FoldingSetNodeID &NID) const; 277fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2787f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \brief Used by the Bitcode serializer to emit APInts to Bitcode. 279fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void Emit(Serializer &S) const; 280fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2817f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \brief Used by the Bitcode deserializer to deserialize APInts. 282fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat ReadVal(Deserializer &D); 283fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 2847f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Arithmetic 2857f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 2867f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 287fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus add(const APFloat &, roundingMode); 288fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus subtract(const APFloat &, roundingMode); 289fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus multiply(const APFloat &, roundingMode); 290fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus divide(const APFloat &, roundingMode); 2917f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// IEEE remainder. 292fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus remainder(const APFloat &); 2937f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// C fmod, or llvm frem. 294fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus mod(const APFloat &, roundingMode); 295fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus fusedMultiplyAdd(const APFloat &, const APFloat &, roundingMode); 296fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus roundToIntegral(roundingMode); 297964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// IEEE-754R 5.3.1: nextUp/nextDown. 298964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman opStatus next(bool nextDown); 299fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 3007f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Sign operations. 3017f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 3027f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 303fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void changeSign(); 304fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void clearSign(); 305fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void copySign(const APFloat &); 306fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 3077f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 3087f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 3097f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Conversions 3107f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 3117f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 312fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convert(const fltSemantics &, roundingMode, bool *); 313fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertToInteger(integerPart *, unsigned int, bool, roundingMode, 314fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool *) const; 315fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertToInteger(APSInt &, roundingMode, bool *) const; 316fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertFromAPInt(const APInt &, bool, roundingMode); 317fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertFromSignExtendedInteger(const integerPart *, unsigned int, 318fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool, roundingMode); 319fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int, 320fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool, roundingMode); 321fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertFromString(StringRef, roundingMode); 322fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APInt bitcastToAPInt() const; 323fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman double convertToDouble() const; 324fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman float convertToFloat() const; 325fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 3267f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 3277f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 3287f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// The definition of equality is not straightforward for floating point, so 3297f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// we won't use operator==. Use one of the following, or write whatever it 3307f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// is you really mean. 331fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool operator==(const APFloat &) const LLVM_DELETED_FUNCTION; 332fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 3337f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// IEEE comparison with another floating point number (NaNs compare 3347f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// unordered, 0==-0). 335fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman cmpResult compare(const APFloat &) const; 336fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 3377f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Bitwise comparison for equality (QNaNs compare equal, 0!=-0). 338fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool bitwiseIsEqual(const APFloat &) const; 339fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 3407f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Write out a hexadecimal representation of the floating point value to DST, 3417f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// which must be of sufficient size, in the C99 form [-]0xh.hhhhp[+-]d. 3427f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Return the number of characters written, excluding the terminating NUL. 343fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman unsigned int convertToHexString(char *dst, unsigned int hexDigits, 344fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool upperCase, roundingMode) const; 345fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 3467f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Simple Queries 3477f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 3487f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 349fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman fltCategory getCategory() const { return category; } 350fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman const fltSemantics &getSemantics() const { return *semantics; } 351fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool isZero() const { return category == fcZero; } 352fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool isNonZero() const { return category != fcZero; } 353fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool isNormal() const { return category == fcNormal; } 354fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool isNaN() const { return category == fcNaN; } 355fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool isInfinity() const { return category == fcInfinity; } 356fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool isNegative() const { return sign; } 357fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool isPosZero() const { return isZero() && !isNegative(); } 358fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool isNegZero() const { return isZero() && isNegative(); } 359fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool isDenormal() const; 360964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// IEEE-754R 5.7.2: isSignaling. Returns true if this is a signaling NaN. 361964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman bool isSignaling() const; 362fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 3637f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 3647f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 365fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APFloat &operator=(const APFloat &); 366fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 367fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \brief Overload to compute a hash code for an APFloat value. 368fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 369fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// Note that the use of hash codes for floating point values is in general 370fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// frought with peril. Equality is hard to define for these values. For 371fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// example, should negative and positive zero hash to different codes? Are 372fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// they equal or not? This hash value implementation specifically 373fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// emphasizes producing different codes for different inputs in order to 374fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// be used in canonicalization and memoization. As such, equality is 375fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// bitwiseIsEqual, and 0 != -0. 376fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman friend hash_code hash_value(const APFloat &Arg); 377fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 378fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// Converts this value into a decimal string. 379fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 380fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \param FormatPrecision The maximum number of digits of 381fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// precision to output. If there are fewer digits available, 382fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// zero padding will not be used unless the value is 383fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// integral and small enough to be expressed in 384fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// FormatPrecision digits. 0 means to use the natural 385fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// precision of the number. 386fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// \param FormatMaxPadding The maximum number of zeros to 387fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// consider inserting before falling back to scientific 388fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// notation. 0 means to always use scientific notation. 389fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 390fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// Number Precision MaxPadding Result 391fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// ------ --------- ---------- ------ 392fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 1.01E+4 5 2 10100 393fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 1.01E+4 4 2 1.01E+4 394fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 1.01E+4 5 1 1.01E+4 395fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 1.01E-2 5 2 0.0101 396fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 1.01E-2 4 2 0.0101 397fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman /// 1.01E-2 4 1 1.01E-2 398fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision = 0, 399fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman unsigned FormatMaxPadding = 3) const; 400fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 4017f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// If this value has an exact multiplicative inverse, store it in inv and 4027f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// return true. 403fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool getExactInverse(APFloat *inv) const; 404fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 405fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanprivate: 406fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 4077f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Simple Queries 4087f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 4097f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 410fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman integerPart *significandParts(); 411fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman const integerPart *significandParts() const; 412fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman unsigned int partCount() const; 413fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 4147f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 4157f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 4167f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Significand operations. 4177f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 4187f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 419fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman integerPart addSignificand(const APFloat &); 420fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman integerPart subtractSignificand(const APFloat &, integerPart); 421fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman lostFraction addOrSubtractSignificand(const APFloat &, bool subtract); 422fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman lostFraction multiplySignificand(const APFloat &, const APFloat *); 423fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman lostFraction divideSignificand(const APFloat &); 424fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void incrementSignificand(); 425fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void initialize(const fltSemantics *); 426fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void shiftSignificandLeft(unsigned int); 427fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman lostFraction shiftSignificandRight(unsigned int); 428fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman unsigned int significandLSB() const; 429fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman unsigned int significandMSB() const; 430fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void zeroSignificand(); 431964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// Return true if the significand excluding the integral bit is all ones. 432964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman bool isSignificandAllOnes() const; 433964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// Return true if the significand excluding the integral bit is all zeros. 434964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman bool isSignificandAllZeros() const; 435fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 4367f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 4377f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 4387f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Arithmetic on special values. 4397f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 4407f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 441fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus addOrSubtractSpecials(const APFloat &, bool subtract); 442fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus divideSpecials(const APFloat &); 443fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus multiplySpecials(const APFloat &); 444fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus modSpecials(const APFloat &); 445fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 4467f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 4477f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 4487f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Special value setters. 4497f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 4507f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 451964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman void makeLargest(bool Neg = false); 452964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman void makeSmallest(bool Neg = false); 453964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman void makeNaN(bool SNaN = false, bool Neg = false, const APInt *fill = 0); 454fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman static APFloat makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative, 455fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman const APInt *fill); 456964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman 4577f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 4587f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 459964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// \name Special value queries only useful internally to APFloat 460964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// @{ 461964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman 462964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// Returns true if and only if the number has the smallest possible non-zero 463964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// magnitude in the current semantics. 464964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman bool isSmallest() const; 465964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// Returns true if and only if the number has the largest possible finite 466964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// magnitude in the current semantics. 467964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman bool isLargest() const; 468964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman 469964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman /// @} 470964722ca40f48c65605e459e3a732bb8783b92f6Michael Gottesman 4717f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// \name Miscellany 4727f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @{ 4737f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 474fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus normalize(roundingMode, lostFraction); 475fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus addOrSubtract(const APFloat &, roundingMode, bool subtract); 476fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman cmpResult compareAbsoluteValue(const APFloat &) const; 477fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus handleOverflow(roundingMode); 478fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman bool roundAwayFromZero(roundingMode, lostFraction, unsigned int) const; 479fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertToSignExtendedInteger(integerPart *, unsigned int, bool, 480fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman roundingMode, bool *) const; 481fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertFromUnsignedParts(const integerPart *, unsigned int, 482fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman roundingMode); 483fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertFromHexadecimalString(StringRef, roundingMode); 484fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus convertFromDecimalString(StringRef, roundingMode); 485fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman char *convertNormalToHexString(char *, unsigned int, bool, 486fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman roundingMode) const; 487fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int, 488fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman roundingMode); 489fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 4907f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// @} 4917f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman 492fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APInt convertHalfAPFloatToAPInt() const; 493fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APInt convertFloatAPFloatToAPInt() const; 494fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APInt convertDoubleAPFloatToAPInt() const; 495fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APInt convertQuadrupleAPFloatToAPInt() const; 496fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APInt convertF80LongDoubleAPFloatToAPInt() const; 497fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman APInt convertPPCDoubleDoubleAPFloatToAPInt() const; 498fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void initFromAPInt(const fltSemantics *Sem, const APInt &api); 499fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void initFromHalfAPInt(const APInt &api); 500fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void initFromFloatAPInt(const APInt &api); 501fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void initFromDoubleAPInt(const APInt &api); 502fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void initFromQuadrupleAPInt(const APInt &api); 503fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void initFromF80LongDoubleAPInt(const APInt &api); 504fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void initFromPPCDoubleDoubleAPInt(const APInt &api); 505fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 506fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void assign(const APFloat &); 507fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void copySignificand(const APFloat &); 508fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman void freeSignificand(); 509fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 5107f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// The semantics that this value obeys. 511fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman const fltSemantics *semantics; 512fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 5137f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// A binary fraction with an explicit integer bit. 5147f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// 5157f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// The significand must be at least one bit wider than the target precision. 516fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman union Significand { 517fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman integerPart part; 518fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman integerPart *parts; 519fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman } significand; 520fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 5217f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// The signed unbiased exponent of the value. 522fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman exponent_t exponent; 523fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 5247f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// What kind of floating point number this is. 5257f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// 5267f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Only 2 bits are required, but VisualStudio incorrectly sign extends it. 5277f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Using the extra bit keeps it from failing under VisualStudio. 528fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman fltCategory category : 3; 529fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 5307f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman /// Sign bit of the number. 531fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman unsigned int sign : 1; 532fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman}; 533fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesman 5347f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// See friend declaration above. 5357f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// 5367f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// This additional declaration is required in order to compile LLVM with IBM 5377f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman/// xlC compiler. 538fce7b6b5d9905bf35641ee7f001b6b66dbc26c2dMichael Gottesmanhash_code hash_value(const APFloat &Arg); 5397f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman} // namespace llvm 540b39cdde41d3c91d1fd48a038e63b78122607bb10Chris Lattner 5417f88a3b20dbaee2a9b265f60a7bc19d2dc6a8f3eMichael Gottesman#endif // LLVM_ADT_APFLOAT_H 542