1193323Sed//===-- APFloat.cpp - Implement APFloat class -----------------------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file implements a class to represent arbitrary precision floating 11193323Sed// point values and provide a variety of arithmetic operations on them. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15193323Sed#include "llvm/ADT/APFloat.h" 16224145Sdim#include "llvm/ADT/APSInt.h" 17234353Sdim#include "llvm/ADT/FoldingSet.h" 18234353Sdim#include "llvm/ADT/Hashing.h" 19249423Sdim#include "llvm/ADT/StringExtras.h" 20198090Srdivacky#include "llvm/ADT/StringRef.h" 21198090Srdivacky#include "llvm/Support/ErrorHandling.h" 22193323Sed#include "llvm/Support/MathExtras.h" 23249423Sdim#include <cstring> 24204642Srdivacky#include <limits.h> 25193323Sed 26193323Sedusing namespace llvm; 27193323Sed 28193323Sed#define convolve(lhs, rhs) ((lhs) * 4 + (rhs)) 29193323Sed 30193323Sed/* Assumed in hexadecimal significand parsing, and conversion to 31193323Sed hexadecimal strings. */ 32193323Sed#define COMPILE_TIME_ASSERT(cond) extern int CTAssert[(cond) ? 1 : -1] 33193323SedCOMPILE_TIME_ASSERT(integerPartWidth % 4 == 0); 34193323Sed 35193323Sednamespace llvm { 36193323Sed 37193323Sed /* Represents floating point arithmetic semantics. */ 38193323Sed struct fltSemantics { 39193323Sed /* The largest E such that 2^E is representable; this matches the 40193323Sed definition of IEEE 754. */ 41193323Sed exponent_t maxExponent; 42193323Sed 43193323Sed /* The smallest E such that 2^E is a normalized number; this 44193323Sed matches the definition of IEEE 754. */ 45193323Sed exponent_t minExponent; 46193323Sed 47193323Sed /* Number of bits in the significand. This includes the integer 48193323Sed bit. */ 49193323Sed unsigned int precision; 50193323Sed }; 51193323Sed 52243830Sdim const fltSemantics APFloat::IEEEhalf = { 15, -14, 11 }; 53243830Sdim const fltSemantics APFloat::IEEEsingle = { 127, -126, 24 }; 54243830Sdim const fltSemantics APFloat::IEEEdouble = { 1023, -1022, 53 }; 55243830Sdim const fltSemantics APFloat::IEEEquad = { 16383, -16382, 113 }; 56243830Sdim const fltSemantics APFloat::x87DoubleExtended = { 16383, -16382, 64 }; 57243830Sdim const fltSemantics APFloat::Bogus = { 0, 0, 0 }; 58193323Sed 59243830Sdim /* The PowerPC format consists of two doubles. It does not map cleanly 60243830Sdim onto the usual format above. It is approximated using twice the 61243830Sdim mantissa bits. Note that for exponents near the double minimum, 62243830Sdim we no longer can represent the full 106 mantissa bits, so those 63243830Sdim will be treated as denormal numbers. 64193323Sed 65243830Sdim FIXME: While this approximation is equivalent to what GCC uses for 66243830Sdim compile-time arithmetic on PPC double-double numbers, it is not able 67243830Sdim to represent all possible values held by a PPC double-double number, 68243830Sdim for example: (long double) 1.0 + (long double) 0x1p-106 69243830Sdim Should this be replaced by a full emulation of PPC double-double? */ 70243830Sdim const fltSemantics APFloat::PPCDoubleDouble = { 1023, -1022 + 53, 53 + 53 }; 71243830Sdim 72193323Sed /* A tight upper bound on number of parts required to hold the value 73193323Sed pow(5, power) is 74193323Sed 75193323Sed power * 815 / (351 * integerPartWidth) + 1 76206083Srdivacky 77193323Sed However, whilst the result may require only this many parts, 78193323Sed because we are multiplying two values to get it, the 79193323Sed multiplication may require an extra part with the excess part 80193323Sed being zero (consider the trivial case of 1 * 1, tcFullMultiply 81193323Sed requires two parts to hold the single-part result). So we add an 82193323Sed extra one to guarantee enough space whilst multiplying. */ 83193323Sed const unsigned int maxExponent = 16383; 84193323Sed const unsigned int maxPrecision = 113; 85193323Sed const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1; 86193323Sed const unsigned int maxPowerOfFiveParts = 2 + ((maxPowerOfFiveExponent * 815) 87193323Sed / (351 * integerPartWidth)); 88193323Sed} 89193323Sed 90193323Sed/* A bunch of private, handy routines. */ 91193323Sed 92193323Sedstatic inline unsigned int 93193323SedpartCountForBits(unsigned int bits) 94193323Sed{ 95193323Sed return ((bits) + integerPartWidth - 1) / integerPartWidth; 96193323Sed} 97193323Sed 98193323Sed/* Returns 0U-9U. Return values >= 10U are not digits. */ 99193323Sedstatic inline unsigned int 100193323SeddecDigitValue(unsigned int c) 101193323Sed{ 102193323Sed return c - '0'; 103193323Sed} 104193323Sed 105193323Sed/* Return the value of a decimal exponent of the form 106193323Sed [+-]ddddddd. 107193323Sed 108193323Sed If the exponent overflows, returns a large exponent with the 109193323Sed appropriate sign. */ 110193323Sedstatic int 111198090SrdivackyreadExponent(StringRef::iterator begin, StringRef::iterator end) 112193323Sed{ 113193323Sed bool isNegative; 114193323Sed unsigned int absExponent; 115193323Sed const unsigned int overlargeExponent = 24000; /* FIXME. */ 116198090Srdivacky StringRef::iterator p = begin; 117193323Sed 118198090Srdivacky assert(p != end && "Exponent has no digits"); 119198090Srdivacky 120193323Sed isNegative = (*p == '-'); 121198090Srdivacky if (*p == '-' || *p == '+') { 122193323Sed p++; 123198090Srdivacky assert(p != end && "Exponent has no digits"); 124198090Srdivacky } 125193323Sed 126193323Sed absExponent = decDigitValue(*p++); 127198090Srdivacky assert(absExponent < 10U && "Invalid character in exponent"); 128193323Sed 129198090Srdivacky for (; p != end; ++p) { 130193323Sed unsigned int value; 131193323Sed 132193323Sed value = decDigitValue(*p); 133198090Srdivacky assert(value < 10U && "Invalid character in exponent"); 134193323Sed 135193323Sed value += absExponent * 10; 136193323Sed if (absExponent >= overlargeExponent) { 137193323Sed absExponent = overlargeExponent; 138212904Sdim p = end; /* outwit assert below */ 139193323Sed break; 140193323Sed } 141193323Sed absExponent = value; 142193323Sed } 143193323Sed 144198090Srdivacky assert(p == end && "Invalid exponent in exponent"); 145198090Srdivacky 146193323Sed if (isNegative) 147193323Sed return -(int) absExponent; 148193323Sed else 149193323Sed return (int) absExponent; 150193323Sed} 151193323Sed 152193323Sed/* This is ugly and needs cleaning up, but I don't immediately see 153193323Sed how whilst remaining safe. */ 154193323Sedstatic int 155198090SrdivackytotalExponent(StringRef::iterator p, StringRef::iterator end, 156198090Srdivacky int exponentAdjustment) 157193323Sed{ 158193323Sed int unsignedExponent; 159193323Sed bool negative, overflow; 160218893Sdim int exponent = 0; 161193323Sed 162198090Srdivacky assert(p != end && "Exponent has no digits"); 163198090Srdivacky 164193323Sed negative = *p == '-'; 165206083Srdivacky if (*p == '-' || *p == '+') { 166193323Sed p++; 167198090Srdivacky assert(p != end && "Exponent has no digits"); 168198090Srdivacky } 169193323Sed 170193323Sed unsignedExponent = 0; 171193323Sed overflow = false; 172206083Srdivacky for (; p != end; ++p) { 173193323Sed unsigned int value; 174193323Sed 175193323Sed value = decDigitValue(*p); 176198090Srdivacky assert(value < 10U && "Invalid character in exponent"); 177193323Sed 178193323Sed unsignedExponent = unsignedExponent * 10 + value; 179243830Sdim if (unsignedExponent > 32767) { 180193323Sed overflow = true; 181243830Sdim break; 182243830Sdim } 183193323Sed } 184193323Sed 185218893Sdim if (exponentAdjustment > 32767 || exponentAdjustment < -32768) 186193323Sed overflow = true; 187193323Sed 188206083Srdivacky if (!overflow) { 189193323Sed exponent = unsignedExponent; 190206083Srdivacky if (negative) 191193323Sed exponent = -exponent; 192193323Sed exponent += exponentAdjustment; 193218893Sdim if (exponent > 32767 || exponent < -32768) 194193323Sed overflow = true; 195193323Sed } 196193323Sed 197206083Srdivacky if (overflow) 198218893Sdim exponent = negative ? -32768: 32767; 199193323Sed 200193323Sed return exponent; 201193323Sed} 202193323Sed 203198090Srdivackystatic StringRef::iterator 204198090SrdivackyskipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, 205198090Srdivacky StringRef::iterator *dot) 206193323Sed{ 207198090Srdivacky StringRef::iterator p = begin; 208198090Srdivacky *dot = end; 209206083Srdivacky while (*p == '0' && p != end) 210193323Sed p++; 211193323Sed 212206083Srdivacky if (*p == '.') { 213193323Sed *dot = p++; 214198090Srdivacky 215198090Srdivacky assert(end - begin != 1 && "Significand has no digits"); 216198090Srdivacky 217206083Srdivacky while (*p == '0' && p != end) 218193323Sed p++; 219193323Sed } 220193323Sed 221193323Sed return p; 222193323Sed} 223193323Sed 224193323Sed/* Given a normal decimal floating point number of the form 225193323Sed 226193323Sed dddd.dddd[eE][+-]ddd 227193323Sed 228193323Sed where the decimal point and exponent are optional, fill out the 229193323Sed structure D. Exponent is appropriate if the significand is 230193323Sed treated as an integer, and normalizedExponent if the significand 231193323Sed is taken to have the decimal point after a single leading 232193323Sed non-zero digit. 233193323Sed 234193323Sed If the value is zero, V->firstSigDigit points to a non-digit, and 235193323Sed the return exponent is zero. 236193323Sed*/ 237193323Sedstruct decimalInfo { 238193323Sed const char *firstSigDigit; 239193323Sed const char *lastSigDigit; 240193323Sed int exponent; 241193323Sed int normalizedExponent; 242193323Sed}; 243193323Sed 244193323Sedstatic void 245198090SrdivackyinterpretDecimal(StringRef::iterator begin, StringRef::iterator end, 246198090Srdivacky decimalInfo *D) 247193323Sed{ 248198090Srdivacky StringRef::iterator dot = end; 249198090Srdivacky StringRef::iterator p = skipLeadingZeroesAndAnyDot (begin, end, &dot); 250193323Sed 251193323Sed D->firstSigDigit = p; 252193323Sed D->exponent = 0; 253193323Sed D->normalizedExponent = 0; 254193323Sed 255198090Srdivacky for (; p != end; ++p) { 256193323Sed if (*p == '.') { 257198090Srdivacky assert(dot == end && "String contains multiple dots"); 258193323Sed dot = p++; 259198090Srdivacky if (p == end) 260198090Srdivacky break; 261193323Sed } 262193323Sed if (decDigitValue(*p) >= 10U) 263193323Sed break; 264193323Sed } 265193323Sed 266198090Srdivacky if (p != end) { 267198090Srdivacky assert((*p == 'e' || *p == 'E') && "Invalid character in significand"); 268198090Srdivacky assert(p != begin && "Significand has no digits"); 269198090Srdivacky assert((dot == end || p - begin != 1) && "Significand has no digits"); 270193323Sed 271198090Srdivacky /* p points to the first non-digit in the string */ 272198090Srdivacky D->exponent = readExponent(p + 1, end); 273198090Srdivacky 274193323Sed /* Implied decimal point? */ 275198090Srdivacky if (dot == end) 276193323Sed dot = p; 277198090Srdivacky } 278193323Sed 279198090Srdivacky /* If number is all zeroes accept any exponent. */ 280198090Srdivacky if (p != D->firstSigDigit) { 281193323Sed /* Drop insignificant trailing zeroes. */ 282198090Srdivacky if (p != begin) { 283193323Sed do 284198090Srdivacky do 285198090Srdivacky p--; 286198090Srdivacky while (p != begin && *p == '0'); 287198090Srdivacky while (p != begin && *p == '.'); 288198090Srdivacky } 289193323Sed 290193323Sed /* Adjust the exponents for any decimal point. */ 291193323Sed D->exponent += static_cast<exponent_t>((dot - p) - (dot > p)); 292193323Sed D->normalizedExponent = (D->exponent + 293193323Sed static_cast<exponent_t>((p - D->firstSigDigit) 294193323Sed - (dot > D->firstSigDigit && dot < p))); 295193323Sed } 296193323Sed 297193323Sed D->lastSigDigit = p; 298193323Sed} 299193323Sed 300193323Sed/* Return the trailing fraction of a hexadecimal number. 301193323Sed DIGITVALUE is the first hex digit of the fraction, P points to 302193323Sed the next digit. */ 303193323Sedstatic lostFraction 304198090SrdivackytrailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, 305198090Srdivacky unsigned int digitValue) 306193323Sed{ 307193323Sed unsigned int hexDigit; 308193323Sed 309193323Sed /* If the first trailing digit isn't 0 or 8 we can work out the 310193323Sed fraction immediately. */ 311206083Srdivacky if (digitValue > 8) 312193323Sed return lfMoreThanHalf; 313206083Srdivacky else if (digitValue < 8 && digitValue > 0) 314193323Sed return lfLessThanHalf; 315193323Sed 316193323Sed /* Otherwise we need to find the first non-zero digit. */ 317206083Srdivacky while (*p == '0') 318193323Sed p++; 319193323Sed 320198090Srdivacky assert(p != end && "Invalid trailing hexadecimal fraction!"); 321198090Srdivacky 322193323Sed hexDigit = hexDigitValue(*p); 323193323Sed 324193323Sed /* If we ran off the end it is exactly zero or one-half, otherwise 325193323Sed a little more. */ 326206083Srdivacky if (hexDigit == -1U) 327193323Sed return digitValue == 0 ? lfExactlyZero: lfExactlyHalf; 328193323Sed else 329193323Sed return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf; 330193323Sed} 331193323Sed 332193323Sed/* Return the fraction lost were a bignum truncated losing the least 333193323Sed significant BITS bits. */ 334193323Sedstatic lostFraction 335193323SedlostFractionThroughTruncation(const integerPart *parts, 336193323Sed unsigned int partCount, 337193323Sed unsigned int bits) 338193323Sed{ 339193323Sed unsigned int lsb; 340193323Sed 341193323Sed lsb = APInt::tcLSB(parts, partCount); 342193323Sed 343193323Sed /* Note this is guaranteed true if bits == 0, or LSB == -1U. */ 344206083Srdivacky if (bits <= lsb) 345193323Sed return lfExactlyZero; 346206083Srdivacky if (bits == lsb + 1) 347193323Sed return lfExactlyHalf; 348206083Srdivacky if (bits <= partCount * integerPartWidth && 349206083Srdivacky APInt::tcExtractBit(parts, bits - 1)) 350193323Sed return lfMoreThanHalf; 351193323Sed 352193323Sed return lfLessThanHalf; 353193323Sed} 354193323Sed 355193323Sed/* Shift DST right BITS bits noting lost fraction. */ 356193323Sedstatic lostFraction 357193323SedshiftRight(integerPart *dst, unsigned int parts, unsigned int bits) 358193323Sed{ 359193323Sed lostFraction lost_fraction; 360193323Sed 361193323Sed lost_fraction = lostFractionThroughTruncation(dst, parts, bits); 362193323Sed 363193323Sed APInt::tcShiftRight(dst, parts, bits); 364193323Sed 365193323Sed return lost_fraction; 366193323Sed} 367193323Sed 368193323Sed/* Combine the effect of two lost fractions. */ 369193323Sedstatic lostFraction 370193323SedcombineLostFractions(lostFraction moreSignificant, 371193323Sed lostFraction lessSignificant) 372193323Sed{ 373206083Srdivacky if (lessSignificant != lfExactlyZero) { 374206083Srdivacky if (moreSignificant == lfExactlyZero) 375193323Sed moreSignificant = lfLessThanHalf; 376206083Srdivacky else if (moreSignificant == lfExactlyHalf) 377193323Sed moreSignificant = lfMoreThanHalf; 378193323Sed } 379193323Sed 380193323Sed return moreSignificant; 381193323Sed} 382193323Sed 383193323Sed/* The error from the true value, in half-ulps, on multiplying two 384193323Sed floating point numbers, which differ from the value they 385193323Sed approximate by at most HUE1 and HUE2 half-ulps, is strictly less 386193323Sed than the returned value. 387193323Sed 388193323Sed See "How to Read Floating Point Numbers Accurately" by William D 389193323Sed Clinger. */ 390193323Sedstatic unsigned int 391193323SedHUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2) 392193323Sed{ 393193323Sed assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8)); 394193323Sed 395193323Sed if (HUerr1 + HUerr2 == 0) 396193323Sed return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */ 397193323Sed else 398193323Sed return inexactMultiply + 2 * (HUerr1 + HUerr2); 399193323Sed} 400193323Sed 401193323Sed/* The number of ulps from the boundary (zero, or half if ISNEAREST) 402193323Sed when the least significant BITS are truncated. BITS cannot be 403193323Sed zero. */ 404193323Sedstatic integerPart 405193323SedulpsFromBoundary(const integerPart *parts, unsigned int bits, bool isNearest) 406193323Sed{ 407193323Sed unsigned int count, partBits; 408193323Sed integerPart part, boundary; 409193323Sed 410198892Srdivacky assert(bits != 0); 411193323Sed 412193323Sed bits--; 413193323Sed count = bits / integerPartWidth; 414193323Sed partBits = bits % integerPartWidth + 1; 415193323Sed 416193323Sed part = parts[count] & (~(integerPart) 0 >> (integerPartWidth - partBits)); 417193323Sed 418193323Sed if (isNearest) 419193323Sed boundary = (integerPart) 1 << (partBits - 1); 420193323Sed else 421193323Sed boundary = 0; 422193323Sed 423193323Sed if (count == 0) { 424193323Sed if (part - boundary <= boundary - part) 425193323Sed return part - boundary; 426193323Sed else 427193323Sed return boundary - part; 428193323Sed } 429193323Sed 430193323Sed if (part == boundary) { 431193323Sed while (--count) 432193323Sed if (parts[count]) 433193323Sed return ~(integerPart) 0; /* A lot. */ 434193323Sed 435193323Sed return parts[0]; 436193323Sed } else if (part == boundary - 1) { 437193323Sed while (--count) 438193323Sed if (~parts[count]) 439193323Sed return ~(integerPart) 0; /* A lot. */ 440193323Sed 441193323Sed return -parts[0]; 442193323Sed } 443193323Sed 444193323Sed return ~(integerPart) 0; /* A lot. */ 445193323Sed} 446193323Sed 447193323Sed/* Place pow(5, power) in DST, and return the number of parts used. 448193323Sed DST must be at least one part larger than size of the answer. */ 449193323Sedstatic unsigned int 450193323SedpowerOf5(integerPart *dst, unsigned int power) 451193323Sed{ 452193323Sed static const integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125, 453193323Sed 15625, 78125 }; 454193323Sed integerPart pow5s[maxPowerOfFiveParts * 2 + 5]; 455193323Sed pow5s[0] = 78125 * 5; 456206083Srdivacky 457193323Sed unsigned int partsCount[16] = { 1 }; 458193323Sed integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5; 459193323Sed unsigned int result; 460193323Sed assert(power <= maxExponent); 461193323Sed 462193323Sed p1 = dst; 463193323Sed p2 = scratch; 464193323Sed 465193323Sed *p1 = firstEightPowers[power & 7]; 466193323Sed power >>= 3; 467193323Sed 468193323Sed result = 1; 469193323Sed pow5 = pow5s; 470193323Sed 471193323Sed for (unsigned int n = 0; power; power >>= 1, n++) { 472193323Sed unsigned int pc; 473193323Sed 474193323Sed pc = partsCount[n]; 475193323Sed 476193323Sed /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */ 477193323Sed if (pc == 0) { 478193323Sed pc = partsCount[n - 1]; 479193323Sed APInt::tcFullMultiply(pow5, pow5 - pc, pow5 - pc, pc, pc); 480193323Sed pc *= 2; 481193323Sed if (pow5[pc - 1] == 0) 482193323Sed pc--; 483193323Sed partsCount[n] = pc; 484193323Sed } 485193323Sed 486193323Sed if (power & 1) { 487193323Sed integerPart *tmp; 488193323Sed 489193323Sed APInt::tcFullMultiply(p2, p1, pow5, result, pc); 490193323Sed result += pc; 491193323Sed if (p2[result - 1] == 0) 492193323Sed result--; 493193323Sed 494193323Sed /* Now result is in p1 with partsCount parts and p2 is scratch 495193323Sed space. */ 496193323Sed tmp = p1, p1 = p2, p2 = tmp; 497193323Sed } 498193323Sed 499193323Sed pow5 += pc; 500193323Sed } 501193323Sed 502193323Sed if (p1 != dst) 503193323Sed APInt::tcAssign(dst, p1, result); 504193323Sed 505193323Sed return result; 506193323Sed} 507193323Sed 508193323Sed/* Zero at the end to avoid modular arithmetic when adding one; used 509193323Sed when rounding up during hexadecimal output. */ 510193323Sedstatic const char hexDigitsLower[] = "0123456789abcdef0"; 511193323Sedstatic const char hexDigitsUpper[] = "0123456789ABCDEF0"; 512193323Sedstatic const char infinityL[] = "infinity"; 513193323Sedstatic const char infinityU[] = "INFINITY"; 514193323Sedstatic const char NaNL[] = "nan"; 515193323Sedstatic const char NaNU[] = "NAN"; 516193323Sed 517193323Sed/* Write out an integerPart in hexadecimal, starting with the most 518193323Sed significant nibble. Write out exactly COUNT hexdigits, return 519193323Sed COUNT. */ 520193323Sedstatic unsigned int 521193323SedpartAsHex (char *dst, integerPart part, unsigned int count, 522193323Sed const char *hexDigitChars) 523193323Sed{ 524193323Sed unsigned int result = count; 525193323Sed 526198892Srdivacky assert(count != 0 && count <= integerPartWidth / 4); 527193323Sed 528193323Sed part >>= (integerPartWidth - 4 * count); 529193323Sed while (count--) { 530193323Sed dst[count] = hexDigitChars[part & 0xf]; 531193323Sed part >>= 4; 532193323Sed } 533193323Sed 534193323Sed return result; 535193323Sed} 536193323Sed 537193323Sed/* Write out an unsigned decimal integer. */ 538193323Sedstatic char * 539193323SedwriteUnsignedDecimal (char *dst, unsigned int n) 540193323Sed{ 541193323Sed char buff[40], *p; 542193323Sed 543193323Sed p = buff; 544193323Sed do 545193323Sed *p++ = '0' + n % 10; 546193323Sed while (n /= 10); 547193323Sed 548193323Sed do 549193323Sed *dst++ = *--p; 550193323Sed while (p != buff); 551193323Sed 552193323Sed return dst; 553193323Sed} 554193323Sed 555193323Sed/* Write out a signed decimal integer. */ 556193323Sedstatic char * 557193323SedwriteSignedDecimal (char *dst, int value) 558193323Sed{ 559193323Sed if (value < 0) { 560193323Sed *dst++ = '-'; 561193323Sed dst = writeUnsignedDecimal(dst, -(unsigned) value); 562193323Sed } else 563193323Sed dst = writeUnsignedDecimal(dst, value); 564193323Sed 565193323Sed return dst; 566193323Sed} 567193323Sed 568193323Sed/* Constructors. */ 569193323Sedvoid 570193323SedAPFloat::initialize(const fltSemantics *ourSemantics) 571193323Sed{ 572193323Sed unsigned int count; 573193323Sed 574193323Sed semantics = ourSemantics; 575193323Sed count = partCount(); 576206083Srdivacky if (count > 1) 577193323Sed significand.parts = new integerPart[count]; 578193323Sed} 579193323Sed 580193323Sedvoid 581193323SedAPFloat::freeSignificand() 582193323Sed{ 583206083Srdivacky if (partCount() > 1) 584193323Sed delete [] significand.parts; 585193323Sed} 586193323Sed 587193323Sedvoid 588193323SedAPFloat::assign(const APFloat &rhs) 589193323Sed{ 590193323Sed assert(semantics == rhs.semantics); 591193323Sed 592193323Sed sign = rhs.sign; 593193323Sed category = rhs.category; 594193323Sed exponent = rhs.exponent; 595206083Srdivacky if (category == fcNormal || category == fcNaN) 596193323Sed copySignificand(rhs); 597193323Sed} 598193323Sed 599193323Sedvoid 600193323SedAPFloat::copySignificand(const APFloat &rhs) 601193323Sed{ 602193323Sed assert(category == fcNormal || category == fcNaN); 603193323Sed assert(rhs.partCount() >= partCount()); 604193323Sed 605193323Sed APInt::tcAssign(significandParts(), rhs.significandParts(), 606193323Sed partCount()); 607193323Sed} 608193323Sed 609193323Sed/* Make this number a NaN, with an arbitrary but deterministic value 610193323Sed for the significand. If double or longer, this is a signalling NaN, 611193323Sed which may not be ideal. If float, this is QNaN(0). */ 612204642Srdivackyvoid APFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill) 613193323Sed{ 614193323Sed category = fcNaN; 615204642Srdivacky sign = Negative; 616204642Srdivacky 617204642Srdivacky integerPart *significand = significandParts(); 618204642Srdivacky unsigned numParts = partCount(); 619204642Srdivacky 620204642Srdivacky // Set the significand bits to the fill. 621204642Srdivacky if (!fill || fill->getNumWords() < numParts) 622204642Srdivacky APInt::tcSet(significand, 0, numParts); 623204642Srdivacky if (fill) { 624204642Srdivacky APInt::tcAssign(significand, fill->getRawData(), 625204642Srdivacky std::min(fill->getNumWords(), numParts)); 626204642Srdivacky 627204642Srdivacky // Zero out the excess bits of the significand. 628204642Srdivacky unsigned bitsToPreserve = semantics->precision - 1; 629204642Srdivacky unsigned part = bitsToPreserve / 64; 630204642Srdivacky bitsToPreserve %= 64; 631204642Srdivacky significand[part] &= ((1ULL << bitsToPreserve) - 1); 632204642Srdivacky for (part++; part != numParts; ++part) 633204642Srdivacky significand[part] = 0; 634204642Srdivacky } 635204642Srdivacky 636204642Srdivacky unsigned QNaNBit = semantics->precision - 2; 637204642Srdivacky 638204642Srdivacky if (SNaN) { 639204642Srdivacky // We always have to clear the QNaN bit to make it an SNaN. 640204642Srdivacky APInt::tcClearBit(significand, QNaNBit); 641204642Srdivacky 642204642Srdivacky // If there are no bits set in the payload, we have to set 643204642Srdivacky // *something* to make it a NaN instead of an infinity; 644204642Srdivacky // conventionally, this is the next bit down from the QNaN bit. 645204642Srdivacky if (APInt::tcIsZero(significand, numParts)) 646204642Srdivacky APInt::tcSetBit(significand, QNaNBit - 1); 647204642Srdivacky } else { 648204642Srdivacky // We always have to set the QNaN bit to make it a QNaN. 649204642Srdivacky APInt::tcSetBit(significand, QNaNBit); 650204642Srdivacky } 651204642Srdivacky 652204642Srdivacky // For x87 extended precision, we want to make a NaN, not a 653204642Srdivacky // pseudo-NaN. Maybe we should expose the ability to make 654204642Srdivacky // pseudo-NaNs? 655204642Srdivacky if (semantics == &APFloat::x87DoubleExtended) 656204642Srdivacky APInt::tcSetBit(significand, QNaNBit + 1); 657193323Sed} 658193323Sed 659204642SrdivackyAPFloat APFloat::makeNaN(const fltSemantics &Sem, bool SNaN, bool Negative, 660204642Srdivacky const APInt *fill) { 661204642Srdivacky APFloat value(Sem, uninitialized); 662204642Srdivacky value.makeNaN(SNaN, Negative, fill); 663204642Srdivacky return value; 664204642Srdivacky} 665204642Srdivacky 666193323SedAPFloat & 667193323SedAPFloat::operator=(const APFloat &rhs) 668193323Sed{ 669206083Srdivacky if (this != &rhs) { 670206083Srdivacky if (semantics != rhs.semantics) { 671193323Sed freeSignificand(); 672193323Sed initialize(rhs.semantics); 673193323Sed } 674193323Sed assign(rhs); 675193323Sed } 676193323Sed 677193323Sed return *this; 678193323Sed} 679193323Sed 680193323Sedbool 681249423SdimAPFloat::isDenormal() const { 682249423Sdim return isNormal() && (exponent == semantics->minExponent) && 683249423Sdim (APInt::tcExtractBit(significandParts(), 684249423Sdim semantics->precision - 1) == 0); 685249423Sdim} 686249423Sdim 687249423Sdimbool 688193323SedAPFloat::bitwiseIsEqual(const APFloat &rhs) const { 689193323Sed if (this == &rhs) 690193323Sed return true; 691193323Sed if (semantics != rhs.semantics || 692193323Sed category != rhs.category || 693193323Sed sign != rhs.sign) 694193323Sed return false; 695193323Sed if (category==fcZero || category==fcInfinity) 696193323Sed return true; 697193323Sed else if (category==fcNormal && exponent!=rhs.exponent) 698193323Sed return false; 699193323Sed else { 700193323Sed int i= partCount(); 701193323Sed const integerPart* p=significandParts(); 702193323Sed const integerPart* q=rhs.significandParts(); 703193323Sed for (; i>0; i--, p++, q++) { 704193323Sed if (*p != *q) 705193323Sed return false; 706193323Sed } 707193323Sed return true; 708193323Sed } 709193323Sed} 710193323Sed 711243830SdimAPFloat::APFloat(const fltSemantics &ourSemantics, integerPart value) { 712193323Sed initialize(&ourSemantics); 713193323Sed sign = 0; 714193323Sed zeroSignificand(); 715193323Sed exponent = ourSemantics.precision - 1; 716193323Sed significandParts()[0] = value; 717193323Sed normalize(rmNearestTiesToEven, lfExactlyZero); 718193323Sed} 719193323Sed 720243830SdimAPFloat::APFloat(const fltSemantics &ourSemantics) { 721198090Srdivacky initialize(&ourSemantics); 722198090Srdivacky category = fcZero; 723198090Srdivacky sign = false; 724198090Srdivacky} 725198090Srdivacky 726243830SdimAPFloat::APFloat(const fltSemantics &ourSemantics, uninitializedTag tag) { 727204642Srdivacky // Allocates storage if necessary but does not initialize it. 728204642Srdivacky initialize(&ourSemantics); 729204642Srdivacky} 730198090Srdivacky 731193323SedAPFloat::APFloat(const fltSemantics &ourSemantics, 732243830Sdim fltCategory ourCategory, bool negative) { 733193323Sed initialize(&ourSemantics); 734193323Sed category = ourCategory; 735193323Sed sign = negative; 736193323Sed if (category == fcNormal) 737193323Sed category = fcZero; 738193323Sed else if (ourCategory == fcNaN) 739204642Srdivacky makeNaN(); 740193323Sed} 741193323Sed 742243830SdimAPFloat::APFloat(const fltSemantics &ourSemantics, StringRef text) { 743193323Sed initialize(&ourSemantics); 744193323Sed convertFromString(text, rmNearestTiesToEven); 745193323Sed} 746193323Sed 747243830SdimAPFloat::APFloat(const APFloat &rhs) { 748193323Sed initialize(rhs.semantics); 749193323Sed assign(rhs); 750193323Sed} 751193323Sed 752193323SedAPFloat::~APFloat() 753193323Sed{ 754193323Sed freeSignificand(); 755193323Sed} 756193323Sed 757193323Sed// Profile - This method 'profiles' an APFloat for use with FoldingSet. 758193323Sedvoid APFloat::Profile(FoldingSetNodeID& ID) const { 759193323Sed ID.Add(bitcastToAPInt()); 760193323Sed} 761193323Sed 762193323Sedunsigned int 763193323SedAPFloat::partCount() const 764193323Sed{ 765193323Sed return partCountForBits(semantics->precision + 1); 766193323Sed} 767193323Sed 768193323Sedunsigned int 769193323SedAPFloat::semanticsPrecision(const fltSemantics &semantics) 770193323Sed{ 771193323Sed return semantics.precision; 772193323Sed} 773193323Sed 774193323Sedconst integerPart * 775193323SedAPFloat::significandParts() const 776193323Sed{ 777193323Sed return const_cast<APFloat *>(this)->significandParts(); 778193323Sed} 779193323Sed 780193323SedintegerPart * 781193323SedAPFloat::significandParts() 782193323Sed{ 783193323Sed assert(category == fcNormal || category == fcNaN); 784193323Sed 785198892Srdivacky if (partCount() > 1) 786193323Sed return significand.parts; 787193323Sed else 788193323Sed return &significand.part; 789193323Sed} 790193323Sed 791193323Sedvoid 792193323SedAPFloat::zeroSignificand() 793193323Sed{ 794193323Sed category = fcNormal; 795193323Sed APInt::tcSet(significandParts(), 0, partCount()); 796193323Sed} 797193323Sed 798193323Sed/* Increment an fcNormal floating point number's significand. */ 799193323Sedvoid 800193323SedAPFloat::incrementSignificand() 801193323Sed{ 802193323Sed integerPart carry; 803193323Sed 804193323Sed carry = APInt::tcIncrement(significandParts(), partCount()); 805193323Sed 806193323Sed /* Our callers should never cause us to overflow. */ 807193323Sed assert(carry == 0); 808226633Sdim (void)carry; 809193323Sed} 810193323Sed 811193323Sed/* Add the significand of the RHS. Returns the carry flag. */ 812193323SedintegerPart 813193323SedAPFloat::addSignificand(const APFloat &rhs) 814193323Sed{ 815193323Sed integerPart *parts; 816193323Sed 817193323Sed parts = significandParts(); 818193323Sed 819193323Sed assert(semantics == rhs.semantics); 820193323Sed assert(exponent == rhs.exponent); 821193323Sed 822193323Sed return APInt::tcAdd(parts, rhs.significandParts(), 0, partCount()); 823193323Sed} 824193323Sed 825193323Sed/* Subtract the significand of the RHS with a borrow flag. Returns 826193323Sed the borrow flag. */ 827193323SedintegerPart 828193323SedAPFloat::subtractSignificand(const APFloat &rhs, integerPart borrow) 829193323Sed{ 830193323Sed integerPart *parts; 831193323Sed 832193323Sed parts = significandParts(); 833193323Sed 834193323Sed assert(semantics == rhs.semantics); 835193323Sed assert(exponent == rhs.exponent); 836193323Sed 837193323Sed return APInt::tcSubtract(parts, rhs.significandParts(), borrow, 838193323Sed partCount()); 839193323Sed} 840193323Sed 841193323Sed/* Multiply the significand of the RHS. If ADDEND is non-NULL, add it 842193323Sed on to the full-precision result of the multiplication. Returns the 843193323Sed lost fraction. */ 844193323SedlostFraction 845193323SedAPFloat::multiplySignificand(const APFloat &rhs, const APFloat *addend) 846193323Sed{ 847193323Sed unsigned int omsb; // One, not zero, based MSB. 848193323Sed unsigned int partsCount, newPartsCount, precision; 849193323Sed integerPart *lhsSignificand; 850193323Sed integerPart scratch[4]; 851193323Sed integerPart *fullSignificand; 852193323Sed lostFraction lost_fraction; 853193323Sed bool ignored; 854193323Sed 855193323Sed assert(semantics == rhs.semantics); 856193323Sed 857193323Sed precision = semantics->precision; 858193323Sed newPartsCount = partCountForBits(precision * 2); 859193323Sed 860206083Srdivacky if (newPartsCount > 4) 861193323Sed fullSignificand = new integerPart[newPartsCount]; 862193323Sed else 863193323Sed fullSignificand = scratch; 864193323Sed 865193323Sed lhsSignificand = significandParts(); 866193323Sed partsCount = partCount(); 867193323Sed 868193323Sed APInt::tcFullMultiply(fullSignificand, lhsSignificand, 869193323Sed rhs.significandParts(), partsCount, partsCount); 870193323Sed 871193323Sed lost_fraction = lfExactlyZero; 872193323Sed omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; 873193323Sed exponent += rhs.exponent; 874193323Sed 875206083Srdivacky if (addend) { 876193323Sed Significand savedSignificand = significand; 877193323Sed const fltSemantics *savedSemantics = semantics; 878193323Sed fltSemantics extendedSemantics; 879193323Sed opStatus status; 880193323Sed unsigned int extendedPrecision; 881193323Sed 882193323Sed /* Normalize our MSB. */ 883193323Sed extendedPrecision = precision + precision - 1; 884206083Srdivacky if (omsb != extendedPrecision) { 885206083Srdivacky APInt::tcShiftLeft(fullSignificand, newPartsCount, 886206083Srdivacky extendedPrecision - omsb); 887206083Srdivacky exponent -= extendedPrecision - omsb; 888206083Srdivacky } 889193323Sed 890193323Sed /* Create new semantics. */ 891193323Sed extendedSemantics = *semantics; 892193323Sed extendedSemantics.precision = extendedPrecision; 893193323Sed 894206083Srdivacky if (newPartsCount == 1) 895193323Sed significand.part = fullSignificand[0]; 896193323Sed else 897193323Sed significand.parts = fullSignificand; 898193323Sed semantics = &extendedSemantics; 899193323Sed 900193323Sed APFloat extendedAddend(*addend); 901193323Sed status = extendedAddend.convert(extendedSemantics, rmTowardZero, &ignored); 902193323Sed assert(status == opOK); 903226633Sdim (void)status; 904193323Sed lost_fraction = addOrSubtractSignificand(extendedAddend, false); 905193323Sed 906193323Sed /* Restore our state. */ 907206083Srdivacky if (newPartsCount == 1) 908193323Sed fullSignificand[0] = significand.part; 909193323Sed significand = savedSignificand; 910193323Sed semantics = savedSemantics; 911193323Sed 912193323Sed omsb = APInt::tcMSB(fullSignificand, newPartsCount) + 1; 913193323Sed } 914193323Sed 915193323Sed exponent -= (precision - 1); 916193323Sed 917206083Srdivacky if (omsb > precision) { 918193323Sed unsigned int bits, significantParts; 919193323Sed lostFraction lf; 920193323Sed 921193323Sed bits = omsb - precision; 922193323Sed significantParts = partCountForBits(omsb); 923193323Sed lf = shiftRight(fullSignificand, significantParts, bits); 924193323Sed lost_fraction = combineLostFractions(lf, lost_fraction); 925193323Sed exponent += bits; 926193323Sed } 927193323Sed 928193323Sed APInt::tcAssign(lhsSignificand, fullSignificand, partsCount); 929193323Sed 930206083Srdivacky if (newPartsCount > 4) 931193323Sed delete [] fullSignificand; 932193323Sed 933193323Sed return lost_fraction; 934193323Sed} 935193323Sed 936193323Sed/* Multiply the significands of LHS and RHS to DST. */ 937193323SedlostFraction 938193323SedAPFloat::divideSignificand(const APFloat &rhs) 939193323Sed{ 940193323Sed unsigned int bit, i, partsCount; 941193323Sed const integerPart *rhsSignificand; 942193323Sed integerPart *lhsSignificand, *dividend, *divisor; 943193323Sed integerPart scratch[4]; 944193323Sed lostFraction lost_fraction; 945193323Sed 946193323Sed assert(semantics == rhs.semantics); 947193323Sed 948193323Sed lhsSignificand = significandParts(); 949193323Sed rhsSignificand = rhs.significandParts(); 950193323Sed partsCount = partCount(); 951193323Sed 952206083Srdivacky if (partsCount > 2) 953193323Sed dividend = new integerPart[partsCount * 2]; 954193323Sed else 955193323Sed dividend = scratch; 956193323Sed 957193323Sed divisor = dividend + partsCount; 958193323Sed 959193323Sed /* Copy the dividend and divisor as they will be modified in-place. */ 960206083Srdivacky for (i = 0; i < partsCount; i++) { 961193323Sed dividend[i] = lhsSignificand[i]; 962193323Sed divisor[i] = rhsSignificand[i]; 963193323Sed lhsSignificand[i] = 0; 964193323Sed } 965193323Sed 966193323Sed exponent -= rhs.exponent; 967193323Sed 968193323Sed unsigned int precision = semantics->precision; 969193323Sed 970193323Sed /* Normalize the divisor. */ 971193323Sed bit = precision - APInt::tcMSB(divisor, partsCount) - 1; 972206083Srdivacky if (bit) { 973193323Sed exponent += bit; 974193323Sed APInt::tcShiftLeft(divisor, partsCount, bit); 975193323Sed } 976193323Sed 977193323Sed /* Normalize the dividend. */ 978193323Sed bit = precision - APInt::tcMSB(dividend, partsCount) - 1; 979206083Srdivacky if (bit) { 980193323Sed exponent -= bit; 981193323Sed APInt::tcShiftLeft(dividend, partsCount, bit); 982193323Sed } 983193323Sed 984193323Sed /* Ensure the dividend >= divisor initially for the loop below. 985193323Sed Incidentally, this means that the division loop below is 986193323Sed guaranteed to set the integer bit to one. */ 987206083Srdivacky if (APInt::tcCompare(dividend, divisor, partsCount) < 0) { 988193323Sed exponent--; 989193323Sed APInt::tcShiftLeft(dividend, partsCount, 1); 990193323Sed assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0); 991193323Sed } 992193323Sed 993193323Sed /* Long division. */ 994206083Srdivacky for (bit = precision; bit; bit -= 1) { 995206083Srdivacky if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) { 996193323Sed APInt::tcSubtract(dividend, divisor, 0, partsCount); 997193323Sed APInt::tcSetBit(lhsSignificand, bit - 1); 998193323Sed } 999193323Sed 1000193323Sed APInt::tcShiftLeft(dividend, partsCount, 1); 1001193323Sed } 1002193323Sed 1003193323Sed /* Figure out the lost fraction. */ 1004193323Sed int cmp = APInt::tcCompare(dividend, divisor, partsCount); 1005193323Sed 1006206083Srdivacky if (cmp > 0) 1007193323Sed lost_fraction = lfMoreThanHalf; 1008206083Srdivacky else if (cmp == 0) 1009193323Sed lost_fraction = lfExactlyHalf; 1010206083Srdivacky else if (APInt::tcIsZero(dividend, partsCount)) 1011193323Sed lost_fraction = lfExactlyZero; 1012193323Sed else 1013193323Sed lost_fraction = lfLessThanHalf; 1014193323Sed 1015206083Srdivacky if (partsCount > 2) 1016193323Sed delete [] dividend; 1017193323Sed 1018193323Sed return lost_fraction; 1019193323Sed} 1020193323Sed 1021193323Sedunsigned int 1022193323SedAPFloat::significandMSB() const 1023193323Sed{ 1024193323Sed return APInt::tcMSB(significandParts(), partCount()); 1025193323Sed} 1026193323Sed 1027193323Sedunsigned int 1028193323SedAPFloat::significandLSB() const 1029193323Sed{ 1030193323Sed return APInt::tcLSB(significandParts(), partCount()); 1031193323Sed} 1032193323Sed 1033193323Sed/* Note that a zero result is NOT normalized to fcZero. */ 1034193323SedlostFraction 1035193323SedAPFloat::shiftSignificandRight(unsigned int bits) 1036193323Sed{ 1037193323Sed /* Our exponent should not overflow. */ 1038193323Sed assert((exponent_t) (exponent + bits) >= exponent); 1039193323Sed 1040193323Sed exponent += bits; 1041193323Sed 1042193323Sed return shiftRight(significandParts(), partCount(), bits); 1043193323Sed} 1044193323Sed 1045193323Sed/* Shift the significand left BITS bits, subtract BITS from its exponent. */ 1046193323Sedvoid 1047193323SedAPFloat::shiftSignificandLeft(unsigned int bits) 1048193323Sed{ 1049193323Sed assert(bits < semantics->precision); 1050193323Sed 1051206083Srdivacky if (bits) { 1052193323Sed unsigned int partsCount = partCount(); 1053193323Sed 1054193323Sed APInt::tcShiftLeft(significandParts(), partsCount, bits); 1055193323Sed exponent -= bits; 1056193323Sed 1057193323Sed assert(!APInt::tcIsZero(significandParts(), partsCount)); 1058193323Sed } 1059193323Sed} 1060193323Sed 1061193323SedAPFloat::cmpResult 1062193323SedAPFloat::compareAbsoluteValue(const APFloat &rhs) const 1063193323Sed{ 1064193323Sed int compare; 1065193323Sed 1066193323Sed assert(semantics == rhs.semantics); 1067193323Sed assert(category == fcNormal); 1068193323Sed assert(rhs.category == fcNormal); 1069193323Sed 1070193323Sed compare = exponent - rhs.exponent; 1071193323Sed 1072193323Sed /* If exponents are equal, do an unsigned bignum comparison of the 1073193323Sed significands. */ 1074206083Srdivacky if (compare == 0) 1075193323Sed compare = APInt::tcCompare(significandParts(), rhs.significandParts(), 1076193323Sed partCount()); 1077193323Sed 1078206083Srdivacky if (compare > 0) 1079193323Sed return cmpGreaterThan; 1080206083Srdivacky else if (compare < 0) 1081193323Sed return cmpLessThan; 1082193323Sed else 1083193323Sed return cmpEqual; 1084193323Sed} 1085193323Sed 1086193323Sed/* Handle overflow. Sign is preserved. We either become infinity or 1087193323Sed the largest finite number. */ 1088193323SedAPFloat::opStatus 1089193323SedAPFloat::handleOverflow(roundingMode rounding_mode) 1090193323Sed{ 1091193323Sed /* Infinity? */ 1092206083Srdivacky if (rounding_mode == rmNearestTiesToEven || 1093206083Srdivacky rounding_mode == rmNearestTiesToAway || 1094206083Srdivacky (rounding_mode == rmTowardPositive && !sign) || 1095206083Srdivacky (rounding_mode == rmTowardNegative && sign)) { 1096206083Srdivacky category = fcInfinity; 1097206083Srdivacky return (opStatus) (opOverflow | opInexact); 1098206083Srdivacky } 1099193323Sed 1100193323Sed /* Otherwise we become the largest finite number. */ 1101193323Sed category = fcNormal; 1102193323Sed exponent = semantics->maxExponent; 1103193323Sed APInt::tcSetLeastSignificantBits(significandParts(), partCount(), 1104193323Sed semantics->precision); 1105193323Sed 1106193323Sed return opInexact; 1107193323Sed} 1108193323Sed 1109193323Sed/* Returns TRUE if, when truncating the current number, with BIT the 1110193323Sed new LSB, with the given lost fraction and rounding mode, the result 1111193323Sed would need to be rounded away from zero (i.e., by increasing the 1112193323Sed signficand). This routine must work for fcZero of both signs, and 1113193323Sed fcNormal numbers. */ 1114193323Sedbool 1115193323SedAPFloat::roundAwayFromZero(roundingMode rounding_mode, 1116193323Sed lostFraction lost_fraction, 1117193323Sed unsigned int bit) const 1118193323Sed{ 1119193323Sed /* NaNs and infinities should not have lost fractions. */ 1120193323Sed assert(category == fcNormal || category == fcZero); 1121193323Sed 1122193323Sed /* Current callers never pass this so we don't handle it. */ 1123193323Sed assert(lost_fraction != lfExactlyZero); 1124193323Sed 1125193323Sed switch (rounding_mode) { 1126193323Sed case rmNearestTiesToAway: 1127193323Sed return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf; 1128193323Sed 1129193323Sed case rmNearestTiesToEven: 1130206083Srdivacky if (lost_fraction == lfMoreThanHalf) 1131193323Sed return true; 1132193323Sed 1133193323Sed /* Our zeroes don't have a significand to test. */ 1134206083Srdivacky if (lost_fraction == lfExactlyHalf && category != fcZero) 1135193323Sed return APInt::tcExtractBit(significandParts(), bit); 1136193323Sed 1137193323Sed return false; 1138193323Sed 1139193323Sed case rmTowardZero: 1140193323Sed return false; 1141193323Sed 1142193323Sed case rmTowardPositive: 1143193323Sed return sign == false; 1144193323Sed 1145193323Sed case rmTowardNegative: 1146193323Sed return sign == true; 1147193323Sed } 1148234353Sdim llvm_unreachable("Invalid rounding mode found"); 1149193323Sed} 1150193323Sed 1151193323SedAPFloat::opStatus 1152193323SedAPFloat::normalize(roundingMode rounding_mode, 1153193323Sed lostFraction lost_fraction) 1154193323Sed{ 1155193323Sed unsigned int omsb; /* One, not zero, based MSB. */ 1156193323Sed int exponentChange; 1157193323Sed 1158206083Srdivacky if (category != fcNormal) 1159193323Sed return opOK; 1160193323Sed 1161193323Sed /* Before rounding normalize the exponent of fcNormal numbers. */ 1162193323Sed omsb = significandMSB() + 1; 1163193323Sed 1164206083Srdivacky if (omsb) { 1165193323Sed /* OMSB is numbered from 1. We want to place it in the integer 1166226633Sdim bit numbered PRECISION if possible, with a compensating change in 1167193323Sed the exponent. */ 1168193323Sed exponentChange = omsb - semantics->precision; 1169193323Sed 1170193323Sed /* If the resulting exponent is too high, overflow according to 1171193323Sed the rounding mode. */ 1172206083Srdivacky if (exponent + exponentChange > semantics->maxExponent) 1173193323Sed return handleOverflow(rounding_mode); 1174193323Sed 1175193323Sed /* Subnormal numbers have exponent minExponent, and their MSB 1176193323Sed is forced based on that. */ 1177206083Srdivacky if (exponent + exponentChange < semantics->minExponent) 1178193323Sed exponentChange = semantics->minExponent - exponent; 1179193323Sed 1180193323Sed /* Shifting left is easy as we don't lose precision. */ 1181206083Srdivacky if (exponentChange < 0) { 1182193323Sed assert(lost_fraction == lfExactlyZero); 1183193323Sed 1184193323Sed shiftSignificandLeft(-exponentChange); 1185193323Sed 1186193323Sed return opOK; 1187193323Sed } 1188193323Sed 1189206083Srdivacky if (exponentChange > 0) { 1190193323Sed lostFraction lf; 1191193323Sed 1192193323Sed /* Shift right and capture any new lost fraction. */ 1193193323Sed lf = shiftSignificandRight(exponentChange); 1194193323Sed 1195193323Sed lost_fraction = combineLostFractions(lf, lost_fraction); 1196193323Sed 1197193323Sed /* Keep OMSB up-to-date. */ 1198206083Srdivacky if (omsb > (unsigned) exponentChange) 1199193323Sed omsb -= exponentChange; 1200193323Sed else 1201193323Sed omsb = 0; 1202193323Sed } 1203193323Sed } 1204193323Sed 1205193323Sed /* Now round the number according to rounding_mode given the lost 1206193323Sed fraction. */ 1207193323Sed 1208193323Sed /* As specified in IEEE 754, since we do not trap we do not report 1209193323Sed underflow for exact results. */ 1210206083Srdivacky if (lost_fraction == lfExactlyZero) { 1211193323Sed /* Canonicalize zeroes. */ 1212206083Srdivacky if (omsb == 0) 1213193323Sed category = fcZero; 1214193323Sed 1215193323Sed return opOK; 1216193323Sed } 1217193323Sed 1218193323Sed /* Increment the significand if we're rounding away from zero. */ 1219206083Srdivacky if (roundAwayFromZero(rounding_mode, lost_fraction, 0)) { 1220206083Srdivacky if (omsb == 0) 1221193323Sed exponent = semantics->minExponent; 1222193323Sed 1223193323Sed incrementSignificand(); 1224193323Sed omsb = significandMSB() + 1; 1225193323Sed 1226193323Sed /* Did the significand increment overflow? */ 1227206083Srdivacky if (omsb == (unsigned) semantics->precision + 1) { 1228193323Sed /* Renormalize by incrementing the exponent and shifting our 1229193323Sed significand right one. However if we already have the 1230193323Sed maximum exponent we overflow to infinity. */ 1231206083Srdivacky if (exponent == semantics->maxExponent) { 1232193323Sed category = fcInfinity; 1233193323Sed 1234193323Sed return (opStatus) (opOverflow | opInexact); 1235193323Sed } 1236193323Sed 1237193323Sed shiftSignificandRight(1); 1238193323Sed 1239193323Sed return opInexact; 1240193323Sed } 1241193323Sed } 1242193323Sed 1243193323Sed /* The normal case - we were and are not denormal, and any 1244193323Sed significand increment above didn't overflow. */ 1245206083Srdivacky if (omsb == semantics->precision) 1246193323Sed return opInexact; 1247193323Sed 1248193323Sed /* We have a non-zero denormal. */ 1249193323Sed assert(omsb < semantics->precision); 1250193323Sed 1251193323Sed /* Canonicalize zeroes. */ 1252206083Srdivacky if (omsb == 0) 1253193323Sed category = fcZero; 1254193323Sed 1255193323Sed /* The fcZero case is a denormal that underflowed to zero. */ 1256193323Sed return (opStatus) (opUnderflow | opInexact); 1257193323Sed} 1258193323Sed 1259193323SedAPFloat::opStatus 1260193323SedAPFloat::addOrSubtractSpecials(const APFloat &rhs, bool subtract) 1261193323Sed{ 1262193323Sed switch (convolve(category, rhs.category)) { 1263193323Sed default: 1264198090Srdivacky llvm_unreachable(0); 1265193323Sed 1266193323Sed case convolve(fcNaN, fcZero): 1267193323Sed case convolve(fcNaN, fcNormal): 1268193323Sed case convolve(fcNaN, fcInfinity): 1269193323Sed case convolve(fcNaN, fcNaN): 1270193323Sed case convolve(fcNormal, fcZero): 1271193323Sed case convolve(fcInfinity, fcNormal): 1272193323Sed case convolve(fcInfinity, fcZero): 1273193323Sed return opOK; 1274193323Sed 1275193323Sed case convolve(fcZero, fcNaN): 1276193323Sed case convolve(fcNormal, fcNaN): 1277193323Sed case convolve(fcInfinity, fcNaN): 1278193323Sed category = fcNaN; 1279193323Sed copySignificand(rhs); 1280193323Sed return opOK; 1281193323Sed 1282193323Sed case convolve(fcNormal, fcInfinity): 1283193323Sed case convolve(fcZero, fcInfinity): 1284193323Sed category = fcInfinity; 1285193323Sed sign = rhs.sign ^ subtract; 1286193323Sed return opOK; 1287193323Sed 1288193323Sed case convolve(fcZero, fcNormal): 1289193323Sed assign(rhs); 1290193323Sed sign = rhs.sign ^ subtract; 1291193323Sed return opOK; 1292193323Sed 1293193323Sed case convolve(fcZero, fcZero): 1294193323Sed /* Sign depends on rounding mode; handled by caller. */ 1295193323Sed return opOK; 1296193323Sed 1297193323Sed case convolve(fcInfinity, fcInfinity): 1298193323Sed /* Differently signed infinities can only be validly 1299193323Sed subtracted. */ 1300206083Srdivacky if (((sign ^ rhs.sign)!=0) != subtract) { 1301193323Sed makeNaN(); 1302193323Sed return opInvalidOp; 1303193323Sed } 1304193323Sed 1305193323Sed return opOK; 1306193323Sed 1307193323Sed case convolve(fcNormal, fcNormal): 1308193323Sed return opDivByZero; 1309193323Sed } 1310193323Sed} 1311193323Sed 1312193323Sed/* Add or subtract two normal numbers. */ 1313193323SedlostFraction 1314193323SedAPFloat::addOrSubtractSignificand(const APFloat &rhs, bool subtract) 1315193323Sed{ 1316193323Sed integerPart carry; 1317193323Sed lostFraction lost_fraction; 1318193323Sed int bits; 1319193323Sed 1320193323Sed /* Determine if the operation on the absolute values is effectively 1321193323Sed an addition or subtraction. */ 1322193323Sed subtract ^= (sign ^ rhs.sign) ? true : false; 1323193323Sed 1324193323Sed /* Are we bigger exponent-wise than the RHS? */ 1325193323Sed bits = exponent - rhs.exponent; 1326193323Sed 1327193323Sed /* Subtraction is more subtle than one might naively expect. */ 1328206083Srdivacky if (subtract) { 1329193323Sed APFloat temp_rhs(rhs); 1330193323Sed bool reverse; 1331193323Sed 1332193323Sed if (bits == 0) { 1333193323Sed reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan; 1334193323Sed lost_fraction = lfExactlyZero; 1335193323Sed } else if (bits > 0) { 1336193323Sed lost_fraction = temp_rhs.shiftSignificandRight(bits - 1); 1337193323Sed shiftSignificandLeft(1); 1338193323Sed reverse = false; 1339193323Sed } else { 1340193323Sed lost_fraction = shiftSignificandRight(-bits - 1); 1341193323Sed temp_rhs.shiftSignificandLeft(1); 1342193323Sed reverse = true; 1343193323Sed } 1344193323Sed 1345193323Sed if (reverse) { 1346193323Sed carry = temp_rhs.subtractSignificand 1347193323Sed (*this, lost_fraction != lfExactlyZero); 1348193323Sed copySignificand(temp_rhs); 1349193323Sed sign = !sign; 1350193323Sed } else { 1351193323Sed carry = subtractSignificand 1352193323Sed (temp_rhs, lost_fraction != lfExactlyZero); 1353193323Sed } 1354193323Sed 1355193323Sed /* Invert the lost fraction - it was on the RHS and 1356193323Sed subtracted. */ 1357206083Srdivacky if (lost_fraction == lfLessThanHalf) 1358193323Sed lost_fraction = lfMoreThanHalf; 1359206083Srdivacky else if (lost_fraction == lfMoreThanHalf) 1360193323Sed lost_fraction = lfLessThanHalf; 1361193323Sed 1362193323Sed /* The code above is intended to ensure that no borrow is 1363193323Sed necessary. */ 1364193323Sed assert(!carry); 1365226633Sdim (void)carry; 1366193323Sed } else { 1367206083Srdivacky if (bits > 0) { 1368193323Sed APFloat temp_rhs(rhs); 1369193323Sed 1370193323Sed lost_fraction = temp_rhs.shiftSignificandRight(bits); 1371193323Sed carry = addSignificand(temp_rhs); 1372193323Sed } else { 1373193323Sed lost_fraction = shiftSignificandRight(-bits); 1374193323Sed carry = addSignificand(rhs); 1375193323Sed } 1376193323Sed 1377193323Sed /* We have a guard bit; generating a carry cannot happen. */ 1378193323Sed assert(!carry); 1379226633Sdim (void)carry; 1380193323Sed } 1381193323Sed 1382193323Sed return lost_fraction; 1383193323Sed} 1384193323Sed 1385193323SedAPFloat::opStatus 1386193323SedAPFloat::multiplySpecials(const APFloat &rhs) 1387193323Sed{ 1388193323Sed switch (convolve(category, rhs.category)) { 1389193323Sed default: 1390198090Srdivacky llvm_unreachable(0); 1391193323Sed 1392193323Sed case convolve(fcNaN, fcZero): 1393193323Sed case convolve(fcNaN, fcNormal): 1394193323Sed case convolve(fcNaN, fcInfinity): 1395193323Sed case convolve(fcNaN, fcNaN): 1396193323Sed return opOK; 1397193323Sed 1398193323Sed case convolve(fcZero, fcNaN): 1399193323Sed case convolve(fcNormal, fcNaN): 1400193323Sed case convolve(fcInfinity, fcNaN): 1401193323Sed category = fcNaN; 1402193323Sed copySignificand(rhs); 1403193323Sed return opOK; 1404193323Sed 1405193323Sed case convolve(fcNormal, fcInfinity): 1406193323Sed case convolve(fcInfinity, fcNormal): 1407193323Sed case convolve(fcInfinity, fcInfinity): 1408193323Sed category = fcInfinity; 1409193323Sed return opOK; 1410193323Sed 1411193323Sed case convolve(fcZero, fcNormal): 1412193323Sed case convolve(fcNormal, fcZero): 1413193323Sed case convolve(fcZero, fcZero): 1414193323Sed category = fcZero; 1415193323Sed return opOK; 1416193323Sed 1417193323Sed case convolve(fcZero, fcInfinity): 1418193323Sed case convolve(fcInfinity, fcZero): 1419193323Sed makeNaN(); 1420193323Sed return opInvalidOp; 1421193323Sed 1422193323Sed case convolve(fcNormal, fcNormal): 1423193323Sed return opOK; 1424193323Sed } 1425193323Sed} 1426193323Sed 1427193323SedAPFloat::opStatus 1428193323SedAPFloat::divideSpecials(const APFloat &rhs) 1429193323Sed{ 1430193323Sed switch (convolve(category, rhs.category)) { 1431193323Sed default: 1432198090Srdivacky llvm_unreachable(0); 1433193323Sed 1434193323Sed case convolve(fcNaN, fcZero): 1435193323Sed case convolve(fcNaN, fcNormal): 1436193323Sed case convolve(fcNaN, fcInfinity): 1437193323Sed case convolve(fcNaN, fcNaN): 1438193323Sed case convolve(fcInfinity, fcZero): 1439193323Sed case convolve(fcInfinity, fcNormal): 1440193323Sed case convolve(fcZero, fcInfinity): 1441193323Sed case convolve(fcZero, fcNormal): 1442193323Sed return opOK; 1443193323Sed 1444193323Sed case convolve(fcZero, fcNaN): 1445193323Sed case convolve(fcNormal, fcNaN): 1446193323Sed case convolve(fcInfinity, fcNaN): 1447193323Sed category = fcNaN; 1448193323Sed copySignificand(rhs); 1449193323Sed return opOK; 1450193323Sed 1451193323Sed case convolve(fcNormal, fcInfinity): 1452193323Sed category = fcZero; 1453193323Sed return opOK; 1454193323Sed 1455193323Sed case convolve(fcNormal, fcZero): 1456193323Sed category = fcInfinity; 1457193323Sed return opDivByZero; 1458193323Sed 1459193323Sed case convolve(fcInfinity, fcInfinity): 1460193323Sed case convolve(fcZero, fcZero): 1461193323Sed makeNaN(); 1462193323Sed return opInvalidOp; 1463193323Sed 1464193323Sed case convolve(fcNormal, fcNormal): 1465193323Sed return opOK; 1466193323Sed } 1467193323Sed} 1468193323Sed 1469193323SedAPFloat::opStatus 1470193323SedAPFloat::modSpecials(const APFloat &rhs) 1471193323Sed{ 1472193323Sed switch (convolve(category, rhs.category)) { 1473193323Sed default: 1474198090Srdivacky llvm_unreachable(0); 1475193323Sed 1476193323Sed case convolve(fcNaN, fcZero): 1477193323Sed case convolve(fcNaN, fcNormal): 1478193323Sed case convolve(fcNaN, fcInfinity): 1479193323Sed case convolve(fcNaN, fcNaN): 1480193323Sed case convolve(fcZero, fcInfinity): 1481193323Sed case convolve(fcZero, fcNormal): 1482193323Sed case convolve(fcNormal, fcInfinity): 1483193323Sed return opOK; 1484193323Sed 1485193323Sed case convolve(fcZero, fcNaN): 1486193323Sed case convolve(fcNormal, fcNaN): 1487193323Sed case convolve(fcInfinity, fcNaN): 1488193323Sed category = fcNaN; 1489193323Sed copySignificand(rhs); 1490193323Sed return opOK; 1491193323Sed 1492193323Sed case convolve(fcNormal, fcZero): 1493193323Sed case convolve(fcInfinity, fcZero): 1494193323Sed case convolve(fcInfinity, fcNormal): 1495193323Sed case convolve(fcInfinity, fcInfinity): 1496193323Sed case convolve(fcZero, fcZero): 1497193323Sed makeNaN(); 1498193323Sed return opInvalidOp; 1499193323Sed 1500193323Sed case convolve(fcNormal, fcNormal): 1501193323Sed return opOK; 1502193323Sed } 1503193323Sed} 1504193323Sed 1505193323Sed/* Change sign. */ 1506193323Sedvoid 1507193323SedAPFloat::changeSign() 1508193323Sed{ 1509193323Sed /* Look mummy, this one's easy. */ 1510193323Sed sign = !sign; 1511193323Sed} 1512193323Sed 1513193323Sedvoid 1514193323SedAPFloat::clearSign() 1515193323Sed{ 1516193323Sed /* So is this one. */ 1517193323Sed sign = 0; 1518193323Sed} 1519193323Sed 1520193323Sedvoid 1521193323SedAPFloat::copySign(const APFloat &rhs) 1522193323Sed{ 1523193323Sed /* And this one. */ 1524193323Sed sign = rhs.sign; 1525193323Sed} 1526193323Sed 1527193323Sed/* Normalized addition or subtraction. */ 1528193323SedAPFloat::opStatus 1529193323SedAPFloat::addOrSubtract(const APFloat &rhs, roundingMode rounding_mode, 1530193323Sed bool subtract) 1531193323Sed{ 1532193323Sed opStatus fs; 1533193323Sed 1534193323Sed fs = addOrSubtractSpecials(rhs, subtract); 1535193323Sed 1536193323Sed /* This return code means it was not a simple case. */ 1537206083Srdivacky if (fs == opDivByZero) { 1538193323Sed lostFraction lost_fraction; 1539193323Sed 1540193323Sed lost_fraction = addOrSubtractSignificand(rhs, subtract); 1541193323Sed fs = normalize(rounding_mode, lost_fraction); 1542193323Sed 1543193323Sed /* Can only be zero if we lost no fraction. */ 1544193323Sed assert(category != fcZero || lost_fraction == lfExactlyZero); 1545193323Sed } 1546193323Sed 1547193323Sed /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a 1548193323Sed positive zero unless rounding to minus infinity, except that 1549193323Sed adding two like-signed zeroes gives that zero. */ 1550206083Srdivacky if (category == fcZero) { 1551206083Srdivacky if (rhs.category != fcZero || (sign == rhs.sign) == subtract) 1552193323Sed sign = (rounding_mode == rmTowardNegative); 1553193323Sed } 1554193323Sed 1555193323Sed return fs; 1556193323Sed} 1557193323Sed 1558193323Sed/* Normalized addition. */ 1559193323SedAPFloat::opStatus 1560193323SedAPFloat::add(const APFloat &rhs, roundingMode rounding_mode) 1561193323Sed{ 1562193323Sed return addOrSubtract(rhs, rounding_mode, false); 1563193323Sed} 1564193323Sed 1565193323Sed/* Normalized subtraction. */ 1566193323SedAPFloat::opStatus 1567193323SedAPFloat::subtract(const APFloat &rhs, roundingMode rounding_mode) 1568193323Sed{ 1569193323Sed return addOrSubtract(rhs, rounding_mode, true); 1570193323Sed} 1571193323Sed 1572193323Sed/* Normalized multiply. */ 1573193323SedAPFloat::opStatus 1574193323SedAPFloat::multiply(const APFloat &rhs, roundingMode rounding_mode) 1575193323Sed{ 1576193323Sed opStatus fs; 1577193323Sed 1578193323Sed sign ^= rhs.sign; 1579193323Sed fs = multiplySpecials(rhs); 1580193323Sed 1581206083Srdivacky if (category == fcNormal) { 1582193323Sed lostFraction lost_fraction = multiplySignificand(rhs, 0); 1583193323Sed fs = normalize(rounding_mode, lost_fraction); 1584206083Srdivacky if (lost_fraction != lfExactlyZero) 1585193323Sed fs = (opStatus) (fs | opInexact); 1586193323Sed } 1587193323Sed 1588193323Sed return fs; 1589193323Sed} 1590193323Sed 1591193323Sed/* Normalized divide. */ 1592193323SedAPFloat::opStatus 1593193323SedAPFloat::divide(const APFloat &rhs, roundingMode rounding_mode) 1594193323Sed{ 1595193323Sed opStatus fs; 1596193323Sed 1597193323Sed sign ^= rhs.sign; 1598193323Sed fs = divideSpecials(rhs); 1599193323Sed 1600206083Srdivacky if (category == fcNormal) { 1601193323Sed lostFraction lost_fraction = divideSignificand(rhs); 1602193323Sed fs = normalize(rounding_mode, lost_fraction); 1603206083Srdivacky if (lost_fraction != lfExactlyZero) 1604193323Sed fs = (opStatus) (fs | opInexact); 1605193323Sed } 1606193323Sed 1607193323Sed return fs; 1608193323Sed} 1609193323Sed 1610193323Sed/* Normalized remainder. This is not currently correct in all cases. */ 1611193323SedAPFloat::opStatus 1612193323SedAPFloat::remainder(const APFloat &rhs) 1613193323Sed{ 1614193323Sed opStatus fs; 1615193323Sed APFloat V = *this; 1616193323Sed unsigned int origSign = sign; 1617193323Sed 1618193323Sed fs = V.divide(rhs, rmNearestTiesToEven); 1619193323Sed if (fs == opDivByZero) 1620193323Sed return fs; 1621193323Sed 1622193323Sed int parts = partCount(); 1623193323Sed integerPart *x = new integerPart[parts]; 1624193323Sed bool ignored; 1625193323Sed fs = V.convertToInteger(x, parts * integerPartWidth, true, 1626193323Sed rmNearestTiesToEven, &ignored); 1627193323Sed if (fs==opInvalidOp) 1628193323Sed return fs; 1629193323Sed 1630193323Sed fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true, 1631193323Sed rmNearestTiesToEven); 1632193323Sed assert(fs==opOK); // should always work 1633193323Sed 1634193323Sed fs = V.multiply(rhs, rmNearestTiesToEven); 1635193323Sed assert(fs==opOK || fs==opInexact); // should not overflow or underflow 1636193323Sed 1637193323Sed fs = subtract(V, rmNearestTiesToEven); 1638193323Sed assert(fs==opOK || fs==opInexact); // likewise 1639193323Sed 1640193323Sed if (isZero()) 1641193323Sed sign = origSign; // IEEE754 requires this 1642193323Sed delete[] x; 1643193323Sed return fs; 1644193323Sed} 1645193323Sed 1646206083Srdivacky/* Normalized llvm frem (C fmod). 1647193323Sed This is not currently correct in all cases. */ 1648193323SedAPFloat::opStatus 1649193323SedAPFloat::mod(const APFloat &rhs, roundingMode rounding_mode) 1650193323Sed{ 1651193323Sed opStatus fs; 1652193323Sed fs = modSpecials(rhs); 1653193323Sed 1654193323Sed if (category == fcNormal && rhs.category == fcNormal) { 1655193323Sed APFloat V = *this; 1656193323Sed unsigned int origSign = sign; 1657193323Sed 1658193323Sed fs = V.divide(rhs, rmNearestTiesToEven); 1659193323Sed if (fs == opDivByZero) 1660193323Sed return fs; 1661193323Sed 1662193323Sed int parts = partCount(); 1663193323Sed integerPart *x = new integerPart[parts]; 1664193323Sed bool ignored; 1665193323Sed fs = V.convertToInteger(x, parts * integerPartWidth, true, 1666193323Sed rmTowardZero, &ignored); 1667193323Sed if (fs==opInvalidOp) 1668193323Sed return fs; 1669193323Sed 1670193323Sed fs = V.convertFromZeroExtendedInteger(x, parts * integerPartWidth, true, 1671193323Sed rmNearestTiesToEven); 1672193323Sed assert(fs==opOK); // should always work 1673193323Sed 1674193323Sed fs = V.multiply(rhs, rounding_mode); 1675193323Sed assert(fs==opOK || fs==opInexact); // should not overflow or underflow 1676193323Sed 1677193323Sed fs = subtract(V, rounding_mode); 1678193323Sed assert(fs==opOK || fs==opInexact); // likewise 1679193323Sed 1680193323Sed if (isZero()) 1681193323Sed sign = origSign; // IEEE754 requires this 1682193323Sed delete[] x; 1683193323Sed } 1684193323Sed return fs; 1685193323Sed} 1686193323Sed 1687193323Sed/* Normalized fused-multiply-add. */ 1688193323SedAPFloat::opStatus 1689193323SedAPFloat::fusedMultiplyAdd(const APFloat &multiplicand, 1690193323Sed const APFloat &addend, 1691193323Sed roundingMode rounding_mode) 1692193323Sed{ 1693193323Sed opStatus fs; 1694193323Sed 1695193323Sed /* Post-multiplication sign, before addition. */ 1696193323Sed sign ^= multiplicand.sign; 1697193323Sed 1698193323Sed /* If and only if all arguments are normal do we need to do an 1699193323Sed extended-precision calculation. */ 1700206083Srdivacky if (category == fcNormal && 1701206083Srdivacky multiplicand.category == fcNormal && 1702206083Srdivacky addend.category == fcNormal) { 1703193323Sed lostFraction lost_fraction; 1704193323Sed 1705193323Sed lost_fraction = multiplySignificand(multiplicand, &addend); 1706193323Sed fs = normalize(rounding_mode, lost_fraction); 1707206083Srdivacky if (lost_fraction != lfExactlyZero) 1708193323Sed fs = (opStatus) (fs | opInexact); 1709193323Sed 1710193323Sed /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a 1711193323Sed positive zero unless rounding to minus infinity, except that 1712193323Sed adding two like-signed zeroes gives that zero. */ 1713206083Srdivacky if (category == fcZero && sign != addend.sign) 1714193323Sed sign = (rounding_mode == rmTowardNegative); 1715193323Sed } else { 1716193323Sed fs = multiplySpecials(multiplicand); 1717193323Sed 1718193323Sed /* FS can only be opOK or opInvalidOp. There is no more work 1719193323Sed to do in the latter case. The IEEE-754R standard says it is 1720193323Sed implementation-defined in this case whether, if ADDEND is a 1721193323Sed quiet NaN, we raise invalid op; this implementation does so. 1722193323Sed 1723193323Sed If we need to do the addition we can do so with normal 1724193323Sed precision. */ 1725206083Srdivacky if (fs == opOK) 1726193323Sed fs = addOrSubtract(addend, rounding_mode, false); 1727193323Sed } 1728193323Sed 1729193323Sed return fs; 1730193323Sed} 1731193323Sed 1732239462Sdim/* Rounding-mode corrrect round to integral value. */ 1733239462SdimAPFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) { 1734239462Sdim opStatus fs; 1735239462Sdim 1736239462Sdim // If the exponent is large enough, we know that this value is already 1737239462Sdim // integral, and the arithmetic below would potentially cause it to saturate 1738239462Sdim // to +/-Inf. Bail out early instead. 1739243830Sdim if (category == fcNormal && exponent+1 >= (int)semanticsPrecision(*semantics)) 1740239462Sdim return opOK; 1741239462Sdim 1742239462Sdim // The algorithm here is quite simple: we add 2^(p-1), where p is the 1743239462Sdim // precision of our format, and then subtract it back off again. The choice 1744239462Sdim // of rounding modes for the addition/subtraction determines the rounding mode 1745239462Sdim // for our integral rounding as well. 1746239462Sdim // NOTE: When the input value is negative, we do subtraction followed by 1747239462Sdim // addition instead. 1748239462Sdim APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1); 1749239462Sdim IntegerConstant <<= semanticsPrecision(*semantics)-1; 1750239462Sdim APFloat MagicConstant(*semantics); 1751239462Sdim fs = MagicConstant.convertFromAPInt(IntegerConstant, false, 1752239462Sdim rmNearestTiesToEven); 1753239462Sdim MagicConstant.copySign(*this); 1754239462Sdim 1755239462Sdim if (fs != opOK) 1756239462Sdim return fs; 1757239462Sdim 1758239462Sdim // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly. 1759239462Sdim bool inputSign = isNegative(); 1760239462Sdim 1761239462Sdim fs = add(MagicConstant, rounding_mode); 1762239462Sdim if (fs != opOK && fs != opInexact) 1763239462Sdim return fs; 1764239462Sdim 1765239462Sdim fs = subtract(MagicConstant, rounding_mode); 1766239462Sdim 1767239462Sdim // Restore the input sign. 1768239462Sdim if (inputSign != isNegative()) 1769239462Sdim changeSign(); 1770239462Sdim 1771239462Sdim return fs; 1772239462Sdim} 1773239462Sdim 1774239462Sdim 1775193323Sed/* Comparison requires normalized numbers. */ 1776193323SedAPFloat::cmpResult 1777193323SedAPFloat::compare(const APFloat &rhs) const 1778193323Sed{ 1779193323Sed cmpResult result; 1780193323Sed 1781193323Sed assert(semantics == rhs.semantics); 1782193323Sed 1783193323Sed switch (convolve(category, rhs.category)) { 1784193323Sed default: 1785198090Srdivacky llvm_unreachable(0); 1786193323Sed 1787193323Sed case convolve(fcNaN, fcZero): 1788193323Sed case convolve(fcNaN, fcNormal): 1789193323Sed case convolve(fcNaN, fcInfinity): 1790193323Sed case convolve(fcNaN, fcNaN): 1791193323Sed case convolve(fcZero, fcNaN): 1792193323Sed case convolve(fcNormal, fcNaN): 1793193323Sed case convolve(fcInfinity, fcNaN): 1794193323Sed return cmpUnordered; 1795193323Sed 1796193323Sed case convolve(fcInfinity, fcNormal): 1797193323Sed case convolve(fcInfinity, fcZero): 1798193323Sed case convolve(fcNormal, fcZero): 1799206083Srdivacky if (sign) 1800193323Sed return cmpLessThan; 1801193323Sed else 1802193323Sed return cmpGreaterThan; 1803193323Sed 1804193323Sed case convolve(fcNormal, fcInfinity): 1805193323Sed case convolve(fcZero, fcInfinity): 1806193323Sed case convolve(fcZero, fcNormal): 1807206083Srdivacky if (rhs.sign) 1808193323Sed return cmpGreaterThan; 1809193323Sed else 1810193323Sed return cmpLessThan; 1811193323Sed 1812193323Sed case convolve(fcInfinity, fcInfinity): 1813206083Srdivacky if (sign == rhs.sign) 1814193323Sed return cmpEqual; 1815206083Srdivacky else if (sign) 1816193323Sed return cmpLessThan; 1817193323Sed else 1818193323Sed return cmpGreaterThan; 1819193323Sed 1820193323Sed case convolve(fcZero, fcZero): 1821193323Sed return cmpEqual; 1822193323Sed 1823193323Sed case convolve(fcNormal, fcNormal): 1824193323Sed break; 1825193323Sed } 1826193323Sed 1827193323Sed /* Two normal numbers. Do they have the same sign? */ 1828206083Srdivacky if (sign != rhs.sign) { 1829206083Srdivacky if (sign) 1830193323Sed result = cmpLessThan; 1831193323Sed else 1832193323Sed result = cmpGreaterThan; 1833193323Sed } else { 1834193323Sed /* Compare absolute values; invert result if negative. */ 1835193323Sed result = compareAbsoluteValue(rhs); 1836193323Sed 1837206083Srdivacky if (sign) { 1838206083Srdivacky if (result == cmpLessThan) 1839193323Sed result = cmpGreaterThan; 1840206083Srdivacky else if (result == cmpGreaterThan) 1841193323Sed result = cmpLessThan; 1842193323Sed } 1843193323Sed } 1844193323Sed 1845193323Sed return result; 1846193323Sed} 1847193323Sed 1848193323Sed/// APFloat::convert - convert a value of one floating point type to another. 1849193323Sed/// The return value corresponds to the IEEE754 exceptions. *losesInfo 1850193323Sed/// records whether the transformation lost information, i.e. whether 1851193323Sed/// converting the result back to the original type will produce the 1852193323Sed/// original value (this is almost the same as return value==fsOK, but there 1853193323Sed/// are edge cases where this is not so). 1854193323Sed 1855193323SedAPFloat::opStatus 1856193323SedAPFloat::convert(const fltSemantics &toSemantics, 1857193323Sed roundingMode rounding_mode, bool *losesInfo) 1858193323Sed{ 1859193323Sed lostFraction lostFraction; 1860193323Sed unsigned int newPartCount, oldPartCount; 1861193323Sed opStatus fs; 1862234353Sdim int shift; 1863234353Sdim const fltSemantics &fromSemantics = *semantics; 1864193323Sed 1865193323Sed lostFraction = lfExactlyZero; 1866193323Sed newPartCount = partCountForBits(toSemantics.precision + 1); 1867193323Sed oldPartCount = partCount(); 1868234353Sdim shift = toSemantics.precision - fromSemantics.precision; 1869193323Sed 1870234353Sdim bool X86SpecialNan = false; 1871234353Sdim if (&fromSemantics == &APFloat::x87DoubleExtended && 1872234353Sdim &toSemantics != &APFloat::x87DoubleExtended && category == fcNaN && 1873234353Sdim (!(*significandParts() & 0x8000000000000000ULL) || 1874234353Sdim !(*significandParts() & 0x4000000000000000ULL))) { 1875234353Sdim // x86 has some unusual NaNs which cannot be represented in any other 1876234353Sdim // format; note them here. 1877234353Sdim X86SpecialNan = true; 1878234353Sdim } 1879234353Sdim 1880234353Sdim // If this is a truncation, perform the shift before we narrow the storage. 1881234353Sdim if (shift < 0 && (category==fcNormal || category==fcNaN)) 1882234353Sdim lostFraction = shiftRight(significandParts(), oldPartCount, -shift); 1883234353Sdim 1884234353Sdim // Fix the storage so it can hold to new value. 1885193323Sed if (newPartCount > oldPartCount) { 1886234353Sdim // The new type requires more storage; make it available. 1887193323Sed integerPart *newParts; 1888193323Sed newParts = new integerPart[newPartCount]; 1889193323Sed APInt::tcSet(newParts, 0, newPartCount); 1890193323Sed if (category==fcNormal || category==fcNaN) 1891193323Sed APInt::tcAssign(newParts, significandParts(), oldPartCount); 1892193323Sed freeSignificand(); 1893193323Sed significand.parts = newParts; 1894234353Sdim } else if (newPartCount == 1 && oldPartCount != 1) { 1895234353Sdim // Switch to built-in storage for a single part. 1896234353Sdim integerPart newPart = 0; 1897234353Sdim if (category==fcNormal || category==fcNaN) 1898234353Sdim newPart = significandParts()[0]; 1899234353Sdim freeSignificand(); 1900234353Sdim significand.part = newPart; 1901193323Sed } 1902193323Sed 1903234353Sdim // Now that we have the right storage, switch the semantics. 1904234353Sdim semantics = &toSemantics; 1905234353Sdim 1906234353Sdim // If this is an extension, perform the shift now that the storage is 1907234353Sdim // available. 1908234353Sdim if (shift > 0 && (category==fcNormal || category==fcNaN)) 1909234353Sdim APInt::tcShiftLeft(significandParts(), newPartCount, shift); 1910234353Sdim 1911206083Srdivacky if (category == fcNormal) { 1912193323Sed fs = normalize(rounding_mode, lostFraction); 1913193323Sed *losesInfo = (fs != opOK); 1914193323Sed } else if (category == fcNaN) { 1915234353Sdim *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan; 1916249423Sdim 1917249423Sdim // For x87 extended precision, we want to make a NaN, not a special NaN if 1918249423Sdim // the input wasn't special either. 1919249423Sdim if (!X86SpecialNan && semantics == &APFloat::x87DoubleExtended) 1920249423Sdim APInt::tcSetBit(significandParts(), semantics->precision - 1); 1921249423Sdim 1922193323Sed // gcc forces the Quiet bit on, which means (float)(double)(float_sNan) 1923193323Sed // does not give you back the same bits. This is dubious, and we 1924193323Sed // don't currently do it. You're really supposed to get 1925193323Sed // an invalid operation signal at runtime, but nobody does that. 1926193323Sed fs = opOK; 1927193323Sed } else { 1928234353Sdim *losesInfo = false; 1929193323Sed fs = opOK; 1930193323Sed } 1931193323Sed 1932193323Sed return fs; 1933193323Sed} 1934193323Sed 1935193323Sed/* Convert a floating point number to an integer according to the 1936193323Sed rounding mode. If the rounded integer value is out of range this 1937193323Sed returns an invalid operation exception and the contents of the 1938193323Sed destination parts are unspecified. If the rounded value is in 1939193323Sed range but the floating point number is not the exact integer, the C 1940193323Sed standard doesn't require an inexact exception to be raised. IEEE 1941193323Sed 854 does require it so we do that. 1942193323Sed 1943193323Sed Note that for conversions to integer type the C standard requires 1944193323Sed round-to-zero to always be used. */ 1945193323SedAPFloat::opStatus 1946193323SedAPFloat::convertToSignExtendedInteger(integerPart *parts, unsigned int width, 1947193323Sed bool isSigned, 1948193323Sed roundingMode rounding_mode, 1949193323Sed bool *isExact) const 1950193323Sed{ 1951193323Sed lostFraction lost_fraction; 1952193323Sed const integerPart *src; 1953193323Sed unsigned int dstPartsCount, truncatedBits; 1954193323Sed 1955193323Sed *isExact = false; 1956193323Sed 1957193323Sed /* Handle the three special cases first. */ 1958206083Srdivacky if (category == fcInfinity || category == fcNaN) 1959193323Sed return opInvalidOp; 1960193323Sed 1961193323Sed dstPartsCount = partCountForBits(width); 1962193323Sed 1963206083Srdivacky if (category == fcZero) { 1964193323Sed APInt::tcSet(parts, 0, dstPartsCount); 1965193323Sed // Negative zero can't be represented as an int. 1966193323Sed *isExact = !sign; 1967193323Sed return opOK; 1968193323Sed } 1969193323Sed 1970193323Sed src = significandParts(); 1971193323Sed 1972193323Sed /* Step 1: place our absolute value, with any fraction truncated, in 1973193323Sed the destination. */ 1974193323Sed if (exponent < 0) { 1975193323Sed /* Our absolute value is less than one; truncate everything. */ 1976193323Sed APInt::tcSet(parts, 0, dstPartsCount); 1977193323Sed /* For exponent -1 the integer bit represents .5, look at that. 1978193323Sed For smaller exponents leftmost truncated bit is 0. */ 1979193323Sed truncatedBits = semantics->precision -1U - exponent; 1980193323Sed } else { 1981193323Sed /* We want the most significant (exponent + 1) bits; the rest are 1982193323Sed truncated. */ 1983193323Sed unsigned int bits = exponent + 1U; 1984193323Sed 1985193323Sed /* Hopelessly large in magnitude? */ 1986193323Sed if (bits > width) 1987193323Sed return opInvalidOp; 1988193323Sed 1989193323Sed if (bits < semantics->precision) { 1990193323Sed /* We truncate (semantics->precision - bits) bits. */ 1991193323Sed truncatedBits = semantics->precision - bits; 1992193323Sed APInt::tcExtract(parts, dstPartsCount, src, bits, truncatedBits); 1993193323Sed } else { 1994193323Sed /* We want at least as many bits as are available. */ 1995193323Sed APInt::tcExtract(parts, dstPartsCount, src, semantics->precision, 0); 1996193323Sed APInt::tcShiftLeft(parts, dstPartsCount, bits - semantics->precision); 1997193323Sed truncatedBits = 0; 1998193323Sed } 1999193323Sed } 2000193323Sed 2001193323Sed /* Step 2: work out any lost fraction, and increment the absolute 2002193323Sed value if we would round away from zero. */ 2003193323Sed if (truncatedBits) { 2004193323Sed lost_fraction = lostFractionThroughTruncation(src, partCount(), 2005193323Sed truncatedBits); 2006206083Srdivacky if (lost_fraction != lfExactlyZero && 2007206083Srdivacky roundAwayFromZero(rounding_mode, lost_fraction, truncatedBits)) { 2008193323Sed if (APInt::tcIncrement(parts, dstPartsCount)) 2009193323Sed return opInvalidOp; /* Overflow. */ 2010193323Sed } 2011193323Sed } else { 2012193323Sed lost_fraction = lfExactlyZero; 2013193323Sed } 2014193323Sed 2015193323Sed /* Step 3: check if we fit in the destination. */ 2016193323Sed unsigned int omsb = APInt::tcMSB(parts, dstPartsCount) + 1; 2017193323Sed 2018193323Sed if (sign) { 2019193323Sed if (!isSigned) { 2020193323Sed /* Negative numbers cannot be represented as unsigned. */ 2021193323Sed if (omsb != 0) 2022193323Sed return opInvalidOp; 2023193323Sed } else { 2024193323Sed /* It takes omsb bits to represent the unsigned integer value. 2025193323Sed We lose a bit for the sign, but care is needed as the 2026193323Sed maximally negative integer is a special case. */ 2027193323Sed if (omsb == width && APInt::tcLSB(parts, dstPartsCount) + 1 != omsb) 2028193323Sed return opInvalidOp; 2029193323Sed 2030193323Sed /* This case can happen because of rounding. */ 2031193323Sed if (omsb > width) 2032193323Sed return opInvalidOp; 2033193323Sed } 2034193323Sed 2035193323Sed APInt::tcNegate (parts, dstPartsCount); 2036193323Sed } else { 2037193323Sed if (omsb >= width + !isSigned) 2038193323Sed return opInvalidOp; 2039193323Sed } 2040193323Sed 2041193323Sed if (lost_fraction == lfExactlyZero) { 2042193323Sed *isExact = true; 2043193323Sed return opOK; 2044193323Sed } else 2045193323Sed return opInexact; 2046193323Sed} 2047193323Sed 2048193323Sed/* Same as convertToSignExtendedInteger, except we provide 2049193323Sed deterministic values in case of an invalid operation exception, 2050193323Sed namely zero for NaNs and the minimal or maximal value respectively 2051193323Sed for underflow or overflow. 2052193323Sed The *isExact output tells whether the result is exact, in the sense 2053193323Sed that converting it back to the original floating point type produces 2054193323Sed the original value. This is almost equivalent to result==opOK, 2055193323Sed except for negative zeroes. 2056193323Sed*/ 2057193323SedAPFloat::opStatus 2058193323SedAPFloat::convertToInteger(integerPart *parts, unsigned int width, 2059193323Sed bool isSigned, 2060193323Sed roundingMode rounding_mode, bool *isExact) const 2061193323Sed{ 2062193323Sed opStatus fs; 2063193323Sed 2064206083Srdivacky fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode, 2065193323Sed isExact); 2066193323Sed 2067193323Sed if (fs == opInvalidOp) { 2068193323Sed unsigned int bits, dstPartsCount; 2069193323Sed 2070193323Sed dstPartsCount = partCountForBits(width); 2071193323Sed 2072193323Sed if (category == fcNaN) 2073193323Sed bits = 0; 2074193323Sed else if (sign) 2075193323Sed bits = isSigned; 2076193323Sed else 2077193323Sed bits = width - isSigned; 2078193323Sed 2079193323Sed APInt::tcSetLeastSignificantBits(parts, dstPartsCount, bits); 2080193323Sed if (sign && isSigned) 2081193323Sed APInt::tcShiftLeft(parts, dstPartsCount, width - 1); 2082193323Sed } 2083193323Sed 2084193323Sed return fs; 2085193323Sed} 2086193323Sed 2087224145Sdim/* Same as convertToInteger(integerPart*, ...), except the result is returned in 2088224145Sdim an APSInt, whose initial bit-width and signed-ness are used to determine the 2089224145Sdim precision of the conversion. 2090224145Sdim */ 2091224145SdimAPFloat::opStatus 2092224145SdimAPFloat::convertToInteger(APSInt &result, 2093224145Sdim roundingMode rounding_mode, bool *isExact) const 2094224145Sdim{ 2095224145Sdim unsigned bitWidth = result.getBitWidth(); 2096224145Sdim SmallVector<uint64_t, 4> parts(result.getNumWords()); 2097224145Sdim opStatus status = convertToInteger( 2098224145Sdim parts.data(), bitWidth, result.isSigned(), rounding_mode, isExact); 2099224145Sdim // Keeps the original signed-ness. 2100226633Sdim result = APInt(bitWidth, parts); 2101224145Sdim return status; 2102224145Sdim} 2103224145Sdim 2104193323Sed/* Convert an unsigned integer SRC to a floating point number, 2105193323Sed rounding according to ROUNDING_MODE. The sign of the floating 2106193323Sed point number is not modified. */ 2107193323SedAPFloat::opStatus 2108193323SedAPFloat::convertFromUnsignedParts(const integerPart *src, 2109193323Sed unsigned int srcCount, 2110193323Sed roundingMode rounding_mode) 2111193323Sed{ 2112193323Sed unsigned int omsb, precision, dstCount; 2113193323Sed integerPart *dst; 2114193323Sed lostFraction lost_fraction; 2115193323Sed 2116193323Sed category = fcNormal; 2117193323Sed omsb = APInt::tcMSB(src, srcCount) + 1; 2118193323Sed dst = significandParts(); 2119193323Sed dstCount = partCount(); 2120193323Sed precision = semantics->precision; 2121193323Sed 2122226633Sdim /* We want the most significant PRECISION bits of SRC. There may not 2123193323Sed be that many; extract what we can. */ 2124193323Sed if (precision <= omsb) { 2125193323Sed exponent = omsb - 1; 2126193323Sed lost_fraction = lostFractionThroughTruncation(src, srcCount, 2127193323Sed omsb - precision); 2128193323Sed APInt::tcExtract(dst, dstCount, src, precision, omsb - precision); 2129193323Sed } else { 2130193323Sed exponent = precision - 1; 2131193323Sed lost_fraction = lfExactlyZero; 2132193323Sed APInt::tcExtract(dst, dstCount, src, omsb, 0); 2133193323Sed } 2134193323Sed 2135193323Sed return normalize(rounding_mode, lost_fraction); 2136193323Sed} 2137193323Sed 2138193323SedAPFloat::opStatus 2139193323SedAPFloat::convertFromAPInt(const APInt &Val, 2140193323Sed bool isSigned, 2141193323Sed roundingMode rounding_mode) 2142193323Sed{ 2143193323Sed unsigned int partCount = Val.getNumWords(); 2144193323Sed APInt api = Val; 2145193323Sed 2146193323Sed sign = false; 2147193323Sed if (isSigned && api.isNegative()) { 2148193323Sed sign = true; 2149193323Sed api = -api; 2150193323Sed } 2151193323Sed 2152193323Sed return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); 2153193323Sed} 2154193323Sed 2155193323Sed/* Convert a two's complement integer SRC to a floating point number, 2156193323Sed rounding according to ROUNDING_MODE. ISSIGNED is true if the 2157193323Sed integer is signed, in which case it must be sign-extended. */ 2158193323SedAPFloat::opStatus 2159193323SedAPFloat::convertFromSignExtendedInteger(const integerPart *src, 2160193323Sed unsigned int srcCount, 2161193323Sed bool isSigned, 2162193323Sed roundingMode rounding_mode) 2163193323Sed{ 2164193323Sed opStatus status; 2165193323Sed 2166206083Srdivacky if (isSigned && 2167206083Srdivacky APInt::tcExtractBit(src, srcCount * integerPartWidth - 1)) { 2168193323Sed integerPart *copy; 2169193323Sed 2170193323Sed /* If we're signed and negative negate a copy. */ 2171193323Sed sign = true; 2172193323Sed copy = new integerPart[srcCount]; 2173193323Sed APInt::tcAssign(copy, src, srcCount); 2174193323Sed APInt::tcNegate(copy, srcCount); 2175193323Sed status = convertFromUnsignedParts(copy, srcCount, rounding_mode); 2176193323Sed delete [] copy; 2177193323Sed } else { 2178193323Sed sign = false; 2179193323Sed status = convertFromUnsignedParts(src, srcCount, rounding_mode); 2180193323Sed } 2181193323Sed 2182193323Sed return status; 2183193323Sed} 2184193323Sed 2185193323Sed/* FIXME: should this just take a const APInt reference? */ 2186193323SedAPFloat::opStatus 2187193323SedAPFloat::convertFromZeroExtendedInteger(const integerPart *parts, 2188193323Sed unsigned int width, bool isSigned, 2189193323Sed roundingMode rounding_mode) 2190193323Sed{ 2191193323Sed unsigned int partCount = partCountForBits(width); 2192226633Sdim APInt api = APInt(width, makeArrayRef(parts, partCount)); 2193193323Sed 2194193323Sed sign = false; 2195206083Srdivacky if (isSigned && APInt::tcExtractBit(parts, width - 1)) { 2196193323Sed sign = true; 2197193323Sed api = -api; 2198193323Sed } 2199193323Sed 2200193323Sed return convertFromUnsignedParts(api.getRawData(), partCount, rounding_mode); 2201193323Sed} 2202193323Sed 2203193323SedAPFloat::opStatus 2204210299SedAPFloat::convertFromHexadecimalString(StringRef s, roundingMode rounding_mode) 2205193323Sed{ 2206198090Srdivacky lostFraction lost_fraction = lfExactlyZero; 2207193323Sed integerPart *significand; 2208193323Sed unsigned int bitPos, partsCount; 2209198090Srdivacky StringRef::iterator dot, firstSignificantDigit; 2210193323Sed 2211193323Sed zeroSignificand(); 2212193323Sed exponent = 0; 2213193323Sed category = fcNormal; 2214193323Sed 2215193323Sed significand = significandParts(); 2216193323Sed partsCount = partCount(); 2217193323Sed bitPos = partsCount * integerPartWidth; 2218193323Sed 2219193323Sed /* Skip leading zeroes and any (hexa)decimal point. */ 2220198090Srdivacky StringRef::iterator begin = s.begin(); 2221198090Srdivacky StringRef::iterator end = s.end(); 2222198090Srdivacky StringRef::iterator p = skipLeadingZeroesAndAnyDot(begin, end, &dot); 2223193323Sed firstSignificantDigit = p; 2224193323Sed 2225206083Srdivacky for (; p != end;) { 2226193323Sed integerPart hex_value; 2227193323Sed 2228206083Srdivacky if (*p == '.') { 2229198090Srdivacky assert(dot == end && "String contains multiple dots"); 2230193323Sed dot = p++; 2231198090Srdivacky if (p == end) { 2232198090Srdivacky break; 2233198090Srdivacky } 2234193323Sed } 2235193323Sed 2236193323Sed hex_value = hexDigitValue(*p); 2237206083Srdivacky if (hex_value == -1U) { 2238193323Sed break; 2239193323Sed } 2240193323Sed 2241193323Sed p++; 2242193323Sed 2243198090Srdivacky if (p == end) { 2244198090Srdivacky break; 2245193323Sed } else { 2246198090Srdivacky /* Store the number whilst 4-bit nibbles remain. */ 2247206083Srdivacky if (bitPos) { 2248198090Srdivacky bitPos -= 4; 2249198090Srdivacky hex_value <<= bitPos % integerPartWidth; 2250198090Srdivacky significand[bitPos / integerPartWidth] |= hex_value; 2251198090Srdivacky } else { 2252198090Srdivacky lost_fraction = trailingHexadecimalFraction(p, end, hex_value); 2253206083Srdivacky while (p != end && hexDigitValue(*p) != -1U) 2254198090Srdivacky p++; 2255198090Srdivacky break; 2256198090Srdivacky } 2257193323Sed } 2258193323Sed } 2259193323Sed 2260193323Sed /* Hex floats require an exponent but not a hexadecimal point. */ 2261198090Srdivacky assert(p != end && "Hex strings require an exponent"); 2262198090Srdivacky assert((*p == 'p' || *p == 'P') && "Invalid character in significand"); 2263198090Srdivacky assert(p != begin && "Significand has no digits"); 2264198090Srdivacky assert((dot == end || p - begin != 1) && "Significand has no digits"); 2265193323Sed 2266193323Sed /* Ignore the exponent if we are zero. */ 2267206083Srdivacky if (p != firstSignificantDigit) { 2268193323Sed int expAdjustment; 2269193323Sed 2270193323Sed /* Implicit hexadecimal point? */ 2271198090Srdivacky if (dot == end) 2272193323Sed dot = p; 2273193323Sed 2274193323Sed /* Calculate the exponent adjustment implicit in the number of 2275193323Sed significant digits. */ 2276193323Sed expAdjustment = static_cast<int>(dot - firstSignificantDigit); 2277206083Srdivacky if (expAdjustment < 0) 2278193323Sed expAdjustment++; 2279193323Sed expAdjustment = expAdjustment * 4 - 1; 2280193323Sed 2281193323Sed /* Adjust for writing the significand starting at the most 2282193323Sed significant nibble. */ 2283193323Sed expAdjustment += semantics->precision; 2284193323Sed expAdjustment -= partsCount * integerPartWidth; 2285193323Sed 2286193323Sed /* Adjust for the given exponent. */ 2287198090Srdivacky exponent = totalExponent(p + 1, end, expAdjustment); 2288193323Sed } 2289193323Sed 2290193323Sed return normalize(rounding_mode, lost_fraction); 2291193323Sed} 2292193323Sed 2293193323SedAPFloat::opStatus 2294193323SedAPFloat::roundSignificandWithExponent(const integerPart *decSigParts, 2295193323Sed unsigned sigPartCount, int exp, 2296193323Sed roundingMode rounding_mode) 2297193323Sed{ 2298193323Sed unsigned int parts, pow5PartCount; 2299243830Sdim fltSemantics calcSemantics = { 32767, -32767, 0 }; 2300193323Sed integerPart pow5Parts[maxPowerOfFiveParts]; 2301193323Sed bool isNearest; 2302193323Sed 2303206083Srdivacky isNearest = (rounding_mode == rmNearestTiesToEven || 2304206083Srdivacky rounding_mode == rmNearestTiesToAway); 2305193323Sed 2306193323Sed parts = partCountForBits(semantics->precision + 11); 2307193323Sed 2308193323Sed /* Calculate pow(5, abs(exp)). */ 2309193323Sed pow5PartCount = powerOf5(pow5Parts, exp >= 0 ? exp: -exp); 2310193323Sed 2311193323Sed for (;; parts *= 2) { 2312193323Sed opStatus sigStatus, powStatus; 2313193323Sed unsigned int excessPrecision, truncatedBits; 2314193323Sed 2315193323Sed calcSemantics.precision = parts * integerPartWidth - 1; 2316193323Sed excessPrecision = calcSemantics.precision - semantics->precision; 2317193323Sed truncatedBits = excessPrecision; 2318193323Sed 2319193323Sed APFloat decSig(calcSemantics, fcZero, sign); 2320193323Sed APFloat pow5(calcSemantics, fcZero, false); 2321193323Sed 2322193323Sed sigStatus = decSig.convertFromUnsignedParts(decSigParts, sigPartCount, 2323193323Sed rmNearestTiesToEven); 2324193323Sed powStatus = pow5.convertFromUnsignedParts(pow5Parts, pow5PartCount, 2325193323Sed rmNearestTiesToEven); 2326193323Sed /* Add exp, as 10^n = 5^n * 2^n. */ 2327193323Sed decSig.exponent += exp; 2328193323Sed 2329193323Sed lostFraction calcLostFraction; 2330193323Sed integerPart HUerr, HUdistance; 2331193323Sed unsigned int powHUerr; 2332193323Sed 2333193323Sed if (exp >= 0) { 2334193323Sed /* multiplySignificand leaves the precision-th bit set to 1. */ 2335193323Sed calcLostFraction = decSig.multiplySignificand(pow5, NULL); 2336193323Sed powHUerr = powStatus != opOK; 2337193323Sed } else { 2338193323Sed calcLostFraction = decSig.divideSignificand(pow5); 2339193323Sed /* Denormal numbers have less precision. */ 2340193323Sed if (decSig.exponent < semantics->minExponent) { 2341193323Sed excessPrecision += (semantics->minExponent - decSig.exponent); 2342193323Sed truncatedBits = excessPrecision; 2343193323Sed if (excessPrecision > calcSemantics.precision) 2344193323Sed excessPrecision = calcSemantics.precision; 2345193323Sed } 2346193323Sed /* Extra half-ulp lost in reciprocal of exponent. */ 2347193323Sed powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2; 2348193323Sed } 2349193323Sed 2350193323Sed /* Both multiplySignificand and divideSignificand return the 2351193323Sed result with the integer bit set. */ 2352198892Srdivacky assert(APInt::tcExtractBit 2353198892Srdivacky (decSig.significandParts(), calcSemantics.precision - 1) == 1); 2354193323Sed 2355193323Sed HUerr = HUerrBound(calcLostFraction != lfExactlyZero, sigStatus != opOK, 2356193323Sed powHUerr); 2357193323Sed HUdistance = 2 * ulpsFromBoundary(decSig.significandParts(), 2358193323Sed excessPrecision, isNearest); 2359193323Sed 2360193323Sed /* Are we guaranteed to round correctly if we truncate? */ 2361193323Sed if (HUdistance >= HUerr) { 2362193323Sed APInt::tcExtract(significandParts(), partCount(), decSig.significandParts(), 2363193323Sed calcSemantics.precision - excessPrecision, 2364193323Sed excessPrecision); 2365193323Sed /* Take the exponent of decSig. If we tcExtract-ed less bits 2366193323Sed above we must adjust our exponent to compensate for the 2367193323Sed implicit right shift. */ 2368193323Sed exponent = (decSig.exponent + semantics->precision 2369193323Sed - (calcSemantics.precision - excessPrecision)); 2370193323Sed calcLostFraction = lostFractionThroughTruncation(decSig.significandParts(), 2371193323Sed decSig.partCount(), 2372193323Sed truncatedBits); 2373193323Sed return normalize(rounding_mode, calcLostFraction); 2374193323Sed } 2375193323Sed } 2376193323Sed} 2377193323Sed 2378193323SedAPFloat::opStatus 2379210299SedAPFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) 2380193323Sed{ 2381193323Sed decimalInfo D; 2382193323Sed opStatus fs; 2383193323Sed 2384193323Sed /* Scan the text. */ 2385198090Srdivacky StringRef::iterator p = str.begin(); 2386198090Srdivacky interpretDecimal(p, str.end(), &D); 2387193323Sed 2388193323Sed /* Handle the quick cases. First the case of no significant digits, 2389193323Sed i.e. zero, and then exponents that are obviously too large or too 2390193323Sed small. Writing L for log 10 / log 2, a number d.ddddd*10^exp 2391193323Sed definitely overflows if 2392193323Sed 2393193323Sed (exp - 1) * L >= maxExponent 2394193323Sed 2395193323Sed and definitely underflows to zero where 2396193323Sed 2397193323Sed (exp + 1) * L <= minExponent - precision 2398193323Sed 2399193323Sed With integer arithmetic the tightest bounds for L are 2400193323Sed 2401193323Sed 93/28 < L < 196/59 [ numerator <= 256 ] 2402193323Sed 42039/12655 < L < 28738/8651 [ numerator <= 65536 ] 2403193323Sed */ 2404193323Sed 2405193323Sed if (decDigitValue(*D.firstSigDigit) >= 10U) { 2406193323Sed category = fcZero; 2407193323Sed fs = opOK; 2408204642Srdivacky 2409204642Srdivacky /* Check whether the normalized exponent is high enough to overflow 2410204642Srdivacky max during the log-rebasing in the max-exponent check below. */ 2411204642Srdivacky } else if (D.normalizedExponent - 1 > INT_MAX / 42039) { 2412204642Srdivacky fs = handleOverflow(rounding_mode); 2413204642Srdivacky 2414204642Srdivacky /* If it wasn't, then it also wasn't high enough to overflow max 2415204642Srdivacky during the log-rebasing in the min-exponent check. Check that it 2416204642Srdivacky won't overflow min in either check, then perform the min-exponent 2417204642Srdivacky check. */ 2418204642Srdivacky } else if (D.normalizedExponent - 1 < INT_MIN / 42039 || 2419204642Srdivacky (D.normalizedExponent + 1) * 28738 <= 2420204642Srdivacky 8651 * (semantics->minExponent - (int) semantics->precision)) { 2421193323Sed /* Underflow to zero and round. */ 2422193323Sed zeroSignificand(); 2423193323Sed fs = normalize(rounding_mode, lfLessThanHalf); 2424204642Srdivacky 2425204642Srdivacky /* We can finally safely perform the max-exponent check. */ 2426193323Sed } else if ((D.normalizedExponent - 1) * 42039 2427193323Sed >= 12655 * semantics->maxExponent) { 2428193323Sed /* Overflow and round. */ 2429193323Sed fs = handleOverflow(rounding_mode); 2430193323Sed } else { 2431193323Sed integerPart *decSignificand; 2432193323Sed unsigned int partCount; 2433193323Sed 2434193323Sed /* A tight upper bound on number of bits required to hold an 2435193323Sed N-digit decimal integer is N * 196 / 59. Allocate enough space 2436193323Sed to hold the full significand, and an extra part required by 2437193323Sed tcMultiplyPart. */ 2438193323Sed partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1; 2439193323Sed partCount = partCountForBits(1 + 196 * partCount / 59); 2440193323Sed decSignificand = new integerPart[partCount + 1]; 2441193323Sed partCount = 0; 2442193323Sed 2443193323Sed /* Convert to binary efficiently - we do almost all multiplication 2444193323Sed in an integerPart. When this would overflow do we do a single 2445193323Sed bignum multiplication, and then revert again to multiplication 2446193323Sed in an integerPart. */ 2447193323Sed do { 2448193323Sed integerPart decValue, val, multiplier; 2449193323Sed 2450193323Sed val = 0; 2451193323Sed multiplier = 1; 2452193323Sed 2453193323Sed do { 2454198090Srdivacky if (*p == '.') { 2455193323Sed p++; 2456198090Srdivacky if (p == str.end()) { 2457198090Srdivacky break; 2458198090Srdivacky } 2459198090Srdivacky } 2460193323Sed decValue = decDigitValue(*p++); 2461198090Srdivacky assert(decValue < 10U && "Invalid character in significand"); 2462193323Sed multiplier *= 10; 2463193323Sed val = val * 10 + decValue; 2464193323Sed /* The maximum number that can be multiplied by ten with any 2465193323Sed digit added without overflowing an integerPart. */ 2466193323Sed } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10); 2467193323Sed 2468193323Sed /* Multiply out the current part. */ 2469193323Sed APInt::tcMultiplyPart(decSignificand, decSignificand, multiplier, val, 2470193323Sed partCount, partCount + 1, false); 2471193323Sed 2472193323Sed /* If we used another part (likely but not guaranteed), increase 2473193323Sed the count. */ 2474193323Sed if (decSignificand[partCount]) 2475193323Sed partCount++; 2476193323Sed } while (p <= D.lastSigDigit); 2477193323Sed 2478193323Sed category = fcNormal; 2479193323Sed fs = roundSignificandWithExponent(decSignificand, partCount, 2480193323Sed D.exponent, rounding_mode); 2481193323Sed 2482193323Sed delete [] decSignificand; 2483193323Sed } 2484193323Sed 2485193323Sed return fs; 2486193323Sed} 2487193323Sed 2488193323SedAPFloat::opStatus 2489210299SedAPFloat::convertFromString(StringRef str, roundingMode rounding_mode) 2490193323Sed{ 2491198090Srdivacky assert(!str.empty() && "Invalid string length"); 2492193323Sed 2493193323Sed /* Handle a leading minus sign. */ 2494198090Srdivacky StringRef::iterator p = str.begin(); 2495198090Srdivacky size_t slen = str.size(); 2496198090Srdivacky sign = *p == '-' ? 1 : 0; 2497206083Srdivacky if (*p == '-' || *p == '+') { 2498198090Srdivacky p++; 2499198090Srdivacky slen--; 2500198090Srdivacky assert(slen && "String has no digits"); 2501198090Srdivacky } 2502193323Sed 2503206083Srdivacky if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { 2504198090Srdivacky assert(slen - 2 && "Invalid string"); 2505198090Srdivacky return convertFromHexadecimalString(StringRef(p + 2, slen - 2), 2506198090Srdivacky rounding_mode); 2507198090Srdivacky } 2508193323Sed 2509198090Srdivacky return convertFromDecimalString(StringRef(p, slen), rounding_mode); 2510193323Sed} 2511193323Sed 2512193323Sed/* Write out a hexadecimal representation of the floating point value 2513193323Sed to DST, which must be of sufficient size, in the C99 form 2514193323Sed [-]0xh.hhhhp[+-]d. Return the number of characters written, 2515193323Sed excluding the terminating NUL. 2516193323Sed 2517193323Sed If UPPERCASE, the output is in upper case, otherwise in lower case. 2518193323Sed 2519193323Sed HEXDIGITS digits appear altogether, rounding the value if 2520193323Sed necessary. If HEXDIGITS is 0, the minimal precision to display the 2521193323Sed number precisely is used instead. If nothing would appear after 2522193323Sed the decimal point it is suppressed. 2523193323Sed 2524193323Sed The decimal exponent is always printed and has at least one digit. 2525193323Sed Zero values display an exponent of zero. Infinities and NaNs 2526193323Sed appear as "infinity" or "nan" respectively. 2527193323Sed 2528193323Sed The above rules are as specified by C99. There is ambiguity about 2529193323Sed what the leading hexadecimal digit should be. This implementation 2530193323Sed uses whatever is necessary so that the exponent is displayed as 2531193323Sed stored. This implies the exponent will fall within the IEEE format 2532193323Sed range, and the leading hexadecimal digit will be 0 (for denormals), 2533193323Sed 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with 2534193323Sed any other digits zero). 2535193323Sed*/ 2536193323Sedunsigned int 2537193323SedAPFloat::convertToHexString(char *dst, unsigned int hexDigits, 2538193323Sed bool upperCase, roundingMode rounding_mode) const 2539193323Sed{ 2540193323Sed char *p; 2541193323Sed 2542193323Sed p = dst; 2543193323Sed if (sign) 2544193323Sed *dst++ = '-'; 2545193323Sed 2546193323Sed switch (category) { 2547193323Sed case fcInfinity: 2548193323Sed memcpy (dst, upperCase ? infinityU: infinityL, sizeof infinityU - 1); 2549193323Sed dst += sizeof infinityL - 1; 2550193323Sed break; 2551193323Sed 2552193323Sed case fcNaN: 2553193323Sed memcpy (dst, upperCase ? NaNU: NaNL, sizeof NaNU - 1); 2554193323Sed dst += sizeof NaNU - 1; 2555193323Sed break; 2556193323Sed 2557193323Sed case fcZero: 2558193323Sed *dst++ = '0'; 2559193323Sed *dst++ = upperCase ? 'X': 'x'; 2560193323Sed *dst++ = '0'; 2561193323Sed if (hexDigits > 1) { 2562193323Sed *dst++ = '.'; 2563193323Sed memset (dst, '0', hexDigits - 1); 2564193323Sed dst += hexDigits - 1; 2565193323Sed } 2566193323Sed *dst++ = upperCase ? 'P': 'p'; 2567193323Sed *dst++ = '0'; 2568193323Sed break; 2569193323Sed 2570193323Sed case fcNormal: 2571193323Sed dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode); 2572193323Sed break; 2573193323Sed } 2574193323Sed 2575193323Sed *dst = 0; 2576193323Sed 2577193323Sed return static_cast<unsigned int>(dst - p); 2578193323Sed} 2579193323Sed 2580193323Sed/* Does the hard work of outputting the correctly rounded hexadecimal 2581193323Sed form of a normal floating point number with the specified number of 2582193323Sed hexadecimal digits. If HEXDIGITS is zero the minimum number of 2583193323Sed digits necessary to print the value precisely is output. */ 2584193323Sedchar * 2585193323SedAPFloat::convertNormalToHexString(char *dst, unsigned int hexDigits, 2586193323Sed bool upperCase, 2587193323Sed roundingMode rounding_mode) const 2588193323Sed{ 2589193323Sed unsigned int count, valueBits, shift, partsCount, outputDigits; 2590193323Sed const char *hexDigitChars; 2591193323Sed const integerPart *significand; 2592193323Sed char *p; 2593193323Sed bool roundUp; 2594193323Sed 2595193323Sed *dst++ = '0'; 2596193323Sed *dst++ = upperCase ? 'X': 'x'; 2597193323Sed 2598193323Sed roundUp = false; 2599193323Sed hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower; 2600193323Sed 2601193323Sed significand = significandParts(); 2602193323Sed partsCount = partCount(); 2603193323Sed 2604193323Sed /* +3 because the first digit only uses the single integer bit, so 2605193323Sed we have 3 virtual zero most-significant-bits. */ 2606193323Sed valueBits = semantics->precision + 3; 2607193323Sed shift = integerPartWidth - valueBits % integerPartWidth; 2608193323Sed 2609193323Sed /* The natural number of digits required ignoring trailing 2610193323Sed insignificant zeroes. */ 2611193323Sed outputDigits = (valueBits - significandLSB () + 3) / 4; 2612193323Sed 2613193323Sed /* hexDigits of zero means use the required number for the 2614193323Sed precision. Otherwise, see if we are truncating. If we are, 2615193323Sed find out if we need to round away from zero. */ 2616193323Sed if (hexDigits) { 2617193323Sed if (hexDigits < outputDigits) { 2618193323Sed /* We are dropping non-zero bits, so need to check how to round. 2619193323Sed "bits" is the number of dropped bits. */ 2620193323Sed unsigned int bits; 2621193323Sed lostFraction fraction; 2622193323Sed 2623193323Sed bits = valueBits - hexDigits * 4; 2624193323Sed fraction = lostFractionThroughTruncation (significand, partsCount, bits); 2625193323Sed roundUp = roundAwayFromZero(rounding_mode, fraction, bits); 2626193323Sed } 2627193323Sed outputDigits = hexDigits; 2628193323Sed } 2629193323Sed 2630193323Sed /* Write the digits consecutively, and start writing in the location 2631193323Sed of the hexadecimal point. We move the most significant digit 2632193323Sed left and add the hexadecimal point later. */ 2633193323Sed p = ++dst; 2634193323Sed 2635193323Sed count = (valueBits + integerPartWidth - 1) / integerPartWidth; 2636193323Sed 2637193323Sed while (outputDigits && count) { 2638193323Sed integerPart part; 2639193323Sed 2640193323Sed /* Put the most significant integerPartWidth bits in "part". */ 2641193323Sed if (--count == partsCount) 2642193323Sed part = 0; /* An imaginary higher zero part. */ 2643193323Sed else 2644193323Sed part = significand[count] << shift; 2645193323Sed 2646193323Sed if (count && shift) 2647193323Sed part |= significand[count - 1] >> (integerPartWidth - shift); 2648193323Sed 2649193323Sed /* Convert as much of "part" to hexdigits as we can. */ 2650193323Sed unsigned int curDigits = integerPartWidth / 4; 2651193323Sed 2652193323Sed if (curDigits > outputDigits) 2653193323Sed curDigits = outputDigits; 2654193323Sed dst += partAsHex (dst, part, curDigits, hexDigitChars); 2655193323Sed outputDigits -= curDigits; 2656193323Sed } 2657193323Sed 2658193323Sed if (roundUp) { 2659193323Sed char *q = dst; 2660193323Sed 2661193323Sed /* Note that hexDigitChars has a trailing '0'. */ 2662193323Sed do { 2663193323Sed q--; 2664193323Sed *q = hexDigitChars[hexDigitValue (*q) + 1]; 2665193323Sed } while (*q == '0'); 2666198892Srdivacky assert(q >= p); 2667193323Sed } else { 2668193323Sed /* Add trailing zeroes. */ 2669193323Sed memset (dst, '0', outputDigits); 2670193323Sed dst += outputDigits; 2671193323Sed } 2672193323Sed 2673193323Sed /* Move the most significant digit to before the point, and if there 2674193323Sed is something after the decimal point add it. This must come 2675193323Sed after rounding above. */ 2676193323Sed p[-1] = p[0]; 2677193323Sed if (dst -1 == p) 2678193323Sed dst--; 2679193323Sed else 2680193323Sed p[0] = '.'; 2681193323Sed 2682193323Sed /* Finally output the exponent. */ 2683193323Sed *dst++ = upperCase ? 'P': 'p'; 2684193323Sed 2685193323Sed return writeSignedDecimal (dst, exponent); 2686193323Sed} 2687193323Sed 2688234353Sdimhash_code llvm::hash_value(const APFloat &Arg) { 2689234353Sdim if (Arg.category != APFloat::fcNormal) 2690234353Sdim return hash_combine((uint8_t)Arg.category, 2691234353Sdim // NaN has no sign, fix it at zero. 2692234353Sdim Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign, 2693234353Sdim Arg.semantics->precision); 2694234353Sdim 2695234353Sdim // Normal floats need their exponent and significand hashed. 2696234353Sdim return hash_combine((uint8_t)Arg.category, (uint8_t)Arg.sign, 2697234353Sdim Arg.semantics->precision, Arg.exponent, 2698234353Sdim hash_combine_range( 2699234353Sdim Arg.significandParts(), 2700234353Sdim Arg.significandParts() + Arg.partCount())); 2701193323Sed} 2702193323Sed 2703193323Sed// Conversion from APFloat to/from host float/double. It may eventually be 2704193323Sed// possible to eliminate these and have everybody deal with APFloats, but that 2705193323Sed// will take a while. This approach will not easily extend to long double. 2706193323Sed// Current implementation requires integerPartWidth==64, which is correct at 2707193323Sed// the moment but could be made more general. 2708193323Sed 2709193323Sed// Denormals have exponent minExponent in APFloat, but minExponent-1 in 2710193323Sed// the actual IEEE respresentations. We compensate for that here. 2711193323Sed 2712193323SedAPInt 2713193323SedAPFloat::convertF80LongDoubleAPFloatToAPInt() const 2714193323Sed{ 2715193323Sed assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended); 2716198892Srdivacky assert(partCount()==2); 2717193323Sed 2718193323Sed uint64_t myexponent, mysignificand; 2719193323Sed 2720193323Sed if (category==fcNormal) { 2721193323Sed myexponent = exponent+16383; //bias 2722193323Sed mysignificand = significandParts()[0]; 2723193323Sed if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL)) 2724193323Sed myexponent = 0; // denormal 2725193323Sed } else if (category==fcZero) { 2726193323Sed myexponent = 0; 2727193323Sed mysignificand = 0; 2728193323Sed } else if (category==fcInfinity) { 2729193323Sed myexponent = 0x7fff; 2730193323Sed mysignificand = 0x8000000000000000ULL; 2731193323Sed } else { 2732193323Sed assert(category == fcNaN && "Unknown category"); 2733193323Sed myexponent = 0x7fff; 2734193323Sed mysignificand = significandParts()[0]; 2735193323Sed } 2736193323Sed 2737193323Sed uint64_t words[2]; 2738193323Sed words[0] = mysignificand; 2739193323Sed words[1] = ((uint64_t)(sign & 1) << 15) | 2740193323Sed (myexponent & 0x7fffLL); 2741226633Sdim return APInt(80, words); 2742193323Sed} 2743193323Sed 2744193323SedAPInt 2745193323SedAPFloat::convertPPCDoubleDoubleAPFloatToAPInt() const 2746193323Sed{ 2747193323Sed assert(semantics == (const llvm::fltSemantics*)&PPCDoubleDouble); 2748198892Srdivacky assert(partCount()==2); 2749193323Sed 2750243830Sdim uint64_t words[2]; 2751243830Sdim opStatus fs; 2752243830Sdim bool losesInfo; 2753193323Sed 2754243830Sdim // Convert number to double. To avoid spurious underflows, we re- 2755243830Sdim // normalize against the "double" minExponent first, and only *then* 2756243830Sdim // truncate the mantissa. The result of that second conversion 2757243830Sdim // may be inexact, but should never underflow. 2758249423Sdim // Declare fltSemantics before APFloat that uses it (and 2759249423Sdim // saves pointer to it) to ensure correct destruction order. 2760243830Sdim fltSemantics extendedSemantics = *semantics; 2761243830Sdim extendedSemantics.minExponent = IEEEdouble.minExponent; 2762249423Sdim APFloat extended(*this); 2763243830Sdim fs = extended.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 2764243830Sdim assert(fs == opOK && !losesInfo); 2765243830Sdim (void)fs; 2766243830Sdim 2767243830Sdim APFloat u(extended); 2768243830Sdim fs = u.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo); 2769243830Sdim assert(fs == opOK || fs == opInexact); 2770243830Sdim (void)fs; 2771243830Sdim words[0] = *u.convertDoubleAPFloatToAPInt().getRawData(); 2772243830Sdim 2773243830Sdim // If conversion was exact or resulted in a special case, we're done; 2774243830Sdim // just set the second double to zero. Otherwise, re-convert back to 2775243830Sdim // the extended format and compute the difference. This now should 2776243830Sdim // convert exactly to double. 2777243830Sdim if (u.category == fcNormal && losesInfo) { 2778243830Sdim fs = u.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); 2779243830Sdim assert(fs == opOK && !losesInfo); 2780243830Sdim (void)fs; 2781243830Sdim 2782243830Sdim APFloat v(extended); 2783243830Sdim v.subtract(u, rmNearestTiesToEven); 2784243830Sdim fs = v.convert(IEEEdouble, rmNearestTiesToEven, &losesInfo); 2785243830Sdim assert(fs == opOK && !losesInfo); 2786243830Sdim (void)fs; 2787243830Sdim words[1] = *v.convertDoubleAPFloatToAPInt().getRawData(); 2788193323Sed } else { 2789243830Sdim words[1] = 0; 2790193323Sed } 2791193323Sed 2792226633Sdim return APInt(128, words); 2793193323Sed} 2794193323Sed 2795193323SedAPInt 2796198090SrdivackyAPFloat::convertQuadrupleAPFloatToAPInt() const 2797198090Srdivacky{ 2798198090Srdivacky assert(semantics == (const llvm::fltSemantics*)&IEEEquad); 2799198892Srdivacky assert(partCount()==2); 2800198090Srdivacky 2801198090Srdivacky uint64_t myexponent, mysignificand, mysignificand2; 2802198090Srdivacky 2803198090Srdivacky if (category==fcNormal) { 2804198090Srdivacky myexponent = exponent+16383; //bias 2805198090Srdivacky mysignificand = significandParts()[0]; 2806198090Srdivacky mysignificand2 = significandParts()[1]; 2807198090Srdivacky if (myexponent==1 && !(mysignificand2 & 0x1000000000000LL)) 2808198090Srdivacky myexponent = 0; // denormal 2809198090Srdivacky } else if (category==fcZero) { 2810198090Srdivacky myexponent = 0; 2811198090Srdivacky mysignificand = mysignificand2 = 0; 2812198090Srdivacky } else if (category==fcInfinity) { 2813198090Srdivacky myexponent = 0x7fff; 2814198090Srdivacky mysignificand = mysignificand2 = 0; 2815198090Srdivacky } else { 2816198090Srdivacky assert(category == fcNaN && "Unknown category!"); 2817198090Srdivacky myexponent = 0x7fff; 2818198090Srdivacky mysignificand = significandParts()[0]; 2819198090Srdivacky mysignificand2 = significandParts()[1]; 2820198090Srdivacky } 2821198090Srdivacky 2822198090Srdivacky uint64_t words[2]; 2823198090Srdivacky words[0] = mysignificand; 2824198090Srdivacky words[1] = ((uint64_t)(sign & 1) << 63) | 2825198090Srdivacky ((myexponent & 0x7fff) << 48) | 2826198090Srdivacky (mysignificand2 & 0xffffffffffffLL); 2827198090Srdivacky 2828226633Sdim return APInt(128, words); 2829198090Srdivacky} 2830198090Srdivacky 2831198090SrdivackyAPInt 2832193323SedAPFloat::convertDoubleAPFloatToAPInt() const 2833193323Sed{ 2834193323Sed assert(semantics == (const llvm::fltSemantics*)&IEEEdouble); 2835198892Srdivacky assert(partCount()==1); 2836193323Sed 2837193323Sed uint64_t myexponent, mysignificand; 2838193323Sed 2839193323Sed if (category==fcNormal) { 2840193323Sed myexponent = exponent+1023; //bias 2841193323Sed mysignificand = *significandParts(); 2842193323Sed if (myexponent==1 && !(mysignificand & 0x10000000000000LL)) 2843193323Sed myexponent = 0; // denormal 2844193323Sed } else if (category==fcZero) { 2845193323Sed myexponent = 0; 2846193323Sed mysignificand = 0; 2847193323Sed } else if (category==fcInfinity) { 2848193323Sed myexponent = 0x7ff; 2849193323Sed mysignificand = 0; 2850193323Sed } else { 2851193323Sed assert(category == fcNaN && "Unknown category!"); 2852193323Sed myexponent = 0x7ff; 2853193323Sed mysignificand = *significandParts(); 2854193323Sed } 2855193323Sed 2856193323Sed return APInt(64, ((((uint64_t)(sign & 1) << 63) | 2857193323Sed ((myexponent & 0x7ff) << 52) | 2858193323Sed (mysignificand & 0xfffffffffffffLL)))); 2859193323Sed} 2860193323Sed 2861193323SedAPInt 2862193323SedAPFloat::convertFloatAPFloatToAPInt() const 2863193323Sed{ 2864193323Sed assert(semantics == (const llvm::fltSemantics*)&IEEEsingle); 2865198892Srdivacky assert(partCount()==1); 2866193323Sed 2867193323Sed uint32_t myexponent, mysignificand; 2868193323Sed 2869193323Sed if (category==fcNormal) { 2870193323Sed myexponent = exponent+127; //bias 2871193323Sed mysignificand = (uint32_t)*significandParts(); 2872193323Sed if (myexponent == 1 && !(mysignificand & 0x800000)) 2873193323Sed myexponent = 0; // denormal 2874193323Sed } else if (category==fcZero) { 2875193323Sed myexponent = 0; 2876193323Sed mysignificand = 0; 2877193323Sed } else if (category==fcInfinity) { 2878193323Sed myexponent = 0xff; 2879193323Sed mysignificand = 0; 2880193323Sed } else { 2881193323Sed assert(category == fcNaN && "Unknown category!"); 2882193323Sed myexponent = 0xff; 2883193323Sed mysignificand = (uint32_t)*significandParts(); 2884193323Sed } 2885193323Sed 2886193323Sed return APInt(32, (((sign&1) << 31) | ((myexponent&0xff) << 23) | 2887193323Sed (mysignificand & 0x7fffff))); 2888193323Sed} 2889193323Sed 2890198396SrdivackyAPInt 2891198396SrdivackyAPFloat::convertHalfAPFloatToAPInt() const 2892198396Srdivacky{ 2893198396Srdivacky assert(semantics == (const llvm::fltSemantics*)&IEEEhalf); 2894198892Srdivacky assert(partCount()==1); 2895198396Srdivacky 2896198396Srdivacky uint32_t myexponent, mysignificand; 2897198396Srdivacky 2898198396Srdivacky if (category==fcNormal) { 2899198396Srdivacky myexponent = exponent+15; //bias 2900198396Srdivacky mysignificand = (uint32_t)*significandParts(); 2901198396Srdivacky if (myexponent == 1 && !(mysignificand & 0x400)) 2902198396Srdivacky myexponent = 0; // denormal 2903198396Srdivacky } else if (category==fcZero) { 2904198396Srdivacky myexponent = 0; 2905198396Srdivacky mysignificand = 0; 2906198396Srdivacky } else if (category==fcInfinity) { 2907198396Srdivacky myexponent = 0x1f; 2908198396Srdivacky mysignificand = 0; 2909198396Srdivacky } else { 2910198396Srdivacky assert(category == fcNaN && "Unknown category!"); 2911198396Srdivacky myexponent = 0x1f; 2912198396Srdivacky mysignificand = (uint32_t)*significandParts(); 2913198396Srdivacky } 2914198396Srdivacky 2915198396Srdivacky return APInt(16, (((sign&1) << 15) | ((myexponent&0x1f) << 10) | 2916198396Srdivacky (mysignificand & 0x3ff))); 2917198396Srdivacky} 2918198396Srdivacky 2919193323Sed// This function creates an APInt that is just a bit map of the floating 2920193323Sed// point constant as it would appear in memory. It is not a conversion, 2921193323Sed// and treating the result as a normal integer is unlikely to be useful. 2922193323Sed 2923193323SedAPInt 2924193323SedAPFloat::bitcastToAPInt() const 2925193323Sed{ 2926198396Srdivacky if (semantics == (const llvm::fltSemantics*)&IEEEhalf) 2927198396Srdivacky return convertHalfAPFloatToAPInt(); 2928198396Srdivacky 2929193323Sed if (semantics == (const llvm::fltSemantics*)&IEEEsingle) 2930193323Sed return convertFloatAPFloatToAPInt(); 2931198090Srdivacky 2932193323Sed if (semantics == (const llvm::fltSemantics*)&IEEEdouble) 2933193323Sed return convertDoubleAPFloatToAPInt(); 2934193323Sed 2935198090Srdivacky if (semantics == (const llvm::fltSemantics*)&IEEEquad) 2936198090Srdivacky return convertQuadrupleAPFloatToAPInt(); 2937198090Srdivacky 2938193323Sed if (semantics == (const llvm::fltSemantics*)&PPCDoubleDouble) 2939193323Sed return convertPPCDoubleDoubleAPFloatToAPInt(); 2940193323Sed 2941193323Sed assert(semantics == (const llvm::fltSemantics*)&x87DoubleExtended && 2942193323Sed "unknown format!"); 2943193323Sed return convertF80LongDoubleAPFloatToAPInt(); 2944193323Sed} 2945193323Sed 2946193323Sedfloat 2947193323SedAPFloat::convertToFloat() const 2948193323Sed{ 2949198090Srdivacky assert(semantics == (const llvm::fltSemantics*)&IEEEsingle && 2950198090Srdivacky "Float semantics are not IEEEsingle"); 2951193323Sed APInt api = bitcastToAPInt(); 2952193323Sed return api.bitsToFloat(); 2953193323Sed} 2954193323Sed 2955193323Seddouble 2956193323SedAPFloat::convertToDouble() const 2957193323Sed{ 2958198090Srdivacky assert(semantics == (const llvm::fltSemantics*)&IEEEdouble && 2959198090Srdivacky "Float semantics are not IEEEdouble"); 2960193323Sed APInt api = bitcastToAPInt(); 2961193323Sed return api.bitsToDouble(); 2962193323Sed} 2963193323Sed 2964193323Sed/// Integer bit is explicit in this format. Intel hardware (387 and later) 2965193323Sed/// does not support these bit patterns: 2966193323Sed/// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity") 2967193323Sed/// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN") 2968193323Sed/// exponent = 0, integer bit 1 ("pseudodenormal") 2969193323Sed/// exponent!=0 nor all 1's, integer bit 0 ("unnormal") 2970193323Sed/// At the moment, the first two are treated as NaNs, the second two as Normal. 2971193323Sedvoid 2972193323SedAPFloat::initFromF80LongDoubleAPInt(const APInt &api) 2973193323Sed{ 2974193323Sed assert(api.getBitWidth()==80); 2975193323Sed uint64_t i1 = api.getRawData()[0]; 2976193323Sed uint64_t i2 = api.getRawData()[1]; 2977193323Sed uint64_t myexponent = (i2 & 0x7fff); 2978193323Sed uint64_t mysignificand = i1; 2979193323Sed 2980193323Sed initialize(&APFloat::x87DoubleExtended); 2981193323Sed assert(partCount()==2); 2982193323Sed 2983193323Sed sign = static_cast<unsigned int>(i2>>15); 2984193323Sed if (myexponent==0 && mysignificand==0) { 2985193323Sed // exponent, significand meaningless 2986193323Sed category = fcZero; 2987193323Sed } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) { 2988193323Sed // exponent, significand meaningless 2989193323Sed category = fcInfinity; 2990193323Sed } else if (myexponent==0x7fff && mysignificand!=0x8000000000000000ULL) { 2991193323Sed // exponent meaningless 2992193323Sed category = fcNaN; 2993193323Sed significandParts()[0] = mysignificand; 2994193323Sed significandParts()[1] = 0; 2995193323Sed } else { 2996193323Sed category = fcNormal; 2997193323Sed exponent = myexponent - 16383; 2998193323Sed significandParts()[0] = mysignificand; 2999193323Sed significandParts()[1] = 0; 3000193323Sed if (myexponent==0) // denormal 3001193323Sed exponent = -16382; 3002193323Sed } 3003193323Sed} 3004193323Sed 3005193323Sedvoid 3006193323SedAPFloat::initFromPPCDoubleDoubleAPInt(const APInt &api) 3007193323Sed{ 3008193323Sed assert(api.getBitWidth()==128); 3009193323Sed uint64_t i1 = api.getRawData()[0]; 3010193323Sed uint64_t i2 = api.getRawData()[1]; 3011243830Sdim opStatus fs; 3012243830Sdim bool losesInfo; 3013193323Sed 3014243830Sdim // Get the first double and convert to our format. 3015243830Sdim initFromDoubleAPInt(APInt(64, i1)); 3016243830Sdim fs = convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo); 3017243830Sdim assert(fs == opOK && !losesInfo); 3018243830Sdim (void)fs; 3019193323Sed 3020243830Sdim // Unless we have a special case, add in second double. 3021243830Sdim if (category == fcNormal) { 3022249423Sdim APFloat v(IEEEdouble, APInt(64, i2)); 3023243830Sdim fs = v.convert(PPCDoubleDouble, rmNearestTiesToEven, &losesInfo); 3024243830Sdim assert(fs == opOK && !losesInfo); 3025243830Sdim (void)fs; 3026243830Sdim 3027243830Sdim add(v, rmNearestTiesToEven); 3028193323Sed } 3029193323Sed} 3030193323Sed 3031193323Sedvoid 3032198090SrdivackyAPFloat::initFromQuadrupleAPInt(const APInt &api) 3033198090Srdivacky{ 3034198090Srdivacky assert(api.getBitWidth()==128); 3035198090Srdivacky uint64_t i1 = api.getRawData()[0]; 3036198090Srdivacky uint64_t i2 = api.getRawData()[1]; 3037198090Srdivacky uint64_t myexponent = (i2 >> 48) & 0x7fff; 3038198090Srdivacky uint64_t mysignificand = i1; 3039198090Srdivacky uint64_t mysignificand2 = i2 & 0xffffffffffffLL; 3040198090Srdivacky 3041198090Srdivacky initialize(&APFloat::IEEEquad); 3042198090Srdivacky assert(partCount()==2); 3043198090Srdivacky 3044198090Srdivacky sign = static_cast<unsigned int>(i2>>63); 3045198090Srdivacky if (myexponent==0 && 3046198090Srdivacky (mysignificand==0 && mysignificand2==0)) { 3047198090Srdivacky // exponent, significand meaningless 3048198090Srdivacky category = fcZero; 3049198090Srdivacky } else if (myexponent==0x7fff && 3050198090Srdivacky (mysignificand==0 && mysignificand2==0)) { 3051198090Srdivacky // exponent, significand meaningless 3052198090Srdivacky category = fcInfinity; 3053198090Srdivacky } else if (myexponent==0x7fff && 3054198090Srdivacky (mysignificand!=0 || mysignificand2 !=0)) { 3055198090Srdivacky // exponent meaningless 3056198090Srdivacky category = fcNaN; 3057198090Srdivacky significandParts()[0] = mysignificand; 3058198090Srdivacky significandParts()[1] = mysignificand2; 3059198090Srdivacky } else { 3060198090Srdivacky category = fcNormal; 3061198090Srdivacky exponent = myexponent - 16383; 3062198090Srdivacky significandParts()[0] = mysignificand; 3063198090Srdivacky significandParts()[1] = mysignificand2; 3064198090Srdivacky if (myexponent==0) // denormal 3065198090Srdivacky exponent = -16382; 3066198090Srdivacky else 3067198090Srdivacky significandParts()[1] |= 0x1000000000000LL; // integer bit 3068198090Srdivacky } 3069198090Srdivacky} 3070198090Srdivacky 3071198090Srdivackyvoid 3072193323SedAPFloat::initFromDoubleAPInt(const APInt &api) 3073193323Sed{ 3074193323Sed assert(api.getBitWidth()==64); 3075193323Sed uint64_t i = *api.getRawData(); 3076193323Sed uint64_t myexponent = (i >> 52) & 0x7ff; 3077193323Sed uint64_t mysignificand = i & 0xfffffffffffffLL; 3078193323Sed 3079193323Sed initialize(&APFloat::IEEEdouble); 3080193323Sed assert(partCount()==1); 3081193323Sed 3082193323Sed sign = static_cast<unsigned int>(i>>63); 3083193323Sed if (myexponent==0 && mysignificand==0) { 3084193323Sed // exponent, significand meaningless 3085193323Sed category = fcZero; 3086193323Sed } else if (myexponent==0x7ff && mysignificand==0) { 3087193323Sed // exponent, significand meaningless 3088193323Sed category = fcInfinity; 3089193323Sed } else if (myexponent==0x7ff && mysignificand!=0) { 3090193323Sed // exponent meaningless 3091193323Sed category = fcNaN; 3092193323Sed *significandParts() = mysignificand; 3093193323Sed } else { 3094193323Sed category = fcNormal; 3095193323Sed exponent = myexponent - 1023; 3096193323Sed *significandParts() = mysignificand; 3097193323Sed if (myexponent==0) // denormal 3098193323Sed exponent = -1022; 3099193323Sed else 3100193323Sed *significandParts() |= 0x10000000000000LL; // integer bit 3101193323Sed } 3102193323Sed} 3103193323Sed 3104193323Sedvoid 3105193323SedAPFloat::initFromFloatAPInt(const APInt & api) 3106193323Sed{ 3107193323Sed assert(api.getBitWidth()==32); 3108193323Sed uint32_t i = (uint32_t)*api.getRawData(); 3109193323Sed uint32_t myexponent = (i >> 23) & 0xff; 3110193323Sed uint32_t mysignificand = i & 0x7fffff; 3111193323Sed 3112193323Sed initialize(&APFloat::IEEEsingle); 3113193323Sed assert(partCount()==1); 3114193323Sed 3115193323Sed sign = i >> 31; 3116193323Sed if (myexponent==0 && mysignificand==0) { 3117193323Sed // exponent, significand meaningless 3118193323Sed category = fcZero; 3119193323Sed } else if (myexponent==0xff && mysignificand==0) { 3120193323Sed // exponent, significand meaningless 3121193323Sed category = fcInfinity; 3122193323Sed } else if (myexponent==0xff && mysignificand!=0) { 3123193323Sed // sign, exponent, significand meaningless 3124193323Sed category = fcNaN; 3125193323Sed *significandParts() = mysignificand; 3126193323Sed } else { 3127193323Sed category = fcNormal; 3128193323Sed exponent = myexponent - 127; //bias 3129193323Sed *significandParts() = mysignificand; 3130193323Sed if (myexponent==0) // denormal 3131193323Sed exponent = -126; 3132193323Sed else 3133193323Sed *significandParts() |= 0x800000; // integer bit 3134193323Sed } 3135193323Sed} 3136193323Sed 3137198396Srdivackyvoid 3138198396SrdivackyAPFloat::initFromHalfAPInt(const APInt & api) 3139198396Srdivacky{ 3140198396Srdivacky assert(api.getBitWidth()==16); 3141198396Srdivacky uint32_t i = (uint32_t)*api.getRawData(); 3142198396Srdivacky uint32_t myexponent = (i >> 10) & 0x1f; 3143198396Srdivacky uint32_t mysignificand = i & 0x3ff; 3144198396Srdivacky 3145198396Srdivacky initialize(&APFloat::IEEEhalf); 3146198396Srdivacky assert(partCount()==1); 3147198396Srdivacky 3148198396Srdivacky sign = i >> 15; 3149198396Srdivacky if (myexponent==0 && mysignificand==0) { 3150198396Srdivacky // exponent, significand meaningless 3151198396Srdivacky category = fcZero; 3152198396Srdivacky } else if (myexponent==0x1f && mysignificand==0) { 3153198396Srdivacky // exponent, significand meaningless 3154198396Srdivacky category = fcInfinity; 3155198396Srdivacky } else if (myexponent==0x1f && mysignificand!=0) { 3156198396Srdivacky // sign, exponent, significand meaningless 3157198396Srdivacky category = fcNaN; 3158198396Srdivacky *significandParts() = mysignificand; 3159198396Srdivacky } else { 3160198396Srdivacky category = fcNormal; 3161198396Srdivacky exponent = myexponent - 15; //bias 3162198396Srdivacky *significandParts() = mysignificand; 3163198396Srdivacky if (myexponent==0) // denormal 3164198396Srdivacky exponent = -14; 3165198396Srdivacky else 3166198396Srdivacky *significandParts() |= 0x400; // integer bit 3167198396Srdivacky } 3168198396Srdivacky} 3169198396Srdivacky 3170193323Sed/// Treat api as containing the bits of a floating point number. Currently 3171193323Sed/// we infer the floating point type from the size of the APInt. The 3172193323Sed/// isIEEE argument distinguishes between PPC128 and IEEE128 (not meaningful 3173193323Sed/// when the size is anything else). 3174193323Sedvoid 3175249423SdimAPFloat::initFromAPInt(const fltSemantics* Sem, const APInt& api) 3176193323Sed{ 3177249423Sdim if (Sem == &IEEEhalf) 3178198396Srdivacky return initFromHalfAPInt(api); 3179249423Sdim if (Sem == &IEEEsingle) 3180193323Sed return initFromFloatAPInt(api); 3181249423Sdim if (Sem == &IEEEdouble) 3182193323Sed return initFromDoubleAPInt(api); 3183249423Sdim if (Sem == &x87DoubleExtended) 3184193323Sed return initFromF80LongDoubleAPInt(api); 3185249423Sdim if (Sem == &IEEEquad) 3186249423Sdim return initFromQuadrupleAPInt(api); 3187249423Sdim if (Sem == &PPCDoubleDouble) 3188249423Sdim return initFromPPCDoubleDoubleAPInt(api); 3189249423Sdim 3190249423Sdim llvm_unreachable(0); 3191193323Sed} 3192193323Sed 3193218893SdimAPFloat 3194218893SdimAPFloat::getAllOnesValue(unsigned BitWidth, bool isIEEE) 3195218893Sdim{ 3196249423Sdim switch (BitWidth) { 3197249423Sdim case 16: 3198249423Sdim return APFloat(IEEEhalf, APInt::getAllOnesValue(BitWidth)); 3199249423Sdim case 32: 3200249423Sdim return APFloat(IEEEsingle, APInt::getAllOnesValue(BitWidth)); 3201249423Sdim case 64: 3202249423Sdim return APFloat(IEEEdouble, APInt::getAllOnesValue(BitWidth)); 3203249423Sdim case 80: 3204249423Sdim return APFloat(x87DoubleExtended, APInt::getAllOnesValue(BitWidth)); 3205249423Sdim case 128: 3206249423Sdim if (isIEEE) 3207249423Sdim return APFloat(IEEEquad, APInt::getAllOnesValue(BitWidth)); 3208249423Sdim return APFloat(PPCDoubleDouble, APInt::getAllOnesValue(BitWidth)); 3209249423Sdim default: 3210249423Sdim llvm_unreachable("Unknown floating bit width"); 3211249423Sdim } 3212218893Sdim} 3213218893Sdim 3214201360SrdivackyAPFloat APFloat::getLargest(const fltSemantics &Sem, bool Negative) { 3215201360Srdivacky APFloat Val(Sem, fcNormal, Negative); 3216201360Srdivacky 3217201360Srdivacky // We want (in interchange format): 3218201360Srdivacky // sign = {Negative} 3219201360Srdivacky // exponent = 1..10 3220201360Srdivacky // significand = 1..1 3221201360Srdivacky 3222201360Srdivacky Val.exponent = Sem.maxExponent; // unbiased 3223201360Srdivacky 3224201360Srdivacky // 1-initialize all bits.... 3225201360Srdivacky Val.zeroSignificand(); 3226201360Srdivacky integerPart *significand = Val.significandParts(); 3227201360Srdivacky unsigned N = partCountForBits(Sem.precision); 3228201360Srdivacky for (unsigned i = 0; i != N; ++i) 3229201360Srdivacky significand[i] = ~((integerPart) 0); 3230201360Srdivacky 3231201360Srdivacky // ...and then clear the top bits for internal consistency. 3232226633Sdim if (Sem.precision % integerPartWidth != 0) 3233226633Sdim significand[N-1] &= 3234226633Sdim (((integerPart) 1) << (Sem.precision % integerPartWidth)) - 1; 3235201360Srdivacky 3236201360Srdivacky return Val; 3237201360Srdivacky} 3238201360Srdivacky 3239201360SrdivackyAPFloat APFloat::getSmallest(const fltSemantics &Sem, bool Negative) { 3240201360Srdivacky APFloat Val(Sem, fcNormal, Negative); 3241201360Srdivacky 3242201360Srdivacky // We want (in interchange format): 3243201360Srdivacky // sign = {Negative} 3244201360Srdivacky // exponent = 0..0 3245201360Srdivacky // significand = 0..01 3246201360Srdivacky 3247201360Srdivacky Val.exponent = Sem.minExponent; // unbiased 3248201360Srdivacky Val.zeroSignificand(); 3249201360Srdivacky Val.significandParts()[0] = 1; 3250201360Srdivacky return Val; 3251201360Srdivacky} 3252201360Srdivacky 3253201360SrdivackyAPFloat APFloat::getSmallestNormalized(const fltSemantics &Sem, bool Negative) { 3254201360Srdivacky APFloat Val(Sem, fcNormal, Negative); 3255201360Srdivacky 3256201360Srdivacky // We want (in interchange format): 3257201360Srdivacky // sign = {Negative} 3258201360Srdivacky // exponent = 0..0 3259201360Srdivacky // significand = 10..0 3260201360Srdivacky 3261201360Srdivacky Val.exponent = Sem.minExponent; 3262201360Srdivacky Val.zeroSignificand(); 3263206083Srdivacky Val.significandParts()[partCountForBits(Sem.precision)-1] |= 3264226633Sdim (((integerPart) 1) << ((Sem.precision - 1) % integerPartWidth)); 3265201360Srdivacky 3266201360Srdivacky return Val; 3267201360Srdivacky} 3268201360Srdivacky 3269249423SdimAPFloat::APFloat(const fltSemantics &Sem, const APInt &API) { 3270249423Sdim initFromAPInt(&Sem, API); 3271193323Sed} 3272193323Sed 3273243830SdimAPFloat::APFloat(float f) { 3274249423Sdim initFromAPInt(&IEEEsingle, APInt::floatToBits(f)); 3275193323Sed} 3276193323Sed 3277243830SdimAPFloat::APFloat(double d) { 3278249423Sdim initFromAPInt(&IEEEdouble, APInt::doubleToBits(d)); 3279193323Sed} 3280201360Srdivacky 3281201360Srdivackynamespace { 3282239462Sdim void append(SmallVectorImpl<char> &Buffer, StringRef Str) { 3283239462Sdim Buffer.append(Str.begin(), Str.end()); 3284201360Srdivacky } 3285201360Srdivacky 3286201360Srdivacky /// Removes data from the given significand until it is no more 3287201360Srdivacky /// precise than is required for the desired precision. 3288201360Srdivacky void AdjustToPrecision(APInt &significand, 3289201360Srdivacky int &exp, unsigned FormatPrecision) { 3290201360Srdivacky unsigned bits = significand.getActiveBits(); 3291201360Srdivacky 3292201360Srdivacky // 196/59 is a very slight overestimate of lg_2(10). 3293201360Srdivacky unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59; 3294201360Srdivacky 3295201360Srdivacky if (bits <= bitsRequired) return; 3296201360Srdivacky 3297201360Srdivacky unsigned tensRemovable = (bits - bitsRequired) * 59 / 196; 3298201360Srdivacky if (!tensRemovable) return; 3299201360Srdivacky 3300201360Srdivacky exp += tensRemovable; 3301201360Srdivacky 3302201360Srdivacky APInt divisor(significand.getBitWidth(), 1); 3303201360Srdivacky APInt powten(significand.getBitWidth(), 10); 3304201360Srdivacky while (true) { 3305201360Srdivacky if (tensRemovable & 1) 3306201360Srdivacky divisor *= powten; 3307201360Srdivacky tensRemovable >>= 1; 3308201360Srdivacky if (!tensRemovable) break; 3309201360Srdivacky powten *= powten; 3310201360Srdivacky } 3311201360Srdivacky 3312201360Srdivacky significand = significand.udiv(divisor); 3313201360Srdivacky 3314249423Sdim // Truncate the significand down to its active bit count. 3315249423Sdim significand = significand.trunc(significand.getActiveBits()); 3316201360Srdivacky } 3317201360Srdivacky 3318201360Srdivacky 3319201360Srdivacky void AdjustToPrecision(SmallVectorImpl<char> &buffer, 3320201360Srdivacky int &exp, unsigned FormatPrecision) { 3321201360Srdivacky unsigned N = buffer.size(); 3322201360Srdivacky if (N <= FormatPrecision) return; 3323201360Srdivacky 3324201360Srdivacky // The most significant figures are the last ones in the buffer. 3325201360Srdivacky unsigned FirstSignificant = N - FormatPrecision; 3326201360Srdivacky 3327201360Srdivacky // Round. 3328201360Srdivacky // FIXME: this probably shouldn't use 'round half up'. 3329201360Srdivacky 3330201360Srdivacky // Rounding down is just a truncation, except we also want to drop 3331201360Srdivacky // trailing zeros from the new result. 3332201360Srdivacky if (buffer[FirstSignificant - 1] < '5') { 3333234353Sdim while (FirstSignificant < N && buffer[FirstSignificant] == '0') 3334201360Srdivacky FirstSignificant++; 3335201360Srdivacky 3336201360Srdivacky exp += FirstSignificant; 3337201360Srdivacky buffer.erase(&buffer[0], &buffer[FirstSignificant]); 3338201360Srdivacky return; 3339201360Srdivacky } 3340201360Srdivacky 3341201360Srdivacky // Rounding up requires a decimal add-with-carry. If we continue 3342201360Srdivacky // the carry, the newly-introduced zeros will just be truncated. 3343201360Srdivacky for (unsigned I = FirstSignificant; I != N; ++I) { 3344201360Srdivacky if (buffer[I] == '9') { 3345201360Srdivacky FirstSignificant++; 3346201360Srdivacky } else { 3347201360Srdivacky buffer[I]++; 3348201360Srdivacky break; 3349201360Srdivacky } 3350201360Srdivacky } 3351201360Srdivacky 3352201360Srdivacky // If we carried through, we have exactly one digit of precision. 3353201360Srdivacky if (FirstSignificant == N) { 3354201360Srdivacky exp += FirstSignificant; 3355201360Srdivacky buffer.clear(); 3356201360Srdivacky buffer.push_back('1'); 3357201360Srdivacky return; 3358201360Srdivacky } 3359201360Srdivacky 3360201360Srdivacky exp += FirstSignificant; 3361201360Srdivacky buffer.erase(&buffer[0], &buffer[FirstSignificant]); 3362201360Srdivacky } 3363201360Srdivacky} 3364201360Srdivacky 3365201360Srdivackyvoid APFloat::toString(SmallVectorImpl<char> &Str, 3366201360Srdivacky unsigned FormatPrecision, 3367204961Srdivacky unsigned FormatMaxPadding) const { 3368201360Srdivacky switch (category) { 3369201360Srdivacky case fcInfinity: 3370201360Srdivacky if (isNegative()) 3371201360Srdivacky return append(Str, "-Inf"); 3372201360Srdivacky else 3373201360Srdivacky return append(Str, "+Inf"); 3374201360Srdivacky 3375201360Srdivacky case fcNaN: return append(Str, "NaN"); 3376201360Srdivacky 3377201360Srdivacky case fcZero: 3378201360Srdivacky if (isNegative()) 3379201360Srdivacky Str.push_back('-'); 3380201360Srdivacky 3381201360Srdivacky if (!FormatMaxPadding) 3382201360Srdivacky append(Str, "0.0E+0"); 3383201360Srdivacky else 3384201360Srdivacky Str.push_back('0'); 3385201360Srdivacky return; 3386201360Srdivacky 3387201360Srdivacky case fcNormal: 3388201360Srdivacky break; 3389201360Srdivacky } 3390201360Srdivacky 3391201360Srdivacky if (isNegative()) 3392201360Srdivacky Str.push_back('-'); 3393201360Srdivacky 3394201360Srdivacky // Decompose the number into an APInt and an exponent. 3395201360Srdivacky int exp = exponent - ((int) semantics->precision - 1); 3396201360Srdivacky APInt significand(semantics->precision, 3397226633Sdim makeArrayRef(significandParts(), 3398226633Sdim partCountForBits(semantics->precision))); 3399201360Srdivacky 3400201360Srdivacky // Set FormatPrecision if zero. We want to do this before we 3401201360Srdivacky // truncate trailing zeros, as those are part of the precision. 3402201360Srdivacky if (!FormatPrecision) { 3403201360Srdivacky // It's an interesting question whether to use the nominal 3404201360Srdivacky // precision or the active precision here for denormals. 3405201360Srdivacky 3406201360Srdivacky // FormatPrecision = ceil(significandBits / lg_2(10)) 3407201360Srdivacky FormatPrecision = (semantics->precision * 59 + 195) / 196; 3408201360Srdivacky } 3409201360Srdivacky 3410201360Srdivacky // Ignore trailing binary zeros. 3411201360Srdivacky int trailingZeros = significand.countTrailingZeros(); 3412201360Srdivacky exp += trailingZeros; 3413201360Srdivacky significand = significand.lshr(trailingZeros); 3414201360Srdivacky 3415201360Srdivacky // Change the exponent from 2^e to 10^e. 3416201360Srdivacky if (exp == 0) { 3417201360Srdivacky // Nothing to do. 3418201360Srdivacky } else if (exp > 0) { 3419201360Srdivacky // Just shift left. 3420218893Sdim significand = significand.zext(semantics->precision + exp); 3421201360Srdivacky significand <<= exp; 3422201360Srdivacky exp = 0; 3423201360Srdivacky } else { /* exp < 0 */ 3424201360Srdivacky int texp = -exp; 3425201360Srdivacky 3426201360Srdivacky // We transform this using the identity: 3427201360Srdivacky // (N)(2^-e) == (N)(5^e)(10^-e) 3428201360Srdivacky // This means we have to multiply N (the significand) by 5^e. 3429201360Srdivacky // To avoid overflow, we have to operate on numbers large 3430201360Srdivacky // enough to store N * 5^e: 3431201360Srdivacky // log2(N * 5^e) == log2(N) + e * log2(5) 3432201360Srdivacky // <= semantics->precision + e * 137 / 59 3433201360Srdivacky // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59) 3434206083Srdivacky 3435226633Sdim unsigned precision = semantics->precision + (137 * texp + 136) / 59; 3436201360Srdivacky 3437201360Srdivacky // Multiply significand by 5^e. 3438201360Srdivacky // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8) 3439218893Sdim significand = significand.zext(precision); 3440201360Srdivacky APInt five_to_the_i(precision, 5); 3441201360Srdivacky while (true) { 3442201360Srdivacky if (texp & 1) significand *= five_to_the_i; 3443206083Srdivacky 3444201360Srdivacky texp >>= 1; 3445201360Srdivacky if (!texp) break; 3446201360Srdivacky five_to_the_i *= five_to_the_i; 3447201360Srdivacky } 3448201360Srdivacky } 3449201360Srdivacky 3450201360Srdivacky AdjustToPrecision(significand, exp, FormatPrecision); 3451201360Srdivacky 3452249423Sdim SmallVector<char, 256> buffer; 3453201360Srdivacky 3454201360Srdivacky // Fill the buffer. 3455201360Srdivacky unsigned precision = significand.getBitWidth(); 3456201360Srdivacky APInt ten(precision, 10); 3457201360Srdivacky APInt digit(precision, 0); 3458201360Srdivacky 3459201360Srdivacky bool inTrail = true; 3460201360Srdivacky while (significand != 0) { 3461201360Srdivacky // digit <- significand % 10 3462201360Srdivacky // significand <- significand / 10 3463201360Srdivacky APInt::udivrem(significand, ten, significand, digit); 3464201360Srdivacky 3465201360Srdivacky unsigned d = digit.getZExtValue(); 3466201360Srdivacky 3467201360Srdivacky // Drop trailing zeros. 3468201360Srdivacky if (inTrail && !d) exp++; 3469201360Srdivacky else { 3470201360Srdivacky buffer.push_back((char) ('0' + d)); 3471201360Srdivacky inTrail = false; 3472201360Srdivacky } 3473201360Srdivacky } 3474201360Srdivacky 3475201360Srdivacky assert(!buffer.empty() && "no characters in buffer!"); 3476201360Srdivacky 3477201360Srdivacky // Drop down to FormatPrecision. 3478201360Srdivacky // TODO: don't do more precise calculations above than are required. 3479201360Srdivacky AdjustToPrecision(buffer, exp, FormatPrecision); 3480201360Srdivacky 3481201360Srdivacky unsigned NDigits = buffer.size(); 3482201360Srdivacky 3483201360Srdivacky // Check whether we should use scientific notation. 3484201360Srdivacky bool FormatScientific; 3485201360Srdivacky if (!FormatMaxPadding) 3486201360Srdivacky FormatScientific = true; 3487201360Srdivacky else { 3488201360Srdivacky if (exp >= 0) { 3489201360Srdivacky // 765e3 --> 765000 3490201360Srdivacky // ^^^ 3491201360Srdivacky // But we shouldn't make the number look more precise than it is. 3492201360Srdivacky FormatScientific = ((unsigned) exp > FormatMaxPadding || 3493201360Srdivacky NDigits + (unsigned) exp > FormatPrecision); 3494201360Srdivacky } else { 3495201360Srdivacky // Power of the most significant digit. 3496201360Srdivacky int MSD = exp + (int) (NDigits - 1); 3497201360Srdivacky if (MSD >= 0) { 3498201360Srdivacky // 765e-2 == 7.65 3499201360Srdivacky FormatScientific = false; 3500201360Srdivacky } else { 3501201360Srdivacky // 765e-5 == 0.00765 3502201360Srdivacky // ^ ^^ 3503201360Srdivacky FormatScientific = ((unsigned) -MSD) > FormatMaxPadding; 3504201360Srdivacky } 3505201360Srdivacky } 3506201360Srdivacky } 3507201360Srdivacky 3508201360Srdivacky // Scientific formatting is pretty straightforward. 3509201360Srdivacky if (FormatScientific) { 3510201360Srdivacky exp += (NDigits - 1); 3511201360Srdivacky 3512201360Srdivacky Str.push_back(buffer[NDigits-1]); 3513201360Srdivacky Str.push_back('.'); 3514201360Srdivacky if (NDigits == 1) 3515201360Srdivacky Str.push_back('0'); 3516201360Srdivacky else 3517201360Srdivacky for (unsigned I = 1; I != NDigits; ++I) 3518201360Srdivacky Str.push_back(buffer[NDigits-1-I]); 3519201360Srdivacky Str.push_back('E'); 3520201360Srdivacky 3521201360Srdivacky Str.push_back(exp >= 0 ? '+' : '-'); 3522201360Srdivacky if (exp < 0) exp = -exp; 3523201360Srdivacky SmallVector<char, 6> expbuf; 3524201360Srdivacky do { 3525201360Srdivacky expbuf.push_back((char) ('0' + (exp % 10))); 3526201360Srdivacky exp /= 10; 3527201360Srdivacky } while (exp); 3528201360Srdivacky for (unsigned I = 0, E = expbuf.size(); I != E; ++I) 3529201360Srdivacky Str.push_back(expbuf[E-1-I]); 3530201360Srdivacky return; 3531201360Srdivacky } 3532201360Srdivacky 3533201360Srdivacky // Non-scientific, positive exponents. 3534201360Srdivacky if (exp >= 0) { 3535201360Srdivacky for (unsigned I = 0; I != NDigits; ++I) 3536201360Srdivacky Str.push_back(buffer[NDigits-1-I]); 3537201360Srdivacky for (unsigned I = 0; I != (unsigned) exp; ++I) 3538201360Srdivacky Str.push_back('0'); 3539201360Srdivacky return; 3540201360Srdivacky } 3541201360Srdivacky 3542201360Srdivacky // Non-scientific, negative exponents. 3543201360Srdivacky 3544201360Srdivacky // The number of digits to the left of the decimal point. 3545201360Srdivacky int NWholeDigits = exp + (int) NDigits; 3546201360Srdivacky 3547201360Srdivacky unsigned I = 0; 3548201360Srdivacky if (NWholeDigits > 0) { 3549201360Srdivacky for (; I != (unsigned) NWholeDigits; ++I) 3550201360Srdivacky Str.push_back(buffer[NDigits-I-1]); 3551201360Srdivacky Str.push_back('.'); 3552201360Srdivacky } else { 3553201360Srdivacky unsigned NZeros = 1 + (unsigned) -NWholeDigits; 3554201360Srdivacky 3555201360Srdivacky Str.push_back('0'); 3556201360Srdivacky Str.push_back('.'); 3557201360Srdivacky for (unsigned Z = 1; Z != NZeros; ++Z) 3558201360Srdivacky Str.push_back('0'); 3559201360Srdivacky } 3560201360Srdivacky 3561201360Srdivacky for (; I != NDigits; ++I) 3562201360Srdivacky Str.push_back(buffer[NDigits-I-1]); 3563201360Srdivacky} 3564221345Sdim 3565221345Sdimbool APFloat::getExactInverse(APFloat *inv) const { 3566221345Sdim // Special floats and denormals have no exact inverse. 3567221345Sdim if (category != fcNormal) 3568221345Sdim return false; 3569221345Sdim 3570221345Sdim // Check that the number is a power of two by making sure that only the 3571221345Sdim // integer bit is set in the significand. 3572221345Sdim if (significandLSB() != semantics->precision - 1) 3573221345Sdim return false; 3574221345Sdim 3575221345Sdim // Get the inverse. 3576221345Sdim APFloat reciprocal(*semantics, 1ULL); 3577221345Sdim if (reciprocal.divide(*this, rmNearestTiesToEven) != opOK) 3578221345Sdim return false; 3579221345Sdim 3580221345Sdim // Avoid multiplication with a denormal, it is not safe on all platforms and 3581221345Sdim // may be slower than a normal division. 3582221345Sdim if (reciprocal.significandMSB() + 1 < reciprocal.semantics->precision) 3583221345Sdim return false; 3584221345Sdim 3585221345Sdim assert(reciprocal.category == fcNormal && 3586221345Sdim reciprocal.significandLSB() == reciprocal.semantics->precision - 1); 3587221345Sdim 3588221345Sdim if (inv) 3589221345Sdim *inv = reciprocal; 3590221345Sdim 3591221345Sdim return true; 3592221345Sdim} 3593