1*0b57cec5SDimitry Andric //===-- APFloat.cpp - Implement APFloat class -----------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file implements a class to represent arbitrary precision floating 10*0b57cec5SDimitry Andric // point values and provide a variety of arithmetic operations on them. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h" 15*0b57cec5SDimitry Andric #include "llvm/ADT/APSInt.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/FoldingSet.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/Hashing.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 20*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 21*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 22*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 23480093f4SDimitry Andric #include "llvm/Support/Error.h" 24*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 25*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 26*0b57cec5SDimitry Andric #include <cstring> 27*0b57cec5SDimitry Andric #include <limits.h> 28*0b57cec5SDimitry Andric 29*0b57cec5SDimitry Andric #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \ 30*0b57cec5SDimitry Andric do { \ 31*0b57cec5SDimitry Andric if (usesLayout<IEEEFloat>(getSemantics())) \ 32*0b57cec5SDimitry Andric return U.IEEE.METHOD_CALL; \ 33*0b57cec5SDimitry Andric if (usesLayout<DoubleAPFloat>(getSemantics())) \ 34*0b57cec5SDimitry Andric return U.Double.METHOD_CALL; \ 35*0b57cec5SDimitry Andric llvm_unreachable("Unexpected semantics"); \ 36*0b57cec5SDimitry Andric } while (false) 37*0b57cec5SDimitry Andric 38*0b57cec5SDimitry Andric using namespace llvm; 39*0b57cec5SDimitry Andric 40*0b57cec5SDimitry Andric /// A macro used to combine two fcCategory enums into one key which can be used 41*0b57cec5SDimitry Andric /// in a switch statement to classify how the interaction of two APFloat's 42*0b57cec5SDimitry Andric /// categories affects an operation. 43*0b57cec5SDimitry Andric /// 44*0b57cec5SDimitry Andric /// TODO: If clang source code is ever allowed to use constexpr in its own 45*0b57cec5SDimitry Andric /// codebase, change this into a static inline function. 46*0b57cec5SDimitry Andric #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs)) 47*0b57cec5SDimitry Andric 48*0b57cec5SDimitry Andric /* Assumed in hexadecimal significand parsing, and conversion to 49*0b57cec5SDimitry Andric hexadecimal strings. */ 50*0b57cec5SDimitry Andric static_assert(APFloatBase::integerPartWidth % 4 == 0, "Part width must be divisible by 4!"); 51*0b57cec5SDimitry Andric 52*0b57cec5SDimitry Andric namespace llvm { 53bdd1243dSDimitry Andric 54bdd1243dSDimitry Andric // How the nonfinite values Inf and NaN are represented. 55bdd1243dSDimitry Andric enum class fltNonfiniteBehavior { 56bdd1243dSDimitry Andric // Represents standard IEEE 754 behavior. A value is nonfinite if the 57bdd1243dSDimitry Andric // exponent field is all 1s. In such cases, a value is Inf if the 58bdd1243dSDimitry Andric // significand bits are all zero, and NaN otherwise 59bdd1243dSDimitry Andric IEEE754, 60bdd1243dSDimitry Andric 61bdd1243dSDimitry Andric // Only the Float8E5M2 has this behavior. There is no Inf representation. A 62bdd1243dSDimitry Andric // value is NaN if the exponent field and the mantissa field are all 1s. 63bdd1243dSDimitry Andric // This behavior matches the FP8 E4M3 type described in 64bdd1243dSDimitry Andric // https://arxiv.org/abs/2209.05433. We treat both signed and unsigned NaNs 65bdd1243dSDimitry Andric // as non-signalling, although the paper does not state whether the NaN 66bdd1243dSDimitry Andric // values are signalling or not. 67bdd1243dSDimitry Andric NanOnly, 68bdd1243dSDimitry Andric }; 69bdd1243dSDimitry Andric 70*0b57cec5SDimitry Andric /* Represents floating point arithmetic semantics. */ 71*0b57cec5SDimitry Andric struct fltSemantics { 72*0b57cec5SDimitry Andric /* The largest E such that 2^E is representable; this matches the 73*0b57cec5SDimitry Andric definition of IEEE 754. */ 74*0b57cec5SDimitry Andric APFloatBase::ExponentType maxExponent; 75*0b57cec5SDimitry Andric 76*0b57cec5SDimitry Andric /* The smallest E such that 2^E is a normalized number; this 77*0b57cec5SDimitry Andric matches the definition of IEEE 754. */ 78*0b57cec5SDimitry Andric APFloatBase::ExponentType minExponent; 79*0b57cec5SDimitry Andric 80*0b57cec5SDimitry Andric /* Number of bits in the significand. This includes the integer 81*0b57cec5SDimitry Andric bit. */ 82*0b57cec5SDimitry Andric unsigned int precision; 83*0b57cec5SDimitry Andric 84*0b57cec5SDimitry Andric /* Number of bits actually used in the semantics. */ 85*0b57cec5SDimitry Andric unsigned int sizeInBits; 86fe6060f1SDimitry Andric 87bdd1243dSDimitry Andric fltNonfiniteBehavior nonFiniteBehavior = fltNonfiniteBehavior::IEEE754; 88bdd1243dSDimitry Andric 89fe6060f1SDimitry Andric // Returns true if any number described by this semantics can be precisely 90bdd1243dSDimitry Andric // represented by the specified semantics. Does not take into account 91bdd1243dSDimitry Andric // the value of fltNonfiniteBehavior. 92fe6060f1SDimitry Andric bool isRepresentableBy(const fltSemantics &S) const { 93fe6060f1SDimitry Andric return maxExponent <= S.maxExponent && minExponent >= S.minExponent && 94fe6060f1SDimitry Andric precision <= S.precision; 95fe6060f1SDimitry Andric } 96*0b57cec5SDimitry Andric }; 97*0b57cec5SDimitry Andric 98*0b57cec5SDimitry Andric static const fltSemantics semIEEEhalf = {15, -14, 11, 16}; 995ffd83dbSDimitry Andric static const fltSemantics semBFloat = {127, -126, 8, 16}; 100*0b57cec5SDimitry Andric static const fltSemantics semIEEEsingle = {127, -126, 24, 32}; 101*0b57cec5SDimitry Andric static const fltSemantics semIEEEdouble = {1023, -1022, 53, 64}; 102*0b57cec5SDimitry Andric static const fltSemantics semIEEEquad = {16383, -16382, 113, 128}; 103bdd1243dSDimitry Andric static const fltSemantics semFloat8E5M2 = {15, -14, 3, 8}; 104bdd1243dSDimitry Andric static const fltSemantics semFloat8E4M3FN = {8, -6, 4, 8, 105bdd1243dSDimitry Andric fltNonfiniteBehavior::NanOnly}; 106*0b57cec5SDimitry Andric static const fltSemantics semX87DoubleExtended = {16383, -16382, 64, 80}; 107*0b57cec5SDimitry Andric static const fltSemantics semBogus = {0, 0, 0, 0}; 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric /* The IBM double-double semantics. Such a number consists of a pair of IEEE 110*0b57cec5SDimitry Andric 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal, 111*0b57cec5SDimitry Andric (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo. 112*0b57cec5SDimitry Andric Therefore it has two 53-bit mantissa parts that aren't necessarily adjacent 113*0b57cec5SDimitry Andric to each other, and two 11-bit exponents. 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric Note: we need to make the value different from semBogus as otherwise 116*0b57cec5SDimitry Andric an unsafe optimization may collapse both values to a single address, 117*0b57cec5SDimitry Andric and we heavily rely on them having distinct addresses. */ 118349cc55cSDimitry Andric static const fltSemantics semPPCDoubleDouble = {-1, 0, 0, 128}; 119*0b57cec5SDimitry Andric 120*0b57cec5SDimitry Andric /* These are legacy semantics for the fallback, inaccrurate implementation of 121*0b57cec5SDimitry Andric IBM double-double, if the accurate semPPCDoubleDouble doesn't handle the 122*0b57cec5SDimitry Andric operation. It's equivalent to having an IEEE number with consecutive 106 123*0b57cec5SDimitry Andric bits of mantissa and 11 bits of exponent. 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric It's not equivalent to IBM double-double. For example, a legit IBM 126*0b57cec5SDimitry Andric double-double, 1 + epsilon: 127*0b57cec5SDimitry Andric 128*0b57cec5SDimitry Andric 1 + epsilon = 1 + (1 >> 1076) 129*0b57cec5SDimitry Andric 130*0b57cec5SDimitry Andric is not representable by a consecutive 106 bits of mantissa. 131*0b57cec5SDimitry Andric 132*0b57cec5SDimitry Andric Currently, these semantics are used in the following way: 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) -> 135*0b57cec5SDimitry Andric (64-bit APInt, 64-bit APInt) -> (128-bit APInt) -> 136*0b57cec5SDimitry Andric semPPCDoubleDoubleLegacy -> IEEE operations 137*0b57cec5SDimitry Andric 138*0b57cec5SDimitry Andric We use bitcastToAPInt() to get the bit representation (in APInt) of the 139*0b57cec5SDimitry Andric underlying IEEEdouble, then use the APInt constructor to construct the 140*0b57cec5SDimitry Andric legacy IEEE float. 141*0b57cec5SDimitry Andric 142*0b57cec5SDimitry Andric TODO: Implement all operations in semPPCDoubleDouble, and delete these 143*0b57cec5SDimitry Andric semantics. */ 144*0b57cec5SDimitry Andric static const fltSemantics semPPCDoubleDoubleLegacy = {1023, -1022 + 53, 145*0b57cec5SDimitry Andric 53 + 53, 128}; 146*0b57cec5SDimitry Andric 147*0b57cec5SDimitry Andric const llvm::fltSemantics &APFloatBase::EnumToSemantics(Semantics S) { 148*0b57cec5SDimitry Andric switch (S) { 149*0b57cec5SDimitry Andric case S_IEEEhalf: 150*0b57cec5SDimitry Andric return IEEEhalf(); 1515ffd83dbSDimitry Andric case S_BFloat: 1525ffd83dbSDimitry Andric return BFloat(); 153*0b57cec5SDimitry Andric case S_IEEEsingle: 154*0b57cec5SDimitry Andric return IEEEsingle(); 155*0b57cec5SDimitry Andric case S_IEEEdouble: 156*0b57cec5SDimitry Andric return IEEEdouble(); 157*0b57cec5SDimitry Andric case S_IEEEquad: 158*0b57cec5SDimitry Andric return IEEEquad(); 159*0b57cec5SDimitry Andric case S_PPCDoubleDouble: 160*0b57cec5SDimitry Andric return PPCDoubleDouble(); 161bdd1243dSDimitry Andric case S_Float8E5M2: 162bdd1243dSDimitry Andric return Float8E5M2(); 163bdd1243dSDimitry Andric case S_Float8E4M3FN: 164bdd1243dSDimitry Andric return Float8E4M3FN(); 165bdd1243dSDimitry Andric case S_x87DoubleExtended: 166bdd1243dSDimitry Andric return x87DoubleExtended(); 167*0b57cec5SDimitry Andric } 168*0b57cec5SDimitry Andric llvm_unreachable("Unrecognised floating semantics"); 169*0b57cec5SDimitry Andric } 170*0b57cec5SDimitry Andric 171*0b57cec5SDimitry Andric APFloatBase::Semantics 172*0b57cec5SDimitry Andric APFloatBase::SemanticsToEnum(const llvm::fltSemantics &Sem) { 173*0b57cec5SDimitry Andric if (&Sem == &llvm::APFloat::IEEEhalf()) 174*0b57cec5SDimitry Andric return S_IEEEhalf; 1755ffd83dbSDimitry Andric else if (&Sem == &llvm::APFloat::BFloat()) 1765ffd83dbSDimitry Andric return S_BFloat; 177*0b57cec5SDimitry Andric else if (&Sem == &llvm::APFloat::IEEEsingle()) 178*0b57cec5SDimitry Andric return S_IEEEsingle; 179*0b57cec5SDimitry Andric else if (&Sem == &llvm::APFloat::IEEEdouble()) 180*0b57cec5SDimitry Andric return S_IEEEdouble; 181*0b57cec5SDimitry Andric else if (&Sem == &llvm::APFloat::IEEEquad()) 182*0b57cec5SDimitry Andric return S_IEEEquad; 183*0b57cec5SDimitry Andric else if (&Sem == &llvm::APFloat::PPCDoubleDouble()) 184*0b57cec5SDimitry Andric return S_PPCDoubleDouble; 185bdd1243dSDimitry Andric else if (&Sem == &llvm::APFloat::Float8E5M2()) 186bdd1243dSDimitry Andric return S_Float8E5M2; 187bdd1243dSDimitry Andric else if (&Sem == &llvm::APFloat::Float8E4M3FN()) 188bdd1243dSDimitry Andric return S_Float8E4M3FN; 189bdd1243dSDimitry Andric else if (&Sem == &llvm::APFloat::x87DoubleExtended()) 190bdd1243dSDimitry Andric return S_x87DoubleExtended; 191*0b57cec5SDimitry Andric else 192*0b57cec5SDimitry Andric llvm_unreachable("Unknown floating semantics"); 193*0b57cec5SDimitry Andric } 194*0b57cec5SDimitry Andric 195*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::IEEEhalf() { 196*0b57cec5SDimitry Andric return semIEEEhalf; 197*0b57cec5SDimitry Andric } 1985ffd83dbSDimitry Andric const fltSemantics &APFloatBase::BFloat() { 1995ffd83dbSDimitry Andric return semBFloat; 2005ffd83dbSDimitry Andric } 201*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::IEEEsingle() { 202*0b57cec5SDimitry Andric return semIEEEsingle; 203*0b57cec5SDimitry Andric } 204*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::IEEEdouble() { 205*0b57cec5SDimitry Andric return semIEEEdouble; 206*0b57cec5SDimitry Andric } 207bdd1243dSDimitry Andric const fltSemantics &APFloatBase::IEEEquad() { return semIEEEquad; } 208*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::PPCDoubleDouble() { 209*0b57cec5SDimitry Andric return semPPCDoubleDouble; 210*0b57cec5SDimitry Andric } 211bdd1243dSDimitry Andric const fltSemantics &APFloatBase::Float8E5M2() { return semFloat8E5M2; } 212bdd1243dSDimitry Andric const fltSemantics &APFloatBase::Float8E4M3FN() { return semFloat8E4M3FN; } 213bdd1243dSDimitry Andric const fltSemantics &APFloatBase::x87DoubleExtended() { 214bdd1243dSDimitry Andric return semX87DoubleExtended; 215bdd1243dSDimitry Andric } 216bdd1243dSDimitry Andric const fltSemantics &APFloatBase::Bogus() { return semBogus; } 217*0b57cec5SDimitry Andric 2185ffd83dbSDimitry Andric constexpr RoundingMode APFloatBase::rmNearestTiesToEven; 2195ffd83dbSDimitry Andric constexpr RoundingMode APFloatBase::rmTowardPositive; 2205ffd83dbSDimitry Andric constexpr RoundingMode APFloatBase::rmTowardNegative; 2215ffd83dbSDimitry Andric constexpr RoundingMode APFloatBase::rmTowardZero; 2225ffd83dbSDimitry Andric constexpr RoundingMode APFloatBase::rmNearestTiesToAway; 2235ffd83dbSDimitry Andric 224*0b57cec5SDimitry Andric /* A tight upper bound on number of parts required to hold the value 225*0b57cec5SDimitry Andric pow(5, power) is 226*0b57cec5SDimitry Andric 227*0b57cec5SDimitry Andric power * 815 / (351 * integerPartWidth) + 1 228*0b57cec5SDimitry Andric 229*0b57cec5SDimitry Andric However, whilst the result may require only this many parts, 230*0b57cec5SDimitry Andric because we are multiplying two values to get it, the 231*0b57cec5SDimitry Andric multiplication may require an extra part with the excess part 232*0b57cec5SDimitry Andric being zero (consider the trivial case of 1 * 1, tcFullMultiply 233*0b57cec5SDimitry Andric requires two parts to hold the single-part result). So we add an 234*0b57cec5SDimitry Andric extra one to guarantee enough space whilst multiplying. */ 235*0b57cec5SDimitry Andric const unsigned int maxExponent = 16383; 236*0b57cec5SDimitry Andric const unsigned int maxPrecision = 113; 237*0b57cec5SDimitry Andric const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1; 238*0b57cec5SDimitry Andric const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815) / (351 * APFloatBase::integerPartWidth)); 239*0b57cec5SDimitry Andric 240*0b57cec5SDimitry Andric unsigned int APFloatBase::semanticsPrecision(const fltSemantics &semantics) { 241*0b57cec5SDimitry Andric return semantics.precision; 242*0b57cec5SDimitry Andric } 243*0b57cec5SDimitry Andric APFloatBase::ExponentType 244*0b57cec5SDimitry Andric APFloatBase::semanticsMaxExponent(const fltSemantics &semantics) { 245*0b57cec5SDimitry Andric return semantics.maxExponent; 246*0b57cec5SDimitry Andric } 247*0b57cec5SDimitry Andric APFloatBase::ExponentType 248*0b57cec5SDimitry Andric APFloatBase::semanticsMinExponent(const fltSemantics &semantics) { 249*0b57cec5SDimitry Andric return semantics.minExponent; 250*0b57cec5SDimitry Andric } 251*0b57cec5SDimitry Andric unsigned int APFloatBase::semanticsSizeInBits(const fltSemantics &semantics) { 252*0b57cec5SDimitry Andric return semantics.sizeInBits; 253*0b57cec5SDimitry Andric } 254*0b57cec5SDimitry Andric 255*0b57cec5SDimitry Andric unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) { 256*0b57cec5SDimitry Andric return Sem.sizeInBits; 257*0b57cec5SDimitry Andric } 258*0b57cec5SDimitry Andric 259*0b57cec5SDimitry Andric /* A bunch of private, handy routines. */ 260*0b57cec5SDimitry Andric 261480093f4SDimitry Andric static inline Error createError(const Twine &Err) { 262480093f4SDimitry Andric return make_error<StringError>(Err, inconvertibleErrorCode()); 263480093f4SDimitry Andric } 264480093f4SDimitry Andric 265*0b57cec5SDimitry Andric static inline unsigned int 266*0b57cec5SDimitry Andric partCountForBits(unsigned int bits) 267*0b57cec5SDimitry Andric { 268*0b57cec5SDimitry Andric return ((bits) + APFloatBase::integerPartWidth - 1) / APFloatBase::integerPartWidth; 269*0b57cec5SDimitry Andric } 270*0b57cec5SDimitry Andric 271*0b57cec5SDimitry Andric /* Returns 0U-9U. Return values >= 10U are not digits. */ 272*0b57cec5SDimitry Andric static inline unsigned int 273*0b57cec5SDimitry Andric decDigitValue(unsigned int c) 274*0b57cec5SDimitry Andric { 275*0b57cec5SDimitry Andric return c - '0'; 276*0b57cec5SDimitry Andric } 277*0b57cec5SDimitry Andric 278*0b57cec5SDimitry Andric /* Return the value of a decimal exponent of the form 279*0b57cec5SDimitry Andric [+-]ddddddd. 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric If the exponent overflows, returns a large exponent with the 282*0b57cec5SDimitry Andric appropriate sign. */ 283480093f4SDimitry Andric static Expected<int> readExponent(StringRef::iterator begin, 284480093f4SDimitry Andric StringRef::iterator end) { 285*0b57cec5SDimitry Andric bool isNegative; 286*0b57cec5SDimitry Andric unsigned int absExponent; 287*0b57cec5SDimitry Andric const unsigned int overlargeExponent = 24000; /* FIXME. */ 288*0b57cec5SDimitry Andric StringRef::iterator p = begin; 289*0b57cec5SDimitry Andric 290*0b57cec5SDimitry Andric // Treat no exponent as 0 to match binutils 291*0b57cec5SDimitry Andric if (p == end || ((*p == '-' || *p == '+') && (p + 1) == end)) { 292*0b57cec5SDimitry Andric return 0; 293*0b57cec5SDimitry Andric } 294*0b57cec5SDimitry Andric 295*0b57cec5SDimitry Andric isNegative = (*p == '-'); 296*0b57cec5SDimitry Andric if (*p == '-' || *p == '+') { 297*0b57cec5SDimitry Andric p++; 298480093f4SDimitry Andric if (p == end) 299480093f4SDimitry Andric return createError("Exponent has no digits"); 300*0b57cec5SDimitry Andric } 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric absExponent = decDigitValue(*p++); 303480093f4SDimitry Andric if (absExponent >= 10U) 304480093f4SDimitry Andric return createError("Invalid character in exponent"); 305*0b57cec5SDimitry Andric 306*0b57cec5SDimitry Andric for (; p != end; ++p) { 307*0b57cec5SDimitry Andric unsigned int value; 308*0b57cec5SDimitry Andric 309*0b57cec5SDimitry Andric value = decDigitValue(*p); 310480093f4SDimitry Andric if (value >= 10U) 311480093f4SDimitry Andric return createError("Invalid character in exponent"); 312*0b57cec5SDimitry Andric 313480093f4SDimitry Andric absExponent = absExponent * 10U + value; 314*0b57cec5SDimitry Andric if (absExponent >= overlargeExponent) { 315*0b57cec5SDimitry Andric absExponent = overlargeExponent; 316*0b57cec5SDimitry Andric break; 317*0b57cec5SDimitry Andric } 318*0b57cec5SDimitry Andric } 319*0b57cec5SDimitry Andric 320*0b57cec5SDimitry Andric if (isNegative) 321*0b57cec5SDimitry Andric return -(int) absExponent; 322*0b57cec5SDimitry Andric else 323*0b57cec5SDimitry Andric return (int) absExponent; 324*0b57cec5SDimitry Andric } 325*0b57cec5SDimitry Andric 326*0b57cec5SDimitry Andric /* This is ugly and needs cleaning up, but I don't immediately see 327*0b57cec5SDimitry Andric how whilst remaining safe. */ 328480093f4SDimitry Andric static Expected<int> totalExponent(StringRef::iterator p, 329480093f4SDimitry Andric StringRef::iterator end, 330480093f4SDimitry Andric int exponentAdjustment) { 331*0b57cec5SDimitry Andric int unsignedExponent; 332*0b57cec5SDimitry Andric bool negative, overflow; 333*0b57cec5SDimitry Andric int exponent = 0; 334*0b57cec5SDimitry Andric 335480093f4SDimitry Andric if (p == end) 336480093f4SDimitry Andric return createError("Exponent has no digits"); 337*0b57cec5SDimitry Andric 338*0b57cec5SDimitry Andric negative = *p == '-'; 339*0b57cec5SDimitry Andric if (*p == '-' || *p == '+') { 340*0b57cec5SDimitry Andric p++; 341480093f4SDimitry Andric if (p == end) 342480093f4SDimitry Andric return createError("Exponent has no digits"); 343*0b57cec5SDimitry Andric } 344*0b57cec5SDimitry Andric 345*0b57cec5SDimitry Andric unsignedExponent = 0; 346*0b57cec5SDimitry Andric overflow = false; 347*0b57cec5SDimitry Andric for (; p != end; ++p) { 348*0b57cec5SDimitry Andric unsigned int value; 349*0b57cec5SDimitry Andric 350*0b57cec5SDimitry Andric value = decDigitValue(*p); 351480093f4SDimitry Andric if (value >= 10U) 352480093f4SDimitry Andric return createError("Invalid character in exponent"); 353*0b57cec5SDimitry Andric 354*0b57cec5SDimitry Andric unsignedExponent = unsignedExponent * 10 + value; 355*0b57cec5SDimitry Andric if (unsignedExponent > 32767) { 356*0b57cec5SDimitry Andric overflow = true; 357*0b57cec5SDimitry Andric break; 358*0b57cec5SDimitry Andric } 359*0b57cec5SDimitry Andric } 360*0b57cec5SDimitry Andric 361*0b57cec5SDimitry Andric if (exponentAdjustment > 32767 || exponentAdjustment < -32768) 362*0b57cec5SDimitry Andric overflow = true; 363*0b57cec5SDimitry Andric 364*0b57cec5SDimitry Andric if (!overflow) { 365*0b57cec5SDimitry Andric exponent = unsignedExponent; 366*0b57cec5SDimitry Andric if (negative) 367*0b57cec5SDimitry Andric exponent = -exponent; 368*0b57cec5SDimitry Andric exponent += exponentAdjustment; 369*0b57cec5SDimitry Andric if (exponent > 32767 || exponent < -32768) 370*0b57cec5SDimitry Andric overflow = true; 371*0b57cec5SDimitry Andric } 372*0b57cec5SDimitry Andric 373*0b57cec5SDimitry Andric if (overflow) 374*0b57cec5SDimitry Andric exponent = negative ? -32768: 32767; 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric return exponent; 377*0b57cec5SDimitry Andric } 378*0b57cec5SDimitry Andric 379480093f4SDimitry Andric static Expected<StringRef::iterator> 380*0b57cec5SDimitry Andric skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, 381480093f4SDimitry Andric StringRef::iterator *dot) { 382*0b57cec5SDimitry Andric StringRef::iterator p = begin; 383*0b57cec5SDimitry Andric *dot = end; 384*0b57cec5SDimitry Andric while (p != end && *p == '0') 385*0b57cec5SDimitry Andric p++; 386*0b57cec5SDimitry Andric 387*0b57cec5SDimitry Andric if (p != end && *p == '.') { 388*0b57cec5SDimitry Andric *dot = p++; 389*0b57cec5SDimitry Andric 390480093f4SDimitry Andric if (end - begin == 1) 391480093f4SDimitry Andric return createError("Significand has no digits"); 392*0b57cec5SDimitry Andric 393*0b57cec5SDimitry Andric while (p != end && *p == '0') 394*0b57cec5SDimitry Andric p++; 395*0b57cec5SDimitry Andric } 396*0b57cec5SDimitry Andric 397*0b57cec5SDimitry Andric return p; 398*0b57cec5SDimitry Andric } 399*0b57cec5SDimitry Andric 400*0b57cec5SDimitry Andric /* Given a normal decimal floating point number of the form 401*0b57cec5SDimitry Andric 402*0b57cec5SDimitry Andric dddd.dddd[eE][+-]ddd 403*0b57cec5SDimitry Andric 404*0b57cec5SDimitry Andric where the decimal point and exponent are optional, fill out the 405*0b57cec5SDimitry Andric structure D. Exponent is appropriate if the significand is 406*0b57cec5SDimitry Andric treated as an integer, and normalizedExponent if the significand 407*0b57cec5SDimitry Andric is taken to have the decimal point after a single leading 408*0b57cec5SDimitry Andric non-zero digit. 409*0b57cec5SDimitry Andric 410*0b57cec5SDimitry Andric If the value is zero, V->firstSigDigit points to a non-digit, and 411*0b57cec5SDimitry Andric the return exponent is zero. 412*0b57cec5SDimitry Andric */ 413*0b57cec5SDimitry Andric struct decimalInfo { 414*0b57cec5SDimitry Andric const char *firstSigDigit; 415*0b57cec5SDimitry Andric const char *lastSigDigit; 416*0b57cec5SDimitry Andric int exponent; 417*0b57cec5SDimitry Andric int normalizedExponent; 418*0b57cec5SDimitry Andric }; 419*0b57cec5SDimitry Andric 420480093f4SDimitry Andric static Error interpretDecimal(StringRef::iterator begin, 421480093f4SDimitry Andric StringRef::iterator end, decimalInfo *D) { 422*0b57cec5SDimitry Andric StringRef::iterator dot = end; 423480093f4SDimitry Andric 424480093f4SDimitry Andric auto PtrOrErr = skipLeadingZeroesAndAnyDot(begin, end, &dot); 425480093f4SDimitry Andric if (!PtrOrErr) 426480093f4SDimitry Andric return PtrOrErr.takeError(); 427480093f4SDimitry Andric StringRef::iterator p = *PtrOrErr; 428*0b57cec5SDimitry Andric 429*0b57cec5SDimitry Andric D->firstSigDigit = p; 430*0b57cec5SDimitry Andric D->exponent = 0; 431*0b57cec5SDimitry Andric D->normalizedExponent = 0; 432*0b57cec5SDimitry Andric 433*0b57cec5SDimitry Andric for (; p != end; ++p) { 434*0b57cec5SDimitry Andric if (*p == '.') { 435480093f4SDimitry Andric if (dot != end) 436480093f4SDimitry Andric return createError("String contains multiple dots"); 437*0b57cec5SDimitry Andric dot = p++; 438*0b57cec5SDimitry Andric if (p == end) 439*0b57cec5SDimitry Andric break; 440*0b57cec5SDimitry Andric } 441*0b57cec5SDimitry Andric if (decDigitValue(*p) >= 10U) 442*0b57cec5SDimitry Andric break; 443*0b57cec5SDimitry Andric } 444*0b57cec5SDimitry Andric 445*0b57cec5SDimitry Andric if (p != end) { 446480093f4SDimitry Andric if (*p != 'e' && *p != 'E') 447480093f4SDimitry Andric return createError("Invalid character in significand"); 448480093f4SDimitry Andric if (p == begin) 449480093f4SDimitry Andric return createError("Significand has no digits"); 450480093f4SDimitry Andric if (dot != end && p - begin == 1) 451480093f4SDimitry Andric return createError("Significand has no digits"); 452*0b57cec5SDimitry Andric 453*0b57cec5SDimitry Andric /* p points to the first non-digit in the string */ 454480093f4SDimitry Andric auto ExpOrErr = readExponent(p + 1, end); 455480093f4SDimitry Andric if (!ExpOrErr) 456480093f4SDimitry Andric return ExpOrErr.takeError(); 457480093f4SDimitry Andric D->exponent = *ExpOrErr; 458*0b57cec5SDimitry Andric 459*0b57cec5SDimitry Andric /* Implied decimal point? */ 460*0b57cec5SDimitry Andric if (dot == end) 461*0b57cec5SDimitry Andric dot = p; 462*0b57cec5SDimitry Andric } 463*0b57cec5SDimitry Andric 464*0b57cec5SDimitry Andric /* If number is all zeroes accept any exponent. */ 465*0b57cec5SDimitry Andric if (p != D->firstSigDigit) { 466*0b57cec5SDimitry Andric /* Drop insignificant trailing zeroes. */ 467*0b57cec5SDimitry Andric if (p != begin) { 468*0b57cec5SDimitry Andric do 469*0b57cec5SDimitry Andric do 470*0b57cec5SDimitry Andric p--; 471*0b57cec5SDimitry Andric while (p != begin && *p == '0'); 472*0b57cec5SDimitry Andric while (p != begin && *p == '.'); 473*0b57cec5SDimitry Andric } 474*0b57cec5SDimitry Andric 475*0b57cec5SDimitry Andric /* Adjust the exponents for any decimal point. */ 476*0b57cec5SDimitry Andric D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p)); 477*0b57cec5SDimitry Andric D->normalizedExponent = (D->exponent + 478*0b57cec5SDimitry Andric static_cast<APFloat::ExponentType>((p - D->firstSigDigit) 479*0b57cec5SDimitry Andric - (dot > D->firstSigDigit && dot < p))); 480*0b57cec5SDimitry Andric } 481*0b57cec5SDimitry Andric 482*0b57cec5SDimitry Andric D->lastSigDigit = p; 483480093f4SDimitry Andric return Error::success(); 484*0b57cec5SDimitry Andric } 485*0b57cec5SDimitry Andric 486*0b57cec5SDimitry Andric /* Return the trailing fraction of a hexadecimal number. 487*0b57cec5SDimitry Andric DIGITVALUE is the first hex digit of the fraction, P points to 488*0b57cec5SDimitry Andric the next digit. */ 489480093f4SDimitry Andric static Expected<lostFraction> 490*0b57cec5SDimitry Andric trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, 491480093f4SDimitry Andric unsigned int digitValue) { 492*0b57cec5SDimitry Andric unsigned int hexDigit; 493*0b57cec5SDimitry Andric 494*0b57cec5SDimitry Andric /* If the first trailing digit isn't 0 or 8 we can work out the 495*0b57cec5SDimitry Andric fraction immediately. */ 496*0b57cec5SDimitry Andric if (digitValue > 8) 497*0b57cec5SDimitry Andric return lfMoreThanHalf; 498*0b57cec5SDimitry Andric else if (digitValue < 8 && digitValue > 0) 499*0b57cec5SDimitry Andric return lfLessThanHalf; 500*0b57cec5SDimitry Andric 501*0b57cec5SDimitry Andric // Otherwise we need to find the first non-zero digit. 502*0b57cec5SDimitry Andric while (p != end && (*p == '0' || *p == '.')) 503*0b57cec5SDimitry Andric p++; 504*0b57cec5SDimitry Andric 505480093f4SDimitry Andric if (p == end) 506480093f4SDimitry Andric return createError("Invalid trailing hexadecimal fraction!"); 507*0b57cec5SDimitry Andric 508*0b57cec5SDimitry Andric hexDigit = hexDigitValue(*p); 509*0b57cec5SDimitry Andric 510*0b57cec5SDimitry Andric /* If we ran off the end it is exactly zero or one-half, otherwise 511*0b57cec5SDimitry Andric a little more. */ 512*0b57cec5SDimitry Andric if (hexDigit == -1U) 513*0b57cec5SDimitry Andric return digitValue == 0 ? lfExactlyZero: lfExactlyHalf; 514*0b57cec5SDimitry Andric else 515*0b57cec5SDimitry Andric return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf; 516*0b57cec5SDimitry Andric } 517*0b57cec5SDimitry Andric 518*0b57cec5SDimitry Andric /* Return the fraction lost were a bignum truncated losing the least 519*0b57cec5SDimitry Andric significant BITS bits. */ 520*0b57cec5SDimitry Andric static lostFraction 521*0b57cec5SDimitry Andric lostFractionThroughTruncation(const APFloatBase::integerPart *parts, 522*0b57cec5SDimitry Andric unsigned int partCount, 523*0b57cec5SDimitry Andric unsigned int bits) 524*0b57cec5SDimitry Andric { 525*0b57cec5SDimitry Andric unsigned int lsb; 526*0b57cec5SDimitry Andric 527*0b57cec5SDimitry Andric lsb = APInt::tcLSB(parts, partCount); 528*0b57cec5SDimitry Andric 529*0b57cec5SDimitry Andric /* Note this is guaranteed true if bits == 0, or LSB == -1U. */ 530*0b57cec5SDimitry Andric if (bits <= lsb) 531*0b57cec5SDimitry Andric return lfExactlyZero; 532*0b57cec5SDimitry Andric if (bits == lsb + 1) 533*0b57cec5SDimitry Andric return lfExactlyHalf; 534*0b57cec5SDimitry Andric if (bits <= partCount * APFloatBase::integerPartWidth && 535*0b57cec5SDimitry Andric APInt::tcExtractBit(parts, bits - 1)) 536*0b57cec5SDimitry Andric return lfMoreThanHalf; 537*0b57cec5SDimitry Andric 538*0b57cec5SDimitry Andric return lfLessThanHalf; 539*0b57cec5SDimitry Andric } 540*0b57cec5SDimitry Andric 541*0b57cec5SDimitry Andric /* Shift DST right BITS bits noting lost fraction. */ 542*0b57cec5SDimitry Andric static lostFraction 543*0b57cec5SDimitry Andric shiftRight(APFloatBase::integerPart *dst, unsigned int parts, unsigned int bits) 544*0b57cec5SDimitry Andric { 545*0b57cec5SDimitry Andric lostFraction lost_fraction; 546*0b57cec5SDimitry Andric 547*0b57cec5SDimitry Andric lost_fraction = lostFractionThroughTruncation(dst, parts, bits); 548*0b57cec5SDimitry Andric 549*0b57cec5SDimitry Andric APInt::tcShiftRight(dst, parts, bits); 550*0b57cec5SDimitry Andric 551*0b57cec5SDimitry Andric return lost_fraction; 552*0b57cec5SDimitry Andric } 553*0b57cec5SDimitry Andric 554*0b57cec5SDimitry Andric /* Combine the effect of two lost fractions. */ 555*0b57cec5SDimitry Andric static lostFraction 556*0b57cec5SDimitry Andric combineLostFractions(lostFraction moreSignificant, 557*0b57cec5SDimitry Andric lostFraction lessSignificant) 558*0b57cec5SDimitry Andric { 559*0b57cec5SDimitry Andric if (lessSignificant != lfExactlyZero) { 560*0b57cec5SDimitry Andric if (moreSignificant == lfExactlyZero) 561*0b57cec5SDimitry Andric moreSignificant = lfLessThanHalf; 562*0b57cec5SDimitry Andric else if (moreSignificant == lfExactlyHalf) 563*0b57cec5SDimitry Andric moreSignificant = lfMoreThanHalf; 564*0b57cec5SDimitry Andric } 565*0b57cec5SDimitry Andric 566*0b57cec5SDimitry Andric return moreSignificant; 567*0b57cec5SDimitry Andric } 568*0b57cec5SDimitry Andric 569*0b57cec5SDimitry Andric /* The error from the true value, in half-ulps, on multiplying two 570*0b57cec5SDimitry Andric floating point numbers, which differ from the value they 571*0b57cec5SDimitry Andric approximate by at most HUE1 and HUE2 half-ulps, is strictly less 572*0b57cec5SDimitry Andric than the returned value. 573*0b57cec5SDimitry Andric 574*0b57cec5SDimitry Andric See "How to Read Floating Point Numbers Accurately" by William D 575*0b57cec5SDimitry Andric Clinger. */ 576*0b57cec5SDimitry Andric static unsigned int 577*0b57cec5SDimitry Andric HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2) 578*0b57cec5SDimitry Andric { 579*0b57cec5SDimitry Andric assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8)); 580*0b57cec5SDimitry Andric 581*0b57cec5SDimitry Andric if (HUerr1 + HUerr2 == 0) 582*0b57cec5SDimitry Andric return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */ 583*0b57cec5SDimitry Andric else 584*0b57cec5SDimitry Andric return inexactMultiply + 2 * (HUerr1 + HUerr2); 585*0b57cec5SDimitry Andric } 586*0b57cec5SDimitry Andric 587*0b57cec5SDimitry Andric /* The number of ulps from the boundary (zero, or half if ISNEAREST) 588*0b57cec5SDimitry Andric when the least significant BITS are truncated. BITS cannot be 589*0b57cec5SDimitry Andric zero. */ 590*0b57cec5SDimitry Andric static APFloatBase::integerPart 591*0b57cec5SDimitry Andric ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits, 592*0b57cec5SDimitry Andric bool isNearest) { 593*0b57cec5SDimitry Andric unsigned int count, partBits; 594*0b57cec5SDimitry Andric APFloatBase::integerPart part, boundary; 595*0b57cec5SDimitry Andric 596*0b57cec5SDimitry Andric assert(bits != 0); 597*0b57cec5SDimitry Andric 598*0b57cec5SDimitry Andric bits--; 599*0b57cec5SDimitry Andric count = bits / APFloatBase::integerPartWidth; 600*0b57cec5SDimitry Andric partBits = bits % APFloatBase::integerPartWidth + 1; 601*0b57cec5SDimitry Andric 602*0b57cec5SDimitry Andric part = parts[count] & (~(APFloatBase::integerPart) 0 >> (APFloatBase::integerPartWidth - partBits)); 603*0b57cec5SDimitry Andric 604*0b57cec5SDimitry Andric if (isNearest) 605*0b57cec5SDimitry Andric boundary = (APFloatBase::integerPart) 1 << (partBits - 1); 606*0b57cec5SDimitry Andric else 607*0b57cec5SDimitry Andric boundary = 0; 608*0b57cec5SDimitry Andric 609*0b57cec5SDimitry Andric if (count == 0) { 610*0b57cec5SDimitry Andric if (part - boundary <= boundary - part) 611*0b57cec5SDimitry Andric return part - boundary; 612*0b57cec5SDimitry Andric else 613*0b57cec5SDimitry Andric return boundary - part; 614*0b57cec5SDimitry Andric } 615*0b57cec5SDimitry Andric 616*0b57cec5SDimitry Andric if (part == boundary) { 617*0b57cec5SDimitry Andric while (--count) 618*0b57cec5SDimitry Andric if (parts[count]) 619*0b57cec5SDimitry Andric return ~(APFloatBase::integerPart) 0; /* A lot. */ 620*0b57cec5SDimitry Andric 621*0b57cec5SDimitry Andric return parts[0]; 622*0b57cec5SDimitry Andric } else if (part == boundary - 1) { 623*0b57cec5SDimitry Andric while (--count) 624*0b57cec5SDimitry Andric if (~parts[count]) 625*0b57cec5SDimitry Andric return ~(APFloatBase::integerPart) 0; /* A lot. */ 626*0b57cec5SDimitry Andric 627*0b57cec5SDimitry Andric return -parts[0]; 628*0b57cec5SDimitry Andric } 629*0b57cec5SDimitry Andric 630*0b57cec5SDimitry Andric return ~(APFloatBase::integerPart) 0; /* A lot. */ 631*0b57cec5SDimitry Andric } 632*0b57cec5SDimitry Andric 633*0b57cec5SDimitry Andric /* Place pow(5, power) in DST, and return the number of parts used. 634*0b57cec5SDimitry Andric DST must be at least one part larger than size of the answer. */ 635*0b57cec5SDimitry Andric static unsigned int 636*0b57cec5SDimitry Andric powerOf5(APFloatBase::integerPart *dst, unsigned int power) { 637*0b57cec5SDimitry Andric static const APFloatBase::integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125, 15625, 78125 }; 638*0b57cec5SDimitry Andric APFloatBase::integerPart pow5s[maxPowerOfFiveParts * 2 + 5]; 639*0b57cec5SDimitry Andric pow5s[0] = 78125 * 5; 640*0b57cec5SDimitry Andric 641*0b57cec5SDimitry Andric unsigned int partsCount[16] = { 1 }; 642*0b57cec5SDimitry Andric APFloatBase::integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5; 643*0b57cec5SDimitry Andric unsigned int result; 644*0b57cec5SDimitry Andric assert(power <= maxExponent); 645*0b57cec5SDimitry Andric 646*0b57cec5SDimitry Andric p1 = dst; 647*0b57cec5SDimitry Andric p2 = scratch; 648*0b57cec5SDimitry Andric 649*0b57cec5SDimitry Andric *p1 = firstEightPowers[power & 7]; 650*0b57cec5SDimitry Andric power >>= 3; 651*0b57cec5SDimitry Andric 652*0b57cec5SDimitry Andric result = 1; 653*0b57cec5SDimitry Andric pow5 = pow5s; 654*0b57cec5SDimitry Andric 655*0b57cec5SDimitry Andric for (unsigned int n = 0; power; power >>= 1, n++) { 656*0b57cec5SDimitry Andric unsigned int pc; 657*0b57cec5SDimitry Andric 658*0b57cec5SDimitry Andric pc = partsCount[n]; 659*0b57cec5SDimitry Andric 660*0b57cec5SDimitry Andric /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */ 661*0b57cec5SDimitry Andric if (pc == 0) { 662*0b57cec5SDimitry Andric pc = partsCount[n - 1]; 663*0b57cec5SDimitry Andric APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc); 664*0b57cec5SDimitry Andric pc *= 2; 665*0b57cec5SDimitry Andric if (pow5[pc - 1] == 0) 666*0b57cec5SDimitry Andric pc--; 667*0b57cec5SDimitry Andric partsCount[n] = pc; 668*0b57cec5SDimitry Andric } 669*0b57cec5SDimitry Andric 670*0b57cec5SDimitry Andric if (power & 1) { 671*0b57cec5SDimitry Andric APFloatBase::integerPart *tmp; 672*0b57cec5SDimitry Andric 673*0b57cec5SDimitry Andric APInt::tcFullMultiply(p2, p1, pow5, result, pc); 674*0b57cec5SDimitry Andric result += pc; 675*0b57cec5SDimitry Andric if (p2[result - 1] == 0) 676*0b57cec5SDimitry Andric result--; 677*0b57cec5SDimitry Andric 678*0b57cec5SDimitry Andric /* Now result is in p1 with partsCount parts and p2 is scratch 679*0b57cec5SDimitry Andric space. */ 680*0b57cec5SDimitry Andric tmp = p1; 681*0b57cec5SDimitry Andric p1 = p2; 682*0b57cec5SDimitry Andric p2 = tmp; 683*0b57cec5SDimitry Andric } 684*0b57cec5SDimitry Andric 685*0b57cec5SDimitry Andric pow5 += pc; 686*0b57cec5SDimitry Andric } 687*0b57cec5SDimitry Andric 688*0b57cec5SDimitry Andric if (p1 != dst) 689*0b57cec5SDimitry Andric APInt::tcAssign(dst, p1, result); 690*0b57cec5SDimitry Andric 691*0b57cec5SDimitry Andric return result; 692*0b57cec5SDimitry Andric } 693*0b57cec5SDimitry Andric 694*0b57cec5SDimitry Andric /* Zero at the end to avoid modular arithmetic when adding one; used 695*0b57cec5SDimitry Andric when rounding up during hexadecimal output. */ 696*0b57cec5SDimitry Andric static const char hexDigitsLower[] = "0123456789abcdef0"; 697*0b57cec5SDimitry Andric static const char hexDigitsUpper[] = "0123456789ABCDEF0"; 698*0b57cec5SDimitry Andric static const char infinityL[] = "infinity"; 699*0b57cec5SDimitry Andric static const char infinityU[] = "INFINITY"; 700*0b57cec5SDimitry Andric static const char NaNL[] = "nan"; 701*0b57cec5SDimitry Andric static const char NaNU[] = "NAN"; 702*0b57cec5SDimitry Andric 703*0b57cec5SDimitry Andric /* Write out an integerPart in hexadecimal, starting with the most 704*0b57cec5SDimitry Andric significant nibble. Write out exactly COUNT hexdigits, return 705*0b57cec5SDimitry Andric COUNT. */ 706*0b57cec5SDimitry Andric static unsigned int 707*0b57cec5SDimitry Andric partAsHex (char *dst, APFloatBase::integerPart part, unsigned int count, 708*0b57cec5SDimitry Andric const char *hexDigitChars) 709*0b57cec5SDimitry Andric { 710*0b57cec5SDimitry Andric unsigned int result = count; 711*0b57cec5SDimitry Andric 712*0b57cec5SDimitry Andric assert(count != 0 && count <= APFloatBase::integerPartWidth / 4); 713*0b57cec5SDimitry Andric 714*0b57cec5SDimitry Andric part >>= (APFloatBase::integerPartWidth - 4 * count); 715*0b57cec5SDimitry Andric while (count--) { 716*0b57cec5SDimitry Andric dst[count] = hexDigitChars[part & 0xf]; 717*0b57cec5SDimitry Andric part >>= 4; 718*0b57cec5SDimitry Andric } 719*0b57cec5SDimitry Andric 720*0b57cec5SDimitry Andric return result; 721*0b57cec5SDimitry Andric } 722*0b57cec5SDimitry Andric 723*0b57cec5SDimitry Andric /* Write out an unsigned decimal integer. */ 724*0b57cec5SDimitry Andric static char * 725*0b57cec5SDimitry Andric writeUnsignedDecimal (char *dst, unsigned int n) 726*0b57cec5SDimitry Andric { 727*0b57cec5SDimitry Andric char buff[40], *p; 728*0b57cec5SDimitry Andric 729*0b57cec5SDimitry Andric p = buff; 730*0b57cec5SDimitry Andric do 731*0b57cec5SDimitry Andric *p++ = '0' + n % 10; 732*0b57cec5SDimitry Andric while (n /= 10); 733*0b57cec5SDimitry Andric 734*0b57cec5SDimitry Andric do 735*0b57cec5SDimitry Andric *dst++ = *--p; 736*0b57cec5SDimitry Andric while (p != buff); 737*0b57cec5SDimitry Andric 738*0b57cec5SDimitry Andric return dst; 739*0b57cec5SDimitry Andric } 740*0b57cec5SDimitry Andric 741*0b57cec5SDimitry Andric /* Write out a signed decimal integer. */ 742*0b57cec5SDimitry Andric static char * 743*0b57cec5SDimitry Andric writeSignedDecimal (char *dst, int value) 744*0b57cec5SDimitry Andric { 745*0b57cec5SDimitry Andric if (value < 0) { 746*0b57cec5SDimitry Andric *dst++ = '-'; 747*0b57cec5SDimitry Andric dst = writeUnsignedDecimal(dst, -(unsigned) value); 748*0b57cec5SDimitry Andric } else 749*0b57cec5SDimitry Andric dst = writeUnsignedDecimal(dst, value); 750*0b57cec5SDimitry Andric 751*0b57cec5SDimitry Andric return dst; 752*0b57cec5SDimitry Andric } 753*0b57cec5SDimitry Andric 754*0b57cec5SDimitry Andric namespace detail { 755*0b57cec5SDimitry Andric /* Constructors. */ 756*0b57cec5SDimitry Andric void IEEEFloat::initialize(const fltSemantics *ourSemantics) { 757*0b57cec5SDimitry Andric unsigned int count; 758*0b57cec5SDimitry Andric 759*0b57cec5SDimitry Andric semantics = ourSemantics; 760*0b57cec5SDimitry Andric count = partCount(); 761*0b57cec5SDimitry Andric if (count > 1) 762*0b57cec5SDimitry Andric significand.parts = new integerPart[count]; 763*0b57cec5SDimitry Andric } 764*0b57cec5SDimitry Andric 765*0b57cec5SDimitry Andric void IEEEFloat::freeSignificand() { 766*0b57cec5SDimitry Andric if (needsCleanup()) 767*0b57cec5SDimitry Andric delete [] significand.parts; 768*0b57cec5SDimitry Andric } 769*0b57cec5SDimitry Andric 770*0b57cec5SDimitry Andric void IEEEFloat::assign(const IEEEFloat &rhs) { 771*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 772*0b57cec5SDimitry Andric 773*0b57cec5SDimitry Andric sign = rhs.sign; 774*0b57cec5SDimitry Andric category = rhs.category; 775*0b57cec5SDimitry Andric exponent = rhs.exponent; 776*0b57cec5SDimitry Andric if (isFiniteNonZero() || category == fcNaN) 777*0b57cec5SDimitry Andric copySignificand(rhs); 778*0b57cec5SDimitry Andric } 779*0b57cec5SDimitry Andric 780*0b57cec5SDimitry Andric void IEEEFloat::copySignificand(const IEEEFloat &rhs) { 781*0b57cec5SDimitry Andric assert(isFiniteNonZero() || category == fcNaN); 782*0b57cec5SDimitry Andric assert(rhs.partCount() >= partCount()); 783*0b57cec5SDimitry Andric 784*0b57cec5SDimitry Andric APInt::tcAssign(significandParts(), rhs.significandParts(), 785*0b57cec5SDimitry Andric partCount()); 786*0b57cec5SDimitry Andric } 787*0b57cec5SDimitry Andric 788*0b57cec5SDimitry Andric /* Make this number a NaN, with an arbitrary but deterministic value 789*0b57cec5SDimitry Andric for the significand. If double or longer, this is a signalling NaN, 790*0b57cec5SDimitry Andric which may not be ideal. If float, this is QNaN(0). */ 791*0b57cec5SDimitry Andric void IEEEFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill) { 792*0b57cec5SDimitry Andric category = fcNaN; 793*0b57cec5SDimitry Andric sign = Negative; 794e8d8bef9SDimitry Andric exponent = exponentNaN(); 795*0b57cec5SDimitry Andric 796*0b57cec5SDimitry Andric integerPart *significand = significandParts(); 797*0b57cec5SDimitry Andric unsigned numParts = partCount(); 798*0b57cec5SDimitry Andric 799bdd1243dSDimitry Andric APInt fill_storage; 800bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { 801bdd1243dSDimitry Andric // The only NaN representation is where the mantissa is all 1s, which is 802bdd1243dSDimitry Andric // non-signalling. 803bdd1243dSDimitry Andric SNaN = false; 804bdd1243dSDimitry Andric fill_storage = APInt::getAllOnes(semantics->precision - 1); 805bdd1243dSDimitry Andric fill = &fill_storage; 806bdd1243dSDimitry Andric } 807bdd1243dSDimitry Andric 808*0b57cec5SDimitry Andric // Set the significand bits to the fill. 809*0b57cec5SDimitry Andric if (!fill || fill->getNumWords() < numParts) 810*0b57cec5SDimitry Andric APInt::tcSet(significand, 0, numParts); 811*0b57cec5SDimitry Andric if (fill) { 812*0b57cec5SDimitry Andric APInt::tcAssign(significand, fill->getRawData(), 813*0b57cec5SDimitry Andric std::min(fill->getNumWords(), numParts)); 814*0b57cec5SDimitry Andric 815*0b57cec5SDimitry Andric // Zero out the excess bits of the significand. 816*0b57cec5SDimitry Andric unsigned bitsToPreserve = semantics->precision - 1; 817*0b57cec5SDimitry Andric unsigned part = bitsToPreserve / 64; 818*0b57cec5SDimitry Andric bitsToPreserve %= 64; 819*0b57cec5SDimitry Andric significand[part] &= ((1ULL << bitsToPreserve) - 1); 820*0b57cec5SDimitry Andric for (part++; part != numParts; ++part) 821*0b57cec5SDimitry Andric significand[part] = 0; 822*0b57cec5SDimitry Andric } 823*0b57cec5SDimitry Andric 824*0b57cec5SDimitry Andric unsigned QNaNBit = semantics->precision - 2; 825*0b57cec5SDimitry Andric 826*0b57cec5SDimitry Andric if (SNaN) { 827*0b57cec5SDimitry Andric // We always have to clear the QNaN bit to make it an SNaN. 828*0b57cec5SDimitry Andric APInt::tcClearBit(significand, QNaNBit); 829*0b57cec5SDimitry Andric 830*0b57cec5SDimitry Andric // If there are no bits set in the payload, we have to set 831*0b57cec5SDimitry Andric // *something* to make it a NaN instead of an infinity; 832*0b57cec5SDimitry Andric // conventionally, this is the next bit down from the QNaN bit. 833*0b57cec5SDimitry Andric if (APInt::tcIsZero(significand, numParts)) 834*0b57cec5SDimitry Andric APInt::tcSetBit(significand, QNaNBit - 1); 835*0b57cec5SDimitry Andric } else { 836*0b57cec5SDimitry Andric // We always have to set the QNaN bit to make it a QNaN. 837*0b57cec5SDimitry Andric APInt::tcSetBit(significand, QNaNBit); 838*0b57cec5SDimitry Andric } 839*0b57cec5SDimitry Andric 840*0b57cec5SDimitry Andric // For x87 extended precision, we want to make a NaN, not a 841*0b57cec5SDimitry Andric // pseudo-NaN. Maybe we should expose the ability to make 842*0b57cec5SDimitry Andric // pseudo-NaNs? 843*0b57cec5SDimitry Andric if (semantics == &semX87DoubleExtended) 844*0b57cec5SDimitry Andric APInt::tcSetBit(significand, QNaNBit + 1); 845*0b57cec5SDimitry Andric } 846*0b57cec5SDimitry Andric 847*0b57cec5SDimitry Andric IEEEFloat &IEEEFloat::operator=(const IEEEFloat &rhs) { 848*0b57cec5SDimitry Andric if (this != &rhs) { 849*0b57cec5SDimitry Andric if (semantics != rhs.semantics) { 850*0b57cec5SDimitry Andric freeSignificand(); 851*0b57cec5SDimitry Andric initialize(rhs.semantics); 852*0b57cec5SDimitry Andric } 853*0b57cec5SDimitry Andric assign(rhs); 854*0b57cec5SDimitry Andric } 855*0b57cec5SDimitry Andric 856*0b57cec5SDimitry Andric return *this; 857*0b57cec5SDimitry Andric } 858*0b57cec5SDimitry Andric 859*0b57cec5SDimitry Andric IEEEFloat &IEEEFloat::operator=(IEEEFloat &&rhs) { 860*0b57cec5SDimitry Andric freeSignificand(); 861*0b57cec5SDimitry Andric 862*0b57cec5SDimitry Andric semantics = rhs.semantics; 863*0b57cec5SDimitry Andric significand = rhs.significand; 864*0b57cec5SDimitry Andric exponent = rhs.exponent; 865*0b57cec5SDimitry Andric category = rhs.category; 866*0b57cec5SDimitry Andric sign = rhs.sign; 867*0b57cec5SDimitry Andric 868*0b57cec5SDimitry Andric rhs.semantics = &semBogus; 869*0b57cec5SDimitry Andric return *this; 870*0b57cec5SDimitry Andric } 871*0b57cec5SDimitry Andric 872*0b57cec5SDimitry Andric bool IEEEFloat::isDenormal() const { 873*0b57cec5SDimitry Andric return isFiniteNonZero() && (exponent == semantics->minExponent) && 874*0b57cec5SDimitry Andric (APInt::tcExtractBit(significandParts(), 875*0b57cec5SDimitry Andric semantics->precision - 1) == 0); 876*0b57cec5SDimitry Andric } 877*0b57cec5SDimitry Andric 878*0b57cec5SDimitry Andric bool IEEEFloat::isSmallest() const { 879*0b57cec5SDimitry Andric // The smallest number by magnitude in our format will be the smallest 880*0b57cec5SDimitry Andric // denormal, i.e. the floating point number with exponent being minimum 881*0b57cec5SDimitry Andric // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0). 882*0b57cec5SDimitry Andric return isFiniteNonZero() && exponent == semantics->minExponent && 883*0b57cec5SDimitry Andric significandMSB() == 0; 884*0b57cec5SDimitry Andric } 885*0b57cec5SDimitry Andric 886bdd1243dSDimitry Andric bool IEEEFloat::isSmallestNormalized() const { 887bdd1243dSDimitry Andric return getCategory() == fcNormal && exponent == semantics->minExponent && 888bdd1243dSDimitry Andric isSignificandAllZerosExceptMSB(); 889bdd1243dSDimitry Andric } 890bdd1243dSDimitry Andric 891*0b57cec5SDimitry Andric bool IEEEFloat::isSignificandAllOnes() const { 892*0b57cec5SDimitry Andric // Test if the significand excluding the integral bit is all ones. This allows 893*0b57cec5SDimitry Andric // us to test for binade boundaries. 894*0b57cec5SDimitry Andric const integerPart *Parts = significandParts(); 895e8d8bef9SDimitry Andric const unsigned PartCount = partCountForBits(semantics->precision); 896*0b57cec5SDimitry Andric for (unsigned i = 0; i < PartCount - 1; i++) 897*0b57cec5SDimitry Andric if (~Parts[i]) 898*0b57cec5SDimitry Andric return false; 899*0b57cec5SDimitry Andric 900*0b57cec5SDimitry Andric // Set the unused high bits to all ones when we compare. 901*0b57cec5SDimitry Andric const unsigned NumHighBits = 902*0b57cec5SDimitry Andric PartCount*integerPartWidth - semantics->precision + 1; 903e8d8bef9SDimitry Andric assert(NumHighBits <= integerPartWidth && NumHighBits > 0 && 904e8d8bef9SDimitry Andric "Can not have more high bits to fill than integerPartWidth"); 905*0b57cec5SDimitry Andric const integerPart HighBitFill = 906*0b57cec5SDimitry Andric ~integerPart(0) << (integerPartWidth - NumHighBits); 907*0b57cec5SDimitry Andric if (~(Parts[PartCount - 1] | HighBitFill)) 908*0b57cec5SDimitry Andric return false; 909*0b57cec5SDimitry Andric 910*0b57cec5SDimitry Andric return true; 911*0b57cec5SDimitry Andric } 912*0b57cec5SDimitry Andric 913bdd1243dSDimitry Andric bool IEEEFloat::isSignificandAllOnesExceptLSB() const { 914bdd1243dSDimitry Andric // Test if the significand excluding the integral bit is all ones except for 915bdd1243dSDimitry Andric // the least significant bit. 916bdd1243dSDimitry Andric const integerPart *Parts = significandParts(); 917bdd1243dSDimitry Andric 918bdd1243dSDimitry Andric if (Parts[0] & 1) 919bdd1243dSDimitry Andric return false; 920bdd1243dSDimitry Andric 921bdd1243dSDimitry Andric const unsigned PartCount = partCountForBits(semantics->precision); 922bdd1243dSDimitry Andric for (unsigned i = 0; i < PartCount - 1; i++) { 923bdd1243dSDimitry Andric if (~Parts[i] & ~unsigned{!i}) 924bdd1243dSDimitry Andric return false; 925bdd1243dSDimitry Andric } 926bdd1243dSDimitry Andric 927bdd1243dSDimitry Andric // Set the unused high bits to all ones when we compare. 928bdd1243dSDimitry Andric const unsigned NumHighBits = 929bdd1243dSDimitry Andric PartCount * integerPartWidth - semantics->precision + 1; 930bdd1243dSDimitry Andric assert(NumHighBits <= integerPartWidth && NumHighBits > 0 && 931bdd1243dSDimitry Andric "Can not have more high bits to fill than integerPartWidth"); 932bdd1243dSDimitry Andric const integerPart HighBitFill = ~integerPart(0) 933bdd1243dSDimitry Andric << (integerPartWidth - NumHighBits); 934bdd1243dSDimitry Andric if (~(Parts[PartCount - 1] | HighBitFill | 0x1)) 935bdd1243dSDimitry Andric return false; 936bdd1243dSDimitry Andric 937bdd1243dSDimitry Andric return true; 938bdd1243dSDimitry Andric } 939bdd1243dSDimitry Andric 940*0b57cec5SDimitry Andric bool IEEEFloat::isSignificandAllZeros() const { 941*0b57cec5SDimitry Andric // Test if the significand excluding the integral bit is all zeros. This 942*0b57cec5SDimitry Andric // allows us to test for binade boundaries. 943*0b57cec5SDimitry Andric const integerPart *Parts = significandParts(); 944e8d8bef9SDimitry Andric const unsigned PartCount = partCountForBits(semantics->precision); 945*0b57cec5SDimitry Andric 946*0b57cec5SDimitry Andric for (unsigned i = 0; i < PartCount - 1; i++) 947*0b57cec5SDimitry Andric if (Parts[i]) 948*0b57cec5SDimitry Andric return false; 949*0b57cec5SDimitry Andric 950e8d8bef9SDimitry Andric // Compute how many bits are used in the final word. 951*0b57cec5SDimitry Andric const unsigned NumHighBits = 952*0b57cec5SDimitry Andric PartCount*integerPartWidth - semantics->precision + 1; 953e8d8bef9SDimitry Andric assert(NumHighBits < integerPartWidth && "Can not have more high bits to " 954*0b57cec5SDimitry Andric "clear than integerPartWidth"); 955*0b57cec5SDimitry Andric const integerPart HighBitMask = ~integerPart(0) >> NumHighBits; 956*0b57cec5SDimitry Andric 957*0b57cec5SDimitry Andric if (Parts[PartCount - 1] & HighBitMask) 958*0b57cec5SDimitry Andric return false; 959*0b57cec5SDimitry Andric 960*0b57cec5SDimitry Andric return true; 961*0b57cec5SDimitry Andric } 962*0b57cec5SDimitry Andric 963bdd1243dSDimitry Andric bool IEEEFloat::isSignificandAllZerosExceptMSB() const { 964bdd1243dSDimitry Andric const integerPart *Parts = significandParts(); 965bdd1243dSDimitry Andric const unsigned PartCount = partCountForBits(semantics->precision); 966bdd1243dSDimitry Andric 967bdd1243dSDimitry Andric for (unsigned i = 0; i < PartCount - 1; i++) { 968bdd1243dSDimitry Andric if (Parts[i]) 969bdd1243dSDimitry Andric return false; 970bdd1243dSDimitry Andric } 971bdd1243dSDimitry Andric 972bdd1243dSDimitry Andric const unsigned NumHighBits = 973bdd1243dSDimitry Andric PartCount * integerPartWidth - semantics->precision + 1; 974bdd1243dSDimitry Andric return Parts[PartCount - 1] == integerPart(1) 975bdd1243dSDimitry Andric << (integerPartWidth - NumHighBits); 976bdd1243dSDimitry Andric } 977bdd1243dSDimitry Andric 978*0b57cec5SDimitry Andric bool IEEEFloat::isLargest() const { 979bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { 980bdd1243dSDimitry Andric // The largest number by magnitude in our format will be the floating point 981bdd1243dSDimitry Andric // number with maximum exponent and with significand that is all ones except 982bdd1243dSDimitry Andric // the LSB. 983bdd1243dSDimitry Andric return isFiniteNonZero() && exponent == semantics->maxExponent && 984bdd1243dSDimitry Andric isSignificandAllOnesExceptLSB(); 985bdd1243dSDimitry Andric } else { 986*0b57cec5SDimitry Andric // The largest number by magnitude in our format will be the floating point 987*0b57cec5SDimitry Andric // number with maximum exponent and with significand that is all ones. 988bdd1243dSDimitry Andric return isFiniteNonZero() && exponent == semantics->maxExponent && 989bdd1243dSDimitry Andric isSignificandAllOnes(); 990bdd1243dSDimitry Andric } 991*0b57cec5SDimitry Andric } 992*0b57cec5SDimitry Andric 993*0b57cec5SDimitry Andric bool IEEEFloat::isInteger() const { 994*0b57cec5SDimitry Andric // This could be made more efficient; I'm going for obviously correct. 995*0b57cec5SDimitry Andric if (!isFinite()) return false; 996*0b57cec5SDimitry Andric IEEEFloat truncated = *this; 997*0b57cec5SDimitry Andric truncated.roundToIntegral(rmTowardZero); 998*0b57cec5SDimitry Andric return compare(truncated) == cmpEqual; 999*0b57cec5SDimitry Andric } 1000*0b57cec5SDimitry Andric 1001*0b57cec5SDimitry Andric bool IEEEFloat::bitwiseIsEqual(const IEEEFloat &rhs) const { 1002*0b57cec5SDimitry Andric if (this == &rhs) 1003*0b57cec5SDimitry Andric return true; 1004*0b57cec5SDimitry Andric if (semantics != rhs.semantics || 1005*0b57cec5SDimitry Andric category != rhs.category || 1006*0b57cec5SDimitry Andric sign != rhs.sign) 1007*0b57cec5SDimitry Andric return false; 1008*0b57cec5SDimitry Andric if (category==fcZero || category==fcInfinity) 1009*0b57cec5SDimitry Andric return true; 1010*0b57cec5SDimitry Andric 1011*0b57cec5SDimitry Andric if (isFiniteNonZero() && exponent != rhs.exponent) 1012*0b57cec5SDimitry Andric return false; 1013*0b57cec5SDimitry Andric 1014*0b57cec5SDimitry Andric return std::equal(significandParts(), significandParts() + partCount(), 1015*0b57cec5SDimitry Andric rhs.significandParts()); 1016*0b57cec5SDimitry Andric } 1017*0b57cec5SDimitry Andric 1018*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, integerPart value) { 1019*0b57cec5SDimitry Andric initialize(&ourSemantics); 1020*0b57cec5SDimitry Andric sign = 0; 1021*0b57cec5SDimitry Andric category = fcNormal; 1022*0b57cec5SDimitry Andric zeroSignificand(); 1023*0b57cec5SDimitry Andric exponent = ourSemantics.precision - 1; 1024*0b57cec5SDimitry Andric significandParts()[0] = value; 1025*0b57cec5SDimitry Andric normalize(rmNearestTiesToEven, lfExactlyZero); 1026*0b57cec5SDimitry Andric } 1027*0b57cec5SDimitry Andric 1028*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics) { 1029*0b57cec5SDimitry Andric initialize(&ourSemantics); 1030e8d8bef9SDimitry Andric makeZero(false); 1031*0b57cec5SDimitry Andric } 1032*0b57cec5SDimitry Andric 1033*0b57cec5SDimitry Andric // Delegate to the previous constructor, because later copy constructor may 1034*0b57cec5SDimitry Andric // actually inspects category, which can't be garbage. 1035*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, uninitializedTag tag) 1036*0b57cec5SDimitry Andric : IEEEFloat(ourSemantics) {} 1037*0b57cec5SDimitry Andric 1038*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const IEEEFloat &rhs) { 1039*0b57cec5SDimitry Andric initialize(rhs.semantics); 1040*0b57cec5SDimitry Andric assign(rhs); 1041*0b57cec5SDimitry Andric } 1042*0b57cec5SDimitry Andric 1043*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(IEEEFloat &&rhs) : semantics(&semBogus) { 1044*0b57cec5SDimitry Andric *this = std::move(rhs); 1045*0b57cec5SDimitry Andric } 1046*0b57cec5SDimitry Andric 1047*0b57cec5SDimitry Andric IEEEFloat::~IEEEFloat() { freeSignificand(); } 1048*0b57cec5SDimitry Andric 1049*0b57cec5SDimitry Andric unsigned int IEEEFloat::partCount() const { 1050*0b57cec5SDimitry Andric return partCountForBits(semantics->precision + 1); 1051*0b57cec5SDimitry Andric } 1052*0b57cec5SDimitry Andric 1053*0b57cec5SDimitry Andric const IEEEFloat::integerPart *IEEEFloat::significandParts() const { 1054*0b57cec5SDimitry Andric return const_cast<IEEEFloat *>(this)->significandParts(); 1055*0b57cec5SDimitry Andric } 1056*0b57cec5SDimitry Andric 1057*0b57cec5SDimitry Andric IEEEFloat::integerPart *IEEEFloat::significandParts() { 1058*0b57cec5SDimitry Andric if (partCount() > 1) 1059*0b57cec5SDimitry Andric return significand.parts; 1060*0b57cec5SDimitry Andric else 1061*0b57cec5SDimitry Andric return &significand.part; 1062*0b57cec5SDimitry Andric } 1063*0b57cec5SDimitry Andric 1064*0b57cec5SDimitry Andric void IEEEFloat::zeroSignificand() { 1065*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 1066*0b57cec5SDimitry Andric } 1067*0b57cec5SDimitry Andric 1068*0b57cec5SDimitry Andric /* Increment an fcNormal floating point number's significand. */ 1069*0b57cec5SDimitry Andric void IEEEFloat::incrementSignificand() { 1070*0b57cec5SDimitry Andric integerPart carry; 1071*0b57cec5SDimitry Andric 1072*0b57cec5SDimitry Andric carry = APInt::tcIncrement(significandParts(), partCount()); 1073*0b57cec5SDimitry Andric 1074*0b57cec5SDimitry Andric /* Our callers should never cause us to overflow. */ 1075*0b57cec5SDimitry Andric assert(carry == 0); 1076*0b57cec5SDimitry Andric (void)carry; 1077*0b57cec5SDimitry Andric } 1078*0b57cec5SDimitry Andric 1079*0b57cec5SDimitry Andric /* Add the significand of the RHS. Returns the carry flag. */ 1080*0b57cec5SDimitry Andric IEEEFloat::integerPart IEEEFloat::addSignificand(const IEEEFloat &rhs) { 1081*0b57cec5SDimitry Andric integerPart *parts; 1082*0b57cec5SDimitry Andric 1083*0b57cec5SDimitry Andric parts = significandParts(); 1084*0b57cec5SDimitry Andric 1085*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 1086*0b57cec5SDimitry Andric assert(exponent == rhs.exponent); 1087*0b57cec5SDimitry Andric 1088*0b57cec5SDimitry Andric return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount()); 1089*0b57cec5SDimitry Andric } 1090*0b57cec5SDimitry Andric 1091*0b57cec5SDimitry Andric /* Subtract the significand of the RHS with a borrow flag. Returns 1092*0b57cec5SDimitry Andric the borrow flag. */ 1093*0b57cec5SDimitry Andric IEEEFloat::integerPart IEEEFloat::subtractSignificand(const IEEEFloat &rhs, 1094*0b57cec5SDimitry Andric integerPart borrow) { 1095*0b57cec5SDimitry Andric integerPart *parts; 1096*0b57cec5SDimitry Andric 1097*0b57cec5SDimitry Andric parts = significandParts(); 1098*0b57cec5SDimitry Andric 1099*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 1100*0b57cec5SDimitry Andric assert(exponent == rhs.exponent); 1101*0b57cec5SDimitry Andric 1102*0b57cec5SDimitry Andric return APInt::tcSubtract(parts, rhs.significandParts(), borrow, 1103*0b57cec5SDimitry Andric partCount()); 1104*0b57cec5SDimitry Andric } 1105*0b57cec5SDimitry Andric 1106*0b57cec5SDimitry Andric /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it 1107*0b57cec5SDimitry Andric on to the full-precision result of the multiplication. Returns the 1108*0b57cec5SDimitry Andric lost fraction. */ 1109*0b57cec5SDimitry Andric lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs, 1110480093f4SDimitry Andric IEEEFloat addend) { 1111*0b57cec5SDimitry Andric unsigned int omsb; // One, not zero, based MSB. 1112*0b57cec5SDimitry Andric unsigned int partsCount, newPartsCount, precision; 1113*0b57cec5SDimitry Andric integerPart *lhsSignificand; 1114*0b57cec5SDimitry Andric integerPart scratch[4]; 1115*0b57cec5SDimitry Andric integerPart *fullSignificand; 1116*0b57cec5SDimitry Andric lostFraction lost_fraction; 1117*0b57cec5SDimitry Andric bool ignored; 1118*0b57cec5SDimitry Andric 1119*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 1120*0b57cec5SDimitry Andric 1121*0b57cec5SDimitry Andric precision = semantics->precision; 1122*0b57cec5SDimitry Andric 1123*0b57cec5SDimitry Andric // Allocate space for twice as many bits as the original significand, plus one 1124*0b57cec5SDimitry Andric // extra bit for the addition to overflow into. 1125*0b57cec5SDimitry Andric newPartsCount = partCountForBits(precision * 2 + 1); 1126*0b57cec5SDimitry Andric 1127*0b57cec5SDimitry Andric if (newPartsCount > 4) 1128*0b57cec5SDimitry Andric fullSignificand = new integerPart[newPartsCount]; 1129*0b57cec5SDimitry Andric else 1130*0b57cec5SDimitry Andric fullSignificand = scratch; 1131*0b57cec5SDimitry Andric 1132*0b57cec5SDimitry Andric lhsSignificand = significandParts(); 1133*0b57cec5SDimitry Andric partsCount = partCount(); 1134*0b57cec5SDimitry Andric 1135*0b57cec5SDimitry Andric APInt::tcFullMultiply(fullSignificand, lhsSignificand, 1136*0b57cec5SDimitry Andric rhs.significandParts(), partsCount, partsCount); 1137*0b57cec5SDimitry Andric 1138*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 1139*0b57cec5SDimitry Andric omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; 1140*0b57cec5SDimitry Andric exponent += rhs.exponent; 1141*0b57cec5SDimitry Andric 1142*0b57cec5SDimitry Andric // Assume the operands involved in the multiplication are single-precision 1143*0b57cec5SDimitry Andric // FP, and the two multiplicants are: 1144*0b57cec5SDimitry Andric // *this = a23 . a22 ... a0 * 2^e1 1145*0b57cec5SDimitry Andric // rhs = b23 . b22 ... b0 * 2^e2 1146*0b57cec5SDimitry Andric // the result of multiplication is: 1147*0b57cec5SDimitry Andric // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2) 1148*0b57cec5SDimitry Andric // Note that there are three significant bits at the left-hand side of the 1149*0b57cec5SDimitry Andric // radix point: two for the multiplication, and an overflow bit for the 1150*0b57cec5SDimitry Andric // addition (that will always be zero at this point). Move the radix point 1151*0b57cec5SDimitry Andric // toward left by two bits, and adjust exponent accordingly. 1152*0b57cec5SDimitry Andric exponent += 2; 1153*0b57cec5SDimitry Andric 1154480093f4SDimitry Andric if (addend.isNonZero()) { 1155*0b57cec5SDimitry Andric // The intermediate result of the multiplication has "2 * precision" 1156*0b57cec5SDimitry Andric // signicant bit; adjust the addend to be consistent with mul result. 1157*0b57cec5SDimitry Andric // 1158*0b57cec5SDimitry Andric Significand savedSignificand = significand; 1159*0b57cec5SDimitry Andric const fltSemantics *savedSemantics = semantics; 1160*0b57cec5SDimitry Andric fltSemantics extendedSemantics; 1161*0b57cec5SDimitry Andric opStatus status; 1162*0b57cec5SDimitry Andric unsigned int extendedPrecision; 1163*0b57cec5SDimitry Andric 1164*0b57cec5SDimitry Andric // Normalize our MSB to one below the top bit to allow for overflow. 1165*0b57cec5SDimitry Andric extendedPrecision = 2 * precision + 1; 1166*0b57cec5SDimitry Andric if (omsb != extendedPrecision - 1) { 1167*0b57cec5SDimitry Andric assert(extendedPrecision > omsb); 1168*0b57cec5SDimitry Andric APInt::tcShiftLeft(fullSignificand, newPartsCount, 1169*0b57cec5SDimitry Andric (extendedPrecision - 1) - omsb); 1170*0b57cec5SDimitry Andric exponent -= (extendedPrecision - 1) - omsb; 1171*0b57cec5SDimitry Andric } 1172*0b57cec5SDimitry Andric 1173*0b57cec5SDimitry Andric /* Create new semantics. */ 1174*0b57cec5SDimitry Andric extendedSemantics = *semantics; 1175*0b57cec5SDimitry Andric extendedSemantics.precision = extendedPrecision; 1176*0b57cec5SDimitry Andric 1177*0b57cec5SDimitry Andric if (newPartsCount == 1) 1178*0b57cec5SDimitry Andric significand.part = fullSignificand[0]; 1179*0b57cec5SDimitry Andric else 1180*0b57cec5SDimitry Andric significand.parts = fullSignificand; 1181*0b57cec5SDimitry Andric semantics = &extendedSemantics; 1182*0b57cec5SDimitry Andric 1183480093f4SDimitry Andric // Make a copy so we can convert it to the extended semantics. 1184480093f4SDimitry Andric // Note that we cannot convert the addend directly, as the extendedSemantics 1185480093f4SDimitry Andric // is a local variable (which we take a reference to). 1186480093f4SDimitry Andric IEEEFloat extendedAddend(addend); 1187*0b57cec5SDimitry Andric status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored); 1188*0b57cec5SDimitry Andric assert(status == opOK); 1189*0b57cec5SDimitry Andric (void)status; 1190*0b57cec5SDimitry Andric 1191*0b57cec5SDimitry Andric // Shift the significand of the addend right by one bit. This guarantees 1192*0b57cec5SDimitry Andric // that the high bit of the significand is zero (same as fullSignificand), 1193*0b57cec5SDimitry Andric // so the addition will overflow (if it does overflow at all) into the top bit. 1194*0b57cec5SDimitry Andric lost_fraction = extendedAddend.shiftSignificandRight(1); 1195*0b57cec5SDimitry Andric assert(lost_fraction == lfExactlyZero && 1196*0b57cec5SDimitry Andric "Lost precision while shifting addend for fused-multiply-add."); 1197*0b57cec5SDimitry Andric 1198*0b57cec5SDimitry Andric lost_fraction = addOrSubtractSignificand(extendedAddend, false); 1199*0b57cec5SDimitry Andric 1200*0b57cec5SDimitry Andric /* Restore our state. */ 1201*0b57cec5SDimitry Andric if (newPartsCount == 1) 1202*0b57cec5SDimitry Andric fullSignificand[0] = significand.part; 1203*0b57cec5SDimitry Andric significand = savedSignificand; 1204*0b57cec5SDimitry Andric semantics = savedSemantics; 1205*0b57cec5SDimitry Andric 1206*0b57cec5SDimitry Andric omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; 1207*0b57cec5SDimitry Andric } 1208*0b57cec5SDimitry Andric 1209*0b57cec5SDimitry Andric // Convert the result having "2 * precision" significant-bits back to the one 1210*0b57cec5SDimitry Andric // having "precision" significant-bits. First, move the radix point from 1211*0b57cec5SDimitry Andric // poision "2*precision - 1" to "precision - 1". The exponent need to be 1212*0b57cec5SDimitry Andric // adjusted by "2*precision - 1" - "precision - 1" = "precision". 1213*0b57cec5SDimitry Andric exponent -= precision + 1; 1214*0b57cec5SDimitry Andric 1215*0b57cec5SDimitry Andric // In case MSB resides at the left-hand side of radix point, shift the 1216*0b57cec5SDimitry Andric // mantissa right by some amount to make sure the MSB reside right before 1217*0b57cec5SDimitry Andric // the radix point (i.e. "MSB . rest-significant-bits"). 1218*0b57cec5SDimitry Andric // 1219*0b57cec5SDimitry Andric // Note that the result is not normalized when "omsb < precision". So, the 1220*0b57cec5SDimitry Andric // caller needs to call IEEEFloat::normalize() if normalized value is 1221*0b57cec5SDimitry Andric // expected. 1222*0b57cec5SDimitry Andric if (omsb > precision) { 1223*0b57cec5SDimitry Andric unsigned int bits, significantParts; 1224*0b57cec5SDimitry Andric lostFraction lf; 1225*0b57cec5SDimitry Andric 1226*0b57cec5SDimitry Andric bits = omsb - precision; 1227*0b57cec5SDimitry Andric significantParts = partCountForBits(omsb); 1228*0b57cec5SDimitry Andric lf = shiftRight(fullSignificand, significantParts, bits); 1229*0b57cec5SDimitry Andric lost_fraction = combineLostFractions(lf, lost_fraction); 1230*0b57cec5SDimitry Andric exponent += bits; 1231*0b57cec5SDimitry Andric } 1232*0b57cec5SDimitry Andric 1233*0b57cec5SDimitry Andric APInt::tcAssign(lhsSignificand, fullSignificand, partsCount); 1234*0b57cec5SDimitry Andric 1235*0b57cec5SDimitry Andric if (newPartsCount > 4) 1236*0b57cec5SDimitry Andric delete [] fullSignificand; 1237*0b57cec5SDimitry Andric 1238*0b57cec5SDimitry Andric return lost_fraction; 1239*0b57cec5SDimitry Andric } 1240*0b57cec5SDimitry Andric 1241480093f4SDimitry Andric lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs) { 1242480093f4SDimitry Andric return multiplySignificand(rhs, IEEEFloat(*semantics)); 1243480093f4SDimitry Andric } 1244480093f4SDimitry Andric 1245*0b57cec5SDimitry Andric /* Multiply the significands of LHS and RHS to DST. */ 1246*0b57cec5SDimitry Andric lostFraction IEEEFloat::divideSignificand(const IEEEFloat &rhs) { 1247*0b57cec5SDimitry Andric unsigned int bit, i, partsCount; 1248*0b57cec5SDimitry Andric const integerPart *rhsSignificand; 1249*0b57cec5SDimitry Andric integerPart *lhsSignificand, *dividend, *divisor; 1250*0b57cec5SDimitry Andric integerPart scratch[4]; 1251*0b57cec5SDimitry Andric lostFraction lost_fraction; 1252*0b57cec5SDimitry Andric 1253*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 1254*0b57cec5SDimitry Andric 1255*0b57cec5SDimitry Andric lhsSignificand = significandParts(); 1256*0b57cec5SDimitry Andric rhsSignificand = rhs.significandParts(); 1257*0b57cec5SDimitry Andric partsCount = partCount(); 1258*0b57cec5SDimitry Andric 1259*0b57cec5SDimitry Andric if (partsCount > 2) 1260*0b57cec5SDimitry Andric dividend = new integerPart[partsCount * 2]; 1261*0b57cec5SDimitry Andric else 1262*0b57cec5SDimitry Andric dividend = scratch; 1263*0b57cec5SDimitry Andric 1264*0b57cec5SDimitry Andric divisor = dividend + partsCount; 1265*0b57cec5SDimitry Andric 1266*0b57cec5SDimitry Andric /* Copy the dividend and divisor as they will be modified in-place. */ 1267*0b57cec5SDimitry Andric for (i = 0; i < partsCount; i++) { 1268*0b57cec5SDimitry Andric dividend[i] = lhsSignificand[i]; 1269*0b57cec5SDimitry Andric divisor[i] = rhsSignificand[i]; 1270*0b57cec5SDimitry Andric lhsSignificand[i] = 0; 1271*0b57cec5SDimitry Andric } 1272*0b57cec5SDimitry Andric 1273*0b57cec5SDimitry Andric exponent -= rhs.exponent; 1274*0b57cec5SDimitry Andric 1275*0b57cec5SDimitry Andric unsigned int precision = semantics->precision; 1276*0b57cec5SDimitry Andric 1277*0b57cec5SDimitry Andric /* Normalize the divisor. */ 1278*0b57cec5SDimitry Andric bit = precision - APInt::tcMSB(divisor, partsCount) - 1; 1279*0b57cec5SDimitry Andric if (bit) { 1280*0b57cec5SDimitry Andric exponent += bit; 1281*0b57cec5SDimitry Andric APInt::tcShiftLeft(divisor, partsCount, bit); 1282*0b57cec5SDimitry Andric } 1283*0b57cec5SDimitry Andric 1284*0b57cec5SDimitry Andric /* Normalize the dividend. */ 1285*0b57cec5SDimitry Andric bit = precision - APInt::tcMSB(dividend, partsCount) - 1; 1286*0b57cec5SDimitry Andric if (bit) { 1287*0b57cec5SDimitry Andric exponent -= bit; 1288*0b57cec5SDimitry Andric APInt::tcShiftLeft(dividend, partsCount, bit); 1289*0b57cec5SDimitry Andric } 1290*0b57cec5SDimitry Andric 1291*0b57cec5SDimitry Andric /* Ensure the dividend >= divisor initially for the loop below. 1292*0b57cec5SDimitry Andric Incidentally, this means that the division loop below is 1293*0b57cec5SDimitry Andric guaranteed to set the integer bit to one. */ 1294*0b57cec5SDimitry Andric if (APInt::tcCompare(dividend, divisor, partsCount) < 0) { 1295*0b57cec5SDimitry Andric exponent--; 1296*0b57cec5SDimitry Andric APInt::tcShiftLeft(dividend, partsCount, 1); 1297*0b57cec5SDimitry Andric assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0); 1298*0b57cec5SDimitry Andric } 1299*0b57cec5SDimitry Andric 1300*0b57cec5SDimitry Andric /* Long division. */ 1301*0b57cec5SDimitry Andric for (bit = precision; bit; bit -= 1) { 1302*0b57cec5SDimitry Andric if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) { 1303*0b57cec5SDimitry Andric APInt::tcSubtract(dividend, divisor, 0, partsCount); 1304*0b57cec5SDimitry Andric APInt::tcSetBit(lhsSignificand, bit - 1); 1305*0b57cec5SDimitry Andric } 1306*0b57cec5SDimitry Andric 1307*0b57cec5SDimitry Andric APInt::tcShiftLeft(dividend, partsCount, 1); 1308*0b57cec5SDimitry Andric } 1309*0b57cec5SDimitry Andric 1310*0b57cec5SDimitry Andric /* Figure out the lost fraction. */ 1311*0b57cec5SDimitry Andric int cmp = APInt::tcCompare(dividend, divisor, partsCount); 1312*0b57cec5SDimitry Andric 1313*0b57cec5SDimitry Andric if (cmp > 0) 1314*0b57cec5SDimitry Andric lost_fraction = lfMoreThanHalf; 1315*0b57cec5SDimitry Andric else if (cmp == 0) 1316*0b57cec5SDimitry Andric lost_fraction = lfExactlyHalf; 1317*0b57cec5SDimitry Andric else if (APInt::tcIsZero(dividend, partsCount)) 1318*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 1319*0b57cec5SDimitry Andric else 1320*0b57cec5SDimitry Andric lost_fraction = lfLessThanHalf; 1321*0b57cec5SDimitry Andric 1322*0b57cec5SDimitry Andric if (partsCount > 2) 1323*0b57cec5SDimitry Andric delete [] dividend; 1324*0b57cec5SDimitry Andric 1325*0b57cec5SDimitry Andric return lost_fraction; 1326*0b57cec5SDimitry Andric } 1327*0b57cec5SDimitry Andric 1328*0b57cec5SDimitry Andric unsigned int IEEEFloat::significandMSB() const { 1329*0b57cec5SDimitry Andric return APInt::tcMSB(significandParts(), partCount()); 1330*0b57cec5SDimitry Andric } 1331*0b57cec5SDimitry Andric 1332*0b57cec5SDimitry Andric unsigned int IEEEFloat::significandLSB() const { 1333*0b57cec5SDimitry Andric return APInt::tcLSB(significandParts(), partCount()); 1334*0b57cec5SDimitry Andric } 1335*0b57cec5SDimitry Andric 1336*0b57cec5SDimitry Andric /* Note that a zero result is NOT normalized to fcZero. */ 1337*0b57cec5SDimitry Andric lostFraction IEEEFloat::shiftSignificandRight(unsigned int bits) { 1338*0b57cec5SDimitry Andric /* Our exponent should not overflow. */ 1339*0b57cec5SDimitry Andric assert((ExponentType) (exponent + bits) >= exponent); 1340*0b57cec5SDimitry Andric 1341*0b57cec5SDimitry Andric exponent += bits; 1342*0b57cec5SDimitry Andric 1343*0b57cec5SDimitry Andric return shiftRight(significandParts(), partCount(), bits); 1344*0b57cec5SDimitry Andric } 1345*0b57cec5SDimitry Andric 1346*0b57cec5SDimitry Andric /* Shift the significand left BITS bits, subtract BITS from its exponent. */ 1347*0b57cec5SDimitry Andric void IEEEFloat::shiftSignificandLeft(unsigned int bits) { 1348*0b57cec5SDimitry Andric assert(bits < semantics->precision); 1349*0b57cec5SDimitry Andric 1350*0b57cec5SDimitry Andric if (bits) { 1351*0b57cec5SDimitry Andric unsigned int partsCount = partCount(); 1352*0b57cec5SDimitry Andric 1353*0b57cec5SDimitry Andric APInt::tcShiftLeft(significandParts(), partsCount, bits); 1354*0b57cec5SDimitry Andric exponent -= bits; 1355*0b57cec5SDimitry Andric 1356*0b57cec5SDimitry Andric assert(!APInt::tcIsZero(significandParts(), partsCount)); 1357*0b57cec5SDimitry Andric } 1358*0b57cec5SDimitry Andric } 1359*0b57cec5SDimitry Andric 1360*0b57cec5SDimitry Andric IEEEFloat::cmpResult 1361*0b57cec5SDimitry Andric IEEEFloat::compareAbsoluteValue(const IEEEFloat &rhs) const { 1362*0b57cec5SDimitry Andric int compare; 1363*0b57cec5SDimitry Andric 1364*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 1365*0b57cec5SDimitry Andric assert(isFiniteNonZero()); 1366*0b57cec5SDimitry Andric assert(rhs.isFiniteNonZero()); 1367*0b57cec5SDimitry Andric 1368*0b57cec5SDimitry Andric compare = exponent - rhs.exponent; 1369*0b57cec5SDimitry Andric 1370*0b57cec5SDimitry Andric /* If exponents are equal, do an unsigned bignum comparison of the 1371*0b57cec5SDimitry Andric significands. */ 1372*0b57cec5SDimitry Andric if (compare == 0) 1373*0b57cec5SDimitry Andric compare = APInt::tcCompare(significandParts(), rhs.significandParts(), 1374*0b57cec5SDimitry Andric partCount()); 1375*0b57cec5SDimitry Andric 1376*0b57cec5SDimitry Andric if (compare > 0) 1377*0b57cec5SDimitry Andric return cmpGreaterThan; 1378*0b57cec5SDimitry Andric else if (compare < 0) 1379*0b57cec5SDimitry Andric return cmpLessThan; 1380*0b57cec5SDimitry Andric else 1381*0b57cec5SDimitry Andric return cmpEqual; 1382*0b57cec5SDimitry Andric } 1383*0b57cec5SDimitry Andric 1384349cc55cSDimitry Andric /* Set the least significant BITS bits of a bignum, clear the 1385349cc55cSDimitry Andric rest. */ 1386349cc55cSDimitry Andric static void tcSetLeastSignificantBits(APInt::WordType *dst, unsigned parts, 1387349cc55cSDimitry Andric unsigned bits) { 1388349cc55cSDimitry Andric unsigned i = 0; 1389349cc55cSDimitry Andric while (bits > APInt::APINT_BITS_PER_WORD) { 1390349cc55cSDimitry Andric dst[i++] = ~(APInt::WordType)0; 1391349cc55cSDimitry Andric bits -= APInt::APINT_BITS_PER_WORD; 1392349cc55cSDimitry Andric } 1393349cc55cSDimitry Andric 1394349cc55cSDimitry Andric if (bits) 1395349cc55cSDimitry Andric dst[i++] = ~(APInt::WordType)0 >> (APInt::APINT_BITS_PER_WORD - bits); 1396349cc55cSDimitry Andric 1397349cc55cSDimitry Andric while (i < parts) 1398349cc55cSDimitry Andric dst[i++] = 0; 1399349cc55cSDimitry Andric } 1400349cc55cSDimitry Andric 1401*0b57cec5SDimitry Andric /* Handle overflow. Sign is preserved. We either become infinity or 1402*0b57cec5SDimitry Andric the largest finite number. */ 1403*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::handleOverflow(roundingMode rounding_mode) { 1404*0b57cec5SDimitry Andric /* Infinity? */ 1405*0b57cec5SDimitry Andric if (rounding_mode == rmNearestTiesToEven || 1406*0b57cec5SDimitry Andric rounding_mode == rmNearestTiesToAway || 1407*0b57cec5SDimitry Andric (rounding_mode == rmTowardPositive && !sign) || 1408*0b57cec5SDimitry Andric (rounding_mode == rmTowardNegative && sign)) { 1409bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) 1410bdd1243dSDimitry Andric makeNaN(false, sign); 1411bdd1243dSDimitry Andric else 1412*0b57cec5SDimitry Andric category = fcInfinity; 1413*0b57cec5SDimitry Andric return (opStatus) (opOverflow | opInexact); 1414*0b57cec5SDimitry Andric } 1415*0b57cec5SDimitry Andric 1416*0b57cec5SDimitry Andric /* Otherwise we become the largest finite number. */ 1417*0b57cec5SDimitry Andric category = fcNormal; 1418*0b57cec5SDimitry Andric exponent = semantics->maxExponent; 1419349cc55cSDimitry Andric tcSetLeastSignificantBits(significandParts(), partCount(), 1420*0b57cec5SDimitry Andric semantics->precision); 1421bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) 1422bdd1243dSDimitry Andric APInt::tcClearBit(significandParts(), 0); 1423*0b57cec5SDimitry Andric 1424*0b57cec5SDimitry Andric return opInexact; 1425*0b57cec5SDimitry Andric } 1426*0b57cec5SDimitry Andric 1427*0b57cec5SDimitry Andric /* Returns TRUE if, when truncating the current number, with BIT the 1428*0b57cec5SDimitry Andric new LSB, with the given lost fraction and rounding mode, the result 1429*0b57cec5SDimitry Andric would need to be rounded away from zero (i.e., by increasing the 1430*0b57cec5SDimitry Andric signficand). This routine must work for fcZero of both signs, and 1431*0b57cec5SDimitry Andric fcNormal numbers. */ 1432*0b57cec5SDimitry Andric bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode, 1433*0b57cec5SDimitry Andric lostFraction lost_fraction, 1434*0b57cec5SDimitry Andric unsigned int bit) const { 1435*0b57cec5SDimitry Andric /* NaNs and infinities should not have lost fractions. */ 1436*0b57cec5SDimitry Andric assert(isFiniteNonZero() || category == fcZero); 1437*0b57cec5SDimitry Andric 1438*0b57cec5SDimitry Andric /* Current callers never pass this so we don't handle it. */ 1439*0b57cec5SDimitry Andric assert(lost_fraction != lfExactlyZero); 1440*0b57cec5SDimitry Andric 1441*0b57cec5SDimitry Andric switch (rounding_mode) { 1442*0b57cec5SDimitry Andric case rmNearestTiesToAway: 1443*0b57cec5SDimitry Andric return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf; 1444*0b57cec5SDimitry Andric 1445*0b57cec5SDimitry Andric case rmNearestTiesToEven: 1446*0b57cec5SDimitry Andric if (lost_fraction == lfMoreThanHalf) 1447*0b57cec5SDimitry Andric return true; 1448*0b57cec5SDimitry Andric 1449*0b57cec5SDimitry Andric /* Our zeroes don't have a significand to test. */ 1450*0b57cec5SDimitry Andric if (lost_fraction == lfExactlyHalf && category != fcZero) 1451*0b57cec5SDimitry Andric return APInt::tcExtractBit(significandParts(), bit); 1452*0b57cec5SDimitry Andric 1453*0b57cec5SDimitry Andric return false; 1454*0b57cec5SDimitry Andric 1455*0b57cec5SDimitry Andric case rmTowardZero: 1456*0b57cec5SDimitry Andric return false; 1457*0b57cec5SDimitry Andric 1458*0b57cec5SDimitry Andric case rmTowardPositive: 1459*0b57cec5SDimitry Andric return !sign; 1460*0b57cec5SDimitry Andric 1461*0b57cec5SDimitry Andric case rmTowardNegative: 1462*0b57cec5SDimitry Andric return sign; 14635ffd83dbSDimitry Andric 14645ffd83dbSDimitry Andric default: 14655ffd83dbSDimitry Andric break; 1466*0b57cec5SDimitry Andric } 1467*0b57cec5SDimitry Andric llvm_unreachable("Invalid rounding mode found"); 1468*0b57cec5SDimitry Andric } 1469*0b57cec5SDimitry Andric 1470*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::normalize(roundingMode rounding_mode, 1471*0b57cec5SDimitry Andric lostFraction lost_fraction) { 1472*0b57cec5SDimitry Andric unsigned int omsb; /* One, not zero, based MSB. */ 1473*0b57cec5SDimitry Andric int exponentChange; 1474*0b57cec5SDimitry Andric 1475*0b57cec5SDimitry Andric if (!isFiniteNonZero()) 1476*0b57cec5SDimitry Andric return opOK; 1477*0b57cec5SDimitry Andric 1478*0b57cec5SDimitry Andric /* Before rounding normalize the exponent of fcNormal numbers. */ 1479*0b57cec5SDimitry Andric omsb = significandMSB() + 1; 1480*0b57cec5SDimitry Andric 1481*0b57cec5SDimitry Andric if (omsb) { 1482*0b57cec5SDimitry Andric /* OMSB is numbered from 1. We want to place it in the integer 1483*0b57cec5SDimitry Andric bit numbered PRECISION if possible, with a compensating change in 1484*0b57cec5SDimitry Andric the exponent. */ 1485*0b57cec5SDimitry Andric exponentChange = omsb - semantics->precision; 1486*0b57cec5SDimitry Andric 1487*0b57cec5SDimitry Andric /* If the resulting exponent is too high, overflow according to 1488*0b57cec5SDimitry Andric the rounding mode. */ 1489*0b57cec5SDimitry Andric if (exponent + exponentChange > semantics->maxExponent) 1490*0b57cec5SDimitry Andric return handleOverflow(rounding_mode); 1491*0b57cec5SDimitry Andric 1492*0b57cec5SDimitry Andric /* Subnormal numbers have exponent minExponent, and their MSB 1493*0b57cec5SDimitry Andric is forced based on that. */ 1494*0b57cec5SDimitry Andric if (exponent + exponentChange < semantics->minExponent) 1495*0b57cec5SDimitry Andric exponentChange = semantics->minExponent - exponent; 1496*0b57cec5SDimitry Andric 1497*0b57cec5SDimitry Andric /* Shifting left is easy as we don't lose precision. */ 1498*0b57cec5SDimitry Andric if (exponentChange < 0) { 1499*0b57cec5SDimitry Andric assert(lost_fraction == lfExactlyZero); 1500*0b57cec5SDimitry Andric 1501*0b57cec5SDimitry Andric shiftSignificandLeft(-exponentChange); 1502*0b57cec5SDimitry Andric 1503*0b57cec5SDimitry Andric return opOK; 1504*0b57cec5SDimitry Andric } 1505*0b57cec5SDimitry Andric 1506*0b57cec5SDimitry Andric if (exponentChange > 0) { 1507*0b57cec5SDimitry Andric lostFraction lf; 1508*0b57cec5SDimitry Andric 1509*0b57cec5SDimitry Andric /* Shift right and capture any new lost fraction. */ 1510*0b57cec5SDimitry Andric lf = shiftSignificandRight(exponentChange); 1511*0b57cec5SDimitry Andric 1512*0b57cec5SDimitry Andric lost_fraction = combineLostFractions(lf, lost_fraction); 1513*0b57cec5SDimitry Andric 1514*0b57cec5SDimitry Andric /* Keep OMSB up-to-date. */ 1515*0b57cec5SDimitry Andric if (omsb > (unsigned) exponentChange) 1516*0b57cec5SDimitry Andric omsb -= exponentChange; 1517*0b57cec5SDimitry Andric else 1518*0b57cec5SDimitry Andric omsb = 0; 1519*0b57cec5SDimitry Andric } 1520*0b57cec5SDimitry Andric } 1521*0b57cec5SDimitry Andric 1522bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly && 1523bdd1243dSDimitry Andric exponent == semantics->maxExponent && isSignificandAllOnes()) 1524bdd1243dSDimitry Andric return handleOverflow(rounding_mode); 1525bdd1243dSDimitry Andric 1526*0b57cec5SDimitry Andric /* Now round the number according to rounding_mode given the lost 1527*0b57cec5SDimitry Andric fraction. */ 1528*0b57cec5SDimitry Andric 1529*0b57cec5SDimitry Andric /* As specified in IEEE 754, since we do not trap we do not report 1530*0b57cec5SDimitry Andric underflow for exact results. */ 1531*0b57cec5SDimitry Andric if (lost_fraction == lfExactlyZero) { 1532*0b57cec5SDimitry Andric /* Canonicalize zeroes. */ 1533*0b57cec5SDimitry Andric if (omsb == 0) 1534*0b57cec5SDimitry Andric category = fcZero; 1535*0b57cec5SDimitry Andric 1536*0b57cec5SDimitry Andric return opOK; 1537*0b57cec5SDimitry Andric } 1538*0b57cec5SDimitry Andric 1539*0b57cec5SDimitry Andric /* Increment the significand if we're rounding away from zero. */ 1540*0b57cec5SDimitry Andric if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) { 1541*0b57cec5SDimitry Andric if (omsb == 0) 1542*0b57cec5SDimitry Andric exponent = semantics->minExponent; 1543*0b57cec5SDimitry Andric 1544*0b57cec5SDimitry Andric incrementSignificand(); 1545*0b57cec5SDimitry Andric omsb = significandMSB() + 1; 1546*0b57cec5SDimitry Andric 1547*0b57cec5SDimitry Andric /* Did the significand increment overflow? */ 1548*0b57cec5SDimitry Andric if (omsb == (unsigned) semantics->precision + 1) { 1549*0b57cec5SDimitry Andric /* Renormalize by incrementing the exponent and shifting our 1550*0b57cec5SDimitry Andric significand right one. However if we already have the 1551*0b57cec5SDimitry Andric maximum exponent we overflow to infinity. */ 1552*0b57cec5SDimitry Andric if (exponent == semantics->maxExponent) { 1553*0b57cec5SDimitry Andric category = fcInfinity; 1554*0b57cec5SDimitry Andric 1555*0b57cec5SDimitry Andric return (opStatus) (opOverflow | opInexact); 1556*0b57cec5SDimitry Andric } 1557*0b57cec5SDimitry Andric 1558*0b57cec5SDimitry Andric shiftSignificandRight(1); 1559*0b57cec5SDimitry Andric 1560*0b57cec5SDimitry Andric return opInexact; 1561*0b57cec5SDimitry Andric } 1562bdd1243dSDimitry Andric 1563bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly && 1564bdd1243dSDimitry Andric exponent == semantics->maxExponent && isSignificandAllOnes()) 1565bdd1243dSDimitry Andric return handleOverflow(rounding_mode); 1566*0b57cec5SDimitry Andric } 1567*0b57cec5SDimitry Andric 1568*0b57cec5SDimitry Andric /* The normal case - we were and are not denormal, and any 1569*0b57cec5SDimitry Andric significand increment above didn't overflow. */ 1570*0b57cec5SDimitry Andric if (omsb == semantics->precision) 1571*0b57cec5SDimitry Andric return opInexact; 1572*0b57cec5SDimitry Andric 1573*0b57cec5SDimitry Andric /* We have a non-zero denormal. */ 1574*0b57cec5SDimitry Andric assert(omsb < semantics->precision); 1575*0b57cec5SDimitry Andric 1576*0b57cec5SDimitry Andric /* Canonicalize zeroes. */ 1577*0b57cec5SDimitry Andric if (omsb == 0) 1578*0b57cec5SDimitry Andric category = fcZero; 1579*0b57cec5SDimitry Andric 1580*0b57cec5SDimitry Andric /* The fcZero case is a denormal that underflowed to zero. */ 1581*0b57cec5SDimitry Andric return (opStatus) (opUnderflow | opInexact); 1582*0b57cec5SDimitry Andric } 1583*0b57cec5SDimitry Andric 1584*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::addOrSubtractSpecials(const IEEEFloat &rhs, 1585*0b57cec5SDimitry Andric bool subtract) { 1586*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 1587*0b57cec5SDimitry Andric default: 1588*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 1589*0b57cec5SDimitry Andric 15905ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 15915ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 15925ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 15935ffd83dbSDimitry Andric assign(rhs); 1594bdd1243dSDimitry Andric [[fallthrough]]; 1595*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 1596*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 1597*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 1598*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 15995ffd83dbSDimitry Andric if (isSignaling()) { 16005ffd83dbSDimitry Andric makeQuiet(); 16015ffd83dbSDimitry Andric return opInvalidOp; 16025ffd83dbSDimitry Andric } 16035ffd83dbSDimitry Andric return rhs.isSignaling() ? opInvalidOp : opOK; 16045ffd83dbSDimitry Andric 1605*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 1606*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 1607*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 1608*0b57cec5SDimitry Andric return opOK; 1609*0b57cec5SDimitry Andric 1610*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 1611*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 1612*0b57cec5SDimitry Andric category = fcInfinity; 1613*0b57cec5SDimitry Andric sign = rhs.sign ^ subtract; 1614*0b57cec5SDimitry Andric return opOK; 1615*0b57cec5SDimitry Andric 1616*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 1617*0b57cec5SDimitry Andric assign(rhs); 1618*0b57cec5SDimitry Andric sign = rhs.sign ^ subtract; 1619*0b57cec5SDimitry Andric return opOK; 1620*0b57cec5SDimitry Andric 1621*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 1622*0b57cec5SDimitry Andric /* Sign depends on rounding mode; handled by caller. */ 1623*0b57cec5SDimitry Andric return opOK; 1624*0b57cec5SDimitry Andric 1625*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1626*0b57cec5SDimitry Andric /* Differently signed infinities can only be validly 1627*0b57cec5SDimitry Andric subtracted. */ 1628*0b57cec5SDimitry Andric if (((sign ^ rhs.sign)!=0) != subtract) { 1629*0b57cec5SDimitry Andric makeNaN(); 1630*0b57cec5SDimitry Andric return opInvalidOp; 1631*0b57cec5SDimitry Andric } 1632*0b57cec5SDimitry Andric 1633*0b57cec5SDimitry Andric return opOK; 1634*0b57cec5SDimitry Andric 1635*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 1636*0b57cec5SDimitry Andric return opDivByZero; 1637*0b57cec5SDimitry Andric } 1638*0b57cec5SDimitry Andric } 1639*0b57cec5SDimitry Andric 1640*0b57cec5SDimitry Andric /* Add or subtract two normal numbers. */ 1641*0b57cec5SDimitry Andric lostFraction IEEEFloat::addOrSubtractSignificand(const IEEEFloat &rhs, 1642*0b57cec5SDimitry Andric bool subtract) { 1643*0b57cec5SDimitry Andric integerPart carry; 1644*0b57cec5SDimitry Andric lostFraction lost_fraction; 1645*0b57cec5SDimitry Andric int bits; 1646*0b57cec5SDimitry Andric 1647*0b57cec5SDimitry Andric /* Determine if the operation on the absolute values is effectively 1648*0b57cec5SDimitry Andric an addition or subtraction. */ 1649*0b57cec5SDimitry Andric subtract ^= static_cast<bool>(sign ^ rhs.sign); 1650*0b57cec5SDimitry Andric 1651*0b57cec5SDimitry Andric /* Are we bigger exponent-wise than the RHS? */ 1652*0b57cec5SDimitry Andric bits = exponent - rhs.exponent; 1653*0b57cec5SDimitry Andric 1654*0b57cec5SDimitry Andric /* Subtraction is more subtle than one might naively expect. */ 1655*0b57cec5SDimitry Andric if (subtract) { 1656*0b57cec5SDimitry Andric IEEEFloat temp_rhs(rhs); 1657*0b57cec5SDimitry Andric 1658480093f4SDimitry Andric if (bits == 0) 1659*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 1660480093f4SDimitry Andric else if (bits > 0) { 1661*0b57cec5SDimitry Andric lost_fraction = temp_rhs.shiftSignificandRight(bits - 1); 1662*0b57cec5SDimitry Andric shiftSignificandLeft(1); 1663*0b57cec5SDimitry Andric } else { 1664*0b57cec5SDimitry Andric lost_fraction = shiftSignificandRight(-bits - 1); 1665*0b57cec5SDimitry Andric temp_rhs.shiftSignificandLeft(1); 1666*0b57cec5SDimitry Andric } 1667*0b57cec5SDimitry Andric 1668480093f4SDimitry Andric // Should we reverse the subtraction. 1669480093f4SDimitry Andric if (compareAbsoluteValue(temp_rhs) == cmpLessThan) { 1670*0b57cec5SDimitry Andric carry = temp_rhs.subtractSignificand 1671*0b57cec5SDimitry Andric (*this, lost_fraction != lfExactlyZero); 1672*0b57cec5SDimitry Andric copySignificand(temp_rhs); 1673*0b57cec5SDimitry Andric sign = !sign; 1674*0b57cec5SDimitry Andric } else { 1675*0b57cec5SDimitry Andric carry = subtractSignificand 1676*0b57cec5SDimitry Andric (temp_rhs, lost_fraction != lfExactlyZero); 1677*0b57cec5SDimitry Andric } 1678*0b57cec5SDimitry Andric 1679*0b57cec5SDimitry Andric /* Invert the lost fraction - it was on the RHS and 1680*0b57cec5SDimitry Andric subtracted. */ 1681*0b57cec5SDimitry Andric if (lost_fraction == lfLessThanHalf) 1682*0b57cec5SDimitry Andric lost_fraction = lfMoreThanHalf; 1683*0b57cec5SDimitry Andric else if (lost_fraction == lfMoreThanHalf) 1684*0b57cec5SDimitry Andric lost_fraction = lfLessThanHalf; 1685*0b57cec5SDimitry Andric 1686*0b57cec5SDimitry Andric /* The code above is intended to ensure that no borrow is 1687*0b57cec5SDimitry Andric necessary. */ 1688*0b57cec5SDimitry Andric assert(!carry); 1689*0b57cec5SDimitry Andric (void)carry; 1690*0b57cec5SDimitry Andric } else { 1691*0b57cec5SDimitry Andric if (bits > 0) { 1692*0b57cec5SDimitry Andric IEEEFloat temp_rhs(rhs); 1693*0b57cec5SDimitry Andric 1694*0b57cec5SDimitry Andric lost_fraction = temp_rhs.shiftSignificandRight(bits); 1695*0b57cec5SDimitry Andric carry = addSignificand(temp_rhs); 1696*0b57cec5SDimitry Andric } else { 1697*0b57cec5SDimitry Andric lost_fraction = shiftSignificandRight(-bits); 1698*0b57cec5SDimitry Andric carry = addSignificand(rhs); 1699*0b57cec5SDimitry Andric } 1700*0b57cec5SDimitry Andric 1701*0b57cec5SDimitry Andric /* We have a guard bit; generating a carry cannot happen. */ 1702*0b57cec5SDimitry Andric assert(!carry); 1703*0b57cec5SDimitry Andric (void)carry; 1704*0b57cec5SDimitry Andric } 1705*0b57cec5SDimitry Andric 1706*0b57cec5SDimitry Andric return lost_fraction; 1707*0b57cec5SDimitry Andric } 1708*0b57cec5SDimitry Andric 1709*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::multiplySpecials(const IEEEFloat &rhs) { 1710*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 1711*0b57cec5SDimitry Andric default: 1712*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 1713*0b57cec5SDimitry Andric 17145ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 17155ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 17165ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 17175ffd83dbSDimitry Andric assign(rhs); 17185ffd83dbSDimitry Andric sign = false; 1719bdd1243dSDimitry Andric [[fallthrough]]; 1720*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 1721*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 1722*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 1723*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 17245ffd83dbSDimitry Andric sign ^= rhs.sign; // restore the original sign 17255ffd83dbSDimitry Andric if (isSignaling()) { 17265ffd83dbSDimitry Andric makeQuiet(); 17275ffd83dbSDimitry Andric return opInvalidOp; 17285ffd83dbSDimitry Andric } 17295ffd83dbSDimitry Andric return rhs.isSignaling() ? opInvalidOp : opOK; 1730*0b57cec5SDimitry Andric 1731*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 1732*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 1733*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1734*0b57cec5SDimitry Andric category = fcInfinity; 1735*0b57cec5SDimitry Andric return opOK; 1736*0b57cec5SDimitry Andric 1737*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 1738*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 1739*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 1740*0b57cec5SDimitry Andric category = fcZero; 1741*0b57cec5SDimitry Andric return opOK; 1742*0b57cec5SDimitry Andric 1743*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 1744*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 1745*0b57cec5SDimitry Andric makeNaN(); 1746*0b57cec5SDimitry Andric return opInvalidOp; 1747*0b57cec5SDimitry Andric 1748*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 1749*0b57cec5SDimitry Andric return opOK; 1750*0b57cec5SDimitry Andric } 1751*0b57cec5SDimitry Andric } 1752*0b57cec5SDimitry Andric 1753*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::divideSpecials(const IEEEFloat &rhs) { 1754*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 1755*0b57cec5SDimitry Andric default: 1756*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 1757*0b57cec5SDimitry Andric 1758*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 1759*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 1760*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 17615ffd83dbSDimitry Andric assign(rhs); 17625ffd83dbSDimitry Andric sign = false; 1763bdd1243dSDimitry Andric [[fallthrough]]; 1764*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 1765*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 1766*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 1767*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 17685ffd83dbSDimitry Andric sign ^= rhs.sign; // restore the original sign 17695ffd83dbSDimitry Andric if (isSignaling()) { 17705ffd83dbSDimitry Andric makeQuiet(); 17715ffd83dbSDimitry Andric return opInvalidOp; 17725ffd83dbSDimitry Andric } 17735ffd83dbSDimitry Andric return rhs.isSignaling() ? opInvalidOp : opOK; 17745ffd83dbSDimitry Andric 1775*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 1776*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 1777*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 1778*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 1779*0b57cec5SDimitry Andric return opOK; 1780*0b57cec5SDimitry Andric 1781*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 1782*0b57cec5SDimitry Andric category = fcZero; 1783*0b57cec5SDimitry Andric return opOK; 1784*0b57cec5SDimitry Andric 1785*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 1786bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) 1787bdd1243dSDimitry Andric makeNaN(false, sign); 1788bdd1243dSDimitry Andric else 1789*0b57cec5SDimitry Andric category = fcInfinity; 1790*0b57cec5SDimitry Andric return opDivByZero; 1791*0b57cec5SDimitry Andric 1792*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1793*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 1794*0b57cec5SDimitry Andric makeNaN(); 1795*0b57cec5SDimitry Andric return opInvalidOp; 1796*0b57cec5SDimitry Andric 1797*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 1798*0b57cec5SDimitry Andric return opOK; 1799*0b57cec5SDimitry Andric } 1800*0b57cec5SDimitry Andric } 1801*0b57cec5SDimitry Andric 1802*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::modSpecials(const IEEEFloat &rhs) { 1803*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 1804*0b57cec5SDimitry Andric default: 1805*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 1806*0b57cec5SDimitry Andric 18075ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 18085ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 18095ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 18105ffd83dbSDimitry Andric assign(rhs); 1811bdd1243dSDimitry Andric [[fallthrough]]; 1812*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 1813*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 1814*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 1815*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 18165ffd83dbSDimitry Andric if (isSignaling()) { 18175ffd83dbSDimitry Andric makeQuiet(); 18185ffd83dbSDimitry Andric return opInvalidOp; 18195ffd83dbSDimitry Andric } 18205ffd83dbSDimitry Andric return rhs.isSignaling() ? opInvalidOp : opOK; 18215ffd83dbSDimitry Andric 1822*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 1823*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 1824*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 1825*0b57cec5SDimitry Andric return opOK; 1826*0b57cec5SDimitry Andric 1827*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 1828*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 1829*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 1830*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1831*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 1832*0b57cec5SDimitry Andric makeNaN(); 1833*0b57cec5SDimitry Andric return opInvalidOp; 1834*0b57cec5SDimitry Andric 1835*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 1836*0b57cec5SDimitry Andric return opOK; 1837*0b57cec5SDimitry Andric } 1838*0b57cec5SDimitry Andric } 1839*0b57cec5SDimitry Andric 18405ffd83dbSDimitry Andric IEEEFloat::opStatus IEEEFloat::remainderSpecials(const IEEEFloat &rhs) { 18415ffd83dbSDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 18425ffd83dbSDimitry Andric default: 18435ffd83dbSDimitry Andric llvm_unreachable(nullptr); 18445ffd83dbSDimitry Andric 18455ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 18465ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 18475ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 18485ffd83dbSDimitry Andric assign(rhs); 1849bdd1243dSDimitry Andric [[fallthrough]]; 18505ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 18515ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 18525ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 18535ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 18545ffd83dbSDimitry Andric if (isSignaling()) { 18555ffd83dbSDimitry Andric makeQuiet(); 18565ffd83dbSDimitry Andric return opInvalidOp; 18575ffd83dbSDimitry Andric } 18585ffd83dbSDimitry Andric return rhs.isSignaling() ? opInvalidOp : opOK; 18595ffd83dbSDimitry Andric 18605ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 18615ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 18625ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 18635ffd83dbSDimitry Andric return opOK; 18645ffd83dbSDimitry Andric 18655ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 18665ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 18675ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 18685ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 18695ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 18705ffd83dbSDimitry Andric makeNaN(); 18715ffd83dbSDimitry Andric return opInvalidOp; 18725ffd83dbSDimitry Andric 18735ffd83dbSDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 18745ffd83dbSDimitry Andric return opDivByZero; // fake status, indicating this is not a special case 18755ffd83dbSDimitry Andric } 18765ffd83dbSDimitry Andric } 18775ffd83dbSDimitry Andric 1878*0b57cec5SDimitry Andric /* Change sign. */ 1879*0b57cec5SDimitry Andric void IEEEFloat::changeSign() { 1880*0b57cec5SDimitry Andric /* Look mummy, this one's easy. */ 1881*0b57cec5SDimitry Andric sign = !sign; 1882*0b57cec5SDimitry Andric } 1883*0b57cec5SDimitry Andric 1884*0b57cec5SDimitry Andric /* Normalized addition or subtraction. */ 1885*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::addOrSubtract(const IEEEFloat &rhs, 1886*0b57cec5SDimitry Andric roundingMode rounding_mode, 1887*0b57cec5SDimitry Andric bool subtract) { 1888*0b57cec5SDimitry Andric opStatus fs; 1889*0b57cec5SDimitry Andric 1890*0b57cec5SDimitry Andric fs = addOrSubtractSpecials(rhs, subtract); 1891*0b57cec5SDimitry Andric 1892*0b57cec5SDimitry Andric /* This return code means it was not a simple case. */ 1893*0b57cec5SDimitry Andric if (fs == opDivByZero) { 1894*0b57cec5SDimitry Andric lostFraction lost_fraction; 1895*0b57cec5SDimitry Andric 1896*0b57cec5SDimitry Andric lost_fraction = addOrSubtractSignificand(rhs, subtract); 1897*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lost_fraction); 1898*0b57cec5SDimitry Andric 1899*0b57cec5SDimitry Andric /* Can only be zero if we lost no fraction. */ 1900*0b57cec5SDimitry Andric assert(category != fcZero || lost_fraction == lfExactlyZero); 1901*0b57cec5SDimitry Andric } 1902*0b57cec5SDimitry Andric 1903*0b57cec5SDimitry Andric /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a 1904*0b57cec5SDimitry Andric positive zero unless rounding to minus infinity, except that 1905*0b57cec5SDimitry Andric adding two like-signed zeroes gives that zero. */ 1906*0b57cec5SDimitry Andric if (category == fcZero) { 1907*0b57cec5SDimitry Andric if (rhs.category != fcZero || (sign == rhs.sign) == subtract) 1908*0b57cec5SDimitry Andric sign = (rounding_mode == rmTowardNegative); 1909*0b57cec5SDimitry Andric } 1910*0b57cec5SDimitry Andric 1911*0b57cec5SDimitry Andric return fs; 1912*0b57cec5SDimitry Andric } 1913*0b57cec5SDimitry Andric 1914*0b57cec5SDimitry Andric /* Normalized addition. */ 1915*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::add(const IEEEFloat &rhs, 1916*0b57cec5SDimitry Andric roundingMode rounding_mode) { 1917*0b57cec5SDimitry Andric return addOrSubtract(rhs, rounding_mode, false); 1918*0b57cec5SDimitry Andric } 1919*0b57cec5SDimitry Andric 1920*0b57cec5SDimitry Andric /* Normalized subtraction. */ 1921*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::subtract(const IEEEFloat &rhs, 1922*0b57cec5SDimitry Andric roundingMode rounding_mode) { 1923*0b57cec5SDimitry Andric return addOrSubtract(rhs, rounding_mode, true); 1924*0b57cec5SDimitry Andric } 1925*0b57cec5SDimitry Andric 1926*0b57cec5SDimitry Andric /* Normalized multiply. */ 1927*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::multiply(const IEEEFloat &rhs, 1928*0b57cec5SDimitry Andric roundingMode rounding_mode) { 1929*0b57cec5SDimitry Andric opStatus fs; 1930*0b57cec5SDimitry Andric 1931*0b57cec5SDimitry Andric sign ^= rhs.sign; 1932*0b57cec5SDimitry Andric fs = multiplySpecials(rhs); 1933*0b57cec5SDimitry Andric 1934*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 1935480093f4SDimitry Andric lostFraction lost_fraction = multiplySignificand(rhs); 1936*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lost_fraction); 1937*0b57cec5SDimitry Andric if (lost_fraction != lfExactlyZero) 1938*0b57cec5SDimitry Andric fs = (opStatus) (fs | opInexact); 1939*0b57cec5SDimitry Andric } 1940*0b57cec5SDimitry Andric 1941*0b57cec5SDimitry Andric return fs; 1942*0b57cec5SDimitry Andric } 1943*0b57cec5SDimitry Andric 1944*0b57cec5SDimitry Andric /* Normalized divide. */ 1945*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::divide(const IEEEFloat &rhs, 1946*0b57cec5SDimitry Andric roundingMode rounding_mode) { 1947*0b57cec5SDimitry Andric opStatus fs; 1948*0b57cec5SDimitry Andric 1949*0b57cec5SDimitry Andric sign ^= rhs.sign; 1950*0b57cec5SDimitry Andric fs = divideSpecials(rhs); 1951*0b57cec5SDimitry Andric 1952*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 1953*0b57cec5SDimitry Andric lostFraction lost_fraction = divideSignificand(rhs); 1954*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lost_fraction); 1955*0b57cec5SDimitry Andric if (lost_fraction != lfExactlyZero) 1956*0b57cec5SDimitry Andric fs = (opStatus) (fs | opInexact); 1957*0b57cec5SDimitry Andric } 1958*0b57cec5SDimitry Andric 1959*0b57cec5SDimitry Andric return fs; 1960*0b57cec5SDimitry Andric } 1961*0b57cec5SDimitry Andric 19625ffd83dbSDimitry Andric /* Normalized remainder. */ 1963*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::remainder(const IEEEFloat &rhs) { 1964*0b57cec5SDimitry Andric opStatus fs; 1965*0b57cec5SDimitry Andric unsigned int origSign = sign; 1966*0b57cec5SDimitry Andric 19675ffd83dbSDimitry Andric // First handle the special cases. 19685ffd83dbSDimitry Andric fs = remainderSpecials(rhs); 19695ffd83dbSDimitry Andric if (fs != opDivByZero) 1970*0b57cec5SDimitry Andric return fs; 1971*0b57cec5SDimitry Andric 19725ffd83dbSDimitry Andric fs = opOK; 19735ffd83dbSDimitry Andric 19745ffd83dbSDimitry Andric // Make sure the current value is less than twice the denom. If the addition 19755ffd83dbSDimitry Andric // did not succeed (an overflow has happened), which means that the finite 19765ffd83dbSDimitry Andric // value we currently posses must be less than twice the denom (as we are 19775ffd83dbSDimitry Andric // using the same semantics). 19785ffd83dbSDimitry Andric IEEEFloat P2 = rhs; 19795ffd83dbSDimitry Andric if (P2.add(rhs, rmNearestTiesToEven) == opOK) { 19805ffd83dbSDimitry Andric fs = mod(P2); 19815ffd83dbSDimitry Andric assert(fs == opOK); 1982*0b57cec5SDimitry Andric } 1983*0b57cec5SDimitry Andric 19845ffd83dbSDimitry Andric // Lets work with absolute numbers. 19855ffd83dbSDimitry Andric IEEEFloat P = rhs; 19865ffd83dbSDimitry Andric P.sign = false; 19875ffd83dbSDimitry Andric sign = false; 1988*0b57cec5SDimitry Andric 19895ffd83dbSDimitry Andric // 19905ffd83dbSDimitry Andric // To calculate the remainder we use the following scheme. 19915ffd83dbSDimitry Andric // 19925ffd83dbSDimitry Andric // The remainder is defained as follows: 19935ffd83dbSDimitry Andric // 19945ffd83dbSDimitry Andric // remainder = numer - rquot * denom = x - r * p 19955ffd83dbSDimitry Andric // 19965ffd83dbSDimitry Andric // Where r is the result of: x/p, rounded toward the nearest integral value 19975ffd83dbSDimitry Andric // (with halfway cases rounded toward the even number). 19985ffd83dbSDimitry Andric // 19995ffd83dbSDimitry Andric // Currently, (after x mod 2p): 20005ffd83dbSDimitry Andric // r is the number of 2p's present inside x, which is inherently, an even 20015ffd83dbSDimitry Andric // number of p's. 20025ffd83dbSDimitry Andric // 20035ffd83dbSDimitry Andric // We may split the remaining calculation into 4 options: 20045ffd83dbSDimitry Andric // - if x < 0.5p then we round to the nearest number with is 0, and are done. 20055ffd83dbSDimitry Andric // - if x == 0.5p then we round to the nearest even number which is 0, and we 20065ffd83dbSDimitry Andric // are done as well. 20075ffd83dbSDimitry Andric // - if 0.5p < x < p then we round to nearest number which is 1, and we have 20085ffd83dbSDimitry Andric // to subtract 1p at least once. 20095ffd83dbSDimitry Andric // - if x >= p then we must subtract p at least once, as x must be a 20105ffd83dbSDimitry Andric // remainder. 20115ffd83dbSDimitry Andric // 20125ffd83dbSDimitry Andric // By now, we were done, or we added 1 to r, which in turn, now an odd number. 20135ffd83dbSDimitry Andric // 20145ffd83dbSDimitry Andric // We can now split the remaining calculation to the following 3 options: 20155ffd83dbSDimitry Andric // - if x < 0.5p then we round to the nearest number with is 0, and are done. 20165ffd83dbSDimitry Andric // - if x == 0.5p then we round to the nearest even number. As r is odd, we 20175ffd83dbSDimitry Andric // must round up to the next even number. so we must subtract p once more. 20185ffd83dbSDimitry Andric // - if x > 0.5p (and inherently x < p) then we must round r up to the next 20195ffd83dbSDimitry Andric // integral, and subtract p once more. 20205ffd83dbSDimitry Andric // 2021*0b57cec5SDimitry Andric 20225ffd83dbSDimitry Andric // Extend the semantics to prevent an overflow/underflow or inexact result. 20235ffd83dbSDimitry Andric bool losesInfo; 20245ffd83dbSDimitry Andric fltSemantics extendedSemantics = *semantics; 20255ffd83dbSDimitry Andric extendedSemantics.maxExponent++; 20265ffd83dbSDimitry Andric extendedSemantics.minExponent--; 20275ffd83dbSDimitry Andric extendedSemantics.precision += 2; 20285ffd83dbSDimitry Andric 20295ffd83dbSDimitry Andric IEEEFloat VEx = *this; 20305ffd83dbSDimitry Andric fs = VEx.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 20315ffd83dbSDimitry Andric assert(fs == opOK && !losesInfo); 20325ffd83dbSDimitry Andric IEEEFloat PEx = P; 20335ffd83dbSDimitry Andric fs = PEx.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 20345ffd83dbSDimitry Andric assert(fs == opOK && !losesInfo); 20355ffd83dbSDimitry Andric 20365ffd83dbSDimitry Andric // It is simpler to work with 2x instead of 0.5p, and we do not need to lose 20375ffd83dbSDimitry Andric // any fraction. 20385ffd83dbSDimitry Andric fs = VEx.add(VEx, rmNearestTiesToEven); 20395ffd83dbSDimitry Andric assert(fs == opOK); 20405ffd83dbSDimitry Andric 20415ffd83dbSDimitry Andric if (VEx.compare(PEx) == cmpGreaterThan) { 20425ffd83dbSDimitry Andric fs = subtract(P, rmNearestTiesToEven); 20435ffd83dbSDimitry Andric assert(fs == opOK); 20445ffd83dbSDimitry Andric 20455ffd83dbSDimitry Andric // Make VEx = this.add(this), but because we have different semantics, we do 20465ffd83dbSDimitry Andric // not want to `convert` again, so we just subtract PEx twice (which equals 20475ffd83dbSDimitry Andric // to the desired value). 20485ffd83dbSDimitry Andric fs = VEx.subtract(PEx, rmNearestTiesToEven); 20495ffd83dbSDimitry Andric assert(fs == opOK); 20505ffd83dbSDimitry Andric fs = VEx.subtract(PEx, rmNearestTiesToEven); 20515ffd83dbSDimitry Andric assert(fs == opOK); 20525ffd83dbSDimitry Andric 20535ffd83dbSDimitry Andric cmpResult result = VEx.compare(PEx); 20545ffd83dbSDimitry Andric if (result == cmpGreaterThan || result == cmpEqual) { 20555ffd83dbSDimitry Andric fs = subtract(P, rmNearestTiesToEven); 20565ffd83dbSDimitry Andric assert(fs == opOK); 20575ffd83dbSDimitry Andric } 20585ffd83dbSDimitry Andric } 2059*0b57cec5SDimitry Andric 2060*0b57cec5SDimitry Andric if (isZero()) 2061*0b57cec5SDimitry Andric sign = origSign; // IEEE754 requires this 20625ffd83dbSDimitry Andric else 20635ffd83dbSDimitry Andric sign ^= origSign; 2064*0b57cec5SDimitry Andric return fs; 2065*0b57cec5SDimitry Andric } 2066*0b57cec5SDimitry Andric 2067*0b57cec5SDimitry Andric /* Normalized llvm frem (C fmod). */ 2068*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) { 2069*0b57cec5SDimitry Andric opStatus fs; 2070*0b57cec5SDimitry Andric fs = modSpecials(rhs); 2071*0b57cec5SDimitry Andric unsigned int origSign = sign; 2072*0b57cec5SDimitry Andric 2073*0b57cec5SDimitry Andric while (isFiniteNonZero() && rhs.isFiniteNonZero() && 2074*0b57cec5SDimitry Andric compareAbsoluteValue(rhs) != cmpLessThan) { 2075bdd1243dSDimitry Andric int Exp = ilogb(*this) - ilogb(rhs); 2076bdd1243dSDimitry Andric IEEEFloat V = scalbn(rhs, Exp, rmNearestTiesToEven); 2077bdd1243dSDimitry Andric // V can overflow to NaN with fltNonfiniteBehavior::NanOnly, so explicitly 2078bdd1243dSDimitry Andric // check for it. 2079bdd1243dSDimitry Andric if (V.isNaN() || compareAbsoluteValue(V) == cmpLessThan) 2080bdd1243dSDimitry Andric V = scalbn(rhs, Exp - 1, rmNearestTiesToEven); 2081*0b57cec5SDimitry Andric V.sign = sign; 2082*0b57cec5SDimitry Andric 2083*0b57cec5SDimitry Andric fs = subtract(V, rmNearestTiesToEven); 2084*0b57cec5SDimitry Andric assert(fs==opOK); 2085*0b57cec5SDimitry Andric } 2086*0b57cec5SDimitry Andric if (isZero()) 2087*0b57cec5SDimitry Andric sign = origSign; // fmod requires this 2088*0b57cec5SDimitry Andric return fs; 2089*0b57cec5SDimitry Andric } 2090*0b57cec5SDimitry Andric 2091*0b57cec5SDimitry Andric /* Normalized fused-multiply-add. */ 2092*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::fusedMultiplyAdd(const IEEEFloat &multiplicand, 2093*0b57cec5SDimitry Andric const IEEEFloat &addend, 2094*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2095*0b57cec5SDimitry Andric opStatus fs; 2096*0b57cec5SDimitry Andric 2097*0b57cec5SDimitry Andric /* Post-multiplication sign, before addition. */ 2098*0b57cec5SDimitry Andric sign ^= multiplicand.sign; 2099*0b57cec5SDimitry Andric 2100*0b57cec5SDimitry Andric /* If and only if all arguments are normal do we need to do an 2101*0b57cec5SDimitry Andric extended-precision calculation. */ 2102*0b57cec5SDimitry Andric if (isFiniteNonZero() && 2103*0b57cec5SDimitry Andric multiplicand.isFiniteNonZero() && 2104*0b57cec5SDimitry Andric addend.isFinite()) { 2105*0b57cec5SDimitry Andric lostFraction lost_fraction; 2106*0b57cec5SDimitry Andric 2107480093f4SDimitry Andric lost_fraction = multiplySignificand(multiplicand, addend); 2108*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lost_fraction); 2109*0b57cec5SDimitry Andric if (lost_fraction != lfExactlyZero) 2110*0b57cec5SDimitry Andric fs = (opStatus) (fs | opInexact); 2111*0b57cec5SDimitry Andric 2112*0b57cec5SDimitry Andric /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a 2113*0b57cec5SDimitry Andric positive zero unless rounding to minus infinity, except that 2114*0b57cec5SDimitry Andric adding two like-signed zeroes gives that zero. */ 2115*0b57cec5SDimitry Andric if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign) 2116*0b57cec5SDimitry Andric sign = (rounding_mode == rmTowardNegative); 2117*0b57cec5SDimitry Andric } else { 2118*0b57cec5SDimitry Andric fs = multiplySpecials(multiplicand); 2119*0b57cec5SDimitry Andric 2120*0b57cec5SDimitry Andric /* FS can only be opOK or opInvalidOp. There is no more work 2121*0b57cec5SDimitry Andric to do in the latter case. The IEEE-754R standard says it is 2122*0b57cec5SDimitry Andric implementation-defined in this case whether, if ADDEND is a 2123*0b57cec5SDimitry Andric quiet NaN, we raise invalid op; this implementation does so. 2124*0b57cec5SDimitry Andric 2125*0b57cec5SDimitry Andric If we need to do the addition we can do so with normal 2126*0b57cec5SDimitry Andric precision. */ 2127*0b57cec5SDimitry Andric if (fs == opOK) 2128*0b57cec5SDimitry Andric fs = addOrSubtract(addend, rounding_mode, false); 2129*0b57cec5SDimitry Andric } 2130*0b57cec5SDimitry Andric 2131*0b57cec5SDimitry Andric return fs; 2132*0b57cec5SDimitry Andric } 2133*0b57cec5SDimitry Andric 21345ffd83dbSDimitry Andric /* Rounding-mode correct round to integral value. */ 2135*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) { 2136*0b57cec5SDimitry Andric opStatus fs; 2137*0b57cec5SDimitry Andric 21385ffd83dbSDimitry Andric if (isInfinity()) 21395ffd83dbSDimitry Andric // [IEEE Std 754-2008 6.1]: 21405ffd83dbSDimitry Andric // The behavior of infinity in floating-point arithmetic is derived from the 21415ffd83dbSDimitry Andric // limiting cases of real arithmetic with operands of arbitrarily 21425ffd83dbSDimitry Andric // large magnitude, when such a limit exists. 21435ffd83dbSDimitry Andric // ... 21445ffd83dbSDimitry Andric // Operations on infinite operands are usually exact and therefore signal no 21455ffd83dbSDimitry Andric // exceptions ... 21465ffd83dbSDimitry Andric return opOK; 21475ffd83dbSDimitry Andric 21485ffd83dbSDimitry Andric if (isNaN()) { 21495ffd83dbSDimitry Andric if (isSignaling()) { 21505ffd83dbSDimitry Andric // [IEEE Std 754-2008 6.2]: 21515ffd83dbSDimitry Andric // Under default exception handling, any operation signaling an invalid 21525ffd83dbSDimitry Andric // operation exception and for which a floating-point result is to be 21535ffd83dbSDimitry Andric // delivered shall deliver a quiet NaN. 21545ffd83dbSDimitry Andric makeQuiet(); 21555ffd83dbSDimitry Andric // [IEEE Std 754-2008 6.2]: 21565ffd83dbSDimitry Andric // Signaling NaNs shall be reserved operands that, under default exception 21575ffd83dbSDimitry Andric // handling, signal the invalid operation exception(see 7.2) for every 21585ffd83dbSDimitry Andric // general-computational and signaling-computational operation except for 21595ffd83dbSDimitry Andric // the conversions described in 5.12. 21605ffd83dbSDimitry Andric return opInvalidOp; 21615ffd83dbSDimitry Andric } else { 21625ffd83dbSDimitry Andric // [IEEE Std 754-2008 6.2]: 21635ffd83dbSDimitry Andric // For an operation with quiet NaN inputs, other than maximum and minimum 21645ffd83dbSDimitry Andric // operations, if a floating-point result is to be delivered the result 21655ffd83dbSDimitry Andric // shall be a quiet NaN which should be one of the input NaNs. 21665ffd83dbSDimitry Andric // ... 21675ffd83dbSDimitry Andric // Every general-computational and quiet-computational operation involving 21685ffd83dbSDimitry Andric // one or more input NaNs, none of them signaling, shall signal no 21695ffd83dbSDimitry Andric // exception, except fusedMultiplyAdd might signal the invalid operation 21705ffd83dbSDimitry Andric // exception(see 7.2). 21715ffd83dbSDimitry Andric return opOK; 21725ffd83dbSDimitry Andric } 21735ffd83dbSDimitry Andric } 21745ffd83dbSDimitry Andric 21755ffd83dbSDimitry Andric if (isZero()) { 21765ffd83dbSDimitry Andric // [IEEE Std 754-2008 6.3]: 21775ffd83dbSDimitry Andric // ... the sign of the result of conversions, the quantize operation, the 21785ffd83dbSDimitry Andric // roundToIntegral operations, and the roundToIntegralExact(see 5.3.1) is 21795ffd83dbSDimitry Andric // the sign of the first or only operand. 21805ffd83dbSDimitry Andric return opOK; 21815ffd83dbSDimitry Andric } 21825ffd83dbSDimitry Andric 2183*0b57cec5SDimitry Andric // If the exponent is large enough, we know that this value is already 2184*0b57cec5SDimitry Andric // integral, and the arithmetic below would potentially cause it to saturate 2185*0b57cec5SDimitry Andric // to +/-Inf. Bail out early instead. 21865ffd83dbSDimitry Andric if (exponent+1 >= (int)semanticsPrecision(*semantics)) 2187*0b57cec5SDimitry Andric return opOK; 2188*0b57cec5SDimitry Andric 2189*0b57cec5SDimitry Andric // The algorithm here is quite simple: we add 2^(p-1), where p is the 2190*0b57cec5SDimitry Andric // precision of our format, and then subtract it back off again. The choice 2191*0b57cec5SDimitry Andric // of rounding modes for the addition/subtraction determines the rounding mode 2192*0b57cec5SDimitry Andric // for our integral rounding as well. 2193*0b57cec5SDimitry Andric // NOTE: When the input value is negative, we do subtraction followed by 2194*0b57cec5SDimitry Andric // addition instead. 2195*0b57cec5SDimitry Andric APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1); 2196*0b57cec5SDimitry Andric IntegerConstant <<= semanticsPrecision(*semantics)-1; 2197*0b57cec5SDimitry Andric IEEEFloat MagicConstant(*semantics); 2198*0b57cec5SDimitry Andric fs = MagicConstant.convertFromAPInt(IntegerConstant, false, 2199*0b57cec5SDimitry Andric rmNearestTiesToEven); 22005ffd83dbSDimitry Andric assert(fs == opOK); 2201*0b57cec5SDimitry Andric MagicConstant.sign = sign; 2202*0b57cec5SDimitry Andric 22035ffd83dbSDimitry Andric // Preserve the input sign so that we can handle the case of zero result 22045ffd83dbSDimitry Andric // correctly. 2205*0b57cec5SDimitry Andric bool inputSign = isNegative(); 2206*0b57cec5SDimitry Andric 2207*0b57cec5SDimitry Andric fs = add(MagicConstant, rounding_mode); 2208*0b57cec5SDimitry Andric 22095ffd83dbSDimitry Andric // Current value and 'MagicConstant' are both integers, so the result of the 22105ffd83dbSDimitry Andric // subtraction is always exact according to Sterbenz' lemma. 22115ffd83dbSDimitry Andric subtract(MagicConstant, rounding_mode); 2212*0b57cec5SDimitry Andric 2213*0b57cec5SDimitry Andric // Restore the input sign. 2214*0b57cec5SDimitry Andric if (inputSign != isNegative()) 2215*0b57cec5SDimitry Andric changeSign(); 2216*0b57cec5SDimitry Andric 2217*0b57cec5SDimitry Andric return fs; 2218*0b57cec5SDimitry Andric } 2219*0b57cec5SDimitry Andric 2220*0b57cec5SDimitry Andric 2221*0b57cec5SDimitry Andric /* Comparison requires normalized numbers. */ 2222*0b57cec5SDimitry Andric IEEEFloat::cmpResult IEEEFloat::compare(const IEEEFloat &rhs) const { 2223*0b57cec5SDimitry Andric cmpResult result; 2224*0b57cec5SDimitry Andric 2225*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 2226*0b57cec5SDimitry Andric 2227*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 2228*0b57cec5SDimitry Andric default: 2229*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 2230*0b57cec5SDimitry Andric 2231*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 2232*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 2233*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 2234*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 2235*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 2236*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 2237*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 2238*0b57cec5SDimitry Andric return cmpUnordered; 2239*0b57cec5SDimitry Andric 2240*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 2241*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 2242*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 2243*0b57cec5SDimitry Andric if (sign) 2244*0b57cec5SDimitry Andric return cmpLessThan; 2245*0b57cec5SDimitry Andric else 2246*0b57cec5SDimitry Andric return cmpGreaterThan; 2247*0b57cec5SDimitry Andric 2248*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 2249*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 2250*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 2251*0b57cec5SDimitry Andric if (rhs.sign) 2252*0b57cec5SDimitry Andric return cmpGreaterThan; 2253*0b57cec5SDimitry Andric else 2254*0b57cec5SDimitry Andric return cmpLessThan; 2255*0b57cec5SDimitry Andric 2256*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 2257*0b57cec5SDimitry Andric if (sign == rhs.sign) 2258*0b57cec5SDimitry Andric return cmpEqual; 2259*0b57cec5SDimitry Andric else if (sign) 2260*0b57cec5SDimitry Andric return cmpLessThan; 2261*0b57cec5SDimitry Andric else 2262*0b57cec5SDimitry Andric return cmpGreaterThan; 2263*0b57cec5SDimitry Andric 2264*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 2265*0b57cec5SDimitry Andric return cmpEqual; 2266*0b57cec5SDimitry Andric 2267*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 2268*0b57cec5SDimitry Andric break; 2269*0b57cec5SDimitry Andric } 2270*0b57cec5SDimitry Andric 2271*0b57cec5SDimitry Andric /* Two normal numbers. Do they have the same sign? */ 2272*0b57cec5SDimitry Andric if (sign != rhs.sign) { 2273*0b57cec5SDimitry Andric if (sign) 2274*0b57cec5SDimitry Andric result = cmpLessThan; 2275*0b57cec5SDimitry Andric else 2276*0b57cec5SDimitry Andric result = cmpGreaterThan; 2277*0b57cec5SDimitry Andric } else { 2278*0b57cec5SDimitry Andric /* Compare absolute values; invert result if negative. */ 2279*0b57cec5SDimitry Andric result = compareAbsoluteValue(rhs); 2280*0b57cec5SDimitry Andric 2281*0b57cec5SDimitry Andric if (sign) { 2282*0b57cec5SDimitry Andric if (result == cmpLessThan) 2283*0b57cec5SDimitry Andric result = cmpGreaterThan; 2284*0b57cec5SDimitry Andric else if (result == cmpGreaterThan) 2285*0b57cec5SDimitry Andric result = cmpLessThan; 2286*0b57cec5SDimitry Andric } 2287*0b57cec5SDimitry Andric } 2288*0b57cec5SDimitry Andric 2289*0b57cec5SDimitry Andric return result; 2290*0b57cec5SDimitry Andric } 2291*0b57cec5SDimitry Andric 2292*0b57cec5SDimitry Andric /// IEEEFloat::convert - convert a value of one floating point type to another. 2293*0b57cec5SDimitry Andric /// The return value corresponds to the IEEE754 exceptions. *losesInfo 2294*0b57cec5SDimitry Andric /// records whether the transformation lost information, i.e. whether 2295*0b57cec5SDimitry Andric /// converting the result back to the original type will produce the 2296*0b57cec5SDimitry Andric /// original value (this is almost the same as return value==fsOK, but there 2297*0b57cec5SDimitry Andric /// are edge cases where this is not so). 2298*0b57cec5SDimitry Andric 2299*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics, 2300*0b57cec5SDimitry Andric roundingMode rounding_mode, 2301*0b57cec5SDimitry Andric bool *losesInfo) { 2302*0b57cec5SDimitry Andric lostFraction lostFraction; 2303*0b57cec5SDimitry Andric unsigned int newPartCount, oldPartCount; 2304*0b57cec5SDimitry Andric opStatus fs; 2305*0b57cec5SDimitry Andric int shift; 2306*0b57cec5SDimitry Andric const fltSemantics &fromSemantics = *semantics; 2307bdd1243dSDimitry Andric bool is_signaling = isSignaling(); 2308*0b57cec5SDimitry Andric 2309*0b57cec5SDimitry Andric lostFraction = lfExactlyZero; 2310*0b57cec5SDimitry Andric newPartCount = partCountForBits(toSemantics.precision + 1); 2311*0b57cec5SDimitry Andric oldPartCount = partCount(); 2312*0b57cec5SDimitry Andric shift = toSemantics.precision - fromSemantics.precision; 2313*0b57cec5SDimitry Andric 2314*0b57cec5SDimitry Andric bool X86SpecialNan = false; 2315*0b57cec5SDimitry Andric if (&fromSemantics == &semX87DoubleExtended && 2316*0b57cec5SDimitry Andric &toSemantics != &semX87DoubleExtended && category == fcNaN && 2317*0b57cec5SDimitry Andric (!(*significandParts() & 0x8000000000000000ULL) || 2318*0b57cec5SDimitry Andric !(*significandParts() & 0x4000000000000000ULL))) { 2319*0b57cec5SDimitry Andric // x86 has some unusual NaNs which cannot be represented in any other 2320*0b57cec5SDimitry Andric // format; note them here. 2321*0b57cec5SDimitry Andric X86SpecialNan = true; 2322*0b57cec5SDimitry Andric } 2323*0b57cec5SDimitry Andric 2324*0b57cec5SDimitry Andric // If this is a truncation of a denormal number, and the target semantics 2325*0b57cec5SDimitry Andric // has larger exponent range than the source semantics (this can happen 2326*0b57cec5SDimitry Andric // when truncating from PowerPC double-double to double format), the 2327*0b57cec5SDimitry Andric // right shift could lose result mantissa bits. Adjust exponent instead 2328*0b57cec5SDimitry Andric // of performing excessive shift. 232981ad6265SDimitry Andric // Also do a similar trick in case shifting denormal would produce zero 233081ad6265SDimitry Andric // significand as this case isn't handled correctly by normalize. 2331*0b57cec5SDimitry Andric if (shift < 0 && isFiniteNonZero()) { 233281ad6265SDimitry Andric int omsb = significandMSB() + 1; 233381ad6265SDimitry Andric int exponentChange = omsb - fromSemantics.precision; 2334*0b57cec5SDimitry Andric if (exponent + exponentChange < toSemantics.minExponent) 2335*0b57cec5SDimitry Andric exponentChange = toSemantics.minExponent - exponent; 2336*0b57cec5SDimitry Andric if (exponentChange < shift) 2337*0b57cec5SDimitry Andric exponentChange = shift; 2338*0b57cec5SDimitry Andric if (exponentChange < 0) { 2339*0b57cec5SDimitry Andric shift -= exponentChange; 2340*0b57cec5SDimitry Andric exponent += exponentChange; 234181ad6265SDimitry Andric } else if (omsb <= -shift) { 234281ad6265SDimitry Andric exponentChange = omsb + shift - 1; // leave at least one bit set 234381ad6265SDimitry Andric shift -= exponentChange; 234481ad6265SDimitry Andric exponent += exponentChange; 2345*0b57cec5SDimitry Andric } 2346*0b57cec5SDimitry Andric } 2347*0b57cec5SDimitry Andric 2348*0b57cec5SDimitry Andric // If this is a truncation, perform the shift before we narrow the storage. 2349bdd1243dSDimitry Andric if (shift < 0 && (isFiniteNonZero() || 2350bdd1243dSDimitry Andric (category == fcNaN && semantics->nonFiniteBehavior != 2351bdd1243dSDimitry Andric fltNonfiniteBehavior::NanOnly))) 2352*0b57cec5SDimitry Andric lostFraction = shiftRight(significandParts(), oldPartCount, -shift); 2353*0b57cec5SDimitry Andric 2354*0b57cec5SDimitry Andric // Fix the storage so it can hold to new value. 2355*0b57cec5SDimitry Andric if (newPartCount > oldPartCount) { 2356*0b57cec5SDimitry Andric // The new type requires more storage; make it available. 2357*0b57cec5SDimitry Andric integerPart *newParts; 2358*0b57cec5SDimitry Andric newParts = new integerPart[newPartCount]; 2359*0b57cec5SDimitry Andric APInt::tcSet(newParts, 0, newPartCount); 2360*0b57cec5SDimitry Andric if (isFiniteNonZero() || category==fcNaN) 2361*0b57cec5SDimitry Andric APInt::tcAssign(newParts, significandParts(), oldPartCount); 2362*0b57cec5SDimitry Andric freeSignificand(); 2363*0b57cec5SDimitry Andric significand.parts = newParts; 2364*0b57cec5SDimitry Andric } else if (newPartCount == 1 && oldPartCount != 1) { 2365*0b57cec5SDimitry Andric // Switch to built-in storage for a single part. 2366*0b57cec5SDimitry Andric integerPart newPart = 0; 2367*0b57cec5SDimitry Andric if (isFiniteNonZero() || category==fcNaN) 2368*0b57cec5SDimitry Andric newPart = significandParts()[0]; 2369*0b57cec5SDimitry Andric freeSignificand(); 2370*0b57cec5SDimitry Andric significand.part = newPart; 2371*0b57cec5SDimitry Andric } 2372*0b57cec5SDimitry Andric 2373*0b57cec5SDimitry Andric // Now that we have the right storage, switch the semantics. 2374*0b57cec5SDimitry Andric semantics = &toSemantics; 2375*0b57cec5SDimitry Andric 2376*0b57cec5SDimitry Andric // If this is an extension, perform the shift now that the storage is 2377*0b57cec5SDimitry Andric // available. 2378*0b57cec5SDimitry Andric if (shift > 0 && (isFiniteNonZero() || category==fcNaN)) 2379*0b57cec5SDimitry Andric APInt::tcShiftLeft(significandParts(), newPartCount, shift); 2380*0b57cec5SDimitry Andric 2381*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 2382*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lostFraction); 2383*0b57cec5SDimitry Andric *losesInfo = (fs != opOK); 2384*0b57cec5SDimitry Andric } else if (category == fcNaN) { 2385bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { 2386bdd1243dSDimitry Andric *losesInfo = 2387bdd1243dSDimitry Andric fromSemantics.nonFiniteBehavior != fltNonfiniteBehavior::NanOnly; 2388bdd1243dSDimitry Andric makeNaN(false, sign); 2389bdd1243dSDimitry Andric return is_signaling ? opInvalidOp : opOK; 2390bdd1243dSDimitry Andric } 2391bdd1243dSDimitry Andric 2392*0b57cec5SDimitry Andric *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan; 2393*0b57cec5SDimitry Andric 2394*0b57cec5SDimitry Andric // For x87 extended precision, we want to make a NaN, not a special NaN if 2395*0b57cec5SDimitry Andric // the input wasn't special either. 2396*0b57cec5SDimitry Andric if (!X86SpecialNan && semantics == &semX87DoubleExtended) 2397*0b57cec5SDimitry Andric APInt::tcSetBit(significandParts(), semantics->precision - 1); 2398*0b57cec5SDimitry Andric 2399e8d8bef9SDimitry Andric // Convert of sNaN creates qNaN and raises an exception (invalid op). 2400e8d8bef9SDimitry Andric // This also guarantees that a sNaN does not become Inf on a truncation 2401e8d8bef9SDimitry Andric // that loses all payload bits. 2402bdd1243dSDimitry Andric if (is_signaling) { 2403e8d8bef9SDimitry Andric makeQuiet(); 2404e8d8bef9SDimitry Andric fs = opInvalidOp; 2405e8d8bef9SDimitry Andric } else { 2406*0b57cec5SDimitry Andric fs = opOK; 2407e8d8bef9SDimitry Andric } 2408bdd1243dSDimitry Andric } else if (category == fcInfinity && 2409bdd1243dSDimitry Andric semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { 2410bdd1243dSDimitry Andric makeNaN(false, sign); 2411bdd1243dSDimitry Andric *losesInfo = true; 2412bdd1243dSDimitry Andric fs = opInexact; 2413*0b57cec5SDimitry Andric } else { 2414*0b57cec5SDimitry Andric *losesInfo = false; 2415*0b57cec5SDimitry Andric fs = opOK; 2416*0b57cec5SDimitry Andric } 2417*0b57cec5SDimitry Andric 2418*0b57cec5SDimitry Andric return fs; 2419*0b57cec5SDimitry Andric } 2420*0b57cec5SDimitry Andric 2421*0b57cec5SDimitry Andric /* Convert a floating point number to an integer according to the 2422*0b57cec5SDimitry Andric rounding mode. If the rounded integer value is out of range this 2423*0b57cec5SDimitry Andric returns an invalid operation exception and the contents of the 2424*0b57cec5SDimitry Andric destination parts are unspecified. If the rounded value is in 2425*0b57cec5SDimitry Andric range but the floating point number is not the exact integer, the C 2426*0b57cec5SDimitry Andric standard doesn't require an inexact exception to be raised. IEEE 2427*0b57cec5SDimitry Andric 854 does require it so we do that. 2428*0b57cec5SDimitry Andric 2429*0b57cec5SDimitry Andric Note that for conversions to integer type the C standard requires 2430*0b57cec5SDimitry Andric round-to-zero to always be used. */ 2431*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::convertToSignExtendedInteger( 2432*0b57cec5SDimitry Andric MutableArrayRef<integerPart> parts, unsigned int width, bool isSigned, 2433*0b57cec5SDimitry Andric roundingMode rounding_mode, bool *isExact) const { 2434*0b57cec5SDimitry Andric lostFraction lost_fraction; 2435*0b57cec5SDimitry Andric const integerPart *src; 2436*0b57cec5SDimitry Andric unsigned int dstPartsCount, truncatedBits; 2437*0b57cec5SDimitry Andric 2438*0b57cec5SDimitry Andric *isExact = false; 2439*0b57cec5SDimitry Andric 2440*0b57cec5SDimitry Andric /* Handle the three special cases first. */ 2441*0b57cec5SDimitry Andric if (category == fcInfinity || category == fcNaN) 2442*0b57cec5SDimitry Andric return opInvalidOp; 2443*0b57cec5SDimitry Andric 2444*0b57cec5SDimitry Andric dstPartsCount = partCountForBits(width); 2445*0b57cec5SDimitry Andric assert(dstPartsCount <= parts.size() && "Integer too big"); 2446*0b57cec5SDimitry Andric 2447*0b57cec5SDimitry Andric if (category == fcZero) { 2448*0b57cec5SDimitry Andric APInt::tcSet(parts.data(), 0, dstPartsCount); 2449*0b57cec5SDimitry Andric // Negative zero can't be represented as an int. 2450*0b57cec5SDimitry Andric *isExact = !sign; 2451*0b57cec5SDimitry Andric return opOK; 2452*0b57cec5SDimitry Andric } 2453*0b57cec5SDimitry Andric 2454*0b57cec5SDimitry Andric src = significandParts(); 2455*0b57cec5SDimitry Andric 2456*0b57cec5SDimitry Andric /* Step 1: place our absolute value, with any fraction truncated, in 2457*0b57cec5SDimitry Andric the destination. */ 2458*0b57cec5SDimitry Andric if (exponent < 0) { 2459*0b57cec5SDimitry Andric /* Our absolute value is less than one; truncate everything. */ 2460*0b57cec5SDimitry Andric APInt::tcSet(parts.data(), 0, dstPartsCount); 2461*0b57cec5SDimitry Andric /* For exponent -1 the integer bit represents .5, look at that. 2462*0b57cec5SDimitry Andric For smaller exponents leftmost truncated bit is 0. */ 2463*0b57cec5SDimitry Andric truncatedBits = semantics->precision -1U - exponent; 2464*0b57cec5SDimitry Andric } else { 2465*0b57cec5SDimitry Andric /* We want the most significant (exponent + 1) bits; the rest are 2466*0b57cec5SDimitry Andric truncated. */ 2467*0b57cec5SDimitry Andric unsigned int bits = exponent + 1U; 2468*0b57cec5SDimitry Andric 2469*0b57cec5SDimitry Andric /* Hopelessly large in magnitude? */ 2470*0b57cec5SDimitry Andric if (bits > width) 2471*0b57cec5SDimitry Andric return opInvalidOp; 2472*0b57cec5SDimitry Andric 2473*0b57cec5SDimitry Andric if (bits < semantics->precision) { 2474*0b57cec5SDimitry Andric /* We truncate (semantics->precision - bits) bits. */ 2475*0b57cec5SDimitry Andric truncatedBits = semantics->precision - bits; 2476*0b57cec5SDimitry Andric APInt::tcExtract(parts.data(), dstPartsCount, src, bits, truncatedBits); 2477*0b57cec5SDimitry Andric } else { 2478*0b57cec5SDimitry Andric /* We want at least as many bits as are available. */ 2479*0b57cec5SDimitry Andric APInt::tcExtract(parts.data(), dstPartsCount, src, semantics->precision, 2480*0b57cec5SDimitry Andric 0); 2481*0b57cec5SDimitry Andric APInt::tcShiftLeft(parts.data(), dstPartsCount, 2482*0b57cec5SDimitry Andric bits - semantics->precision); 2483*0b57cec5SDimitry Andric truncatedBits = 0; 2484*0b57cec5SDimitry Andric } 2485*0b57cec5SDimitry Andric } 2486*0b57cec5SDimitry Andric 2487*0b57cec5SDimitry Andric /* Step 2: work out any lost fraction, and increment the absolute 2488*0b57cec5SDimitry Andric value if we would round away from zero. */ 2489*0b57cec5SDimitry Andric if (truncatedBits) { 2490*0b57cec5SDimitry Andric lost_fraction = lostFractionThroughTruncation(src, partCount(), 2491*0b57cec5SDimitry Andric truncatedBits); 2492*0b57cec5SDimitry Andric if (lost_fraction != lfExactlyZero && 2493*0b57cec5SDimitry Andric roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) { 2494*0b57cec5SDimitry Andric if (APInt::tcIncrement(parts.data(), dstPartsCount)) 2495*0b57cec5SDimitry Andric return opInvalidOp; /* Overflow. */ 2496*0b57cec5SDimitry Andric } 2497*0b57cec5SDimitry Andric } else { 2498*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 2499*0b57cec5SDimitry Andric } 2500*0b57cec5SDimitry Andric 2501*0b57cec5SDimitry Andric /* Step 3: check if we fit in the destination. */ 2502*0b57cec5SDimitry Andric unsigned int omsb = APInt::tcMSB(parts.data(), dstPartsCount) + 1; 2503*0b57cec5SDimitry Andric 2504*0b57cec5SDimitry Andric if (sign) { 2505*0b57cec5SDimitry Andric if (!isSigned) { 2506*0b57cec5SDimitry Andric /* Negative numbers cannot be represented as unsigned. */ 2507*0b57cec5SDimitry Andric if (omsb != 0) 2508*0b57cec5SDimitry Andric return opInvalidOp; 2509*0b57cec5SDimitry Andric } else { 2510*0b57cec5SDimitry Andric /* It takes omsb bits to represent the unsigned integer value. 2511*0b57cec5SDimitry Andric We lose a bit for the sign, but care is needed as the 2512*0b57cec5SDimitry Andric maximally negative integer is a special case. */ 2513*0b57cec5SDimitry Andric if (omsb == width && 2514*0b57cec5SDimitry Andric APInt::tcLSB(parts.data(), dstPartsCount) + 1 != omsb) 2515*0b57cec5SDimitry Andric return opInvalidOp; 2516*0b57cec5SDimitry Andric 2517*0b57cec5SDimitry Andric /* This case can happen because of rounding. */ 2518*0b57cec5SDimitry Andric if (omsb > width) 2519*0b57cec5SDimitry Andric return opInvalidOp; 2520*0b57cec5SDimitry Andric } 2521*0b57cec5SDimitry Andric 2522*0b57cec5SDimitry Andric APInt::tcNegate (parts.data(), dstPartsCount); 2523*0b57cec5SDimitry Andric } else { 2524*0b57cec5SDimitry Andric if (omsb >= width + !isSigned) 2525*0b57cec5SDimitry Andric return opInvalidOp; 2526*0b57cec5SDimitry Andric } 2527*0b57cec5SDimitry Andric 2528*0b57cec5SDimitry Andric if (lost_fraction == lfExactlyZero) { 2529*0b57cec5SDimitry Andric *isExact = true; 2530*0b57cec5SDimitry Andric return opOK; 2531*0b57cec5SDimitry Andric } else 2532*0b57cec5SDimitry Andric return opInexact; 2533*0b57cec5SDimitry Andric } 2534*0b57cec5SDimitry Andric 2535*0b57cec5SDimitry Andric /* Same as convertToSignExtendedInteger, except we provide 2536*0b57cec5SDimitry Andric deterministic values in case of an invalid operation exception, 2537*0b57cec5SDimitry Andric namely zero for NaNs and the minimal or maximal value respectively 2538*0b57cec5SDimitry Andric for underflow or overflow. 2539*0b57cec5SDimitry Andric The *isExact output tells whether the result is exact, in the sense 2540*0b57cec5SDimitry Andric that converting it back to the original floating point type produces 2541*0b57cec5SDimitry Andric the original value. This is almost equivalent to result==opOK, 2542*0b57cec5SDimitry Andric except for negative zeroes. 2543*0b57cec5SDimitry Andric */ 2544*0b57cec5SDimitry Andric IEEEFloat::opStatus 2545*0b57cec5SDimitry Andric IEEEFloat::convertToInteger(MutableArrayRef<integerPart> parts, 2546*0b57cec5SDimitry Andric unsigned int width, bool isSigned, 2547*0b57cec5SDimitry Andric roundingMode rounding_mode, bool *isExact) const { 2548*0b57cec5SDimitry Andric opStatus fs; 2549*0b57cec5SDimitry Andric 2550*0b57cec5SDimitry Andric fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode, 2551*0b57cec5SDimitry Andric isExact); 2552*0b57cec5SDimitry Andric 2553*0b57cec5SDimitry Andric if (fs == opInvalidOp) { 2554*0b57cec5SDimitry Andric unsigned int bits, dstPartsCount; 2555*0b57cec5SDimitry Andric 2556*0b57cec5SDimitry Andric dstPartsCount = partCountForBits(width); 2557*0b57cec5SDimitry Andric assert(dstPartsCount <= parts.size() && "Integer too big"); 2558*0b57cec5SDimitry Andric 2559*0b57cec5SDimitry Andric if (category == fcNaN) 2560*0b57cec5SDimitry Andric bits = 0; 2561*0b57cec5SDimitry Andric else if (sign) 2562*0b57cec5SDimitry Andric bits = isSigned; 2563*0b57cec5SDimitry Andric else 2564*0b57cec5SDimitry Andric bits = width - isSigned; 2565*0b57cec5SDimitry Andric 2566349cc55cSDimitry Andric tcSetLeastSignificantBits(parts.data(), dstPartsCount, bits); 2567*0b57cec5SDimitry Andric if (sign && isSigned) 2568*0b57cec5SDimitry Andric APInt::tcShiftLeft(parts.data(), dstPartsCount, width - 1); 2569*0b57cec5SDimitry Andric } 2570*0b57cec5SDimitry Andric 2571*0b57cec5SDimitry Andric return fs; 2572*0b57cec5SDimitry Andric } 2573*0b57cec5SDimitry Andric 2574*0b57cec5SDimitry Andric /* Convert an unsigned integer SRC to a floating point number, 2575*0b57cec5SDimitry Andric rounding according to ROUNDING_MODE. The sign of the floating 2576*0b57cec5SDimitry Andric point number is not modified. */ 2577*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::convertFromUnsignedParts( 2578*0b57cec5SDimitry Andric const integerPart *src, unsigned int srcCount, roundingMode rounding_mode) { 2579*0b57cec5SDimitry Andric unsigned int omsb, precision, dstCount; 2580*0b57cec5SDimitry Andric integerPart *dst; 2581*0b57cec5SDimitry Andric lostFraction lost_fraction; 2582*0b57cec5SDimitry Andric 2583*0b57cec5SDimitry Andric category = fcNormal; 2584*0b57cec5SDimitry Andric omsb = APInt::tcMSB(src, srcCount) + 1; 2585*0b57cec5SDimitry Andric dst = significandParts(); 2586*0b57cec5SDimitry Andric dstCount = partCount(); 2587*0b57cec5SDimitry Andric precision = semantics->precision; 2588*0b57cec5SDimitry Andric 2589*0b57cec5SDimitry Andric /* We want the most significant PRECISION bits of SRC. There may not 2590*0b57cec5SDimitry Andric be that many; extract what we can. */ 2591*0b57cec5SDimitry Andric if (precision <= omsb) { 2592*0b57cec5SDimitry Andric exponent = omsb - 1; 2593*0b57cec5SDimitry Andric lost_fraction = lostFractionThroughTruncation(src, srcCount, 2594*0b57cec5SDimitry Andric omsb - precision); 2595*0b57cec5SDimitry Andric APInt::tcExtract(dst, dstCount, src, precision, omsb - precision); 2596*0b57cec5SDimitry Andric } else { 2597*0b57cec5SDimitry Andric exponent = precision - 1; 2598*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 2599*0b57cec5SDimitry Andric APInt::tcExtract(dst, dstCount, src, omsb, 0); 2600*0b57cec5SDimitry Andric } 2601*0b57cec5SDimitry Andric 2602*0b57cec5SDimitry Andric return normalize(rounding_mode, lost_fraction); 2603*0b57cec5SDimitry Andric } 2604*0b57cec5SDimitry Andric 2605*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::convertFromAPInt(const APInt &Val, bool isSigned, 2606*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2607*0b57cec5SDimitry Andric unsigned int partCount = Val.getNumWords(); 2608*0b57cec5SDimitry Andric APInt api = Val; 2609*0b57cec5SDimitry Andric 2610*0b57cec5SDimitry Andric sign = false; 2611*0b57cec5SDimitry Andric if (isSigned && api.isNegative()) { 2612*0b57cec5SDimitry Andric sign = true; 2613*0b57cec5SDimitry Andric api = -api; 2614*0b57cec5SDimitry Andric } 2615*0b57cec5SDimitry Andric 2616*0b57cec5SDimitry Andric return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); 2617*0b57cec5SDimitry Andric } 2618*0b57cec5SDimitry Andric 2619*0b57cec5SDimitry Andric /* Convert a two's complement integer SRC to a floating point number, 2620*0b57cec5SDimitry Andric rounding according to ROUNDING_MODE. ISSIGNED is true if the 2621*0b57cec5SDimitry Andric integer is signed, in which case it must be sign-extended. */ 2622*0b57cec5SDimitry Andric IEEEFloat::opStatus 2623*0b57cec5SDimitry Andric IEEEFloat::convertFromSignExtendedInteger(const integerPart *src, 2624*0b57cec5SDimitry Andric unsigned int srcCount, bool isSigned, 2625*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2626*0b57cec5SDimitry Andric opStatus status; 2627*0b57cec5SDimitry Andric 2628*0b57cec5SDimitry Andric if (isSigned && 2629*0b57cec5SDimitry Andric APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) { 2630*0b57cec5SDimitry Andric integerPart *copy; 2631*0b57cec5SDimitry Andric 2632*0b57cec5SDimitry Andric /* If we're signed and negative negate a copy. */ 2633*0b57cec5SDimitry Andric sign = true; 2634*0b57cec5SDimitry Andric copy = new integerPart[srcCount]; 2635*0b57cec5SDimitry Andric APInt::tcAssign(copy, src, srcCount); 2636*0b57cec5SDimitry Andric APInt::tcNegate(copy, srcCount); 2637*0b57cec5SDimitry Andric status = convertFromUnsignedParts(copy, srcCount, rounding_mode); 2638*0b57cec5SDimitry Andric delete [] copy; 2639*0b57cec5SDimitry Andric } else { 2640*0b57cec5SDimitry Andric sign = false; 2641*0b57cec5SDimitry Andric status = convertFromUnsignedParts(src, srcCount, rounding_mode); 2642*0b57cec5SDimitry Andric } 2643*0b57cec5SDimitry Andric 2644*0b57cec5SDimitry Andric return status; 2645*0b57cec5SDimitry Andric } 2646*0b57cec5SDimitry Andric 2647*0b57cec5SDimitry Andric /* FIXME: should this just take a const APInt reference? */ 2648*0b57cec5SDimitry Andric IEEEFloat::opStatus 2649*0b57cec5SDimitry Andric IEEEFloat::convertFromZeroExtendedInteger(const integerPart *parts, 2650*0b57cec5SDimitry Andric unsigned int width, bool isSigned, 2651*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2652*0b57cec5SDimitry Andric unsigned int partCount = partCountForBits(width); 2653bdd1243dSDimitry Andric APInt api = APInt(width, ArrayRef(parts, partCount)); 2654*0b57cec5SDimitry Andric 2655*0b57cec5SDimitry Andric sign = false; 2656*0b57cec5SDimitry Andric if (isSigned && APInt::tcExtractBit(parts, width - 1)) { 2657*0b57cec5SDimitry Andric sign = true; 2658*0b57cec5SDimitry Andric api = -api; 2659*0b57cec5SDimitry Andric } 2660*0b57cec5SDimitry Andric 2661*0b57cec5SDimitry Andric return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); 2662*0b57cec5SDimitry Andric } 2663*0b57cec5SDimitry Andric 2664480093f4SDimitry Andric Expected<IEEEFloat::opStatus> 2665*0b57cec5SDimitry Andric IEEEFloat::convertFromHexadecimalString(StringRef s, 2666*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2667*0b57cec5SDimitry Andric lostFraction lost_fraction = lfExactlyZero; 2668*0b57cec5SDimitry Andric 2669*0b57cec5SDimitry Andric category = fcNormal; 2670*0b57cec5SDimitry Andric zeroSignificand(); 2671*0b57cec5SDimitry Andric exponent = 0; 2672*0b57cec5SDimitry Andric 2673*0b57cec5SDimitry Andric integerPart *significand = significandParts(); 2674*0b57cec5SDimitry Andric unsigned partsCount = partCount(); 2675*0b57cec5SDimitry Andric unsigned bitPos = partsCount * integerPartWidth; 2676*0b57cec5SDimitry Andric bool computedTrailingFraction = false; 2677*0b57cec5SDimitry Andric 2678*0b57cec5SDimitry Andric // Skip leading zeroes and any (hexa)decimal point. 2679*0b57cec5SDimitry Andric StringRef::iterator begin = s.begin(); 2680*0b57cec5SDimitry Andric StringRef::iterator end = s.end(); 2681*0b57cec5SDimitry Andric StringRef::iterator dot; 2682480093f4SDimitry Andric auto PtrOrErr = skipLeadingZeroesAndAnyDot(begin, end, &dot); 2683480093f4SDimitry Andric if (!PtrOrErr) 2684480093f4SDimitry Andric return PtrOrErr.takeError(); 2685480093f4SDimitry Andric StringRef::iterator p = *PtrOrErr; 2686*0b57cec5SDimitry Andric StringRef::iterator firstSignificantDigit = p; 2687*0b57cec5SDimitry Andric 2688*0b57cec5SDimitry Andric while (p != end) { 2689*0b57cec5SDimitry Andric integerPart hex_value; 2690*0b57cec5SDimitry Andric 2691*0b57cec5SDimitry Andric if (*p == '.') { 2692480093f4SDimitry Andric if (dot != end) 2693480093f4SDimitry Andric return createError("String contains multiple dots"); 2694*0b57cec5SDimitry Andric dot = p++; 2695*0b57cec5SDimitry Andric continue; 2696*0b57cec5SDimitry Andric } 2697*0b57cec5SDimitry Andric 2698*0b57cec5SDimitry Andric hex_value = hexDigitValue(*p); 2699*0b57cec5SDimitry Andric if (hex_value == -1U) 2700*0b57cec5SDimitry Andric break; 2701*0b57cec5SDimitry Andric 2702*0b57cec5SDimitry Andric p++; 2703*0b57cec5SDimitry Andric 2704*0b57cec5SDimitry Andric // Store the number while we have space. 2705*0b57cec5SDimitry Andric if (bitPos) { 2706*0b57cec5SDimitry Andric bitPos -= 4; 2707*0b57cec5SDimitry Andric hex_value <<= bitPos % integerPartWidth; 2708*0b57cec5SDimitry Andric significand[bitPos / integerPartWidth] |= hex_value; 2709*0b57cec5SDimitry Andric } else if (!computedTrailingFraction) { 2710480093f4SDimitry Andric auto FractOrErr = trailingHexadecimalFraction(p, end, hex_value); 2711480093f4SDimitry Andric if (!FractOrErr) 2712480093f4SDimitry Andric return FractOrErr.takeError(); 2713480093f4SDimitry Andric lost_fraction = *FractOrErr; 2714*0b57cec5SDimitry Andric computedTrailingFraction = true; 2715*0b57cec5SDimitry Andric } 2716*0b57cec5SDimitry Andric } 2717*0b57cec5SDimitry Andric 2718*0b57cec5SDimitry Andric /* Hex floats require an exponent but not a hexadecimal point. */ 2719480093f4SDimitry Andric if (p == end) 2720480093f4SDimitry Andric return createError("Hex strings require an exponent"); 2721480093f4SDimitry Andric if (*p != 'p' && *p != 'P') 2722480093f4SDimitry Andric return createError("Invalid character in significand"); 2723480093f4SDimitry Andric if (p == begin) 2724480093f4SDimitry Andric return createError("Significand has no digits"); 2725480093f4SDimitry Andric if (dot != end && p - begin == 1) 2726480093f4SDimitry Andric return createError("Significand has no digits"); 2727*0b57cec5SDimitry Andric 2728*0b57cec5SDimitry Andric /* Ignore the exponent if we are zero. */ 2729*0b57cec5SDimitry Andric if (p != firstSignificantDigit) { 2730*0b57cec5SDimitry Andric int expAdjustment; 2731*0b57cec5SDimitry Andric 2732*0b57cec5SDimitry Andric /* Implicit hexadecimal point? */ 2733*0b57cec5SDimitry Andric if (dot == end) 2734*0b57cec5SDimitry Andric dot = p; 2735*0b57cec5SDimitry Andric 2736*0b57cec5SDimitry Andric /* Calculate the exponent adjustment implicit in the number of 2737*0b57cec5SDimitry Andric significant digits. */ 2738*0b57cec5SDimitry Andric expAdjustment = static_cast<int>(dot - firstSignificantDigit); 2739*0b57cec5SDimitry Andric if (expAdjustment < 0) 2740*0b57cec5SDimitry Andric expAdjustment++; 2741*0b57cec5SDimitry Andric expAdjustment = expAdjustment * 4 - 1; 2742*0b57cec5SDimitry Andric 2743*0b57cec5SDimitry Andric /* Adjust for writing the significand starting at the most 2744*0b57cec5SDimitry Andric significant nibble. */ 2745*0b57cec5SDimitry Andric expAdjustment += semantics->precision; 2746*0b57cec5SDimitry Andric expAdjustment -= partsCount * integerPartWidth; 2747*0b57cec5SDimitry Andric 2748*0b57cec5SDimitry Andric /* Adjust for the given exponent. */ 2749480093f4SDimitry Andric auto ExpOrErr = totalExponent(p + 1, end, expAdjustment); 2750480093f4SDimitry Andric if (!ExpOrErr) 2751480093f4SDimitry Andric return ExpOrErr.takeError(); 2752480093f4SDimitry Andric exponent = *ExpOrErr; 2753*0b57cec5SDimitry Andric } 2754*0b57cec5SDimitry Andric 2755*0b57cec5SDimitry Andric return normalize(rounding_mode, lost_fraction); 2756*0b57cec5SDimitry Andric } 2757*0b57cec5SDimitry Andric 2758*0b57cec5SDimitry Andric IEEEFloat::opStatus 2759*0b57cec5SDimitry Andric IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts, 2760*0b57cec5SDimitry Andric unsigned sigPartCount, int exp, 2761*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2762*0b57cec5SDimitry Andric unsigned int parts, pow5PartCount; 2763*0b57cec5SDimitry Andric fltSemantics calcSemantics = { 32767, -32767, 0, 0 }; 2764*0b57cec5SDimitry Andric integerPart pow5Parts[maxPowerOfFiveParts]; 2765*0b57cec5SDimitry Andric bool isNearest; 2766*0b57cec5SDimitry Andric 2767*0b57cec5SDimitry Andric isNearest = (rounding_mode == rmNearestTiesToEven || 2768*0b57cec5SDimitry Andric rounding_mode == rmNearestTiesToAway); 2769*0b57cec5SDimitry Andric 2770*0b57cec5SDimitry Andric parts = partCountForBits(semantics->precision + 11); 2771*0b57cec5SDimitry Andric 2772*0b57cec5SDimitry Andric /* Calculate pow(5, abs(exp)). */ 2773*0b57cec5SDimitry Andric pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp); 2774*0b57cec5SDimitry Andric 2775*0b57cec5SDimitry Andric for (;; parts *= 2) { 2776*0b57cec5SDimitry Andric opStatus sigStatus, powStatus; 2777*0b57cec5SDimitry Andric unsigned int excessPrecision, truncatedBits; 2778*0b57cec5SDimitry Andric 2779*0b57cec5SDimitry Andric calcSemantics.precision = parts * integerPartWidth - 1; 2780*0b57cec5SDimitry Andric excessPrecision = calcSemantics.precision - semantics->precision; 2781*0b57cec5SDimitry Andric truncatedBits = excessPrecision; 2782*0b57cec5SDimitry Andric 2783*0b57cec5SDimitry Andric IEEEFloat decSig(calcSemantics, uninitialized); 2784*0b57cec5SDimitry Andric decSig.makeZero(sign); 2785*0b57cec5SDimitry Andric IEEEFloat pow5(calcSemantics); 2786*0b57cec5SDimitry Andric 2787*0b57cec5SDimitry Andric sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount, 2788*0b57cec5SDimitry Andric rmNearestTiesToEven); 2789*0b57cec5SDimitry Andric powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount, 2790*0b57cec5SDimitry Andric rmNearestTiesToEven); 2791*0b57cec5SDimitry Andric /* Add exp, as 10^n = 5^n * 2^n. */ 2792*0b57cec5SDimitry Andric decSig.exponent += exp; 2793*0b57cec5SDimitry Andric 2794*0b57cec5SDimitry Andric lostFraction calcLostFraction; 2795*0b57cec5SDimitry Andric integerPart HUerr, HUdistance; 2796*0b57cec5SDimitry Andric unsigned int powHUerr; 2797*0b57cec5SDimitry Andric 2798*0b57cec5SDimitry Andric if (exp >= 0) { 2799*0b57cec5SDimitry Andric /* multiplySignificand leaves the precision-th bit set to 1. */ 2800480093f4SDimitry Andric calcLostFraction = decSig.multiplySignificand(pow5); 2801*0b57cec5SDimitry Andric powHUerr = powStatus != opOK; 2802*0b57cec5SDimitry Andric } else { 2803*0b57cec5SDimitry Andric calcLostFraction = decSig.divideSignificand(pow5); 2804*0b57cec5SDimitry Andric /* Denormal numbers have less precision. */ 2805*0b57cec5SDimitry Andric if (decSig.exponent < semantics->minExponent) { 2806*0b57cec5SDimitry Andric excessPrecision += (semantics->minExponent - decSig.exponent); 2807*0b57cec5SDimitry Andric truncatedBits = excessPrecision; 2808*0b57cec5SDimitry Andric if (excessPrecision > calcSemantics.precision) 2809*0b57cec5SDimitry Andric excessPrecision = calcSemantics.precision; 2810*0b57cec5SDimitry Andric } 2811*0b57cec5SDimitry Andric /* Extra half-ulp lost in reciprocal of exponent. */ 2812*0b57cec5SDimitry Andric powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2; 2813*0b57cec5SDimitry Andric } 2814*0b57cec5SDimitry Andric 2815*0b57cec5SDimitry Andric /* Both multiplySignificand and divideSignificand return the 2816*0b57cec5SDimitry Andric result with the integer bit set. */ 2817*0b57cec5SDimitry Andric assert(APInt::tcExtractBit 2818*0b57cec5SDimitry Andric (decSig.significandParts(), calcSemantics.precision - 1) == 1); 2819*0b57cec5SDimitry Andric 2820*0b57cec5SDimitry Andric HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK, 2821*0b57cec5SDimitry Andric powHUerr); 2822*0b57cec5SDimitry Andric HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(), 2823*0b57cec5SDimitry Andric excessPrecision, isNearest); 2824*0b57cec5SDimitry Andric 2825*0b57cec5SDimitry Andric /* Are we guaranteed to round correctly if we truncate? */ 2826*0b57cec5SDimitry Andric if (HUdistance >= HUerr) { 2827*0b57cec5SDimitry Andric APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(), 2828*0b57cec5SDimitry Andric calcSemantics.precision - excessPrecision, 2829*0b57cec5SDimitry Andric excessPrecision); 2830*0b57cec5SDimitry Andric /* Take the exponent of decSig. If we tcExtract-ed less bits 2831*0b57cec5SDimitry Andric above we must adjust our exponent to compensate for the 2832*0b57cec5SDimitry Andric implicit right shift. */ 2833*0b57cec5SDimitry Andric exponent = (decSig.exponent + semantics->precision 2834*0b57cec5SDimitry Andric - (calcSemantics.precision - excessPrecision)); 2835*0b57cec5SDimitry Andric calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(), 2836*0b57cec5SDimitry Andric decSig.partCount(), 2837*0b57cec5SDimitry Andric truncatedBits); 2838*0b57cec5SDimitry Andric return normalize(rounding_mode, calcLostFraction); 2839*0b57cec5SDimitry Andric } 2840*0b57cec5SDimitry Andric } 2841*0b57cec5SDimitry Andric } 2842*0b57cec5SDimitry Andric 2843480093f4SDimitry Andric Expected<IEEEFloat::opStatus> 2844*0b57cec5SDimitry Andric IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) { 2845*0b57cec5SDimitry Andric decimalInfo D; 2846*0b57cec5SDimitry Andric opStatus fs; 2847*0b57cec5SDimitry Andric 2848*0b57cec5SDimitry Andric /* Scan the text. */ 2849*0b57cec5SDimitry Andric StringRef::iterator p = str.begin(); 2850480093f4SDimitry Andric if (Error Err = interpretDecimal(p, str.end(), &D)) 2851480093f4SDimitry Andric return std::move(Err); 2852*0b57cec5SDimitry Andric 2853*0b57cec5SDimitry Andric /* Handle the quick cases. First the case of no significant digits, 2854*0b57cec5SDimitry Andric i.e. zero, and then exponents that are obviously too large or too 2855*0b57cec5SDimitry Andric small. Writing L for log 10 / log 2, a number d.ddddd*10^exp 2856*0b57cec5SDimitry Andric definitely overflows if 2857*0b57cec5SDimitry Andric 2858*0b57cec5SDimitry Andric (exp - 1) * L >= maxExponent 2859*0b57cec5SDimitry Andric 2860*0b57cec5SDimitry Andric and definitely underflows to zero where 2861*0b57cec5SDimitry Andric 2862*0b57cec5SDimitry Andric (exp + 1) * L <= minExponent - precision 2863*0b57cec5SDimitry Andric 2864*0b57cec5SDimitry Andric With integer arithmetic the tightest bounds for L are 2865*0b57cec5SDimitry Andric 2866*0b57cec5SDimitry Andric 93/28 < L < 196/59 [ numerator <= 256 ] 2867*0b57cec5SDimitry Andric 42039/12655 < L < 28738/8651 [ numerator <= 65536 ] 2868*0b57cec5SDimitry Andric */ 2869*0b57cec5SDimitry Andric 2870*0b57cec5SDimitry Andric // Test if we have a zero number allowing for strings with no null terminators 2871*0b57cec5SDimitry Andric // and zero decimals with non-zero exponents. 2872*0b57cec5SDimitry Andric // 2873*0b57cec5SDimitry Andric // We computed firstSigDigit by ignoring all zeros and dots. Thus if 2874*0b57cec5SDimitry Andric // D->firstSigDigit equals str.end(), every digit must be a zero and there can 2875*0b57cec5SDimitry Andric // be at most one dot. On the other hand, if we have a zero with a non-zero 2876*0b57cec5SDimitry Andric // exponent, then we know that D.firstSigDigit will be non-numeric. 2877*0b57cec5SDimitry Andric if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) { 2878*0b57cec5SDimitry Andric category = fcZero; 2879*0b57cec5SDimitry Andric fs = opOK; 2880*0b57cec5SDimitry Andric 2881*0b57cec5SDimitry Andric /* Check whether the normalized exponent is high enough to overflow 2882*0b57cec5SDimitry Andric max during the log-rebasing in the max-exponent check below. */ 2883*0b57cec5SDimitry Andric } else if (D.normalizedExponent - 1 > INT_MAX / 42039) { 2884*0b57cec5SDimitry Andric fs = handleOverflow(rounding_mode); 2885*0b57cec5SDimitry Andric 2886*0b57cec5SDimitry Andric /* If it wasn't, then it also wasn't high enough to overflow max 2887*0b57cec5SDimitry Andric during the log-rebasing in the min-exponent check. Check that it 2888*0b57cec5SDimitry Andric won't overflow min in either check, then perform the min-exponent 2889*0b57cec5SDimitry Andric check. */ 2890*0b57cec5SDimitry Andric } else if (D.normalizedExponent - 1 < INT_MIN / 42039 || 2891*0b57cec5SDimitry Andric (D.normalizedExponent + 1) * 28738 <= 2892*0b57cec5SDimitry Andric 8651 * (semantics->minExponent - (int) semantics->precision)) { 2893*0b57cec5SDimitry Andric /* Underflow to zero and round. */ 2894*0b57cec5SDimitry Andric category = fcNormal; 2895*0b57cec5SDimitry Andric zeroSignificand(); 2896*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lfLessThanHalf); 2897*0b57cec5SDimitry Andric 2898*0b57cec5SDimitry Andric /* We can finally safely perform the max-exponent check. */ 2899*0b57cec5SDimitry Andric } else if ((D.normalizedExponent - 1) * 42039 2900*0b57cec5SDimitry Andric >= 12655 * semantics->maxExponent) { 2901*0b57cec5SDimitry Andric /* Overflow and round. */ 2902*0b57cec5SDimitry Andric fs = handleOverflow(rounding_mode); 2903*0b57cec5SDimitry Andric } else { 2904*0b57cec5SDimitry Andric integerPart *decSignificand; 2905*0b57cec5SDimitry Andric unsigned int partCount; 2906*0b57cec5SDimitry Andric 2907*0b57cec5SDimitry Andric /* A tight upper bound on number of bits required to hold an 2908*0b57cec5SDimitry Andric N-digit decimal integer is N * 196 / 59. Allocate enough space 2909*0b57cec5SDimitry Andric to hold the full significand, and an extra part required by 2910*0b57cec5SDimitry Andric tcMultiplyPart. */ 2911*0b57cec5SDimitry Andric partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1; 2912*0b57cec5SDimitry Andric partCount = partCountForBits(1 + 196 * partCount / 59); 2913*0b57cec5SDimitry Andric decSignificand = new integerPart[partCount + 1]; 2914*0b57cec5SDimitry Andric partCount = 0; 2915*0b57cec5SDimitry Andric 2916*0b57cec5SDimitry Andric /* Convert to binary efficiently - we do almost all multiplication 2917*0b57cec5SDimitry Andric in an integerPart. When this would overflow do we do a single 2918*0b57cec5SDimitry Andric bignum multiplication, and then revert again to multiplication 2919*0b57cec5SDimitry Andric in an integerPart. */ 2920*0b57cec5SDimitry Andric do { 2921*0b57cec5SDimitry Andric integerPart decValue, val, multiplier; 2922*0b57cec5SDimitry Andric 2923*0b57cec5SDimitry Andric val = 0; 2924*0b57cec5SDimitry Andric multiplier = 1; 2925*0b57cec5SDimitry Andric 2926*0b57cec5SDimitry Andric do { 2927*0b57cec5SDimitry Andric if (*p == '.') { 2928*0b57cec5SDimitry Andric p++; 2929*0b57cec5SDimitry Andric if (p == str.end()) { 2930*0b57cec5SDimitry Andric break; 2931*0b57cec5SDimitry Andric } 2932*0b57cec5SDimitry Andric } 2933*0b57cec5SDimitry Andric decValue = decDigitValue(*p++); 2934480093f4SDimitry Andric if (decValue >= 10U) { 2935480093f4SDimitry Andric delete[] decSignificand; 2936480093f4SDimitry Andric return createError("Invalid character in significand"); 2937480093f4SDimitry Andric } 2938*0b57cec5SDimitry Andric multiplier *= 10; 2939*0b57cec5SDimitry Andric val = val * 10 + decValue; 2940*0b57cec5SDimitry Andric /* The maximum number that can be multiplied by ten with any 2941*0b57cec5SDimitry Andric digit added without overflowing an integerPart. */ 2942*0b57cec5SDimitry Andric } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10); 2943*0b57cec5SDimitry Andric 2944*0b57cec5SDimitry Andric /* Multiply out the current part. */ 2945*0b57cec5SDimitry Andric APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val, 2946*0b57cec5SDimitry Andric partCount, partCount + 1, false); 2947*0b57cec5SDimitry Andric 2948*0b57cec5SDimitry Andric /* If we used another part (likely but not guaranteed), increase 2949*0b57cec5SDimitry Andric the count. */ 2950*0b57cec5SDimitry Andric if (decSignificand[partCount]) 2951*0b57cec5SDimitry Andric partCount++; 2952*0b57cec5SDimitry Andric } while (p <= D.lastSigDigit); 2953*0b57cec5SDimitry Andric 2954*0b57cec5SDimitry Andric category = fcNormal; 2955*0b57cec5SDimitry Andric fs = roundSignificandWithExponent(decSignificand, partCount, 2956*0b57cec5SDimitry Andric D.exponent, rounding_mode); 2957*0b57cec5SDimitry Andric 2958*0b57cec5SDimitry Andric delete [] decSignificand; 2959*0b57cec5SDimitry Andric } 2960*0b57cec5SDimitry Andric 2961*0b57cec5SDimitry Andric return fs; 2962*0b57cec5SDimitry Andric } 2963*0b57cec5SDimitry Andric 2964*0b57cec5SDimitry Andric bool IEEEFloat::convertFromStringSpecials(StringRef str) { 29655ffd83dbSDimitry Andric const size_t MIN_NAME_SIZE = 3; 29665ffd83dbSDimitry Andric 29675ffd83dbSDimitry Andric if (str.size() < MIN_NAME_SIZE) 29685ffd83dbSDimitry Andric return false; 29695ffd83dbSDimitry Andric 2970*0b57cec5SDimitry Andric if (str.equals("inf") || str.equals("INFINITY") || str.equals("+Inf")) { 2971*0b57cec5SDimitry Andric makeInf(false); 2972*0b57cec5SDimitry Andric return true; 2973*0b57cec5SDimitry Andric } 2974*0b57cec5SDimitry Andric 29755ffd83dbSDimitry Andric bool IsNegative = str.front() == '-'; 29765ffd83dbSDimitry Andric if (IsNegative) { 29775ffd83dbSDimitry Andric str = str.drop_front(); 29785ffd83dbSDimitry Andric if (str.size() < MIN_NAME_SIZE) 29795ffd83dbSDimitry Andric return false; 29805ffd83dbSDimitry Andric 29815ffd83dbSDimitry Andric if (str.equals("inf") || str.equals("INFINITY") || str.equals("Inf")) { 2982*0b57cec5SDimitry Andric makeInf(true); 2983*0b57cec5SDimitry Andric return true; 2984*0b57cec5SDimitry Andric } 29855ffd83dbSDimitry Andric } 2986*0b57cec5SDimitry Andric 29875ffd83dbSDimitry Andric // If we have a 's' (or 'S') prefix, then this is a Signaling NaN. 29885ffd83dbSDimitry Andric bool IsSignaling = str.front() == 's' || str.front() == 'S'; 29895ffd83dbSDimitry Andric if (IsSignaling) { 29905ffd83dbSDimitry Andric str = str.drop_front(); 29915ffd83dbSDimitry Andric if (str.size() < MIN_NAME_SIZE) 29925ffd83dbSDimitry Andric return false; 29935ffd83dbSDimitry Andric } 29945ffd83dbSDimitry Andric 29955ffd83dbSDimitry Andric if (str.startswith("nan") || str.startswith("NaN")) { 29965ffd83dbSDimitry Andric str = str.drop_front(3); 29975ffd83dbSDimitry Andric 29985ffd83dbSDimitry Andric // A NaN without payload. 29995ffd83dbSDimitry Andric if (str.empty()) { 30005ffd83dbSDimitry Andric makeNaN(IsSignaling, IsNegative); 3001*0b57cec5SDimitry Andric return true; 3002*0b57cec5SDimitry Andric } 3003*0b57cec5SDimitry Andric 30045ffd83dbSDimitry Andric // Allow the payload to be inside parentheses. 30055ffd83dbSDimitry Andric if (str.front() == '(') { 30065ffd83dbSDimitry Andric // Parentheses should be balanced (and not empty). 30075ffd83dbSDimitry Andric if (str.size() <= 2 || str.back() != ')') 30085ffd83dbSDimitry Andric return false; 30095ffd83dbSDimitry Andric 30105ffd83dbSDimitry Andric str = str.slice(1, str.size() - 1); 30115ffd83dbSDimitry Andric } 30125ffd83dbSDimitry Andric 30135ffd83dbSDimitry Andric // Determine the payload number's radix. 30145ffd83dbSDimitry Andric unsigned Radix = 10; 30155ffd83dbSDimitry Andric if (str[0] == '0') { 30165ffd83dbSDimitry Andric if (str.size() > 1 && tolower(str[1]) == 'x') { 30175ffd83dbSDimitry Andric str = str.drop_front(2); 30185ffd83dbSDimitry Andric Radix = 16; 30195ffd83dbSDimitry Andric } else 30205ffd83dbSDimitry Andric Radix = 8; 30215ffd83dbSDimitry Andric } 30225ffd83dbSDimitry Andric 30235ffd83dbSDimitry Andric // Parse the payload and make the NaN. 30245ffd83dbSDimitry Andric APInt Payload; 30255ffd83dbSDimitry Andric if (!str.getAsInteger(Radix, Payload)) { 30265ffd83dbSDimitry Andric makeNaN(IsSignaling, IsNegative, &Payload); 3027*0b57cec5SDimitry Andric return true; 3028*0b57cec5SDimitry Andric } 30295ffd83dbSDimitry Andric } 3030*0b57cec5SDimitry Andric 3031*0b57cec5SDimitry Andric return false; 3032*0b57cec5SDimitry Andric } 3033*0b57cec5SDimitry Andric 3034480093f4SDimitry Andric Expected<IEEEFloat::opStatus> 3035480093f4SDimitry Andric IEEEFloat::convertFromString(StringRef str, roundingMode rounding_mode) { 3036480093f4SDimitry Andric if (str.empty()) 3037480093f4SDimitry Andric return createError("Invalid string length"); 3038*0b57cec5SDimitry Andric 3039*0b57cec5SDimitry Andric // Handle special cases. 3040*0b57cec5SDimitry Andric if (convertFromStringSpecials(str)) 3041*0b57cec5SDimitry Andric return opOK; 3042*0b57cec5SDimitry Andric 3043*0b57cec5SDimitry Andric /* Handle a leading minus sign. */ 3044*0b57cec5SDimitry Andric StringRef::iterator p = str.begin(); 3045*0b57cec5SDimitry Andric size_t slen = str.size(); 3046*0b57cec5SDimitry Andric sign = *p == '-' ? 1 : 0; 3047*0b57cec5SDimitry Andric if (*p == '-' || *p == '+') { 3048*0b57cec5SDimitry Andric p++; 3049*0b57cec5SDimitry Andric slen--; 3050480093f4SDimitry Andric if (!slen) 3051480093f4SDimitry Andric return createError("String has no digits"); 3052*0b57cec5SDimitry Andric } 3053*0b57cec5SDimitry Andric 3054*0b57cec5SDimitry Andric if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 3055480093f4SDimitry Andric if (slen == 2) 3056480093f4SDimitry Andric return createError("Invalid string"); 3057*0b57cec5SDimitry Andric return convertFromHexadecimalString(StringRef(p + 2, slen - 2), 3058*0b57cec5SDimitry Andric rounding_mode); 3059*0b57cec5SDimitry Andric } 3060*0b57cec5SDimitry Andric 3061*0b57cec5SDimitry Andric return convertFromDecimalString(StringRef(p, slen), rounding_mode); 3062*0b57cec5SDimitry Andric } 3063*0b57cec5SDimitry Andric 3064*0b57cec5SDimitry Andric /* Write out a hexadecimal representation of the floating point value 3065*0b57cec5SDimitry Andric to DST, which must be of sufficient size, in the C99 form 3066*0b57cec5SDimitry Andric [-]0xh.hhhhp[+-]d. Return the number of characters written, 3067*0b57cec5SDimitry Andric excluding the terminating NUL. 3068*0b57cec5SDimitry Andric 3069*0b57cec5SDimitry Andric If UPPERCASE, the output is in upper case, otherwise in lower case. 3070*0b57cec5SDimitry Andric 3071*0b57cec5SDimitry Andric HEXDIGITS digits appear altogether, rounding the value if 3072*0b57cec5SDimitry Andric necessary. If HEXDIGITS is 0, the minimal precision to display the 3073*0b57cec5SDimitry Andric number precisely is used instead. If nothing would appear after 3074*0b57cec5SDimitry Andric the decimal point it is suppressed. 3075*0b57cec5SDimitry Andric 3076*0b57cec5SDimitry Andric The decimal exponent is always printed and has at least one digit. 3077*0b57cec5SDimitry Andric Zero values display an exponent of zero. Infinities and NaNs 3078*0b57cec5SDimitry Andric appear as "infinity" or "nan" respectively. 3079*0b57cec5SDimitry Andric 3080*0b57cec5SDimitry Andric The above rules are as specified by C99. There is ambiguity about 3081*0b57cec5SDimitry Andric what the leading hexadecimal digit should be. This implementation 3082*0b57cec5SDimitry Andric uses whatever is necessary so that the exponent is displayed as 3083*0b57cec5SDimitry Andric stored. This implies the exponent will fall within the IEEE format 3084*0b57cec5SDimitry Andric range, and the leading hexadecimal digit will be 0 (for denormals), 3085*0b57cec5SDimitry Andric 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with 3086*0b57cec5SDimitry Andric any other digits zero). 3087*0b57cec5SDimitry Andric */ 3088*0b57cec5SDimitry Andric unsigned int IEEEFloat::convertToHexString(char *dst, unsigned int hexDigits, 3089*0b57cec5SDimitry Andric bool upperCase, 3090*0b57cec5SDimitry Andric roundingMode rounding_mode) const { 3091*0b57cec5SDimitry Andric char *p; 3092*0b57cec5SDimitry Andric 3093*0b57cec5SDimitry Andric p = dst; 3094*0b57cec5SDimitry Andric if (sign) 3095*0b57cec5SDimitry Andric *dst++ = '-'; 3096*0b57cec5SDimitry Andric 3097*0b57cec5SDimitry Andric switch (category) { 3098*0b57cec5SDimitry Andric case fcInfinity: 3099*0b57cec5SDimitry Andric memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1); 3100*0b57cec5SDimitry Andric dst += sizeof infinityL - 1; 3101*0b57cec5SDimitry Andric break; 3102*0b57cec5SDimitry Andric 3103*0b57cec5SDimitry Andric case fcNaN: 3104*0b57cec5SDimitry Andric memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1); 3105*0b57cec5SDimitry Andric dst += sizeof NaNU - 1; 3106*0b57cec5SDimitry Andric break; 3107*0b57cec5SDimitry Andric 3108*0b57cec5SDimitry Andric case fcZero: 3109*0b57cec5SDimitry Andric *dst++ = '0'; 3110*0b57cec5SDimitry Andric *dst++ = upperCase ? 'X': 'x'; 3111*0b57cec5SDimitry Andric *dst++ = '0'; 3112*0b57cec5SDimitry Andric if (hexDigits > 1) { 3113*0b57cec5SDimitry Andric *dst++ = '.'; 3114*0b57cec5SDimitry Andric memset (dst, '0', hexDigits - 1); 3115*0b57cec5SDimitry Andric dst += hexDigits - 1; 3116*0b57cec5SDimitry Andric } 3117*0b57cec5SDimitry Andric *dst++ = upperCase ? 'P': 'p'; 3118*0b57cec5SDimitry Andric *dst++ = '0'; 3119*0b57cec5SDimitry Andric break; 3120*0b57cec5SDimitry Andric 3121*0b57cec5SDimitry Andric case fcNormal: 3122*0b57cec5SDimitry Andric dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode); 3123*0b57cec5SDimitry Andric break; 3124*0b57cec5SDimitry Andric } 3125*0b57cec5SDimitry Andric 3126*0b57cec5SDimitry Andric *dst = 0; 3127*0b57cec5SDimitry Andric 3128*0b57cec5SDimitry Andric return static_cast<unsigned int>(dst - p); 3129*0b57cec5SDimitry Andric } 3130*0b57cec5SDimitry Andric 3131*0b57cec5SDimitry Andric /* Does the hard work of outputting the correctly rounded hexadecimal 3132*0b57cec5SDimitry Andric form of a normal floating point number with the specified number of 3133*0b57cec5SDimitry Andric hexadecimal digits. If HEXDIGITS is zero the minimum number of 3134*0b57cec5SDimitry Andric digits necessary to print the value precisely is output. */ 3135*0b57cec5SDimitry Andric char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits, 3136*0b57cec5SDimitry Andric bool upperCase, 3137*0b57cec5SDimitry Andric roundingMode rounding_mode) const { 3138*0b57cec5SDimitry Andric unsigned int count, valueBits, shift, partsCount, outputDigits; 3139*0b57cec5SDimitry Andric const char *hexDigitChars; 3140*0b57cec5SDimitry Andric const integerPart *significand; 3141*0b57cec5SDimitry Andric char *p; 3142*0b57cec5SDimitry Andric bool roundUp; 3143*0b57cec5SDimitry Andric 3144*0b57cec5SDimitry Andric *dst++ = '0'; 3145*0b57cec5SDimitry Andric *dst++ = upperCase ? 'X': 'x'; 3146*0b57cec5SDimitry Andric 3147*0b57cec5SDimitry Andric roundUp = false; 3148*0b57cec5SDimitry Andric hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower; 3149*0b57cec5SDimitry Andric 3150*0b57cec5SDimitry Andric significand = significandParts(); 3151*0b57cec5SDimitry Andric partsCount = partCount(); 3152*0b57cec5SDimitry Andric 3153*0b57cec5SDimitry Andric /* +3 because the first digit only uses the single integer bit, so 3154*0b57cec5SDimitry Andric we have 3 virtual zero most-significant-bits. */ 3155*0b57cec5SDimitry Andric valueBits = semantics->precision + 3; 3156*0b57cec5SDimitry Andric shift = integerPartWidth - valueBits % integerPartWidth; 3157*0b57cec5SDimitry Andric 3158*0b57cec5SDimitry Andric /* The natural number of digits required ignoring trailing 3159*0b57cec5SDimitry Andric insignificant zeroes. */ 3160*0b57cec5SDimitry Andric outputDigits = (valueBits - significandLSB () + 3) / 4; 3161*0b57cec5SDimitry Andric 3162*0b57cec5SDimitry Andric /* hexDigits of zero means use the required number for the 3163*0b57cec5SDimitry Andric precision. Otherwise, see if we are truncating. If we are, 3164*0b57cec5SDimitry Andric find out if we need to round away from zero. */ 3165*0b57cec5SDimitry Andric if (hexDigits) { 3166*0b57cec5SDimitry Andric if (hexDigits < outputDigits) { 3167*0b57cec5SDimitry Andric /* We are dropping non-zero bits, so need to check how to round. 3168*0b57cec5SDimitry Andric "bits" is the number of dropped bits. */ 3169*0b57cec5SDimitry Andric unsigned int bits; 3170*0b57cec5SDimitry Andric lostFraction fraction; 3171*0b57cec5SDimitry Andric 3172*0b57cec5SDimitry Andric bits = valueBits - hexDigits * 4; 3173*0b57cec5SDimitry Andric fraction = lostFractionThroughTruncation (significand, partsCount, bits); 3174*0b57cec5SDimitry Andric roundUp = roundAwayFromZero(rounding_mode, fraction, bits); 3175*0b57cec5SDimitry Andric } 3176*0b57cec5SDimitry Andric outputDigits = hexDigits; 3177*0b57cec5SDimitry Andric } 3178*0b57cec5SDimitry Andric 3179*0b57cec5SDimitry Andric /* Write the digits consecutively, and start writing in the location 3180*0b57cec5SDimitry Andric of the hexadecimal point. We move the most significant digit 3181*0b57cec5SDimitry Andric left and add the hexadecimal point later. */ 3182*0b57cec5SDimitry Andric p = ++dst; 3183*0b57cec5SDimitry Andric 3184*0b57cec5SDimitry Andric count = (valueBits + integerPartWidth - 1) / integerPartWidth; 3185*0b57cec5SDimitry Andric 3186*0b57cec5SDimitry Andric while (outputDigits && count) { 3187*0b57cec5SDimitry Andric integerPart part; 3188*0b57cec5SDimitry Andric 3189*0b57cec5SDimitry Andric /* Put the most significant integerPartWidth bits in "part". */ 3190*0b57cec5SDimitry Andric if (--count == partsCount) 3191*0b57cec5SDimitry Andric part = 0; /* An imaginary higher zero part. */ 3192*0b57cec5SDimitry Andric else 3193*0b57cec5SDimitry Andric part = significand[count] << shift; 3194*0b57cec5SDimitry Andric 3195*0b57cec5SDimitry Andric if (count && shift) 3196*0b57cec5SDimitry Andric part |= significand[count - 1] >> (integerPartWidth - shift); 3197*0b57cec5SDimitry Andric 3198*0b57cec5SDimitry Andric /* Convert as much of "part" to hexdigits as we can. */ 3199*0b57cec5SDimitry Andric unsigned int curDigits = integerPartWidth / 4; 3200*0b57cec5SDimitry Andric 3201*0b57cec5SDimitry Andric if (curDigits > outputDigits) 3202*0b57cec5SDimitry Andric curDigits = outputDigits; 3203*0b57cec5SDimitry Andric dst += partAsHex (dst, part, curDigits, hexDigitChars); 3204*0b57cec5SDimitry Andric outputDigits -= curDigits; 3205*0b57cec5SDimitry Andric } 3206*0b57cec5SDimitry Andric 3207*0b57cec5SDimitry Andric if (roundUp) { 3208*0b57cec5SDimitry Andric char *q = dst; 3209*0b57cec5SDimitry Andric 3210*0b57cec5SDimitry Andric /* Note that hexDigitChars has a trailing '0'. */ 3211*0b57cec5SDimitry Andric do { 3212*0b57cec5SDimitry Andric q--; 3213*0b57cec5SDimitry Andric *q = hexDigitChars[hexDigitValue (*q) + 1]; 3214*0b57cec5SDimitry Andric } while (*q == '0'); 3215*0b57cec5SDimitry Andric assert(q >= p); 3216*0b57cec5SDimitry Andric } else { 3217*0b57cec5SDimitry Andric /* Add trailing zeroes. */ 3218*0b57cec5SDimitry Andric memset (dst, '0', outputDigits); 3219*0b57cec5SDimitry Andric dst += outputDigits; 3220*0b57cec5SDimitry Andric } 3221*0b57cec5SDimitry Andric 3222*0b57cec5SDimitry Andric /* Move the most significant digit to before the point, and if there 3223*0b57cec5SDimitry Andric is something after the decimal point add it. This must come 3224*0b57cec5SDimitry Andric after rounding above. */ 3225*0b57cec5SDimitry Andric p[-1] = p[0]; 3226*0b57cec5SDimitry Andric if (dst -1 == p) 3227*0b57cec5SDimitry Andric dst--; 3228*0b57cec5SDimitry Andric else 3229*0b57cec5SDimitry Andric p[0] = '.'; 3230*0b57cec5SDimitry Andric 3231*0b57cec5SDimitry Andric /* Finally output the exponent. */ 3232*0b57cec5SDimitry Andric *dst++ = upperCase ? 'P': 'p'; 3233*0b57cec5SDimitry Andric 3234*0b57cec5SDimitry Andric return writeSignedDecimal (dst, exponent); 3235*0b57cec5SDimitry Andric } 3236*0b57cec5SDimitry Andric 3237*0b57cec5SDimitry Andric hash_code hash_value(const IEEEFloat &Arg) { 3238*0b57cec5SDimitry Andric if (!Arg.isFiniteNonZero()) 3239*0b57cec5SDimitry Andric return hash_combine((uint8_t)Arg.category, 3240*0b57cec5SDimitry Andric // NaN has no sign, fix it at zero. 3241*0b57cec5SDimitry Andric Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign, 3242*0b57cec5SDimitry Andric Arg.semantics->precision); 3243*0b57cec5SDimitry Andric 3244*0b57cec5SDimitry Andric // Normal floats need their exponent and significand hashed. 3245*0b57cec5SDimitry Andric return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign, 3246*0b57cec5SDimitry Andric Arg.semantics->precision, Arg.exponent, 3247*0b57cec5SDimitry Andric hash_combine_range( 3248*0b57cec5SDimitry Andric Arg.significandParts(), 3249*0b57cec5SDimitry Andric Arg.significandParts() + Arg.partCount())); 3250*0b57cec5SDimitry Andric } 3251*0b57cec5SDimitry Andric 3252*0b57cec5SDimitry Andric // Conversion from APFloat to/from host float/double. It may eventually be 3253*0b57cec5SDimitry Andric // possible to eliminate these and have everybody deal with APFloats, but that 3254*0b57cec5SDimitry Andric // will take a while. This approach will not easily extend to long double. 3255*0b57cec5SDimitry Andric // Current implementation requires integerPartWidth==64, which is correct at 3256*0b57cec5SDimitry Andric // the moment but could be made more general. 3257*0b57cec5SDimitry Andric 3258*0b57cec5SDimitry Andric // Denormals have exponent minExponent in APFloat, but minExponent-1 in 3259*0b57cec5SDimitry Andric // the actual IEEE respresentations. We compensate for that here. 3260*0b57cec5SDimitry Andric 3261*0b57cec5SDimitry Andric APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const { 3262*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended); 3263*0b57cec5SDimitry Andric assert(partCount()==2); 3264*0b57cec5SDimitry Andric 3265*0b57cec5SDimitry Andric uint64_t myexponent, mysignificand; 3266*0b57cec5SDimitry Andric 3267*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 3268*0b57cec5SDimitry Andric myexponent = exponent+16383; //bias 3269*0b57cec5SDimitry Andric mysignificand = significandParts()[0]; 3270*0b57cec5SDimitry Andric if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL)) 3271*0b57cec5SDimitry Andric myexponent = 0; // denormal 3272*0b57cec5SDimitry Andric } else if (category==fcZero) { 3273*0b57cec5SDimitry Andric myexponent = 0; 3274*0b57cec5SDimitry Andric mysignificand = 0; 3275*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 3276*0b57cec5SDimitry Andric myexponent = 0x7fff; 3277*0b57cec5SDimitry Andric mysignificand = 0x8000000000000000ULL; 3278*0b57cec5SDimitry Andric } else { 3279*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category"); 3280*0b57cec5SDimitry Andric myexponent = 0x7fff; 3281*0b57cec5SDimitry Andric mysignificand = significandParts()[0]; 3282*0b57cec5SDimitry Andric } 3283*0b57cec5SDimitry Andric 3284*0b57cec5SDimitry Andric uint64_t words[2]; 3285*0b57cec5SDimitry Andric words[0] = mysignificand; 3286*0b57cec5SDimitry Andric words[1] = ((uint64_t)(sign & 1) << 15) | 3287*0b57cec5SDimitry Andric (myexponent & 0x7fffLL); 3288*0b57cec5SDimitry Andric return APInt(80, words); 3289*0b57cec5SDimitry Andric } 3290*0b57cec5SDimitry Andric 3291*0b57cec5SDimitry Andric APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt() const { 3292*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy); 3293*0b57cec5SDimitry Andric assert(partCount()==2); 3294*0b57cec5SDimitry Andric 3295*0b57cec5SDimitry Andric uint64_t words[2]; 3296*0b57cec5SDimitry Andric opStatus fs; 3297*0b57cec5SDimitry Andric bool losesInfo; 3298*0b57cec5SDimitry Andric 3299*0b57cec5SDimitry Andric // Convert number to double. To avoid spurious underflows, we re- 3300*0b57cec5SDimitry Andric // normalize against the "double" minExponent first, and only *then* 3301*0b57cec5SDimitry Andric // truncate the mantissa. The result of that second conversion 3302*0b57cec5SDimitry Andric // may be inexact, but should never underflow. 3303*0b57cec5SDimitry Andric // Declare fltSemantics before APFloat that uses it (and 3304*0b57cec5SDimitry Andric // saves pointer to it) to ensure correct destruction order. 3305*0b57cec5SDimitry Andric fltSemantics extendedSemantics = *semantics; 3306*0b57cec5SDimitry Andric extendedSemantics.minExponent = semIEEEdouble.minExponent; 3307*0b57cec5SDimitry Andric IEEEFloat extended(*this); 3308*0b57cec5SDimitry Andric fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 3309*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 3310*0b57cec5SDimitry Andric (void)fs; 3311*0b57cec5SDimitry Andric 3312*0b57cec5SDimitry Andric IEEEFloat u(extended); 3313*0b57cec5SDimitry Andric fs = u.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo); 3314*0b57cec5SDimitry Andric assert(fs == opOK || fs == opInexact); 3315*0b57cec5SDimitry Andric (void)fs; 3316*0b57cec5SDimitry Andric words[0] = *u.convertDoubleAPFloatToAPInt().getRawData(); 3317*0b57cec5SDimitry Andric 3318*0b57cec5SDimitry Andric // If conversion was exact or resulted in a special case, we're done; 3319*0b57cec5SDimitry Andric // just set the second double to zero. Otherwise, re-convert back to 3320*0b57cec5SDimitry Andric // the extended format and compute the difference. This now should 3321*0b57cec5SDimitry Andric // convert exactly to double. 3322*0b57cec5SDimitry Andric if (u.isFiniteNonZero() && losesInfo) { 3323*0b57cec5SDimitry Andric fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 3324*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 3325*0b57cec5SDimitry Andric (void)fs; 3326*0b57cec5SDimitry Andric 3327*0b57cec5SDimitry Andric IEEEFloat v(extended); 3328*0b57cec5SDimitry Andric v.subtract(u, rmNearestTiesToEven); 3329*0b57cec5SDimitry Andric fs = v.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo); 3330*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 3331*0b57cec5SDimitry Andric (void)fs; 3332*0b57cec5SDimitry Andric words[1] = *v.convertDoubleAPFloatToAPInt().getRawData(); 3333*0b57cec5SDimitry Andric } else { 3334*0b57cec5SDimitry Andric words[1] = 0; 3335*0b57cec5SDimitry Andric } 3336*0b57cec5SDimitry Andric 3337*0b57cec5SDimitry Andric return APInt(128, words); 3338*0b57cec5SDimitry Andric } 3339*0b57cec5SDimitry Andric 3340*0b57cec5SDimitry Andric APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const { 3341*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEquad); 3342*0b57cec5SDimitry Andric assert(partCount()==2); 3343*0b57cec5SDimitry Andric 3344*0b57cec5SDimitry Andric uint64_t myexponent, mysignificand, mysignificand2; 3345*0b57cec5SDimitry Andric 3346*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 3347*0b57cec5SDimitry Andric myexponent = exponent+16383; //bias 3348*0b57cec5SDimitry Andric mysignificand = significandParts()[0]; 3349*0b57cec5SDimitry Andric mysignificand2 = significandParts()[1]; 3350*0b57cec5SDimitry Andric if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL)) 3351*0b57cec5SDimitry Andric myexponent = 0; // denormal 3352*0b57cec5SDimitry Andric } else if (category==fcZero) { 3353*0b57cec5SDimitry Andric myexponent = 0; 3354*0b57cec5SDimitry Andric mysignificand = mysignificand2 = 0; 3355*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 3356*0b57cec5SDimitry Andric myexponent = 0x7fff; 3357*0b57cec5SDimitry Andric mysignificand = mysignificand2 = 0; 3358*0b57cec5SDimitry Andric } else { 3359*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category!"); 3360*0b57cec5SDimitry Andric myexponent = 0x7fff; 3361*0b57cec5SDimitry Andric mysignificand = significandParts()[0]; 3362*0b57cec5SDimitry Andric mysignificand2 = significandParts()[1]; 3363*0b57cec5SDimitry Andric } 3364*0b57cec5SDimitry Andric 3365*0b57cec5SDimitry Andric uint64_t words[2]; 3366*0b57cec5SDimitry Andric words[0] = mysignificand; 3367*0b57cec5SDimitry Andric words[1] = ((uint64_t)(sign & 1) << 63) | 3368*0b57cec5SDimitry Andric ((myexponent & 0x7fff) << 48) | 3369*0b57cec5SDimitry Andric (mysignificand2 & 0xffffffffffffLL); 3370*0b57cec5SDimitry Andric 3371*0b57cec5SDimitry Andric return APInt(128, words); 3372*0b57cec5SDimitry Andric } 3373*0b57cec5SDimitry Andric 3374*0b57cec5SDimitry Andric APInt IEEEFloat::convertDoubleAPFloatToAPInt() const { 3375*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble); 3376*0b57cec5SDimitry Andric assert(partCount()==1); 3377*0b57cec5SDimitry Andric 3378*0b57cec5SDimitry Andric uint64_t myexponent, mysignificand; 3379*0b57cec5SDimitry Andric 3380*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 3381*0b57cec5SDimitry Andric myexponent = exponent+1023; //bias 3382*0b57cec5SDimitry Andric mysignificand = *significandParts(); 3383*0b57cec5SDimitry Andric if (myexponent==1 && !(mysignificand & 0x10000000000000LL)) 3384*0b57cec5SDimitry Andric myexponent = 0; // denormal 3385*0b57cec5SDimitry Andric } else if (category==fcZero) { 3386*0b57cec5SDimitry Andric myexponent = 0; 3387*0b57cec5SDimitry Andric mysignificand = 0; 3388*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 3389*0b57cec5SDimitry Andric myexponent = 0x7ff; 3390*0b57cec5SDimitry Andric mysignificand = 0; 3391*0b57cec5SDimitry Andric } else { 3392*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category!"); 3393*0b57cec5SDimitry Andric myexponent = 0x7ff; 3394*0b57cec5SDimitry Andric mysignificand = *significandParts(); 3395*0b57cec5SDimitry Andric } 3396*0b57cec5SDimitry Andric 3397*0b57cec5SDimitry Andric return APInt(64, ((((uint64_t)(sign & 1) << 63) | 3398*0b57cec5SDimitry Andric ((myexponent & 0x7ff) << 52) | 3399*0b57cec5SDimitry Andric (mysignificand & 0xfffffffffffffLL)))); 3400*0b57cec5SDimitry Andric } 3401*0b57cec5SDimitry Andric 3402*0b57cec5SDimitry Andric APInt IEEEFloat::convertFloatAPFloatToAPInt() const { 3403*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle); 3404*0b57cec5SDimitry Andric assert(partCount()==1); 3405*0b57cec5SDimitry Andric 3406*0b57cec5SDimitry Andric uint32_t myexponent, mysignificand; 3407*0b57cec5SDimitry Andric 3408*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 3409*0b57cec5SDimitry Andric myexponent = exponent+127; //bias 3410*0b57cec5SDimitry Andric mysignificand = (uint32_t)*significandParts(); 3411*0b57cec5SDimitry Andric if (myexponent == 1 && !(mysignificand & 0x800000)) 3412*0b57cec5SDimitry Andric myexponent = 0; // denormal 3413*0b57cec5SDimitry Andric } else if (category==fcZero) { 3414*0b57cec5SDimitry Andric myexponent = 0; 3415*0b57cec5SDimitry Andric mysignificand = 0; 3416*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 3417*0b57cec5SDimitry Andric myexponent = 0xff; 3418*0b57cec5SDimitry Andric mysignificand = 0; 3419*0b57cec5SDimitry Andric } else { 3420*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category!"); 3421*0b57cec5SDimitry Andric myexponent = 0xff; 3422*0b57cec5SDimitry Andric mysignificand = (uint32_t)*significandParts(); 3423*0b57cec5SDimitry Andric } 3424*0b57cec5SDimitry Andric 3425*0b57cec5SDimitry Andric return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) | 3426*0b57cec5SDimitry Andric (mysignificand & 0x7fffff))); 3427*0b57cec5SDimitry Andric } 3428*0b57cec5SDimitry Andric 34295ffd83dbSDimitry Andric APInt IEEEFloat::convertBFloatAPFloatToAPInt() const { 34305ffd83dbSDimitry Andric assert(semantics == (const llvm::fltSemantics *)&semBFloat); 34315ffd83dbSDimitry Andric assert(partCount() == 1); 34325ffd83dbSDimitry Andric 34335ffd83dbSDimitry Andric uint32_t myexponent, mysignificand; 34345ffd83dbSDimitry Andric 34355ffd83dbSDimitry Andric if (isFiniteNonZero()) { 34365ffd83dbSDimitry Andric myexponent = exponent + 127; // bias 34375ffd83dbSDimitry Andric mysignificand = (uint32_t)*significandParts(); 34385ffd83dbSDimitry Andric if (myexponent == 1 && !(mysignificand & 0x80)) 34395ffd83dbSDimitry Andric myexponent = 0; // denormal 34405ffd83dbSDimitry Andric } else if (category == fcZero) { 34415ffd83dbSDimitry Andric myexponent = 0; 34425ffd83dbSDimitry Andric mysignificand = 0; 34435ffd83dbSDimitry Andric } else if (category == fcInfinity) { 34445ffd83dbSDimitry Andric myexponent = 0xff; 34455ffd83dbSDimitry Andric mysignificand = 0; 34465ffd83dbSDimitry Andric } else { 34475ffd83dbSDimitry Andric assert(category == fcNaN && "Unknown category!"); 34485ffd83dbSDimitry Andric myexponent = 0xff; 34495ffd83dbSDimitry Andric mysignificand = (uint32_t)*significandParts(); 34505ffd83dbSDimitry Andric } 34515ffd83dbSDimitry Andric 34525ffd83dbSDimitry Andric return APInt(16, (((sign & 1) << 15) | ((myexponent & 0xff) << 7) | 34535ffd83dbSDimitry Andric (mysignificand & 0x7f))); 34545ffd83dbSDimitry Andric } 34555ffd83dbSDimitry Andric 3456*0b57cec5SDimitry Andric APInt IEEEFloat::convertHalfAPFloatToAPInt() const { 3457*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEhalf); 3458*0b57cec5SDimitry Andric assert(partCount()==1); 3459*0b57cec5SDimitry Andric 3460*0b57cec5SDimitry Andric uint32_t myexponent, mysignificand; 3461*0b57cec5SDimitry Andric 3462*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 3463*0b57cec5SDimitry Andric myexponent = exponent+15; //bias 3464*0b57cec5SDimitry Andric mysignificand = (uint32_t)*significandParts(); 3465*0b57cec5SDimitry Andric if (myexponent == 1 && !(mysignificand & 0x400)) 3466*0b57cec5SDimitry Andric myexponent = 0; // denormal 3467*0b57cec5SDimitry Andric } else if (category==fcZero) { 3468*0b57cec5SDimitry Andric myexponent = 0; 3469*0b57cec5SDimitry Andric mysignificand = 0; 3470*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 3471*0b57cec5SDimitry Andric myexponent = 0x1f; 3472*0b57cec5SDimitry Andric mysignificand = 0; 3473*0b57cec5SDimitry Andric } else { 3474*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category!"); 3475*0b57cec5SDimitry Andric myexponent = 0x1f; 3476*0b57cec5SDimitry Andric mysignificand = (uint32_t)*significandParts(); 3477*0b57cec5SDimitry Andric } 3478*0b57cec5SDimitry Andric 3479*0b57cec5SDimitry Andric return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) | 3480*0b57cec5SDimitry Andric (mysignificand & 0x3ff))); 3481*0b57cec5SDimitry Andric } 3482*0b57cec5SDimitry Andric 3483bdd1243dSDimitry Andric APInt IEEEFloat::convertFloat8E5M2APFloatToAPInt() const { 3484bdd1243dSDimitry Andric assert(semantics == (const llvm::fltSemantics *)&semFloat8E5M2); 3485bdd1243dSDimitry Andric assert(partCount() == 1); 3486bdd1243dSDimitry Andric 3487bdd1243dSDimitry Andric uint32_t myexponent, mysignificand; 3488bdd1243dSDimitry Andric 3489bdd1243dSDimitry Andric if (isFiniteNonZero()) { 3490bdd1243dSDimitry Andric myexponent = exponent + 15; // bias 3491bdd1243dSDimitry Andric mysignificand = (uint32_t)*significandParts(); 3492bdd1243dSDimitry Andric if (myexponent == 1 && !(mysignificand & 0x4)) 3493bdd1243dSDimitry Andric myexponent = 0; // denormal 3494bdd1243dSDimitry Andric } else if (category == fcZero) { 3495bdd1243dSDimitry Andric myexponent = 0; 3496bdd1243dSDimitry Andric mysignificand = 0; 3497bdd1243dSDimitry Andric } else if (category == fcInfinity) { 3498bdd1243dSDimitry Andric myexponent = 0x1f; 3499bdd1243dSDimitry Andric mysignificand = 0; 3500bdd1243dSDimitry Andric } else { 3501bdd1243dSDimitry Andric assert(category == fcNaN && "Unknown category!"); 3502bdd1243dSDimitry Andric myexponent = 0x1f; 3503bdd1243dSDimitry Andric mysignificand = (uint32_t)*significandParts(); 3504bdd1243dSDimitry Andric } 3505bdd1243dSDimitry Andric 3506bdd1243dSDimitry Andric return APInt(8, (((sign & 1) << 7) | ((myexponent & 0x1f) << 2) | 3507bdd1243dSDimitry Andric (mysignificand & 0x3))); 3508bdd1243dSDimitry Andric } 3509bdd1243dSDimitry Andric 3510bdd1243dSDimitry Andric APInt IEEEFloat::convertFloat8E4M3FNAPFloatToAPInt() const { 3511bdd1243dSDimitry Andric assert(semantics == (const llvm::fltSemantics *)&semFloat8E4M3FN); 3512bdd1243dSDimitry Andric assert(partCount() == 1); 3513bdd1243dSDimitry Andric 3514bdd1243dSDimitry Andric uint32_t myexponent, mysignificand; 3515bdd1243dSDimitry Andric 3516bdd1243dSDimitry Andric if (isFiniteNonZero()) { 3517bdd1243dSDimitry Andric myexponent = exponent + 7; // bias 3518bdd1243dSDimitry Andric mysignificand = (uint32_t)*significandParts(); 3519bdd1243dSDimitry Andric if (myexponent == 1 && !(mysignificand & 0x8)) 3520bdd1243dSDimitry Andric myexponent = 0; // denormal 3521bdd1243dSDimitry Andric } else if (category == fcZero) { 3522bdd1243dSDimitry Andric myexponent = 0; 3523bdd1243dSDimitry Andric mysignificand = 0; 3524bdd1243dSDimitry Andric } else if (category == fcInfinity) { 3525bdd1243dSDimitry Andric myexponent = 0xf; 3526bdd1243dSDimitry Andric mysignificand = 0; 3527bdd1243dSDimitry Andric } else { 3528bdd1243dSDimitry Andric assert(category == fcNaN && "Unknown category!"); 3529bdd1243dSDimitry Andric myexponent = 0xf; 3530bdd1243dSDimitry Andric mysignificand = (uint32_t)*significandParts(); 3531bdd1243dSDimitry Andric } 3532bdd1243dSDimitry Andric 3533bdd1243dSDimitry Andric return APInt(8, (((sign & 1) << 7) | ((myexponent & 0xf) << 3) | 3534bdd1243dSDimitry Andric (mysignificand & 0x7))); 3535bdd1243dSDimitry Andric } 3536bdd1243dSDimitry Andric 3537*0b57cec5SDimitry Andric // This function creates an APInt that is just a bit map of the floating 3538*0b57cec5SDimitry Andric // point constant as it would appear in memory. It is not a conversion, 3539*0b57cec5SDimitry Andric // and treating the result as a normal integer is unlikely to be useful. 3540*0b57cec5SDimitry Andric 3541*0b57cec5SDimitry Andric APInt IEEEFloat::bitcastToAPInt() const { 3542*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics*)&semIEEEhalf) 3543*0b57cec5SDimitry Andric return convertHalfAPFloatToAPInt(); 3544*0b57cec5SDimitry Andric 35455ffd83dbSDimitry Andric if (semantics == (const llvm::fltSemantics *)&semBFloat) 35465ffd83dbSDimitry Andric return convertBFloatAPFloatToAPInt(); 35475ffd83dbSDimitry Andric 3548*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics*)&semIEEEsingle) 3549*0b57cec5SDimitry Andric return convertFloatAPFloatToAPInt(); 3550*0b57cec5SDimitry Andric 3551*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics*)&semIEEEdouble) 3552*0b57cec5SDimitry Andric return convertDoubleAPFloatToAPInt(); 3553*0b57cec5SDimitry Andric 3554*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics*)&semIEEEquad) 3555*0b57cec5SDimitry Andric return convertQuadrupleAPFloatToAPInt(); 3556*0b57cec5SDimitry Andric 3557*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy) 3558*0b57cec5SDimitry Andric return convertPPCDoubleDoubleAPFloatToAPInt(); 3559*0b57cec5SDimitry Andric 3560bdd1243dSDimitry Andric if (semantics == (const llvm::fltSemantics *)&semFloat8E5M2) 3561bdd1243dSDimitry Andric return convertFloat8E5M2APFloatToAPInt(); 3562bdd1243dSDimitry Andric 3563bdd1243dSDimitry Andric if (semantics == (const llvm::fltSemantics *)&semFloat8E4M3FN) 3564bdd1243dSDimitry Andric return convertFloat8E4M3FNAPFloatToAPInt(); 3565bdd1243dSDimitry Andric 3566*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended && 3567*0b57cec5SDimitry Andric "unknown format!"); 3568*0b57cec5SDimitry Andric return convertF80LongDoubleAPFloatToAPInt(); 3569*0b57cec5SDimitry Andric } 3570*0b57cec5SDimitry Andric 3571*0b57cec5SDimitry Andric float IEEEFloat::convertToFloat() const { 3572*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle && 3573*0b57cec5SDimitry Andric "Float semantics are not IEEEsingle"); 3574*0b57cec5SDimitry Andric APInt api = bitcastToAPInt(); 3575*0b57cec5SDimitry Andric return api.bitsToFloat(); 3576*0b57cec5SDimitry Andric } 3577*0b57cec5SDimitry Andric 3578*0b57cec5SDimitry Andric double IEEEFloat::convertToDouble() const { 3579*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble && 3580*0b57cec5SDimitry Andric "Float semantics are not IEEEdouble"); 3581*0b57cec5SDimitry Andric APInt api = bitcastToAPInt(); 3582*0b57cec5SDimitry Andric return api.bitsToDouble(); 3583*0b57cec5SDimitry Andric } 3584*0b57cec5SDimitry Andric 3585*0b57cec5SDimitry Andric /// Integer bit is explicit in this format. Intel hardware (387 and later) 3586*0b57cec5SDimitry Andric /// does not support these bit patterns: 3587*0b57cec5SDimitry Andric /// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity") 3588*0b57cec5SDimitry Andric /// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN") 3589*0b57cec5SDimitry Andric /// exponent!=0 nor all 1's, integer bit 0 ("unnormal") 3590*0b57cec5SDimitry Andric /// exponent = 0, integer bit 1 ("pseudodenormal") 3591*0b57cec5SDimitry Andric /// At the moment, the first three are treated as NaNs, the last one as Normal. 3592*0b57cec5SDimitry Andric void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) { 3593*0b57cec5SDimitry Andric uint64_t i1 = api.getRawData()[0]; 3594*0b57cec5SDimitry Andric uint64_t i2 = api.getRawData()[1]; 3595*0b57cec5SDimitry Andric uint64_t myexponent = (i2 & 0x7fff); 3596*0b57cec5SDimitry Andric uint64_t mysignificand = i1; 3597*0b57cec5SDimitry Andric uint8_t myintegerbit = mysignificand >> 63; 3598*0b57cec5SDimitry Andric 3599*0b57cec5SDimitry Andric initialize(&semX87DoubleExtended); 3600*0b57cec5SDimitry Andric assert(partCount()==2); 3601*0b57cec5SDimitry Andric 3602*0b57cec5SDimitry Andric sign = static_cast<unsigned int>(i2>>15); 3603*0b57cec5SDimitry Andric if (myexponent == 0 && mysignificand == 0) { 3604e8d8bef9SDimitry Andric makeZero(sign); 3605*0b57cec5SDimitry Andric } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) { 3606e8d8bef9SDimitry Andric makeInf(sign); 3607*0b57cec5SDimitry Andric } else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) || 3608*0b57cec5SDimitry Andric (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) { 3609*0b57cec5SDimitry Andric category = fcNaN; 3610e8d8bef9SDimitry Andric exponent = exponentNaN(); 3611*0b57cec5SDimitry Andric significandParts()[0] = mysignificand; 3612*0b57cec5SDimitry Andric significandParts()[1] = 0; 3613*0b57cec5SDimitry Andric } else { 3614*0b57cec5SDimitry Andric category = fcNormal; 3615*0b57cec5SDimitry Andric exponent = myexponent - 16383; 3616*0b57cec5SDimitry Andric significandParts()[0] = mysignificand; 3617*0b57cec5SDimitry Andric significandParts()[1] = 0; 3618*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3619*0b57cec5SDimitry Andric exponent = -16382; 3620*0b57cec5SDimitry Andric } 3621*0b57cec5SDimitry Andric } 3622*0b57cec5SDimitry Andric 3623*0b57cec5SDimitry Andric void IEEEFloat::initFromPPCDoubleDoubleAPInt(const APInt &api) { 3624*0b57cec5SDimitry Andric uint64_t i1 = api.getRawData()[0]; 3625*0b57cec5SDimitry Andric uint64_t i2 = api.getRawData()[1]; 3626*0b57cec5SDimitry Andric opStatus fs; 3627*0b57cec5SDimitry Andric bool losesInfo; 3628*0b57cec5SDimitry Andric 3629*0b57cec5SDimitry Andric // Get the first double and convert to our format. 3630*0b57cec5SDimitry Andric initFromDoubleAPInt(APInt(64, i1)); 3631*0b57cec5SDimitry Andric fs = convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo); 3632*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 3633*0b57cec5SDimitry Andric (void)fs; 3634*0b57cec5SDimitry Andric 3635*0b57cec5SDimitry Andric // Unless we have a special case, add in second double. 3636*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 3637*0b57cec5SDimitry Andric IEEEFloat v(semIEEEdouble, APInt(64, i2)); 3638*0b57cec5SDimitry Andric fs = v.convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo); 3639*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 3640*0b57cec5SDimitry Andric (void)fs; 3641*0b57cec5SDimitry Andric 3642*0b57cec5SDimitry Andric add(v, rmNearestTiesToEven); 3643*0b57cec5SDimitry Andric } 3644*0b57cec5SDimitry Andric } 3645*0b57cec5SDimitry Andric 3646*0b57cec5SDimitry Andric void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) { 3647*0b57cec5SDimitry Andric uint64_t i1 = api.getRawData()[0]; 3648*0b57cec5SDimitry Andric uint64_t i2 = api.getRawData()[1]; 3649*0b57cec5SDimitry Andric uint64_t myexponent = (i2 >> 48) & 0x7fff; 3650*0b57cec5SDimitry Andric uint64_t mysignificand = i1; 3651*0b57cec5SDimitry Andric uint64_t mysignificand2 = i2 & 0xffffffffffffLL; 3652*0b57cec5SDimitry Andric 3653*0b57cec5SDimitry Andric initialize(&semIEEEquad); 3654*0b57cec5SDimitry Andric assert(partCount()==2); 3655*0b57cec5SDimitry Andric 3656*0b57cec5SDimitry Andric sign = static_cast<unsigned int>(i2>>63); 3657*0b57cec5SDimitry Andric if (myexponent==0 && 3658*0b57cec5SDimitry Andric (mysignificand==0 && mysignificand2==0)) { 3659e8d8bef9SDimitry Andric makeZero(sign); 3660*0b57cec5SDimitry Andric } else if (myexponent==0x7fff && 3661*0b57cec5SDimitry Andric (mysignificand==0 && mysignificand2==0)) { 3662e8d8bef9SDimitry Andric makeInf(sign); 3663*0b57cec5SDimitry Andric } else if (myexponent==0x7fff && 3664*0b57cec5SDimitry Andric (mysignificand!=0 || mysignificand2 !=0)) { 3665*0b57cec5SDimitry Andric category = fcNaN; 3666e8d8bef9SDimitry Andric exponent = exponentNaN(); 3667*0b57cec5SDimitry Andric significandParts()[0] = mysignificand; 3668*0b57cec5SDimitry Andric significandParts()[1] = mysignificand2; 3669*0b57cec5SDimitry Andric } else { 3670*0b57cec5SDimitry Andric category = fcNormal; 3671*0b57cec5SDimitry Andric exponent = myexponent - 16383; 3672*0b57cec5SDimitry Andric significandParts()[0] = mysignificand; 3673*0b57cec5SDimitry Andric significandParts()[1] = mysignificand2; 3674*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3675*0b57cec5SDimitry Andric exponent = -16382; 3676*0b57cec5SDimitry Andric else 3677*0b57cec5SDimitry Andric significandParts()[1] |= 0x1000000000000LL; // integer bit 3678*0b57cec5SDimitry Andric } 3679*0b57cec5SDimitry Andric } 3680*0b57cec5SDimitry Andric 3681*0b57cec5SDimitry Andric void IEEEFloat::initFromDoubleAPInt(const APInt &api) { 3682*0b57cec5SDimitry Andric uint64_t i = *api.getRawData(); 3683*0b57cec5SDimitry Andric uint64_t myexponent = (i >> 52) & 0x7ff; 3684*0b57cec5SDimitry Andric uint64_t mysignificand = i & 0xfffffffffffffLL; 3685*0b57cec5SDimitry Andric 3686*0b57cec5SDimitry Andric initialize(&semIEEEdouble); 3687*0b57cec5SDimitry Andric assert(partCount()==1); 3688*0b57cec5SDimitry Andric 3689*0b57cec5SDimitry Andric sign = static_cast<unsigned int>(i>>63); 3690*0b57cec5SDimitry Andric if (myexponent==0 && mysignificand==0) { 3691e8d8bef9SDimitry Andric makeZero(sign); 3692*0b57cec5SDimitry Andric } else if (myexponent==0x7ff && mysignificand==0) { 3693e8d8bef9SDimitry Andric makeInf(sign); 3694*0b57cec5SDimitry Andric } else if (myexponent==0x7ff && mysignificand!=0) { 3695*0b57cec5SDimitry Andric category = fcNaN; 3696e8d8bef9SDimitry Andric exponent = exponentNaN(); 3697*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3698*0b57cec5SDimitry Andric } else { 3699*0b57cec5SDimitry Andric category = fcNormal; 3700*0b57cec5SDimitry Andric exponent = myexponent - 1023; 3701*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3702*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3703*0b57cec5SDimitry Andric exponent = -1022; 3704*0b57cec5SDimitry Andric else 3705*0b57cec5SDimitry Andric *significandParts() |= 0x10000000000000LL; // integer bit 3706*0b57cec5SDimitry Andric } 3707*0b57cec5SDimitry Andric } 3708*0b57cec5SDimitry Andric 3709*0b57cec5SDimitry Andric void IEEEFloat::initFromFloatAPInt(const APInt &api) { 3710*0b57cec5SDimitry Andric uint32_t i = (uint32_t)*api.getRawData(); 3711*0b57cec5SDimitry Andric uint32_t myexponent = (i >> 23) & 0xff; 3712*0b57cec5SDimitry Andric uint32_t mysignificand = i & 0x7fffff; 3713*0b57cec5SDimitry Andric 3714*0b57cec5SDimitry Andric initialize(&semIEEEsingle); 3715*0b57cec5SDimitry Andric assert(partCount()==1); 3716*0b57cec5SDimitry Andric 3717*0b57cec5SDimitry Andric sign = i >> 31; 3718*0b57cec5SDimitry Andric if (myexponent==0 && mysignificand==0) { 3719e8d8bef9SDimitry Andric makeZero(sign); 3720*0b57cec5SDimitry Andric } else if (myexponent==0xff && mysignificand==0) { 3721e8d8bef9SDimitry Andric makeInf(sign); 3722*0b57cec5SDimitry Andric } else if (myexponent==0xff && mysignificand!=0) { 3723*0b57cec5SDimitry Andric category = fcNaN; 3724e8d8bef9SDimitry Andric exponent = exponentNaN(); 3725*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3726*0b57cec5SDimitry Andric } else { 3727*0b57cec5SDimitry Andric category = fcNormal; 3728*0b57cec5SDimitry Andric exponent = myexponent - 127; //bias 3729*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3730*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3731*0b57cec5SDimitry Andric exponent = -126; 3732*0b57cec5SDimitry Andric else 3733*0b57cec5SDimitry Andric *significandParts() |= 0x800000; // integer bit 3734*0b57cec5SDimitry Andric } 3735*0b57cec5SDimitry Andric } 3736*0b57cec5SDimitry Andric 37375ffd83dbSDimitry Andric void IEEEFloat::initFromBFloatAPInt(const APInt &api) { 37385ffd83dbSDimitry Andric uint32_t i = (uint32_t)*api.getRawData(); 37395ffd83dbSDimitry Andric uint32_t myexponent = (i >> 7) & 0xff; 37405ffd83dbSDimitry Andric uint32_t mysignificand = i & 0x7f; 37415ffd83dbSDimitry Andric 37425ffd83dbSDimitry Andric initialize(&semBFloat); 37435ffd83dbSDimitry Andric assert(partCount() == 1); 37445ffd83dbSDimitry Andric 37455ffd83dbSDimitry Andric sign = i >> 15; 37465ffd83dbSDimitry Andric if (myexponent == 0 && mysignificand == 0) { 3747e8d8bef9SDimitry Andric makeZero(sign); 37485ffd83dbSDimitry Andric } else if (myexponent == 0xff && mysignificand == 0) { 3749e8d8bef9SDimitry Andric makeInf(sign); 37505ffd83dbSDimitry Andric } else if (myexponent == 0xff && mysignificand != 0) { 37515ffd83dbSDimitry Andric category = fcNaN; 3752e8d8bef9SDimitry Andric exponent = exponentNaN(); 37535ffd83dbSDimitry Andric *significandParts() = mysignificand; 37545ffd83dbSDimitry Andric } else { 37555ffd83dbSDimitry Andric category = fcNormal; 37565ffd83dbSDimitry Andric exponent = myexponent - 127; // bias 37575ffd83dbSDimitry Andric *significandParts() = mysignificand; 37585ffd83dbSDimitry Andric if (myexponent == 0) // denormal 37595ffd83dbSDimitry Andric exponent = -126; 37605ffd83dbSDimitry Andric else 37615ffd83dbSDimitry Andric *significandParts() |= 0x80; // integer bit 37625ffd83dbSDimitry Andric } 37635ffd83dbSDimitry Andric } 37645ffd83dbSDimitry Andric 3765*0b57cec5SDimitry Andric void IEEEFloat::initFromHalfAPInt(const APInt &api) { 3766*0b57cec5SDimitry Andric uint32_t i = (uint32_t)*api.getRawData(); 3767*0b57cec5SDimitry Andric uint32_t myexponent = (i >> 10) & 0x1f; 3768*0b57cec5SDimitry Andric uint32_t mysignificand = i & 0x3ff; 3769*0b57cec5SDimitry Andric 3770*0b57cec5SDimitry Andric initialize(&semIEEEhalf); 3771*0b57cec5SDimitry Andric assert(partCount()==1); 3772*0b57cec5SDimitry Andric 3773*0b57cec5SDimitry Andric sign = i >> 15; 3774*0b57cec5SDimitry Andric if (myexponent==0 && mysignificand==0) { 3775e8d8bef9SDimitry Andric makeZero(sign); 3776*0b57cec5SDimitry Andric } else if (myexponent==0x1f && mysignificand==0) { 3777e8d8bef9SDimitry Andric makeInf(sign); 3778*0b57cec5SDimitry Andric } else if (myexponent==0x1f && mysignificand!=0) { 3779*0b57cec5SDimitry Andric category = fcNaN; 3780e8d8bef9SDimitry Andric exponent = exponentNaN(); 3781*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3782*0b57cec5SDimitry Andric } else { 3783*0b57cec5SDimitry Andric category = fcNormal; 3784*0b57cec5SDimitry Andric exponent = myexponent - 15; //bias 3785*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3786*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3787*0b57cec5SDimitry Andric exponent = -14; 3788*0b57cec5SDimitry Andric else 3789*0b57cec5SDimitry Andric *significandParts() |= 0x400; // integer bit 3790*0b57cec5SDimitry Andric } 3791*0b57cec5SDimitry Andric } 3792*0b57cec5SDimitry Andric 3793bdd1243dSDimitry Andric void IEEEFloat::initFromFloat8E5M2APInt(const APInt &api) { 3794bdd1243dSDimitry Andric uint32_t i = (uint32_t)*api.getRawData(); 3795bdd1243dSDimitry Andric uint32_t myexponent = (i >> 2) & 0x1f; 3796bdd1243dSDimitry Andric uint32_t mysignificand = i & 0x3; 3797bdd1243dSDimitry Andric 3798bdd1243dSDimitry Andric initialize(&semFloat8E5M2); 3799bdd1243dSDimitry Andric assert(partCount() == 1); 3800bdd1243dSDimitry Andric 3801bdd1243dSDimitry Andric sign = i >> 7; 3802bdd1243dSDimitry Andric if (myexponent == 0 && mysignificand == 0) { 3803bdd1243dSDimitry Andric makeZero(sign); 3804bdd1243dSDimitry Andric } else if (myexponent == 0x1f && mysignificand == 0) { 3805bdd1243dSDimitry Andric makeInf(sign); 3806bdd1243dSDimitry Andric } else if (myexponent == 0x1f && mysignificand != 0) { 3807bdd1243dSDimitry Andric category = fcNaN; 3808bdd1243dSDimitry Andric exponent = exponentNaN(); 3809bdd1243dSDimitry Andric *significandParts() = mysignificand; 3810bdd1243dSDimitry Andric } else { 3811bdd1243dSDimitry Andric category = fcNormal; 3812bdd1243dSDimitry Andric exponent = myexponent - 15; // bias 3813bdd1243dSDimitry Andric *significandParts() = mysignificand; 3814bdd1243dSDimitry Andric if (myexponent == 0) // denormal 3815bdd1243dSDimitry Andric exponent = -14; 3816bdd1243dSDimitry Andric else 3817bdd1243dSDimitry Andric *significandParts() |= 0x4; // integer bit 3818bdd1243dSDimitry Andric } 3819bdd1243dSDimitry Andric } 3820bdd1243dSDimitry Andric 3821bdd1243dSDimitry Andric void IEEEFloat::initFromFloat8E4M3FNAPInt(const APInt &api) { 3822bdd1243dSDimitry Andric uint32_t i = (uint32_t)*api.getRawData(); 3823bdd1243dSDimitry Andric uint32_t myexponent = (i >> 3) & 0xf; 3824bdd1243dSDimitry Andric uint32_t mysignificand = i & 0x7; 3825bdd1243dSDimitry Andric 3826bdd1243dSDimitry Andric initialize(&semFloat8E4M3FN); 3827bdd1243dSDimitry Andric assert(partCount() == 1); 3828bdd1243dSDimitry Andric 3829bdd1243dSDimitry Andric sign = i >> 7; 3830bdd1243dSDimitry Andric if (myexponent == 0 && mysignificand == 0) { 3831bdd1243dSDimitry Andric makeZero(sign); 3832bdd1243dSDimitry Andric } else if (myexponent == 0xf && mysignificand == 7) { 3833bdd1243dSDimitry Andric category = fcNaN; 3834bdd1243dSDimitry Andric exponent = exponentNaN(); 3835bdd1243dSDimitry Andric *significandParts() = mysignificand; 3836bdd1243dSDimitry Andric } else { 3837bdd1243dSDimitry Andric category = fcNormal; 3838bdd1243dSDimitry Andric exponent = myexponent - 7; // bias 3839bdd1243dSDimitry Andric *significandParts() = mysignificand; 3840bdd1243dSDimitry Andric if (myexponent == 0) // denormal 3841bdd1243dSDimitry Andric exponent = -6; 3842bdd1243dSDimitry Andric else 3843bdd1243dSDimitry Andric *significandParts() |= 0x8; // integer bit 3844bdd1243dSDimitry Andric } 3845bdd1243dSDimitry Andric } 3846bdd1243dSDimitry Andric 3847bdd1243dSDimitry Andric /// Treat api as containing the bits of a floating point number. 3848*0b57cec5SDimitry Andric void IEEEFloat::initFromAPInt(const fltSemantics *Sem, const APInt &api) { 3849349cc55cSDimitry Andric assert(api.getBitWidth() == Sem->sizeInBits); 3850*0b57cec5SDimitry Andric if (Sem == &semIEEEhalf) 3851*0b57cec5SDimitry Andric return initFromHalfAPInt(api); 38525ffd83dbSDimitry Andric if (Sem == &semBFloat) 38535ffd83dbSDimitry Andric return initFromBFloatAPInt(api); 3854*0b57cec5SDimitry Andric if (Sem == &semIEEEsingle) 3855*0b57cec5SDimitry Andric return initFromFloatAPInt(api); 3856*0b57cec5SDimitry Andric if (Sem == &semIEEEdouble) 3857*0b57cec5SDimitry Andric return initFromDoubleAPInt(api); 3858*0b57cec5SDimitry Andric if (Sem == &semX87DoubleExtended) 3859*0b57cec5SDimitry Andric return initFromF80LongDoubleAPInt(api); 3860*0b57cec5SDimitry Andric if (Sem == &semIEEEquad) 3861*0b57cec5SDimitry Andric return initFromQuadrupleAPInt(api); 3862*0b57cec5SDimitry Andric if (Sem == &semPPCDoubleDoubleLegacy) 3863*0b57cec5SDimitry Andric return initFromPPCDoubleDoubleAPInt(api); 3864bdd1243dSDimitry Andric if (Sem == &semFloat8E5M2) 3865bdd1243dSDimitry Andric return initFromFloat8E5M2APInt(api); 3866bdd1243dSDimitry Andric if (Sem == &semFloat8E4M3FN) 3867bdd1243dSDimitry Andric return initFromFloat8E4M3FNAPInt(api); 3868*0b57cec5SDimitry Andric 3869*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 3870*0b57cec5SDimitry Andric } 3871*0b57cec5SDimitry Andric 3872*0b57cec5SDimitry Andric /// Make this number the largest magnitude normal number in the given 3873*0b57cec5SDimitry Andric /// semantics. 3874*0b57cec5SDimitry Andric void IEEEFloat::makeLargest(bool Negative) { 3875*0b57cec5SDimitry Andric // We want (in interchange format): 3876*0b57cec5SDimitry Andric // sign = {Negative} 3877*0b57cec5SDimitry Andric // exponent = 1..10 3878*0b57cec5SDimitry Andric // significand = 1..1 3879*0b57cec5SDimitry Andric category = fcNormal; 3880*0b57cec5SDimitry Andric sign = Negative; 3881*0b57cec5SDimitry Andric exponent = semantics->maxExponent; 3882*0b57cec5SDimitry Andric 3883*0b57cec5SDimitry Andric // Use memset to set all but the highest integerPart to all ones. 3884*0b57cec5SDimitry Andric integerPart *significand = significandParts(); 3885*0b57cec5SDimitry Andric unsigned PartCount = partCount(); 3886*0b57cec5SDimitry Andric memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1)); 3887*0b57cec5SDimitry Andric 3888*0b57cec5SDimitry Andric // Set the high integerPart especially setting all unused top bits for 3889*0b57cec5SDimitry Andric // internal consistency. 3890*0b57cec5SDimitry Andric const unsigned NumUnusedHighBits = 3891*0b57cec5SDimitry Andric PartCount*integerPartWidth - semantics->precision; 3892*0b57cec5SDimitry Andric significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth) 3893*0b57cec5SDimitry Andric ? (~integerPart(0) >> NumUnusedHighBits) 3894*0b57cec5SDimitry Andric : 0; 3895bdd1243dSDimitry Andric 3896bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) 3897bdd1243dSDimitry Andric significand[0] &= ~integerPart(1); 3898*0b57cec5SDimitry Andric } 3899*0b57cec5SDimitry Andric 3900*0b57cec5SDimitry Andric /// Make this number the smallest magnitude denormal number in the given 3901*0b57cec5SDimitry Andric /// semantics. 3902*0b57cec5SDimitry Andric void IEEEFloat::makeSmallest(bool Negative) { 3903*0b57cec5SDimitry Andric // We want (in interchange format): 3904*0b57cec5SDimitry Andric // sign = {Negative} 3905*0b57cec5SDimitry Andric // exponent = 0..0 3906*0b57cec5SDimitry Andric // significand = 0..01 3907*0b57cec5SDimitry Andric category = fcNormal; 3908*0b57cec5SDimitry Andric sign = Negative; 3909*0b57cec5SDimitry Andric exponent = semantics->minExponent; 3910*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 1, partCount()); 3911*0b57cec5SDimitry Andric } 3912*0b57cec5SDimitry Andric 3913*0b57cec5SDimitry Andric void IEEEFloat::makeSmallestNormalized(bool Negative) { 3914*0b57cec5SDimitry Andric // We want (in interchange format): 3915*0b57cec5SDimitry Andric // sign = {Negative} 3916*0b57cec5SDimitry Andric // exponent = 0..0 3917*0b57cec5SDimitry Andric // significand = 10..0 3918*0b57cec5SDimitry Andric 3919*0b57cec5SDimitry Andric category = fcNormal; 3920*0b57cec5SDimitry Andric zeroSignificand(); 3921*0b57cec5SDimitry Andric sign = Negative; 3922*0b57cec5SDimitry Andric exponent = semantics->minExponent; 3923bdd1243dSDimitry Andric APInt::tcSetBit(significandParts(), semantics->precision - 1); 3924*0b57cec5SDimitry Andric } 3925*0b57cec5SDimitry Andric 3926*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const fltSemantics &Sem, const APInt &API) { 3927*0b57cec5SDimitry Andric initFromAPInt(&Sem, API); 3928*0b57cec5SDimitry Andric } 3929*0b57cec5SDimitry Andric 3930*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(float f) { 3931*0b57cec5SDimitry Andric initFromAPInt(&semIEEEsingle, APInt::floatToBits(f)); 3932*0b57cec5SDimitry Andric } 3933*0b57cec5SDimitry Andric 3934*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(double d) { 3935*0b57cec5SDimitry Andric initFromAPInt(&semIEEEdouble, APInt::doubleToBits(d)); 3936*0b57cec5SDimitry Andric } 3937*0b57cec5SDimitry Andric 3938*0b57cec5SDimitry Andric namespace { 3939*0b57cec5SDimitry Andric void append(SmallVectorImpl<char> &Buffer, StringRef Str) { 3940*0b57cec5SDimitry Andric Buffer.append(Str.begin(), Str.end()); 3941*0b57cec5SDimitry Andric } 3942*0b57cec5SDimitry Andric 3943*0b57cec5SDimitry Andric /// Removes data from the given significand until it is no more 3944*0b57cec5SDimitry Andric /// precise than is required for the desired precision. 3945*0b57cec5SDimitry Andric void AdjustToPrecision(APInt &significand, 3946*0b57cec5SDimitry Andric int &exp, unsigned FormatPrecision) { 3947*0b57cec5SDimitry Andric unsigned bits = significand.getActiveBits(); 3948*0b57cec5SDimitry Andric 3949*0b57cec5SDimitry Andric // 196/59 is a very slight overestimate of lg_2(10). 3950*0b57cec5SDimitry Andric unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59; 3951*0b57cec5SDimitry Andric 3952*0b57cec5SDimitry Andric if (bits <= bitsRequired) return; 3953*0b57cec5SDimitry Andric 3954*0b57cec5SDimitry Andric unsigned tensRemovable = (bits - bitsRequired) * 59 / 196; 3955*0b57cec5SDimitry Andric if (!tensRemovable) return; 3956*0b57cec5SDimitry Andric 3957*0b57cec5SDimitry Andric exp += tensRemovable; 3958*0b57cec5SDimitry Andric 3959*0b57cec5SDimitry Andric APInt divisor(significand.getBitWidth(), 1); 3960*0b57cec5SDimitry Andric APInt powten(significand.getBitWidth(), 10); 3961*0b57cec5SDimitry Andric while (true) { 3962*0b57cec5SDimitry Andric if (tensRemovable & 1) 3963*0b57cec5SDimitry Andric divisor *= powten; 3964*0b57cec5SDimitry Andric tensRemovable >>= 1; 3965*0b57cec5SDimitry Andric if (!tensRemovable) break; 3966*0b57cec5SDimitry Andric powten *= powten; 3967*0b57cec5SDimitry Andric } 3968*0b57cec5SDimitry Andric 3969*0b57cec5SDimitry Andric significand = significand.udiv(divisor); 3970*0b57cec5SDimitry Andric 3971*0b57cec5SDimitry Andric // Truncate the significand down to its active bit count. 3972*0b57cec5SDimitry Andric significand = significand.trunc(significand.getActiveBits()); 3973*0b57cec5SDimitry Andric } 3974*0b57cec5SDimitry Andric 3975*0b57cec5SDimitry Andric 3976*0b57cec5SDimitry Andric void AdjustToPrecision(SmallVectorImpl<char> &buffer, 3977*0b57cec5SDimitry Andric int &exp, unsigned FormatPrecision) { 3978*0b57cec5SDimitry Andric unsigned N = buffer.size(); 3979*0b57cec5SDimitry Andric if (N <= FormatPrecision) return; 3980*0b57cec5SDimitry Andric 3981*0b57cec5SDimitry Andric // The most significant figures are the last ones in the buffer. 3982*0b57cec5SDimitry Andric unsigned FirstSignificant = N - FormatPrecision; 3983*0b57cec5SDimitry Andric 3984*0b57cec5SDimitry Andric // Round. 3985*0b57cec5SDimitry Andric // FIXME: this probably shouldn't use 'round half up'. 3986*0b57cec5SDimitry Andric 3987*0b57cec5SDimitry Andric // Rounding down is just a truncation, except we also want to drop 3988*0b57cec5SDimitry Andric // trailing zeros from the new result. 3989*0b57cec5SDimitry Andric if (buffer[FirstSignificant - 1] < '5') { 3990*0b57cec5SDimitry Andric while (FirstSignificant < N && buffer[FirstSignificant] == '0') 3991*0b57cec5SDimitry Andric FirstSignificant++; 3992*0b57cec5SDimitry Andric 3993*0b57cec5SDimitry Andric exp += FirstSignificant; 3994*0b57cec5SDimitry Andric buffer.erase(&buffer[0], &buffer[FirstSignificant]); 3995*0b57cec5SDimitry Andric return; 3996*0b57cec5SDimitry Andric } 3997*0b57cec5SDimitry Andric 3998*0b57cec5SDimitry Andric // Rounding up requires a decimal add-with-carry. If we continue 3999*0b57cec5SDimitry Andric // the carry, the newly-introduced zeros will just be truncated. 4000*0b57cec5SDimitry Andric for (unsigned I = FirstSignificant; I != N; ++I) { 4001*0b57cec5SDimitry Andric if (buffer[I] == '9') { 4002*0b57cec5SDimitry Andric FirstSignificant++; 4003*0b57cec5SDimitry Andric } else { 4004*0b57cec5SDimitry Andric buffer[I]++; 4005*0b57cec5SDimitry Andric break; 4006*0b57cec5SDimitry Andric } 4007*0b57cec5SDimitry Andric } 4008*0b57cec5SDimitry Andric 4009*0b57cec5SDimitry Andric // If we carried through, we have exactly one digit of precision. 4010*0b57cec5SDimitry Andric if (FirstSignificant == N) { 4011*0b57cec5SDimitry Andric exp += FirstSignificant; 4012*0b57cec5SDimitry Andric buffer.clear(); 4013*0b57cec5SDimitry Andric buffer.push_back('1'); 4014*0b57cec5SDimitry Andric return; 4015*0b57cec5SDimitry Andric } 4016*0b57cec5SDimitry Andric 4017*0b57cec5SDimitry Andric exp += FirstSignificant; 4018*0b57cec5SDimitry Andric buffer.erase(&buffer[0], &buffer[FirstSignificant]); 4019*0b57cec5SDimitry Andric } 4020fe6060f1SDimitry Andric } // namespace 4021*0b57cec5SDimitry Andric 4022*0b57cec5SDimitry Andric void IEEEFloat::toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision, 4023*0b57cec5SDimitry Andric unsigned FormatMaxPadding, bool TruncateZero) const { 4024*0b57cec5SDimitry Andric switch (category) { 4025*0b57cec5SDimitry Andric case fcInfinity: 4026*0b57cec5SDimitry Andric if (isNegative()) 4027*0b57cec5SDimitry Andric return append(Str, "-Inf"); 4028*0b57cec5SDimitry Andric else 4029*0b57cec5SDimitry Andric return append(Str, "+Inf"); 4030*0b57cec5SDimitry Andric 4031*0b57cec5SDimitry Andric case fcNaN: return append(Str, "NaN"); 4032*0b57cec5SDimitry Andric 4033*0b57cec5SDimitry Andric case fcZero: 4034*0b57cec5SDimitry Andric if (isNegative()) 4035*0b57cec5SDimitry Andric Str.push_back('-'); 4036*0b57cec5SDimitry Andric 4037*0b57cec5SDimitry Andric if (!FormatMaxPadding) { 4038*0b57cec5SDimitry Andric if (TruncateZero) 4039*0b57cec5SDimitry Andric append(Str, "0.0E+0"); 4040*0b57cec5SDimitry Andric else { 4041*0b57cec5SDimitry Andric append(Str, "0.0"); 4042*0b57cec5SDimitry Andric if (FormatPrecision > 1) 4043*0b57cec5SDimitry Andric Str.append(FormatPrecision - 1, '0'); 4044*0b57cec5SDimitry Andric append(Str, "e+00"); 4045*0b57cec5SDimitry Andric } 4046*0b57cec5SDimitry Andric } else 4047*0b57cec5SDimitry Andric Str.push_back('0'); 4048*0b57cec5SDimitry Andric return; 4049*0b57cec5SDimitry Andric 4050*0b57cec5SDimitry Andric case fcNormal: 4051*0b57cec5SDimitry Andric break; 4052*0b57cec5SDimitry Andric } 4053*0b57cec5SDimitry Andric 4054*0b57cec5SDimitry Andric if (isNegative()) 4055*0b57cec5SDimitry Andric Str.push_back('-'); 4056*0b57cec5SDimitry Andric 4057*0b57cec5SDimitry Andric // Decompose the number into an APInt and an exponent. 4058*0b57cec5SDimitry Andric int exp = exponent - ((int) semantics->precision - 1); 4059bdd1243dSDimitry Andric APInt significand( 4060bdd1243dSDimitry Andric semantics->precision, 4061bdd1243dSDimitry Andric ArrayRef(significandParts(), partCountForBits(semantics->precision))); 4062*0b57cec5SDimitry Andric 4063*0b57cec5SDimitry Andric // Set FormatPrecision if zero. We want to do this before we 4064*0b57cec5SDimitry Andric // truncate trailing zeros, as those are part of the precision. 4065*0b57cec5SDimitry Andric if (!FormatPrecision) { 4066*0b57cec5SDimitry Andric // We use enough digits so the number can be round-tripped back to an 4067*0b57cec5SDimitry Andric // APFloat. The formula comes from "How to Print Floating-Point Numbers 4068*0b57cec5SDimitry Andric // Accurately" by Steele and White. 4069*0b57cec5SDimitry Andric // FIXME: Using a formula based purely on the precision is conservative; 4070*0b57cec5SDimitry Andric // we can print fewer digits depending on the actual value being printed. 4071*0b57cec5SDimitry Andric 4072*0b57cec5SDimitry Andric // FormatPrecision = 2 + floor(significandBits / lg_2(10)) 4073*0b57cec5SDimitry Andric FormatPrecision = 2 + semantics->precision * 59 / 196; 4074*0b57cec5SDimitry Andric } 4075*0b57cec5SDimitry Andric 4076*0b57cec5SDimitry Andric // Ignore trailing binary zeros. 4077*0b57cec5SDimitry Andric int trailingZeros = significand.countTrailingZeros(); 4078*0b57cec5SDimitry Andric exp += trailingZeros; 4079*0b57cec5SDimitry Andric significand.lshrInPlace(trailingZeros); 4080*0b57cec5SDimitry Andric 4081*0b57cec5SDimitry Andric // Change the exponent from 2^e to 10^e. 4082*0b57cec5SDimitry Andric if (exp == 0) { 4083*0b57cec5SDimitry Andric // Nothing to do. 4084*0b57cec5SDimitry Andric } else if (exp > 0) { 4085*0b57cec5SDimitry Andric // Just shift left. 4086*0b57cec5SDimitry Andric significand = significand.zext(semantics->precision + exp); 4087*0b57cec5SDimitry Andric significand <<= exp; 4088*0b57cec5SDimitry Andric exp = 0; 4089*0b57cec5SDimitry Andric } else { /* exp < 0 */ 4090*0b57cec5SDimitry Andric int texp = -exp; 4091*0b57cec5SDimitry Andric 4092*0b57cec5SDimitry Andric // We transform this using the identity: 4093*0b57cec5SDimitry Andric // (N)(2^-e) == (N)(5^e)(10^-e) 4094*0b57cec5SDimitry Andric // This means we have to multiply N (the significand) by 5^e. 4095*0b57cec5SDimitry Andric // To avoid overflow, we have to operate on numbers large 4096*0b57cec5SDimitry Andric // enough to store N * 5^e: 4097*0b57cec5SDimitry Andric // log2(N * 5^e) == log2(N) + e * log2(5) 4098*0b57cec5SDimitry Andric // <= semantics->precision + e * 137 / 59 4099*0b57cec5SDimitry Andric // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59) 4100*0b57cec5SDimitry Andric 4101*0b57cec5SDimitry Andric unsigned precision = semantics->precision + (137 * texp + 136) / 59; 4102*0b57cec5SDimitry Andric 4103*0b57cec5SDimitry Andric // Multiply significand by 5^e. 4104*0b57cec5SDimitry Andric // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8) 4105*0b57cec5SDimitry Andric significand = significand.zext(precision); 4106*0b57cec5SDimitry Andric APInt five_to_the_i(precision, 5); 4107*0b57cec5SDimitry Andric while (true) { 4108*0b57cec5SDimitry Andric if (texp & 1) significand *= five_to_the_i; 4109*0b57cec5SDimitry Andric 4110*0b57cec5SDimitry Andric texp >>= 1; 4111*0b57cec5SDimitry Andric if (!texp) break; 4112*0b57cec5SDimitry Andric five_to_the_i *= five_to_the_i; 4113*0b57cec5SDimitry Andric } 4114*0b57cec5SDimitry Andric } 4115*0b57cec5SDimitry Andric 4116*0b57cec5SDimitry Andric AdjustToPrecision(significand, exp, FormatPrecision); 4117*0b57cec5SDimitry Andric 4118*0b57cec5SDimitry Andric SmallVector<char, 256> buffer; 4119*0b57cec5SDimitry Andric 4120*0b57cec5SDimitry Andric // Fill the buffer. 4121*0b57cec5SDimitry Andric unsigned precision = significand.getBitWidth(); 4122bdd1243dSDimitry Andric if (precision < 4) { 4123bdd1243dSDimitry Andric // We need enough precision to store the value 10. 4124bdd1243dSDimitry Andric precision = 4; 4125bdd1243dSDimitry Andric significand = significand.zext(precision); 4126bdd1243dSDimitry Andric } 4127*0b57cec5SDimitry Andric APInt ten(precision, 10); 4128*0b57cec5SDimitry Andric APInt digit(precision, 0); 4129*0b57cec5SDimitry Andric 4130*0b57cec5SDimitry Andric bool inTrail = true; 4131*0b57cec5SDimitry Andric while (significand != 0) { 4132*0b57cec5SDimitry Andric // digit <- significand % 10 4133*0b57cec5SDimitry Andric // significand <- significand / 10 4134*0b57cec5SDimitry Andric APInt::udivrem(significand, ten, significand, digit); 4135*0b57cec5SDimitry Andric 4136*0b57cec5SDimitry Andric unsigned d = digit.getZExtValue(); 4137*0b57cec5SDimitry Andric 4138*0b57cec5SDimitry Andric // Drop trailing zeros. 4139*0b57cec5SDimitry Andric if (inTrail && !d) exp++; 4140*0b57cec5SDimitry Andric else { 4141*0b57cec5SDimitry Andric buffer.push_back((char) ('0' + d)); 4142*0b57cec5SDimitry Andric inTrail = false; 4143*0b57cec5SDimitry Andric } 4144*0b57cec5SDimitry Andric } 4145*0b57cec5SDimitry Andric 4146*0b57cec5SDimitry Andric assert(!buffer.empty() && "no characters in buffer!"); 4147*0b57cec5SDimitry Andric 4148*0b57cec5SDimitry Andric // Drop down to FormatPrecision. 4149*0b57cec5SDimitry Andric // TODO: don't do more precise calculations above than are required. 4150*0b57cec5SDimitry Andric AdjustToPrecision(buffer, exp, FormatPrecision); 4151*0b57cec5SDimitry Andric 4152*0b57cec5SDimitry Andric unsigned NDigits = buffer.size(); 4153*0b57cec5SDimitry Andric 4154*0b57cec5SDimitry Andric // Check whether we should use scientific notation. 4155*0b57cec5SDimitry Andric bool FormatScientific; 4156*0b57cec5SDimitry Andric if (!FormatMaxPadding) 4157*0b57cec5SDimitry Andric FormatScientific = true; 4158*0b57cec5SDimitry Andric else { 4159*0b57cec5SDimitry Andric if (exp >= 0) { 4160*0b57cec5SDimitry Andric // 765e3 --> 765000 4161*0b57cec5SDimitry Andric // ^^^ 4162*0b57cec5SDimitry Andric // But we shouldn't make the number look more precise than it is. 4163*0b57cec5SDimitry Andric FormatScientific = ((unsigned) exp > FormatMaxPadding || 4164*0b57cec5SDimitry Andric NDigits + (unsigned) exp > FormatPrecision); 4165*0b57cec5SDimitry Andric } else { 4166*0b57cec5SDimitry Andric // Power of the most significant digit. 4167*0b57cec5SDimitry Andric int MSD = exp + (int) (NDigits - 1); 4168*0b57cec5SDimitry Andric if (MSD >= 0) { 4169*0b57cec5SDimitry Andric // 765e-2 == 7.65 4170*0b57cec5SDimitry Andric FormatScientific = false; 4171*0b57cec5SDimitry Andric } else { 4172*0b57cec5SDimitry Andric // 765e-5 == 0.00765 4173*0b57cec5SDimitry Andric // ^ ^^ 4174*0b57cec5SDimitry Andric FormatScientific = ((unsigned) -MSD) > FormatMaxPadding; 4175*0b57cec5SDimitry Andric } 4176*0b57cec5SDimitry Andric } 4177*0b57cec5SDimitry Andric } 4178*0b57cec5SDimitry Andric 4179*0b57cec5SDimitry Andric // Scientific formatting is pretty straightforward. 4180*0b57cec5SDimitry Andric if (FormatScientific) { 4181*0b57cec5SDimitry Andric exp += (NDigits - 1); 4182*0b57cec5SDimitry Andric 4183*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-1]); 4184*0b57cec5SDimitry Andric Str.push_back('.'); 4185*0b57cec5SDimitry Andric if (NDigits == 1 && TruncateZero) 4186*0b57cec5SDimitry Andric Str.push_back('0'); 4187*0b57cec5SDimitry Andric else 4188*0b57cec5SDimitry Andric for (unsigned I = 1; I != NDigits; ++I) 4189*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-1-I]); 4190*0b57cec5SDimitry Andric // Fill with zeros up to FormatPrecision. 4191*0b57cec5SDimitry Andric if (!TruncateZero && FormatPrecision > NDigits - 1) 4192*0b57cec5SDimitry Andric Str.append(FormatPrecision - NDigits + 1, '0'); 4193*0b57cec5SDimitry Andric // For !TruncateZero we use lower 'e'. 4194*0b57cec5SDimitry Andric Str.push_back(TruncateZero ? 'E' : 'e'); 4195*0b57cec5SDimitry Andric 4196*0b57cec5SDimitry Andric Str.push_back(exp >= 0 ? '+' : '-'); 4197*0b57cec5SDimitry Andric if (exp < 0) exp = -exp; 4198*0b57cec5SDimitry Andric SmallVector<char, 6> expbuf; 4199*0b57cec5SDimitry Andric do { 4200*0b57cec5SDimitry Andric expbuf.push_back((char) ('0' + (exp % 10))); 4201*0b57cec5SDimitry Andric exp /= 10; 4202*0b57cec5SDimitry Andric } while (exp); 4203*0b57cec5SDimitry Andric // Exponent always at least two digits if we do not truncate zeros. 4204*0b57cec5SDimitry Andric if (!TruncateZero && expbuf.size() < 2) 4205*0b57cec5SDimitry Andric expbuf.push_back('0'); 4206*0b57cec5SDimitry Andric for (unsigned I = 0, E = expbuf.size(); I != E; ++I) 4207*0b57cec5SDimitry Andric Str.push_back(expbuf[E-1-I]); 4208*0b57cec5SDimitry Andric return; 4209*0b57cec5SDimitry Andric } 4210*0b57cec5SDimitry Andric 4211*0b57cec5SDimitry Andric // Non-scientific, positive exponents. 4212*0b57cec5SDimitry Andric if (exp >= 0) { 4213*0b57cec5SDimitry Andric for (unsigned I = 0; I != NDigits; ++I) 4214*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-1-I]); 4215*0b57cec5SDimitry Andric for (unsigned I = 0; I != (unsigned) exp; ++I) 4216*0b57cec5SDimitry Andric Str.push_back('0'); 4217*0b57cec5SDimitry Andric return; 4218*0b57cec5SDimitry Andric } 4219*0b57cec5SDimitry Andric 4220*0b57cec5SDimitry Andric // Non-scientific, negative exponents. 4221*0b57cec5SDimitry Andric 4222*0b57cec5SDimitry Andric // The number of digits to the left of the decimal point. 4223*0b57cec5SDimitry Andric int NWholeDigits = exp + (int) NDigits; 4224*0b57cec5SDimitry Andric 4225*0b57cec5SDimitry Andric unsigned I = 0; 4226*0b57cec5SDimitry Andric if (NWholeDigits > 0) { 4227*0b57cec5SDimitry Andric for (; I != (unsigned) NWholeDigits; ++I) 4228*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-I-1]); 4229*0b57cec5SDimitry Andric Str.push_back('.'); 4230*0b57cec5SDimitry Andric } else { 4231*0b57cec5SDimitry Andric unsigned NZeros = 1 + (unsigned) -NWholeDigits; 4232*0b57cec5SDimitry Andric 4233*0b57cec5SDimitry Andric Str.push_back('0'); 4234*0b57cec5SDimitry Andric Str.push_back('.'); 4235*0b57cec5SDimitry Andric for (unsigned Z = 1; Z != NZeros; ++Z) 4236*0b57cec5SDimitry Andric Str.push_back('0'); 4237*0b57cec5SDimitry Andric } 4238*0b57cec5SDimitry Andric 4239*0b57cec5SDimitry Andric for (; I != NDigits; ++I) 4240*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-I-1]); 4241*0b57cec5SDimitry Andric } 4242*0b57cec5SDimitry Andric 4243*0b57cec5SDimitry Andric bool IEEEFloat::getExactInverse(APFloat *inv) const { 4244*0b57cec5SDimitry Andric // Special floats and denormals have no exact inverse. 4245*0b57cec5SDimitry Andric if (!isFiniteNonZero()) 4246*0b57cec5SDimitry Andric return false; 4247*0b57cec5SDimitry Andric 4248*0b57cec5SDimitry Andric // Check that the number is a power of two by making sure that only the 4249*0b57cec5SDimitry Andric // integer bit is set in the significand. 4250*0b57cec5SDimitry Andric if (significandLSB() != semantics->precision - 1) 4251*0b57cec5SDimitry Andric return false; 4252*0b57cec5SDimitry Andric 4253*0b57cec5SDimitry Andric // Get the inverse. 4254*0b57cec5SDimitry Andric IEEEFloat reciprocal(*semantics, 1ULL); 4255*0b57cec5SDimitry Andric if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK) 4256*0b57cec5SDimitry Andric return false; 4257*0b57cec5SDimitry Andric 4258*0b57cec5SDimitry Andric // Avoid multiplication with a denormal, it is not safe on all platforms and 4259*0b57cec5SDimitry Andric // may be slower than a normal division. 4260*0b57cec5SDimitry Andric if (reciprocal.isDenormal()) 4261*0b57cec5SDimitry Andric return false; 4262*0b57cec5SDimitry Andric 4263*0b57cec5SDimitry Andric assert(reciprocal.isFiniteNonZero() && 4264*0b57cec5SDimitry Andric reciprocal.significandLSB() == reciprocal.semantics->precision - 1); 4265*0b57cec5SDimitry Andric 4266*0b57cec5SDimitry Andric if (inv) 4267*0b57cec5SDimitry Andric *inv = APFloat(reciprocal, *semantics); 4268*0b57cec5SDimitry Andric 4269*0b57cec5SDimitry Andric return true; 4270*0b57cec5SDimitry Andric } 4271*0b57cec5SDimitry Andric 4272*0b57cec5SDimitry Andric bool IEEEFloat::isSignaling() const { 4273*0b57cec5SDimitry Andric if (!isNaN()) 4274*0b57cec5SDimitry Andric return false; 4275bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) 4276bdd1243dSDimitry Andric return false; 4277*0b57cec5SDimitry Andric 4278*0b57cec5SDimitry Andric // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the 4279*0b57cec5SDimitry Andric // first bit of the trailing significand being 0. 4280*0b57cec5SDimitry Andric return !APInt::tcExtractBit(significandParts(), semantics->precision - 2); 4281*0b57cec5SDimitry Andric } 4282*0b57cec5SDimitry Andric 4283*0b57cec5SDimitry Andric /// IEEE-754R 2008 5.3.1: nextUp/nextDown. 4284*0b57cec5SDimitry Andric /// 4285*0b57cec5SDimitry Andric /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with 4286*0b57cec5SDimitry Andric /// appropriate sign switching before/after the computation. 4287*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::next(bool nextDown) { 4288*0b57cec5SDimitry Andric // If we are performing nextDown, swap sign so we have -x. 4289*0b57cec5SDimitry Andric if (nextDown) 4290*0b57cec5SDimitry Andric changeSign(); 4291*0b57cec5SDimitry Andric 4292*0b57cec5SDimitry Andric // Compute nextUp(x) 4293*0b57cec5SDimitry Andric opStatus result = opOK; 4294*0b57cec5SDimitry Andric 4295*0b57cec5SDimitry Andric // Handle each float category separately. 4296*0b57cec5SDimitry Andric switch (category) { 4297*0b57cec5SDimitry Andric case fcInfinity: 4298*0b57cec5SDimitry Andric // nextUp(+inf) = +inf 4299*0b57cec5SDimitry Andric if (!isNegative()) 4300*0b57cec5SDimitry Andric break; 4301*0b57cec5SDimitry Andric // nextUp(-inf) = -getLargest() 4302*0b57cec5SDimitry Andric makeLargest(true); 4303*0b57cec5SDimitry Andric break; 4304*0b57cec5SDimitry Andric case fcNaN: 4305*0b57cec5SDimitry Andric // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag. 4306*0b57cec5SDimitry Andric // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not 4307*0b57cec5SDimitry Andric // change the payload. 4308*0b57cec5SDimitry Andric if (isSignaling()) { 4309*0b57cec5SDimitry Andric result = opInvalidOp; 4310*0b57cec5SDimitry Andric // For consistency, propagate the sign of the sNaN to the qNaN. 4311*0b57cec5SDimitry Andric makeNaN(false, isNegative(), nullptr); 4312*0b57cec5SDimitry Andric } 4313*0b57cec5SDimitry Andric break; 4314*0b57cec5SDimitry Andric case fcZero: 4315*0b57cec5SDimitry Andric // nextUp(pm 0) = +getSmallest() 4316*0b57cec5SDimitry Andric makeSmallest(false); 4317*0b57cec5SDimitry Andric break; 4318*0b57cec5SDimitry Andric case fcNormal: 4319*0b57cec5SDimitry Andric // nextUp(-getSmallest()) = -0 4320*0b57cec5SDimitry Andric if (isSmallest() && isNegative()) { 4321*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 4322*0b57cec5SDimitry Andric category = fcZero; 4323*0b57cec5SDimitry Andric exponent = 0; 4324*0b57cec5SDimitry Andric break; 4325*0b57cec5SDimitry Andric } 4326*0b57cec5SDimitry Andric 4327*0b57cec5SDimitry Andric if (isLargest() && !isNegative()) { 4328bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { 4329bdd1243dSDimitry Andric // nextUp(getLargest()) == NAN 4330bdd1243dSDimitry Andric makeNaN(); 4331bdd1243dSDimitry Andric break; 4332bdd1243dSDimitry Andric } else { 4333bdd1243dSDimitry Andric // nextUp(getLargest()) == INFINITY 4334*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 4335*0b57cec5SDimitry Andric category = fcInfinity; 4336*0b57cec5SDimitry Andric exponent = semantics->maxExponent + 1; 4337*0b57cec5SDimitry Andric break; 4338*0b57cec5SDimitry Andric } 4339bdd1243dSDimitry Andric } 4340*0b57cec5SDimitry Andric 4341*0b57cec5SDimitry Andric // nextUp(normal) == normal + inc. 4342*0b57cec5SDimitry Andric if (isNegative()) { 4343*0b57cec5SDimitry Andric // If we are negative, we need to decrement the significand. 4344*0b57cec5SDimitry Andric 4345*0b57cec5SDimitry Andric // We only cross a binade boundary that requires adjusting the exponent 4346*0b57cec5SDimitry Andric // if: 4347*0b57cec5SDimitry Andric // 1. exponent != semantics->minExponent. This implies we are not in the 4348*0b57cec5SDimitry Andric // smallest binade or are dealing with denormals. 4349*0b57cec5SDimitry Andric // 2. Our significand excluding the integral bit is all zeros. 4350*0b57cec5SDimitry Andric bool WillCrossBinadeBoundary = 4351*0b57cec5SDimitry Andric exponent != semantics->minExponent && isSignificandAllZeros(); 4352*0b57cec5SDimitry Andric 4353*0b57cec5SDimitry Andric // Decrement the significand. 4354*0b57cec5SDimitry Andric // 4355*0b57cec5SDimitry Andric // We always do this since: 4356*0b57cec5SDimitry Andric // 1. If we are dealing with a non-binade decrement, by definition we 4357*0b57cec5SDimitry Andric // just decrement the significand. 4358*0b57cec5SDimitry Andric // 2. If we are dealing with a normal -> normal binade decrement, since 4359*0b57cec5SDimitry Andric // we have an explicit integral bit the fact that all bits but the 4360*0b57cec5SDimitry Andric // integral bit are zero implies that subtracting one will yield a 4361*0b57cec5SDimitry Andric // significand with 0 integral bit and 1 in all other spots. Thus we 4362*0b57cec5SDimitry Andric // must just adjust the exponent and set the integral bit to 1. 4363*0b57cec5SDimitry Andric // 3. If we are dealing with a normal -> denormal binade decrement, 4364*0b57cec5SDimitry Andric // since we set the integral bit to 0 when we represent denormals, we 4365*0b57cec5SDimitry Andric // just decrement the significand. 4366*0b57cec5SDimitry Andric integerPart *Parts = significandParts(); 4367*0b57cec5SDimitry Andric APInt::tcDecrement(Parts, partCount()); 4368*0b57cec5SDimitry Andric 4369*0b57cec5SDimitry Andric if (WillCrossBinadeBoundary) { 4370*0b57cec5SDimitry Andric // Our result is a normal number. Do the following: 4371*0b57cec5SDimitry Andric // 1. Set the integral bit to 1. 4372*0b57cec5SDimitry Andric // 2. Decrement the exponent. 4373*0b57cec5SDimitry Andric APInt::tcSetBit(Parts, semantics->precision - 1); 4374*0b57cec5SDimitry Andric exponent--; 4375*0b57cec5SDimitry Andric } 4376*0b57cec5SDimitry Andric } else { 4377*0b57cec5SDimitry Andric // If we are positive, we need to increment the significand. 4378*0b57cec5SDimitry Andric 4379*0b57cec5SDimitry Andric // We only cross a binade boundary that requires adjusting the exponent if 4380*0b57cec5SDimitry Andric // the input is not a denormal and all of said input's significand bits 4381*0b57cec5SDimitry Andric // are set. If all of said conditions are true: clear the significand, set 4382*0b57cec5SDimitry Andric // the integral bit to 1, and increment the exponent. If we have a 4383*0b57cec5SDimitry Andric // denormal always increment since moving denormals and the numbers in the 4384*0b57cec5SDimitry Andric // smallest normal binade have the same exponent in our representation. 4385*0b57cec5SDimitry Andric bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes(); 4386*0b57cec5SDimitry Andric 4387*0b57cec5SDimitry Andric if (WillCrossBinadeBoundary) { 4388*0b57cec5SDimitry Andric integerPart *Parts = significandParts(); 4389*0b57cec5SDimitry Andric APInt::tcSet(Parts, 0, partCount()); 4390*0b57cec5SDimitry Andric APInt::tcSetBit(Parts, semantics->precision - 1); 4391*0b57cec5SDimitry Andric assert(exponent != semantics->maxExponent && 4392*0b57cec5SDimitry Andric "We can not increment an exponent beyond the maxExponent allowed" 4393*0b57cec5SDimitry Andric " by the given floating point semantics."); 4394*0b57cec5SDimitry Andric exponent++; 4395*0b57cec5SDimitry Andric } else { 4396*0b57cec5SDimitry Andric incrementSignificand(); 4397*0b57cec5SDimitry Andric } 4398*0b57cec5SDimitry Andric } 4399*0b57cec5SDimitry Andric break; 4400*0b57cec5SDimitry Andric } 4401*0b57cec5SDimitry Andric 4402*0b57cec5SDimitry Andric // If we are performing nextDown, swap sign so we have -nextUp(-x) 4403*0b57cec5SDimitry Andric if (nextDown) 4404*0b57cec5SDimitry Andric changeSign(); 4405*0b57cec5SDimitry Andric 4406*0b57cec5SDimitry Andric return result; 4407*0b57cec5SDimitry Andric } 4408*0b57cec5SDimitry Andric 4409e8d8bef9SDimitry Andric APFloatBase::ExponentType IEEEFloat::exponentNaN() const { 4410bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) 4411bdd1243dSDimitry Andric return semantics->maxExponent; 4412e8d8bef9SDimitry Andric return semantics->maxExponent + 1; 4413e8d8bef9SDimitry Andric } 4414e8d8bef9SDimitry Andric 4415e8d8bef9SDimitry Andric APFloatBase::ExponentType IEEEFloat::exponentInf() const { 4416e8d8bef9SDimitry Andric return semantics->maxExponent + 1; 4417e8d8bef9SDimitry Andric } 4418e8d8bef9SDimitry Andric 4419e8d8bef9SDimitry Andric APFloatBase::ExponentType IEEEFloat::exponentZero() const { 4420e8d8bef9SDimitry Andric return semantics->minExponent - 1; 4421e8d8bef9SDimitry Andric } 4422e8d8bef9SDimitry Andric 4423*0b57cec5SDimitry Andric void IEEEFloat::makeInf(bool Negative) { 4424bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { 4425bdd1243dSDimitry Andric // There is no Inf, so make NaN instead. 4426bdd1243dSDimitry Andric makeNaN(false, Negative); 4427bdd1243dSDimitry Andric return; 4428bdd1243dSDimitry Andric } 4429*0b57cec5SDimitry Andric category = fcInfinity; 4430*0b57cec5SDimitry Andric sign = Negative; 4431e8d8bef9SDimitry Andric exponent = exponentInf(); 4432*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 4433*0b57cec5SDimitry Andric } 4434*0b57cec5SDimitry Andric 4435*0b57cec5SDimitry Andric void IEEEFloat::makeZero(bool Negative) { 4436*0b57cec5SDimitry Andric category = fcZero; 4437*0b57cec5SDimitry Andric sign = Negative; 4438e8d8bef9SDimitry Andric exponent = exponentZero(); 4439*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 4440*0b57cec5SDimitry Andric } 4441*0b57cec5SDimitry Andric 4442*0b57cec5SDimitry Andric void IEEEFloat::makeQuiet() { 4443*0b57cec5SDimitry Andric assert(isNaN()); 4444bdd1243dSDimitry Andric if (semantics->nonFiniteBehavior != fltNonfiniteBehavior::NanOnly) 4445*0b57cec5SDimitry Andric APInt::tcSetBit(significandParts(), semantics->precision - 2); 4446*0b57cec5SDimitry Andric } 4447*0b57cec5SDimitry Andric 4448*0b57cec5SDimitry Andric int ilogb(const IEEEFloat &Arg) { 4449*0b57cec5SDimitry Andric if (Arg.isNaN()) 4450*0b57cec5SDimitry Andric return IEEEFloat::IEK_NaN; 4451*0b57cec5SDimitry Andric if (Arg.isZero()) 4452*0b57cec5SDimitry Andric return IEEEFloat::IEK_Zero; 4453*0b57cec5SDimitry Andric if (Arg.isInfinity()) 4454*0b57cec5SDimitry Andric return IEEEFloat::IEK_Inf; 4455*0b57cec5SDimitry Andric if (!Arg.isDenormal()) 4456*0b57cec5SDimitry Andric return Arg.exponent; 4457*0b57cec5SDimitry Andric 4458*0b57cec5SDimitry Andric IEEEFloat Normalized(Arg); 4459*0b57cec5SDimitry Andric int SignificandBits = Arg.getSemantics().precision - 1; 4460*0b57cec5SDimitry Andric 4461*0b57cec5SDimitry Andric Normalized.exponent += SignificandBits; 4462*0b57cec5SDimitry Andric Normalized.normalize(IEEEFloat::rmNearestTiesToEven, lfExactlyZero); 4463*0b57cec5SDimitry Andric return Normalized.exponent - SignificandBits; 4464*0b57cec5SDimitry Andric } 4465*0b57cec5SDimitry Andric 4466*0b57cec5SDimitry Andric IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode RoundingMode) { 4467*0b57cec5SDimitry Andric auto MaxExp = X.getSemantics().maxExponent; 4468*0b57cec5SDimitry Andric auto MinExp = X.getSemantics().minExponent; 4469*0b57cec5SDimitry Andric 4470*0b57cec5SDimitry Andric // If Exp is wildly out-of-scale, simply adding it to X.exponent will 4471*0b57cec5SDimitry Andric // overflow; clamp it to a safe range before adding, but ensure that the range 4472*0b57cec5SDimitry Andric // is large enough that the clamp does not change the result. The range we 4473*0b57cec5SDimitry Andric // need to support is the difference between the largest possible exponent and 4474*0b57cec5SDimitry Andric // the normalized exponent of half the smallest denormal. 4475*0b57cec5SDimitry Andric 4476*0b57cec5SDimitry Andric int SignificandBits = X.getSemantics().precision - 1; 4477*0b57cec5SDimitry Andric int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1; 4478*0b57cec5SDimitry Andric 4479*0b57cec5SDimitry Andric // Clamp to one past the range ends to let normalize handle overlflow. 4480*0b57cec5SDimitry Andric X.exponent += std::min(std::max(Exp, -MaxIncrement - 1), MaxIncrement); 4481*0b57cec5SDimitry Andric X.normalize(RoundingMode, lfExactlyZero); 4482*0b57cec5SDimitry Andric if (X.isNaN()) 4483*0b57cec5SDimitry Andric X.makeQuiet(); 4484*0b57cec5SDimitry Andric return X; 4485*0b57cec5SDimitry Andric } 4486*0b57cec5SDimitry Andric 4487*0b57cec5SDimitry Andric IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM) { 4488*0b57cec5SDimitry Andric Exp = ilogb(Val); 4489*0b57cec5SDimitry Andric 4490*0b57cec5SDimitry Andric // Quiet signalling nans. 4491*0b57cec5SDimitry Andric if (Exp == IEEEFloat::IEK_NaN) { 4492*0b57cec5SDimitry Andric IEEEFloat Quiet(Val); 4493*0b57cec5SDimitry Andric Quiet.makeQuiet(); 4494*0b57cec5SDimitry Andric return Quiet; 4495*0b57cec5SDimitry Andric } 4496*0b57cec5SDimitry Andric 4497*0b57cec5SDimitry Andric if (Exp == IEEEFloat::IEK_Inf) 4498*0b57cec5SDimitry Andric return Val; 4499*0b57cec5SDimitry Andric 4500*0b57cec5SDimitry Andric // 1 is added because frexp is defined to return a normalized fraction in 4501*0b57cec5SDimitry Andric // +/-[0.5, 1.0), rather than the usual +/-[1.0, 2.0). 4502*0b57cec5SDimitry Andric Exp = Exp == IEEEFloat::IEK_Zero ? 0 : Exp + 1; 4503*0b57cec5SDimitry Andric return scalbn(Val, -Exp, RM); 4504*0b57cec5SDimitry Andric } 4505*0b57cec5SDimitry Andric 4506*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S) 4507*0b57cec5SDimitry Andric : Semantics(&S), 4508*0b57cec5SDimitry Andric Floats(new APFloat[2]{APFloat(semIEEEdouble), APFloat(semIEEEdouble)}) { 4509*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 4510*0b57cec5SDimitry Andric } 4511*0b57cec5SDimitry Andric 4512*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, uninitializedTag) 4513*0b57cec5SDimitry Andric : Semantics(&S), 4514*0b57cec5SDimitry Andric Floats(new APFloat[2]{APFloat(semIEEEdouble, uninitialized), 4515*0b57cec5SDimitry Andric APFloat(semIEEEdouble, uninitialized)}) { 4516*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 4517*0b57cec5SDimitry Andric } 4518*0b57cec5SDimitry Andric 4519*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, integerPart I) 4520*0b57cec5SDimitry Andric : Semantics(&S), Floats(new APFloat[2]{APFloat(semIEEEdouble, I), 4521*0b57cec5SDimitry Andric APFloat(semIEEEdouble)}) { 4522*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 4523*0b57cec5SDimitry Andric } 4524*0b57cec5SDimitry Andric 4525*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, const APInt &I) 4526*0b57cec5SDimitry Andric : Semantics(&S), 4527*0b57cec5SDimitry Andric Floats(new APFloat[2]{ 4528*0b57cec5SDimitry Andric APFloat(semIEEEdouble, APInt(64, I.getRawData()[0])), 4529*0b57cec5SDimitry Andric APFloat(semIEEEdouble, APInt(64, I.getRawData()[1]))}) { 4530*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 4531*0b57cec5SDimitry Andric } 4532*0b57cec5SDimitry Andric 4533*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, APFloat &&First, 4534*0b57cec5SDimitry Andric APFloat &&Second) 4535*0b57cec5SDimitry Andric : Semantics(&S), 4536*0b57cec5SDimitry Andric Floats(new APFloat[2]{std::move(First), std::move(Second)}) { 4537*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 4538*0b57cec5SDimitry Andric assert(&Floats[0].getSemantics() == &semIEEEdouble); 4539*0b57cec5SDimitry Andric assert(&Floats[1].getSemantics() == &semIEEEdouble); 4540*0b57cec5SDimitry Andric } 4541*0b57cec5SDimitry Andric 4542*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const DoubleAPFloat &RHS) 4543*0b57cec5SDimitry Andric : Semantics(RHS.Semantics), 4544*0b57cec5SDimitry Andric Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]), 4545*0b57cec5SDimitry Andric APFloat(RHS.Floats[1])} 4546*0b57cec5SDimitry Andric : nullptr) { 4547*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 4548*0b57cec5SDimitry Andric } 4549*0b57cec5SDimitry Andric 4550*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(DoubleAPFloat &&RHS) 4551*0b57cec5SDimitry Andric : Semantics(RHS.Semantics), Floats(std::move(RHS.Floats)) { 4552*0b57cec5SDimitry Andric RHS.Semantics = &semBogus; 4553*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 4554*0b57cec5SDimitry Andric } 4555*0b57cec5SDimitry Andric 4556*0b57cec5SDimitry Andric DoubleAPFloat &DoubleAPFloat::operator=(const DoubleAPFloat &RHS) { 4557*0b57cec5SDimitry Andric if (Semantics == RHS.Semantics && RHS.Floats) { 4558*0b57cec5SDimitry Andric Floats[0] = RHS.Floats[0]; 4559*0b57cec5SDimitry Andric Floats[1] = RHS.Floats[1]; 4560*0b57cec5SDimitry Andric } else if (this != &RHS) { 4561*0b57cec5SDimitry Andric this->~DoubleAPFloat(); 4562*0b57cec5SDimitry Andric new (this) DoubleAPFloat(RHS); 4563*0b57cec5SDimitry Andric } 4564*0b57cec5SDimitry Andric return *this; 4565*0b57cec5SDimitry Andric } 4566*0b57cec5SDimitry Andric 4567*0b57cec5SDimitry Andric // Implement addition, subtraction, multiplication and division based on: 4568*0b57cec5SDimitry Andric // "Software for Doubled-Precision Floating-Point Computations", 4569*0b57cec5SDimitry Andric // by Seppo Linnainmaa, ACM TOMS vol 7 no 3, September 1981, pages 272-283. 4570*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::addImpl(const APFloat &a, const APFloat &aa, 4571*0b57cec5SDimitry Andric const APFloat &c, const APFloat &cc, 4572*0b57cec5SDimitry Andric roundingMode RM) { 4573*0b57cec5SDimitry Andric int Status = opOK; 4574*0b57cec5SDimitry Andric APFloat z = a; 4575*0b57cec5SDimitry Andric Status |= z.add(c, RM); 4576*0b57cec5SDimitry Andric if (!z.isFinite()) { 4577*0b57cec5SDimitry Andric if (!z.isInfinity()) { 4578*0b57cec5SDimitry Andric Floats[0] = std::move(z); 4579*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4580*0b57cec5SDimitry Andric return (opStatus)Status; 4581*0b57cec5SDimitry Andric } 4582*0b57cec5SDimitry Andric Status = opOK; 4583*0b57cec5SDimitry Andric auto AComparedToC = a.compareAbsoluteValue(c); 4584*0b57cec5SDimitry Andric z = cc; 4585*0b57cec5SDimitry Andric Status |= z.add(aa, RM); 4586*0b57cec5SDimitry Andric if (AComparedToC == APFloat::cmpGreaterThan) { 4587*0b57cec5SDimitry Andric // z = cc + aa + c + a; 4588*0b57cec5SDimitry Andric Status |= z.add(c, RM); 4589*0b57cec5SDimitry Andric Status |= z.add(a, RM); 4590*0b57cec5SDimitry Andric } else { 4591*0b57cec5SDimitry Andric // z = cc + aa + a + c; 4592*0b57cec5SDimitry Andric Status |= z.add(a, RM); 4593*0b57cec5SDimitry Andric Status |= z.add(c, RM); 4594*0b57cec5SDimitry Andric } 4595*0b57cec5SDimitry Andric if (!z.isFinite()) { 4596*0b57cec5SDimitry Andric Floats[0] = std::move(z); 4597*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4598*0b57cec5SDimitry Andric return (opStatus)Status; 4599*0b57cec5SDimitry Andric } 4600*0b57cec5SDimitry Andric Floats[0] = z; 4601*0b57cec5SDimitry Andric APFloat zz = aa; 4602*0b57cec5SDimitry Andric Status |= zz.add(cc, RM); 4603*0b57cec5SDimitry Andric if (AComparedToC == APFloat::cmpGreaterThan) { 4604*0b57cec5SDimitry Andric // Floats[1] = a - z + c + zz; 4605*0b57cec5SDimitry Andric Floats[1] = a; 4606*0b57cec5SDimitry Andric Status |= Floats[1].subtract(z, RM); 4607*0b57cec5SDimitry Andric Status |= Floats[1].add(c, RM); 4608*0b57cec5SDimitry Andric Status |= Floats[1].add(zz, RM); 4609*0b57cec5SDimitry Andric } else { 4610*0b57cec5SDimitry Andric // Floats[1] = c - z + a + zz; 4611*0b57cec5SDimitry Andric Floats[1] = c; 4612*0b57cec5SDimitry Andric Status |= Floats[1].subtract(z, RM); 4613*0b57cec5SDimitry Andric Status |= Floats[1].add(a, RM); 4614*0b57cec5SDimitry Andric Status |= Floats[1].add(zz, RM); 4615*0b57cec5SDimitry Andric } 4616*0b57cec5SDimitry Andric } else { 4617*0b57cec5SDimitry Andric // q = a - z; 4618*0b57cec5SDimitry Andric APFloat q = a; 4619*0b57cec5SDimitry Andric Status |= q.subtract(z, RM); 4620*0b57cec5SDimitry Andric 4621*0b57cec5SDimitry Andric // zz = q + c + (a - (q + z)) + aa + cc; 4622*0b57cec5SDimitry Andric // Compute a - (q + z) as -((q + z) - a) to avoid temporary copies. 4623*0b57cec5SDimitry Andric auto zz = q; 4624*0b57cec5SDimitry Andric Status |= zz.add(c, RM); 4625*0b57cec5SDimitry Andric Status |= q.add(z, RM); 4626*0b57cec5SDimitry Andric Status |= q.subtract(a, RM); 4627*0b57cec5SDimitry Andric q.changeSign(); 4628*0b57cec5SDimitry Andric Status |= zz.add(q, RM); 4629*0b57cec5SDimitry Andric Status |= zz.add(aa, RM); 4630*0b57cec5SDimitry Andric Status |= zz.add(cc, RM); 4631*0b57cec5SDimitry Andric if (zz.isZero() && !zz.isNegative()) { 4632*0b57cec5SDimitry Andric Floats[0] = std::move(z); 4633*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4634*0b57cec5SDimitry Andric return opOK; 4635*0b57cec5SDimitry Andric } 4636*0b57cec5SDimitry Andric Floats[0] = z; 4637*0b57cec5SDimitry Andric Status |= Floats[0].add(zz, RM); 4638*0b57cec5SDimitry Andric if (!Floats[0].isFinite()) { 4639*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4640*0b57cec5SDimitry Andric return (opStatus)Status; 4641*0b57cec5SDimitry Andric } 4642*0b57cec5SDimitry Andric Floats[1] = std::move(z); 4643*0b57cec5SDimitry Andric Status |= Floats[1].subtract(Floats[0], RM); 4644*0b57cec5SDimitry Andric Status |= Floats[1].add(zz, RM); 4645*0b57cec5SDimitry Andric } 4646*0b57cec5SDimitry Andric return (opStatus)Status; 4647*0b57cec5SDimitry Andric } 4648*0b57cec5SDimitry Andric 4649*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::addWithSpecial(const DoubleAPFloat &LHS, 4650*0b57cec5SDimitry Andric const DoubleAPFloat &RHS, 4651*0b57cec5SDimitry Andric DoubleAPFloat &Out, 4652*0b57cec5SDimitry Andric roundingMode RM) { 4653*0b57cec5SDimitry Andric if (LHS.getCategory() == fcNaN) { 4654*0b57cec5SDimitry Andric Out = LHS; 4655*0b57cec5SDimitry Andric return opOK; 4656*0b57cec5SDimitry Andric } 4657*0b57cec5SDimitry Andric if (RHS.getCategory() == fcNaN) { 4658*0b57cec5SDimitry Andric Out = RHS; 4659*0b57cec5SDimitry Andric return opOK; 4660*0b57cec5SDimitry Andric } 4661*0b57cec5SDimitry Andric if (LHS.getCategory() == fcZero) { 4662*0b57cec5SDimitry Andric Out = RHS; 4663*0b57cec5SDimitry Andric return opOK; 4664*0b57cec5SDimitry Andric } 4665*0b57cec5SDimitry Andric if (RHS.getCategory() == fcZero) { 4666*0b57cec5SDimitry Andric Out = LHS; 4667*0b57cec5SDimitry Andric return opOK; 4668*0b57cec5SDimitry Andric } 4669*0b57cec5SDimitry Andric if (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcInfinity && 4670*0b57cec5SDimitry Andric LHS.isNegative() != RHS.isNegative()) { 4671*0b57cec5SDimitry Andric Out.makeNaN(false, Out.isNegative(), nullptr); 4672*0b57cec5SDimitry Andric return opInvalidOp; 4673*0b57cec5SDimitry Andric } 4674*0b57cec5SDimitry Andric if (LHS.getCategory() == fcInfinity) { 4675*0b57cec5SDimitry Andric Out = LHS; 4676*0b57cec5SDimitry Andric return opOK; 4677*0b57cec5SDimitry Andric } 4678*0b57cec5SDimitry Andric if (RHS.getCategory() == fcInfinity) { 4679*0b57cec5SDimitry Andric Out = RHS; 4680*0b57cec5SDimitry Andric return opOK; 4681*0b57cec5SDimitry Andric } 4682*0b57cec5SDimitry Andric assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal); 4683*0b57cec5SDimitry Andric 4684*0b57cec5SDimitry Andric APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]), C(RHS.Floats[0]), 4685*0b57cec5SDimitry Andric CC(RHS.Floats[1]); 4686*0b57cec5SDimitry Andric assert(&A.getSemantics() == &semIEEEdouble); 4687*0b57cec5SDimitry Andric assert(&AA.getSemantics() == &semIEEEdouble); 4688*0b57cec5SDimitry Andric assert(&C.getSemantics() == &semIEEEdouble); 4689*0b57cec5SDimitry Andric assert(&CC.getSemantics() == &semIEEEdouble); 4690*0b57cec5SDimitry Andric assert(&Out.Floats[0].getSemantics() == &semIEEEdouble); 4691*0b57cec5SDimitry Andric assert(&Out.Floats[1].getSemantics() == &semIEEEdouble); 4692*0b57cec5SDimitry Andric return Out.addImpl(A, AA, C, CC, RM); 4693*0b57cec5SDimitry Andric } 4694*0b57cec5SDimitry Andric 4695*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::add(const DoubleAPFloat &RHS, 4696*0b57cec5SDimitry Andric roundingMode RM) { 4697*0b57cec5SDimitry Andric return addWithSpecial(*this, RHS, *this, RM); 4698*0b57cec5SDimitry Andric } 4699*0b57cec5SDimitry Andric 4700*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::subtract(const DoubleAPFloat &RHS, 4701*0b57cec5SDimitry Andric roundingMode RM) { 4702*0b57cec5SDimitry Andric changeSign(); 4703*0b57cec5SDimitry Andric auto Ret = add(RHS, RM); 4704*0b57cec5SDimitry Andric changeSign(); 4705*0b57cec5SDimitry Andric return Ret; 4706*0b57cec5SDimitry Andric } 4707*0b57cec5SDimitry Andric 4708*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::multiply(const DoubleAPFloat &RHS, 4709*0b57cec5SDimitry Andric APFloat::roundingMode RM) { 4710*0b57cec5SDimitry Andric const auto &LHS = *this; 4711*0b57cec5SDimitry Andric auto &Out = *this; 4712*0b57cec5SDimitry Andric /* Interesting observation: For special categories, finding the lowest 4713*0b57cec5SDimitry Andric common ancestor of the following layered graph gives the correct 4714*0b57cec5SDimitry Andric return category: 4715*0b57cec5SDimitry Andric 4716*0b57cec5SDimitry Andric NaN 4717*0b57cec5SDimitry Andric / \ 4718*0b57cec5SDimitry Andric Zero Inf 4719*0b57cec5SDimitry Andric \ / 4720*0b57cec5SDimitry Andric Normal 4721*0b57cec5SDimitry Andric 4722*0b57cec5SDimitry Andric e.g. NaN * NaN = NaN 4723*0b57cec5SDimitry Andric Zero * Inf = NaN 4724*0b57cec5SDimitry Andric Normal * Zero = Zero 4725*0b57cec5SDimitry Andric Normal * Inf = Inf 4726*0b57cec5SDimitry Andric */ 4727*0b57cec5SDimitry Andric if (LHS.getCategory() == fcNaN) { 4728*0b57cec5SDimitry Andric Out = LHS; 4729*0b57cec5SDimitry Andric return opOK; 4730*0b57cec5SDimitry Andric } 4731*0b57cec5SDimitry Andric if (RHS.getCategory() == fcNaN) { 4732*0b57cec5SDimitry Andric Out = RHS; 4733*0b57cec5SDimitry Andric return opOK; 4734*0b57cec5SDimitry Andric } 4735*0b57cec5SDimitry Andric if ((LHS.getCategory() == fcZero && RHS.getCategory() == fcInfinity) || 4736*0b57cec5SDimitry Andric (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcZero)) { 4737*0b57cec5SDimitry Andric Out.makeNaN(false, false, nullptr); 4738*0b57cec5SDimitry Andric return opOK; 4739*0b57cec5SDimitry Andric } 4740*0b57cec5SDimitry Andric if (LHS.getCategory() == fcZero || LHS.getCategory() == fcInfinity) { 4741*0b57cec5SDimitry Andric Out = LHS; 4742*0b57cec5SDimitry Andric return opOK; 4743*0b57cec5SDimitry Andric } 4744*0b57cec5SDimitry Andric if (RHS.getCategory() == fcZero || RHS.getCategory() == fcInfinity) { 4745*0b57cec5SDimitry Andric Out = RHS; 4746*0b57cec5SDimitry Andric return opOK; 4747*0b57cec5SDimitry Andric } 4748*0b57cec5SDimitry Andric assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal && 4749*0b57cec5SDimitry Andric "Special cases not handled exhaustively"); 4750*0b57cec5SDimitry Andric 4751*0b57cec5SDimitry Andric int Status = opOK; 4752*0b57cec5SDimitry Andric APFloat A = Floats[0], B = Floats[1], C = RHS.Floats[0], D = RHS.Floats[1]; 4753*0b57cec5SDimitry Andric // t = a * c 4754*0b57cec5SDimitry Andric APFloat T = A; 4755*0b57cec5SDimitry Andric Status |= T.multiply(C, RM); 4756*0b57cec5SDimitry Andric if (!T.isFiniteNonZero()) { 4757*0b57cec5SDimitry Andric Floats[0] = T; 4758*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4759*0b57cec5SDimitry Andric return (opStatus)Status; 4760*0b57cec5SDimitry Andric } 4761*0b57cec5SDimitry Andric 4762*0b57cec5SDimitry Andric // tau = fmsub(a, c, t), that is -fmadd(-a, c, t). 4763*0b57cec5SDimitry Andric APFloat Tau = A; 4764*0b57cec5SDimitry Andric T.changeSign(); 4765*0b57cec5SDimitry Andric Status |= Tau.fusedMultiplyAdd(C, T, RM); 4766*0b57cec5SDimitry Andric T.changeSign(); 4767*0b57cec5SDimitry Andric { 4768*0b57cec5SDimitry Andric // v = a * d 4769*0b57cec5SDimitry Andric APFloat V = A; 4770*0b57cec5SDimitry Andric Status |= V.multiply(D, RM); 4771*0b57cec5SDimitry Andric // w = b * c 4772*0b57cec5SDimitry Andric APFloat W = B; 4773*0b57cec5SDimitry Andric Status |= W.multiply(C, RM); 4774*0b57cec5SDimitry Andric Status |= V.add(W, RM); 4775*0b57cec5SDimitry Andric // tau += v + w 4776*0b57cec5SDimitry Andric Status |= Tau.add(V, RM); 4777*0b57cec5SDimitry Andric } 4778*0b57cec5SDimitry Andric // u = t + tau 4779*0b57cec5SDimitry Andric APFloat U = T; 4780*0b57cec5SDimitry Andric Status |= U.add(Tau, RM); 4781*0b57cec5SDimitry Andric 4782*0b57cec5SDimitry Andric Floats[0] = U; 4783*0b57cec5SDimitry Andric if (!U.isFinite()) { 4784*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4785*0b57cec5SDimitry Andric } else { 4786*0b57cec5SDimitry Andric // Floats[1] = (t - u) + tau 4787*0b57cec5SDimitry Andric Status |= T.subtract(U, RM); 4788*0b57cec5SDimitry Andric Status |= T.add(Tau, RM); 4789*0b57cec5SDimitry Andric Floats[1] = T; 4790*0b57cec5SDimitry Andric } 4791*0b57cec5SDimitry Andric return (opStatus)Status; 4792*0b57cec5SDimitry Andric } 4793*0b57cec5SDimitry Andric 4794*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::divide(const DoubleAPFloat &RHS, 4795*0b57cec5SDimitry Andric APFloat::roundingMode RM) { 4796*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4797*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4798*0b57cec5SDimitry Andric auto Ret = 4799*0b57cec5SDimitry Andric Tmp.divide(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()), RM); 4800*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4801*0b57cec5SDimitry Andric return Ret; 4802*0b57cec5SDimitry Andric } 4803*0b57cec5SDimitry Andric 4804*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::remainder(const DoubleAPFloat &RHS) { 4805*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4806*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4807*0b57cec5SDimitry Andric auto Ret = 4808*0b57cec5SDimitry Andric Tmp.remainder(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); 4809*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4810*0b57cec5SDimitry Andric return Ret; 4811*0b57cec5SDimitry Andric } 4812*0b57cec5SDimitry Andric 4813*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::mod(const DoubleAPFloat &RHS) { 4814*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4815*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4816*0b57cec5SDimitry Andric auto Ret = Tmp.mod(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); 4817*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4818*0b57cec5SDimitry Andric return Ret; 4819*0b57cec5SDimitry Andric } 4820*0b57cec5SDimitry Andric 4821*0b57cec5SDimitry Andric APFloat::opStatus 4822*0b57cec5SDimitry Andric DoubleAPFloat::fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, 4823*0b57cec5SDimitry Andric const DoubleAPFloat &Addend, 4824*0b57cec5SDimitry Andric APFloat::roundingMode RM) { 4825*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4826*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4827*0b57cec5SDimitry Andric auto Ret = Tmp.fusedMultiplyAdd( 4828*0b57cec5SDimitry Andric APFloat(semPPCDoubleDoubleLegacy, Multiplicand.bitcastToAPInt()), 4829*0b57cec5SDimitry Andric APFloat(semPPCDoubleDoubleLegacy, Addend.bitcastToAPInt()), RM); 4830*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4831*0b57cec5SDimitry Andric return Ret; 4832*0b57cec5SDimitry Andric } 4833*0b57cec5SDimitry Andric 4834*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::roundToIntegral(APFloat::roundingMode RM) { 4835*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4836*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4837*0b57cec5SDimitry Andric auto Ret = Tmp.roundToIntegral(RM); 4838*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4839*0b57cec5SDimitry Andric return Ret; 4840*0b57cec5SDimitry Andric } 4841*0b57cec5SDimitry Andric 4842*0b57cec5SDimitry Andric void DoubleAPFloat::changeSign() { 4843*0b57cec5SDimitry Andric Floats[0].changeSign(); 4844*0b57cec5SDimitry Andric Floats[1].changeSign(); 4845*0b57cec5SDimitry Andric } 4846*0b57cec5SDimitry Andric 4847*0b57cec5SDimitry Andric APFloat::cmpResult 4848*0b57cec5SDimitry Andric DoubleAPFloat::compareAbsoluteValue(const DoubleAPFloat &RHS) const { 4849*0b57cec5SDimitry Andric auto Result = Floats[0].compareAbsoluteValue(RHS.Floats[0]); 4850*0b57cec5SDimitry Andric if (Result != cmpEqual) 4851*0b57cec5SDimitry Andric return Result; 4852*0b57cec5SDimitry Andric Result = Floats[1].compareAbsoluteValue(RHS.Floats[1]); 4853*0b57cec5SDimitry Andric if (Result == cmpLessThan || Result == cmpGreaterThan) { 4854*0b57cec5SDimitry Andric auto Against = Floats[0].isNegative() ^ Floats[1].isNegative(); 4855*0b57cec5SDimitry Andric auto RHSAgainst = RHS.Floats[0].isNegative() ^ RHS.Floats[1].isNegative(); 4856*0b57cec5SDimitry Andric if (Against && !RHSAgainst) 4857*0b57cec5SDimitry Andric return cmpLessThan; 4858*0b57cec5SDimitry Andric if (!Against && RHSAgainst) 4859*0b57cec5SDimitry Andric return cmpGreaterThan; 4860*0b57cec5SDimitry Andric if (!Against && !RHSAgainst) 4861*0b57cec5SDimitry Andric return Result; 4862*0b57cec5SDimitry Andric if (Against && RHSAgainst) 4863*0b57cec5SDimitry Andric return (cmpResult)(cmpLessThan + cmpGreaterThan - Result); 4864*0b57cec5SDimitry Andric } 4865*0b57cec5SDimitry Andric return Result; 4866*0b57cec5SDimitry Andric } 4867*0b57cec5SDimitry Andric 4868*0b57cec5SDimitry Andric APFloat::fltCategory DoubleAPFloat::getCategory() const { 4869*0b57cec5SDimitry Andric return Floats[0].getCategory(); 4870*0b57cec5SDimitry Andric } 4871*0b57cec5SDimitry Andric 4872*0b57cec5SDimitry Andric bool DoubleAPFloat::isNegative() const { return Floats[0].isNegative(); } 4873*0b57cec5SDimitry Andric 4874*0b57cec5SDimitry Andric void DoubleAPFloat::makeInf(bool Neg) { 4875*0b57cec5SDimitry Andric Floats[0].makeInf(Neg); 4876*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4877*0b57cec5SDimitry Andric } 4878*0b57cec5SDimitry Andric 4879*0b57cec5SDimitry Andric void DoubleAPFloat::makeZero(bool Neg) { 4880*0b57cec5SDimitry Andric Floats[0].makeZero(Neg); 4881*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4882*0b57cec5SDimitry Andric } 4883*0b57cec5SDimitry Andric 4884*0b57cec5SDimitry Andric void DoubleAPFloat::makeLargest(bool Neg) { 4885*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4886*0b57cec5SDimitry Andric Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x7fefffffffffffffull)); 4887*0b57cec5SDimitry Andric Floats[1] = APFloat(semIEEEdouble, APInt(64, 0x7c8ffffffffffffeull)); 4888*0b57cec5SDimitry Andric if (Neg) 4889*0b57cec5SDimitry Andric changeSign(); 4890*0b57cec5SDimitry Andric } 4891*0b57cec5SDimitry Andric 4892*0b57cec5SDimitry Andric void DoubleAPFloat::makeSmallest(bool Neg) { 4893*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4894*0b57cec5SDimitry Andric Floats[0].makeSmallest(Neg); 4895*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4896*0b57cec5SDimitry Andric } 4897*0b57cec5SDimitry Andric 4898*0b57cec5SDimitry Andric void DoubleAPFloat::makeSmallestNormalized(bool Neg) { 4899*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4900*0b57cec5SDimitry Andric Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x0360000000000000ull)); 4901*0b57cec5SDimitry Andric if (Neg) 4902*0b57cec5SDimitry Andric Floats[0].changeSign(); 4903*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4904*0b57cec5SDimitry Andric } 4905*0b57cec5SDimitry Andric 4906*0b57cec5SDimitry Andric void DoubleAPFloat::makeNaN(bool SNaN, bool Neg, const APInt *fill) { 4907*0b57cec5SDimitry Andric Floats[0].makeNaN(SNaN, Neg, fill); 4908*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4909*0b57cec5SDimitry Andric } 4910*0b57cec5SDimitry Andric 4911*0b57cec5SDimitry Andric APFloat::cmpResult DoubleAPFloat::compare(const DoubleAPFloat &RHS) const { 4912*0b57cec5SDimitry Andric auto Result = Floats[0].compare(RHS.Floats[0]); 4913*0b57cec5SDimitry Andric // |Float[0]| > |Float[1]| 4914*0b57cec5SDimitry Andric if (Result == APFloat::cmpEqual) 4915*0b57cec5SDimitry Andric return Floats[1].compare(RHS.Floats[1]); 4916*0b57cec5SDimitry Andric return Result; 4917*0b57cec5SDimitry Andric } 4918*0b57cec5SDimitry Andric 4919*0b57cec5SDimitry Andric bool DoubleAPFloat::bitwiseIsEqual(const DoubleAPFloat &RHS) const { 4920*0b57cec5SDimitry Andric return Floats[0].bitwiseIsEqual(RHS.Floats[0]) && 4921*0b57cec5SDimitry Andric Floats[1].bitwiseIsEqual(RHS.Floats[1]); 4922*0b57cec5SDimitry Andric } 4923*0b57cec5SDimitry Andric 4924*0b57cec5SDimitry Andric hash_code hash_value(const DoubleAPFloat &Arg) { 4925*0b57cec5SDimitry Andric if (Arg.Floats) 4926*0b57cec5SDimitry Andric return hash_combine(hash_value(Arg.Floats[0]), hash_value(Arg.Floats[1])); 4927*0b57cec5SDimitry Andric return hash_combine(Arg.Semantics); 4928*0b57cec5SDimitry Andric } 4929*0b57cec5SDimitry Andric 4930*0b57cec5SDimitry Andric APInt DoubleAPFloat::bitcastToAPInt() const { 4931*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4932*0b57cec5SDimitry Andric uint64_t Data[] = { 4933*0b57cec5SDimitry Andric Floats[0].bitcastToAPInt().getRawData()[0], 4934*0b57cec5SDimitry Andric Floats[1].bitcastToAPInt().getRawData()[0], 4935*0b57cec5SDimitry Andric }; 4936*0b57cec5SDimitry Andric return APInt(128, 2, Data); 4937*0b57cec5SDimitry Andric } 4938*0b57cec5SDimitry Andric 4939480093f4SDimitry Andric Expected<APFloat::opStatus> DoubleAPFloat::convertFromString(StringRef S, 4940*0b57cec5SDimitry Andric roundingMode RM) { 4941*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4942*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy); 4943*0b57cec5SDimitry Andric auto Ret = Tmp.convertFromString(S, RM); 4944*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4945*0b57cec5SDimitry Andric return Ret; 4946*0b57cec5SDimitry Andric } 4947*0b57cec5SDimitry Andric 4948*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::next(bool nextDown) { 4949*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4950*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4951*0b57cec5SDimitry Andric auto Ret = Tmp.next(nextDown); 4952*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4953*0b57cec5SDimitry Andric return Ret; 4954*0b57cec5SDimitry Andric } 4955*0b57cec5SDimitry Andric 4956*0b57cec5SDimitry Andric APFloat::opStatus 4957*0b57cec5SDimitry Andric DoubleAPFloat::convertToInteger(MutableArrayRef<integerPart> Input, 4958*0b57cec5SDimitry Andric unsigned int Width, bool IsSigned, 4959*0b57cec5SDimitry Andric roundingMode RM, bool *IsExact) const { 4960*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4961*0b57cec5SDimitry Andric return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) 4962*0b57cec5SDimitry Andric .convertToInteger(Input, Width, IsSigned, RM, IsExact); 4963*0b57cec5SDimitry Andric } 4964*0b57cec5SDimitry Andric 4965*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::convertFromAPInt(const APInt &Input, 4966*0b57cec5SDimitry Andric bool IsSigned, 4967*0b57cec5SDimitry Andric roundingMode RM) { 4968*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4969*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy); 4970*0b57cec5SDimitry Andric auto Ret = Tmp.convertFromAPInt(Input, IsSigned, RM); 4971*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4972*0b57cec5SDimitry Andric return Ret; 4973*0b57cec5SDimitry Andric } 4974*0b57cec5SDimitry Andric 4975*0b57cec5SDimitry Andric APFloat::opStatus 4976*0b57cec5SDimitry Andric DoubleAPFloat::convertFromSignExtendedInteger(const integerPart *Input, 4977*0b57cec5SDimitry Andric unsigned int InputSize, 4978*0b57cec5SDimitry Andric bool IsSigned, roundingMode RM) { 4979*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4980*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy); 4981*0b57cec5SDimitry Andric auto Ret = Tmp.convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM); 4982*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4983*0b57cec5SDimitry Andric return Ret; 4984*0b57cec5SDimitry Andric } 4985*0b57cec5SDimitry Andric 4986*0b57cec5SDimitry Andric APFloat::opStatus 4987*0b57cec5SDimitry Andric DoubleAPFloat::convertFromZeroExtendedInteger(const integerPart *Input, 4988*0b57cec5SDimitry Andric unsigned int InputSize, 4989*0b57cec5SDimitry Andric bool IsSigned, roundingMode RM) { 4990*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4991*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy); 4992*0b57cec5SDimitry Andric auto Ret = Tmp.convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM); 4993*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4994*0b57cec5SDimitry Andric return Ret; 4995*0b57cec5SDimitry Andric } 4996*0b57cec5SDimitry Andric 4997*0b57cec5SDimitry Andric unsigned int DoubleAPFloat::convertToHexString(char *DST, 4998*0b57cec5SDimitry Andric unsigned int HexDigits, 4999*0b57cec5SDimitry Andric bool UpperCase, 5000*0b57cec5SDimitry Andric roundingMode RM) const { 5001*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 5002*0b57cec5SDimitry Andric return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) 5003*0b57cec5SDimitry Andric .convertToHexString(DST, HexDigits, UpperCase, RM); 5004*0b57cec5SDimitry Andric } 5005*0b57cec5SDimitry Andric 5006*0b57cec5SDimitry Andric bool DoubleAPFloat::isDenormal() const { 5007*0b57cec5SDimitry Andric return getCategory() == fcNormal && 5008*0b57cec5SDimitry Andric (Floats[0].isDenormal() || Floats[1].isDenormal() || 5009*0b57cec5SDimitry Andric // (double)(Hi + Lo) == Hi defines a normal number. 50105ffd83dbSDimitry Andric Floats[0] != Floats[0] + Floats[1]); 5011*0b57cec5SDimitry Andric } 5012*0b57cec5SDimitry Andric 5013*0b57cec5SDimitry Andric bool DoubleAPFloat::isSmallest() const { 5014*0b57cec5SDimitry Andric if (getCategory() != fcNormal) 5015*0b57cec5SDimitry Andric return false; 5016*0b57cec5SDimitry Andric DoubleAPFloat Tmp(*this); 5017*0b57cec5SDimitry Andric Tmp.makeSmallest(this->isNegative()); 5018*0b57cec5SDimitry Andric return Tmp.compare(*this) == cmpEqual; 5019*0b57cec5SDimitry Andric } 5020*0b57cec5SDimitry Andric 5021bdd1243dSDimitry Andric bool DoubleAPFloat::isSmallestNormalized() const { 5022bdd1243dSDimitry Andric if (getCategory() != fcNormal) 5023bdd1243dSDimitry Andric return false; 5024bdd1243dSDimitry Andric 5025bdd1243dSDimitry Andric DoubleAPFloat Tmp(*this); 5026bdd1243dSDimitry Andric Tmp.makeSmallestNormalized(this->isNegative()); 5027bdd1243dSDimitry Andric return Tmp.compare(*this) == cmpEqual; 5028bdd1243dSDimitry Andric } 5029bdd1243dSDimitry Andric 5030*0b57cec5SDimitry Andric bool DoubleAPFloat::isLargest() const { 5031*0b57cec5SDimitry Andric if (getCategory() != fcNormal) 5032*0b57cec5SDimitry Andric return false; 5033*0b57cec5SDimitry Andric DoubleAPFloat Tmp(*this); 5034*0b57cec5SDimitry Andric Tmp.makeLargest(this->isNegative()); 5035*0b57cec5SDimitry Andric return Tmp.compare(*this) == cmpEqual; 5036*0b57cec5SDimitry Andric } 5037*0b57cec5SDimitry Andric 5038*0b57cec5SDimitry Andric bool DoubleAPFloat::isInteger() const { 5039*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 5040*0b57cec5SDimitry Andric return Floats[0].isInteger() && Floats[1].isInteger(); 5041*0b57cec5SDimitry Andric } 5042*0b57cec5SDimitry Andric 5043*0b57cec5SDimitry Andric void DoubleAPFloat::toString(SmallVectorImpl<char> &Str, 5044*0b57cec5SDimitry Andric unsigned FormatPrecision, 5045*0b57cec5SDimitry Andric unsigned FormatMaxPadding, 5046*0b57cec5SDimitry Andric bool TruncateZero) const { 5047*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 5048*0b57cec5SDimitry Andric APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) 5049*0b57cec5SDimitry Andric .toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero); 5050*0b57cec5SDimitry Andric } 5051*0b57cec5SDimitry Andric 5052*0b57cec5SDimitry Andric bool DoubleAPFloat::getExactInverse(APFloat *inv) const { 5053*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 5054*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 5055*0b57cec5SDimitry Andric if (!inv) 5056*0b57cec5SDimitry Andric return Tmp.getExactInverse(nullptr); 5057*0b57cec5SDimitry Andric APFloat Inv(semPPCDoubleDoubleLegacy); 5058*0b57cec5SDimitry Andric auto Ret = Tmp.getExactInverse(&Inv); 5059*0b57cec5SDimitry Andric *inv = APFloat(semPPCDoubleDouble, Inv.bitcastToAPInt()); 5060*0b57cec5SDimitry Andric return Ret; 5061*0b57cec5SDimitry Andric } 5062*0b57cec5SDimitry Andric 5063fe6060f1SDimitry Andric DoubleAPFloat scalbn(const DoubleAPFloat &Arg, int Exp, 5064fe6060f1SDimitry Andric APFloat::roundingMode RM) { 5065*0b57cec5SDimitry Andric assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 5066*0b57cec5SDimitry Andric return DoubleAPFloat(semPPCDoubleDouble, scalbn(Arg.Floats[0], Exp, RM), 5067*0b57cec5SDimitry Andric scalbn(Arg.Floats[1], Exp, RM)); 5068*0b57cec5SDimitry Andric } 5069*0b57cec5SDimitry Andric 5070*0b57cec5SDimitry Andric DoubleAPFloat frexp(const DoubleAPFloat &Arg, int &Exp, 5071*0b57cec5SDimitry Andric APFloat::roundingMode RM) { 5072*0b57cec5SDimitry Andric assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 5073*0b57cec5SDimitry Andric APFloat First = frexp(Arg.Floats[0], Exp, RM); 5074*0b57cec5SDimitry Andric APFloat Second = Arg.Floats[1]; 5075*0b57cec5SDimitry Andric if (Arg.getCategory() == APFloat::fcNormal) 5076*0b57cec5SDimitry Andric Second = scalbn(Second, -Exp, RM); 5077*0b57cec5SDimitry Andric return DoubleAPFloat(semPPCDoubleDouble, std::move(First), std::move(Second)); 5078*0b57cec5SDimitry Andric } 5079*0b57cec5SDimitry Andric 5080fe6060f1SDimitry Andric } // namespace detail 5081*0b57cec5SDimitry Andric 5082*0b57cec5SDimitry Andric APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) { 5083*0b57cec5SDimitry Andric if (usesLayout<IEEEFloat>(Semantics)) { 5084*0b57cec5SDimitry Andric new (&IEEE) IEEEFloat(std::move(F)); 5085*0b57cec5SDimitry Andric return; 5086*0b57cec5SDimitry Andric } 5087*0b57cec5SDimitry Andric if (usesLayout<DoubleAPFloat>(Semantics)) { 5088*0b57cec5SDimitry Andric const fltSemantics& S = F.getSemantics(); 5089*0b57cec5SDimitry Andric new (&Double) 5090*0b57cec5SDimitry Andric DoubleAPFloat(Semantics, APFloat(std::move(F), S), 5091*0b57cec5SDimitry Andric APFloat(semIEEEdouble)); 5092*0b57cec5SDimitry Andric return; 5093*0b57cec5SDimitry Andric } 5094*0b57cec5SDimitry Andric llvm_unreachable("Unexpected semantics"); 5095*0b57cec5SDimitry Andric } 5096*0b57cec5SDimitry Andric 5097480093f4SDimitry Andric Expected<APFloat::opStatus> APFloat::convertFromString(StringRef Str, 5098480093f4SDimitry Andric roundingMode RM) { 5099*0b57cec5SDimitry Andric APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM)); 5100*0b57cec5SDimitry Andric } 5101*0b57cec5SDimitry Andric 5102*0b57cec5SDimitry Andric hash_code hash_value(const APFloat &Arg) { 5103*0b57cec5SDimitry Andric if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics())) 5104*0b57cec5SDimitry Andric return hash_value(Arg.U.IEEE); 5105*0b57cec5SDimitry Andric if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics())) 5106*0b57cec5SDimitry Andric return hash_value(Arg.U.Double); 5107*0b57cec5SDimitry Andric llvm_unreachable("Unexpected semantics"); 5108*0b57cec5SDimitry Andric } 5109*0b57cec5SDimitry Andric 5110*0b57cec5SDimitry Andric APFloat::APFloat(const fltSemantics &Semantics, StringRef S) 5111*0b57cec5SDimitry Andric : APFloat(Semantics) { 5112480093f4SDimitry Andric auto StatusOrErr = convertFromString(S, rmNearestTiesToEven); 5113480093f4SDimitry Andric assert(StatusOrErr && "Invalid floating point representation"); 5114480093f4SDimitry Andric consumeError(StatusOrErr.takeError()); 5115*0b57cec5SDimitry Andric } 5116*0b57cec5SDimitry Andric 5117*0b57cec5SDimitry Andric APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics, 5118*0b57cec5SDimitry Andric roundingMode RM, bool *losesInfo) { 5119*0b57cec5SDimitry Andric if (&getSemantics() == &ToSemantics) { 5120*0b57cec5SDimitry Andric *losesInfo = false; 5121*0b57cec5SDimitry Andric return opOK; 5122*0b57cec5SDimitry Andric } 5123*0b57cec5SDimitry Andric if (usesLayout<IEEEFloat>(getSemantics()) && 5124*0b57cec5SDimitry Andric usesLayout<IEEEFloat>(ToSemantics)) 5125*0b57cec5SDimitry Andric return U.IEEE.convert(ToSemantics, RM, losesInfo); 5126*0b57cec5SDimitry Andric if (usesLayout<IEEEFloat>(getSemantics()) && 5127*0b57cec5SDimitry Andric usesLayout<DoubleAPFloat>(ToSemantics)) { 5128*0b57cec5SDimitry Andric assert(&ToSemantics == &semPPCDoubleDouble); 5129*0b57cec5SDimitry Andric auto Ret = U.IEEE.convert(semPPCDoubleDoubleLegacy, RM, losesInfo); 5130*0b57cec5SDimitry Andric *this = APFloat(ToSemantics, U.IEEE.bitcastToAPInt()); 5131*0b57cec5SDimitry Andric return Ret; 5132*0b57cec5SDimitry Andric } 5133*0b57cec5SDimitry Andric if (usesLayout<DoubleAPFloat>(getSemantics()) && 5134*0b57cec5SDimitry Andric usesLayout<IEEEFloat>(ToSemantics)) { 5135*0b57cec5SDimitry Andric auto Ret = getIEEE().convert(ToSemantics, RM, losesInfo); 5136*0b57cec5SDimitry Andric *this = APFloat(std::move(getIEEE()), ToSemantics); 5137*0b57cec5SDimitry Andric return Ret; 5138*0b57cec5SDimitry Andric } 5139*0b57cec5SDimitry Andric llvm_unreachable("Unexpected semantics"); 5140*0b57cec5SDimitry Andric } 5141*0b57cec5SDimitry Andric 5142349cc55cSDimitry Andric APFloat APFloat::getAllOnesValue(const fltSemantics &Semantics) { 5143349cc55cSDimitry Andric return APFloat(Semantics, APInt::getAllOnes(Semantics.sizeInBits)); 5144*0b57cec5SDimitry Andric } 5145*0b57cec5SDimitry Andric 5146*0b57cec5SDimitry Andric void APFloat::print(raw_ostream &OS) const { 5147*0b57cec5SDimitry Andric SmallVector<char, 16> Buffer; 5148*0b57cec5SDimitry Andric toString(Buffer); 5149*0b57cec5SDimitry Andric OS << Buffer << "\n"; 5150*0b57cec5SDimitry Andric } 5151*0b57cec5SDimitry Andric 5152*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 5153*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void APFloat::dump() const { print(dbgs()); } 5154*0b57cec5SDimitry Andric #endif 5155*0b57cec5SDimitry Andric 5156*0b57cec5SDimitry Andric void APFloat::Profile(FoldingSetNodeID &NID) const { 5157*0b57cec5SDimitry Andric NID.Add(bitcastToAPInt()); 5158*0b57cec5SDimitry Andric } 5159*0b57cec5SDimitry Andric 5160*0b57cec5SDimitry Andric /* Same as convertToInteger(integerPart*, ...), except the result is returned in 5161*0b57cec5SDimitry Andric an APSInt, whose initial bit-width and signed-ness are used to determine the 5162*0b57cec5SDimitry Andric precision of the conversion. 5163*0b57cec5SDimitry Andric */ 5164*0b57cec5SDimitry Andric APFloat::opStatus APFloat::convertToInteger(APSInt &result, 5165*0b57cec5SDimitry Andric roundingMode rounding_mode, 5166*0b57cec5SDimitry Andric bool *isExact) const { 5167*0b57cec5SDimitry Andric unsigned bitWidth = result.getBitWidth(); 5168*0b57cec5SDimitry Andric SmallVector<uint64_t, 4> parts(result.getNumWords()); 5169*0b57cec5SDimitry Andric opStatus status = convertToInteger(parts, bitWidth, result.isSigned(), 5170*0b57cec5SDimitry Andric rounding_mode, isExact); 5171*0b57cec5SDimitry Andric // Keeps the original signed-ness. 5172*0b57cec5SDimitry Andric result = APInt(bitWidth, parts); 5173*0b57cec5SDimitry Andric return status; 5174*0b57cec5SDimitry Andric } 5175*0b57cec5SDimitry Andric 5176fe6060f1SDimitry Andric double APFloat::convertToDouble() const { 5177fe6060f1SDimitry Andric if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEdouble) 5178fe6060f1SDimitry Andric return getIEEE().convertToDouble(); 5179fe6060f1SDimitry Andric assert(getSemantics().isRepresentableBy(semIEEEdouble) && 5180fe6060f1SDimitry Andric "Float semantics is not representable by IEEEdouble"); 5181fe6060f1SDimitry Andric APFloat Temp = *this; 5182fe6060f1SDimitry Andric bool LosesInfo; 5183fe6060f1SDimitry Andric opStatus St = Temp.convert(semIEEEdouble, rmNearestTiesToEven, &LosesInfo); 5184fe6060f1SDimitry Andric assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision"); 5185fe6060f1SDimitry Andric (void)St; 5186fe6060f1SDimitry Andric return Temp.getIEEE().convertToDouble(); 5187fe6060f1SDimitry Andric } 5188fe6060f1SDimitry Andric 5189fe6060f1SDimitry Andric float APFloat::convertToFloat() const { 5190fe6060f1SDimitry Andric if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEsingle) 5191fe6060f1SDimitry Andric return getIEEE().convertToFloat(); 5192fe6060f1SDimitry Andric assert(getSemantics().isRepresentableBy(semIEEEsingle) && 5193fe6060f1SDimitry Andric "Float semantics is not representable by IEEEsingle"); 5194fe6060f1SDimitry Andric APFloat Temp = *this; 5195fe6060f1SDimitry Andric bool LosesInfo; 5196fe6060f1SDimitry Andric opStatus St = Temp.convert(semIEEEsingle, rmNearestTiesToEven, &LosesInfo); 5197fe6060f1SDimitry Andric assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision"); 5198fe6060f1SDimitry Andric (void)St; 5199fe6060f1SDimitry Andric return Temp.getIEEE().convertToFloat(); 5200fe6060f1SDimitry Andric } 5201fe6060f1SDimitry Andric 5202e8d8bef9SDimitry Andric } // namespace llvm 5203*0b57cec5SDimitry Andric 5204*0b57cec5SDimitry Andric #undef APFLOAT_DISPATCH_ON_SEMANTICS 5205