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" 23*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.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 { 53*0b57cec5SDimitry Andric /* Represents floating point arithmetic semantics. */ 54*0b57cec5SDimitry Andric struct fltSemantics { 55*0b57cec5SDimitry Andric /* The largest E such that 2^E is representable; this matches the 56*0b57cec5SDimitry Andric definition of IEEE 754. */ 57*0b57cec5SDimitry Andric APFloatBase::ExponentType maxExponent; 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric /* The smallest E such that 2^E is a normalized number; this 60*0b57cec5SDimitry Andric matches the definition of IEEE 754. */ 61*0b57cec5SDimitry Andric APFloatBase::ExponentType minExponent; 62*0b57cec5SDimitry Andric 63*0b57cec5SDimitry Andric /* Number of bits in the significand. This includes the integer 64*0b57cec5SDimitry Andric bit. */ 65*0b57cec5SDimitry Andric unsigned int precision; 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric /* Number of bits actually used in the semantics. */ 68*0b57cec5SDimitry Andric unsigned int sizeInBits; 69*0b57cec5SDimitry Andric }; 70*0b57cec5SDimitry Andric 71*0b57cec5SDimitry Andric static const fltSemantics semIEEEhalf = {15, -14, 11, 16}; 72*0b57cec5SDimitry Andric static const fltSemantics semIEEEsingle = {127, -126, 24, 32}; 73*0b57cec5SDimitry Andric static const fltSemantics semIEEEdouble = {1023, -1022, 53, 64}; 74*0b57cec5SDimitry Andric static const fltSemantics semIEEEquad = {16383, -16382, 113, 128}; 75*0b57cec5SDimitry Andric static const fltSemantics semX87DoubleExtended = {16383, -16382, 64, 80}; 76*0b57cec5SDimitry Andric static const fltSemantics semBogus = {0, 0, 0, 0}; 77*0b57cec5SDimitry Andric 78*0b57cec5SDimitry Andric /* The IBM double-double semantics. Such a number consists of a pair of IEEE 79*0b57cec5SDimitry Andric 64-bit doubles (Hi, Lo), where |Hi| > |Lo|, and if normal, 80*0b57cec5SDimitry Andric (double)(Hi + Lo) == Hi. The numeric value it's modeling is Hi + Lo. 81*0b57cec5SDimitry Andric Therefore it has two 53-bit mantissa parts that aren't necessarily adjacent 82*0b57cec5SDimitry Andric to each other, and two 11-bit exponents. 83*0b57cec5SDimitry Andric 84*0b57cec5SDimitry Andric Note: we need to make the value different from semBogus as otherwise 85*0b57cec5SDimitry Andric an unsafe optimization may collapse both values to a single address, 86*0b57cec5SDimitry Andric and we heavily rely on them having distinct addresses. */ 87*0b57cec5SDimitry Andric static const fltSemantics semPPCDoubleDouble = {-1, 0, 0, 0}; 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric /* These are legacy semantics for the fallback, inaccrurate implementation of 90*0b57cec5SDimitry Andric IBM double-double, if the accurate semPPCDoubleDouble doesn't handle the 91*0b57cec5SDimitry Andric operation. It's equivalent to having an IEEE number with consecutive 106 92*0b57cec5SDimitry Andric bits of mantissa and 11 bits of exponent. 93*0b57cec5SDimitry Andric 94*0b57cec5SDimitry Andric It's not equivalent to IBM double-double. For example, a legit IBM 95*0b57cec5SDimitry Andric double-double, 1 + epsilon: 96*0b57cec5SDimitry Andric 97*0b57cec5SDimitry Andric 1 + epsilon = 1 + (1 >> 1076) 98*0b57cec5SDimitry Andric 99*0b57cec5SDimitry Andric is not representable by a consecutive 106 bits of mantissa. 100*0b57cec5SDimitry Andric 101*0b57cec5SDimitry Andric Currently, these semantics are used in the following way: 102*0b57cec5SDimitry Andric 103*0b57cec5SDimitry Andric semPPCDoubleDouble -> (IEEEdouble, IEEEdouble) -> 104*0b57cec5SDimitry Andric (64-bit APInt, 64-bit APInt) -> (128-bit APInt) -> 105*0b57cec5SDimitry Andric semPPCDoubleDoubleLegacy -> IEEE operations 106*0b57cec5SDimitry Andric 107*0b57cec5SDimitry Andric We use bitcastToAPInt() to get the bit representation (in APInt) of the 108*0b57cec5SDimitry Andric underlying IEEEdouble, then use the APInt constructor to construct the 109*0b57cec5SDimitry Andric legacy IEEE float. 110*0b57cec5SDimitry Andric 111*0b57cec5SDimitry Andric TODO: Implement all operations in semPPCDoubleDouble, and delete these 112*0b57cec5SDimitry Andric semantics. */ 113*0b57cec5SDimitry Andric static const fltSemantics semPPCDoubleDoubleLegacy = {1023, -1022 + 53, 114*0b57cec5SDimitry Andric 53 + 53, 128}; 115*0b57cec5SDimitry Andric 116*0b57cec5SDimitry Andric const llvm::fltSemantics &APFloatBase::EnumToSemantics(Semantics S) { 117*0b57cec5SDimitry Andric switch (S) { 118*0b57cec5SDimitry Andric case S_IEEEhalf: 119*0b57cec5SDimitry Andric return IEEEhalf(); 120*0b57cec5SDimitry Andric case S_IEEEsingle: 121*0b57cec5SDimitry Andric return IEEEsingle(); 122*0b57cec5SDimitry Andric case S_IEEEdouble: 123*0b57cec5SDimitry Andric return IEEEdouble(); 124*0b57cec5SDimitry Andric case S_x87DoubleExtended: 125*0b57cec5SDimitry Andric return x87DoubleExtended(); 126*0b57cec5SDimitry Andric case S_IEEEquad: 127*0b57cec5SDimitry Andric return IEEEquad(); 128*0b57cec5SDimitry Andric case S_PPCDoubleDouble: 129*0b57cec5SDimitry Andric return PPCDoubleDouble(); 130*0b57cec5SDimitry Andric } 131*0b57cec5SDimitry Andric llvm_unreachable("Unrecognised floating semantics"); 132*0b57cec5SDimitry Andric } 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric APFloatBase::Semantics 135*0b57cec5SDimitry Andric APFloatBase::SemanticsToEnum(const llvm::fltSemantics &Sem) { 136*0b57cec5SDimitry Andric if (&Sem == &llvm::APFloat::IEEEhalf()) 137*0b57cec5SDimitry Andric return S_IEEEhalf; 138*0b57cec5SDimitry Andric else if (&Sem == &llvm::APFloat::IEEEsingle()) 139*0b57cec5SDimitry Andric return S_IEEEsingle; 140*0b57cec5SDimitry Andric else if (&Sem == &llvm::APFloat::IEEEdouble()) 141*0b57cec5SDimitry Andric return S_IEEEdouble; 142*0b57cec5SDimitry Andric else if (&Sem == &llvm::APFloat::x87DoubleExtended()) 143*0b57cec5SDimitry Andric return S_x87DoubleExtended; 144*0b57cec5SDimitry Andric else if (&Sem == &llvm::APFloat::IEEEquad()) 145*0b57cec5SDimitry Andric return S_IEEEquad; 146*0b57cec5SDimitry Andric else if (&Sem == &llvm::APFloat::PPCDoubleDouble()) 147*0b57cec5SDimitry Andric return S_PPCDoubleDouble; 148*0b57cec5SDimitry Andric else 149*0b57cec5SDimitry Andric llvm_unreachable("Unknown floating semantics"); 150*0b57cec5SDimitry Andric } 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::IEEEhalf() { 153*0b57cec5SDimitry Andric return semIEEEhalf; 154*0b57cec5SDimitry Andric } 155*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::IEEEsingle() { 156*0b57cec5SDimitry Andric return semIEEEsingle; 157*0b57cec5SDimitry Andric } 158*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::IEEEdouble() { 159*0b57cec5SDimitry Andric return semIEEEdouble; 160*0b57cec5SDimitry Andric } 161*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::IEEEquad() { 162*0b57cec5SDimitry Andric return semIEEEquad; 163*0b57cec5SDimitry Andric } 164*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::x87DoubleExtended() { 165*0b57cec5SDimitry Andric return semX87DoubleExtended; 166*0b57cec5SDimitry Andric } 167*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::Bogus() { 168*0b57cec5SDimitry Andric return semBogus; 169*0b57cec5SDimitry Andric } 170*0b57cec5SDimitry Andric const fltSemantics &APFloatBase::PPCDoubleDouble() { 171*0b57cec5SDimitry Andric return semPPCDoubleDouble; 172*0b57cec5SDimitry Andric } 173*0b57cec5SDimitry Andric 174*0b57cec5SDimitry Andric /* A tight upper bound on number of parts required to hold the value 175*0b57cec5SDimitry Andric pow(5, power) is 176*0b57cec5SDimitry Andric 177*0b57cec5SDimitry Andric power * 815 / (351 * integerPartWidth) + 1 178*0b57cec5SDimitry Andric 179*0b57cec5SDimitry Andric However, whilst the result may require only this many parts, 180*0b57cec5SDimitry Andric because we are multiplying two values to get it, the 181*0b57cec5SDimitry Andric multiplication may require an extra part with the excess part 182*0b57cec5SDimitry Andric being zero (consider the trivial case of 1 * 1, tcFullMultiply 183*0b57cec5SDimitry Andric requires two parts to hold the single-part result). So we add an 184*0b57cec5SDimitry Andric extra one to guarantee enough space whilst multiplying. */ 185*0b57cec5SDimitry Andric const unsigned int maxExponent = 16383; 186*0b57cec5SDimitry Andric const unsigned int maxPrecision = 113; 187*0b57cec5SDimitry Andric const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1; 188*0b57cec5SDimitry Andric const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815) / (351 * APFloatBase::integerPartWidth)); 189*0b57cec5SDimitry Andric 190*0b57cec5SDimitry Andric unsigned int APFloatBase::semanticsPrecision(const fltSemantics &semantics) { 191*0b57cec5SDimitry Andric return semantics.precision; 192*0b57cec5SDimitry Andric } 193*0b57cec5SDimitry Andric APFloatBase::ExponentType 194*0b57cec5SDimitry Andric APFloatBase::semanticsMaxExponent(const fltSemantics &semantics) { 195*0b57cec5SDimitry Andric return semantics.maxExponent; 196*0b57cec5SDimitry Andric } 197*0b57cec5SDimitry Andric APFloatBase::ExponentType 198*0b57cec5SDimitry Andric APFloatBase::semanticsMinExponent(const fltSemantics &semantics) { 199*0b57cec5SDimitry Andric return semantics.minExponent; 200*0b57cec5SDimitry Andric } 201*0b57cec5SDimitry Andric unsigned int APFloatBase::semanticsSizeInBits(const fltSemantics &semantics) { 202*0b57cec5SDimitry Andric return semantics.sizeInBits; 203*0b57cec5SDimitry Andric } 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) { 206*0b57cec5SDimitry Andric return Sem.sizeInBits; 207*0b57cec5SDimitry Andric } 208*0b57cec5SDimitry Andric 209*0b57cec5SDimitry Andric /* A bunch of private, handy routines. */ 210*0b57cec5SDimitry Andric 211*0b57cec5SDimitry Andric static inline unsigned int 212*0b57cec5SDimitry Andric partCountForBits(unsigned int bits) 213*0b57cec5SDimitry Andric { 214*0b57cec5SDimitry Andric return ((bits) + APFloatBase::integerPartWidth - 1) / APFloatBase::integerPartWidth; 215*0b57cec5SDimitry Andric } 216*0b57cec5SDimitry Andric 217*0b57cec5SDimitry Andric /* Returns 0U-9U. Return values >= 10U are not digits. */ 218*0b57cec5SDimitry Andric static inline unsigned int 219*0b57cec5SDimitry Andric decDigitValue(unsigned int c) 220*0b57cec5SDimitry Andric { 221*0b57cec5SDimitry Andric return c - '0'; 222*0b57cec5SDimitry Andric } 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric /* Return the value of a decimal exponent of the form 225*0b57cec5SDimitry Andric [+-]ddddddd. 226*0b57cec5SDimitry Andric 227*0b57cec5SDimitry Andric If the exponent overflows, returns a large exponent with the 228*0b57cec5SDimitry Andric appropriate sign. */ 229*0b57cec5SDimitry Andric static int 230*0b57cec5SDimitry Andric readExponent(StringRef::iterator begin, StringRef::iterator end) 231*0b57cec5SDimitry Andric { 232*0b57cec5SDimitry Andric bool isNegative; 233*0b57cec5SDimitry Andric unsigned int absExponent; 234*0b57cec5SDimitry Andric const unsigned int overlargeExponent = 24000; /* FIXME. */ 235*0b57cec5SDimitry Andric StringRef::iterator p = begin; 236*0b57cec5SDimitry Andric 237*0b57cec5SDimitry Andric // Treat no exponent as 0 to match binutils 238*0b57cec5SDimitry Andric if (p == end || ((*p == '-' || *p == '+') && (p + 1) == end)) { 239*0b57cec5SDimitry Andric return 0; 240*0b57cec5SDimitry Andric } 241*0b57cec5SDimitry Andric 242*0b57cec5SDimitry Andric isNegative = (*p == '-'); 243*0b57cec5SDimitry Andric if (*p == '-' || *p == '+') { 244*0b57cec5SDimitry Andric p++; 245*0b57cec5SDimitry Andric assert(p != end && "Exponent has no digits"); 246*0b57cec5SDimitry Andric } 247*0b57cec5SDimitry Andric 248*0b57cec5SDimitry Andric absExponent = decDigitValue(*p++); 249*0b57cec5SDimitry Andric assert(absExponent < 10U && "Invalid character in exponent"); 250*0b57cec5SDimitry Andric 251*0b57cec5SDimitry Andric for (; p != end; ++p) { 252*0b57cec5SDimitry Andric unsigned int value; 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric value = decDigitValue(*p); 255*0b57cec5SDimitry Andric assert(value < 10U && "Invalid character in exponent"); 256*0b57cec5SDimitry Andric 257*0b57cec5SDimitry Andric value += absExponent * 10; 258*0b57cec5SDimitry Andric if (absExponent >= overlargeExponent) { 259*0b57cec5SDimitry Andric absExponent = overlargeExponent; 260*0b57cec5SDimitry Andric p = end; /* outwit assert below */ 261*0b57cec5SDimitry Andric break; 262*0b57cec5SDimitry Andric } 263*0b57cec5SDimitry Andric absExponent = value; 264*0b57cec5SDimitry Andric } 265*0b57cec5SDimitry Andric 266*0b57cec5SDimitry Andric assert(p == end && "Invalid exponent in exponent"); 267*0b57cec5SDimitry Andric 268*0b57cec5SDimitry Andric if (isNegative) 269*0b57cec5SDimitry Andric return -(int) absExponent; 270*0b57cec5SDimitry Andric else 271*0b57cec5SDimitry Andric return (int) absExponent; 272*0b57cec5SDimitry Andric } 273*0b57cec5SDimitry Andric 274*0b57cec5SDimitry Andric /* This is ugly and needs cleaning up, but I don't immediately see 275*0b57cec5SDimitry Andric how whilst remaining safe. */ 276*0b57cec5SDimitry Andric static int 277*0b57cec5SDimitry Andric totalExponent(StringRef::iterator p, StringRef::iterator end, 278*0b57cec5SDimitry Andric int exponentAdjustment) 279*0b57cec5SDimitry Andric { 280*0b57cec5SDimitry Andric int unsignedExponent; 281*0b57cec5SDimitry Andric bool negative, overflow; 282*0b57cec5SDimitry Andric int exponent = 0; 283*0b57cec5SDimitry Andric 284*0b57cec5SDimitry Andric assert(p != end && "Exponent has no digits"); 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric negative = *p == '-'; 287*0b57cec5SDimitry Andric if (*p == '-' || *p == '+') { 288*0b57cec5SDimitry Andric p++; 289*0b57cec5SDimitry Andric assert(p != end && "Exponent has no digits"); 290*0b57cec5SDimitry Andric } 291*0b57cec5SDimitry Andric 292*0b57cec5SDimitry Andric unsignedExponent = 0; 293*0b57cec5SDimitry Andric overflow = false; 294*0b57cec5SDimitry Andric for (; p != end; ++p) { 295*0b57cec5SDimitry Andric unsigned int value; 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric value = decDigitValue(*p); 298*0b57cec5SDimitry Andric assert(value < 10U && "Invalid character in exponent"); 299*0b57cec5SDimitry Andric 300*0b57cec5SDimitry Andric unsignedExponent = unsignedExponent * 10 + value; 301*0b57cec5SDimitry Andric if (unsignedExponent > 32767) { 302*0b57cec5SDimitry Andric overflow = true; 303*0b57cec5SDimitry Andric break; 304*0b57cec5SDimitry Andric } 305*0b57cec5SDimitry Andric } 306*0b57cec5SDimitry Andric 307*0b57cec5SDimitry Andric if (exponentAdjustment > 32767 || exponentAdjustment < -32768) 308*0b57cec5SDimitry Andric overflow = true; 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric if (!overflow) { 311*0b57cec5SDimitry Andric exponent = unsignedExponent; 312*0b57cec5SDimitry Andric if (negative) 313*0b57cec5SDimitry Andric exponent = -exponent; 314*0b57cec5SDimitry Andric exponent += exponentAdjustment; 315*0b57cec5SDimitry Andric if (exponent > 32767 || exponent < -32768) 316*0b57cec5SDimitry Andric overflow = true; 317*0b57cec5SDimitry Andric } 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric if (overflow) 320*0b57cec5SDimitry Andric exponent = negative ? -32768: 32767; 321*0b57cec5SDimitry Andric 322*0b57cec5SDimitry Andric return exponent; 323*0b57cec5SDimitry Andric } 324*0b57cec5SDimitry Andric 325*0b57cec5SDimitry Andric static StringRef::iterator 326*0b57cec5SDimitry Andric skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, 327*0b57cec5SDimitry Andric StringRef::iterator *dot) 328*0b57cec5SDimitry Andric { 329*0b57cec5SDimitry Andric StringRef::iterator p = begin; 330*0b57cec5SDimitry Andric *dot = end; 331*0b57cec5SDimitry Andric while (p != end && *p == '0') 332*0b57cec5SDimitry Andric p++; 333*0b57cec5SDimitry Andric 334*0b57cec5SDimitry Andric if (p != end && *p == '.') { 335*0b57cec5SDimitry Andric *dot = p++; 336*0b57cec5SDimitry Andric 337*0b57cec5SDimitry Andric assert(end - begin != 1 && "Significand has no digits"); 338*0b57cec5SDimitry Andric 339*0b57cec5SDimitry Andric while (p != end && *p == '0') 340*0b57cec5SDimitry Andric p++; 341*0b57cec5SDimitry Andric } 342*0b57cec5SDimitry Andric 343*0b57cec5SDimitry Andric return p; 344*0b57cec5SDimitry Andric } 345*0b57cec5SDimitry Andric 346*0b57cec5SDimitry Andric /* Given a normal decimal floating point number of the form 347*0b57cec5SDimitry Andric 348*0b57cec5SDimitry Andric dddd.dddd[eE][+-]ddd 349*0b57cec5SDimitry Andric 350*0b57cec5SDimitry Andric where the decimal point and exponent are optional, fill out the 351*0b57cec5SDimitry Andric structure D. Exponent is appropriate if the significand is 352*0b57cec5SDimitry Andric treated as an integer, and normalizedExponent if the significand 353*0b57cec5SDimitry Andric is taken to have the decimal point after a single leading 354*0b57cec5SDimitry Andric non-zero digit. 355*0b57cec5SDimitry Andric 356*0b57cec5SDimitry Andric If the value is zero, V->firstSigDigit points to a non-digit, and 357*0b57cec5SDimitry Andric the return exponent is zero. 358*0b57cec5SDimitry Andric */ 359*0b57cec5SDimitry Andric struct decimalInfo { 360*0b57cec5SDimitry Andric const char *firstSigDigit; 361*0b57cec5SDimitry Andric const char *lastSigDigit; 362*0b57cec5SDimitry Andric int exponent; 363*0b57cec5SDimitry Andric int normalizedExponent; 364*0b57cec5SDimitry Andric }; 365*0b57cec5SDimitry Andric 366*0b57cec5SDimitry Andric static void 367*0b57cec5SDimitry Andric interpretDecimal(StringRef::iterator begin, StringRef::iterator end, 368*0b57cec5SDimitry Andric decimalInfo *D) 369*0b57cec5SDimitry Andric { 370*0b57cec5SDimitry Andric StringRef::iterator dot = end; 371*0b57cec5SDimitry Andric StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot); 372*0b57cec5SDimitry Andric 373*0b57cec5SDimitry Andric D->firstSigDigit = p; 374*0b57cec5SDimitry Andric D->exponent = 0; 375*0b57cec5SDimitry Andric D->normalizedExponent = 0; 376*0b57cec5SDimitry Andric 377*0b57cec5SDimitry Andric for (; p != end; ++p) { 378*0b57cec5SDimitry Andric if (*p == '.') { 379*0b57cec5SDimitry Andric assert(dot == end && "String contains multiple dots"); 380*0b57cec5SDimitry Andric dot = p++; 381*0b57cec5SDimitry Andric if (p == end) 382*0b57cec5SDimitry Andric break; 383*0b57cec5SDimitry Andric } 384*0b57cec5SDimitry Andric if (decDigitValue(*p) >= 10U) 385*0b57cec5SDimitry Andric break; 386*0b57cec5SDimitry Andric } 387*0b57cec5SDimitry Andric 388*0b57cec5SDimitry Andric if (p != end) { 389*0b57cec5SDimitry Andric assert((*p == 'e' || *p == 'E') && "Invalid character in significand"); 390*0b57cec5SDimitry Andric assert(p != begin && "Significand has no digits"); 391*0b57cec5SDimitry Andric assert((dot == end || p - begin != 1) && "Significand has no digits"); 392*0b57cec5SDimitry Andric 393*0b57cec5SDimitry Andric /* p points to the first non-digit in the string */ 394*0b57cec5SDimitry Andric D->exponent = readExponent(p + 1, end); 395*0b57cec5SDimitry Andric 396*0b57cec5SDimitry Andric /* Implied decimal point? */ 397*0b57cec5SDimitry Andric if (dot == end) 398*0b57cec5SDimitry Andric dot = p; 399*0b57cec5SDimitry Andric } 400*0b57cec5SDimitry Andric 401*0b57cec5SDimitry Andric /* If number is all zeroes accept any exponent. */ 402*0b57cec5SDimitry Andric if (p != D->firstSigDigit) { 403*0b57cec5SDimitry Andric /* Drop insignificant trailing zeroes. */ 404*0b57cec5SDimitry Andric if (p != begin) { 405*0b57cec5SDimitry Andric do 406*0b57cec5SDimitry Andric do 407*0b57cec5SDimitry Andric p--; 408*0b57cec5SDimitry Andric while (p != begin && *p == '0'); 409*0b57cec5SDimitry Andric while (p != begin && *p == '.'); 410*0b57cec5SDimitry Andric } 411*0b57cec5SDimitry Andric 412*0b57cec5SDimitry Andric /* Adjust the exponents for any decimal point. */ 413*0b57cec5SDimitry Andric D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p)); 414*0b57cec5SDimitry Andric D->normalizedExponent = (D->exponent + 415*0b57cec5SDimitry Andric static_cast<APFloat::ExponentType>((p - D->firstSigDigit) 416*0b57cec5SDimitry Andric - (dot > D->firstSigDigit && dot < p))); 417*0b57cec5SDimitry Andric } 418*0b57cec5SDimitry Andric 419*0b57cec5SDimitry Andric D->lastSigDigit = p; 420*0b57cec5SDimitry Andric } 421*0b57cec5SDimitry Andric 422*0b57cec5SDimitry Andric /* Return the trailing fraction of a hexadecimal number. 423*0b57cec5SDimitry Andric DIGITVALUE is the first hex digit of the fraction, P points to 424*0b57cec5SDimitry Andric the next digit. */ 425*0b57cec5SDimitry Andric static lostFraction 426*0b57cec5SDimitry Andric trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, 427*0b57cec5SDimitry Andric unsigned int digitValue) 428*0b57cec5SDimitry Andric { 429*0b57cec5SDimitry Andric unsigned int hexDigit; 430*0b57cec5SDimitry Andric 431*0b57cec5SDimitry Andric /* If the first trailing digit isn't 0 or 8 we can work out the 432*0b57cec5SDimitry Andric fraction immediately. */ 433*0b57cec5SDimitry Andric if (digitValue > 8) 434*0b57cec5SDimitry Andric return lfMoreThanHalf; 435*0b57cec5SDimitry Andric else if (digitValue < 8 && digitValue > 0) 436*0b57cec5SDimitry Andric return lfLessThanHalf; 437*0b57cec5SDimitry Andric 438*0b57cec5SDimitry Andric // Otherwise we need to find the first non-zero digit. 439*0b57cec5SDimitry Andric while (p != end && (*p == '0' || *p == '.')) 440*0b57cec5SDimitry Andric p++; 441*0b57cec5SDimitry Andric 442*0b57cec5SDimitry Andric assert(p != end && "Invalid trailing hexadecimal fraction!"); 443*0b57cec5SDimitry Andric 444*0b57cec5SDimitry Andric hexDigit = hexDigitValue(*p); 445*0b57cec5SDimitry Andric 446*0b57cec5SDimitry Andric /* If we ran off the end it is exactly zero or one-half, otherwise 447*0b57cec5SDimitry Andric a little more. */ 448*0b57cec5SDimitry Andric if (hexDigit == -1U) 449*0b57cec5SDimitry Andric return digitValue == 0 ? lfExactlyZero: lfExactlyHalf; 450*0b57cec5SDimitry Andric else 451*0b57cec5SDimitry Andric return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf; 452*0b57cec5SDimitry Andric } 453*0b57cec5SDimitry Andric 454*0b57cec5SDimitry Andric /* Return the fraction lost were a bignum truncated losing the least 455*0b57cec5SDimitry Andric significant BITS bits. */ 456*0b57cec5SDimitry Andric static lostFraction 457*0b57cec5SDimitry Andric lostFractionThroughTruncation(const APFloatBase::integerPart *parts, 458*0b57cec5SDimitry Andric unsigned int partCount, 459*0b57cec5SDimitry Andric unsigned int bits) 460*0b57cec5SDimitry Andric { 461*0b57cec5SDimitry Andric unsigned int lsb; 462*0b57cec5SDimitry Andric 463*0b57cec5SDimitry Andric lsb = APInt::tcLSB(parts, partCount); 464*0b57cec5SDimitry Andric 465*0b57cec5SDimitry Andric /* Note this is guaranteed true if bits == 0, or LSB == -1U. */ 466*0b57cec5SDimitry Andric if (bits <= lsb) 467*0b57cec5SDimitry Andric return lfExactlyZero; 468*0b57cec5SDimitry Andric if (bits == lsb + 1) 469*0b57cec5SDimitry Andric return lfExactlyHalf; 470*0b57cec5SDimitry Andric if (bits <= partCount * APFloatBase::integerPartWidth && 471*0b57cec5SDimitry Andric APInt::tcExtractBit(parts, bits - 1)) 472*0b57cec5SDimitry Andric return lfMoreThanHalf; 473*0b57cec5SDimitry Andric 474*0b57cec5SDimitry Andric return lfLessThanHalf; 475*0b57cec5SDimitry Andric } 476*0b57cec5SDimitry Andric 477*0b57cec5SDimitry Andric /* Shift DST right BITS bits noting lost fraction. */ 478*0b57cec5SDimitry Andric static lostFraction 479*0b57cec5SDimitry Andric shiftRight(APFloatBase::integerPart *dst, unsigned int parts, unsigned int bits) 480*0b57cec5SDimitry Andric { 481*0b57cec5SDimitry Andric lostFraction lost_fraction; 482*0b57cec5SDimitry Andric 483*0b57cec5SDimitry Andric lost_fraction = lostFractionThroughTruncation(dst, parts, bits); 484*0b57cec5SDimitry Andric 485*0b57cec5SDimitry Andric APInt::tcShiftRight(dst, parts, bits); 486*0b57cec5SDimitry Andric 487*0b57cec5SDimitry Andric return lost_fraction; 488*0b57cec5SDimitry Andric } 489*0b57cec5SDimitry Andric 490*0b57cec5SDimitry Andric /* Combine the effect of two lost fractions. */ 491*0b57cec5SDimitry Andric static lostFraction 492*0b57cec5SDimitry Andric combineLostFractions(lostFraction moreSignificant, 493*0b57cec5SDimitry Andric lostFraction lessSignificant) 494*0b57cec5SDimitry Andric { 495*0b57cec5SDimitry Andric if (lessSignificant != lfExactlyZero) { 496*0b57cec5SDimitry Andric if (moreSignificant == lfExactlyZero) 497*0b57cec5SDimitry Andric moreSignificant = lfLessThanHalf; 498*0b57cec5SDimitry Andric else if (moreSignificant == lfExactlyHalf) 499*0b57cec5SDimitry Andric moreSignificant = lfMoreThanHalf; 500*0b57cec5SDimitry Andric } 501*0b57cec5SDimitry Andric 502*0b57cec5SDimitry Andric return moreSignificant; 503*0b57cec5SDimitry Andric } 504*0b57cec5SDimitry Andric 505*0b57cec5SDimitry Andric /* The error from the true value, in half-ulps, on multiplying two 506*0b57cec5SDimitry Andric floating point numbers, which differ from the value they 507*0b57cec5SDimitry Andric approximate by at most HUE1 and HUE2 half-ulps, is strictly less 508*0b57cec5SDimitry Andric than the returned value. 509*0b57cec5SDimitry Andric 510*0b57cec5SDimitry Andric See "How to Read Floating Point Numbers Accurately" by William D 511*0b57cec5SDimitry Andric Clinger. */ 512*0b57cec5SDimitry Andric static unsigned int 513*0b57cec5SDimitry Andric HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2) 514*0b57cec5SDimitry Andric { 515*0b57cec5SDimitry Andric assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8)); 516*0b57cec5SDimitry Andric 517*0b57cec5SDimitry Andric if (HUerr1 + HUerr2 == 0) 518*0b57cec5SDimitry Andric return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */ 519*0b57cec5SDimitry Andric else 520*0b57cec5SDimitry Andric return inexactMultiply + 2 * (HUerr1 + HUerr2); 521*0b57cec5SDimitry Andric } 522*0b57cec5SDimitry Andric 523*0b57cec5SDimitry Andric /* The number of ulps from the boundary (zero, or half if ISNEAREST) 524*0b57cec5SDimitry Andric when the least significant BITS are truncated. BITS cannot be 525*0b57cec5SDimitry Andric zero. */ 526*0b57cec5SDimitry Andric static APFloatBase::integerPart 527*0b57cec5SDimitry Andric ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits, 528*0b57cec5SDimitry Andric bool isNearest) { 529*0b57cec5SDimitry Andric unsigned int count, partBits; 530*0b57cec5SDimitry Andric APFloatBase::integerPart part, boundary; 531*0b57cec5SDimitry Andric 532*0b57cec5SDimitry Andric assert(bits != 0); 533*0b57cec5SDimitry Andric 534*0b57cec5SDimitry Andric bits--; 535*0b57cec5SDimitry Andric count = bits / APFloatBase::integerPartWidth; 536*0b57cec5SDimitry Andric partBits = bits % APFloatBase::integerPartWidth + 1; 537*0b57cec5SDimitry Andric 538*0b57cec5SDimitry Andric part = parts[count] & (~(APFloatBase::integerPart) 0 >> (APFloatBase::integerPartWidth - partBits)); 539*0b57cec5SDimitry Andric 540*0b57cec5SDimitry Andric if (isNearest) 541*0b57cec5SDimitry Andric boundary = (APFloatBase::integerPart) 1 << (partBits - 1); 542*0b57cec5SDimitry Andric else 543*0b57cec5SDimitry Andric boundary = 0; 544*0b57cec5SDimitry Andric 545*0b57cec5SDimitry Andric if (count == 0) { 546*0b57cec5SDimitry Andric if (part - boundary <= boundary - part) 547*0b57cec5SDimitry Andric return part - boundary; 548*0b57cec5SDimitry Andric else 549*0b57cec5SDimitry Andric return boundary - part; 550*0b57cec5SDimitry Andric } 551*0b57cec5SDimitry Andric 552*0b57cec5SDimitry Andric if (part == boundary) { 553*0b57cec5SDimitry Andric while (--count) 554*0b57cec5SDimitry Andric if (parts[count]) 555*0b57cec5SDimitry Andric return ~(APFloatBase::integerPart) 0; /* A lot. */ 556*0b57cec5SDimitry Andric 557*0b57cec5SDimitry Andric return parts[0]; 558*0b57cec5SDimitry Andric } else if (part == boundary - 1) { 559*0b57cec5SDimitry Andric while (--count) 560*0b57cec5SDimitry Andric if (~parts[count]) 561*0b57cec5SDimitry Andric return ~(APFloatBase::integerPart) 0; /* A lot. */ 562*0b57cec5SDimitry Andric 563*0b57cec5SDimitry Andric return -parts[0]; 564*0b57cec5SDimitry Andric } 565*0b57cec5SDimitry Andric 566*0b57cec5SDimitry Andric return ~(APFloatBase::integerPart) 0; /* A lot. */ 567*0b57cec5SDimitry Andric } 568*0b57cec5SDimitry Andric 569*0b57cec5SDimitry Andric /* Place pow(5, power) in DST, and return the number of parts used. 570*0b57cec5SDimitry Andric DST must be at least one part larger than size of the answer. */ 571*0b57cec5SDimitry Andric static unsigned int 572*0b57cec5SDimitry Andric powerOf5(APFloatBase::integerPart *dst, unsigned int power) { 573*0b57cec5SDimitry Andric static const APFloatBase::integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125, 15625, 78125 }; 574*0b57cec5SDimitry Andric APFloatBase::integerPart pow5s[maxPowerOfFiveParts * 2 + 5]; 575*0b57cec5SDimitry Andric pow5s[0] = 78125 * 5; 576*0b57cec5SDimitry Andric 577*0b57cec5SDimitry Andric unsigned int partsCount[16] = { 1 }; 578*0b57cec5SDimitry Andric APFloatBase::integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5; 579*0b57cec5SDimitry Andric unsigned int result; 580*0b57cec5SDimitry Andric assert(power <= maxExponent); 581*0b57cec5SDimitry Andric 582*0b57cec5SDimitry Andric p1 = dst; 583*0b57cec5SDimitry Andric p2 = scratch; 584*0b57cec5SDimitry Andric 585*0b57cec5SDimitry Andric *p1 = firstEightPowers[power & 7]; 586*0b57cec5SDimitry Andric power >>= 3; 587*0b57cec5SDimitry Andric 588*0b57cec5SDimitry Andric result = 1; 589*0b57cec5SDimitry Andric pow5 = pow5s; 590*0b57cec5SDimitry Andric 591*0b57cec5SDimitry Andric for (unsigned int n = 0; power; power >>= 1, n++) { 592*0b57cec5SDimitry Andric unsigned int pc; 593*0b57cec5SDimitry Andric 594*0b57cec5SDimitry Andric pc = partsCount[n]; 595*0b57cec5SDimitry Andric 596*0b57cec5SDimitry Andric /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */ 597*0b57cec5SDimitry Andric if (pc == 0) { 598*0b57cec5SDimitry Andric pc = partsCount[n - 1]; 599*0b57cec5SDimitry Andric APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc); 600*0b57cec5SDimitry Andric pc *= 2; 601*0b57cec5SDimitry Andric if (pow5[pc - 1] == 0) 602*0b57cec5SDimitry Andric pc--; 603*0b57cec5SDimitry Andric partsCount[n] = pc; 604*0b57cec5SDimitry Andric } 605*0b57cec5SDimitry Andric 606*0b57cec5SDimitry Andric if (power & 1) { 607*0b57cec5SDimitry Andric APFloatBase::integerPart *tmp; 608*0b57cec5SDimitry Andric 609*0b57cec5SDimitry Andric APInt::tcFullMultiply(p2, p1, pow5, result, pc); 610*0b57cec5SDimitry Andric result += pc; 611*0b57cec5SDimitry Andric if (p2[result - 1] == 0) 612*0b57cec5SDimitry Andric result--; 613*0b57cec5SDimitry Andric 614*0b57cec5SDimitry Andric /* Now result is in p1 with partsCount parts and p2 is scratch 615*0b57cec5SDimitry Andric space. */ 616*0b57cec5SDimitry Andric tmp = p1; 617*0b57cec5SDimitry Andric p1 = p2; 618*0b57cec5SDimitry Andric p2 = tmp; 619*0b57cec5SDimitry Andric } 620*0b57cec5SDimitry Andric 621*0b57cec5SDimitry Andric pow5 += pc; 622*0b57cec5SDimitry Andric } 623*0b57cec5SDimitry Andric 624*0b57cec5SDimitry Andric if (p1 != dst) 625*0b57cec5SDimitry Andric APInt::tcAssign(dst, p1, result); 626*0b57cec5SDimitry Andric 627*0b57cec5SDimitry Andric return result; 628*0b57cec5SDimitry Andric } 629*0b57cec5SDimitry Andric 630*0b57cec5SDimitry Andric /* Zero at the end to avoid modular arithmetic when adding one; used 631*0b57cec5SDimitry Andric when rounding up during hexadecimal output. */ 632*0b57cec5SDimitry Andric static const char hexDigitsLower[] = "0123456789abcdef0"; 633*0b57cec5SDimitry Andric static const char hexDigitsUpper[] = "0123456789ABCDEF0"; 634*0b57cec5SDimitry Andric static const char infinityL[] = "infinity"; 635*0b57cec5SDimitry Andric static const char infinityU[] = "INFINITY"; 636*0b57cec5SDimitry Andric static const char NaNL[] = "nan"; 637*0b57cec5SDimitry Andric static const char NaNU[] = "NAN"; 638*0b57cec5SDimitry Andric 639*0b57cec5SDimitry Andric /* Write out an integerPart in hexadecimal, starting with the most 640*0b57cec5SDimitry Andric significant nibble. Write out exactly COUNT hexdigits, return 641*0b57cec5SDimitry Andric COUNT. */ 642*0b57cec5SDimitry Andric static unsigned int 643*0b57cec5SDimitry Andric partAsHex (char *dst, APFloatBase::integerPart part, unsigned int count, 644*0b57cec5SDimitry Andric const char *hexDigitChars) 645*0b57cec5SDimitry Andric { 646*0b57cec5SDimitry Andric unsigned int result = count; 647*0b57cec5SDimitry Andric 648*0b57cec5SDimitry Andric assert(count != 0 && count <= APFloatBase::integerPartWidth / 4); 649*0b57cec5SDimitry Andric 650*0b57cec5SDimitry Andric part >>= (APFloatBase::integerPartWidth - 4 * count); 651*0b57cec5SDimitry Andric while (count--) { 652*0b57cec5SDimitry Andric dst[count] = hexDigitChars[part & 0xf]; 653*0b57cec5SDimitry Andric part >>= 4; 654*0b57cec5SDimitry Andric } 655*0b57cec5SDimitry Andric 656*0b57cec5SDimitry Andric return result; 657*0b57cec5SDimitry Andric } 658*0b57cec5SDimitry Andric 659*0b57cec5SDimitry Andric /* Write out an unsigned decimal integer. */ 660*0b57cec5SDimitry Andric static char * 661*0b57cec5SDimitry Andric writeUnsignedDecimal (char *dst, unsigned int n) 662*0b57cec5SDimitry Andric { 663*0b57cec5SDimitry Andric char buff[40], *p; 664*0b57cec5SDimitry Andric 665*0b57cec5SDimitry Andric p = buff; 666*0b57cec5SDimitry Andric do 667*0b57cec5SDimitry Andric *p++ = '0' + n % 10; 668*0b57cec5SDimitry Andric while (n /= 10); 669*0b57cec5SDimitry Andric 670*0b57cec5SDimitry Andric do 671*0b57cec5SDimitry Andric *dst++ = *--p; 672*0b57cec5SDimitry Andric while (p != buff); 673*0b57cec5SDimitry Andric 674*0b57cec5SDimitry Andric return dst; 675*0b57cec5SDimitry Andric } 676*0b57cec5SDimitry Andric 677*0b57cec5SDimitry Andric /* Write out a signed decimal integer. */ 678*0b57cec5SDimitry Andric static char * 679*0b57cec5SDimitry Andric writeSignedDecimal (char *dst, int value) 680*0b57cec5SDimitry Andric { 681*0b57cec5SDimitry Andric if (value < 0) { 682*0b57cec5SDimitry Andric *dst++ = '-'; 683*0b57cec5SDimitry Andric dst = writeUnsignedDecimal(dst, -(unsigned) value); 684*0b57cec5SDimitry Andric } else 685*0b57cec5SDimitry Andric dst = writeUnsignedDecimal(dst, value); 686*0b57cec5SDimitry Andric 687*0b57cec5SDimitry Andric return dst; 688*0b57cec5SDimitry Andric } 689*0b57cec5SDimitry Andric 690*0b57cec5SDimitry Andric namespace detail { 691*0b57cec5SDimitry Andric /* Constructors. */ 692*0b57cec5SDimitry Andric void IEEEFloat::initialize(const fltSemantics *ourSemantics) { 693*0b57cec5SDimitry Andric unsigned int count; 694*0b57cec5SDimitry Andric 695*0b57cec5SDimitry Andric semantics = ourSemantics; 696*0b57cec5SDimitry Andric count = partCount(); 697*0b57cec5SDimitry Andric if (count > 1) 698*0b57cec5SDimitry Andric significand.parts = new integerPart[count]; 699*0b57cec5SDimitry Andric } 700*0b57cec5SDimitry Andric 701*0b57cec5SDimitry Andric void IEEEFloat::freeSignificand() { 702*0b57cec5SDimitry Andric if (needsCleanup()) 703*0b57cec5SDimitry Andric delete [] significand.parts; 704*0b57cec5SDimitry Andric } 705*0b57cec5SDimitry Andric 706*0b57cec5SDimitry Andric void IEEEFloat::assign(const IEEEFloat &rhs) { 707*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 708*0b57cec5SDimitry Andric 709*0b57cec5SDimitry Andric sign = rhs.sign; 710*0b57cec5SDimitry Andric category = rhs.category; 711*0b57cec5SDimitry Andric exponent = rhs.exponent; 712*0b57cec5SDimitry Andric if (isFiniteNonZero() || category == fcNaN) 713*0b57cec5SDimitry Andric copySignificand(rhs); 714*0b57cec5SDimitry Andric } 715*0b57cec5SDimitry Andric 716*0b57cec5SDimitry Andric void IEEEFloat::copySignificand(const IEEEFloat &rhs) { 717*0b57cec5SDimitry Andric assert(isFiniteNonZero() || category == fcNaN); 718*0b57cec5SDimitry Andric assert(rhs.partCount() >= partCount()); 719*0b57cec5SDimitry Andric 720*0b57cec5SDimitry Andric APInt::tcAssign(significandParts(), rhs.significandParts(), 721*0b57cec5SDimitry Andric partCount()); 722*0b57cec5SDimitry Andric } 723*0b57cec5SDimitry Andric 724*0b57cec5SDimitry Andric /* Make this number a NaN, with an arbitrary but deterministic value 725*0b57cec5SDimitry Andric for the significand. If double or longer, this is a signalling NaN, 726*0b57cec5SDimitry Andric which may not be ideal. If float, this is QNaN(0). */ 727*0b57cec5SDimitry Andric void IEEEFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill) { 728*0b57cec5SDimitry Andric category = fcNaN; 729*0b57cec5SDimitry Andric sign = Negative; 730*0b57cec5SDimitry Andric 731*0b57cec5SDimitry Andric integerPart *significand = significandParts(); 732*0b57cec5SDimitry Andric unsigned numParts = partCount(); 733*0b57cec5SDimitry Andric 734*0b57cec5SDimitry Andric // Set the significand bits to the fill. 735*0b57cec5SDimitry Andric if (!fill || fill->getNumWords() < numParts) 736*0b57cec5SDimitry Andric APInt::tcSet(significand, 0, numParts); 737*0b57cec5SDimitry Andric if (fill) { 738*0b57cec5SDimitry Andric APInt::tcAssign(significand, fill->getRawData(), 739*0b57cec5SDimitry Andric std::min(fill->getNumWords(), numParts)); 740*0b57cec5SDimitry Andric 741*0b57cec5SDimitry Andric // Zero out the excess bits of the significand. 742*0b57cec5SDimitry Andric unsigned bitsToPreserve = semantics->precision - 1; 743*0b57cec5SDimitry Andric unsigned part = bitsToPreserve / 64; 744*0b57cec5SDimitry Andric bitsToPreserve %= 64; 745*0b57cec5SDimitry Andric significand[part] &= ((1ULL << bitsToPreserve) - 1); 746*0b57cec5SDimitry Andric for (part++; part != numParts; ++part) 747*0b57cec5SDimitry Andric significand[part] = 0; 748*0b57cec5SDimitry Andric } 749*0b57cec5SDimitry Andric 750*0b57cec5SDimitry Andric unsigned QNaNBit = semantics->precision - 2; 751*0b57cec5SDimitry Andric 752*0b57cec5SDimitry Andric if (SNaN) { 753*0b57cec5SDimitry Andric // We always have to clear the QNaN bit to make it an SNaN. 754*0b57cec5SDimitry Andric APInt::tcClearBit(significand, QNaNBit); 755*0b57cec5SDimitry Andric 756*0b57cec5SDimitry Andric // If there are no bits set in the payload, we have to set 757*0b57cec5SDimitry Andric // *something* to make it a NaN instead of an infinity; 758*0b57cec5SDimitry Andric // conventionally, this is the next bit down from the QNaN bit. 759*0b57cec5SDimitry Andric if (APInt::tcIsZero(significand, numParts)) 760*0b57cec5SDimitry Andric APInt::tcSetBit(significand, QNaNBit - 1); 761*0b57cec5SDimitry Andric } else { 762*0b57cec5SDimitry Andric // We always have to set the QNaN bit to make it a QNaN. 763*0b57cec5SDimitry Andric APInt::tcSetBit(significand, QNaNBit); 764*0b57cec5SDimitry Andric } 765*0b57cec5SDimitry Andric 766*0b57cec5SDimitry Andric // For x87 extended precision, we want to make a NaN, not a 767*0b57cec5SDimitry Andric // pseudo-NaN. Maybe we should expose the ability to make 768*0b57cec5SDimitry Andric // pseudo-NaNs? 769*0b57cec5SDimitry Andric if (semantics == &semX87DoubleExtended) 770*0b57cec5SDimitry Andric APInt::tcSetBit(significand, QNaNBit + 1); 771*0b57cec5SDimitry Andric } 772*0b57cec5SDimitry Andric 773*0b57cec5SDimitry Andric IEEEFloat &IEEEFloat::operator=(const IEEEFloat &rhs) { 774*0b57cec5SDimitry Andric if (this != &rhs) { 775*0b57cec5SDimitry Andric if (semantics != rhs.semantics) { 776*0b57cec5SDimitry Andric freeSignificand(); 777*0b57cec5SDimitry Andric initialize(rhs.semantics); 778*0b57cec5SDimitry Andric } 779*0b57cec5SDimitry Andric assign(rhs); 780*0b57cec5SDimitry Andric } 781*0b57cec5SDimitry Andric 782*0b57cec5SDimitry Andric return *this; 783*0b57cec5SDimitry Andric } 784*0b57cec5SDimitry Andric 785*0b57cec5SDimitry Andric IEEEFloat &IEEEFloat::operator=(IEEEFloat &&rhs) { 786*0b57cec5SDimitry Andric freeSignificand(); 787*0b57cec5SDimitry Andric 788*0b57cec5SDimitry Andric semantics = rhs.semantics; 789*0b57cec5SDimitry Andric significand = rhs.significand; 790*0b57cec5SDimitry Andric exponent = rhs.exponent; 791*0b57cec5SDimitry Andric category = rhs.category; 792*0b57cec5SDimitry Andric sign = rhs.sign; 793*0b57cec5SDimitry Andric 794*0b57cec5SDimitry Andric rhs.semantics = &semBogus; 795*0b57cec5SDimitry Andric return *this; 796*0b57cec5SDimitry Andric } 797*0b57cec5SDimitry Andric 798*0b57cec5SDimitry Andric bool IEEEFloat::isDenormal() const { 799*0b57cec5SDimitry Andric return isFiniteNonZero() && (exponent == semantics->minExponent) && 800*0b57cec5SDimitry Andric (APInt::tcExtractBit(significandParts(), 801*0b57cec5SDimitry Andric semantics->precision - 1) == 0); 802*0b57cec5SDimitry Andric } 803*0b57cec5SDimitry Andric 804*0b57cec5SDimitry Andric bool IEEEFloat::isSmallest() const { 805*0b57cec5SDimitry Andric // The smallest number by magnitude in our format will be the smallest 806*0b57cec5SDimitry Andric // denormal, i.e. the floating point number with exponent being minimum 807*0b57cec5SDimitry Andric // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0). 808*0b57cec5SDimitry Andric return isFiniteNonZero() && exponent == semantics->minExponent && 809*0b57cec5SDimitry Andric significandMSB() == 0; 810*0b57cec5SDimitry Andric } 811*0b57cec5SDimitry Andric 812*0b57cec5SDimitry Andric bool IEEEFloat::isSignificandAllOnes() const { 813*0b57cec5SDimitry Andric // Test if the significand excluding the integral bit is all ones. This allows 814*0b57cec5SDimitry Andric // us to test for binade boundaries. 815*0b57cec5SDimitry Andric const integerPart *Parts = significandParts(); 816*0b57cec5SDimitry Andric const unsigned PartCount = partCount(); 817*0b57cec5SDimitry Andric for (unsigned i = 0; i < PartCount - 1; i++) 818*0b57cec5SDimitry Andric if (~Parts[i]) 819*0b57cec5SDimitry Andric return false; 820*0b57cec5SDimitry Andric 821*0b57cec5SDimitry Andric // Set the unused high bits to all ones when we compare. 822*0b57cec5SDimitry Andric const unsigned NumHighBits = 823*0b57cec5SDimitry Andric PartCount*integerPartWidth - semantics->precision + 1; 824*0b57cec5SDimitry Andric assert(NumHighBits <= integerPartWidth && "Can not have more high bits to " 825*0b57cec5SDimitry Andric "fill than integerPartWidth"); 826*0b57cec5SDimitry Andric const integerPart HighBitFill = 827*0b57cec5SDimitry Andric ~integerPart(0) << (integerPartWidth - NumHighBits); 828*0b57cec5SDimitry Andric if (~(Parts[PartCount - 1] | HighBitFill)) 829*0b57cec5SDimitry Andric return false; 830*0b57cec5SDimitry Andric 831*0b57cec5SDimitry Andric return true; 832*0b57cec5SDimitry Andric } 833*0b57cec5SDimitry Andric 834*0b57cec5SDimitry Andric bool IEEEFloat::isSignificandAllZeros() const { 835*0b57cec5SDimitry Andric // Test if the significand excluding the integral bit is all zeros. This 836*0b57cec5SDimitry Andric // allows us to test for binade boundaries. 837*0b57cec5SDimitry Andric const integerPart *Parts = significandParts(); 838*0b57cec5SDimitry Andric const unsigned PartCount = partCount(); 839*0b57cec5SDimitry Andric 840*0b57cec5SDimitry Andric for (unsigned i = 0; i < PartCount - 1; i++) 841*0b57cec5SDimitry Andric if (Parts[i]) 842*0b57cec5SDimitry Andric return false; 843*0b57cec5SDimitry Andric 844*0b57cec5SDimitry Andric const unsigned NumHighBits = 845*0b57cec5SDimitry Andric PartCount*integerPartWidth - semantics->precision + 1; 846*0b57cec5SDimitry Andric assert(NumHighBits <= integerPartWidth && "Can not have more high bits to " 847*0b57cec5SDimitry Andric "clear than integerPartWidth"); 848*0b57cec5SDimitry Andric const integerPart HighBitMask = ~integerPart(0) >> NumHighBits; 849*0b57cec5SDimitry Andric 850*0b57cec5SDimitry Andric if (Parts[PartCount - 1] & HighBitMask) 851*0b57cec5SDimitry Andric return false; 852*0b57cec5SDimitry Andric 853*0b57cec5SDimitry Andric return true; 854*0b57cec5SDimitry Andric } 855*0b57cec5SDimitry Andric 856*0b57cec5SDimitry Andric bool IEEEFloat::isLargest() const { 857*0b57cec5SDimitry Andric // The largest number by magnitude in our format will be the floating point 858*0b57cec5SDimitry Andric // number with maximum exponent and with significand that is all ones. 859*0b57cec5SDimitry Andric return isFiniteNonZero() && exponent == semantics->maxExponent 860*0b57cec5SDimitry Andric && isSignificandAllOnes(); 861*0b57cec5SDimitry Andric } 862*0b57cec5SDimitry Andric 863*0b57cec5SDimitry Andric bool IEEEFloat::isInteger() const { 864*0b57cec5SDimitry Andric // This could be made more efficient; I'm going for obviously correct. 865*0b57cec5SDimitry Andric if (!isFinite()) return false; 866*0b57cec5SDimitry Andric IEEEFloat truncated = *this; 867*0b57cec5SDimitry Andric truncated.roundToIntegral(rmTowardZero); 868*0b57cec5SDimitry Andric return compare(truncated) == cmpEqual; 869*0b57cec5SDimitry Andric } 870*0b57cec5SDimitry Andric 871*0b57cec5SDimitry Andric bool IEEEFloat::bitwiseIsEqual(const IEEEFloat &rhs) const { 872*0b57cec5SDimitry Andric if (this == &rhs) 873*0b57cec5SDimitry Andric return true; 874*0b57cec5SDimitry Andric if (semantics != rhs.semantics || 875*0b57cec5SDimitry Andric category != rhs.category || 876*0b57cec5SDimitry Andric sign != rhs.sign) 877*0b57cec5SDimitry Andric return false; 878*0b57cec5SDimitry Andric if (category==fcZero || category==fcInfinity) 879*0b57cec5SDimitry Andric return true; 880*0b57cec5SDimitry Andric 881*0b57cec5SDimitry Andric if (isFiniteNonZero() && exponent != rhs.exponent) 882*0b57cec5SDimitry Andric return false; 883*0b57cec5SDimitry Andric 884*0b57cec5SDimitry Andric return std::equal(significandParts(), significandParts() + partCount(), 885*0b57cec5SDimitry Andric rhs.significandParts()); 886*0b57cec5SDimitry Andric } 887*0b57cec5SDimitry Andric 888*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, integerPart value) { 889*0b57cec5SDimitry Andric initialize(&ourSemantics); 890*0b57cec5SDimitry Andric sign = 0; 891*0b57cec5SDimitry Andric category = fcNormal; 892*0b57cec5SDimitry Andric zeroSignificand(); 893*0b57cec5SDimitry Andric exponent = ourSemantics.precision - 1; 894*0b57cec5SDimitry Andric significandParts()[0] = value; 895*0b57cec5SDimitry Andric normalize(rmNearestTiesToEven, lfExactlyZero); 896*0b57cec5SDimitry Andric } 897*0b57cec5SDimitry Andric 898*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics) { 899*0b57cec5SDimitry Andric initialize(&ourSemantics); 900*0b57cec5SDimitry Andric category = fcZero; 901*0b57cec5SDimitry Andric sign = false; 902*0b57cec5SDimitry Andric } 903*0b57cec5SDimitry Andric 904*0b57cec5SDimitry Andric // Delegate to the previous constructor, because later copy constructor may 905*0b57cec5SDimitry Andric // actually inspects category, which can't be garbage. 906*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, uninitializedTag tag) 907*0b57cec5SDimitry Andric : IEEEFloat(ourSemantics) {} 908*0b57cec5SDimitry Andric 909*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const IEEEFloat &rhs) { 910*0b57cec5SDimitry Andric initialize(rhs.semantics); 911*0b57cec5SDimitry Andric assign(rhs); 912*0b57cec5SDimitry Andric } 913*0b57cec5SDimitry Andric 914*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(IEEEFloat &&rhs) : semantics(&semBogus) { 915*0b57cec5SDimitry Andric *this = std::move(rhs); 916*0b57cec5SDimitry Andric } 917*0b57cec5SDimitry Andric 918*0b57cec5SDimitry Andric IEEEFloat::~IEEEFloat() { freeSignificand(); } 919*0b57cec5SDimitry Andric 920*0b57cec5SDimitry Andric unsigned int IEEEFloat::partCount() const { 921*0b57cec5SDimitry Andric return partCountForBits(semantics->precision + 1); 922*0b57cec5SDimitry Andric } 923*0b57cec5SDimitry Andric 924*0b57cec5SDimitry Andric const IEEEFloat::integerPart *IEEEFloat::significandParts() const { 925*0b57cec5SDimitry Andric return const_cast<IEEEFloat *>(this)->significandParts(); 926*0b57cec5SDimitry Andric } 927*0b57cec5SDimitry Andric 928*0b57cec5SDimitry Andric IEEEFloat::integerPart *IEEEFloat::significandParts() { 929*0b57cec5SDimitry Andric if (partCount() > 1) 930*0b57cec5SDimitry Andric return significand.parts; 931*0b57cec5SDimitry Andric else 932*0b57cec5SDimitry Andric return &significand.part; 933*0b57cec5SDimitry Andric } 934*0b57cec5SDimitry Andric 935*0b57cec5SDimitry Andric void IEEEFloat::zeroSignificand() { 936*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 937*0b57cec5SDimitry Andric } 938*0b57cec5SDimitry Andric 939*0b57cec5SDimitry Andric /* Increment an fcNormal floating point number's significand. */ 940*0b57cec5SDimitry Andric void IEEEFloat::incrementSignificand() { 941*0b57cec5SDimitry Andric integerPart carry; 942*0b57cec5SDimitry Andric 943*0b57cec5SDimitry Andric carry = APInt::tcIncrement(significandParts(), partCount()); 944*0b57cec5SDimitry Andric 945*0b57cec5SDimitry Andric /* Our callers should never cause us to overflow. */ 946*0b57cec5SDimitry Andric assert(carry == 0); 947*0b57cec5SDimitry Andric (void)carry; 948*0b57cec5SDimitry Andric } 949*0b57cec5SDimitry Andric 950*0b57cec5SDimitry Andric /* Add the significand of the RHS. Returns the carry flag. */ 951*0b57cec5SDimitry Andric IEEEFloat::integerPart IEEEFloat::addSignificand(const IEEEFloat &rhs) { 952*0b57cec5SDimitry Andric integerPart *parts; 953*0b57cec5SDimitry Andric 954*0b57cec5SDimitry Andric parts = significandParts(); 955*0b57cec5SDimitry Andric 956*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 957*0b57cec5SDimitry Andric assert(exponent == rhs.exponent); 958*0b57cec5SDimitry Andric 959*0b57cec5SDimitry Andric return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount()); 960*0b57cec5SDimitry Andric } 961*0b57cec5SDimitry Andric 962*0b57cec5SDimitry Andric /* Subtract the significand of the RHS with a borrow flag. Returns 963*0b57cec5SDimitry Andric the borrow flag. */ 964*0b57cec5SDimitry Andric IEEEFloat::integerPart IEEEFloat::subtractSignificand(const IEEEFloat &rhs, 965*0b57cec5SDimitry Andric integerPart borrow) { 966*0b57cec5SDimitry Andric integerPart *parts; 967*0b57cec5SDimitry Andric 968*0b57cec5SDimitry Andric parts = significandParts(); 969*0b57cec5SDimitry Andric 970*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 971*0b57cec5SDimitry Andric assert(exponent == rhs.exponent); 972*0b57cec5SDimitry Andric 973*0b57cec5SDimitry Andric return APInt::tcSubtract(parts, rhs.significandParts(), borrow, 974*0b57cec5SDimitry Andric partCount()); 975*0b57cec5SDimitry Andric } 976*0b57cec5SDimitry Andric 977*0b57cec5SDimitry Andric /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it 978*0b57cec5SDimitry Andric on to the full-precision result of the multiplication. Returns the 979*0b57cec5SDimitry Andric lost fraction. */ 980*0b57cec5SDimitry Andric lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs, 981*0b57cec5SDimitry Andric const IEEEFloat *addend) { 982*0b57cec5SDimitry Andric unsigned int omsb; // One, not zero, based MSB. 983*0b57cec5SDimitry Andric unsigned int partsCount, newPartsCount, precision; 984*0b57cec5SDimitry Andric integerPart *lhsSignificand; 985*0b57cec5SDimitry Andric integerPart scratch[4]; 986*0b57cec5SDimitry Andric integerPart *fullSignificand; 987*0b57cec5SDimitry Andric lostFraction lost_fraction; 988*0b57cec5SDimitry Andric bool ignored; 989*0b57cec5SDimitry Andric 990*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 991*0b57cec5SDimitry Andric 992*0b57cec5SDimitry Andric precision = semantics->precision; 993*0b57cec5SDimitry Andric 994*0b57cec5SDimitry Andric // Allocate space for twice as many bits as the original significand, plus one 995*0b57cec5SDimitry Andric // extra bit for the addition to overflow into. 996*0b57cec5SDimitry Andric newPartsCount = partCountForBits(precision * 2 + 1); 997*0b57cec5SDimitry Andric 998*0b57cec5SDimitry Andric if (newPartsCount > 4) 999*0b57cec5SDimitry Andric fullSignificand = new integerPart[newPartsCount]; 1000*0b57cec5SDimitry Andric else 1001*0b57cec5SDimitry Andric fullSignificand = scratch; 1002*0b57cec5SDimitry Andric 1003*0b57cec5SDimitry Andric lhsSignificand = significandParts(); 1004*0b57cec5SDimitry Andric partsCount = partCount(); 1005*0b57cec5SDimitry Andric 1006*0b57cec5SDimitry Andric APInt::tcFullMultiply(fullSignificand, lhsSignificand, 1007*0b57cec5SDimitry Andric rhs.significandParts(), partsCount, partsCount); 1008*0b57cec5SDimitry Andric 1009*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 1010*0b57cec5SDimitry Andric omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; 1011*0b57cec5SDimitry Andric exponent += rhs.exponent; 1012*0b57cec5SDimitry Andric 1013*0b57cec5SDimitry Andric // Assume the operands involved in the multiplication are single-precision 1014*0b57cec5SDimitry Andric // FP, and the two multiplicants are: 1015*0b57cec5SDimitry Andric // *this = a23 . a22 ... a0 * 2^e1 1016*0b57cec5SDimitry Andric // rhs = b23 . b22 ... b0 * 2^e2 1017*0b57cec5SDimitry Andric // the result of multiplication is: 1018*0b57cec5SDimitry Andric // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2) 1019*0b57cec5SDimitry Andric // Note that there are three significant bits at the left-hand side of the 1020*0b57cec5SDimitry Andric // radix point: two for the multiplication, and an overflow bit for the 1021*0b57cec5SDimitry Andric // addition (that will always be zero at this point). Move the radix point 1022*0b57cec5SDimitry Andric // toward left by two bits, and adjust exponent accordingly. 1023*0b57cec5SDimitry Andric exponent += 2; 1024*0b57cec5SDimitry Andric 1025*0b57cec5SDimitry Andric if (addend && addend->isNonZero()) { 1026*0b57cec5SDimitry Andric // The intermediate result of the multiplication has "2 * precision" 1027*0b57cec5SDimitry Andric // signicant bit; adjust the addend to be consistent with mul result. 1028*0b57cec5SDimitry Andric // 1029*0b57cec5SDimitry Andric Significand savedSignificand = significand; 1030*0b57cec5SDimitry Andric const fltSemantics *savedSemantics = semantics; 1031*0b57cec5SDimitry Andric fltSemantics extendedSemantics; 1032*0b57cec5SDimitry Andric opStatus status; 1033*0b57cec5SDimitry Andric unsigned int extendedPrecision; 1034*0b57cec5SDimitry Andric 1035*0b57cec5SDimitry Andric // Normalize our MSB to one below the top bit to allow for overflow. 1036*0b57cec5SDimitry Andric extendedPrecision = 2 * precision + 1; 1037*0b57cec5SDimitry Andric if (omsb != extendedPrecision - 1) { 1038*0b57cec5SDimitry Andric assert(extendedPrecision > omsb); 1039*0b57cec5SDimitry Andric APInt::tcShiftLeft(fullSignificand, newPartsCount, 1040*0b57cec5SDimitry Andric (extendedPrecision - 1) - omsb); 1041*0b57cec5SDimitry Andric exponent -= (extendedPrecision - 1) - omsb; 1042*0b57cec5SDimitry Andric } 1043*0b57cec5SDimitry Andric 1044*0b57cec5SDimitry Andric /* Create new semantics. */ 1045*0b57cec5SDimitry Andric extendedSemantics = *semantics; 1046*0b57cec5SDimitry Andric extendedSemantics.precision = extendedPrecision; 1047*0b57cec5SDimitry Andric 1048*0b57cec5SDimitry Andric if (newPartsCount == 1) 1049*0b57cec5SDimitry Andric significand.part = fullSignificand[0]; 1050*0b57cec5SDimitry Andric else 1051*0b57cec5SDimitry Andric significand.parts = fullSignificand; 1052*0b57cec5SDimitry Andric semantics = &extendedSemantics; 1053*0b57cec5SDimitry Andric 1054*0b57cec5SDimitry Andric IEEEFloat extendedAddend(*addend); 1055*0b57cec5SDimitry Andric status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored); 1056*0b57cec5SDimitry Andric assert(status == opOK); 1057*0b57cec5SDimitry Andric (void)status; 1058*0b57cec5SDimitry Andric 1059*0b57cec5SDimitry Andric // Shift the significand of the addend right by one bit. This guarantees 1060*0b57cec5SDimitry Andric // that the high bit of the significand is zero (same as fullSignificand), 1061*0b57cec5SDimitry Andric // so the addition will overflow (if it does overflow at all) into the top bit. 1062*0b57cec5SDimitry Andric lost_fraction = extendedAddend.shiftSignificandRight(1); 1063*0b57cec5SDimitry Andric assert(lost_fraction == lfExactlyZero && 1064*0b57cec5SDimitry Andric "Lost precision while shifting addend for fused-multiply-add."); 1065*0b57cec5SDimitry Andric 1066*0b57cec5SDimitry Andric lost_fraction = addOrSubtractSignificand(extendedAddend, false); 1067*0b57cec5SDimitry Andric 1068*0b57cec5SDimitry Andric /* Restore our state. */ 1069*0b57cec5SDimitry Andric if (newPartsCount == 1) 1070*0b57cec5SDimitry Andric fullSignificand[0] = significand.part; 1071*0b57cec5SDimitry Andric significand = savedSignificand; 1072*0b57cec5SDimitry Andric semantics = savedSemantics; 1073*0b57cec5SDimitry Andric 1074*0b57cec5SDimitry Andric omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; 1075*0b57cec5SDimitry Andric } 1076*0b57cec5SDimitry Andric 1077*0b57cec5SDimitry Andric // Convert the result having "2 * precision" significant-bits back to the one 1078*0b57cec5SDimitry Andric // having "precision" significant-bits. First, move the radix point from 1079*0b57cec5SDimitry Andric // poision "2*precision - 1" to "precision - 1". The exponent need to be 1080*0b57cec5SDimitry Andric // adjusted by "2*precision - 1" - "precision - 1" = "precision". 1081*0b57cec5SDimitry Andric exponent -= precision + 1; 1082*0b57cec5SDimitry Andric 1083*0b57cec5SDimitry Andric // In case MSB resides at the left-hand side of radix point, shift the 1084*0b57cec5SDimitry Andric // mantissa right by some amount to make sure the MSB reside right before 1085*0b57cec5SDimitry Andric // the radix point (i.e. "MSB . rest-significant-bits"). 1086*0b57cec5SDimitry Andric // 1087*0b57cec5SDimitry Andric // Note that the result is not normalized when "omsb < precision". So, the 1088*0b57cec5SDimitry Andric // caller needs to call IEEEFloat::normalize() if normalized value is 1089*0b57cec5SDimitry Andric // expected. 1090*0b57cec5SDimitry Andric if (omsb > precision) { 1091*0b57cec5SDimitry Andric unsigned int bits, significantParts; 1092*0b57cec5SDimitry Andric lostFraction lf; 1093*0b57cec5SDimitry Andric 1094*0b57cec5SDimitry Andric bits = omsb - precision; 1095*0b57cec5SDimitry Andric significantParts = partCountForBits(omsb); 1096*0b57cec5SDimitry Andric lf = shiftRight(fullSignificand, significantParts, bits); 1097*0b57cec5SDimitry Andric lost_fraction = combineLostFractions(lf, lost_fraction); 1098*0b57cec5SDimitry Andric exponent += bits; 1099*0b57cec5SDimitry Andric } 1100*0b57cec5SDimitry Andric 1101*0b57cec5SDimitry Andric APInt::tcAssign(lhsSignificand, fullSignificand, partsCount); 1102*0b57cec5SDimitry Andric 1103*0b57cec5SDimitry Andric if (newPartsCount > 4) 1104*0b57cec5SDimitry Andric delete [] fullSignificand; 1105*0b57cec5SDimitry Andric 1106*0b57cec5SDimitry Andric return lost_fraction; 1107*0b57cec5SDimitry Andric } 1108*0b57cec5SDimitry Andric 1109*0b57cec5SDimitry Andric /* Multiply the significands of LHS and RHS to DST. */ 1110*0b57cec5SDimitry Andric lostFraction IEEEFloat::divideSignificand(const IEEEFloat &rhs) { 1111*0b57cec5SDimitry Andric unsigned int bit, i, partsCount; 1112*0b57cec5SDimitry Andric const integerPart *rhsSignificand; 1113*0b57cec5SDimitry Andric integerPart *lhsSignificand, *dividend, *divisor; 1114*0b57cec5SDimitry Andric integerPart scratch[4]; 1115*0b57cec5SDimitry Andric lostFraction lost_fraction; 1116*0b57cec5SDimitry Andric 1117*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 1118*0b57cec5SDimitry Andric 1119*0b57cec5SDimitry Andric lhsSignificand = significandParts(); 1120*0b57cec5SDimitry Andric rhsSignificand = rhs.significandParts(); 1121*0b57cec5SDimitry Andric partsCount = partCount(); 1122*0b57cec5SDimitry Andric 1123*0b57cec5SDimitry Andric if (partsCount > 2) 1124*0b57cec5SDimitry Andric dividend = new integerPart[partsCount * 2]; 1125*0b57cec5SDimitry Andric else 1126*0b57cec5SDimitry Andric dividend = scratch; 1127*0b57cec5SDimitry Andric 1128*0b57cec5SDimitry Andric divisor = dividend + partsCount; 1129*0b57cec5SDimitry Andric 1130*0b57cec5SDimitry Andric /* Copy the dividend and divisor as they will be modified in-place. */ 1131*0b57cec5SDimitry Andric for (i = 0; i < partsCount; i++) { 1132*0b57cec5SDimitry Andric dividend[i] = lhsSignificand[i]; 1133*0b57cec5SDimitry Andric divisor[i] = rhsSignificand[i]; 1134*0b57cec5SDimitry Andric lhsSignificand[i] = 0; 1135*0b57cec5SDimitry Andric } 1136*0b57cec5SDimitry Andric 1137*0b57cec5SDimitry Andric exponent -= rhs.exponent; 1138*0b57cec5SDimitry Andric 1139*0b57cec5SDimitry Andric unsigned int precision = semantics->precision; 1140*0b57cec5SDimitry Andric 1141*0b57cec5SDimitry Andric /* Normalize the divisor. */ 1142*0b57cec5SDimitry Andric bit = precision - APInt::tcMSB(divisor, partsCount) - 1; 1143*0b57cec5SDimitry Andric if (bit) { 1144*0b57cec5SDimitry Andric exponent += bit; 1145*0b57cec5SDimitry Andric APInt::tcShiftLeft(divisor, partsCount, bit); 1146*0b57cec5SDimitry Andric } 1147*0b57cec5SDimitry Andric 1148*0b57cec5SDimitry Andric /* Normalize the dividend. */ 1149*0b57cec5SDimitry Andric bit = precision - APInt::tcMSB(dividend, partsCount) - 1; 1150*0b57cec5SDimitry Andric if (bit) { 1151*0b57cec5SDimitry Andric exponent -= bit; 1152*0b57cec5SDimitry Andric APInt::tcShiftLeft(dividend, partsCount, bit); 1153*0b57cec5SDimitry Andric } 1154*0b57cec5SDimitry Andric 1155*0b57cec5SDimitry Andric /* Ensure the dividend >= divisor initially for the loop below. 1156*0b57cec5SDimitry Andric Incidentally, this means that the division loop below is 1157*0b57cec5SDimitry Andric guaranteed to set the integer bit to one. */ 1158*0b57cec5SDimitry Andric if (APInt::tcCompare(dividend, divisor, partsCount) < 0) { 1159*0b57cec5SDimitry Andric exponent--; 1160*0b57cec5SDimitry Andric APInt::tcShiftLeft(dividend, partsCount, 1); 1161*0b57cec5SDimitry Andric assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0); 1162*0b57cec5SDimitry Andric } 1163*0b57cec5SDimitry Andric 1164*0b57cec5SDimitry Andric /* Long division. */ 1165*0b57cec5SDimitry Andric for (bit = precision; bit; bit -= 1) { 1166*0b57cec5SDimitry Andric if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) { 1167*0b57cec5SDimitry Andric APInt::tcSubtract(dividend, divisor, 0, partsCount); 1168*0b57cec5SDimitry Andric APInt::tcSetBit(lhsSignificand, bit - 1); 1169*0b57cec5SDimitry Andric } 1170*0b57cec5SDimitry Andric 1171*0b57cec5SDimitry Andric APInt::tcShiftLeft(dividend, partsCount, 1); 1172*0b57cec5SDimitry Andric } 1173*0b57cec5SDimitry Andric 1174*0b57cec5SDimitry Andric /* Figure out the lost fraction. */ 1175*0b57cec5SDimitry Andric int cmp = APInt::tcCompare(dividend, divisor, partsCount); 1176*0b57cec5SDimitry Andric 1177*0b57cec5SDimitry Andric if (cmp > 0) 1178*0b57cec5SDimitry Andric lost_fraction = lfMoreThanHalf; 1179*0b57cec5SDimitry Andric else if (cmp == 0) 1180*0b57cec5SDimitry Andric lost_fraction = lfExactlyHalf; 1181*0b57cec5SDimitry Andric else if (APInt::tcIsZero(dividend, partsCount)) 1182*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 1183*0b57cec5SDimitry Andric else 1184*0b57cec5SDimitry Andric lost_fraction = lfLessThanHalf; 1185*0b57cec5SDimitry Andric 1186*0b57cec5SDimitry Andric if (partsCount > 2) 1187*0b57cec5SDimitry Andric delete [] dividend; 1188*0b57cec5SDimitry Andric 1189*0b57cec5SDimitry Andric return lost_fraction; 1190*0b57cec5SDimitry Andric } 1191*0b57cec5SDimitry Andric 1192*0b57cec5SDimitry Andric unsigned int IEEEFloat::significandMSB() const { 1193*0b57cec5SDimitry Andric return APInt::tcMSB(significandParts(), partCount()); 1194*0b57cec5SDimitry Andric } 1195*0b57cec5SDimitry Andric 1196*0b57cec5SDimitry Andric unsigned int IEEEFloat::significandLSB() const { 1197*0b57cec5SDimitry Andric return APInt::tcLSB(significandParts(), partCount()); 1198*0b57cec5SDimitry Andric } 1199*0b57cec5SDimitry Andric 1200*0b57cec5SDimitry Andric /* Note that a zero result is NOT normalized to fcZero. */ 1201*0b57cec5SDimitry Andric lostFraction IEEEFloat::shiftSignificandRight(unsigned int bits) { 1202*0b57cec5SDimitry Andric /* Our exponent should not overflow. */ 1203*0b57cec5SDimitry Andric assert((ExponentType) (exponent + bits) >= exponent); 1204*0b57cec5SDimitry Andric 1205*0b57cec5SDimitry Andric exponent += bits; 1206*0b57cec5SDimitry Andric 1207*0b57cec5SDimitry Andric return shiftRight(significandParts(), partCount(), bits); 1208*0b57cec5SDimitry Andric } 1209*0b57cec5SDimitry Andric 1210*0b57cec5SDimitry Andric /* Shift the significand left BITS bits, subtract BITS from its exponent. */ 1211*0b57cec5SDimitry Andric void IEEEFloat::shiftSignificandLeft(unsigned int bits) { 1212*0b57cec5SDimitry Andric assert(bits < semantics->precision); 1213*0b57cec5SDimitry Andric 1214*0b57cec5SDimitry Andric if (bits) { 1215*0b57cec5SDimitry Andric unsigned int partsCount = partCount(); 1216*0b57cec5SDimitry Andric 1217*0b57cec5SDimitry Andric APInt::tcShiftLeft(significandParts(), partsCount, bits); 1218*0b57cec5SDimitry Andric exponent -= bits; 1219*0b57cec5SDimitry Andric 1220*0b57cec5SDimitry Andric assert(!APInt::tcIsZero(significandParts(), partsCount)); 1221*0b57cec5SDimitry Andric } 1222*0b57cec5SDimitry Andric } 1223*0b57cec5SDimitry Andric 1224*0b57cec5SDimitry Andric IEEEFloat::cmpResult 1225*0b57cec5SDimitry Andric IEEEFloat::compareAbsoluteValue(const IEEEFloat &rhs) const { 1226*0b57cec5SDimitry Andric int compare; 1227*0b57cec5SDimitry Andric 1228*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 1229*0b57cec5SDimitry Andric assert(isFiniteNonZero()); 1230*0b57cec5SDimitry Andric assert(rhs.isFiniteNonZero()); 1231*0b57cec5SDimitry Andric 1232*0b57cec5SDimitry Andric compare = exponent - rhs.exponent; 1233*0b57cec5SDimitry Andric 1234*0b57cec5SDimitry Andric /* If exponents are equal, do an unsigned bignum comparison of the 1235*0b57cec5SDimitry Andric significands. */ 1236*0b57cec5SDimitry Andric if (compare == 0) 1237*0b57cec5SDimitry Andric compare = APInt::tcCompare(significandParts(), rhs.significandParts(), 1238*0b57cec5SDimitry Andric partCount()); 1239*0b57cec5SDimitry Andric 1240*0b57cec5SDimitry Andric if (compare > 0) 1241*0b57cec5SDimitry Andric return cmpGreaterThan; 1242*0b57cec5SDimitry Andric else if (compare < 0) 1243*0b57cec5SDimitry Andric return cmpLessThan; 1244*0b57cec5SDimitry Andric else 1245*0b57cec5SDimitry Andric return cmpEqual; 1246*0b57cec5SDimitry Andric } 1247*0b57cec5SDimitry Andric 1248*0b57cec5SDimitry Andric /* Handle overflow. Sign is preserved. We either become infinity or 1249*0b57cec5SDimitry Andric the largest finite number. */ 1250*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::handleOverflow(roundingMode rounding_mode) { 1251*0b57cec5SDimitry Andric /* Infinity? */ 1252*0b57cec5SDimitry Andric if (rounding_mode == rmNearestTiesToEven || 1253*0b57cec5SDimitry Andric rounding_mode == rmNearestTiesToAway || 1254*0b57cec5SDimitry Andric (rounding_mode == rmTowardPositive && !sign) || 1255*0b57cec5SDimitry Andric (rounding_mode == rmTowardNegative && sign)) { 1256*0b57cec5SDimitry Andric category = fcInfinity; 1257*0b57cec5SDimitry Andric return (opStatus) (opOverflow | opInexact); 1258*0b57cec5SDimitry Andric } 1259*0b57cec5SDimitry Andric 1260*0b57cec5SDimitry Andric /* Otherwise we become the largest finite number. */ 1261*0b57cec5SDimitry Andric category = fcNormal; 1262*0b57cec5SDimitry Andric exponent = semantics->maxExponent; 1263*0b57cec5SDimitry Andric APInt::tcSetLeastSignificantBits(significandParts(), partCount(), 1264*0b57cec5SDimitry Andric semantics->precision); 1265*0b57cec5SDimitry Andric 1266*0b57cec5SDimitry Andric return opInexact; 1267*0b57cec5SDimitry Andric } 1268*0b57cec5SDimitry Andric 1269*0b57cec5SDimitry Andric /* Returns TRUE if, when truncating the current number, with BIT the 1270*0b57cec5SDimitry Andric new LSB, with the given lost fraction and rounding mode, the result 1271*0b57cec5SDimitry Andric would need to be rounded away from zero (i.e., by increasing the 1272*0b57cec5SDimitry Andric signficand). This routine must work for fcZero of both signs, and 1273*0b57cec5SDimitry Andric fcNormal numbers. */ 1274*0b57cec5SDimitry Andric bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode, 1275*0b57cec5SDimitry Andric lostFraction lost_fraction, 1276*0b57cec5SDimitry Andric unsigned int bit) const { 1277*0b57cec5SDimitry Andric /* NaNs and infinities should not have lost fractions. */ 1278*0b57cec5SDimitry Andric assert(isFiniteNonZero() || category == fcZero); 1279*0b57cec5SDimitry Andric 1280*0b57cec5SDimitry Andric /* Current callers never pass this so we don't handle it. */ 1281*0b57cec5SDimitry Andric assert(lost_fraction != lfExactlyZero); 1282*0b57cec5SDimitry Andric 1283*0b57cec5SDimitry Andric switch (rounding_mode) { 1284*0b57cec5SDimitry Andric case rmNearestTiesToAway: 1285*0b57cec5SDimitry Andric return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf; 1286*0b57cec5SDimitry Andric 1287*0b57cec5SDimitry Andric case rmNearestTiesToEven: 1288*0b57cec5SDimitry Andric if (lost_fraction == lfMoreThanHalf) 1289*0b57cec5SDimitry Andric return true; 1290*0b57cec5SDimitry Andric 1291*0b57cec5SDimitry Andric /* Our zeroes don't have a significand to test. */ 1292*0b57cec5SDimitry Andric if (lost_fraction == lfExactlyHalf && category != fcZero) 1293*0b57cec5SDimitry Andric return APInt::tcExtractBit(significandParts(), bit); 1294*0b57cec5SDimitry Andric 1295*0b57cec5SDimitry Andric return false; 1296*0b57cec5SDimitry Andric 1297*0b57cec5SDimitry Andric case rmTowardZero: 1298*0b57cec5SDimitry Andric return false; 1299*0b57cec5SDimitry Andric 1300*0b57cec5SDimitry Andric case rmTowardPositive: 1301*0b57cec5SDimitry Andric return !sign; 1302*0b57cec5SDimitry Andric 1303*0b57cec5SDimitry Andric case rmTowardNegative: 1304*0b57cec5SDimitry Andric return sign; 1305*0b57cec5SDimitry Andric } 1306*0b57cec5SDimitry Andric llvm_unreachable("Invalid rounding mode found"); 1307*0b57cec5SDimitry Andric } 1308*0b57cec5SDimitry Andric 1309*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::normalize(roundingMode rounding_mode, 1310*0b57cec5SDimitry Andric lostFraction lost_fraction) { 1311*0b57cec5SDimitry Andric unsigned int omsb; /* One, not zero, based MSB. */ 1312*0b57cec5SDimitry Andric int exponentChange; 1313*0b57cec5SDimitry Andric 1314*0b57cec5SDimitry Andric if (!isFiniteNonZero()) 1315*0b57cec5SDimitry Andric return opOK; 1316*0b57cec5SDimitry Andric 1317*0b57cec5SDimitry Andric /* Before rounding normalize the exponent of fcNormal numbers. */ 1318*0b57cec5SDimitry Andric omsb = significandMSB() + 1; 1319*0b57cec5SDimitry Andric 1320*0b57cec5SDimitry Andric if (omsb) { 1321*0b57cec5SDimitry Andric /* OMSB is numbered from 1. We want to place it in the integer 1322*0b57cec5SDimitry Andric bit numbered PRECISION if possible, with a compensating change in 1323*0b57cec5SDimitry Andric the exponent. */ 1324*0b57cec5SDimitry Andric exponentChange = omsb - semantics->precision; 1325*0b57cec5SDimitry Andric 1326*0b57cec5SDimitry Andric /* If the resulting exponent is too high, overflow according to 1327*0b57cec5SDimitry Andric the rounding mode. */ 1328*0b57cec5SDimitry Andric if (exponent + exponentChange > semantics->maxExponent) 1329*0b57cec5SDimitry Andric return handleOverflow(rounding_mode); 1330*0b57cec5SDimitry Andric 1331*0b57cec5SDimitry Andric /* Subnormal numbers have exponent minExponent, and their MSB 1332*0b57cec5SDimitry Andric is forced based on that. */ 1333*0b57cec5SDimitry Andric if (exponent + exponentChange < semantics->minExponent) 1334*0b57cec5SDimitry Andric exponentChange = semantics->minExponent - exponent; 1335*0b57cec5SDimitry Andric 1336*0b57cec5SDimitry Andric /* Shifting left is easy as we don't lose precision. */ 1337*0b57cec5SDimitry Andric if (exponentChange < 0) { 1338*0b57cec5SDimitry Andric assert(lost_fraction == lfExactlyZero); 1339*0b57cec5SDimitry Andric 1340*0b57cec5SDimitry Andric shiftSignificandLeft(-exponentChange); 1341*0b57cec5SDimitry Andric 1342*0b57cec5SDimitry Andric return opOK; 1343*0b57cec5SDimitry Andric } 1344*0b57cec5SDimitry Andric 1345*0b57cec5SDimitry Andric if (exponentChange > 0) { 1346*0b57cec5SDimitry Andric lostFraction lf; 1347*0b57cec5SDimitry Andric 1348*0b57cec5SDimitry Andric /* Shift right and capture any new lost fraction. */ 1349*0b57cec5SDimitry Andric lf = shiftSignificandRight(exponentChange); 1350*0b57cec5SDimitry Andric 1351*0b57cec5SDimitry Andric lost_fraction = combineLostFractions(lf, lost_fraction); 1352*0b57cec5SDimitry Andric 1353*0b57cec5SDimitry Andric /* Keep OMSB up-to-date. */ 1354*0b57cec5SDimitry Andric if (omsb > (unsigned) exponentChange) 1355*0b57cec5SDimitry Andric omsb -= exponentChange; 1356*0b57cec5SDimitry Andric else 1357*0b57cec5SDimitry Andric omsb = 0; 1358*0b57cec5SDimitry Andric } 1359*0b57cec5SDimitry Andric } 1360*0b57cec5SDimitry Andric 1361*0b57cec5SDimitry Andric /* Now round the number according to rounding_mode given the lost 1362*0b57cec5SDimitry Andric fraction. */ 1363*0b57cec5SDimitry Andric 1364*0b57cec5SDimitry Andric /* As specified in IEEE 754, since we do not trap we do not report 1365*0b57cec5SDimitry Andric underflow for exact results. */ 1366*0b57cec5SDimitry Andric if (lost_fraction == lfExactlyZero) { 1367*0b57cec5SDimitry Andric /* Canonicalize zeroes. */ 1368*0b57cec5SDimitry Andric if (omsb == 0) 1369*0b57cec5SDimitry Andric category = fcZero; 1370*0b57cec5SDimitry Andric 1371*0b57cec5SDimitry Andric return opOK; 1372*0b57cec5SDimitry Andric } 1373*0b57cec5SDimitry Andric 1374*0b57cec5SDimitry Andric /* Increment the significand if we're rounding away from zero. */ 1375*0b57cec5SDimitry Andric if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) { 1376*0b57cec5SDimitry Andric if (omsb == 0) 1377*0b57cec5SDimitry Andric exponent = semantics->minExponent; 1378*0b57cec5SDimitry Andric 1379*0b57cec5SDimitry Andric incrementSignificand(); 1380*0b57cec5SDimitry Andric omsb = significandMSB() + 1; 1381*0b57cec5SDimitry Andric 1382*0b57cec5SDimitry Andric /* Did the significand increment overflow? */ 1383*0b57cec5SDimitry Andric if (omsb == (unsigned) semantics->precision + 1) { 1384*0b57cec5SDimitry Andric /* Renormalize by incrementing the exponent and shifting our 1385*0b57cec5SDimitry Andric significand right one. However if we already have the 1386*0b57cec5SDimitry Andric maximum exponent we overflow to infinity. */ 1387*0b57cec5SDimitry Andric if (exponent == semantics->maxExponent) { 1388*0b57cec5SDimitry Andric category = fcInfinity; 1389*0b57cec5SDimitry Andric 1390*0b57cec5SDimitry Andric return (opStatus) (opOverflow | opInexact); 1391*0b57cec5SDimitry Andric } 1392*0b57cec5SDimitry Andric 1393*0b57cec5SDimitry Andric shiftSignificandRight(1); 1394*0b57cec5SDimitry Andric 1395*0b57cec5SDimitry Andric return opInexact; 1396*0b57cec5SDimitry Andric } 1397*0b57cec5SDimitry Andric } 1398*0b57cec5SDimitry Andric 1399*0b57cec5SDimitry Andric /* The normal case - we were and are not denormal, and any 1400*0b57cec5SDimitry Andric significand increment above didn't overflow. */ 1401*0b57cec5SDimitry Andric if (omsb == semantics->precision) 1402*0b57cec5SDimitry Andric return opInexact; 1403*0b57cec5SDimitry Andric 1404*0b57cec5SDimitry Andric /* We have a non-zero denormal. */ 1405*0b57cec5SDimitry Andric assert(omsb < semantics->precision); 1406*0b57cec5SDimitry Andric 1407*0b57cec5SDimitry Andric /* Canonicalize zeroes. */ 1408*0b57cec5SDimitry Andric if (omsb == 0) 1409*0b57cec5SDimitry Andric category = fcZero; 1410*0b57cec5SDimitry Andric 1411*0b57cec5SDimitry Andric /* The fcZero case is a denormal that underflowed to zero. */ 1412*0b57cec5SDimitry Andric return (opStatus) (opUnderflow | opInexact); 1413*0b57cec5SDimitry Andric } 1414*0b57cec5SDimitry Andric 1415*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::addOrSubtractSpecials(const IEEEFloat &rhs, 1416*0b57cec5SDimitry Andric bool subtract) { 1417*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 1418*0b57cec5SDimitry Andric default: 1419*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 1420*0b57cec5SDimitry Andric 1421*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 1422*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 1423*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 1424*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 1425*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 1426*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 1427*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 1428*0b57cec5SDimitry Andric return opOK; 1429*0b57cec5SDimitry Andric 1430*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 1431*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 1432*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 1433*0b57cec5SDimitry Andric // We need to be sure to flip the sign here for subtraction because we 1434*0b57cec5SDimitry Andric // don't have a separate negate operation so -NaN becomes 0 - NaN here. 1435*0b57cec5SDimitry Andric sign = rhs.sign ^ subtract; 1436*0b57cec5SDimitry Andric category = fcNaN; 1437*0b57cec5SDimitry Andric copySignificand(rhs); 1438*0b57cec5SDimitry Andric return opOK; 1439*0b57cec5SDimitry Andric 1440*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 1441*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 1442*0b57cec5SDimitry Andric category = fcInfinity; 1443*0b57cec5SDimitry Andric sign = rhs.sign ^ subtract; 1444*0b57cec5SDimitry Andric return opOK; 1445*0b57cec5SDimitry Andric 1446*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 1447*0b57cec5SDimitry Andric assign(rhs); 1448*0b57cec5SDimitry Andric sign = rhs.sign ^ subtract; 1449*0b57cec5SDimitry Andric return opOK; 1450*0b57cec5SDimitry Andric 1451*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 1452*0b57cec5SDimitry Andric /* Sign depends on rounding mode; handled by caller. */ 1453*0b57cec5SDimitry Andric return opOK; 1454*0b57cec5SDimitry Andric 1455*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1456*0b57cec5SDimitry Andric /* Differently signed infinities can only be validly 1457*0b57cec5SDimitry Andric subtracted. */ 1458*0b57cec5SDimitry Andric if (((sign ^ rhs.sign)!=0) != subtract) { 1459*0b57cec5SDimitry Andric makeNaN(); 1460*0b57cec5SDimitry Andric return opInvalidOp; 1461*0b57cec5SDimitry Andric } 1462*0b57cec5SDimitry Andric 1463*0b57cec5SDimitry Andric return opOK; 1464*0b57cec5SDimitry Andric 1465*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 1466*0b57cec5SDimitry Andric return opDivByZero; 1467*0b57cec5SDimitry Andric } 1468*0b57cec5SDimitry Andric } 1469*0b57cec5SDimitry Andric 1470*0b57cec5SDimitry Andric /* Add or subtract two normal numbers. */ 1471*0b57cec5SDimitry Andric lostFraction IEEEFloat::addOrSubtractSignificand(const IEEEFloat &rhs, 1472*0b57cec5SDimitry Andric bool subtract) { 1473*0b57cec5SDimitry Andric integerPart carry; 1474*0b57cec5SDimitry Andric lostFraction lost_fraction; 1475*0b57cec5SDimitry Andric int bits; 1476*0b57cec5SDimitry Andric 1477*0b57cec5SDimitry Andric /* Determine if the operation on the absolute values is effectively 1478*0b57cec5SDimitry Andric an addition or subtraction. */ 1479*0b57cec5SDimitry Andric subtract ^= static_cast<bool>(sign ^ rhs.sign); 1480*0b57cec5SDimitry Andric 1481*0b57cec5SDimitry Andric /* Are we bigger exponent-wise than the RHS? */ 1482*0b57cec5SDimitry Andric bits = exponent - rhs.exponent; 1483*0b57cec5SDimitry Andric 1484*0b57cec5SDimitry Andric /* Subtraction is more subtle than one might naively expect. */ 1485*0b57cec5SDimitry Andric if (subtract) { 1486*0b57cec5SDimitry Andric IEEEFloat temp_rhs(rhs); 1487*0b57cec5SDimitry Andric bool reverse; 1488*0b57cec5SDimitry Andric 1489*0b57cec5SDimitry Andric if (bits == 0) { 1490*0b57cec5SDimitry Andric reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan; 1491*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 1492*0b57cec5SDimitry Andric } else if (bits > 0) { 1493*0b57cec5SDimitry Andric lost_fraction = temp_rhs.shiftSignificandRight(bits - 1); 1494*0b57cec5SDimitry Andric shiftSignificandLeft(1); 1495*0b57cec5SDimitry Andric reverse = false; 1496*0b57cec5SDimitry Andric } else { 1497*0b57cec5SDimitry Andric lost_fraction = shiftSignificandRight(-bits - 1); 1498*0b57cec5SDimitry Andric temp_rhs.shiftSignificandLeft(1); 1499*0b57cec5SDimitry Andric reverse = true; 1500*0b57cec5SDimitry Andric } 1501*0b57cec5SDimitry Andric 1502*0b57cec5SDimitry Andric if (reverse) { 1503*0b57cec5SDimitry Andric carry = temp_rhs.subtractSignificand 1504*0b57cec5SDimitry Andric (*this, lost_fraction != lfExactlyZero); 1505*0b57cec5SDimitry Andric copySignificand(temp_rhs); 1506*0b57cec5SDimitry Andric sign = !sign; 1507*0b57cec5SDimitry Andric } else { 1508*0b57cec5SDimitry Andric carry = subtractSignificand 1509*0b57cec5SDimitry Andric (temp_rhs, lost_fraction != lfExactlyZero); 1510*0b57cec5SDimitry Andric } 1511*0b57cec5SDimitry Andric 1512*0b57cec5SDimitry Andric /* Invert the lost fraction - it was on the RHS and 1513*0b57cec5SDimitry Andric subtracted. */ 1514*0b57cec5SDimitry Andric if (lost_fraction == lfLessThanHalf) 1515*0b57cec5SDimitry Andric lost_fraction = lfMoreThanHalf; 1516*0b57cec5SDimitry Andric else if (lost_fraction == lfMoreThanHalf) 1517*0b57cec5SDimitry Andric lost_fraction = lfLessThanHalf; 1518*0b57cec5SDimitry Andric 1519*0b57cec5SDimitry Andric /* The code above is intended to ensure that no borrow is 1520*0b57cec5SDimitry Andric necessary. */ 1521*0b57cec5SDimitry Andric assert(!carry); 1522*0b57cec5SDimitry Andric (void)carry; 1523*0b57cec5SDimitry Andric } else { 1524*0b57cec5SDimitry Andric if (bits > 0) { 1525*0b57cec5SDimitry Andric IEEEFloat temp_rhs(rhs); 1526*0b57cec5SDimitry Andric 1527*0b57cec5SDimitry Andric lost_fraction = temp_rhs.shiftSignificandRight(bits); 1528*0b57cec5SDimitry Andric carry = addSignificand(temp_rhs); 1529*0b57cec5SDimitry Andric } else { 1530*0b57cec5SDimitry Andric lost_fraction = shiftSignificandRight(-bits); 1531*0b57cec5SDimitry Andric carry = addSignificand(rhs); 1532*0b57cec5SDimitry Andric } 1533*0b57cec5SDimitry Andric 1534*0b57cec5SDimitry Andric /* We have a guard bit; generating a carry cannot happen. */ 1535*0b57cec5SDimitry Andric assert(!carry); 1536*0b57cec5SDimitry Andric (void)carry; 1537*0b57cec5SDimitry Andric } 1538*0b57cec5SDimitry Andric 1539*0b57cec5SDimitry Andric return lost_fraction; 1540*0b57cec5SDimitry Andric } 1541*0b57cec5SDimitry Andric 1542*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::multiplySpecials(const IEEEFloat &rhs) { 1543*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 1544*0b57cec5SDimitry Andric default: 1545*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 1546*0b57cec5SDimitry Andric 1547*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 1548*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 1549*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 1550*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 1551*0b57cec5SDimitry Andric sign = false; 1552*0b57cec5SDimitry Andric return opOK; 1553*0b57cec5SDimitry Andric 1554*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 1555*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 1556*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 1557*0b57cec5SDimitry Andric sign = false; 1558*0b57cec5SDimitry Andric category = fcNaN; 1559*0b57cec5SDimitry Andric copySignificand(rhs); 1560*0b57cec5SDimitry Andric return opOK; 1561*0b57cec5SDimitry Andric 1562*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 1563*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 1564*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1565*0b57cec5SDimitry Andric category = fcInfinity; 1566*0b57cec5SDimitry Andric return opOK; 1567*0b57cec5SDimitry Andric 1568*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 1569*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 1570*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 1571*0b57cec5SDimitry Andric category = fcZero; 1572*0b57cec5SDimitry Andric return opOK; 1573*0b57cec5SDimitry Andric 1574*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 1575*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 1576*0b57cec5SDimitry Andric makeNaN(); 1577*0b57cec5SDimitry Andric return opInvalidOp; 1578*0b57cec5SDimitry Andric 1579*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 1580*0b57cec5SDimitry Andric return opOK; 1581*0b57cec5SDimitry Andric } 1582*0b57cec5SDimitry Andric } 1583*0b57cec5SDimitry Andric 1584*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::divideSpecials(const IEEEFloat &rhs) { 1585*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 1586*0b57cec5SDimitry Andric default: 1587*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 1588*0b57cec5SDimitry Andric 1589*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 1590*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 1591*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 1592*0b57cec5SDimitry Andric category = fcNaN; 1593*0b57cec5SDimitry Andric copySignificand(rhs); 1594*0b57cec5SDimitry Andric LLVM_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): 1599*0b57cec5SDimitry Andric sign = false; 1600*0b57cec5SDimitry Andric LLVM_FALLTHROUGH; 1601*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 1602*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 1603*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 1604*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 1605*0b57cec5SDimitry Andric return opOK; 1606*0b57cec5SDimitry Andric 1607*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 1608*0b57cec5SDimitry Andric category = fcZero; 1609*0b57cec5SDimitry Andric return opOK; 1610*0b57cec5SDimitry Andric 1611*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 1612*0b57cec5SDimitry Andric category = fcInfinity; 1613*0b57cec5SDimitry Andric return opDivByZero; 1614*0b57cec5SDimitry Andric 1615*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1616*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 1617*0b57cec5SDimitry Andric makeNaN(); 1618*0b57cec5SDimitry Andric return opInvalidOp; 1619*0b57cec5SDimitry Andric 1620*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 1621*0b57cec5SDimitry Andric return opOK; 1622*0b57cec5SDimitry Andric } 1623*0b57cec5SDimitry Andric } 1624*0b57cec5SDimitry Andric 1625*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::modSpecials(const IEEEFloat &rhs) { 1626*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 1627*0b57cec5SDimitry Andric default: 1628*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 1629*0b57cec5SDimitry Andric 1630*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 1631*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 1632*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 1633*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 1634*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 1635*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 1636*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 1637*0b57cec5SDimitry Andric return opOK; 1638*0b57cec5SDimitry Andric 1639*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 1640*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 1641*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 1642*0b57cec5SDimitry Andric sign = false; 1643*0b57cec5SDimitry Andric category = fcNaN; 1644*0b57cec5SDimitry Andric copySignificand(rhs); 1645*0b57cec5SDimitry Andric return opOK; 1646*0b57cec5SDimitry Andric 1647*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 1648*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 1649*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 1650*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1651*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 1652*0b57cec5SDimitry Andric makeNaN(); 1653*0b57cec5SDimitry Andric return opInvalidOp; 1654*0b57cec5SDimitry Andric 1655*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 1656*0b57cec5SDimitry Andric return opOK; 1657*0b57cec5SDimitry Andric } 1658*0b57cec5SDimitry Andric } 1659*0b57cec5SDimitry Andric 1660*0b57cec5SDimitry Andric /* Change sign. */ 1661*0b57cec5SDimitry Andric void IEEEFloat::changeSign() { 1662*0b57cec5SDimitry Andric /* Look mummy, this one's easy. */ 1663*0b57cec5SDimitry Andric sign = !sign; 1664*0b57cec5SDimitry Andric } 1665*0b57cec5SDimitry Andric 1666*0b57cec5SDimitry Andric /* Normalized addition or subtraction. */ 1667*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::addOrSubtract(const IEEEFloat &rhs, 1668*0b57cec5SDimitry Andric roundingMode rounding_mode, 1669*0b57cec5SDimitry Andric bool subtract) { 1670*0b57cec5SDimitry Andric opStatus fs; 1671*0b57cec5SDimitry Andric 1672*0b57cec5SDimitry Andric fs = addOrSubtractSpecials(rhs, subtract); 1673*0b57cec5SDimitry Andric 1674*0b57cec5SDimitry Andric /* This return code means it was not a simple case. */ 1675*0b57cec5SDimitry Andric if (fs == opDivByZero) { 1676*0b57cec5SDimitry Andric lostFraction lost_fraction; 1677*0b57cec5SDimitry Andric 1678*0b57cec5SDimitry Andric lost_fraction = addOrSubtractSignificand(rhs, subtract); 1679*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lost_fraction); 1680*0b57cec5SDimitry Andric 1681*0b57cec5SDimitry Andric /* Can only be zero if we lost no fraction. */ 1682*0b57cec5SDimitry Andric assert(category != fcZero || lost_fraction == lfExactlyZero); 1683*0b57cec5SDimitry Andric } 1684*0b57cec5SDimitry Andric 1685*0b57cec5SDimitry Andric /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a 1686*0b57cec5SDimitry Andric positive zero unless rounding to minus infinity, except that 1687*0b57cec5SDimitry Andric adding two like-signed zeroes gives that zero. */ 1688*0b57cec5SDimitry Andric if (category == fcZero) { 1689*0b57cec5SDimitry Andric if (rhs.category != fcZero || (sign == rhs.sign) == subtract) 1690*0b57cec5SDimitry Andric sign = (rounding_mode == rmTowardNegative); 1691*0b57cec5SDimitry Andric } 1692*0b57cec5SDimitry Andric 1693*0b57cec5SDimitry Andric return fs; 1694*0b57cec5SDimitry Andric } 1695*0b57cec5SDimitry Andric 1696*0b57cec5SDimitry Andric /* Normalized addition. */ 1697*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::add(const IEEEFloat &rhs, 1698*0b57cec5SDimitry Andric roundingMode rounding_mode) { 1699*0b57cec5SDimitry Andric return addOrSubtract(rhs, rounding_mode, false); 1700*0b57cec5SDimitry Andric } 1701*0b57cec5SDimitry Andric 1702*0b57cec5SDimitry Andric /* Normalized subtraction. */ 1703*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::subtract(const IEEEFloat &rhs, 1704*0b57cec5SDimitry Andric roundingMode rounding_mode) { 1705*0b57cec5SDimitry Andric return addOrSubtract(rhs, rounding_mode, true); 1706*0b57cec5SDimitry Andric } 1707*0b57cec5SDimitry Andric 1708*0b57cec5SDimitry Andric /* Normalized multiply. */ 1709*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::multiply(const IEEEFloat &rhs, 1710*0b57cec5SDimitry Andric roundingMode rounding_mode) { 1711*0b57cec5SDimitry Andric opStatus fs; 1712*0b57cec5SDimitry Andric 1713*0b57cec5SDimitry Andric sign ^= rhs.sign; 1714*0b57cec5SDimitry Andric fs = multiplySpecials(rhs); 1715*0b57cec5SDimitry Andric 1716*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 1717*0b57cec5SDimitry Andric lostFraction lost_fraction = multiplySignificand(rhs, nullptr); 1718*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lost_fraction); 1719*0b57cec5SDimitry Andric if (lost_fraction != lfExactlyZero) 1720*0b57cec5SDimitry Andric fs = (opStatus) (fs | opInexact); 1721*0b57cec5SDimitry Andric } 1722*0b57cec5SDimitry Andric 1723*0b57cec5SDimitry Andric return fs; 1724*0b57cec5SDimitry Andric } 1725*0b57cec5SDimitry Andric 1726*0b57cec5SDimitry Andric /* Normalized divide. */ 1727*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::divide(const IEEEFloat &rhs, 1728*0b57cec5SDimitry Andric roundingMode rounding_mode) { 1729*0b57cec5SDimitry Andric opStatus fs; 1730*0b57cec5SDimitry Andric 1731*0b57cec5SDimitry Andric sign ^= rhs.sign; 1732*0b57cec5SDimitry Andric fs = divideSpecials(rhs); 1733*0b57cec5SDimitry Andric 1734*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 1735*0b57cec5SDimitry Andric lostFraction lost_fraction = divideSignificand(rhs); 1736*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lost_fraction); 1737*0b57cec5SDimitry Andric if (lost_fraction != lfExactlyZero) 1738*0b57cec5SDimitry Andric fs = (opStatus) (fs | opInexact); 1739*0b57cec5SDimitry Andric } 1740*0b57cec5SDimitry Andric 1741*0b57cec5SDimitry Andric return fs; 1742*0b57cec5SDimitry Andric } 1743*0b57cec5SDimitry Andric 1744*0b57cec5SDimitry Andric /* Normalized remainder. This is not currently correct in all cases. */ 1745*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::remainder(const IEEEFloat &rhs) { 1746*0b57cec5SDimitry Andric opStatus fs; 1747*0b57cec5SDimitry Andric IEEEFloat V = *this; 1748*0b57cec5SDimitry Andric unsigned int origSign = sign; 1749*0b57cec5SDimitry Andric 1750*0b57cec5SDimitry Andric fs = V.divide(rhs, rmNearestTiesToEven); 1751*0b57cec5SDimitry Andric if (fs == opDivByZero) 1752*0b57cec5SDimitry Andric return fs; 1753*0b57cec5SDimitry Andric 1754*0b57cec5SDimitry Andric int parts = partCount(); 1755*0b57cec5SDimitry Andric integerPart *x = new integerPart[parts]; 1756*0b57cec5SDimitry Andric bool ignored; 1757*0b57cec5SDimitry Andric fs = V.convertToInteger(makeMutableArrayRef(x, parts), 1758*0b57cec5SDimitry Andric parts * integerPartWidth, true, rmNearestTiesToEven, 1759*0b57cec5SDimitry Andric &ignored); 1760*0b57cec5SDimitry Andric if (fs == opInvalidOp) { 1761*0b57cec5SDimitry Andric delete[] x; 1762*0b57cec5SDimitry Andric return fs; 1763*0b57cec5SDimitry Andric } 1764*0b57cec5SDimitry Andric 1765*0b57cec5SDimitry Andric fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true, 1766*0b57cec5SDimitry Andric rmNearestTiesToEven); 1767*0b57cec5SDimitry Andric assert(fs==opOK); // should always work 1768*0b57cec5SDimitry Andric 1769*0b57cec5SDimitry Andric fs = V.multiply(rhs, rmNearestTiesToEven); 1770*0b57cec5SDimitry Andric assert(fs==opOK || fs==opInexact); // should not overflow or underflow 1771*0b57cec5SDimitry Andric 1772*0b57cec5SDimitry Andric fs = subtract(V, rmNearestTiesToEven); 1773*0b57cec5SDimitry Andric assert(fs==opOK || fs==opInexact); // likewise 1774*0b57cec5SDimitry Andric 1775*0b57cec5SDimitry Andric if (isZero()) 1776*0b57cec5SDimitry Andric sign = origSign; // IEEE754 requires this 1777*0b57cec5SDimitry Andric delete[] x; 1778*0b57cec5SDimitry Andric return fs; 1779*0b57cec5SDimitry Andric } 1780*0b57cec5SDimitry Andric 1781*0b57cec5SDimitry Andric /* Normalized llvm frem (C fmod). */ 1782*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) { 1783*0b57cec5SDimitry Andric opStatus fs; 1784*0b57cec5SDimitry Andric fs = modSpecials(rhs); 1785*0b57cec5SDimitry Andric unsigned int origSign = sign; 1786*0b57cec5SDimitry Andric 1787*0b57cec5SDimitry Andric while (isFiniteNonZero() && rhs.isFiniteNonZero() && 1788*0b57cec5SDimitry Andric compareAbsoluteValue(rhs) != cmpLessThan) { 1789*0b57cec5SDimitry Andric IEEEFloat V = scalbn(rhs, ilogb(*this) - ilogb(rhs), rmNearestTiesToEven); 1790*0b57cec5SDimitry Andric if (compareAbsoluteValue(V) == cmpLessThan) 1791*0b57cec5SDimitry Andric V = scalbn(V, -1, rmNearestTiesToEven); 1792*0b57cec5SDimitry Andric V.sign = sign; 1793*0b57cec5SDimitry Andric 1794*0b57cec5SDimitry Andric fs = subtract(V, rmNearestTiesToEven); 1795*0b57cec5SDimitry Andric assert(fs==opOK); 1796*0b57cec5SDimitry Andric } 1797*0b57cec5SDimitry Andric if (isZero()) 1798*0b57cec5SDimitry Andric sign = origSign; // fmod requires this 1799*0b57cec5SDimitry Andric return fs; 1800*0b57cec5SDimitry Andric } 1801*0b57cec5SDimitry Andric 1802*0b57cec5SDimitry Andric /* Normalized fused-multiply-add. */ 1803*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::fusedMultiplyAdd(const IEEEFloat &multiplicand, 1804*0b57cec5SDimitry Andric const IEEEFloat &addend, 1805*0b57cec5SDimitry Andric roundingMode rounding_mode) { 1806*0b57cec5SDimitry Andric opStatus fs; 1807*0b57cec5SDimitry Andric 1808*0b57cec5SDimitry Andric /* Post-multiplication sign, before addition. */ 1809*0b57cec5SDimitry Andric sign ^= multiplicand.sign; 1810*0b57cec5SDimitry Andric 1811*0b57cec5SDimitry Andric /* If and only if all arguments are normal do we need to do an 1812*0b57cec5SDimitry Andric extended-precision calculation. */ 1813*0b57cec5SDimitry Andric if (isFiniteNonZero() && 1814*0b57cec5SDimitry Andric multiplicand.isFiniteNonZero() && 1815*0b57cec5SDimitry Andric addend.isFinite()) { 1816*0b57cec5SDimitry Andric lostFraction lost_fraction; 1817*0b57cec5SDimitry Andric 1818*0b57cec5SDimitry Andric lost_fraction = multiplySignificand(multiplicand, &addend); 1819*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lost_fraction); 1820*0b57cec5SDimitry Andric if (lost_fraction != lfExactlyZero) 1821*0b57cec5SDimitry Andric fs = (opStatus) (fs | opInexact); 1822*0b57cec5SDimitry Andric 1823*0b57cec5SDimitry Andric /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a 1824*0b57cec5SDimitry Andric positive zero unless rounding to minus infinity, except that 1825*0b57cec5SDimitry Andric adding two like-signed zeroes gives that zero. */ 1826*0b57cec5SDimitry Andric if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign) 1827*0b57cec5SDimitry Andric sign = (rounding_mode == rmTowardNegative); 1828*0b57cec5SDimitry Andric } else { 1829*0b57cec5SDimitry Andric fs = multiplySpecials(multiplicand); 1830*0b57cec5SDimitry Andric 1831*0b57cec5SDimitry Andric /* FS can only be opOK or opInvalidOp. There is no more work 1832*0b57cec5SDimitry Andric to do in the latter case. The IEEE-754R standard says it is 1833*0b57cec5SDimitry Andric implementation-defined in this case whether, if ADDEND is a 1834*0b57cec5SDimitry Andric quiet NaN, we raise invalid op; this implementation does so. 1835*0b57cec5SDimitry Andric 1836*0b57cec5SDimitry Andric If we need to do the addition we can do so with normal 1837*0b57cec5SDimitry Andric precision. */ 1838*0b57cec5SDimitry Andric if (fs == opOK) 1839*0b57cec5SDimitry Andric fs = addOrSubtract(addend, rounding_mode, false); 1840*0b57cec5SDimitry Andric } 1841*0b57cec5SDimitry Andric 1842*0b57cec5SDimitry Andric return fs; 1843*0b57cec5SDimitry Andric } 1844*0b57cec5SDimitry Andric 1845*0b57cec5SDimitry Andric /* Rounding-mode corrrect round to integral value. */ 1846*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) { 1847*0b57cec5SDimitry Andric opStatus fs; 1848*0b57cec5SDimitry Andric 1849*0b57cec5SDimitry Andric // If the exponent is large enough, we know that this value is already 1850*0b57cec5SDimitry Andric // integral, and the arithmetic below would potentially cause it to saturate 1851*0b57cec5SDimitry Andric // to +/-Inf. Bail out early instead. 1852*0b57cec5SDimitry Andric if (isFiniteNonZero() && exponent+1 >= (int)semanticsPrecision(*semantics)) 1853*0b57cec5SDimitry Andric return opOK; 1854*0b57cec5SDimitry Andric 1855*0b57cec5SDimitry Andric // The algorithm here is quite simple: we add 2^(p-1), where p is the 1856*0b57cec5SDimitry Andric // precision of our format, and then subtract it back off again. The choice 1857*0b57cec5SDimitry Andric // of rounding modes for the addition/subtraction determines the rounding mode 1858*0b57cec5SDimitry Andric // for our integral rounding as well. 1859*0b57cec5SDimitry Andric // NOTE: When the input value is negative, we do subtraction followed by 1860*0b57cec5SDimitry Andric // addition instead. 1861*0b57cec5SDimitry Andric APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1); 1862*0b57cec5SDimitry Andric IntegerConstant <<= semanticsPrecision(*semantics)-1; 1863*0b57cec5SDimitry Andric IEEEFloat MagicConstant(*semantics); 1864*0b57cec5SDimitry Andric fs = MagicConstant.convertFromAPInt(IntegerConstant, false, 1865*0b57cec5SDimitry Andric rmNearestTiesToEven); 1866*0b57cec5SDimitry Andric MagicConstant.sign = sign; 1867*0b57cec5SDimitry Andric 1868*0b57cec5SDimitry Andric if (fs != opOK) 1869*0b57cec5SDimitry Andric return fs; 1870*0b57cec5SDimitry Andric 1871*0b57cec5SDimitry Andric // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly. 1872*0b57cec5SDimitry Andric bool inputSign = isNegative(); 1873*0b57cec5SDimitry Andric 1874*0b57cec5SDimitry Andric fs = add(MagicConstant, rounding_mode); 1875*0b57cec5SDimitry Andric if (fs != opOK && fs != opInexact) 1876*0b57cec5SDimitry Andric return fs; 1877*0b57cec5SDimitry Andric 1878*0b57cec5SDimitry Andric fs = subtract(MagicConstant, rounding_mode); 1879*0b57cec5SDimitry Andric 1880*0b57cec5SDimitry Andric // Restore the input sign. 1881*0b57cec5SDimitry Andric if (inputSign != isNegative()) 1882*0b57cec5SDimitry Andric changeSign(); 1883*0b57cec5SDimitry Andric 1884*0b57cec5SDimitry Andric return fs; 1885*0b57cec5SDimitry Andric } 1886*0b57cec5SDimitry Andric 1887*0b57cec5SDimitry Andric 1888*0b57cec5SDimitry Andric /* Comparison requires normalized numbers. */ 1889*0b57cec5SDimitry Andric IEEEFloat::cmpResult IEEEFloat::compare(const IEEEFloat &rhs) const { 1890*0b57cec5SDimitry Andric cmpResult result; 1891*0b57cec5SDimitry Andric 1892*0b57cec5SDimitry Andric assert(semantics == rhs.semantics); 1893*0b57cec5SDimitry Andric 1894*0b57cec5SDimitry Andric switch (PackCategoriesIntoKey(category, rhs.category)) { 1895*0b57cec5SDimitry Andric default: 1896*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 1897*0b57cec5SDimitry Andric 1898*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcZero): 1899*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNormal): 1900*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcInfinity): 1901*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNaN, fcNaN): 1902*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNaN): 1903*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNaN): 1904*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNaN): 1905*0b57cec5SDimitry Andric return cmpUnordered; 1906*0b57cec5SDimitry Andric 1907*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcNormal): 1908*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcZero): 1909*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcZero): 1910*0b57cec5SDimitry Andric if (sign) 1911*0b57cec5SDimitry Andric return cmpLessThan; 1912*0b57cec5SDimitry Andric else 1913*0b57cec5SDimitry Andric return cmpGreaterThan; 1914*0b57cec5SDimitry Andric 1915*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcInfinity): 1916*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcInfinity): 1917*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcNormal): 1918*0b57cec5SDimitry Andric if (rhs.sign) 1919*0b57cec5SDimitry Andric return cmpGreaterThan; 1920*0b57cec5SDimitry Andric else 1921*0b57cec5SDimitry Andric return cmpLessThan; 1922*0b57cec5SDimitry Andric 1923*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcInfinity, fcInfinity): 1924*0b57cec5SDimitry Andric if (sign == rhs.sign) 1925*0b57cec5SDimitry Andric return cmpEqual; 1926*0b57cec5SDimitry Andric else if (sign) 1927*0b57cec5SDimitry Andric return cmpLessThan; 1928*0b57cec5SDimitry Andric else 1929*0b57cec5SDimitry Andric return cmpGreaterThan; 1930*0b57cec5SDimitry Andric 1931*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcZero, fcZero): 1932*0b57cec5SDimitry Andric return cmpEqual; 1933*0b57cec5SDimitry Andric 1934*0b57cec5SDimitry Andric case PackCategoriesIntoKey(fcNormal, fcNormal): 1935*0b57cec5SDimitry Andric break; 1936*0b57cec5SDimitry Andric } 1937*0b57cec5SDimitry Andric 1938*0b57cec5SDimitry Andric /* Two normal numbers. Do they have the same sign? */ 1939*0b57cec5SDimitry Andric if (sign != rhs.sign) { 1940*0b57cec5SDimitry Andric if (sign) 1941*0b57cec5SDimitry Andric result = cmpLessThan; 1942*0b57cec5SDimitry Andric else 1943*0b57cec5SDimitry Andric result = cmpGreaterThan; 1944*0b57cec5SDimitry Andric } else { 1945*0b57cec5SDimitry Andric /* Compare absolute values; invert result if negative. */ 1946*0b57cec5SDimitry Andric result = compareAbsoluteValue(rhs); 1947*0b57cec5SDimitry Andric 1948*0b57cec5SDimitry Andric if (sign) { 1949*0b57cec5SDimitry Andric if (result == cmpLessThan) 1950*0b57cec5SDimitry Andric result = cmpGreaterThan; 1951*0b57cec5SDimitry Andric else if (result == cmpGreaterThan) 1952*0b57cec5SDimitry Andric result = cmpLessThan; 1953*0b57cec5SDimitry Andric } 1954*0b57cec5SDimitry Andric } 1955*0b57cec5SDimitry Andric 1956*0b57cec5SDimitry Andric return result; 1957*0b57cec5SDimitry Andric } 1958*0b57cec5SDimitry Andric 1959*0b57cec5SDimitry Andric /// IEEEFloat::convert - convert a value of one floating point type to another. 1960*0b57cec5SDimitry Andric /// The return value corresponds to the IEEE754 exceptions. *losesInfo 1961*0b57cec5SDimitry Andric /// records whether the transformation lost information, i.e. whether 1962*0b57cec5SDimitry Andric /// converting the result back to the original type will produce the 1963*0b57cec5SDimitry Andric /// original value (this is almost the same as return value==fsOK, but there 1964*0b57cec5SDimitry Andric /// are edge cases where this is not so). 1965*0b57cec5SDimitry Andric 1966*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics, 1967*0b57cec5SDimitry Andric roundingMode rounding_mode, 1968*0b57cec5SDimitry Andric bool *losesInfo) { 1969*0b57cec5SDimitry Andric lostFraction lostFraction; 1970*0b57cec5SDimitry Andric unsigned int newPartCount, oldPartCount; 1971*0b57cec5SDimitry Andric opStatus fs; 1972*0b57cec5SDimitry Andric int shift; 1973*0b57cec5SDimitry Andric const fltSemantics &fromSemantics = *semantics; 1974*0b57cec5SDimitry Andric 1975*0b57cec5SDimitry Andric lostFraction = lfExactlyZero; 1976*0b57cec5SDimitry Andric newPartCount = partCountForBits(toSemantics.precision + 1); 1977*0b57cec5SDimitry Andric oldPartCount = partCount(); 1978*0b57cec5SDimitry Andric shift = toSemantics.precision - fromSemantics.precision; 1979*0b57cec5SDimitry Andric 1980*0b57cec5SDimitry Andric bool X86SpecialNan = false; 1981*0b57cec5SDimitry Andric if (&fromSemantics == &semX87DoubleExtended && 1982*0b57cec5SDimitry Andric &toSemantics != &semX87DoubleExtended && category == fcNaN && 1983*0b57cec5SDimitry Andric (!(*significandParts() & 0x8000000000000000ULL) || 1984*0b57cec5SDimitry Andric !(*significandParts() & 0x4000000000000000ULL))) { 1985*0b57cec5SDimitry Andric // x86 has some unusual NaNs which cannot be represented in any other 1986*0b57cec5SDimitry Andric // format; note them here. 1987*0b57cec5SDimitry Andric X86SpecialNan = true; 1988*0b57cec5SDimitry Andric } 1989*0b57cec5SDimitry Andric 1990*0b57cec5SDimitry Andric // If this is a truncation of a denormal number, and the target semantics 1991*0b57cec5SDimitry Andric // has larger exponent range than the source semantics (this can happen 1992*0b57cec5SDimitry Andric // when truncating from PowerPC double-double to double format), the 1993*0b57cec5SDimitry Andric // right shift could lose result mantissa bits. Adjust exponent instead 1994*0b57cec5SDimitry Andric // of performing excessive shift. 1995*0b57cec5SDimitry Andric if (shift < 0 && isFiniteNonZero()) { 1996*0b57cec5SDimitry Andric int exponentChange = significandMSB() + 1 - fromSemantics.precision; 1997*0b57cec5SDimitry Andric if (exponent + exponentChange < toSemantics.minExponent) 1998*0b57cec5SDimitry Andric exponentChange = toSemantics.minExponent - exponent; 1999*0b57cec5SDimitry Andric if (exponentChange < shift) 2000*0b57cec5SDimitry Andric exponentChange = shift; 2001*0b57cec5SDimitry Andric if (exponentChange < 0) { 2002*0b57cec5SDimitry Andric shift -= exponentChange; 2003*0b57cec5SDimitry Andric exponent += exponentChange; 2004*0b57cec5SDimitry Andric } 2005*0b57cec5SDimitry Andric } 2006*0b57cec5SDimitry Andric 2007*0b57cec5SDimitry Andric // If this is a truncation, perform the shift before we narrow the storage. 2008*0b57cec5SDimitry Andric if (shift < 0 && (isFiniteNonZero() || category==fcNaN)) 2009*0b57cec5SDimitry Andric lostFraction = shiftRight(significandParts(), oldPartCount, -shift); 2010*0b57cec5SDimitry Andric 2011*0b57cec5SDimitry Andric // Fix the storage so it can hold to new value. 2012*0b57cec5SDimitry Andric if (newPartCount > oldPartCount) { 2013*0b57cec5SDimitry Andric // The new type requires more storage; make it available. 2014*0b57cec5SDimitry Andric integerPart *newParts; 2015*0b57cec5SDimitry Andric newParts = new integerPart[newPartCount]; 2016*0b57cec5SDimitry Andric APInt::tcSet(newParts, 0, newPartCount); 2017*0b57cec5SDimitry Andric if (isFiniteNonZero() || category==fcNaN) 2018*0b57cec5SDimitry Andric APInt::tcAssign(newParts, significandParts(), oldPartCount); 2019*0b57cec5SDimitry Andric freeSignificand(); 2020*0b57cec5SDimitry Andric significand.parts = newParts; 2021*0b57cec5SDimitry Andric } else if (newPartCount == 1 && oldPartCount != 1) { 2022*0b57cec5SDimitry Andric // Switch to built-in storage for a single part. 2023*0b57cec5SDimitry Andric integerPart newPart = 0; 2024*0b57cec5SDimitry Andric if (isFiniteNonZero() || category==fcNaN) 2025*0b57cec5SDimitry Andric newPart = significandParts()[0]; 2026*0b57cec5SDimitry Andric freeSignificand(); 2027*0b57cec5SDimitry Andric significand.part = newPart; 2028*0b57cec5SDimitry Andric } 2029*0b57cec5SDimitry Andric 2030*0b57cec5SDimitry Andric // Now that we have the right storage, switch the semantics. 2031*0b57cec5SDimitry Andric semantics = &toSemantics; 2032*0b57cec5SDimitry Andric 2033*0b57cec5SDimitry Andric // If this is an extension, perform the shift now that the storage is 2034*0b57cec5SDimitry Andric // available. 2035*0b57cec5SDimitry Andric if (shift > 0 && (isFiniteNonZero() || category==fcNaN)) 2036*0b57cec5SDimitry Andric APInt::tcShiftLeft(significandParts(), newPartCount, shift); 2037*0b57cec5SDimitry Andric 2038*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 2039*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lostFraction); 2040*0b57cec5SDimitry Andric *losesInfo = (fs != opOK); 2041*0b57cec5SDimitry Andric } else if (category == fcNaN) { 2042*0b57cec5SDimitry Andric *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan; 2043*0b57cec5SDimitry Andric 2044*0b57cec5SDimitry Andric // For x87 extended precision, we want to make a NaN, not a special NaN if 2045*0b57cec5SDimitry Andric // the input wasn't special either. 2046*0b57cec5SDimitry Andric if (!X86SpecialNan && semantics == &semX87DoubleExtended) 2047*0b57cec5SDimitry Andric APInt::tcSetBit(significandParts(), semantics->precision - 1); 2048*0b57cec5SDimitry Andric 2049*0b57cec5SDimitry Andric // gcc forces the Quiet bit on, which means (float)(double)(float_sNan) 2050*0b57cec5SDimitry Andric // does not give you back the same bits. This is dubious, and we 2051*0b57cec5SDimitry Andric // don't currently do it. You're really supposed to get 2052*0b57cec5SDimitry Andric // an invalid operation signal at runtime, but nobody does that. 2053*0b57cec5SDimitry Andric fs = opOK; 2054*0b57cec5SDimitry Andric } else { 2055*0b57cec5SDimitry Andric *losesInfo = false; 2056*0b57cec5SDimitry Andric fs = opOK; 2057*0b57cec5SDimitry Andric } 2058*0b57cec5SDimitry Andric 2059*0b57cec5SDimitry Andric return fs; 2060*0b57cec5SDimitry Andric } 2061*0b57cec5SDimitry Andric 2062*0b57cec5SDimitry Andric /* Convert a floating point number to an integer according to the 2063*0b57cec5SDimitry Andric rounding mode. If the rounded integer value is out of range this 2064*0b57cec5SDimitry Andric returns an invalid operation exception and the contents of the 2065*0b57cec5SDimitry Andric destination parts are unspecified. If the rounded value is in 2066*0b57cec5SDimitry Andric range but the floating point number is not the exact integer, the C 2067*0b57cec5SDimitry Andric standard doesn't require an inexact exception to be raised. IEEE 2068*0b57cec5SDimitry Andric 854 does require it so we do that. 2069*0b57cec5SDimitry Andric 2070*0b57cec5SDimitry Andric Note that for conversions to integer type the C standard requires 2071*0b57cec5SDimitry Andric round-to-zero to always be used. */ 2072*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::convertToSignExtendedInteger( 2073*0b57cec5SDimitry Andric MutableArrayRef<integerPart> parts, unsigned int width, bool isSigned, 2074*0b57cec5SDimitry Andric roundingMode rounding_mode, bool *isExact) const { 2075*0b57cec5SDimitry Andric lostFraction lost_fraction; 2076*0b57cec5SDimitry Andric const integerPart *src; 2077*0b57cec5SDimitry Andric unsigned int dstPartsCount, truncatedBits; 2078*0b57cec5SDimitry Andric 2079*0b57cec5SDimitry Andric *isExact = false; 2080*0b57cec5SDimitry Andric 2081*0b57cec5SDimitry Andric /* Handle the three special cases first. */ 2082*0b57cec5SDimitry Andric if (category == fcInfinity || category == fcNaN) 2083*0b57cec5SDimitry Andric return opInvalidOp; 2084*0b57cec5SDimitry Andric 2085*0b57cec5SDimitry Andric dstPartsCount = partCountForBits(width); 2086*0b57cec5SDimitry Andric assert(dstPartsCount <= parts.size() && "Integer too big"); 2087*0b57cec5SDimitry Andric 2088*0b57cec5SDimitry Andric if (category == fcZero) { 2089*0b57cec5SDimitry Andric APInt::tcSet(parts.data(), 0, dstPartsCount); 2090*0b57cec5SDimitry Andric // Negative zero can't be represented as an int. 2091*0b57cec5SDimitry Andric *isExact = !sign; 2092*0b57cec5SDimitry Andric return opOK; 2093*0b57cec5SDimitry Andric } 2094*0b57cec5SDimitry Andric 2095*0b57cec5SDimitry Andric src = significandParts(); 2096*0b57cec5SDimitry Andric 2097*0b57cec5SDimitry Andric /* Step 1: place our absolute value, with any fraction truncated, in 2098*0b57cec5SDimitry Andric the destination. */ 2099*0b57cec5SDimitry Andric if (exponent < 0) { 2100*0b57cec5SDimitry Andric /* Our absolute value is less than one; truncate everything. */ 2101*0b57cec5SDimitry Andric APInt::tcSet(parts.data(), 0, dstPartsCount); 2102*0b57cec5SDimitry Andric /* For exponent -1 the integer bit represents .5, look at that. 2103*0b57cec5SDimitry Andric For smaller exponents leftmost truncated bit is 0. */ 2104*0b57cec5SDimitry Andric truncatedBits = semantics->precision -1U - exponent; 2105*0b57cec5SDimitry Andric } else { 2106*0b57cec5SDimitry Andric /* We want the most significant (exponent + 1) bits; the rest are 2107*0b57cec5SDimitry Andric truncated. */ 2108*0b57cec5SDimitry Andric unsigned int bits = exponent + 1U; 2109*0b57cec5SDimitry Andric 2110*0b57cec5SDimitry Andric /* Hopelessly large in magnitude? */ 2111*0b57cec5SDimitry Andric if (bits > width) 2112*0b57cec5SDimitry Andric return opInvalidOp; 2113*0b57cec5SDimitry Andric 2114*0b57cec5SDimitry Andric if (bits < semantics->precision) { 2115*0b57cec5SDimitry Andric /* We truncate (semantics->precision - bits) bits. */ 2116*0b57cec5SDimitry Andric truncatedBits = semantics->precision - bits; 2117*0b57cec5SDimitry Andric APInt::tcExtract(parts.data(), dstPartsCount, src, bits, truncatedBits); 2118*0b57cec5SDimitry Andric } else { 2119*0b57cec5SDimitry Andric /* We want at least as many bits as are available. */ 2120*0b57cec5SDimitry Andric APInt::tcExtract(parts.data(), dstPartsCount, src, semantics->precision, 2121*0b57cec5SDimitry Andric 0); 2122*0b57cec5SDimitry Andric APInt::tcShiftLeft(parts.data(), dstPartsCount, 2123*0b57cec5SDimitry Andric bits - semantics->precision); 2124*0b57cec5SDimitry Andric truncatedBits = 0; 2125*0b57cec5SDimitry Andric } 2126*0b57cec5SDimitry Andric } 2127*0b57cec5SDimitry Andric 2128*0b57cec5SDimitry Andric /* Step 2: work out any lost fraction, and increment the absolute 2129*0b57cec5SDimitry Andric value if we would round away from zero. */ 2130*0b57cec5SDimitry Andric if (truncatedBits) { 2131*0b57cec5SDimitry Andric lost_fraction = lostFractionThroughTruncation(src, partCount(), 2132*0b57cec5SDimitry Andric truncatedBits); 2133*0b57cec5SDimitry Andric if (lost_fraction != lfExactlyZero && 2134*0b57cec5SDimitry Andric roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) { 2135*0b57cec5SDimitry Andric if (APInt::tcIncrement(parts.data(), dstPartsCount)) 2136*0b57cec5SDimitry Andric return opInvalidOp; /* Overflow. */ 2137*0b57cec5SDimitry Andric } 2138*0b57cec5SDimitry Andric } else { 2139*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 2140*0b57cec5SDimitry Andric } 2141*0b57cec5SDimitry Andric 2142*0b57cec5SDimitry Andric /* Step 3: check if we fit in the destination. */ 2143*0b57cec5SDimitry Andric unsigned int omsb = APInt::tcMSB(parts.data(), dstPartsCount) + 1; 2144*0b57cec5SDimitry Andric 2145*0b57cec5SDimitry Andric if (sign) { 2146*0b57cec5SDimitry Andric if (!isSigned) { 2147*0b57cec5SDimitry Andric /* Negative numbers cannot be represented as unsigned. */ 2148*0b57cec5SDimitry Andric if (omsb != 0) 2149*0b57cec5SDimitry Andric return opInvalidOp; 2150*0b57cec5SDimitry Andric } else { 2151*0b57cec5SDimitry Andric /* It takes omsb bits to represent the unsigned integer value. 2152*0b57cec5SDimitry Andric We lose a bit for the sign, but care is needed as the 2153*0b57cec5SDimitry Andric maximally negative integer is a special case. */ 2154*0b57cec5SDimitry Andric if (omsb == width && 2155*0b57cec5SDimitry Andric APInt::tcLSB(parts.data(), dstPartsCount) + 1 != omsb) 2156*0b57cec5SDimitry Andric return opInvalidOp; 2157*0b57cec5SDimitry Andric 2158*0b57cec5SDimitry Andric /* This case can happen because of rounding. */ 2159*0b57cec5SDimitry Andric if (omsb > width) 2160*0b57cec5SDimitry Andric return opInvalidOp; 2161*0b57cec5SDimitry Andric } 2162*0b57cec5SDimitry Andric 2163*0b57cec5SDimitry Andric APInt::tcNegate (parts.data(), dstPartsCount); 2164*0b57cec5SDimitry Andric } else { 2165*0b57cec5SDimitry Andric if (omsb >= width + !isSigned) 2166*0b57cec5SDimitry Andric return opInvalidOp; 2167*0b57cec5SDimitry Andric } 2168*0b57cec5SDimitry Andric 2169*0b57cec5SDimitry Andric if (lost_fraction == lfExactlyZero) { 2170*0b57cec5SDimitry Andric *isExact = true; 2171*0b57cec5SDimitry Andric return opOK; 2172*0b57cec5SDimitry Andric } else 2173*0b57cec5SDimitry Andric return opInexact; 2174*0b57cec5SDimitry Andric } 2175*0b57cec5SDimitry Andric 2176*0b57cec5SDimitry Andric /* Same as convertToSignExtendedInteger, except we provide 2177*0b57cec5SDimitry Andric deterministic values in case of an invalid operation exception, 2178*0b57cec5SDimitry Andric namely zero for NaNs and the minimal or maximal value respectively 2179*0b57cec5SDimitry Andric for underflow or overflow. 2180*0b57cec5SDimitry Andric The *isExact output tells whether the result is exact, in the sense 2181*0b57cec5SDimitry Andric that converting it back to the original floating point type produces 2182*0b57cec5SDimitry Andric the original value. This is almost equivalent to result==opOK, 2183*0b57cec5SDimitry Andric except for negative zeroes. 2184*0b57cec5SDimitry Andric */ 2185*0b57cec5SDimitry Andric IEEEFloat::opStatus 2186*0b57cec5SDimitry Andric IEEEFloat::convertToInteger(MutableArrayRef<integerPart> parts, 2187*0b57cec5SDimitry Andric unsigned int width, bool isSigned, 2188*0b57cec5SDimitry Andric roundingMode rounding_mode, bool *isExact) const { 2189*0b57cec5SDimitry Andric opStatus fs; 2190*0b57cec5SDimitry Andric 2191*0b57cec5SDimitry Andric fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode, 2192*0b57cec5SDimitry Andric isExact); 2193*0b57cec5SDimitry Andric 2194*0b57cec5SDimitry Andric if (fs == opInvalidOp) { 2195*0b57cec5SDimitry Andric unsigned int bits, dstPartsCount; 2196*0b57cec5SDimitry Andric 2197*0b57cec5SDimitry Andric dstPartsCount = partCountForBits(width); 2198*0b57cec5SDimitry Andric assert(dstPartsCount <= parts.size() && "Integer too big"); 2199*0b57cec5SDimitry Andric 2200*0b57cec5SDimitry Andric if (category == fcNaN) 2201*0b57cec5SDimitry Andric bits = 0; 2202*0b57cec5SDimitry Andric else if (sign) 2203*0b57cec5SDimitry Andric bits = isSigned; 2204*0b57cec5SDimitry Andric else 2205*0b57cec5SDimitry Andric bits = width - isSigned; 2206*0b57cec5SDimitry Andric 2207*0b57cec5SDimitry Andric APInt::tcSetLeastSignificantBits(parts.data(), dstPartsCount, bits); 2208*0b57cec5SDimitry Andric if (sign && isSigned) 2209*0b57cec5SDimitry Andric APInt::tcShiftLeft(parts.data(), dstPartsCount, width - 1); 2210*0b57cec5SDimitry Andric } 2211*0b57cec5SDimitry Andric 2212*0b57cec5SDimitry Andric return fs; 2213*0b57cec5SDimitry Andric } 2214*0b57cec5SDimitry Andric 2215*0b57cec5SDimitry Andric /* Convert an unsigned integer SRC to a floating point number, 2216*0b57cec5SDimitry Andric rounding according to ROUNDING_MODE. The sign of the floating 2217*0b57cec5SDimitry Andric point number is not modified. */ 2218*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::convertFromUnsignedParts( 2219*0b57cec5SDimitry Andric const integerPart *src, unsigned int srcCount, roundingMode rounding_mode) { 2220*0b57cec5SDimitry Andric unsigned int omsb, precision, dstCount; 2221*0b57cec5SDimitry Andric integerPart *dst; 2222*0b57cec5SDimitry Andric lostFraction lost_fraction; 2223*0b57cec5SDimitry Andric 2224*0b57cec5SDimitry Andric category = fcNormal; 2225*0b57cec5SDimitry Andric omsb = APInt::tcMSB(src, srcCount) + 1; 2226*0b57cec5SDimitry Andric dst = significandParts(); 2227*0b57cec5SDimitry Andric dstCount = partCount(); 2228*0b57cec5SDimitry Andric precision = semantics->precision; 2229*0b57cec5SDimitry Andric 2230*0b57cec5SDimitry Andric /* We want the most significant PRECISION bits of SRC. There may not 2231*0b57cec5SDimitry Andric be that many; extract what we can. */ 2232*0b57cec5SDimitry Andric if (precision <= omsb) { 2233*0b57cec5SDimitry Andric exponent = omsb - 1; 2234*0b57cec5SDimitry Andric lost_fraction = lostFractionThroughTruncation(src, srcCount, 2235*0b57cec5SDimitry Andric omsb - precision); 2236*0b57cec5SDimitry Andric APInt::tcExtract(dst, dstCount, src, precision, omsb - precision); 2237*0b57cec5SDimitry Andric } else { 2238*0b57cec5SDimitry Andric exponent = precision - 1; 2239*0b57cec5SDimitry Andric lost_fraction = lfExactlyZero; 2240*0b57cec5SDimitry Andric APInt::tcExtract(dst, dstCount, src, omsb, 0); 2241*0b57cec5SDimitry Andric } 2242*0b57cec5SDimitry Andric 2243*0b57cec5SDimitry Andric return normalize(rounding_mode, lost_fraction); 2244*0b57cec5SDimitry Andric } 2245*0b57cec5SDimitry Andric 2246*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::convertFromAPInt(const APInt &Val, bool isSigned, 2247*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2248*0b57cec5SDimitry Andric unsigned int partCount = Val.getNumWords(); 2249*0b57cec5SDimitry Andric APInt api = Val; 2250*0b57cec5SDimitry Andric 2251*0b57cec5SDimitry Andric sign = false; 2252*0b57cec5SDimitry Andric if (isSigned && api.isNegative()) { 2253*0b57cec5SDimitry Andric sign = true; 2254*0b57cec5SDimitry Andric api = -api; 2255*0b57cec5SDimitry Andric } 2256*0b57cec5SDimitry Andric 2257*0b57cec5SDimitry Andric return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); 2258*0b57cec5SDimitry Andric } 2259*0b57cec5SDimitry Andric 2260*0b57cec5SDimitry Andric /* Convert a two's complement integer SRC to a floating point number, 2261*0b57cec5SDimitry Andric rounding according to ROUNDING_MODE. ISSIGNED is true if the 2262*0b57cec5SDimitry Andric integer is signed, in which case it must be sign-extended. */ 2263*0b57cec5SDimitry Andric IEEEFloat::opStatus 2264*0b57cec5SDimitry Andric IEEEFloat::convertFromSignExtendedInteger(const integerPart *src, 2265*0b57cec5SDimitry Andric unsigned int srcCount, bool isSigned, 2266*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2267*0b57cec5SDimitry Andric opStatus status; 2268*0b57cec5SDimitry Andric 2269*0b57cec5SDimitry Andric if (isSigned && 2270*0b57cec5SDimitry Andric APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) { 2271*0b57cec5SDimitry Andric integerPart *copy; 2272*0b57cec5SDimitry Andric 2273*0b57cec5SDimitry Andric /* If we're signed and negative negate a copy. */ 2274*0b57cec5SDimitry Andric sign = true; 2275*0b57cec5SDimitry Andric copy = new integerPart[srcCount]; 2276*0b57cec5SDimitry Andric APInt::tcAssign(copy, src, srcCount); 2277*0b57cec5SDimitry Andric APInt::tcNegate(copy, srcCount); 2278*0b57cec5SDimitry Andric status = convertFromUnsignedParts(copy, srcCount, rounding_mode); 2279*0b57cec5SDimitry Andric delete [] copy; 2280*0b57cec5SDimitry Andric } else { 2281*0b57cec5SDimitry Andric sign = false; 2282*0b57cec5SDimitry Andric status = convertFromUnsignedParts(src, srcCount, rounding_mode); 2283*0b57cec5SDimitry Andric } 2284*0b57cec5SDimitry Andric 2285*0b57cec5SDimitry Andric return status; 2286*0b57cec5SDimitry Andric } 2287*0b57cec5SDimitry Andric 2288*0b57cec5SDimitry Andric /* FIXME: should this just take a const APInt reference? */ 2289*0b57cec5SDimitry Andric IEEEFloat::opStatus 2290*0b57cec5SDimitry Andric IEEEFloat::convertFromZeroExtendedInteger(const integerPart *parts, 2291*0b57cec5SDimitry Andric unsigned int width, bool isSigned, 2292*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2293*0b57cec5SDimitry Andric unsigned int partCount = partCountForBits(width); 2294*0b57cec5SDimitry Andric APInt api = APInt(width, makeArrayRef(parts, partCount)); 2295*0b57cec5SDimitry Andric 2296*0b57cec5SDimitry Andric sign = false; 2297*0b57cec5SDimitry Andric if (isSigned && APInt::tcExtractBit(parts, width - 1)) { 2298*0b57cec5SDimitry Andric sign = true; 2299*0b57cec5SDimitry Andric api = -api; 2300*0b57cec5SDimitry Andric } 2301*0b57cec5SDimitry Andric 2302*0b57cec5SDimitry Andric return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); 2303*0b57cec5SDimitry Andric } 2304*0b57cec5SDimitry Andric 2305*0b57cec5SDimitry Andric IEEEFloat::opStatus 2306*0b57cec5SDimitry Andric IEEEFloat::convertFromHexadecimalString(StringRef s, 2307*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2308*0b57cec5SDimitry Andric lostFraction lost_fraction = lfExactlyZero; 2309*0b57cec5SDimitry Andric 2310*0b57cec5SDimitry Andric category = fcNormal; 2311*0b57cec5SDimitry Andric zeroSignificand(); 2312*0b57cec5SDimitry Andric exponent = 0; 2313*0b57cec5SDimitry Andric 2314*0b57cec5SDimitry Andric integerPart *significand = significandParts(); 2315*0b57cec5SDimitry Andric unsigned partsCount = partCount(); 2316*0b57cec5SDimitry Andric unsigned bitPos = partsCount * integerPartWidth; 2317*0b57cec5SDimitry Andric bool computedTrailingFraction = false; 2318*0b57cec5SDimitry Andric 2319*0b57cec5SDimitry Andric // Skip leading zeroes and any (hexa)decimal point. 2320*0b57cec5SDimitry Andric StringRef::iterator begin = s.begin(); 2321*0b57cec5SDimitry Andric StringRef::iterator end = s.end(); 2322*0b57cec5SDimitry Andric StringRef::iterator dot; 2323*0b57cec5SDimitry Andric StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot); 2324*0b57cec5SDimitry Andric StringRef::iterator firstSignificantDigit = p; 2325*0b57cec5SDimitry Andric 2326*0b57cec5SDimitry Andric while (p != end) { 2327*0b57cec5SDimitry Andric integerPart hex_value; 2328*0b57cec5SDimitry Andric 2329*0b57cec5SDimitry Andric if (*p == '.') { 2330*0b57cec5SDimitry Andric assert(dot == end && "String contains multiple dots"); 2331*0b57cec5SDimitry Andric dot = p++; 2332*0b57cec5SDimitry Andric continue; 2333*0b57cec5SDimitry Andric } 2334*0b57cec5SDimitry Andric 2335*0b57cec5SDimitry Andric hex_value = hexDigitValue(*p); 2336*0b57cec5SDimitry Andric if (hex_value == -1U) 2337*0b57cec5SDimitry Andric break; 2338*0b57cec5SDimitry Andric 2339*0b57cec5SDimitry Andric p++; 2340*0b57cec5SDimitry Andric 2341*0b57cec5SDimitry Andric // Store the number while we have space. 2342*0b57cec5SDimitry Andric if (bitPos) { 2343*0b57cec5SDimitry Andric bitPos -= 4; 2344*0b57cec5SDimitry Andric hex_value <<= bitPos % integerPartWidth; 2345*0b57cec5SDimitry Andric significand[bitPos / integerPartWidth] |= hex_value; 2346*0b57cec5SDimitry Andric } else if (!computedTrailingFraction) { 2347*0b57cec5SDimitry Andric lost_fraction = trailingHexadecimalFraction(p, end, hex_value); 2348*0b57cec5SDimitry Andric computedTrailingFraction = true; 2349*0b57cec5SDimitry Andric } 2350*0b57cec5SDimitry Andric } 2351*0b57cec5SDimitry Andric 2352*0b57cec5SDimitry Andric /* Hex floats require an exponent but not a hexadecimal point. */ 2353*0b57cec5SDimitry Andric assert(p != end && "Hex strings require an exponent"); 2354*0b57cec5SDimitry Andric assert((*p == 'p' || *p == 'P') && "Invalid character in significand"); 2355*0b57cec5SDimitry Andric assert(p != begin && "Significand has no digits"); 2356*0b57cec5SDimitry Andric assert((dot == end || p - begin != 1) && "Significand has no digits"); 2357*0b57cec5SDimitry Andric 2358*0b57cec5SDimitry Andric /* Ignore the exponent if we are zero. */ 2359*0b57cec5SDimitry Andric if (p != firstSignificantDigit) { 2360*0b57cec5SDimitry Andric int expAdjustment; 2361*0b57cec5SDimitry Andric 2362*0b57cec5SDimitry Andric /* Implicit hexadecimal point? */ 2363*0b57cec5SDimitry Andric if (dot == end) 2364*0b57cec5SDimitry Andric dot = p; 2365*0b57cec5SDimitry Andric 2366*0b57cec5SDimitry Andric /* Calculate the exponent adjustment implicit in the number of 2367*0b57cec5SDimitry Andric significant digits. */ 2368*0b57cec5SDimitry Andric expAdjustment = static_cast<int>(dot - firstSignificantDigit); 2369*0b57cec5SDimitry Andric if (expAdjustment < 0) 2370*0b57cec5SDimitry Andric expAdjustment++; 2371*0b57cec5SDimitry Andric expAdjustment = expAdjustment * 4 - 1; 2372*0b57cec5SDimitry Andric 2373*0b57cec5SDimitry Andric /* Adjust for writing the significand starting at the most 2374*0b57cec5SDimitry Andric significant nibble. */ 2375*0b57cec5SDimitry Andric expAdjustment += semantics->precision; 2376*0b57cec5SDimitry Andric expAdjustment -= partsCount * integerPartWidth; 2377*0b57cec5SDimitry Andric 2378*0b57cec5SDimitry Andric /* Adjust for the given exponent. */ 2379*0b57cec5SDimitry Andric exponent = totalExponent(p + 1, end, expAdjustment); 2380*0b57cec5SDimitry Andric } 2381*0b57cec5SDimitry Andric 2382*0b57cec5SDimitry Andric return normalize(rounding_mode, lost_fraction); 2383*0b57cec5SDimitry Andric } 2384*0b57cec5SDimitry Andric 2385*0b57cec5SDimitry Andric IEEEFloat::opStatus 2386*0b57cec5SDimitry Andric IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts, 2387*0b57cec5SDimitry Andric unsigned sigPartCount, int exp, 2388*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2389*0b57cec5SDimitry Andric unsigned int parts, pow5PartCount; 2390*0b57cec5SDimitry Andric fltSemantics calcSemantics = { 32767, -32767, 0, 0 }; 2391*0b57cec5SDimitry Andric integerPart pow5Parts[maxPowerOfFiveParts]; 2392*0b57cec5SDimitry Andric bool isNearest; 2393*0b57cec5SDimitry Andric 2394*0b57cec5SDimitry Andric isNearest = (rounding_mode == rmNearestTiesToEven || 2395*0b57cec5SDimitry Andric rounding_mode == rmNearestTiesToAway); 2396*0b57cec5SDimitry Andric 2397*0b57cec5SDimitry Andric parts = partCountForBits(semantics->precision + 11); 2398*0b57cec5SDimitry Andric 2399*0b57cec5SDimitry Andric /* Calculate pow(5, abs(exp)). */ 2400*0b57cec5SDimitry Andric pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp); 2401*0b57cec5SDimitry Andric 2402*0b57cec5SDimitry Andric for (;; parts *= 2) { 2403*0b57cec5SDimitry Andric opStatus sigStatus, powStatus; 2404*0b57cec5SDimitry Andric unsigned int excessPrecision, truncatedBits; 2405*0b57cec5SDimitry Andric 2406*0b57cec5SDimitry Andric calcSemantics.precision = parts * integerPartWidth - 1; 2407*0b57cec5SDimitry Andric excessPrecision = calcSemantics.precision - semantics->precision; 2408*0b57cec5SDimitry Andric truncatedBits = excessPrecision; 2409*0b57cec5SDimitry Andric 2410*0b57cec5SDimitry Andric IEEEFloat decSig(calcSemantics, uninitialized); 2411*0b57cec5SDimitry Andric decSig.makeZero(sign); 2412*0b57cec5SDimitry Andric IEEEFloat pow5(calcSemantics); 2413*0b57cec5SDimitry Andric 2414*0b57cec5SDimitry Andric sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount, 2415*0b57cec5SDimitry Andric rmNearestTiesToEven); 2416*0b57cec5SDimitry Andric powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount, 2417*0b57cec5SDimitry Andric rmNearestTiesToEven); 2418*0b57cec5SDimitry Andric /* Add exp, as 10^n = 5^n * 2^n. */ 2419*0b57cec5SDimitry Andric decSig.exponent += exp; 2420*0b57cec5SDimitry Andric 2421*0b57cec5SDimitry Andric lostFraction calcLostFraction; 2422*0b57cec5SDimitry Andric integerPart HUerr, HUdistance; 2423*0b57cec5SDimitry Andric unsigned int powHUerr; 2424*0b57cec5SDimitry Andric 2425*0b57cec5SDimitry Andric if (exp >= 0) { 2426*0b57cec5SDimitry Andric /* multiplySignificand leaves the precision-th bit set to 1. */ 2427*0b57cec5SDimitry Andric calcLostFraction = decSig.multiplySignificand(pow5, nullptr); 2428*0b57cec5SDimitry Andric powHUerr = powStatus != opOK; 2429*0b57cec5SDimitry Andric } else { 2430*0b57cec5SDimitry Andric calcLostFraction = decSig.divideSignificand(pow5); 2431*0b57cec5SDimitry Andric /* Denormal numbers have less precision. */ 2432*0b57cec5SDimitry Andric if (decSig.exponent < semantics->minExponent) { 2433*0b57cec5SDimitry Andric excessPrecision += (semantics->minExponent - decSig.exponent); 2434*0b57cec5SDimitry Andric truncatedBits = excessPrecision; 2435*0b57cec5SDimitry Andric if (excessPrecision > calcSemantics.precision) 2436*0b57cec5SDimitry Andric excessPrecision = calcSemantics.precision; 2437*0b57cec5SDimitry Andric } 2438*0b57cec5SDimitry Andric /* Extra half-ulp lost in reciprocal of exponent. */ 2439*0b57cec5SDimitry Andric powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2; 2440*0b57cec5SDimitry Andric } 2441*0b57cec5SDimitry Andric 2442*0b57cec5SDimitry Andric /* Both multiplySignificand and divideSignificand return the 2443*0b57cec5SDimitry Andric result with the integer bit set. */ 2444*0b57cec5SDimitry Andric assert(APInt::tcExtractBit 2445*0b57cec5SDimitry Andric (decSig.significandParts(), calcSemantics.precision - 1) == 1); 2446*0b57cec5SDimitry Andric 2447*0b57cec5SDimitry Andric HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK, 2448*0b57cec5SDimitry Andric powHUerr); 2449*0b57cec5SDimitry Andric HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(), 2450*0b57cec5SDimitry Andric excessPrecision, isNearest); 2451*0b57cec5SDimitry Andric 2452*0b57cec5SDimitry Andric /* Are we guaranteed to round correctly if we truncate? */ 2453*0b57cec5SDimitry Andric if (HUdistance >= HUerr) { 2454*0b57cec5SDimitry Andric APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(), 2455*0b57cec5SDimitry Andric calcSemantics.precision - excessPrecision, 2456*0b57cec5SDimitry Andric excessPrecision); 2457*0b57cec5SDimitry Andric /* Take the exponent of decSig. If we tcExtract-ed less bits 2458*0b57cec5SDimitry Andric above we must adjust our exponent to compensate for the 2459*0b57cec5SDimitry Andric implicit right shift. */ 2460*0b57cec5SDimitry Andric exponent = (decSig.exponent + semantics->precision 2461*0b57cec5SDimitry Andric - (calcSemantics.precision - excessPrecision)); 2462*0b57cec5SDimitry Andric calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(), 2463*0b57cec5SDimitry Andric decSig.partCount(), 2464*0b57cec5SDimitry Andric truncatedBits); 2465*0b57cec5SDimitry Andric return normalize(rounding_mode, calcLostFraction); 2466*0b57cec5SDimitry Andric } 2467*0b57cec5SDimitry Andric } 2468*0b57cec5SDimitry Andric } 2469*0b57cec5SDimitry Andric 2470*0b57cec5SDimitry Andric IEEEFloat::opStatus 2471*0b57cec5SDimitry Andric IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) { 2472*0b57cec5SDimitry Andric decimalInfo D; 2473*0b57cec5SDimitry Andric opStatus fs; 2474*0b57cec5SDimitry Andric 2475*0b57cec5SDimitry Andric /* Scan the text. */ 2476*0b57cec5SDimitry Andric StringRef::iterator p = str.begin(); 2477*0b57cec5SDimitry Andric interpretDecimal(p, str.end(), &D); 2478*0b57cec5SDimitry Andric 2479*0b57cec5SDimitry Andric /* Handle the quick cases. First the case of no significant digits, 2480*0b57cec5SDimitry Andric i.e. zero, and then exponents that are obviously too large or too 2481*0b57cec5SDimitry Andric small. Writing L for log 10 / log 2, a number d.ddddd*10^exp 2482*0b57cec5SDimitry Andric definitely overflows if 2483*0b57cec5SDimitry Andric 2484*0b57cec5SDimitry Andric (exp - 1) * L >= maxExponent 2485*0b57cec5SDimitry Andric 2486*0b57cec5SDimitry Andric and definitely underflows to zero where 2487*0b57cec5SDimitry Andric 2488*0b57cec5SDimitry Andric (exp + 1) * L <= minExponent - precision 2489*0b57cec5SDimitry Andric 2490*0b57cec5SDimitry Andric With integer arithmetic the tightest bounds for L are 2491*0b57cec5SDimitry Andric 2492*0b57cec5SDimitry Andric 93/28 < L < 196/59 [ numerator <= 256 ] 2493*0b57cec5SDimitry Andric 42039/12655 < L < 28738/8651 [ numerator <= 65536 ] 2494*0b57cec5SDimitry Andric */ 2495*0b57cec5SDimitry Andric 2496*0b57cec5SDimitry Andric // Test if we have a zero number allowing for strings with no null terminators 2497*0b57cec5SDimitry Andric // and zero decimals with non-zero exponents. 2498*0b57cec5SDimitry Andric // 2499*0b57cec5SDimitry Andric // We computed firstSigDigit by ignoring all zeros and dots. Thus if 2500*0b57cec5SDimitry Andric // D->firstSigDigit equals str.end(), every digit must be a zero and there can 2501*0b57cec5SDimitry Andric // be at most one dot. On the other hand, if we have a zero with a non-zero 2502*0b57cec5SDimitry Andric // exponent, then we know that D.firstSigDigit will be non-numeric. 2503*0b57cec5SDimitry Andric if (D.firstSigDigit == str.end() || decDigitValue(*D.firstSigDigit) >= 10U) { 2504*0b57cec5SDimitry Andric category = fcZero; 2505*0b57cec5SDimitry Andric fs = opOK; 2506*0b57cec5SDimitry Andric 2507*0b57cec5SDimitry Andric /* Check whether the normalized exponent is high enough to overflow 2508*0b57cec5SDimitry Andric max during the log-rebasing in the max-exponent check below. */ 2509*0b57cec5SDimitry Andric } else if (D.normalizedExponent - 1 > INT_MAX / 42039) { 2510*0b57cec5SDimitry Andric fs = handleOverflow(rounding_mode); 2511*0b57cec5SDimitry Andric 2512*0b57cec5SDimitry Andric /* If it wasn't, then it also wasn't high enough to overflow max 2513*0b57cec5SDimitry Andric during the log-rebasing in the min-exponent check. Check that it 2514*0b57cec5SDimitry Andric won't overflow min in either check, then perform the min-exponent 2515*0b57cec5SDimitry Andric check. */ 2516*0b57cec5SDimitry Andric } else if (D.normalizedExponent - 1 < INT_MIN / 42039 || 2517*0b57cec5SDimitry Andric (D.normalizedExponent + 1) * 28738 <= 2518*0b57cec5SDimitry Andric 8651 * (semantics->minExponent - (int) semantics->precision)) { 2519*0b57cec5SDimitry Andric /* Underflow to zero and round. */ 2520*0b57cec5SDimitry Andric category = fcNormal; 2521*0b57cec5SDimitry Andric zeroSignificand(); 2522*0b57cec5SDimitry Andric fs = normalize(rounding_mode, lfLessThanHalf); 2523*0b57cec5SDimitry Andric 2524*0b57cec5SDimitry Andric /* We can finally safely perform the max-exponent check. */ 2525*0b57cec5SDimitry Andric } else if ((D.normalizedExponent - 1) * 42039 2526*0b57cec5SDimitry Andric >= 12655 * semantics->maxExponent) { 2527*0b57cec5SDimitry Andric /* Overflow and round. */ 2528*0b57cec5SDimitry Andric fs = handleOverflow(rounding_mode); 2529*0b57cec5SDimitry Andric } else { 2530*0b57cec5SDimitry Andric integerPart *decSignificand; 2531*0b57cec5SDimitry Andric unsigned int partCount; 2532*0b57cec5SDimitry Andric 2533*0b57cec5SDimitry Andric /* A tight upper bound on number of bits required to hold an 2534*0b57cec5SDimitry Andric N-digit decimal integer is N * 196 / 59. Allocate enough space 2535*0b57cec5SDimitry Andric to hold the full significand, and an extra part required by 2536*0b57cec5SDimitry Andric tcMultiplyPart. */ 2537*0b57cec5SDimitry Andric partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1; 2538*0b57cec5SDimitry Andric partCount = partCountForBits(1 + 196 * partCount / 59); 2539*0b57cec5SDimitry Andric decSignificand = new integerPart[partCount + 1]; 2540*0b57cec5SDimitry Andric partCount = 0; 2541*0b57cec5SDimitry Andric 2542*0b57cec5SDimitry Andric /* Convert to binary efficiently - we do almost all multiplication 2543*0b57cec5SDimitry Andric in an integerPart. When this would overflow do we do a single 2544*0b57cec5SDimitry Andric bignum multiplication, and then revert again to multiplication 2545*0b57cec5SDimitry Andric in an integerPart. */ 2546*0b57cec5SDimitry Andric do { 2547*0b57cec5SDimitry Andric integerPart decValue, val, multiplier; 2548*0b57cec5SDimitry Andric 2549*0b57cec5SDimitry Andric val = 0; 2550*0b57cec5SDimitry Andric multiplier = 1; 2551*0b57cec5SDimitry Andric 2552*0b57cec5SDimitry Andric do { 2553*0b57cec5SDimitry Andric if (*p == '.') { 2554*0b57cec5SDimitry Andric p++; 2555*0b57cec5SDimitry Andric if (p == str.end()) { 2556*0b57cec5SDimitry Andric break; 2557*0b57cec5SDimitry Andric } 2558*0b57cec5SDimitry Andric } 2559*0b57cec5SDimitry Andric decValue = decDigitValue(*p++); 2560*0b57cec5SDimitry Andric assert(decValue < 10U && "Invalid character in significand"); 2561*0b57cec5SDimitry Andric multiplier *= 10; 2562*0b57cec5SDimitry Andric val = val * 10 + decValue; 2563*0b57cec5SDimitry Andric /* The maximum number that can be multiplied by ten with any 2564*0b57cec5SDimitry Andric digit added without overflowing an integerPart. */ 2565*0b57cec5SDimitry Andric } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10); 2566*0b57cec5SDimitry Andric 2567*0b57cec5SDimitry Andric /* Multiply out the current part. */ 2568*0b57cec5SDimitry Andric APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val, 2569*0b57cec5SDimitry Andric partCount, partCount + 1, false); 2570*0b57cec5SDimitry Andric 2571*0b57cec5SDimitry Andric /* If we used another part (likely but not guaranteed), increase 2572*0b57cec5SDimitry Andric the count. */ 2573*0b57cec5SDimitry Andric if (decSignificand[partCount]) 2574*0b57cec5SDimitry Andric partCount++; 2575*0b57cec5SDimitry Andric } while (p <= D.lastSigDigit); 2576*0b57cec5SDimitry Andric 2577*0b57cec5SDimitry Andric category = fcNormal; 2578*0b57cec5SDimitry Andric fs = roundSignificandWithExponent(decSignificand, partCount, 2579*0b57cec5SDimitry Andric D.exponent, rounding_mode); 2580*0b57cec5SDimitry Andric 2581*0b57cec5SDimitry Andric delete [] decSignificand; 2582*0b57cec5SDimitry Andric } 2583*0b57cec5SDimitry Andric 2584*0b57cec5SDimitry Andric return fs; 2585*0b57cec5SDimitry Andric } 2586*0b57cec5SDimitry Andric 2587*0b57cec5SDimitry Andric bool IEEEFloat::convertFromStringSpecials(StringRef str) { 2588*0b57cec5SDimitry Andric if (str.equals("inf") || str.equals("INFINITY") || str.equals("+Inf")) { 2589*0b57cec5SDimitry Andric makeInf(false); 2590*0b57cec5SDimitry Andric return true; 2591*0b57cec5SDimitry Andric } 2592*0b57cec5SDimitry Andric 2593*0b57cec5SDimitry Andric if (str.equals("-inf") || str.equals("-INFINITY") || str.equals("-Inf")) { 2594*0b57cec5SDimitry Andric makeInf(true); 2595*0b57cec5SDimitry Andric return true; 2596*0b57cec5SDimitry Andric } 2597*0b57cec5SDimitry Andric 2598*0b57cec5SDimitry Andric if (str.equals("nan") || str.equals("NaN")) { 2599*0b57cec5SDimitry Andric makeNaN(false, false); 2600*0b57cec5SDimitry Andric return true; 2601*0b57cec5SDimitry Andric } 2602*0b57cec5SDimitry Andric 2603*0b57cec5SDimitry Andric if (str.equals("-nan") || str.equals("-NaN")) { 2604*0b57cec5SDimitry Andric makeNaN(false, true); 2605*0b57cec5SDimitry Andric return true; 2606*0b57cec5SDimitry Andric } 2607*0b57cec5SDimitry Andric 2608*0b57cec5SDimitry Andric return false; 2609*0b57cec5SDimitry Andric } 2610*0b57cec5SDimitry Andric 2611*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::convertFromString(StringRef str, 2612*0b57cec5SDimitry Andric roundingMode rounding_mode) { 2613*0b57cec5SDimitry Andric assert(!str.empty() && "Invalid string length"); 2614*0b57cec5SDimitry Andric 2615*0b57cec5SDimitry Andric // Handle special cases. 2616*0b57cec5SDimitry Andric if (convertFromStringSpecials(str)) 2617*0b57cec5SDimitry Andric return opOK; 2618*0b57cec5SDimitry Andric 2619*0b57cec5SDimitry Andric /* Handle a leading minus sign. */ 2620*0b57cec5SDimitry Andric StringRef::iterator p = str.begin(); 2621*0b57cec5SDimitry Andric size_t slen = str.size(); 2622*0b57cec5SDimitry Andric sign = *p == '-' ? 1 : 0; 2623*0b57cec5SDimitry Andric if (*p == '-' || *p == '+') { 2624*0b57cec5SDimitry Andric p++; 2625*0b57cec5SDimitry Andric slen--; 2626*0b57cec5SDimitry Andric assert(slen && "String has no digits"); 2627*0b57cec5SDimitry Andric } 2628*0b57cec5SDimitry Andric 2629*0b57cec5SDimitry Andric if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 2630*0b57cec5SDimitry Andric assert(slen - 2 && "Invalid string"); 2631*0b57cec5SDimitry Andric return convertFromHexadecimalString(StringRef(p + 2, slen - 2), 2632*0b57cec5SDimitry Andric rounding_mode); 2633*0b57cec5SDimitry Andric } 2634*0b57cec5SDimitry Andric 2635*0b57cec5SDimitry Andric return convertFromDecimalString(StringRef(p, slen), rounding_mode); 2636*0b57cec5SDimitry Andric } 2637*0b57cec5SDimitry Andric 2638*0b57cec5SDimitry Andric /* Write out a hexadecimal representation of the floating point value 2639*0b57cec5SDimitry Andric to DST, which must be of sufficient size, in the C99 form 2640*0b57cec5SDimitry Andric [-]0xh.hhhhp[+-]d. Return the number of characters written, 2641*0b57cec5SDimitry Andric excluding the terminating NUL. 2642*0b57cec5SDimitry Andric 2643*0b57cec5SDimitry Andric If UPPERCASE, the output is in upper case, otherwise in lower case. 2644*0b57cec5SDimitry Andric 2645*0b57cec5SDimitry Andric HEXDIGITS digits appear altogether, rounding the value if 2646*0b57cec5SDimitry Andric necessary. If HEXDIGITS is 0, the minimal precision to display the 2647*0b57cec5SDimitry Andric number precisely is used instead. If nothing would appear after 2648*0b57cec5SDimitry Andric the decimal point it is suppressed. 2649*0b57cec5SDimitry Andric 2650*0b57cec5SDimitry Andric The decimal exponent is always printed and has at least one digit. 2651*0b57cec5SDimitry Andric Zero values display an exponent of zero. Infinities and NaNs 2652*0b57cec5SDimitry Andric appear as "infinity" or "nan" respectively. 2653*0b57cec5SDimitry Andric 2654*0b57cec5SDimitry Andric The above rules are as specified by C99. There is ambiguity about 2655*0b57cec5SDimitry Andric what the leading hexadecimal digit should be. This implementation 2656*0b57cec5SDimitry Andric uses whatever is necessary so that the exponent is displayed as 2657*0b57cec5SDimitry Andric stored. This implies the exponent will fall within the IEEE format 2658*0b57cec5SDimitry Andric range, and the leading hexadecimal digit will be 0 (for denormals), 2659*0b57cec5SDimitry Andric 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with 2660*0b57cec5SDimitry Andric any other digits zero). 2661*0b57cec5SDimitry Andric */ 2662*0b57cec5SDimitry Andric unsigned int IEEEFloat::convertToHexString(char *dst, unsigned int hexDigits, 2663*0b57cec5SDimitry Andric bool upperCase, 2664*0b57cec5SDimitry Andric roundingMode rounding_mode) const { 2665*0b57cec5SDimitry Andric char *p; 2666*0b57cec5SDimitry Andric 2667*0b57cec5SDimitry Andric p = dst; 2668*0b57cec5SDimitry Andric if (sign) 2669*0b57cec5SDimitry Andric *dst++ = '-'; 2670*0b57cec5SDimitry Andric 2671*0b57cec5SDimitry Andric switch (category) { 2672*0b57cec5SDimitry Andric case fcInfinity: 2673*0b57cec5SDimitry Andric memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1); 2674*0b57cec5SDimitry Andric dst += sizeof infinityL - 1; 2675*0b57cec5SDimitry Andric break; 2676*0b57cec5SDimitry Andric 2677*0b57cec5SDimitry Andric case fcNaN: 2678*0b57cec5SDimitry Andric memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1); 2679*0b57cec5SDimitry Andric dst += sizeof NaNU - 1; 2680*0b57cec5SDimitry Andric break; 2681*0b57cec5SDimitry Andric 2682*0b57cec5SDimitry Andric case fcZero: 2683*0b57cec5SDimitry Andric *dst++ = '0'; 2684*0b57cec5SDimitry Andric *dst++ = upperCase ? 'X': 'x'; 2685*0b57cec5SDimitry Andric *dst++ = '0'; 2686*0b57cec5SDimitry Andric if (hexDigits > 1) { 2687*0b57cec5SDimitry Andric *dst++ = '.'; 2688*0b57cec5SDimitry Andric memset (dst, '0', hexDigits - 1); 2689*0b57cec5SDimitry Andric dst += hexDigits - 1; 2690*0b57cec5SDimitry Andric } 2691*0b57cec5SDimitry Andric *dst++ = upperCase ? 'P': 'p'; 2692*0b57cec5SDimitry Andric *dst++ = '0'; 2693*0b57cec5SDimitry Andric break; 2694*0b57cec5SDimitry Andric 2695*0b57cec5SDimitry Andric case fcNormal: 2696*0b57cec5SDimitry Andric dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode); 2697*0b57cec5SDimitry Andric break; 2698*0b57cec5SDimitry Andric } 2699*0b57cec5SDimitry Andric 2700*0b57cec5SDimitry Andric *dst = 0; 2701*0b57cec5SDimitry Andric 2702*0b57cec5SDimitry Andric return static_cast<unsigned int>(dst - p); 2703*0b57cec5SDimitry Andric } 2704*0b57cec5SDimitry Andric 2705*0b57cec5SDimitry Andric /* Does the hard work of outputting the correctly rounded hexadecimal 2706*0b57cec5SDimitry Andric form of a normal floating point number with the specified number of 2707*0b57cec5SDimitry Andric hexadecimal digits. If HEXDIGITS is zero the minimum number of 2708*0b57cec5SDimitry Andric digits necessary to print the value precisely is output. */ 2709*0b57cec5SDimitry Andric char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits, 2710*0b57cec5SDimitry Andric bool upperCase, 2711*0b57cec5SDimitry Andric roundingMode rounding_mode) const { 2712*0b57cec5SDimitry Andric unsigned int count, valueBits, shift, partsCount, outputDigits; 2713*0b57cec5SDimitry Andric const char *hexDigitChars; 2714*0b57cec5SDimitry Andric const integerPart *significand; 2715*0b57cec5SDimitry Andric char *p; 2716*0b57cec5SDimitry Andric bool roundUp; 2717*0b57cec5SDimitry Andric 2718*0b57cec5SDimitry Andric *dst++ = '0'; 2719*0b57cec5SDimitry Andric *dst++ = upperCase ? 'X': 'x'; 2720*0b57cec5SDimitry Andric 2721*0b57cec5SDimitry Andric roundUp = false; 2722*0b57cec5SDimitry Andric hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower; 2723*0b57cec5SDimitry Andric 2724*0b57cec5SDimitry Andric significand = significandParts(); 2725*0b57cec5SDimitry Andric partsCount = partCount(); 2726*0b57cec5SDimitry Andric 2727*0b57cec5SDimitry Andric /* +3 because the first digit only uses the single integer bit, so 2728*0b57cec5SDimitry Andric we have 3 virtual zero most-significant-bits. */ 2729*0b57cec5SDimitry Andric valueBits = semantics->precision + 3; 2730*0b57cec5SDimitry Andric shift = integerPartWidth - valueBits % integerPartWidth; 2731*0b57cec5SDimitry Andric 2732*0b57cec5SDimitry Andric /* The natural number of digits required ignoring trailing 2733*0b57cec5SDimitry Andric insignificant zeroes. */ 2734*0b57cec5SDimitry Andric outputDigits = (valueBits - significandLSB () + 3) / 4; 2735*0b57cec5SDimitry Andric 2736*0b57cec5SDimitry Andric /* hexDigits of zero means use the required number for the 2737*0b57cec5SDimitry Andric precision. Otherwise, see if we are truncating. If we are, 2738*0b57cec5SDimitry Andric find out if we need to round away from zero. */ 2739*0b57cec5SDimitry Andric if (hexDigits) { 2740*0b57cec5SDimitry Andric if (hexDigits < outputDigits) { 2741*0b57cec5SDimitry Andric /* We are dropping non-zero bits, so need to check how to round. 2742*0b57cec5SDimitry Andric "bits" is the number of dropped bits. */ 2743*0b57cec5SDimitry Andric unsigned int bits; 2744*0b57cec5SDimitry Andric lostFraction fraction; 2745*0b57cec5SDimitry Andric 2746*0b57cec5SDimitry Andric bits = valueBits - hexDigits * 4; 2747*0b57cec5SDimitry Andric fraction = lostFractionThroughTruncation (significand, partsCount, bits); 2748*0b57cec5SDimitry Andric roundUp = roundAwayFromZero(rounding_mode, fraction, bits); 2749*0b57cec5SDimitry Andric } 2750*0b57cec5SDimitry Andric outputDigits = hexDigits; 2751*0b57cec5SDimitry Andric } 2752*0b57cec5SDimitry Andric 2753*0b57cec5SDimitry Andric /* Write the digits consecutively, and start writing in the location 2754*0b57cec5SDimitry Andric of the hexadecimal point. We move the most significant digit 2755*0b57cec5SDimitry Andric left and add the hexadecimal point later. */ 2756*0b57cec5SDimitry Andric p = ++dst; 2757*0b57cec5SDimitry Andric 2758*0b57cec5SDimitry Andric count = (valueBits + integerPartWidth - 1) / integerPartWidth; 2759*0b57cec5SDimitry Andric 2760*0b57cec5SDimitry Andric while (outputDigits && count) { 2761*0b57cec5SDimitry Andric integerPart part; 2762*0b57cec5SDimitry Andric 2763*0b57cec5SDimitry Andric /* Put the most significant integerPartWidth bits in "part". */ 2764*0b57cec5SDimitry Andric if (--count == partsCount) 2765*0b57cec5SDimitry Andric part = 0; /* An imaginary higher zero part. */ 2766*0b57cec5SDimitry Andric else 2767*0b57cec5SDimitry Andric part = significand[count] << shift; 2768*0b57cec5SDimitry Andric 2769*0b57cec5SDimitry Andric if (count && shift) 2770*0b57cec5SDimitry Andric part |= significand[count - 1] >> (integerPartWidth - shift); 2771*0b57cec5SDimitry Andric 2772*0b57cec5SDimitry Andric /* Convert as much of "part" to hexdigits as we can. */ 2773*0b57cec5SDimitry Andric unsigned int curDigits = integerPartWidth / 4; 2774*0b57cec5SDimitry Andric 2775*0b57cec5SDimitry Andric if (curDigits > outputDigits) 2776*0b57cec5SDimitry Andric curDigits = outputDigits; 2777*0b57cec5SDimitry Andric dst += partAsHex (dst, part, curDigits, hexDigitChars); 2778*0b57cec5SDimitry Andric outputDigits -= curDigits; 2779*0b57cec5SDimitry Andric } 2780*0b57cec5SDimitry Andric 2781*0b57cec5SDimitry Andric if (roundUp) { 2782*0b57cec5SDimitry Andric char *q = dst; 2783*0b57cec5SDimitry Andric 2784*0b57cec5SDimitry Andric /* Note that hexDigitChars has a trailing '0'. */ 2785*0b57cec5SDimitry Andric do { 2786*0b57cec5SDimitry Andric q--; 2787*0b57cec5SDimitry Andric *q = hexDigitChars[hexDigitValue (*q) + 1]; 2788*0b57cec5SDimitry Andric } while (*q == '0'); 2789*0b57cec5SDimitry Andric assert(q >= p); 2790*0b57cec5SDimitry Andric } else { 2791*0b57cec5SDimitry Andric /* Add trailing zeroes. */ 2792*0b57cec5SDimitry Andric memset (dst, '0', outputDigits); 2793*0b57cec5SDimitry Andric dst += outputDigits; 2794*0b57cec5SDimitry Andric } 2795*0b57cec5SDimitry Andric 2796*0b57cec5SDimitry Andric /* Move the most significant digit to before the point, and if there 2797*0b57cec5SDimitry Andric is something after the decimal point add it. This must come 2798*0b57cec5SDimitry Andric after rounding above. */ 2799*0b57cec5SDimitry Andric p[-1] = p[0]; 2800*0b57cec5SDimitry Andric if (dst -1 == p) 2801*0b57cec5SDimitry Andric dst--; 2802*0b57cec5SDimitry Andric else 2803*0b57cec5SDimitry Andric p[0] = '.'; 2804*0b57cec5SDimitry Andric 2805*0b57cec5SDimitry Andric /* Finally output the exponent. */ 2806*0b57cec5SDimitry Andric *dst++ = upperCase ? 'P': 'p'; 2807*0b57cec5SDimitry Andric 2808*0b57cec5SDimitry Andric return writeSignedDecimal (dst, exponent); 2809*0b57cec5SDimitry Andric } 2810*0b57cec5SDimitry Andric 2811*0b57cec5SDimitry Andric hash_code hash_value(const IEEEFloat &Arg) { 2812*0b57cec5SDimitry Andric if (!Arg.isFiniteNonZero()) 2813*0b57cec5SDimitry Andric return hash_combine((uint8_t)Arg.category, 2814*0b57cec5SDimitry Andric // NaN has no sign, fix it at zero. 2815*0b57cec5SDimitry Andric Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign, 2816*0b57cec5SDimitry Andric Arg.semantics->precision); 2817*0b57cec5SDimitry Andric 2818*0b57cec5SDimitry Andric // Normal floats need their exponent and significand hashed. 2819*0b57cec5SDimitry Andric return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign, 2820*0b57cec5SDimitry Andric Arg.semantics->precision, Arg.exponent, 2821*0b57cec5SDimitry Andric hash_combine_range( 2822*0b57cec5SDimitry Andric Arg.significandParts(), 2823*0b57cec5SDimitry Andric Arg.significandParts() + Arg.partCount())); 2824*0b57cec5SDimitry Andric } 2825*0b57cec5SDimitry Andric 2826*0b57cec5SDimitry Andric // Conversion from APFloat to/from host float/double. It may eventually be 2827*0b57cec5SDimitry Andric // possible to eliminate these and have everybody deal with APFloats, but that 2828*0b57cec5SDimitry Andric // will take a while. This approach will not easily extend to long double. 2829*0b57cec5SDimitry Andric // Current implementation requires integerPartWidth==64, which is correct at 2830*0b57cec5SDimitry Andric // the moment but could be made more general. 2831*0b57cec5SDimitry Andric 2832*0b57cec5SDimitry Andric // Denormals have exponent minExponent in APFloat, but minExponent-1 in 2833*0b57cec5SDimitry Andric // the actual IEEE respresentations. We compensate for that here. 2834*0b57cec5SDimitry Andric 2835*0b57cec5SDimitry Andric APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const { 2836*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended); 2837*0b57cec5SDimitry Andric assert(partCount()==2); 2838*0b57cec5SDimitry Andric 2839*0b57cec5SDimitry Andric uint64_t myexponent, mysignificand; 2840*0b57cec5SDimitry Andric 2841*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 2842*0b57cec5SDimitry Andric myexponent = exponent+16383; //bias 2843*0b57cec5SDimitry Andric mysignificand = significandParts()[0]; 2844*0b57cec5SDimitry Andric if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL)) 2845*0b57cec5SDimitry Andric myexponent = 0; // denormal 2846*0b57cec5SDimitry Andric } else if (category==fcZero) { 2847*0b57cec5SDimitry Andric myexponent = 0; 2848*0b57cec5SDimitry Andric mysignificand = 0; 2849*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 2850*0b57cec5SDimitry Andric myexponent = 0x7fff; 2851*0b57cec5SDimitry Andric mysignificand = 0x8000000000000000ULL; 2852*0b57cec5SDimitry Andric } else { 2853*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category"); 2854*0b57cec5SDimitry Andric myexponent = 0x7fff; 2855*0b57cec5SDimitry Andric mysignificand = significandParts()[0]; 2856*0b57cec5SDimitry Andric } 2857*0b57cec5SDimitry Andric 2858*0b57cec5SDimitry Andric uint64_t words[2]; 2859*0b57cec5SDimitry Andric words[0] = mysignificand; 2860*0b57cec5SDimitry Andric words[1] = ((uint64_t)(sign & 1) << 15) | 2861*0b57cec5SDimitry Andric (myexponent & 0x7fffLL); 2862*0b57cec5SDimitry Andric return APInt(80, words); 2863*0b57cec5SDimitry Andric } 2864*0b57cec5SDimitry Andric 2865*0b57cec5SDimitry Andric APInt IEEEFloat::convertPPCDoubleDoubleAPFloatToAPInt() const { 2866*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy); 2867*0b57cec5SDimitry Andric assert(partCount()==2); 2868*0b57cec5SDimitry Andric 2869*0b57cec5SDimitry Andric uint64_t words[2]; 2870*0b57cec5SDimitry Andric opStatus fs; 2871*0b57cec5SDimitry Andric bool losesInfo; 2872*0b57cec5SDimitry Andric 2873*0b57cec5SDimitry Andric // Convert number to double. To avoid spurious underflows, we re- 2874*0b57cec5SDimitry Andric // normalize against the "double" minExponent first, and only *then* 2875*0b57cec5SDimitry Andric // truncate the mantissa. The result of that second conversion 2876*0b57cec5SDimitry Andric // may be inexact, but should never underflow. 2877*0b57cec5SDimitry Andric // Declare fltSemantics before APFloat that uses it (and 2878*0b57cec5SDimitry Andric // saves pointer to it) to ensure correct destruction order. 2879*0b57cec5SDimitry Andric fltSemantics extendedSemantics = *semantics; 2880*0b57cec5SDimitry Andric extendedSemantics.minExponent = semIEEEdouble.minExponent; 2881*0b57cec5SDimitry Andric IEEEFloat extended(*this); 2882*0b57cec5SDimitry Andric fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 2883*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 2884*0b57cec5SDimitry Andric (void)fs; 2885*0b57cec5SDimitry Andric 2886*0b57cec5SDimitry Andric IEEEFloat u(extended); 2887*0b57cec5SDimitry Andric fs = u.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo); 2888*0b57cec5SDimitry Andric assert(fs == opOK || fs == opInexact); 2889*0b57cec5SDimitry Andric (void)fs; 2890*0b57cec5SDimitry Andric words[0] = *u.convertDoubleAPFloatToAPInt().getRawData(); 2891*0b57cec5SDimitry Andric 2892*0b57cec5SDimitry Andric // If conversion was exact or resulted in a special case, we're done; 2893*0b57cec5SDimitry Andric // just set the second double to zero. Otherwise, re-convert back to 2894*0b57cec5SDimitry Andric // the extended format and compute the difference. This now should 2895*0b57cec5SDimitry Andric // convert exactly to double. 2896*0b57cec5SDimitry Andric if (u.isFiniteNonZero() && losesInfo) { 2897*0b57cec5SDimitry Andric fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 2898*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 2899*0b57cec5SDimitry Andric (void)fs; 2900*0b57cec5SDimitry Andric 2901*0b57cec5SDimitry Andric IEEEFloat v(extended); 2902*0b57cec5SDimitry Andric v.subtract(u, rmNearestTiesToEven); 2903*0b57cec5SDimitry Andric fs = v.convert(semIEEEdouble, rmNearestTiesToEven, &losesInfo); 2904*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 2905*0b57cec5SDimitry Andric (void)fs; 2906*0b57cec5SDimitry Andric words[1] = *v.convertDoubleAPFloatToAPInt().getRawData(); 2907*0b57cec5SDimitry Andric } else { 2908*0b57cec5SDimitry Andric words[1] = 0; 2909*0b57cec5SDimitry Andric } 2910*0b57cec5SDimitry Andric 2911*0b57cec5SDimitry Andric return APInt(128, words); 2912*0b57cec5SDimitry Andric } 2913*0b57cec5SDimitry Andric 2914*0b57cec5SDimitry Andric APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const { 2915*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEquad); 2916*0b57cec5SDimitry Andric assert(partCount()==2); 2917*0b57cec5SDimitry Andric 2918*0b57cec5SDimitry Andric uint64_t myexponent, mysignificand, mysignificand2; 2919*0b57cec5SDimitry Andric 2920*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 2921*0b57cec5SDimitry Andric myexponent = exponent+16383; //bias 2922*0b57cec5SDimitry Andric mysignificand = significandParts()[0]; 2923*0b57cec5SDimitry Andric mysignificand2 = significandParts()[1]; 2924*0b57cec5SDimitry Andric if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL)) 2925*0b57cec5SDimitry Andric myexponent = 0; // denormal 2926*0b57cec5SDimitry Andric } else if (category==fcZero) { 2927*0b57cec5SDimitry Andric myexponent = 0; 2928*0b57cec5SDimitry Andric mysignificand = mysignificand2 = 0; 2929*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 2930*0b57cec5SDimitry Andric myexponent = 0x7fff; 2931*0b57cec5SDimitry Andric mysignificand = mysignificand2 = 0; 2932*0b57cec5SDimitry Andric } else { 2933*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category!"); 2934*0b57cec5SDimitry Andric myexponent = 0x7fff; 2935*0b57cec5SDimitry Andric mysignificand = significandParts()[0]; 2936*0b57cec5SDimitry Andric mysignificand2 = significandParts()[1]; 2937*0b57cec5SDimitry Andric } 2938*0b57cec5SDimitry Andric 2939*0b57cec5SDimitry Andric uint64_t words[2]; 2940*0b57cec5SDimitry Andric words[0] = mysignificand; 2941*0b57cec5SDimitry Andric words[1] = ((uint64_t)(sign & 1) << 63) | 2942*0b57cec5SDimitry Andric ((myexponent & 0x7fff) << 48) | 2943*0b57cec5SDimitry Andric (mysignificand2 & 0xffffffffffffLL); 2944*0b57cec5SDimitry Andric 2945*0b57cec5SDimitry Andric return APInt(128, words); 2946*0b57cec5SDimitry Andric } 2947*0b57cec5SDimitry Andric 2948*0b57cec5SDimitry Andric APInt IEEEFloat::convertDoubleAPFloatToAPInt() const { 2949*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble); 2950*0b57cec5SDimitry Andric assert(partCount()==1); 2951*0b57cec5SDimitry Andric 2952*0b57cec5SDimitry Andric uint64_t myexponent, mysignificand; 2953*0b57cec5SDimitry Andric 2954*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 2955*0b57cec5SDimitry Andric myexponent = exponent+1023; //bias 2956*0b57cec5SDimitry Andric mysignificand = *significandParts(); 2957*0b57cec5SDimitry Andric if (myexponent==1 && !(mysignificand & 0x10000000000000LL)) 2958*0b57cec5SDimitry Andric myexponent = 0; // denormal 2959*0b57cec5SDimitry Andric } else if (category==fcZero) { 2960*0b57cec5SDimitry Andric myexponent = 0; 2961*0b57cec5SDimitry Andric mysignificand = 0; 2962*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 2963*0b57cec5SDimitry Andric myexponent = 0x7ff; 2964*0b57cec5SDimitry Andric mysignificand = 0; 2965*0b57cec5SDimitry Andric } else { 2966*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category!"); 2967*0b57cec5SDimitry Andric myexponent = 0x7ff; 2968*0b57cec5SDimitry Andric mysignificand = *significandParts(); 2969*0b57cec5SDimitry Andric } 2970*0b57cec5SDimitry Andric 2971*0b57cec5SDimitry Andric return APInt(64, ((((uint64_t)(sign & 1) << 63) | 2972*0b57cec5SDimitry Andric ((myexponent & 0x7ff) << 52) | 2973*0b57cec5SDimitry Andric (mysignificand & 0xfffffffffffffLL)))); 2974*0b57cec5SDimitry Andric } 2975*0b57cec5SDimitry Andric 2976*0b57cec5SDimitry Andric APInt IEEEFloat::convertFloatAPFloatToAPInt() const { 2977*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle); 2978*0b57cec5SDimitry Andric assert(partCount()==1); 2979*0b57cec5SDimitry Andric 2980*0b57cec5SDimitry Andric uint32_t myexponent, mysignificand; 2981*0b57cec5SDimitry Andric 2982*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 2983*0b57cec5SDimitry Andric myexponent = exponent+127; //bias 2984*0b57cec5SDimitry Andric mysignificand = (uint32_t)*significandParts(); 2985*0b57cec5SDimitry Andric if (myexponent == 1 && !(mysignificand & 0x800000)) 2986*0b57cec5SDimitry Andric myexponent = 0; // denormal 2987*0b57cec5SDimitry Andric } else if (category==fcZero) { 2988*0b57cec5SDimitry Andric myexponent = 0; 2989*0b57cec5SDimitry Andric mysignificand = 0; 2990*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 2991*0b57cec5SDimitry Andric myexponent = 0xff; 2992*0b57cec5SDimitry Andric mysignificand = 0; 2993*0b57cec5SDimitry Andric } else { 2994*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category!"); 2995*0b57cec5SDimitry Andric myexponent = 0xff; 2996*0b57cec5SDimitry Andric mysignificand = (uint32_t)*significandParts(); 2997*0b57cec5SDimitry Andric } 2998*0b57cec5SDimitry Andric 2999*0b57cec5SDimitry Andric return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) | 3000*0b57cec5SDimitry Andric (mysignificand & 0x7fffff))); 3001*0b57cec5SDimitry Andric } 3002*0b57cec5SDimitry Andric 3003*0b57cec5SDimitry Andric APInt IEEEFloat::convertHalfAPFloatToAPInt() const { 3004*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEhalf); 3005*0b57cec5SDimitry Andric assert(partCount()==1); 3006*0b57cec5SDimitry Andric 3007*0b57cec5SDimitry Andric uint32_t myexponent, mysignificand; 3008*0b57cec5SDimitry Andric 3009*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 3010*0b57cec5SDimitry Andric myexponent = exponent+15; //bias 3011*0b57cec5SDimitry Andric mysignificand = (uint32_t)*significandParts(); 3012*0b57cec5SDimitry Andric if (myexponent == 1 && !(mysignificand & 0x400)) 3013*0b57cec5SDimitry Andric myexponent = 0; // denormal 3014*0b57cec5SDimitry Andric } else if (category==fcZero) { 3015*0b57cec5SDimitry Andric myexponent = 0; 3016*0b57cec5SDimitry Andric mysignificand = 0; 3017*0b57cec5SDimitry Andric } else if (category==fcInfinity) { 3018*0b57cec5SDimitry Andric myexponent = 0x1f; 3019*0b57cec5SDimitry Andric mysignificand = 0; 3020*0b57cec5SDimitry Andric } else { 3021*0b57cec5SDimitry Andric assert(category == fcNaN && "Unknown category!"); 3022*0b57cec5SDimitry Andric myexponent = 0x1f; 3023*0b57cec5SDimitry Andric mysignificand = (uint32_t)*significandParts(); 3024*0b57cec5SDimitry Andric } 3025*0b57cec5SDimitry Andric 3026*0b57cec5SDimitry Andric return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) | 3027*0b57cec5SDimitry Andric (mysignificand & 0x3ff))); 3028*0b57cec5SDimitry Andric } 3029*0b57cec5SDimitry Andric 3030*0b57cec5SDimitry Andric // This function creates an APInt that is just a bit map of the floating 3031*0b57cec5SDimitry Andric // point constant as it would appear in memory. It is not a conversion, 3032*0b57cec5SDimitry Andric // and treating the result as a normal integer is unlikely to be useful. 3033*0b57cec5SDimitry Andric 3034*0b57cec5SDimitry Andric APInt IEEEFloat::bitcastToAPInt() const { 3035*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics*)&semIEEEhalf) 3036*0b57cec5SDimitry Andric return convertHalfAPFloatToAPInt(); 3037*0b57cec5SDimitry Andric 3038*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics*)&semIEEEsingle) 3039*0b57cec5SDimitry Andric return convertFloatAPFloatToAPInt(); 3040*0b57cec5SDimitry Andric 3041*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics*)&semIEEEdouble) 3042*0b57cec5SDimitry Andric return convertDoubleAPFloatToAPInt(); 3043*0b57cec5SDimitry Andric 3044*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics*)&semIEEEquad) 3045*0b57cec5SDimitry Andric return convertQuadrupleAPFloatToAPInt(); 3046*0b57cec5SDimitry Andric 3047*0b57cec5SDimitry Andric if (semantics == (const llvm::fltSemantics *)&semPPCDoubleDoubleLegacy) 3048*0b57cec5SDimitry Andric return convertPPCDoubleDoubleAPFloatToAPInt(); 3049*0b57cec5SDimitry Andric 3050*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semX87DoubleExtended && 3051*0b57cec5SDimitry Andric "unknown format!"); 3052*0b57cec5SDimitry Andric return convertF80LongDoubleAPFloatToAPInt(); 3053*0b57cec5SDimitry Andric } 3054*0b57cec5SDimitry Andric 3055*0b57cec5SDimitry Andric float IEEEFloat::convertToFloat() const { 3056*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEsingle && 3057*0b57cec5SDimitry Andric "Float semantics are not IEEEsingle"); 3058*0b57cec5SDimitry Andric APInt api = bitcastToAPInt(); 3059*0b57cec5SDimitry Andric return api.bitsToFloat(); 3060*0b57cec5SDimitry Andric } 3061*0b57cec5SDimitry Andric 3062*0b57cec5SDimitry Andric double IEEEFloat::convertToDouble() const { 3063*0b57cec5SDimitry Andric assert(semantics == (const llvm::fltSemantics*)&semIEEEdouble && 3064*0b57cec5SDimitry Andric "Float semantics are not IEEEdouble"); 3065*0b57cec5SDimitry Andric APInt api = bitcastToAPInt(); 3066*0b57cec5SDimitry Andric return api.bitsToDouble(); 3067*0b57cec5SDimitry Andric } 3068*0b57cec5SDimitry Andric 3069*0b57cec5SDimitry Andric /// Integer bit is explicit in this format. Intel hardware (387 and later) 3070*0b57cec5SDimitry Andric /// does not support these bit patterns: 3071*0b57cec5SDimitry Andric /// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity") 3072*0b57cec5SDimitry Andric /// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN") 3073*0b57cec5SDimitry Andric /// exponent!=0 nor all 1's, integer bit 0 ("unnormal") 3074*0b57cec5SDimitry Andric /// exponent = 0, integer bit 1 ("pseudodenormal") 3075*0b57cec5SDimitry Andric /// At the moment, the first three are treated as NaNs, the last one as Normal. 3076*0b57cec5SDimitry Andric void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) { 3077*0b57cec5SDimitry Andric assert(api.getBitWidth()==80); 3078*0b57cec5SDimitry Andric uint64_t i1 = api.getRawData()[0]; 3079*0b57cec5SDimitry Andric uint64_t i2 = api.getRawData()[1]; 3080*0b57cec5SDimitry Andric uint64_t myexponent = (i2 & 0x7fff); 3081*0b57cec5SDimitry Andric uint64_t mysignificand = i1; 3082*0b57cec5SDimitry Andric uint8_t myintegerbit = mysignificand >> 63; 3083*0b57cec5SDimitry Andric 3084*0b57cec5SDimitry Andric initialize(&semX87DoubleExtended); 3085*0b57cec5SDimitry Andric assert(partCount()==2); 3086*0b57cec5SDimitry Andric 3087*0b57cec5SDimitry Andric sign = static_cast<unsigned int>(i2>>15); 3088*0b57cec5SDimitry Andric if (myexponent == 0 && mysignificand == 0) { 3089*0b57cec5SDimitry Andric // exponent, significand meaningless 3090*0b57cec5SDimitry Andric category = fcZero; 3091*0b57cec5SDimitry Andric } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) { 3092*0b57cec5SDimitry Andric // exponent, significand meaningless 3093*0b57cec5SDimitry Andric category = fcInfinity; 3094*0b57cec5SDimitry Andric } else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) || 3095*0b57cec5SDimitry Andric (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) { 3096*0b57cec5SDimitry Andric // exponent meaningless 3097*0b57cec5SDimitry Andric category = fcNaN; 3098*0b57cec5SDimitry Andric significandParts()[0] = mysignificand; 3099*0b57cec5SDimitry Andric significandParts()[1] = 0; 3100*0b57cec5SDimitry Andric } else { 3101*0b57cec5SDimitry Andric category = fcNormal; 3102*0b57cec5SDimitry Andric exponent = myexponent - 16383; 3103*0b57cec5SDimitry Andric significandParts()[0] = mysignificand; 3104*0b57cec5SDimitry Andric significandParts()[1] = 0; 3105*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3106*0b57cec5SDimitry Andric exponent = -16382; 3107*0b57cec5SDimitry Andric } 3108*0b57cec5SDimitry Andric } 3109*0b57cec5SDimitry Andric 3110*0b57cec5SDimitry Andric void IEEEFloat::initFromPPCDoubleDoubleAPInt(const APInt &api) { 3111*0b57cec5SDimitry Andric assert(api.getBitWidth()==128); 3112*0b57cec5SDimitry Andric uint64_t i1 = api.getRawData()[0]; 3113*0b57cec5SDimitry Andric uint64_t i2 = api.getRawData()[1]; 3114*0b57cec5SDimitry Andric opStatus fs; 3115*0b57cec5SDimitry Andric bool losesInfo; 3116*0b57cec5SDimitry Andric 3117*0b57cec5SDimitry Andric // Get the first double and convert to our format. 3118*0b57cec5SDimitry Andric initFromDoubleAPInt(APInt(64, i1)); 3119*0b57cec5SDimitry Andric fs = convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo); 3120*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 3121*0b57cec5SDimitry Andric (void)fs; 3122*0b57cec5SDimitry Andric 3123*0b57cec5SDimitry Andric // Unless we have a special case, add in second double. 3124*0b57cec5SDimitry Andric if (isFiniteNonZero()) { 3125*0b57cec5SDimitry Andric IEEEFloat v(semIEEEdouble, APInt(64, i2)); 3126*0b57cec5SDimitry Andric fs = v.convert(semPPCDoubleDoubleLegacy, rmNearestTiesToEven, &losesInfo); 3127*0b57cec5SDimitry Andric assert(fs == opOK && !losesInfo); 3128*0b57cec5SDimitry Andric (void)fs; 3129*0b57cec5SDimitry Andric 3130*0b57cec5SDimitry Andric add(v, rmNearestTiesToEven); 3131*0b57cec5SDimitry Andric } 3132*0b57cec5SDimitry Andric } 3133*0b57cec5SDimitry Andric 3134*0b57cec5SDimitry Andric void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) { 3135*0b57cec5SDimitry Andric assert(api.getBitWidth()==128); 3136*0b57cec5SDimitry Andric uint64_t i1 = api.getRawData()[0]; 3137*0b57cec5SDimitry Andric uint64_t i2 = api.getRawData()[1]; 3138*0b57cec5SDimitry Andric uint64_t myexponent = (i2 >> 48) & 0x7fff; 3139*0b57cec5SDimitry Andric uint64_t mysignificand = i1; 3140*0b57cec5SDimitry Andric uint64_t mysignificand2 = i2 & 0xffffffffffffLL; 3141*0b57cec5SDimitry Andric 3142*0b57cec5SDimitry Andric initialize(&semIEEEquad); 3143*0b57cec5SDimitry Andric assert(partCount()==2); 3144*0b57cec5SDimitry Andric 3145*0b57cec5SDimitry Andric sign = static_cast<unsigned int>(i2>>63); 3146*0b57cec5SDimitry Andric if (myexponent==0 && 3147*0b57cec5SDimitry Andric (mysignificand==0 && mysignificand2==0)) { 3148*0b57cec5SDimitry Andric // exponent, significand meaningless 3149*0b57cec5SDimitry Andric category = fcZero; 3150*0b57cec5SDimitry Andric } else if (myexponent==0x7fff && 3151*0b57cec5SDimitry Andric (mysignificand==0 && mysignificand2==0)) { 3152*0b57cec5SDimitry Andric // exponent, significand meaningless 3153*0b57cec5SDimitry Andric category = fcInfinity; 3154*0b57cec5SDimitry Andric } else if (myexponent==0x7fff && 3155*0b57cec5SDimitry Andric (mysignificand!=0 || mysignificand2 !=0)) { 3156*0b57cec5SDimitry Andric // exponent meaningless 3157*0b57cec5SDimitry Andric category = fcNaN; 3158*0b57cec5SDimitry Andric significandParts()[0] = mysignificand; 3159*0b57cec5SDimitry Andric significandParts()[1] = mysignificand2; 3160*0b57cec5SDimitry Andric } else { 3161*0b57cec5SDimitry Andric category = fcNormal; 3162*0b57cec5SDimitry Andric exponent = myexponent - 16383; 3163*0b57cec5SDimitry Andric significandParts()[0] = mysignificand; 3164*0b57cec5SDimitry Andric significandParts()[1] = mysignificand2; 3165*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3166*0b57cec5SDimitry Andric exponent = -16382; 3167*0b57cec5SDimitry Andric else 3168*0b57cec5SDimitry Andric significandParts()[1] |= 0x1000000000000LL; // integer bit 3169*0b57cec5SDimitry Andric } 3170*0b57cec5SDimitry Andric } 3171*0b57cec5SDimitry Andric 3172*0b57cec5SDimitry Andric void IEEEFloat::initFromDoubleAPInt(const APInt &api) { 3173*0b57cec5SDimitry Andric assert(api.getBitWidth()==64); 3174*0b57cec5SDimitry Andric uint64_t i = *api.getRawData(); 3175*0b57cec5SDimitry Andric uint64_t myexponent = (i >> 52) & 0x7ff; 3176*0b57cec5SDimitry Andric uint64_t mysignificand = i & 0xfffffffffffffLL; 3177*0b57cec5SDimitry Andric 3178*0b57cec5SDimitry Andric initialize(&semIEEEdouble); 3179*0b57cec5SDimitry Andric assert(partCount()==1); 3180*0b57cec5SDimitry Andric 3181*0b57cec5SDimitry Andric sign = static_cast<unsigned int>(i>>63); 3182*0b57cec5SDimitry Andric if (myexponent==0 && mysignificand==0) { 3183*0b57cec5SDimitry Andric // exponent, significand meaningless 3184*0b57cec5SDimitry Andric category = fcZero; 3185*0b57cec5SDimitry Andric } else if (myexponent==0x7ff && mysignificand==0) { 3186*0b57cec5SDimitry Andric // exponent, significand meaningless 3187*0b57cec5SDimitry Andric category = fcInfinity; 3188*0b57cec5SDimitry Andric } else if (myexponent==0x7ff && mysignificand!=0) { 3189*0b57cec5SDimitry Andric // exponent meaningless 3190*0b57cec5SDimitry Andric category = fcNaN; 3191*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3192*0b57cec5SDimitry Andric } else { 3193*0b57cec5SDimitry Andric category = fcNormal; 3194*0b57cec5SDimitry Andric exponent = myexponent - 1023; 3195*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3196*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3197*0b57cec5SDimitry Andric exponent = -1022; 3198*0b57cec5SDimitry Andric else 3199*0b57cec5SDimitry Andric *significandParts() |= 0x10000000000000LL; // integer bit 3200*0b57cec5SDimitry Andric } 3201*0b57cec5SDimitry Andric } 3202*0b57cec5SDimitry Andric 3203*0b57cec5SDimitry Andric void IEEEFloat::initFromFloatAPInt(const APInt &api) { 3204*0b57cec5SDimitry Andric assert(api.getBitWidth()==32); 3205*0b57cec5SDimitry Andric uint32_t i = (uint32_t)*api.getRawData(); 3206*0b57cec5SDimitry Andric uint32_t myexponent = (i >> 23) & 0xff; 3207*0b57cec5SDimitry Andric uint32_t mysignificand = i & 0x7fffff; 3208*0b57cec5SDimitry Andric 3209*0b57cec5SDimitry Andric initialize(&semIEEEsingle); 3210*0b57cec5SDimitry Andric assert(partCount()==1); 3211*0b57cec5SDimitry Andric 3212*0b57cec5SDimitry Andric sign = i >> 31; 3213*0b57cec5SDimitry Andric if (myexponent==0 && mysignificand==0) { 3214*0b57cec5SDimitry Andric // exponent, significand meaningless 3215*0b57cec5SDimitry Andric category = fcZero; 3216*0b57cec5SDimitry Andric } else if (myexponent==0xff && mysignificand==0) { 3217*0b57cec5SDimitry Andric // exponent, significand meaningless 3218*0b57cec5SDimitry Andric category = fcInfinity; 3219*0b57cec5SDimitry Andric } else if (myexponent==0xff && mysignificand!=0) { 3220*0b57cec5SDimitry Andric // sign, exponent, significand meaningless 3221*0b57cec5SDimitry Andric category = fcNaN; 3222*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3223*0b57cec5SDimitry Andric } else { 3224*0b57cec5SDimitry Andric category = fcNormal; 3225*0b57cec5SDimitry Andric exponent = myexponent - 127; //bias 3226*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3227*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3228*0b57cec5SDimitry Andric exponent = -126; 3229*0b57cec5SDimitry Andric else 3230*0b57cec5SDimitry Andric *significandParts() |= 0x800000; // integer bit 3231*0b57cec5SDimitry Andric } 3232*0b57cec5SDimitry Andric } 3233*0b57cec5SDimitry Andric 3234*0b57cec5SDimitry Andric void IEEEFloat::initFromHalfAPInt(const APInt &api) { 3235*0b57cec5SDimitry Andric assert(api.getBitWidth()==16); 3236*0b57cec5SDimitry Andric uint32_t i = (uint32_t)*api.getRawData(); 3237*0b57cec5SDimitry Andric uint32_t myexponent = (i >> 10) & 0x1f; 3238*0b57cec5SDimitry Andric uint32_t mysignificand = i & 0x3ff; 3239*0b57cec5SDimitry Andric 3240*0b57cec5SDimitry Andric initialize(&semIEEEhalf); 3241*0b57cec5SDimitry Andric assert(partCount()==1); 3242*0b57cec5SDimitry Andric 3243*0b57cec5SDimitry Andric sign = i >> 15; 3244*0b57cec5SDimitry Andric if (myexponent==0 && mysignificand==0) { 3245*0b57cec5SDimitry Andric // exponent, significand meaningless 3246*0b57cec5SDimitry Andric category = fcZero; 3247*0b57cec5SDimitry Andric } else if (myexponent==0x1f && mysignificand==0) { 3248*0b57cec5SDimitry Andric // exponent, significand meaningless 3249*0b57cec5SDimitry Andric category = fcInfinity; 3250*0b57cec5SDimitry Andric } else if (myexponent==0x1f && mysignificand!=0) { 3251*0b57cec5SDimitry Andric // sign, exponent, significand meaningless 3252*0b57cec5SDimitry Andric category = fcNaN; 3253*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3254*0b57cec5SDimitry Andric } else { 3255*0b57cec5SDimitry Andric category = fcNormal; 3256*0b57cec5SDimitry Andric exponent = myexponent - 15; //bias 3257*0b57cec5SDimitry Andric *significandParts() = mysignificand; 3258*0b57cec5SDimitry Andric if (myexponent==0) // denormal 3259*0b57cec5SDimitry Andric exponent = -14; 3260*0b57cec5SDimitry Andric else 3261*0b57cec5SDimitry Andric *significandParts() |= 0x400; // integer bit 3262*0b57cec5SDimitry Andric } 3263*0b57cec5SDimitry Andric } 3264*0b57cec5SDimitry Andric 3265*0b57cec5SDimitry Andric /// Treat api as containing the bits of a floating point number. Currently 3266*0b57cec5SDimitry Andric /// we infer the floating point type from the size of the APInt. The 3267*0b57cec5SDimitry Andric /// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful 3268*0b57cec5SDimitry Andric /// when the size is anything else). 3269*0b57cec5SDimitry Andric void IEEEFloat::initFromAPInt(const fltSemantics *Sem, const APInt &api) { 3270*0b57cec5SDimitry Andric if (Sem == &semIEEEhalf) 3271*0b57cec5SDimitry Andric return initFromHalfAPInt(api); 3272*0b57cec5SDimitry Andric if (Sem == &semIEEEsingle) 3273*0b57cec5SDimitry Andric return initFromFloatAPInt(api); 3274*0b57cec5SDimitry Andric if (Sem == &semIEEEdouble) 3275*0b57cec5SDimitry Andric return initFromDoubleAPInt(api); 3276*0b57cec5SDimitry Andric if (Sem == &semX87DoubleExtended) 3277*0b57cec5SDimitry Andric return initFromF80LongDoubleAPInt(api); 3278*0b57cec5SDimitry Andric if (Sem == &semIEEEquad) 3279*0b57cec5SDimitry Andric return initFromQuadrupleAPInt(api); 3280*0b57cec5SDimitry Andric if (Sem == &semPPCDoubleDoubleLegacy) 3281*0b57cec5SDimitry Andric return initFromPPCDoubleDoubleAPInt(api); 3282*0b57cec5SDimitry Andric 3283*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 3284*0b57cec5SDimitry Andric } 3285*0b57cec5SDimitry Andric 3286*0b57cec5SDimitry Andric /// Make this number the largest magnitude normal number in the given 3287*0b57cec5SDimitry Andric /// semantics. 3288*0b57cec5SDimitry Andric void IEEEFloat::makeLargest(bool Negative) { 3289*0b57cec5SDimitry Andric // We want (in interchange format): 3290*0b57cec5SDimitry Andric // sign = {Negative} 3291*0b57cec5SDimitry Andric // exponent = 1..10 3292*0b57cec5SDimitry Andric // significand = 1..1 3293*0b57cec5SDimitry Andric category = fcNormal; 3294*0b57cec5SDimitry Andric sign = Negative; 3295*0b57cec5SDimitry Andric exponent = semantics->maxExponent; 3296*0b57cec5SDimitry Andric 3297*0b57cec5SDimitry Andric // Use memset to set all but the highest integerPart to all ones. 3298*0b57cec5SDimitry Andric integerPart *significand = significandParts(); 3299*0b57cec5SDimitry Andric unsigned PartCount = partCount(); 3300*0b57cec5SDimitry Andric memset(significand, 0xFF, sizeof(integerPart)*(PartCount - 1)); 3301*0b57cec5SDimitry Andric 3302*0b57cec5SDimitry Andric // Set the high integerPart especially setting all unused top bits for 3303*0b57cec5SDimitry Andric // internal consistency. 3304*0b57cec5SDimitry Andric const unsigned NumUnusedHighBits = 3305*0b57cec5SDimitry Andric PartCount*integerPartWidth - semantics->precision; 3306*0b57cec5SDimitry Andric significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth) 3307*0b57cec5SDimitry Andric ? (~integerPart(0) >> NumUnusedHighBits) 3308*0b57cec5SDimitry Andric : 0; 3309*0b57cec5SDimitry Andric } 3310*0b57cec5SDimitry Andric 3311*0b57cec5SDimitry Andric /// Make this number the smallest magnitude denormal number in the given 3312*0b57cec5SDimitry Andric /// semantics. 3313*0b57cec5SDimitry Andric void IEEEFloat::makeSmallest(bool Negative) { 3314*0b57cec5SDimitry Andric // We want (in interchange format): 3315*0b57cec5SDimitry Andric // sign = {Negative} 3316*0b57cec5SDimitry Andric // exponent = 0..0 3317*0b57cec5SDimitry Andric // significand = 0..01 3318*0b57cec5SDimitry Andric category = fcNormal; 3319*0b57cec5SDimitry Andric sign = Negative; 3320*0b57cec5SDimitry Andric exponent = semantics->minExponent; 3321*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 1, partCount()); 3322*0b57cec5SDimitry Andric } 3323*0b57cec5SDimitry Andric 3324*0b57cec5SDimitry Andric void IEEEFloat::makeSmallestNormalized(bool Negative) { 3325*0b57cec5SDimitry Andric // We want (in interchange format): 3326*0b57cec5SDimitry Andric // sign = {Negative} 3327*0b57cec5SDimitry Andric // exponent = 0..0 3328*0b57cec5SDimitry Andric // significand = 10..0 3329*0b57cec5SDimitry Andric 3330*0b57cec5SDimitry Andric category = fcNormal; 3331*0b57cec5SDimitry Andric zeroSignificand(); 3332*0b57cec5SDimitry Andric sign = Negative; 3333*0b57cec5SDimitry Andric exponent = semantics->minExponent; 3334*0b57cec5SDimitry Andric significandParts()[partCountForBits(semantics->precision) - 1] |= 3335*0b57cec5SDimitry Andric (((integerPart)1) << ((semantics->precision - 1) % integerPartWidth)); 3336*0b57cec5SDimitry Andric } 3337*0b57cec5SDimitry Andric 3338*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(const fltSemantics &Sem, const APInt &API) { 3339*0b57cec5SDimitry Andric initFromAPInt(&Sem, API); 3340*0b57cec5SDimitry Andric } 3341*0b57cec5SDimitry Andric 3342*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(float f) { 3343*0b57cec5SDimitry Andric initFromAPInt(&semIEEEsingle, APInt::floatToBits(f)); 3344*0b57cec5SDimitry Andric } 3345*0b57cec5SDimitry Andric 3346*0b57cec5SDimitry Andric IEEEFloat::IEEEFloat(double d) { 3347*0b57cec5SDimitry Andric initFromAPInt(&semIEEEdouble, APInt::doubleToBits(d)); 3348*0b57cec5SDimitry Andric } 3349*0b57cec5SDimitry Andric 3350*0b57cec5SDimitry Andric namespace { 3351*0b57cec5SDimitry Andric void append(SmallVectorImpl<char> &Buffer, StringRef Str) { 3352*0b57cec5SDimitry Andric Buffer.append(Str.begin(), Str.end()); 3353*0b57cec5SDimitry Andric } 3354*0b57cec5SDimitry Andric 3355*0b57cec5SDimitry Andric /// Removes data from the given significand until it is no more 3356*0b57cec5SDimitry Andric /// precise than is required for the desired precision. 3357*0b57cec5SDimitry Andric void AdjustToPrecision(APInt &significand, 3358*0b57cec5SDimitry Andric int &exp, unsigned FormatPrecision) { 3359*0b57cec5SDimitry Andric unsigned bits = significand.getActiveBits(); 3360*0b57cec5SDimitry Andric 3361*0b57cec5SDimitry Andric // 196/59 is a very slight overestimate of lg_2(10). 3362*0b57cec5SDimitry Andric unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59; 3363*0b57cec5SDimitry Andric 3364*0b57cec5SDimitry Andric if (bits <= bitsRequired) return; 3365*0b57cec5SDimitry Andric 3366*0b57cec5SDimitry Andric unsigned tensRemovable = (bits - bitsRequired) * 59 / 196; 3367*0b57cec5SDimitry Andric if (!tensRemovable) return; 3368*0b57cec5SDimitry Andric 3369*0b57cec5SDimitry Andric exp += tensRemovable; 3370*0b57cec5SDimitry Andric 3371*0b57cec5SDimitry Andric APInt divisor(significand.getBitWidth(), 1); 3372*0b57cec5SDimitry Andric APInt powten(significand.getBitWidth(), 10); 3373*0b57cec5SDimitry Andric while (true) { 3374*0b57cec5SDimitry Andric if (tensRemovable & 1) 3375*0b57cec5SDimitry Andric divisor *= powten; 3376*0b57cec5SDimitry Andric tensRemovable >>= 1; 3377*0b57cec5SDimitry Andric if (!tensRemovable) break; 3378*0b57cec5SDimitry Andric powten *= powten; 3379*0b57cec5SDimitry Andric } 3380*0b57cec5SDimitry Andric 3381*0b57cec5SDimitry Andric significand = significand.udiv(divisor); 3382*0b57cec5SDimitry Andric 3383*0b57cec5SDimitry Andric // Truncate the significand down to its active bit count. 3384*0b57cec5SDimitry Andric significand = significand.trunc(significand.getActiveBits()); 3385*0b57cec5SDimitry Andric } 3386*0b57cec5SDimitry Andric 3387*0b57cec5SDimitry Andric 3388*0b57cec5SDimitry Andric void AdjustToPrecision(SmallVectorImpl<char> &buffer, 3389*0b57cec5SDimitry Andric int &exp, unsigned FormatPrecision) { 3390*0b57cec5SDimitry Andric unsigned N = buffer.size(); 3391*0b57cec5SDimitry Andric if (N <= FormatPrecision) return; 3392*0b57cec5SDimitry Andric 3393*0b57cec5SDimitry Andric // The most significant figures are the last ones in the buffer. 3394*0b57cec5SDimitry Andric unsigned FirstSignificant = N - FormatPrecision; 3395*0b57cec5SDimitry Andric 3396*0b57cec5SDimitry Andric // Round. 3397*0b57cec5SDimitry Andric // FIXME: this probably shouldn't use 'round half up'. 3398*0b57cec5SDimitry Andric 3399*0b57cec5SDimitry Andric // Rounding down is just a truncation, except we also want to drop 3400*0b57cec5SDimitry Andric // trailing zeros from the new result. 3401*0b57cec5SDimitry Andric if (buffer[FirstSignificant - 1] < '5') { 3402*0b57cec5SDimitry Andric while (FirstSignificant < N && buffer[FirstSignificant] == '0') 3403*0b57cec5SDimitry Andric FirstSignificant++; 3404*0b57cec5SDimitry Andric 3405*0b57cec5SDimitry Andric exp += FirstSignificant; 3406*0b57cec5SDimitry Andric buffer.erase(&buffer[0], &buffer[FirstSignificant]); 3407*0b57cec5SDimitry Andric return; 3408*0b57cec5SDimitry Andric } 3409*0b57cec5SDimitry Andric 3410*0b57cec5SDimitry Andric // Rounding up requires a decimal add-with-carry. If we continue 3411*0b57cec5SDimitry Andric // the carry, the newly-introduced zeros will just be truncated. 3412*0b57cec5SDimitry Andric for (unsigned I = FirstSignificant; I != N; ++I) { 3413*0b57cec5SDimitry Andric if (buffer[I] == '9') { 3414*0b57cec5SDimitry Andric FirstSignificant++; 3415*0b57cec5SDimitry Andric } else { 3416*0b57cec5SDimitry Andric buffer[I]++; 3417*0b57cec5SDimitry Andric break; 3418*0b57cec5SDimitry Andric } 3419*0b57cec5SDimitry Andric } 3420*0b57cec5SDimitry Andric 3421*0b57cec5SDimitry Andric // If we carried through, we have exactly one digit of precision. 3422*0b57cec5SDimitry Andric if (FirstSignificant == N) { 3423*0b57cec5SDimitry Andric exp += FirstSignificant; 3424*0b57cec5SDimitry Andric buffer.clear(); 3425*0b57cec5SDimitry Andric buffer.push_back('1'); 3426*0b57cec5SDimitry Andric return; 3427*0b57cec5SDimitry Andric } 3428*0b57cec5SDimitry Andric 3429*0b57cec5SDimitry Andric exp += FirstSignificant; 3430*0b57cec5SDimitry Andric buffer.erase(&buffer[0], &buffer[FirstSignificant]); 3431*0b57cec5SDimitry Andric } 3432*0b57cec5SDimitry Andric } 3433*0b57cec5SDimitry Andric 3434*0b57cec5SDimitry Andric void IEEEFloat::toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision, 3435*0b57cec5SDimitry Andric unsigned FormatMaxPadding, bool TruncateZero) const { 3436*0b57cec5SDimitry Andric switch (category) { 3437*0b57cec5SDimitry Andric case fcInfinity: 3438*0b57cec5SDimitry Andric if (isNegative()) 3439*0b57cec5SDimitry Andric return append(Str, "-Inf"); 3440*0b57cec5SDimitry Andric else 3441*0b57cec5SDimitry Andric return append(Str, "+Inf"); 3442*0b57cec5SDimitry Andric 3443*0b57cec5SDimitry Andric case fcNaN: return append(Str, "NaN"); 3444*0b57cec5SDimitry Andric 3445*0b57cec5SDimitry Andric case fcZero: 3446*0b57cec5SDimitry Andric if (isNegative()) 3447*0b57cec5SDimitry Andric Str.push_back('-'); 3448*0b57cec5SDimitry Andric 3449*0b57cec5SDimitry Andric if (!FormatMaxPadding) { 3450*0b57cec5SDimitry Andric if (TruncateZero) 3451*0b57cec5SDimitry Andric append(Str, "0.0E+0"); 3452*0b57cec5SDimitry Andric else { 3453*0b57cec5SDimitry Andric append(Str, "0.0"); 3454*0b57cec5SDimitry Andric if (FormatPrecision > 1) 3455*0b57cec5SDimitry Andric Str.append(FormatPrecision - 1, '0'); 3456*0b57cec5SDimitry Andric append(Str, "e+00"); 3457*0b57cec5SDimitry Andric } 3458*0b57cec5SDimitry Andric } else 3459*0b57cec5SDimitry Andric Str.push_back('0'); 3460*0b57cec5SDimitry Andric return; 3461*0b57cec5SDimitry Andric 3462*0b57cec5SDimitry Andric case fcNormal: 3463*0b57cec5SDimitry Andric break; 3464*0b57cec5SDimitry Andric } 3465*0b57cec5SDimitry Andric 3466*0b57cec5SDimitry Andric if (isNegative()) 3467*0b57cec5SDimitry Andric Str.push_back('-'); 3468*0b57cec5SDimitry Andric 3469*0b57cec5SDimitry Andric // Decompose the number into an APInt and an exponent. 3470*0b57cec5SDimitry Andric int exp = exponent - ((int) semantics->precision - 1); 3471*0b57cec5SDimitry Andric APInt significand(semantics->precision, 3472*0b57cec5SDimitry Andric makeArrayRef(significandParts(), 3473*0b57cec5SDimitry Andric partCountForBits(semantics->precision))); 3474*0b57cec5SDimitry Andric 3475*0b57cec5SDimitry Andric // Set FormatPrecision if zero. We want to do this before we 3476*0b57cec5SDimitry Andric // truncate trailing zeros, as those are part of the precision. 3477*0b57cec5SDimitry Andric if (!FormatPrecision) { 3478*0b57cec5SDimitry Andric // We use enough digits so the number can be round-tripped back to an 3479*0b57cec5SDimitry Andric // APFloat. The formula comes from "How to Print Floating-Point Numbers 3480*0b57cec5SDimitry Andric // Accurately" by Steele and White. 3481*0b57cec5SDimitry Andric // FIXME: Using a formula based purely on the precision is conservative; 3482*0b57cec5SDimitry Andric // we can print fewer digits depending on the actual value being printed. 3483*0b57cec5SDimitry Andric 3484*0b57cec5SDimitry Andric // FormatPrecision = 2 + floor(significandBits / lg_2(10)) 3485*0b57cec5SDimitry Andric FormatPrecision = 2 + semantics->precision * 59 / 196; 3486*0b57cec5SDimitry Andric } 3487*0b57cec5SDimitry Andric 3488*0b57cec5SDimitry Andric // Ignore trailing binary zeros. 3489*0b57cec5SDimitry Andric int trailingZeros = significand.countTrailingZeros(); 3490*0b57cec5SDimitry Andric exp += trailingZeros; 3491*0b57cec5SDimitry Andric significand.lshrInPlace(trailingZeros); 3492*0b57cec5SDimitry Andric 3493*0b57cec5SDimitry Andric // Change the exponent from 2^e to 10^e. 3494*0b57cec5SDimitry Andric if (exp == 0) { 3495*0b57cec5SDimitry Andric // Nothing to do. 3496*0b57cec5SDimitry Andric } else if (exp > 0) { 3497*0b57cec5SDimitry Andric // Just shift left. 3498*0b57cec5SDimitry Andric significand = significand.zext(semantics->precision + exp); 3499*0b57cec5SDimitry Andric significand <<= exp; 3500*0b57cec5SDimitry Andric exp = 0; 3501*0b57cec5SDimitry Andric } else { /* exp < 0 */ 3502*0b57cec5SDimitry Andric int texp = -exp; 3503*0b57cec5SDimitry Andric 3504*0b57cec5SDimitry Andric // We transform this using the identity: 3505*0b57cec5SDimitry Andric // (N)(2^-e) == (N)(5^e)(10^-e) 3506*0b57cec5SDimitry Andric // This means we have to multiply N (the significand) by 5^e. 3507*0b57cec5SDimitry Andric // To avoid overflow, we have to operate on numbers large 3508*0b57cec5SDimitry Andric // enough to store N * 5^e: 3509*0b57cec5SDimitry Andric // log2(N * 5^e) == log2(N) + e * log2(5) 3510*0b57cec5SDimitry Andric // <= semantics->precision + e * 137 / 59 3511*0b57cec5SDimitry Andric // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59) 3512*0b57cec5SDimitry Andric 3513*0b57cec5SDimitry Andric unsigned precision = semantics->precision + (137 * texp + 136) / 59; 3514*0b57cec5SDimitry Andric 3515*0b57cec5SDimitry Andric // Multiply significand by 5^e. 3516*0b57cec5SDimitry Andric // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8) 3517*0b57cec5SDimitry Andric significand = significand.zext(precision); 3518*0b57cec5SDimitry Andric APInt five_to_the_i(precision, 5); 3519*0b57cec5SDimitry Andric while (true) { 3520*0b57cec5SDimitry Andric if (texp & 1) significand *= five_to_the_i; 3521*0b57cec5SDimitry Andric 3522*0b57cec5SDimitry Andric texp >>= 1; 3523*0b57cec5SDimitry Andric if (!texp) break; 3524*0b57cec5SDimitry Andric five_to_the_i *= five_to_the_i; 3525*0b57cec5SDimitry Andric } 3526*0b57cec5SDimitry Andric } 3527*0b57cec5SDimitry Andric 3528*0b57cec5SDimitry Andric AdjustToPrecision(significand, exp, FormatPrecision); 3529*0b57cec5SDimitry Andric 3530*0b57cec5SDimitry Andric SmallVector<char, 256> buffer; 3531*0b57cec5SDimitry Andric 3532*0b57cec5SDimitry Andric // Fill the buffer. 3533*0b57cec5SDimitry Andric unsigned precision = significand.getBitWidth(); 3534*0b57cec5SDimitry Andric APInt ten(precision, 10); 3535*0b57cec5SDimitry Andric APInt digit(precision, 0); 3536*0b57cec5SDimitry Andric 3537*0b57cec5SDimitry Andric bool inTrail = true; 3538*0b57cec5SDimitry Andric while (significand != 0) { 3539*0b57cec5SDimitry Andric // digit <- significand % 10 3540*0b57cec5SDimitry Andric // significand <- significand / 10 3541*0b57cec5SDimitry Andric APInt::udivrem(significand, ten, significand, digit); 3542*0b57cec5SDimitry Andric 3543*0b57cec5SDimitry Andric unsigned d = digit.getZExtValue(); 3544*0b57cec5SDimitry Andric 3545*0b57cec5SDimitry Andric // Drop trailing zeros. 3546*0b57cec5SDimitry Andric if (inTrail && !d) exp++; 3547*0b57cec5SDimitry Andric else { 3548*0b57cec5SDimitry Andric buffer.push_back((char) ('0' + d)); 3549*0b57cec5SDimitry Andric inTrail = false; 3550*0b57cec5SDimitry Andric } 3551*0b57cec5SDimitry Andric } 3552*0b57cec5SDimitry Andric 3553*0b57cec5SDimitry Andric assert(!buffer.empty() && "no characters in buffer!"); 3554*0b57cec5SDimitry Andric 3555*0b57cec5SDimitry Andric // Drop down to FormatPrecision. 3556*0b57cec5SDimitry Andric // TODO: don't do more precise calculations above than are required. 3557*0b57cec5SDimitry Andric AdjustToPrecision(buffer, exp, FormatPrecision); 3558*0b57cec5SDimitry Andric 3559*0b57cec5SDimitry Andric unsigned NDigits = buffer.size(); 3560*0b57cec5SDimitry Andric 3561*0b57cec5SDimitry Andric // Check whether we should use scientific notation. 3562*0b57cec5SDimitry Andric bool FormatScientific; 3563*0b57cec5SDimitry Andric if (!FormatMaxPadding) 3564*0b57cec5SDimitry Andric FormatScientific = true; 3565*0b57cec5SDimitry Andric else { 3566*0b57cec5SDimitry Andric if (exp >= 0) { 3567*0b57cec5SDimitry Andric // 765e3 --> 765000 3568*0b57cec5SDimitry Andric // ^^^ 3569*0b57cec5SDimitry Andric // But we shouldn't make the number look more precise than it is. 3570*0b57cec5SDimitry Andric FormatScientific = ((unsigned) exp > FormatMaxPadding || 3571*0b57cec5SDimitry Andric NDigits + (unsigned) exp > FormatPrecision); 3572*0b57cec5SDimitry Andric } else { 3573*0b57cec5SDimitry Andric // Power of the most significant digit. 3574*0b57cec5SDimitry Andric int MSD = exp + (int) (NDigits - 1); 3575*0b57cec5SDimitry Andric if (MSD >= 0) { 3576*0b57cec5SDimitry Andric // 765e-2 == 7.65 3577*0b57cec5SDimitry Andric FormatScientific = false; 3578*0b57cec5SDimitry Andric } else { 3579*0b57cec5SDimitry Andric // 765e-5 == 0.00765 3580*0b57cec5SDimitry Andric // ^ ^^ 3581*0b57cec5SDimitry Andric FormatScientific = ((unsigned) -MSD) > FormatMaxPadding; 3582*0b57cec5SDimitry Andric } 3583*0b57cec5SDimitry Andric } 3584*0b57cec5SDimitry Andric } 3585*0b57cec5SDimitry Andric 3586*0b57cec5SDimitry Andric // Scientific formatting is pretty straightforward. 3587*0b57cec5SDimitry Andric if (FormatScientific) { 3588*0b57cec5SDimitry Andric exp += (NDigits - 1); 3589*0b57cec5SDimitry Andric 3590*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-1]); 3591*0b57cec5SDimitry Andric Str.push_back('.'); 3592*0b57cec5SDimitry Andric if (NDigits == 1 && TruncateZero) 3593*0b57cec5SDimitry Andric Str.push_back('0'); 3594*0b57cec5SDimitry Andric else 3595*0b57cec5SDimitry Andric for (unsigned I = 1; I != NDigits; ++I) 3596*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-1-I]); 3597*0b57cec5SDimitry Andric // Fill with zeros up to FormatPrecision. 3598*0b57cec5SDimitry Andric if (!TruncateZero && FormatPrecision > NDigits - 1) 3599*0b57cec5SDimitry Andric Str.append(FormatPrecision - NDigits + 1, '0'); 3600*0b57cec5SDimitry Andric // For !TruncateZero we use lower 'e'. 3601*0b57cec5SDimitry Andric Str.push_back(TruncateZero ? 'E' : 'e'); 3602*0b57cec5SDimitry Andric 3603*0b57cec5SDimitry Andric Str.push_back(exp >= 0 ? '+' : '-'); 3604*0b57cec5SDimitry Andric if (exp < 0) exp = -exp; 3605*0b57cec5SDimitry Andric SmallVector<char, 6> expbuf; 3606*0b57cec5SDimitry Andric do { 3607*0b57cec5SDimitry Andric expbuf.push_back((char) ('0' + (exp % 10))); 3608*0b57cec5SDimitry Andric exp /= 10; 3609*0b57cec5SDimitry Andric } while (exp); 3610*0b57cec5SDimitry Andric // Exponent always at least two digits if we do not truncate zeros. 3611*0b57cec5SDimitry Andric if (!TruncateZero && expbuf.size() < 2) 3612*0b57cec5SDimitry Andric expbuf.push_back('0'); 3613*0b57cec5SDimitry Andric for (unsigned I = 0, E = expbuf.size(); I != E; ++I) 3614*0b57cec5SDimitry Andric Str.push_back(expbuf[E-1-I]); 3615*0b57cec5SDimitry Andric return; 3616*0b57cec5SDimitry Andric } 3617*0b57cec5SDimitry Andric 3618*0b57cec5SDimitry Andric // Non-scientific, positive exponents. 3619*0b57cec5SDimitry Andric if (exp >= 0) { 3620*0b57cec5SDimitry Andric for (unsigned I = 0; I != NDigits; ++I) 3621*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-1-I]); 3622*0b57cec5SDimitry Andric for (unsigned I = 0; I != (unsigned) exp; ++I) 3623*0b57cec5SDimitry Andric Str.push_back('0'); 3624*0b57cec5SDimitry Andric return; 3625*0b57cec5SDimitry Andric } 3626*0b57cec5SDimitry Andric 3627*0b57cec5SDimitry Andric // Non-scientific, negative exponents. 3628*0b57cec5SDimitry Andric 3629*0b57cec5SDimitry Andric // The number of digits to the left of the decimal point. 3630*0b57cec5SDimitry Andric int NWholeDigits = exp + (int) NDigits; 3631*0b57cec5SDimitry Andric 3632*0b57cec5SDimitry Andric unsigned I = 0; 3633*0b57cec5SDimitry Andric if (NWholeDigits > 0) { 3634*0b57cec5SDimitry Andric for (; I != (unsigned) NWholeDigits; ++I) 3635*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-I-1]); 3636*0b57cec5SDimitry Andric Str.push_back('.'); 3637*0b57cec5SDimitry Andric } else { 3638*0b57cec5SDimitry Andric unsigned NZeros = 1 + (unsigned) -NWholeDigits; 3639*0b57cec5SDimitry Andric 3640*0b57cec5SDimitry Andric Str.push_back('0'); 3641*0b57cec5SDimitry Andric Str.push_back('.'); 3642*0b57cec5SDimitry Andric for (unsigned Z = 1; Z != NZeros; ++Z) 3643*0b57cec5SDimitry Andric Str.push_back('0'); 3644*0b57cec5SDimitry Andric } 3645*0b57cec5SDimitry Andric 3646*0b57cec5SDimitry Andric for (; I != NDigits; ++I) 3647*0b57cec5SDimitry Andric Str.push_back(buffer[NDigits-I-1]); 3648*0b57cec5SDimitry Andric } 3649*0b57cec5SDimitry Andric 3650*0b57cec5SDimitry Andric bool IEEEFloat::getExactInverse(APFloat *inv) const { 3651*0b57cec5SDimitry Andric // Special floats and denormals have no exact inverse. 3652*0b57cec5SDimitry Andric if (!isFiniteNonZero()) 3653*0b57cec5SDimitry Andric return false; 3654*0b57cec5SDimitry Andric 3655*0b57cec5SDimitry Andric // Check that the number is a power of two by making sure that only the 3656*0b57cec5SDimitry Andric // integer bit is set in the significand. 3657*0b57cec5SDimitry Andric if (significandLSB() != semantics->precision - 1) 3658*0b57cec5SDimitry Andric return false; 3659*0b57cec5SDimitry Andric 3660*0b57cec5SDimitry Andric // Get the inverse. 3661*0b57cec5SDimitry Andric IEEEFloat reciprocal(*semantics, 1ULL); 3662*0b57cec5SDimitry Andric if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK) 3663*0b57cec5SDimitry Andric return false; 3664*0b57cec5SDimitry Andric 3665*0b57cec5SDimitry Andric // Avoid multiplication with a denormal, it is not safe on all platforms and 3666*0b57cec5SDimitry Andric // may be slower than a normal division. 3667*0b57cec5SDimitry Andric if (reciprocal.isDenormal()) 3668*0b57cec5SDimitry Andric return false; 3669*0b57cec5SDimitry Andric 3670*0b57cec5SDimitry Andric assert(reciprocal.isFiniteNonZero() && 3671*0b57cec5SDimitry Andric reciprocal.significandLSB() == reciprocal.semantics->precision - 1); 3672*0b57cec5SDimitry Andric 3673*0b57cec5SDimitry Andric if (inv) 3674*0b57cec5SDimitry Andric *inv = APFloat(reciprocal, *semantics); 3675*0b57cec5SDimitry Andric 3676*0b57cec5SDimitry Andric return true; 3677*0b57cec5SDimitry Andric } 3678*0b57cec5SDimitry Andric 3679*0b57cec5SDimitry Andric bool IEEEFloat::isSignaling() const { 3680*0b57cec5SDimitry Andric if (!isNaN()) 3681*0b57cec5SDimitry Andric return false; 3682*0b57cec5SDimitry Andric 3683*0b57cec5SDimitry Andric // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the 3684*0b57cec5SDimitry Andric // first bit of the trailing significand being 0. 3685*0b57cec5SDimitry Andric return !APInt::tcExtractBit(significandParts(), semantics->precision - 2); 3686*0b57cec5SDimitry Andric } 3687*0b57cec5SDimitry Andric 3688*0b57cec5SDimitry Andric /// IEEE-754R 2008 5.3.1: nextUp/nextDown. 3689*0b57cec5SDimitry Andric /// 3690*0b57cec5SDimitry Andric /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with 3691*0b57cec5SDimitry Andric /// appropriate sign switching before/after the computation. 3692*0b57cec5SDimitry Andric IEEEFloat::opStatus IEEEFloat::next(bool nextDown) { 3693*0b57cec5SDimitry Andric // If we are performing nextDown, swap sign so we have -x. 3694*0b57cec5SDimitry Andric if (nextDown) 3695*0b57cec5SDimitry Andric changeSign(); 3696*0b57cec5SDimitry Andric 3697*0b57cec5SDimitry Andric // Compute nextUp(x) 3698*0b57cec5SDimitry Andric opStatus result = opOK; 3699*0b57cec5SDimitry Andric 3700*0b57cec5SDimitry Andric // Handle each float category separately. 3701*0b57cec5SDimitry Andric switch (category) { 3702*0b57cec5SDimitry Andric case fcInfinity: 3703*0b57cec5SDimitry Andric // nextUp(+inf) = +inf 3704*0b57cec5SDimitry Andric if (!isNegative()) 3705*0b57cec5SDimitry Andric break; 3706*0b57cec5SDimitry Andric // nextUp(-inf) = -getLargest() 3707*0b57cec5SDimitry Andric makeLargest(true); 3708*0b57cec5SDimitry Andric break; 3709*0b57cec5SDimitry Andric case fcNaN: 3710*0b57cec5SDimitry Andric // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag. 3711*0b57cec5SDimitry Andric // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not 3712*0b57cec5SDimitry Andric // change the payload. 3713*0b57cec5SDimitry Andric if (isSignaling()) { 3714*0b57cec5SDimitry Andric result = opInvalidOp; 3715*0b57cec5SDimitry Andric // For consistency, propagate the sign of the sNaN to the qNaN. 3716*0b57cec5SDimitry Andric makeNaN(false, isNegative(), nullptr); 3717*0b57cec5SDimitry Andric } 3718*0b57cec5SDimitry Andric break; 3719*0b57cec5SDimitry Andric case fcZero: 3720*0b57cec5SDimitry Andric // nextUp(pm 0) = +getSmallest() 3721*0b57cec5SDimitry Andric makeSmallest(false); 3722*0b57cec5SDimitry Andric break; 3723*0b57cec5SDimitry Andric case fcNormal: 3724*0b57cec5SDimitry Andric // nextUp(-getSmallest()) = -0 3725*0b57cec5SDimitry Andric if (isSmallest() && isNegative()) { 3726*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 3727*0b57cec5SDimitry Andric category = fcZero; 3728*0b57cec5SDimitry Andric exponent = 0; 3729*0b57cec5SDimitry Andric break; 3730*0b57cec5SDimitry Andric } 3731*0b57cec5SDimitry Andric 3732*0b57cec5SDimitry Andric // nextUp(getLargest()) == INFINITY 3733*0b57cec5SDimitry Andric if (isLargest() && !isNegative()) { 3734*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 3735*0b57cec5SDimitry Andric category = fcInfinity; 3736*0b57cec5SDimitry Andric exponent = semantics->maxExponent + 1; 3737*0b57cec5SDimitry Andric break; 3738*0b57cec5SDimitry Andric } 3739*0b57cec5SDimitry Andric 3740*0b57cec5SDimitry Andric // nextUp(normal) == normal + inc. 3741*0b57cec5SDimitry Andric if (isNegative()) { 3742*0b57cec5SDimitry Andric // If we are negative, we need to decrement the significand. 3743*0b57cec5SDimitry Andric 3744*0b57cec5SDimitry Andric // We only cross a binade boundary that requires adjusting the exponent 3745*0b57cec5SDimitry Andric // if: 3746*0b57cec5SDimitry Andric // 1. exponent != semantics->minExponent. This implies we are not in the 3747*0b57cec5SDimitry Andric // smallest binade or are dealing with denormals. 3748*0b57cec5SDimitry Andric // 2. Our significand excluding the integral bit is all zeros. 3749*0b57cec5SDimitry Andric bool WillCrossBinadeBoundary = 3750*0b57cec5SDimitry Andric exponent != semantics->minExponent && isSignificandAllZeros(); 3751*0b57cec5SDimitry Andric 3752*0b57cec5SDimitry Andric // Decrement the significand. 3753*0b57cec5SDimitry Andric // 3754*0b57cec5SDimitry Andric // We always do this since: 3755*0b57cec5SDimitry Andric // 1. If we are dealing with a non-binade decrement, by definition we 3756*0b57cec5SDimitry Andric // just decrement the significand. 3757*0b57cec5SDimitry Andric // 2. If we are dealing with a normal -> normal binade decrement, since 3758*0b57cec5SDimitry Andric // we have an explicit integral bit the fact that all bits but the 3759*0b57cec5SDimitry Andric // integral bit are zero implies that subtracting one will yield a 3760*0b57cec5SDimitry Andric // significand with 0 integral bit and 1 in all other spots. Thus we 3761*0b57cec5SDimitry Andric // must just adjust the exponent and set the integral bit to 1. 3762*0b57cec5SDimitry Andric // 3. If we are dealing with a normal -> denormal binade decrement, 3763*0b57cec5SDimitry Andric // since we set the integral bit to 0 when we represent denormals, we 3764*0b57cec5SDimitry Andric // just decrement the significand. 3765*0b57cec5SDimitry Andric integerPart *Parts = significandParts(); 3766*0b57cec5SDimitry Andric APInt::tcDecrement(Parts, partCount()); 3767*0b57cec5SDimitry Andric 3768*0b57cec5SDimitry Andric if (WillCrossBinadeBoundary) { 3769*0b57cec5SDimitry Andric // Our result is a normal number. Do the following: 3770*0b57cec5SDimitry Andric // 1. Set the integral bit to 1. 3771*0b57cec5SDimitry Andric // 2. Decrement the exponent. 3772*0b57cec5SDimitry Andric APInt::tcSetBit(Parts, semantics->precision - 1); 3773*0b57cec5SDimitry Andric exponent--; 3774*0b57cec5SDimitry Andric } 3775*0b57cec5SDimitry Andric } else { 3776*0b57cec5SDimitry Andric // If we are positive, we need to increment the significand. 3777*0b57cec5SDimitry Andric 3778*0b57cec5SDimitry Andric // We only cross a binade boundary that requires adjusting the exponent if 3779*0b57cec5SDimitry Andric // the input is not a denormal and all of said input's significand bits 3780*0b57cec5SDimitry Andric // are set. If all of said conditions are true: clear the significand, set 3781*0b57cec5SDimitry Andric // the integral bit to 1, and increment the exponent. If we have a 3782*0b57cec5SDimitry Andric // denormal always increment since moving denormals and the numbers in the 3783*0b57cec5SDimitry Andric // smallest normal binade have the same exponent in our representation. 3784*0b57cec5SDimitry Andric bool WillCrossBinadeBoundary = !isDenormal() && isSignificandAllOnes(); 3785*0b57cec5SDimitry Andric 3786*0b57cec5SDimitry Andric if (WillCrossBinadeBoundary) { 3787*0b57cec5SDimitry Andric integerPart *Parts = significandParts(); 3788*0b57cec5SDimitry Andric APInt::tcSet(Parts, 0, partCount()); 3789*0b57cec5SDimitry Andric APInt::tcSetBit(Parts, semantics->precision - 1); 3790*0b57cec5SDimitry Andric assert(exponent != semantics->maxExponent && 3791*0b57cec5SDimitry Andric "We can not increment an exponent beyond the maxExponent allowed" 3792*0b57cec5SDimitry Andric " by the given floating point semantics."); 3793*0b57cec5SDimitry Andric exponent++; 3794*0b57cec5SDimitry Andric } else { 3795*0b57cec5SDimitry Andric incrementSignificand(); 3796*0b57cec5SDimitry Andric } 3797*0b57cec5SDimitry Andric } 3798*0b57cec5SDimitry Andric break; 3799*0b57cec5SDimitry Andric } 3800*0b57cec5SDimitry Andric 3801*0b57cec5SDimitry Andric // If we are performing nextDown, swap sign so we have -nextUp(-x) 3802*0b57cec5SDimitry Andric if (nextDown) 3803*0b57cec5SDimitry Andric changeSign(); 3804*0b57cec5SDimitry Andric 3805*0b57cec5SDimitry Andric return result; 3806*0b57cec5SDimitry Andric } 3807*0b57cec5SDimitry Andric 3808*0b57cec5SDimitry Andric void IEEEFloat::makeInf(bool Negative) { 3809*0b57cec5SDimitry Andric category = fcInfinity; 3810*0b57cec5SDimitry Andric sign = Negative; 3811*0b57cec5SDimitry Andric exponent = semantics->maxExponent + 1; 3812*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 3813*0b57cec5SDimitry Andric } 3814*0b57cec5SDimitry Andric 3815*0b57cec5SDimitry Andric void IEEEFloat::makeZero(bool Negative) { 3816*0b57cec5SDimitry Andric category = fcZero; 3817*0b57cec5SDimitry Andric sign = Negative; 3818*0b57cec5SDimitry Andric exponent = semantics->minExponent-1; 3819*0b57cec5SDimitry Andric APInt::tcSet(significandParts(), 0, partCount()); 3820*0b57cec5SDimitry Andric } 3821*0b57cec5SDimitry Andric 3822*0b57cec5SDimitry Andric void IEEEFloat::makeQuiet() { 3823*0b57cec5SDimitry Andric assert(isNaN()); 3824*0b57cec5SDimitry Andric APInt::tcSetBit(significandParts(), semantics->precision - 2); 3825*0b57cec5SDimitry Andric } 3826*0b57cec5SDimitry Andric 3827*0b57cec5SDimitry Andric int ilogb(const IEEEFloat &Arg) { 3828*0b57cec5SDimitry Andric if (Arg.isNaN()) 3829*0b57cec5SDimitry Andric return IEEEFloat::IEK_NaN; 3830*0b57cec5SDimitry Andric if (Arg.isZero()) 3831*0b57cec5SDimitry Andric return IEEEFloat::IEK_Zero; 3832*0b57cec5SDimitry Andric if (Arg.isInfinity()) 3833*0b57cec5SDimitry Andric return IEEEFloat::IEK_Inf; 3834*0b57cec5SDimitry Andric if (!Arg.isDenormal()) 3835*0b57cec5SDimitry Andric return Arg.exponent; 3836*0b57cec5SDimitry Andric 3837*0b57cec5SDimitry Andric IEEEFloat Normalized(Arg); 3838*0b57cec5SDimitry Andric int SignificandBits = Arg.getSemantics().precision - 1; 3839*0b57cec5SDimitry Andric 3840*0b57cec5SDimitry Andric Normalized.exponent += SignificandBits; 3841*0b57cec5SDimitry Andric Normalized.normalize(IEEEFloat::rmNearestTiesToEven, lfExactlyZero); 3842*0b57cec5SDimitry Andric return Normalized.exponent - SignificandBits; 3843*0b57cec5SDimitry Andric } 3844*0b57cec5SDimitry Andric 3845*0b57cec5SDimitry Andric IEEEFloat scalbn(IEEEFloat X, int Exp, IEEEFloat::roundingMode RoundingMode) { 3846*0b57cec5SDimitry Andric auto MaxExp = X.getSemantics().maxExponent; 3847*0b57cec5SDimitry Andric auto MinExp = X.getSemantics().minExponent; 3848*0b57cec5SDimitry Andric 3849*0b57cec5SDimitry Andric // If Exp is wildly out-of-scale, simply adding it to X.exponent will 3850*0b57cec5SDimitry Andric // overflow; clamp it to a safe range before adding, but ensure that the range 3851*0b57cec5SDimitry Andric // is large enough that the clamp does not change the result. The range we 3852*0b57cec5SDimitry Andric // need to support is the difference between the largest possible exponent and 3853*0b57cec5SDimitry Andric // the normalized exponent of half the smallest denormal. 3854*0b57cec5SDimitry Andric 3855*0b57cec5SDimitry Andric int SignificandBits = X.getSemantics().precision - 1; 3856*0b57cec5SDimitry Andric int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1; 3857*0b57cec5SDimitry Andric 3858*0b57cec5SDimitry Andric // Clamp to one past the range ends to let normalize handle overlflow. 3859*0b57cec5SDimitry Andric X.exponent += std::min(std::max(Exp, -MaxIncrement - 1), MaxIncrement); 3860*0b57cec5SDimitry Andric X.normalize(RoundingMode, lfExactlyZero); 3861*0b57cec5SDimitry Andric if (X.isNaN()) 3862*0b57cec5SDimitry Andric X.makeQuiet(); 3863*0b57cec5SDimitry Andric return X; 3864*0b57cec5SDimitry Andric } 3865*0b57cec5SDimitry Andric 3866*0b57cec5SDimitry Andric IEEEFloat frexp(const IEEEFloat &Val, int &Exp, IEEEFloat::roundingMode RM) { 3867*0b57cec5SDimitry Andric Exp = ilogb(Val); 3868*0b57cec5SDimitry Andric 3869*0b57cec5SDimitry Andric // Quiet signalling nans. 3870*0b57cec5SDimitry Andric if (Exp == IEEEFloat::IEK_NaN) { 3871*0b57cec5SDimitry Andric IEEEFloat Quiet(Val); 3872*0b57cec5SDimitry Andric Quiet.makeQuiet(); 3873*0b57cec5SDimitry Andric return Quiet; 3874*0b57cec5SDimitry Andric } 3875*0b57cec5SDimitry Andric 3876*0b57cec5SDimitry Andric if (Exp == IEEEFloat::IEK_Inf) 3877*0b57cec5SDimitry Andric return Val; 3878*0b57cec5SDimitry Andric 3879*0b57cec5SDimitry Andric // 1 is added because frexp is defined to return a normalized fraction in 3880*0b57cec5SDimitry Andric // +/-[0.5, 1.0), rather than the usual +/-[1.0, 2.0). 3881*0b57cec5SDimitry Andric Exp = Exp == IEEEFloat::IEK_Zero ? 0 : Exp + 1; 3882*0b57cec5SDimitry Andric return scalbn(Val, -Exp, RM); 3883*0b57cec5SDimitry Andric } 3884*0b57cec5SDimitry Andric 3885*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S) 3886*0b57cec5SDimitry Andric : Semantics(&S), 3887*0b57cec5SDimitry Andric Floats(new APFloat[2]{APFloat(semIEEEdouble), APFloat(semIEEEdouble)}) { 3888*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 3889*0b57cec5SDimitry Andric } 3890*0b57cec5SDimitry Andric 3891*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, uninitializedTag) 3892*0b57cec5SDimitry Andric : Semantics(&S), 3893*0b57cec5SDimitry Andric Floats(new APFloat[2]{APFloat(semIEEEdouble, uninitialized), 3894*0b57cec5SDimitry Andric APFloat(semIEEEdouble, uninitialized)}) { 3895*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 3896*0b57cec5SDimitry Andric } 3897*0b57cec5SDimitry Andric 3898*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, integerPart I) 3899*0b57cec5SDimitry Andric : Semantics(&S), Floats(new APFloat[2]{APFloat(semIEEEdouble, I), 3900*0b57cec5SDimitry Andric APFloat(semIEEEdouble)}) { 3901*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 3902*0b57cec5SDimitry Andric } 3903*0b57cec5SDimitry Andric 3904*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, const APInt &I) 3905*0b57cec5SDimitry Andric : Semantics(&S), 3906*0b57cec5SDimitry Andric Floats(new APFloat[2]{ 3907*0b57cec5SDimitry Andric APFloat(semIEEEdouble, APInt(64, I.getRawData()[0])), 3908*0b57cec5SDimitry Andric APFloat(semIEEEdouble, APInt(64, I.getRawData()[1]))}) { 3909*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 3910*0b57cec5SDimitry Andric } 3911*0b57cec5SDimitry Andric 3912*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, APFloat &&First, 3913*0b57cec5SDimitry Andric APFloat &&Second) 3914*0b57cec5SDimitry Andric : Semantics(&S), 3915*0b57cec5SDimitry Andric Floats(new APFloat[2]{std::move(First), std::move(Second)}) { 3916*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 3917*0b57cec5SDimitry Andric assert(&Floats[0].getSemantics() == &semIEEEdouble); 3918*0b57cec5SDimitry Andric assert(&Floats[1].getSemantics() == &semIEEEdouble); 3919*0b57cec5SDimitry Andric } 3920*0b57cec5SDimitry Andric 3921*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(const DoubleAPFloat &RHS) 3922*0b57cec5SDimitry Andric : Semantics(RHS.Semantics), 3923*0b57cec5SDimitry Andric Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]), 3924*0b57cec5SDimitry Andric APFloat(RHS.Floats[1])} 3925*0b57cec5SDimitry Andric : nullptr) { 3926*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 3927*0b57cec5SDimitry Andric } 3928*0b57cec5SDimitry Andric 3929*0b57cec5SDimitry Andric DoubleAPFloat::DoubleAPFloat(DoubleAPFloat &&RHS) 3930*0b57cec5SDimitry Andric : Semantics(RHS.Semantics), Floats(std::move(RHS.Floats)) { 3931*0b57cec5SDimitry Andric RHS.Semantics = &semBogus; 3932*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble); 3933*0b57cec5SDimitry Andric } 3934*0b57cec5SDimitry Andric 3935*0b57cec5SDimitry Andric DoubleAPFloat &DoubleAPFloat::operator=(const DoubleAPFloat &RHS) { 3936*0b57cec5SDimitry Andric if (Semantics == RHS.Semantics && RHS.Floats) { 3937*0b57cec5SDimitry Andric Floats[0] = RHS.Floats[0]; 3938*0b57cec5SDimitry Andric Floats[1] = RHS.Floats[1]; 3939*0b57cec5SDimitry Andric } else if (this != &RHS) { 3940*0b57cec5SDimitry Andric this->~DoubleAPFloat(); 3941*0b57cec5SDimitry Andric new (this) DoubleAPFloat(RHS); 3942*0b57cec5SDimitry Andric } 3943*0b57cec5SDimitry Andric return *this; 3944*0b57cec5SDimitry Andric } 3945*0b57cec5SDimitry Andric 3946*0b57cec5SDimitry Andric // Implement addition, subtraction, multiplication and division based on: 3947*0b57cec5SDimitry Andric // "Software for Doubled-Precision Floating-Point Computations", 3948*0b57cec5SDimitry Andric // by Seppo Linnainmaa, ACM TOMS vol 7 no 3, September 1981, pages 272-283. 3949*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::addImpl(const APFloat &a, const APFloat &aa, 3950*0b57cec5SDimitry Andric const APFloat &c, const APFloat &cc, 3951*0b57cec5SDimitry Andric roundingMode RM) { 3952*0b57cec5SDimitry Andric int Status = opOK; 3953*0b57cec5SDimitry Andric APFloat z = a; 3954*0b57cec5SDimitry Andric Status |= z.add(c, RM); 3955*0b57cec5SDimitry Andric if (!z.isFinite()) { 3956*0b57cec5SDimitry Andric if (!z.isInfinity()) { 3957*0b57cec5SDimitry Andric Floats[0] = std::move(z); 3958*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 3959*0b57cec5SDimitry Andric return (opStatus)Status; 3960*0b57cec5SDimitry Andric } 3961*0b57cec5SDimitry Andric Status = opOK; 3962*0b57cec5SDimitry Andric auto AComparedToC = a.compareAbsoluteValue(c); 3963*0b57cec5SDimitry Andric z = cc; 3964*0b57cec5SDimitry Andric Status |= z.add(aa, RM); 3965*0b57cec5SDimitry Andric if (AComparedToC == APFloat::cmpGreaterThan) { 3966*0b57cec5SDimitry Andric // z = cc + aa + c + a; 3967*0b57cec5SDimitry Andric Status |= z.add(c, RM); 3968*0b57cec5SDimitry Andric Status |= z.add(a, RM); 3969*0b57cec5SDimitry Andric } else { 3970*0b57cec5SDimitry Andric // z = cc + aa + a + c; 3971*0b57cec5SDimitry Andric Status |= z.add(a, RM); 3972*0b57cec5SDimitry Andric Status |= z.add(c, RM); 3973*0b57cec5SDimitry Andric } 3974*0b57cec5SDimitry Andric if (!z.isFinite()) { 3975*0b57cec5SDimitry Andric Floats[0] = std::move(z); 3976*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 3977*0b57cec5SDimitry Andric return (opStatus)Status; 3978*0b57cec5SDimitry Andric } 3979*0b57cec5SDimitry Andric Floats[0] = z; 3980*0b57cec5SDimitry Andric APFloat zz = aa; 3981*0b57cec5SDimitry Andric Status |= zz.add(cc, RM); 3982*0b57cec5SDimitry Andric if (AComparedToC == APFloat::cmpGreaterThan) { 3983*0b57cec5SDimitry Andric // Floats[1] = a - z + c + zz; 3984*0b57cec5SDimitry Andric Floats[1] = a; 3985*0b57cec5SDimitry Andric Status |= Floats[1].subtract(z, RM); 3986*0b57cec5SDimitry Andric Status |= Floats[1].add(c, RM); 3987*0b57cec5SDimitry Andric Status |= Floats[1].add(zz, RM); 3988*0b57cec5SDimitry Andric } else { 3989*0b57cec5SDimitry Andric // Floats[1] = c - z + a + zz; 3990*0b57cec5SDimitry Andric Floats[1] = c; 3991*0b57cec5SDimitry Andric Status |= Floats[1].subtract(z, RM); 3992*0b57cec5SDimitry Andric Status |= Floats[1].add(a, RM); 3993*0b57cec5SDimitry Andric Status |= Floats[1].add(zz, RM); 3994*0b57cec5SDimitry Andric } 3995*0b57cec5SDimitry Andric } else { 3996*0b57cec5SDimitry Andric // q = a - z; 3997*0b57cec5SDimitry Andric APFloat q = a; 3998*0b57cec5SDimitry Andric Status |= q.subtract(z, RM); 3999*0b57cec5SDimitry Andric 4000*0b57cec5SDimitry Andric // zz = q + c + (a - (q + z)) + aa + cc; 4001*0b57cec5SDimitry Andric // Compute a - (q + z) as -((q + z) - a) to avoid temporary copies. 4002*0b57cec5SDimitry Andric auto zz = q; 4003*0b57cec5SDimitry Andric Status |= zz.add(c, RM); 4004*0b57cec5SDimitry Andric Status |= q.add(z, RM); 4005*0b57cec5SDimitry Andric Status |= q.subtract(a, RM); 4006*0b57cec5SDimitry Andric q.changeSign(); 4007*0b57cec5SDimitry Andric Status |= zz.add(q, RM); 4008*0b57cec5SDimitry Andric Status |= zz.add(aa, RM); 4009*0b57cec5SDimitry Andric Status |= zz.add(cc, RM); 4010*0b57cec5SDimitry Andric if (zz.isZero() && !zz.isNegative()) { 4011*0b57cec5SDimitry Andric Floats[0] = std::move(z); 4012*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4013*0b57cec5SDimitry Andric return opOK; 4014*0b57cec5SDimitry Andric } 4015*0b57cec5SDimitry Andric Floats[0] = z; 4016*0b57cec5SDimitry Andric Status |= Floats[0].add(zz, RM); 4017*0b57cec5SDimitry Andric if (!Floats[0].isFinite()) { 4018*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4019*0b57cec5SDimitry Andric return (opStatus)Status; 4020*0b57cec5SDimitry Andric } 4021*0b57cec5SDimitry Andric Floats[1] = std::move(z); 4022*0b57cec5SDimitry Andric Status |= Floats[1].subtract(Floats[0], RM); 4023*0b57cec5SDimitry Andric Status |= Floats[1].add(zz, RM); 4024*0b57cec5SDimitry Andric } 4025*0b57cec5SDimitry Andric return (opStatus)Status; 4026*0b57cec5SDimitry Andric } 4027*0b57cec5SDimitry Andric 4028*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::addWithSpecial(const DoubleAPFloat &LHS, 4029*0b57cec5SDimitry Andric const DoubleAPFloat &RHS, 4030*0b57cec5SDimitry Andric DoubleAPFloat &Out, 4031*0b57cec5SDimitry Andric roundingMode RM) { 4032*0b57cec5SDimitry Andric if (LHS.getCategory() == fcNaN) { 4033*0b57cec5SDimitry Andric Out = LHS; 4034*0b57cec5SDimitry Andric return opOK; 4035*0b57cec5SDimitry Andric } 4036*0b57cec5SDimitry Andric if (RHS.getCategory() == fcNaN) { 4037*0b57cec5SDimitry Andric Out = RHS; 4038*0b57cec5SDimitry Andric return opOK; 4039*0b57cec5SDimitry Andric } 4040*0b57cec5SDimitry Andric if (LHS.getCategory() == fcZero) { 4041*0b57cec5SDimitry Andric Out = RHS; 4042*0b57cec5SDimitry Andric return opOK; 4043*0b57cec5SDimitry Andric } 4044*0b57cec5SDimitry Andric if (RHS.getCategory() == fcZero) { 4045*0b57cec5SDimitry Andric Out = LHS; 4046*0b57cec5SDimitry Andric return opOK; 4047*0b57cec5SDimitry Andric } 4048*0b57cec5SDimitry Andric if (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcInfinity && 4049*0b57cec5SDimitry Andric LHS.isNegative() != RHS.isNegative()) { 4050*0b57cec5SDimitry Andric Out.makeNaN(false, Out.isNegative(), nullptr); 4051*0b57cec5SDimitry Andric return opInvalidOp; 4052*0b57cec5SDimitry Andric } 4053*0b57cec5SDimitry Andric if (LHS.getCategory() == fcInfinity) { 4054*0b57cec5SDimitry Andric Out = LHS; 4055*0b57cec5SDimitry Andric return opOK; 4056*0b57cec5SDimitry Andric } 4057*0b57cec5SDimitry Andric if (RHS.getCategory() == fcInfinity) { 4058*0b57cec5SDimitry Andric Out = RHS; 4059*0b57cec5SDimitry Andric return opOK; 4060*0b57cec5SDimitry Andric } 4061*0b57cec5SDimitry Andric assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal); 4062*0b57cec5SDimitry Andric 4063*0b57cec5SDimitry Andric APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]), C(RHS.Floats[0]), 4064*0b57cec5SDimitry Andric CC(RHS.Floats[1]); 4065*0b57cec5SDimitry Andric assert(&A.getSemantics() == &semIEEEdouble); 4066*0b57cec5SDimitry Andric assert(&AA.getSemantics() == &semIEEEdouble); 4067*0b57cec5SDimitry Andric assert(&C.getSemantics() == &semIEEEdouble); 4068*0b57cec5SDimitry Andric assert(&CC.getSemantics() == &semIEEEdouble); 4069*0b57cec5SDimitry Andric assert(&Out.Floats[0].getSemantics() == &semIEEEdouble); 4070*0b57cec5SDimitry Andric assert(&Out.Floats[1].getSemantics() == &semIEEEdouble); 4071*0b57cec5SDimitry Andric return Out.addImpl(A, AA, C, CC, RM); 4072*0b57cec5SDimitry Andric } 4073*0b57cec5SDimitry Andric 4074*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::add(const DoubleAPFloat &RHS, 4075*0b57cec5SDimitry Andric roundingMode RM) { 4076*0b57cec5SDimitry Andric return addWithSpecial(*this, RHS, *this, RM); 4077*0b57cec5SDimitry Andric } 4078*0b57cec5SDimitry Andric 4079*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::subtract(const DoubleAPFloat &RHS, 4080*0b57cec5SDimitry Andric roundingMode RM) { 4081*0b57cec5SDimitry Andric changeSign(); 4082*0b57cec5SDimitry Andric auto Ret = add(RHS, RM); 4083*0b57cec5SDimitry Andric changeSign(); 4084*0b57cec5SDimitry Andric return Ret; 4085*0b57cec5SDimitry Andric } 4086*0b57cec5SDimitry Andric 4087*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::multiply(const DoubleAPFloat &RHS, 4088*0b57cec5SDimitry Andric APFloat::roundingMode RM) { 4089*0b57cec5SDimitry Andric const auto &LHS = *this; 4090*0b57cec5SDimitry Andric auto &Out = *this; 4091*0b57cec5SDimitry Andric /* Interesting observation: For special categories, finding the lowest 4092*0b57cec5SDimitry Andric common ancestor of the following layered graph gives the correct 4093*0b57cec5SDimitry Andric return category: 4094*0b57cec5SDimitry Andric 4095*0b57cec5SDimitry Andric NaN 4096*0b57cec5SDimitry Andric / \ 4097*0b57cec5SDimitry Andric Zero Inf 4098*0b57cec5SDimitry Andric \ / 4099*0b57cec5SDimitry Andric Normal 4100*0b57cec5SDimitry Andric 4101*0b57cec5SDimitry Andric e.g. NaN * NaN = NaN 4102*0b57cec5SDimitry Andric Zero * Inf = NaN 4103*0b57cec5SDimitry Andric Normal * Zero = Zero 4104*0b57cec5SDimitry Andric Normal * Inf = Inf 4105*0b57cec5SDimitry Andric */ 4106*0b57cec5SDimitry Andric if (LHS.getCategory() == fcNaN) { 4107*0b57cec5SDimitry Andric Out = LHS; 4108*0b57cec5SDimitry Andric return opOK; 4109*0b57cec5SDimitry Andric } 4110*0b57cec5SDimitry Andric if (RHS.getCategory() == fcNaN) { 4111*0b57cec5SDimitry Andric Out = RHS; 4112*0b57cec5SDimitry Andric return opOK; 4113*0b57cec5SDimitry Andric } 4114*0b57cec5SDimitry Andric if ((LHS.getCategory() == fcZero && RHS.getCategory() == fcInfinity) || 4115*0b57cec5SDimitry Andric (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcZero)) { 4116*0b57cec5SDimitry Andric Out.makeNaN(false, false, nullptr); 4117*0b57cec5SDimitry Andric return opOK; 4118*0b57cec5SDimitry Andric } 4119*0b57cec5SDimitry Andric if (LHS.getCategory() == fcZero || LHS.getCategory() == fcInfinity) { 4120*0b57cec5SDimitry Andric Out = LHS; 4121*0b57cec5SDimitry Andric return opOK; 4122*0b57cec5SDimitry Andric } 4123*0b57cec5SDimitry Andric if (RHS.getCategory() == fcZero || RHS.getCategory() == fcInfinity) { 4124*0b57cec5SDimitry Andric Out = RHS; 4125*0b57cec5SDimitry Andric return opOK; 4126*0b57cec5SDimitry Andric } 4127*0b57cec5SDimitry Andric assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal && 4128*0b57cec5SDimitry Andric "Special cases not handled exhaustively"); 4129*0b57cec5SDimitry Andric 4130*0b57cec5SDimitry Andric int Status = opOK; 4131*0b57cec5SDimitry Andric APFloat A = Floats[0], B = Floats[1], C = RHS.Floats[0], D = RHS.Floats[1]; 4132*0b57cec5SDimitry Andric // t = a * c 4133*0b57cec5SDimitry Andric APFloat T = A; 4134*0b57cec5SDimitry Andric Status |= T.multiply(C, RM); 4135*0b57cec5SDimitry Andric if (!T.isFiniteNonZero()) { 4136*0b57cec5SDimitry Andric Floats[0] = T; 4137*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4138*0b57cec5SDimitry Andric return (opStatus)Status; 4139*0b57cec5SDimitry Andric } 4140*0b57cec5SDimitry Andric 4141*0b57cec5SDimitry Andric // tau = fmsub(a, c, t), that is -fmadd(-a, c, t). 4142*0b57cec5SDimitry Andric APFloat Tau = A; 4143*0b57cec5SDimitry Andric T.changeSign(); 4144*0b57cec5SDimitry Andric Status |= Tau.fusedMultiplyAdd(C, T, RM); 4145*0b57cec5SDimitry Andric T.changeSign(); 4146*0b57cec5SDimitry Andric { 4147*0b57cec5SDimitry Andric // v = a * d 4148*0b57cec5SDimitry Andric APFloat V = A; 4149*0b57cec5SDimitry Andric Status |= V.multiply(D, RM); 4150*0b57cec5SDimitry Andric // w = b * c 4151*0b57cec5SDimitry Andric APFloat W = B; 4152*0b57cec5SDimitry Andric Status |= W.multiply(C, RM); 4153*0b57cec5SDimitry Andric Status |= V.add(W, RM); 4154*0b57cec5SDimitry Andric // tau += v + w 4155*0b57cec5SDimitry Andric Status |= Tau.add(V, RM); 4156*0b57cec5SDimitry Andric } 4157*0b57cec5SDimitry Andric // u = t + tau 4158*0b57cec5SDimitry Andric APFloat U = T; 4159*0b57cec5SDimitry Andric Status |= U.add(Tau, RM); 4160*0b57cec5SDimitry Andric 4161*0b57cec5SDimitry Andric Floats[0] = U; 4162*0b57cec5SDimitry Andric if (!U.isFinite()) { 4163*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4164*0b57cec5SDimitry Andric } else { 4165*0b57cec5SDimitry Andric // Floats[1] = (t - u) + tau 4166*0b57cec5SDimitry Andric Status |= T.subtract(U, RM); 4167*0b57cec5SDimitry Andric Status |= T.add(Tau, RM); 4168*0b57cec5SDimitry Andric Floats[1] = T; 4169*0b57cec5SDimitry Andric } 4170*0b57cec5SDimitry Andric return (opStatus)Status; 4171*0b57cec5SDimitry Andric } 4172*0b57cec5SDimitry Andric 4173*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::divide(const DoubleAPFloat &RHS, 4174*0b57cec5SDimitry Andric APFloat::roundingMode RM) { 4175*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4176*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4177*0b57cec5SDimitry Andric auto Ret = 4178*0b57cec5SDimitry Andric Tmp.divide(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()), RM); 4179*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4180*0b57cec5SDimitry Andric return Ret; 4181*0b57cec5SDimitry Andric } 4182*0b57cec5SDimitry Andric 4183*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::remainder(const DoubleAPFloat &RHS) { 4184*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4185*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4186*0b57cec5SDimitry Andric auto Ret = 4187*0b57cec5SDimitry Andric Tmp.remainder(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); 4188*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4189*0b57cec5SDimitry Andric return Ret; 4190*0b57cec5SDimitry Andric } 4191*0b57cec5SDimitry Andric 4192*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::mod(const DoubleAPFloat &RHS) { 4193*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4194*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4195*0b57cec5SDimitry Andric auto Ret = Tmp.mod(APFloat(semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); 4196*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4197*0b57cec5SDimitry Andric return Ret; 4198*0b57cec5SDimitry Andric } 4199*0b57cec5SDimitry Andric 4200*0b57cec5SDimitry Andric APFloat::opStatus 4201*0b57cec5SDimitry Andric DoubleAPFloat::fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, 4202*0b57cec5SDimitry Andric const DoubleAPFloat &Addend, 4203*0b57cec5SDimitry Andric APFloat::roundingMode RM) { 4204*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4205*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4206*0b57cec5SDimitry Andric auto Ret = Tmp.fusedMultiplyAdd( 4207*0b57cec5SDimitry Andric APFloat(semPPCDoubleDoubleLegacy, Multiplicand.bitcastToAPInt()), 4208*0b57cec5SDimitry Andric APFloat(semPPCDoubleDoubleLegacy, Addend.bitcastToAPInt()), RM); 4209*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4210*0b57cec5SDimitry Andric return Ret; 4211*0b57cec5SDimitry Andric } 4212*0b57cec5SDimitry Andric 4213*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::roundToIntegral(APFloat::roundingMode RM) { 4214*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4215*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4216*0b57cec5SDimitry Andric auto Ret = Tmp.roundToIntegral(RM); 4217*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4218*0b57cec5SDimitry Andric return Ret; 4219*0b57cec5SDimitry Andric } 4220*0b57cec5SDimitry Andric 4221*0b57cec5SDimitry Andric void DoubleAPFloat::changeSign() { 4222*0b57cec5SDimitry Andric Floats[0].changeSign(); 4223*0b57cec5SDimitry Andric Floats[1].changeSign(); 4224*0b57cec5SDimitry Andric } 4225*0b57cec5SDimitry Andric 4226*0b57cec5SDimitry Andric APFloat::cmpResult 4227*0b57cec5SDimitry Andric DoubleAPFloat::compareAbsoluteValue(const DoubleAPFloat &RHS) const { 4228*0b57cec5SDimitry Andric auto Result = Floats[0].compareAbsoluteValue(RHS.Floats[0]); 4229*0b57cec5SDimitry Andric if (Result != cmpEqual) 4230*0b57cec5SDimitry Andric return Result; 4231*0b57cec5SDimitry Andric Result = Floats[1].compareAbsoluteValue(RHS.Floats[1]); 4232*0b57cec5SDimitry Andric if (Result == cmpLessThan || Result == cmpGreaterThan) { 4233*0b57cec5SDimitry Andric auto Against = Floats[0].isNegative() ^ Floats[1].isNegative(); 4234*0b57cec5SDimitry Andric auto RHSAgainst = RHS.Floats[0].isNegative() ^ RHS.Floats[1].isNegative(); 4235*0b57cec5SDimitry Andric if (Against && !RHSAgainst) 4236*0b57cec5SDimitry Andric return cmpLessThan; 4237*0b57cec5SDimitry Andric if (!Against && RHSAgainst) 4238*0b57cec5SDimitry Andric return cmpGreaterThan; 4239*0b57cec5SDimitry Andric if (!Against && !RHSAgainst) 4240*0b57cec5SDimitry Andric return Result; 4241*0b57cec5SDimitry Andric if (Against && RHSAgainst) 4242*0b57cec5SDimitry Andric return (cmpResult)(cmpLessThan + cmpGreaterThan - Result); 4243*0b57cec5SDimitry Andric } 4244*0b57cec5SDimitry Andric return Result; 4245*0b57cec5SDimitry Andric } 4246*0b57cec5SDimitry Andric 4247*0b57cec5SDimitry Andric APFloat::fltCategory DoubleAPFloat::getCategory() const { 4248*0b57cec5SDimitry Andric return Floats[0].getCategory(); 4249*0b57cec5SDimitry Andric } 4250*0b57cec5SDimitry Andric 4251*0b57cec5SDimitry Andric bool DoubleAPFloat::isNegative() const { return Floats[0].isNegative(); } 4252*0b57cec5SDimitry Andric 4253*0b57cec5SDimitry Andric void DoubleAPFloat::makeInf(bool Neg) { 4254*0b57cec5SDimitry Andric Floats[0].makeInf(Neg); 4255*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4256*0b57cec5SDimitry Andric } 4257*0b57cec5SDimitry Andric 4258*0b57cec5SDimitry Andric void DoubleAPFloat::makeZero(bool Neg) { 4259*0b57cec5SDimitry Andric Floats[0].makeZero(Neg); 4260*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4261*0b57cec5SDimitry Andric } 4262*0b57cec5SDimitry Andric 4263*0b57cec5SDimitry Andric void DoubleAPFloat::makeLargest(bool Neg) { 4264*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4265*0b57cec5SDimitry Andric Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x7fefffffffffffffull)); 4266*0b57cec5SDimitry Andric Floats[1] = APFloat(semIEEEdouble, APInt(64, 0x7c8ffffffffffffeull)); 4267*0b57cec5SDimitry Andric if (Neg) 4268*0b57cec5SDimitry Andric changeSign(); 4269*0b57cec5SDimitry Andric } 4270*0b57cec5SDimitry Andric 4271*0b57cec5SDimitry Andric void DoubleAPFloat::makeSmallest(bool Neg) { 4272*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4273*0b57cec5SDimitry Andric Floats[0].makeSmallest(Neg); 4274*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4275*0b57cec5SDimitry Andric } 4276*0b57cec5SDimitry Andric 4277*0b57cec5SDimitry Andric void DoubleAPFloat::makeSmallestNormalized(bool Neg) { 4278*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4279*0b57cec5SDimitry Andric Floats[0] = APFloat(semIEEEdouble, APInt(64, 0x0360000000000000ull)); 4280*0b57cec5SDimitry Andric if (Neg) 4281*0b57cec5SDimitry Andric Floats[0].changeSign(); 4282*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4283*0b57cec5SDimitry Andric } 4284*0b57cec5SDimitry Andric 4285*0b57cec5SDimitry Andric void DoubleAPFloat::makeNaN(bool SNaN, bool Neg, const APInt *fill) { 4286*0b57cec5SDimitry Andric Floats[0].makeNaN(SNaN, Neg, fill); 4287*0b57cec5SDimitry Andric Floats[1].makeZero(/* Neg = */ false); 4288*0b57cec5SDimitry Andric } 4289*0b57cec5SDimitry Andric 4290*0b57cec5SDimitry Andric APFloat::cmpResult DoubleAPFloat::compare(const DoubleAPFloat &RHS) const { 4291*0b57cec5SDimitry Andric auto Result = Floats[0].compare(RHS.Floats[0]); 4292*0b57cec5SDimitry Andric // |Float[0]| > |Float[1]| 4293*0b57cec5SDimitry Andric if (Result == APFloat::cmpEqual) 4294*0b57cec5SDimitry Andric return Floats[1].compare(RHS.Floats[1]); 4295*0b57cec5SDimitry Andric return Result; 4296*0b57cec5SDimitry Andric } 4297*0b57cec5SDimitry Andric 4298*0b57cec5SDimitry Andric bool DoubleAPFloat::bitwiseIsEqual(const DoubleAPFloat &RHS) const { 4299*0b57cec5SDimitry Andric return Floats[0].bitwiseIsEqual(RHS.Floats[0]) && 4300*0b57cec5SDimitry Andric Floats[1].bitwiseIsEqual(RHS.Floats[1]); 4301*0b57cec5SDimitry Andric } 4302*0b57cec5SDimitry Andric 4303*0b57cec5SDimitry Andric hash_code hash_value(const DoubleAPFloat &Arg) { 4304*0b57cec5SDimitry Andric if (Arg.Floats) 4305*0b57cec5SDimitry Andric return hash_combine(hash_value(Arg.Floats[0]), hash_value(Arg.Floats[1])); 4306*0b57cec5SDimitry Andric return hash_combine(Arg.Semantics); 4307*0b57cec5SDimitry Andric } 4308*0b57cec5SDimitry Andric 4309*0b57cec5SDimitry Andric APInt DoubleAPFloat::bitcastToAPInt() const { 4310*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4311*0b57cec5SDimitry Andric uint64_t Data[] = { 4312*0b57cec5SDimitry Andric Floats[0].bitcastToAPInt().getRawData()[0], 4313*0b57cec5SDimitry Andric Floats[1].bitcastToAPInt().getRawData()[0], 4314*0b57cec5SDimitry Andric }; 4315*0b57cec5SDimitry Andric return APInt(128, 2, Data); 4316*0b57cec5SDimitry Andric } 4317*0b57cec5SDimitry Andric 4318*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::convertFromString(StringRef S, 4319*0b57cec5SDimitry Andric roundingMode RM) { 4320*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4321*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy); 4322*0b57cec5SDimitry Andric auto Ret = Tmp.convertFromString(S, RM); 4323*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4324*0b57cec5SDimitry Andric return Ret; 4325*0b57cec5SDimitry Andric } 4326*0b57cec5SDimitry Andric 4327*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::next(bool nextDown) { 4328*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4329*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4330*0b57cec5SDimitry Andric auto Ret = Tmp.next(nextDown); 4331*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4332*0b57cec5SDimitry Andric return Ret; 4333*0b57cec5SDimitry Andric } 4334*0b57cec5SDimitry Andric 4335*0b57cec5SDimitry Andric APFloat::opStatus 4336*0b57cec5SDimitry Andric DoubleAPFloat::convertToInteger(MutableArrayRef<integerPart> Input, 4337*0b57cec5SDimitry Andric unsigned int Width, bool IsSigned, 4338*0b57cec5SDimitry Andric roundingMode RM, bool *IsExact) const { 4339*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4340*0b57cec5SDimitry Andric return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) 4341*0b57cec5SDimitry Andric .convertToInteger(Input, Width, IsSigned, RM, IsExact); 4342*0b57cec5SDimitry Andric } 4343*0b57cec5SDimitry Andric 4344*0b57cec5SDimitry Andric APFloat::opStatus DoubleAPFloat::convertFromAPInt(const APInt &Input, 4345*0b57cec5SDimitry Andric bool IsSigned, 4346*0b57cec5SDimitry Andric roundingMode RM) { 4347*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4348*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy); 4349*0b57cec5SDimitry Andric auto Ret = Tmp.convertFromAPInt(Input, IsSigned, RM); 4350*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4351*0b57cec5SDimitry Andric return Ret; 4352*0b57cec5SDimitry Andric } 4353*0b57cec5SDimitry Andric 4354*0b57cec5SDimitry Andric APFloat::opStatus 4355*0b57cec5SDimitry Andric DoubleAPFloat::convertFromSignExtendedInteger(const integerPart *Input, 4356*0b57cec5SDimitry Andric unsigned int InputSize, 4357*0b57cec5SDimitry Andric bool IsSigned, roundingMode RM) { 4358*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4359*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy); 4360*0b57cec5SDimitry Andric auto Ret = Tmp.convertFromSignExtendedInteger(Input, InputSize, IsSigned, RM); 4361*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4362*0b57cec5SDimitry Andric return Ret; 4363*0b57cec5SDimitry Andric } 4364*0b57cec5SDimitry Andric 4365*0b57cec5SDimitry Andric APFloat::opStatus 4366*0b57cec5SDimitry Andric DoubleAPFloat::convertFromZeroExtendedInteger(const integerPart *Input, 4367*0b57cec5SDimitry Andric unsigned int InputSize, 4368*0b57cec5SDimitry Andric bool IsSigned, roundingMode RM) { 4369*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4370*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy); 4371*0b57cec5SDimitry Andric auto Ret = Tmp.convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM); 4372*0b57cec5SDimitry Andric *this = DoubleAPFloat(semPPCDoubleDouble, Tmp.bitcastToAPInt()); 4373*0b57cec5SDimitry Andric return Ret; 4374*0b57cec5SDimitry Andric } 4375*0b57cec5SDimitry Andric 4376*0b57cec5SDimitry Andric unsigned int DoubleAPFloat::convertToHexString(char *DST, 4377*0b57cec5SDimitry Andric unsigned int HexDigits, 4378*0b57cec5SDimitry Andric bool UpperCase, 4379*0b57cec5SDimitry Andric roundingMode RM) const { 4380*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4381*0b57cec5SDimitry Andric return APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) 4382*0b57cec5SDimitry Andric .convertToHexString(DST, HexDigits, UpperCase, RM); 4383*0b57cec5SDimitry Andric } 4384*0b57cec5SDimitry Andric 4385*0b57cec5SDimitry Andric bool DoubleAPFloat::isDenormal() const { 4386*0b57cec5SDimitry Andric return getCategory() == fcNormal && 4387*0b57cec5SDimitry Andric (Floats[0].isDenormal() || Floats[1].isDenormal() || 4388*0b57cec5SDimitry Andric // (double)(Hi + Lo) == Hi defines a normal number. 4389*0b57cec5SDimitry Andric Floats[0].compare(Floats[0] + Floats[1]) != cmpEqual); 4390*0b57cec5SDimitry Andric } 4391*0b57cec5SDimitry Andric 4392*0b57cec5SDimitry Andric bool DoubleAPFloat::isSmallest() const { 4393*0b57cec5SDimitry Andric if (getCategory() != fcNormal) 4394*0b57cec5SDimitry Andric return false; 4395*0b57cec5SDimitry Andric DoubleAPFloat Tmp(*this); 4396*0b57cec5SDimitry Andric Tmp.makeSmallest(this->isNegative()); 4397*0b57cec5SDimitry Andric return Tmp.compare(*this) == cmpEqual; 4398*0b57cec5SDimitry Andric } 4399*0b57cec5SDimitry Andric 4400*0b57cec5SDimitry Andric bool DoubleAPFloat::isLargest() const { 4401*0b57cec5SDimitry Andric if (getCategory() != fcNormal) 4402*0b57cec5SDimitry Andric return false; 4403*0b57cec5SDimitry Andric DoubleAPFloat Tmp(*this); 4404*0b57cec5SDimitry Andric Tmp.makeLargest(this->isNegative()); 4405*0b57cec5SDimitry Andric return Tmp.compare(*this) == cmpEqual; 4406*0b57cec5SDimitry Andric } 4407*0b57cec5SDimitry Andric 4408*0b57cec5SDimitry Andric bool DoubleAPFloat::isInteger() const { 4409*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4410*0b57cec5SDimitry Andric return Floats[0].isInteger() && Floats[1].isInteger(); 4411*0b57cec5SDimitry Andric } 4412*0b57cec5SDimitry Andric 4413*0b57cec5SDimitry Andric void DoubleAPFloat::toString(SmallVectorImpl<char> &Str, 4414*0b57cec5SDimitry Andric unsigned FormatPrecision, 4415*0b57cec5SDimitry Andric unsigned FormatMaxPadding, 4416*0b57cec5SDimitry Andric bool TruncateZero) const { 4417*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4418*0b57cec5SDimitry Andric APFloat(semPPCDoubleDoubleLegacy, bitcastToAPInt()) 4419*0b57cec5SDimitry Andric .toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero); 4420*0b57cec5SDimitry Andric } 4421*0b57cec5SDimitry Andric 4422*0b57cec5SDimitry Andric bool DoubleAPFloat::getExactInverse(APFloat *inv) const { 4423*0b57cec5SDimitry Andric assert(Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4424*0b57cec5SDimitry Andric APFloat Tmp(semPPCDoubleDoubleLegacy, bitcastToAPInt()); 4425*0b57cec5SDimitry Andric if (!inv) 4426*0b57cec5SDimitry Andric return Tmp.getExactInverse(nullptr); 4427*0b57cec5SDimitry Andric APFloat Inv(semPPCDoubleDoubleLegacy); 4428*0b57cec5SDimitry Andric auto Ret = Tmp.getExactInverse(&Inv); 4429*0b57cec5SDimitry Andric *inv = APFloat(semPPCDoubleDouble, Inv.bitcastToAPInt()); 4430*0b57cec5SDimitry Andric return Ret; 4431*0b57cec5SDimitry Andric } 4432*0b57cec5SDimitry Andric 4433*0b57cec5SDimitry Andric DoubleAPFloat scalbn(DoubleAPFloat Arg, int Exp, APFloat::roundingMode RM) { 4434*0b57cec5SDimitry Andric assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4435*0b57cec5SDimitry Andric return DoubleAPFloat(semPPCDoubleDouble, scalbn(Arg.Floats[0], Exp, RM), 4436*0b57cec5SDimitry Andric scalbn(Arg.Floats[1], Exp, RM)); 4437*0b57cec5SDimitry Andric } 4438*0b57cec5SDimitry Andric 4439*0b57cec5SDimitry Andric DoubleAPFloat frexp(const DoubleAPFloat &Arg, int &Exp, 4440*0b57cec5SDimitry Andric APFloat::roundingMode RM) { 4441*0b57cec5SDimitry Andric assert(Arg.Semantics == &semPPCDoubleDouble && "Unexpected Semantics"); 4442*0b57cec5SDimitry Andric APFloat First = frexp(Arg.Floats[0], Exp, RM); 4443*0b57cec5SDimitry Andric APFloat Second = Arg.Floats[1]; 4444*0b57cec5SDimitry Andric if (Arg.getCategory() == APFloat::fcNormal) 4445*0b57cec5SDimitry Andric Second = scalbn(Second, -Exp, RM); 4446*0b57cec5SDimitry Andric return DoubleAPFloat(semPPCDoubleDouble, std::move(First), std::move(Second)); 4447*0b57cec5SDimitry Andric } 4448*0b57cec5SDimitry Andric 4449*0b57cec5SDimitry Andric } // End detail namespace 4450*0b57cec5SDimitry Andric 4451*0b57cec5SDimitry Andric APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) { 4452*0b57cec5SDimitry Andric if (usesLayout<IEEEFloat>(Semantics)) { 4453*0b57cec5SDimitry Andric new (&IEEE) IEEEFloat(std::move(F)); 4454*0b57cec5SDimitry Andric return; 4455*0b57cec5SDimitry Andric } 4456*0b57cec5SDimitry Andric if (usesLayout<DoubleAPFloat>(Semantics)) { 4457*0b57cec5SDimitry Andric const fltSemantics& S = F.getSemantics(); 4458*0b57cec5SDimitry Andric new (&Double) 4459*0b57cec5SDimitry Andric DoubleAPFloat(Semantics, APFloat(std::move(F), S), 4460*0b57cec5SDimitry Andric APFloat(semIEEEdouble)); 4461*0b57cec5SDimitry Andric return; 4462*0b57cec5SDimitry Andric } 4463*0b57cec5SDimitry Andric llvm_unreachable("Unexpected semantics"); 4464*0b57cec5SDimitry Andric } 4465*0b57cec5SDimitry Andric 4466*0b57cec5SDimitry Andric APFloat::opStatus APFloat::convertFromString(StringRef Str, roundingMode RM) { 4467*0b57cec5SDimitry Andric APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM)); 4468*0b57cec5SDimitry Andric } 4469*0b57cec5SDimitry Andric 4470*0b57cec5SDimitry Andric hash_code hash_value(const APFloat &Arg) { 4471*0b57cec5SDimitry Andric if (APFloat::usesLayout<detail::IEEEFloat>(Arg.getSemantics())) 4472*0b57cec5SDimitry Andric return hash_value(Arg.U.IEEE); 4473*0b57cec5SDimitry Andric if (APFloat::usesLayout<detail::DoubleAPFloat>(Arg.getSemantics())) 4474*0b57cec5SDimitry Andric return hash_value(Arg.U.Double); 4475*0b57cec5SDimitry Andric llvm_unreachable("Unexpected semantics"); 4476*0b57cec5SDimitry Andric } 4477*0b57cec5SDimitry Andric 4478*0b57cec5SDimitry Andric APFloat::APFloat(const fltSemantics &Semantics, StringRef S) 4479*0b57cec5SDimitry Andric : APFloat(Semantics) { 4480*0b57cec5SDimitry Andric convertFromString(S, rmNearestTiesToEven); 4481*0b57cec5SDimitry Andric } 4482*0b57cec5SDimitry Andric 4483*0b57cec5SDimitry Andric APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics, 4484*0b57cec5SDimitry Andric roundingMode RM, bool *losesInfo) { 4485*0b57cec5SDimitry Andric if (&getSemantics() == &ToSemantics) { 4486*0b57cec5SDimitry Andric *losesInfo = false; 4487*0b57cec5SDimitry Andric return opOK; 4488*0b57cec5SDimitry Andric } 4489*0b57cec5SDimitry Andric if (usesLayout<IEEEFloat>(getSemantics()) && 4490*0b57cec5SDimitry Andric usesLayout<IEEEFloat>(ToSemantics)) 4491*0b57cec5SDimitry Andric return U.IEEE.convert(ToSemantics, RM, losesInfo); 4492*0b57cec5SDimitry Andric if (usesLayout<IEEEFloat>(getSemantics()) && 4493*0b57cec5SDimitry Andric usesLayout<DoubleAPFloat>(ToSemantics)) { 4494*0b57cec5SDimitry Andric assert(&ToSemantics == &semPPCDoubleDouble); 4495*0b57cec5SDimitry Andric auto Ret = U.IEEE.convert(semPPCDoubleDoubleLegacy, RM, losesInfo); 4496*0b57cec5SDimitry Andric *this = APFloat(ToSemantics, U.IEEE.bitcastToAPInt()); 4497*0b57cec5SDimitry Andric return Ret; 4498*0b57cec5SDimitry Andric } 4499*0b57cec5SDimitry Andric if (usesLayout<DoubleAPFloat>(getSemantics()) && 4500*0b57cec5SDimitry Andric usesLayout<IEEEFloat>(ToSemantics)) { 4501*0b57cec5SDimitry Andric auto Ret = getIEEE().convert(ToSemantics, RM, losesInfo); 4502*0b57cec5SDimitry Andric *this = APFloat(std::move(getIEEE()), ToSemantics); 4503*0b57cec5SDimitry Andric return Ret; 4504*0b57cec5SDimitry Andric } 4505*0b57cec5SDimitry Andric llvm_unreachable("Unexpected semantics"); 4506*0b57cec5SDimitry Andric } 4507*0b57cec5SDimitry Andric 4508*0b57cec5SDimitry Andric APFloat APFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE) { 4509*0b57cec5SDimitry Andric if (isIEEE) { 4510*0b57cec5SDimitry Andric switch (BitWidth) { 4511*0b57cec5SDimitry Andric case 16: 4512*0b57cec5SDimitry Andric return APFloat(semIEEEhalf, APInt::getAllOnesValue(BitWidth)); 4513*0b57cec5SDimitry Andric case 32: 4514*0b57cec5SDimitry Andric return APFloat(semIEEEsingle, APInt::getAllOnesValue(BitWidth)); 4515*0b57cec5SDimitry Andric case 64: 4516*0b57cec5SDimitry Andric return APFloat(semIEEEdouble, APInt::getAllOnesValue(BitWidth)); 4517*0b57cec5SDimitry Andric case 80: 4518*0b57cec5SDimitry Andric return APFloat(semX87DoubleExtended, APInt::getAllOnesValue(BitWidth)); 4519*0b57cec5SDimitry Andric case 128: 4520*0b57cec5SDimitry Andric return APFloat(semIEEEquad, APInt::getAllOnesValue(BitWidth)); 4521*0b57cec5SDimitry Andric default: 4522*0b57cec5SDimitry Andric llvm_unreachable("Unknown floating bit width"); 4523*0b57cec5SDimitry Andric } 4524*0b57cec5SDimitry Andric } else { 4525*0b57cec5SDimitry Andric assert(BitWidth == 128); 4526*0b57cec5SDimitry Andric return APFloat(semPPCDoubleDouble, APInt::getAllOnesValue(BitWidth)); 4527*0b57cec5SDimitry Andric } 4528*0b57cec5SDimitry Andric } 4529*0b57cec5SDimitry Andric 4530*0b57cec5SDimitry Andric void APFloat::print(raw_ostream &OS) const { 4531*0b57cec5SDimitry Andric SmallVector<char, 16> Buffer; 4532*0b57cec5SDimitry Andric toString(Buffer); 4533*0b57cec5SDimitry Andric OS << Buffer << "\n"; 4534*0b57cec5SDimitry Andric } 4535*0b57cec5SDimitry Andric 4536*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 4537*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void APFloat::dump() const { print(dbgs()); } 4538*0b57cec5SDimitry Andric #endif 4539*0b57cec5SDimitry Andric 4540*0b57cec5SDimitry Andric void APFloat::Profile(FoldingSetNodeID &NID) const { 4541*0b57cec5SDimitry Andric NID.Add(bitcastToAPInt()); 4542*0b57cec5SDimitry Andric } 4543*0b57cec5SDimitry Andric 4544*0b57cec5SDimitry Andric /* Same as convertToInteger(integerPart*, ...), except the result is returned in 4545*0b57cec5SDimitry Andric an APSInt, whose initial bit-width and signed-ness are used to determine the 4546*0b57cec5SDimitry Andric precision of the conversion. 4547*0b57cec5SDimitry Andric */ 4548*0b57cec5SDimitry Andric APFloat::opStatus APFloat::convertToInteger(APSInt &result, 4549*0b57cec5SDimitry Andric roundingMode rounding_mode, 4550*0b57cec5SDimitry Andric bool *isExact) const { 4551*0b57cec5SDimitry Andric unsigned bitWidth = result.getBitWidth(); 4552*0b57cec5SDimitry Andric SmallVector<uint64_t, 4> parts(result.getNumWords()); 4553*0b57cec5SDimitry Andric opStatus status = convertToInteger(parts, bitWidth, result.isSigned(), 4554*0b57cec5SDimitry Andric rounding_mode, isExact); 4555*0b57cec5SDimitry Andric // Keeps the original signed-ness. 4556*0b57cec5SDimitry Andric result = APInt(bitWidth, parts); 4557*0b57cec5SDimitry Andric return status; 4558*0b57cec5SDimitry Andric } 4559*0b57cec5SDimitry Andric 4560*0b57cec5SDimitry Andric } // End llvm namespace 4561*0b57cec5SDimitry Andric 4562*0b57cec5SDimitry Andric #undef APFLOAT_DISPATCH_ON_SEMANTICS 4563