| 1 | //===-- APFloat.cpp - Implement APFloat class -----------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements a class to represent arbitrary precision floating |
| 10 | // point values and provide a variety of arithmetic operations on them. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/APFloat.h" |
| 15 | #include "llvm/ADT/APSInt.h" |
| 16 | #include "llvm/ADT/ArrayRef.h" |
| 17 | #include "llvm/ADT/FloatingPointMode.h" |
| 18 | #include "llvm/ADT/FoldingSet.h" |
| 19 | #include "llvm/ADT/Hashing.h" |
| 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
| 23 | #include "llvm/Config/llvm-config.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/Error.h" |
| 26 | #include "llvm/Support/MathExtras.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include <cstring> |
| 29 | #include <limits.h> |
| 30 | |
| 31 | #define APFLOAT_DISPATCH_ON_SEMANTICS(METHOD_CALL) \ |
| 32 | do { \ |
| 33 | if (usesLayout<IEEEFloat>(getSemantics())) \ |
| 34 | return U.IEEE.METHOD_CALL; \ |
| 35 | if (usesLayout<DoubleAPFloat>(getSemantics())) \ |
| 36 | return U.Double.METHOD_CALL; \ |
| 37 | llvm_unreachable("Unexpected semantics"); \ |
| 38 | } while (false) |
| 39 | |
| 40 | using namespace llvm; |
| 41 | |
| 42 | /// A macro used to combine two fcCategory enums into one key which can be used |
| 43 | /// in a switch statement to classify how the interaction of two APFloat's |
| 44 | /// categories affects an operation. |
| 45 | /// |
| 46 | /// TODO: If clang source code is ever allowed to use constexpr in its own |
| 47 | /// codebase, change this into a static inline function. |
| 48 | #define PackCategoriesIntoKey(_lhs, _rhs) ((_lhs) * 4 + (_rhs)) |
| 49 | |
| 50 | /* Assumed in hexadecimal significand parsing, and conversion to |
| 51 | hexadecimal strings. */ |
| 52 | static_assert(APFloatBase::integerPartWidth % 4 == 0, "Part width must be divisible by 4!" ); |
| 53 | |
| 54 | namespace llvm { |
| 55 | |
| 56 | constexpr fltSemantics APFloatBase::semIEEEhalf = {.maxExponent: 15, .minExponent: -14, .precision: 11, .sizeInBits: 16}; |
| 57 | constexpr fltSemantics APFloatBase::semBFloat = {.maxExponent: 127, .minExponent: -126, .precision: 8, .sizeInBits: 16}; |
| 58 | constexpr fltSemantics APFloatBase::semIEEEsingle = {.maxExponent: 127, .minExponent: -126, .precision: 24, .sizeInBits: 32}; |
| 59 | constexpr fltSemantics APFloatBase::semIEEEdouble = {.maxExponent: 1023, .minExponent: -1022, .precision: 53, .sizeInBits: 64}; |
| 60 | constexpr fltSemantics APFloatBase::semIEEEquad = {.maxExponent: 16383, .minExponent: -16382, .precision: 113, .sizeInBits: 128}; |
| 61 | constexpr fltSemantics APFloatBase::semFloat8E5M2 = {.maxExponent: 15, .minExponent: -14, .precision: 3, .sizeInBits: 8}; |
| 62 | constexpr fltSemantics APFloatBase::semFloat8E5M2FNUZ = { |
| 63 | .maxExponent: 15, .minExponent: -15, .precision: 3, .sizeInBits: 8, .nonFiniteBehavior: fltNonfiniteBehavior::NanOnly, .nanEncoding: fltNanEncoding::NegativeZero}; |
| 64 | constexpr fltSemantics APFloatBase::semFloat8E4M3 = {.maxExponent: 7, .minExponent: -6, .precision: 4, .sizeInBits: 8}; |
| 65 | constexpr fltSemantics APFloatBase::semFloat8E4M3FN = { |
| 66 | .maxExponent: 8, .minExponent: -6, .precision: 4, .sizeInBits: 8, .nonFiniteBehavior: fltNonfiniteBehavior::NanOnly, .nanEncoding: fltNanEncoding::AllOnes}; |
| 67 | constexpr fltSemantics APFloatBase::semFloat8E4M3FNUZ = { |
| 68 | .maxExponent: 7, .minExponent: -7, .precision: 4, .sizeInBits: 8, .nonFiniteBehavior: fltNonfiniteBehavior::NanOnly, .nanEncoding: fltNanEncoding::NegativeZero}; |
| 69 | constexpr fltSemantics APFloatBase::semFloat8E4M3B11FNUZ = { |
| 70 | .maxExponent: 4, .minExponent: -10, .precision: 4, .sizeInBits: 8, .nonFiniteBehavior: fltNonfiniteBehavior::NanOnly, .nanEncoding: fltNanEncoding::NegativeZero}; |
| 71 | constexpr fltSemantics APFloatBase::semFloat8E3M4 = {.maxExponent: 3, .minExponent: -2, .precision: 5, .sizeInBits: 8}; |
| 72 | constexpr fltSemantics APFloatBase::semFloatTF32 = {.maxExponent: 127, .minExponent: -126, .precision: 11, .sizeInBits: 19}; |
| 73 | constexpr fltSemantics APFloatBase::semFloat8E8M0FNU = { |
| 74 | .maxExponent: 127, |
| 75 | .minExponent: -127, |
| 76 | .precision: 1, |
| 77 | .sizeInBits: 8, |
| 78 | .nonFiniteBehavior: fltNonfiniteBehavior::NanOnly, |
| 79 | .nanEncoding: fltNanEncoding::AllOnes, |
| 80 | .hasZero: false, |
| 81 | .hasSignedRepr: false, |
| 82 | .hasSignBitInMSB: false}; |
| 83 | |
| 84 | constexpr fltSemantics APFloatBase::semFloat6E3M2FN = { |
| 85 | .maxExponent: 4, .minExponent: -2, .precision: 3, .sizeInBits: 6, .nonFiniteBehavior: fltNonfiniteBehavior::FiniteOnly}; |
| 86 | constexpr fltSemantics APFloatBase::semFloat6E2M3FN = { |
| 87 | .maxExponent: 2, .minExponent: 0, .precision: 4, .sizeInBits: 6, .nonFiniteBehavior: fltNonfiniteBehavior::FiniteOnly}; |
| 88 | constexpr fltSemantics APFloatBase::semFloat4E2M1FN = { |
| 89 | .maxExponent: 2, .minExponent: 0, .precision: 2, .sizeInBits: 4, .nonFiniteBehavior: fltNonfiniteBehavior::FiniteOnly}; |
| 90 | constexpr fltSemantics APFloatBase::semX87DoubleExtended = {.maxExponent: 16383, .minExponent: -16382, .precision: 64, |
| 91 | .sizeInBits: 80}; |
| 92 | constexpr fltSemantics APFloatBase::semBogus = {.maxExponent: 0, .minExponent: 0, .precision: 0, .sizeInBits: 0}; |
| 93 | constexpr fltSemantics APFloatBase::semPPCDoubleDouble = {.maxExponent: -1, .minExponent: 0, .precision: 0, .sizeInBits: 128}; |
| 94 | constexpr fltSemantics APFloatBase::semPPCDoubleDoubleLegacy = { |
| 95 | .maxExponent: 1023, .minExponent: -1022 + 53, .precision: 53 + 53, .sizeInBits: 128}; |
| 96 | |
| 97 | const llvm::fltSemantics &APFloatBase::EnumToSemantics(Semantics S) { |
| 98 | switch (S) { |
| 99 | case S_IEEEhalf: |
| 100 | return IEEEhalf(); |
| 101 | case S_BFloat: |
| 102 | return BFloat(); |
| 103 | case S_IEEEsingle: |
| 104 | return IEEEsingle(); |
| 105 | case S_IEEEdouble: |
| 106 | return IEEEdouble(); |
| 107 | case S_IEEEquad: |
| 108 | return IEEEquad(); |
| 109 | case S_PPCDoubleDouble: |
| 110 | return PPCDoubleDouble(); |
| 111 | case S_PPCDoubleDoubleLegacy: |
| 112 | return PPCDoubleDoubleLegacy(); |
| 113 | case S_Float8E5M2: |
| 114 | return Float8E5M2(); |
| 115 | case S_Float8E5M2FNUZ: |
| 116 | return Float8E5M2FNUZ(); |
| 117 | case S_Float8E4M3: |
| 118 | return Float8E4M3(); |
| 119 | case S_Float8E4M3FN: |
| 120 | return Float8E4M3FN(); |
| 121 | case S_Float8E4M3FNUZ: |
| 122 | return Float8E4M3FNUZ(); |
| 123 | case S_Float8E4M3B11FNUZ: |
| 124 | return Float8E4M3B11FNUZ(); |
| 125 | case S_Float8E3M4: |
| 126 | return Float8E3M4(); |
| 127 | case S_FloatTF32: |
| 128 | return FloatTF32(); |
| 129 | case S_Float8E8M0FNU: |
| 130 | return Float8E8M0FNU(); |
| 131 | case S_Float6E3M2FN: |
| 132 | return Float6E3M2FN(); |
| 133 | case S_Float6E2M3FN: |
| 134 | return Float6E2M3FN(); |
| 135 | case S_Float4E2M1FN: |
| 136 | return Float4E2M1FN(); |
| 137 | case S_x87DoubleExtended: |
| 138 | return x87DoubleExtended(); |
| 139 | } |
| 140 | llvm_unreachable("Unrecognised floating semantics" ); |
| 141 | } |
| 142 | |
| 143 | APFloatBase::Semantics |
| 144 | APFloatBase::SemanticsToEnum(const llvm::fltSemantics &Sem) { |
| 145 | if (&Sem == &llvm::APFloat::IEEEhalf()) |
| 146 | return S_IEEEhalf; |
| 147 | else if (&Sem == &llvm::APFloat::BFloat()) |
| 148 | return S_BFloat; |
| 149 | else if (&Sem == &llvm::APFloat::IEEEsingle()) |
| 150 | return S_IEEEsingle; |
| 151 | else if (&Sem == &llvm::APFloat::IEEEdouble()) |
| 152 | return S_IEEEdouble; |
| 153 | else if (&Sem == &llvm::APFloat::IEEEquad()) |
| 154 | return S_IEEEquad; |
| 155 | else if (&Sem == &llvm::APFloat::PPCDoubleDouble()) |
| 156 | return S_PPCDoubleDouble; |
| 157 | else if (&Sem == &llvm::APFloat::PPCDoubleDoubleLegacy()) |
| 158 | return S_PPCDoubleDoubleLegacy; |
| 159 | else if (&Sem == &llvm::APFloat::Float8E5M2()) |
| 160 | return S_Float8E5M2; |
| 161 | else if (&Sem == &llvm::APFloat::Float8E5M2FNUZ()) |
| 162 | return S_Float8E5M2FNUZ; |
| 163 | else if (&Sem == &llvm::APFloat::Float8E4M3()) |
| 164 | return S_Float8E4M3; |
| 165 | else if (&Sem == &llvm::APFloat::Float8E4M3FN()) |
| 166 | return S_Float8E4M3FN; |
| 167 | else if (&Sem == &llvm::APFloat::Float8E4M3FNUZ()) |
| 168 | return S_Float8E4M3FNUZ; |
| 169 | else if (&Sem == &llvm::APFloat::Float8E4M3B11FNUZ()) |
| 170 | return S_Float8E4M3B11FNUZ; |
| 171 | else if (&Sem == &llvm::APFloat::Float8E3M4()) |
| 172 | return S_Float8E3M4; |
| 173 | else if (&Sem == &llvm::APFloat::FloatTF32()) |
| 174 | return S_FloatTF32; |
| 175 | else if (&Sem == &llvm::APFloat::Float8E8M0FNU()) |
| 176 | return S_Float8E8M0FNU; |
| 177 | else if (&Sem == &llvm::APFloat::Float6E3M2FN()) |
| 178 | return S_Float6E3M2FN; |
| 179 | else if (&Sem == &llvm::APFloat::Float6E2M3FN()) |
| 180 | return S_Float6E2M3FN; |
| 181 | else if (&Sem == &llvm::APFloat::Float4E2M1FN()) |
| 182 | return S_Float4E2M1FN; |
| 183 | else if (&Sem == &llvm::APFloat::x87DoubleExtended()) |
| 184 | return S_x87DoubleExtended; |
| 185 | else |
| 186 | llvm_unreachable("Unknown floating semantics" ); |
| 187 | } |
| 188 | |
| 189 | bool APFloatBase::isRepresentableBy(const fltSemantics &A, |
| 190 | const fltSemantics &B) { |
| 191 | return A.maxExponent <= B.maxExponent && A.minExponent >= B.minExponent && |
| 192 | A.precision <= B.precision; |
| 193 | } |
| 194 | |
| 195 | /* A tight upper bound on number of parts required to hold the value |
| 196 | pow(5, power) is |
| 197 | |
| 198 | power * 815 / (351 * integerPartWidth) + 1 |
| 199 | |
| 200 | However, whilst the result may require only this many parts, |
| 201 | because we are multiplying two values to get it, the |
| 202 | multiplication may require an extra part with the excess part |
| 203 | being zero (consider the trivial case of 1 * 1, tcFullMultiply |
| 204 | requires two parts to hold the single-part result). So we add an |
| 205 | extra one to guarantee enough space whilst multiplying. */ |
| 206 | const unsigned int maxExponent = 16383; |
| 207 | const unsigned int maxPrecision = 113; |
| 208 | const unsigned int maxPowerOfFiveExponent = maxExponent + maxPrecision - 1; |
| 209 | const unsigned int maxPowerOfFiveParts = |
| 210 | 2 + |
| 211 | ((maxPowerOfFiveExponent * 815) / (351 * APFloatBase::integerPartWidth)); |
| 212 | |
| 213 | unsigned int APFloatBase::semanticsPrecision(const fltSemantics &semantics) { |
| 214 | return semantics.precision; |
| 215 | } |
| 216 | APFloatBase::ExponentType |
| 217 | APFloatBase::semanticsMaxExponent(const fltSemantics &semantics) { |
| 218 | return semantics.maxExponent; |
| 219 | } |
| 220 | APFloatBase::ExponentType |
| 221 | APFloatBase::semanticsMinExponent(const fltSemantics &semantics) { |
| 222 | return semantics.minExponent; |
| 223 | } |
| 224 | unsigned int APFloatBase::semanticsSizeInBits(const fltSemantics &semantics) { |
| 225 | return semantics.sizeInBits; |
| 226 | } |
| 227 | unsigned int APFloatBase::semanticsIntSizeInBits(const fltSemantics &semantics, |
| 228 | bool isSigned) { |
| 229 | // The max FP value is pow(2, MaxExponent) * (1 + MaxFraction), so we need |
| 230 | // at least one more bit than the MaxExponent to hold the max FP value. |
| 231 | unsigned int MinBitWidth = semanticsMaxExponent(semantics) + 1; |
| 232 | // Extra sign bit needed. |
| 233 | if (isSigned) |
| 234 | ++MinBitWidth; |
| 235 | return MinBitWidth; |
| 236 | } |
| 237 | |
| 238 | bool APFloatBase::semanticsHasZero(const fltSemantics &semantics) { |
| 239 | return semantics.hasZero; |
| 240 | } |
| 241 | |
| 242 | bool APFloatBase::semanticsHasSignedRepr(const fltSemantics &semantics) { |
| 243 | return semantics.hasSignedRepr; |
| 244 | } |
| 245 | |
| 246 | bool APFloatBase::semanticsHasInf(const fltSemantics &semantics) { |
| 247 | return semantics.nonFiniteBehavior == fltNonfiniteBehavior::IEEE754; |
| 248 | } |
| 249 | |
| 250 | bool APFloatBase::semanticsHasNaN(const fltSemantics &semantics) { |
| 251 | return semantics.nonFiniteBehavior != fltNonfiniteBehavior::FiniteOnly; |
| 252 | } |
| 253 | |
| 254 | bool APFloatBase::isIEEELikeFP(const fltSemantics &semantics) { |
| 255 | // Keep in sync with Type::isIEEELikeFPTy |
| 256 | return SemanticsToEnum(Sem: semantics) <= S_IEEEquad; |
| 257 | } |
| 258 | |
| 259 | bool APFloatBase::hasSignBitInMSB(const fltSemantics &semantics) { |
| 260 | return semantics.hasSignBitInMSB; |
| 261 | } |
| 262 | |
| 263 | bool APFloatBase::isRepresentableAsNormalIn(const fltSemantics &Src, |
| 264 | const fltSemantics &Dst) { |
| 265 | // Exponent range must be larger. |
| 266 | if (Src.maxExponent >= Dst.maxExponent || Src.minExponent <= Dst.minExponent) |
| 267 | return false; |
| 268 | |
| 269 | // If the mantissa is long enough, the result value could still be denormal |
| 270 | // with a larger exponent range. |
| 271 | // |
| 272 | // FIXME: This condition is probably not accurate but also shouldn't be a |
| 273 | // practical concern with existing types. |
| 274 | return Dst.precision >= Src.precision; |
| 275 | } |
| 276 | |
| 277 | unsigned APFloatBase::getSizeInBits(const fltSemantics &Sem) { |
| 278 | return Sem.sizeInBits; |
| 279 | } |
| 280 | |
| 281 | static constexpr APFloatBase::ExponentType |
| 282 | exponentZero(const fltSemantics &semantics) { |
| 283 | return semantics.minExponent - 1; |
| 284 | } |
| 285 | |
| 286 | static constexpr APFloatBase::ExponentType |
| 287 | exponentInf(const fltSemantics &semantics) { |
| 288 | return semantics.maxExponent + 1; |
| 289 | } |
| 290 | |
| 291 | static constexpr APFloatBase::ExponentType |
| 292 | exponentNaN(const fltSemantics &semantics) { |
| 293 | if (semantics.nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { |
| 294 | if (semantics.nanEncoding == fltNanEncoding::NegativeZero) |
| 295 | return exponentZero(semantics); |
| 296 | if (semantics.hasSignedRepr) |
| 297 | return semantics.maxExponent; |
| 298 | } |
| 299 | return semantics.maxExponent + 1; |
| 300 | } |
| 301 | |
| 302 | /* A bunch of private, handy routines. */ |
| 303 | |
| 304 | static inline Error createError(const Twine &Err) { |
| 305 | return make_error<StringError>(Args: Err, Args: inconvertibleErrorCode()); |
| 306 | } |
| 307 | |
| 308 | static constexpr inline unsigned int partCountForBits(unsigned int bits) { |
| 309 | return std::max(a: 1u, b: (bits + APFloatBase::integerPartWidth - 1) / |
| 310 | APFloatBase::integerPartWidth); |
| 311 | } |
| 312 | |
| 313 | /* Returns 0U-9U. Return values >= 10U are not digits. */ |
| 314 | static inline unsigned int |
| 315 | decDigitValue(unsigned int c) |
| 316 | { |
| 317 | return c - '0'; |
| 318 | } |
| 319 | |
| 320 | /* Return the value of a decimal exponent of the form |
| 321 | [+-]ddddddd. |
| 322 | |
| 323 | If the exponent overflows, returns a large exponent with the |
| 324 | appropriate sign. */ |
| 325 | static Expected<int> readExponent(StringRef::iterator begin, |
| 326 | StringRef::iterator end) { |
| 327 | bool isNegative; |
| 328 | unsigned int absExponent; |
| 329 | const unsigned int overlargeExponent = 24000; /* FIXME. */ |
| 330 | StringRef::iterator p = begin; |
| 331 | |
| 332 | // Treat no exponent as 0 to match binutils |
| 333 | if (p == end || ((*p == '-' || *p == '+') && (p + 1) == end)) { |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | isNegative = (*p == '-'); |
| 338 | if (*p == '-' || *p == '+') { |
| 339 | p++; |
| 340 | if (p == end) |
| 341 | return createError(Err: "Exponent has no digits" ); |
| 342 | } |
| 343 | |
| 344 | absExponent = decDigitValue(c: *p++); |
| 345 | if (absExponent >= 10U) |
| 346 | return createError(Err: "Invalid character in exponent" ); |
| 347 | |
| 348 | for (; p != end; ++p) { |
| 349 | unsigned int value; |
| 350 | |
| 351 | value = decDigitValue(c: *p); |
| 352 | if (value >= 10U) |
| 353 | return createError(Err: "Invalid character in exponent" ); |
| 354 | |
| 355 | absExponent = absExponent * 10U + value; |
| 356 | if (absExponent >= overlargeExponent) { |
| 357 | absExponent = overlargeExponent; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if (isNegative) |
| 363 | return -(int) absExponent; |
| 364 | else |
| 365 | return (int) absExponent; |
| 366 | } |
| 367 | |
| 368 | /* This is ugly and needs cleaning up, but I don't immediately see |
| 369 | how whilst remaining safe. */ |
| 370 | static Expected<int> totalExponent(StringRef::iterator p, |
| 371 | StringRef::iterator end, |
| 372 | int exponentAdjustment) { |
| 373 | int unsignedExponent; |
| 374 | bool negative, overflow; |
| 375 | int exponent = 0; |
| 376 | |
| 377 | if (p == end) |
| 378 | return createError(Err: "Exponent has no digits" ); |
| 379 | |
| 380 | negative = *p == '-'; |
| 381 | if (*p == '-' || *p == '+') { |
| 382 | p++; |
| 383 | if (p == end) |
| 384 | return createError(Err: "Exponent has no digits" ); |
| 385 | } |
| 386 | |
| 387 | unsignedExponent = 0; |
| 388 | overflow = false; |
| 389 | for (; p != end; ++p) { |
| 390 | unsigned int value; |
| 391 | |
| 392 | value = decDigitValue(c: *p); |
| 393 | if (value >= 10U) |
| 394 | return createError(Err: "Invalid character in exponent" ); |
| 395 | |
| 396 | unsignedExponent = unsignedExponent * 10 + value; |
| 397 | if (unsignedExponent > 32767) { |
| 398 | overflow = true; |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (exponentAdjustment > 32767 || exponentAdjustment < -32768) |
| 404 | overflow = true; |
| 405 | |
| 406 | if (!overflow) { |
| 407 | exponent = unsignedExponent; |
| 408 | if (negative) |
| 409 | exponent = -exponent; |
| 410 | exponent += exponentAdjustment; |
| 411 | if (exponent > 32767 || exponent < -32768) |
| 412 | overflow = true; |
| 413 | } |
| 414 | |
| 415 | if (overflow) |
| 416 | exponent = negative ? -32768: 32767; |
| 417 | |
| 418 | return exponent; |
| 419 | } |
| 420 | |
| 421 | static Expected<StringRef::iterator> |
| 422 | skipLeadingZeroesAndAnyDot(StringRef::iterator begin, StringRef::iterator end, |
| 423 | StringRef::iterator *dot) { |
| 424 | StringRef::iterator p = begin; |
| 425 | *dot = end; |
| 426 | while (p != end && *p == '0') |
| 427 | p++; |
| 428 | |
| 429 | if (p != end && *p == '.') { |
| 430 | *dot = p++; |
| 431 | |
| 432 | if (end - begin == 1) |
| 433 | return createError(Err: "Significand has no digits" ); |
| 434 | |
| 435 | while (p != end && *p == '0') |
| 436 | p++; |
| 437 | } |
| 438 | |
| 439 | return p; |
| 440 | } |
| 441 | |
| 442 | /* Given a normal decimal floating point number of the form |
| 443 | |
| 444 | dddd.dddd[eE][+-]ddd |
| 445 | |
| 446 | where the decimal point and exponent are optional, fill out the |
| 447 | structure D. Exponent is appropriate if the significand is |
| 448 | treated as an integer, and normalizedExponent if the significand |
| 449 | is taken to have the decimal point after a single leading |
| 450 | non-zero digit. |
| 451 | |
| 452 | If the value is zero, V->firstSigDigit points to a non-digit, and |
| 453 | the return exponent is zero. |
| 454 | */ |
| 455 | struct decimalInfo { |
| 456 | const char *firstSigDigit; |
| 457 | const char *lastSigDigit; |
| 458 | int exponent; |
| 459 | int normalizedExponent; |
| 460 | }; |
| 461 | |
| 462 | static Error interpretDecimal(StringRef::iterator begin, |
| 463 | StringRef::iterator end, decimalInfo *D) { |
| 464 | StringRef::iterator dot = end; |
| 465 | |
| 466 | auto PtrOrErr = skipLeadingZeroesAndAnyDot(begin, end, dot: &dot); |
| 467 | if (!PtrOrErr) |
| 468 | return PtrOrErr.takeError(); |
| 469 | StringRef::iterator p = *PtrOrErr; |
| 470 | |
| 471 | D->firstSigDigit = p; |
| 472 | D->exponent = 0; |
| 473 | D->normalizedExponent = 0; |
| 474 | |
| 475 | for (; p != end; ++p) { |
| 476 | if (*p == '.') { |
| 477 | if (dot != end) |
| 478 | return createError(Err: "String contains multiple dots" ); |
| 479 | dot = p++; |
| 480 | if (p == end) |
| 481 | break; |
| 482 | } |
| 483 | if (decDigitValue(c: *p) >= 10U) |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | if (p != end) { |
| 488 | if (*p != 'e' && *p != 'E') |
| 489 | return createError(Err: "Invalid character in significand" ); |
| 490 | if (p == begin) |
| 491 | return createError(Err: "Significand has no digits" ); |
| 492 | if (dot != end && p - begin == 1) |
| 493 | return createError(Err: "Significand has no digits" ); |
| 494 | |
| 495 | /* p points to the first non-digit in the string */ |
| 496 | auto ExpOrErr = readExponent(begin: p + 1, end); |
| 497 | if (!ExpOrErr) |
| 498 | return ExpOrErr.takeError(); |
| 499 | D->exponent = *ExpOrErr; |
| 500 | |
| 501 | /* Implied decimal point? */ |
| 502 | if (dot == end) |
| 503 | dot = p; |
| 504 | } |
| 505 | |
| 506 | /* If number is all zeroes accept any exponent. */ |
| 507 | if (p != D->firstSigDigit) { |
| 508 | /* Drop insignificant trailing zeroes. */ |
| 509 | if (p != begin) { |
| 510 | do |
| 511 | do |
| 512 | p--; |
| 513 | while (p != begin && *p == '0'); |
| 514 | while (p != begin && *p == '.'); |
| 515 | } |
| 516 | |
| 517 | /* Adjust the exponents for any decimal point. */ |
| 518 | D->exponent += static_cast<APFloat::ExponentType>((dot - p) - (dot > p)); |
| 519 | D->normalizedExponent = (D->exponent + |
| 520 | static_cast<APFloat::ExponentType>((p - D->firstSigDigit) |
| 521 | - (dot > D->firstSigDigit && dot < p))); |
| 522 | } |
| 523 | |
| 524 | D->lastSigDigit = p; |
| 525 | return Error::success(); |
| 526 | } |
| 527 | |
| 528 | /* Return the trailing fraction of a hexadecimal number. |
| 529 | DIGITVALUE is the first hex digit of the fraction, P points to |
| 530 | the next digit. */ |
| 531 | static Expected<lostFraction> |
| 532 | trailingHexadecimalFraction(StringRef::iterator p, StringRef::iterator end, |
| 533 | unsigned int digitValue) { |
| 534 | unsigned int hexDigit; |
| 535 | |
| 536 | /* If the first trailing digit isn't 0 or 8 we can work out the |
| 537 | fraction immediately. */ |
| 538 | if (digitValue > 8) |
| 539 | return lfMoreThanHalf; |
| 540 | else if (digitValue < 8 && digitValue > 0) |
| 541 | return lfLessThanHalf; |
| 542 | |
| 543 | // Otherwise we need to find the first non-zero digit. |
| 544 | while (p != end && (*p == '0' || *p == '.')) |
| 545 | p++; |
| 546 | |
| 547 | if (p == end) |
| 548 | return createError(Err: "Invalid trailing hexadecimal fraction!" ); |
| 549 | |
| 550 | hexDigit = hexDigitValue(C: *p); |
| 551 | |
| 552 | /* If we ran off the end it is exactly zero or one-half, otherwise |
| 553 | a little more. */ |
| 554 | if (hexDigit == UINT_MAX) |
| 555 | return digitValue == 0 ? lfExactlyZero: lfExactlyHalf; |
| 556 | else |
| 557 | return digitValue == 0 ? lfLessThanHalf: lfMoreThanHalf; |
| 558 | } |
| 559 | |
| 560 | /* Return the fraction lost were a bignum truncated losing the least |
| 561 | significant BITS bits. */ |
| 562 | static lostFraction |
| 563 | lostFractionThroughTruncation(const APFloatBase::integerPart *parts, |
| 564 | unsigned int partCount, |
| 565 | unsigned int bits) |
| 566 | { |
| 567 | unsigned int lsb; |
| 568 | |
| 569 | lsb = APInt::tcLSB(parts, n: partCount); |
| 570 | |
| 571 | /* Note this is guaranteed true if bits == 0, or LSB == UINT_MAX. */ |
| 572 | if (bits <= lsb) |
| 573 | return lfExactlyZero; |
| 574 | if (bits == lsb + 1) |
| 575 | return lfExactlyHalf; |
| 576 | if (bits <= partCount * APFloatBase::integerPartWidth && |
| 577 | APInt::tcExtractBit(parts, bit: bits - 1)) |
| 578 | return lfMoreThanHalf; |
| 579 | |
| 580 | return lfLessThanHalf; |
| 581 | } |
| 582 | |
| 583 | /* Shift DST right BITS bits noting lost fraction. */ |
| 584 | static lostFraction |
| 585 | shiftRight(APFloatBase::integerPart *dst, unsigned int parts, unsigned int bits) |
| 586 | { |
| 587 | lostFraction lost_fraction; |
| 588 | |
| 589 | lost_fraction = lostFractionThroughTruncation(parts: dst, partCount: parts, bits); |
| 590 | |
| 591 | APInt::tcShiftRight(dst, Words: parts, Count: bits); |
| 592 | |
| 593 | return lost_fraction; |
| 594 | } |
| 595 | |
| 596 | /* Combine the effect of two lost fractions. */ |
| 597 | static lostFraction |
| 598 | combineLostFractions(lostFraction moreSignificant, |
| 599 | lostFraction lessSignificant) |
| 600 | { |
| 601 | if (lessSignificant != lfExactlyZero) { |
| 602 | if (moreSignificant == lfExactlyZero) |
| 603 | moreSignificant = lfLessThanHalf; |
| 604 | else if (moreSignificant == lfExactlyHalf) |
| 605 | moreSignificant = lfMoreThanHalf; |
| 606 | } |
| 607 | |
| 608 | return moreSignificant; |
| 609 | } |
| 610 | |
| 611 | /* The error from the true value, in half-ulps, on multiplying two |
| 612 | floating point numbers, which differ from the value they |
| 613 | approximate by at most HUE1 and HUE2 half-ulps, is strictly less |
| 614 | than the returned value. |
| 615 | |
| 616 | See "How to Read Floating Point Numbers Accurately" by William D |
| 617 | Clinger. */ |
| 618 | static unsigned int |
| 619 | HUerrBound(bool inexactMultiply, unsigned int HUerr1, unsigned int HUerr2) |
| 620 | { |
| 621 | assert(HUerr1 < 2 || HUerr2 < 2 || (HUerr1 + HUerr2 < 8)); |
| 622 | |
| 623 | if (HUerr1 + HUerr2 == 0) |
| 624 | return inexactMultiply * 2; /* <= inexactMultiply half-ulps. */ |
| 625 | else |
| 626 | return inexactMultiply + 2 * (HUerr1 + HUerr2); |
| 627 | } |
| 628 | |
| 629 | /* The number of ulps from the boundary (zero, or half if ISNEAREST) |
| 630 | when the least significant BITS are truncated. BITS cannot be |
| 631 | zero. */ |
| 632 | static APFloatBase::integerPart |
| 633 | ulpsFromBoundary(const APFloatBase::integerPart *parts, unsigned int bits, |
| 634 | bool isNearest) { |
| 635 | unsigned int count, partBits; |
| 636 | APFloatBase::integerPart part, boundary; |
| 637 | |
| 638 | assert(bits != 0); |
| 639 | |
| 640 | bits--; |
| 641 | count = bits / APFloatBase::integerPartWidth; |
| 642 | partBits = bits % APFloatBase::integerPartWidth + 1; |
| 643 | |
| 644 | part = parts[count] & (~(APFloatBase::integerPart) 0 >> (APFloatBase::integerPartWidth - partBits)); |
| 645 | |
| 646 | if (isNearest) |
| 647 | boundary = (APFloatBase::integerPart) 1 << (partBits - 1); |
| 648 | else |
| 649 | boundary = 0; |
| 650 | |
| 651 | if (count == 0) { |
| 652 | if (part - boundary <= boundary - part) |
| 653 | return part - boundary; |
| 654 | else |
| 655 | return boundary - part; |
| 656 | } |
| 657 | |
| 658 | if (part == boundary) { |
| 659 | while (--count) |
| 660 | if (parts[count]) |
| 661 | return ~(APFloatBase::integerPart) 0; /* A lot. */ |
| 662 | |
| 663 | return parts[0]; |
| 664 | } else if (part == boundary - 1) { |
| 665 | while (--count) |
| 666 | if (~parts[count]) |
| 667 | return ~(APFloatBase::integerPart) 0; /* A lot. */ |
| 668 | |
| 669 | return -parts[0]; |
| 670 | } |
| 671 | |
| 672 | return ~(APFloatBase::integerPart) 0; /* A lot. */ |
| 673 | } |
| 674 | |
| 675 | /* Place pow(5, power) in DST, and return the number of parts used. |
| 676 | DST must be at least one part larger than size of the answer. */ |
| 677 | static unsigned int |
| 678 | powerOf5(APFloatBase::integerPart *dst, unsigned int power) { |
| 679 | static const APFloatBase::integerPart firstEightPowers[] = { 1, 5, 25, 125, 625, 3125, 15625, 78125 }; |
| 680 | APFloatBase::integerPart pow5s[maxPowerOfFiveParts * 2 + 5]; |
| 681 | pow5s[0] = 78125 * 5; |
| 682 | |
| 683 | unsigned int partsCount = 1; |
| 684 | APFloatBase::integerPart scratch[maxPowerOfFiveParts], *p1, *p2, *pow5; |
| 685 | unsigned int result; |
| 686 | assert(power <= maxExponent); |
| 687 | |
| 688 | p1 = dst; |
| 689 | p2 = scratch; |
| 690 | |
| 691 | *p1 = firstEightPowers[power & 7]; |
| 692 | power >>= 3; |
| 693 | |
| 694 | result = 1; |
| 695 | pow5 = pow5s; |
| 696 | |
| 697 | for (unsigned int n = 0; power; power >>= 1, n++) { |
| 698 | /* Calculate pow(5,pow(2,n+3)) if we haven't yet. */ |
| 699 | if (n != 0) { |
| 700 | APInt::tcFullMultiply(pow5, pow5 - partsCount, pow5 - partsCount, |
| 701 | partsCount, partsCount); |
| 702 | partsCount *= 2; |
| 703 | if (pow5[partsCount - 1] == 0) |
| 704 | partsCount--; |
| 705 | } |
| 706 | |
| 707 | if (power & 1) { |
| 708 | APFloatBase::integerPart *tmp; |
| 709 | |
| 710 | APInt::tcFullMultiply(p2, p1, pow5, result, partsCount); |
| 711 | result += partsCount; |
| 712 | if (p2[result - 1] == 0) |
| 713 | result--; |
| 714 | |
| 715 | /* Now result is in p1 with partsCount parts and p2 is scratch |
| 716 | space. */ |
| 717 | tmp = p1; |
| 718 | p1 = p2; |
| 719 | p2 = tmp; |
| 720 | } |
| 721 | |
| 722 | pow5 += partsCount; |
| 723 | } |
| 724 | |
| 725 | if (p1 != dst) |
| 726 | APInt::tcAssign(dst, p1, result); |
| 727 | |
| 728 | return result; |
| 729 | } |
| 730 | |
| 731 | /* Zero at the end to avoid modular arithmetic when adding one; used |
| 732 | when rounding up during hexadecimal output. */ |
| 733 | static const char hexDigitsLower[] = "0123456789abcdef0" ; |
| 734 | static const char hexDigitsUpper[] = "0123456789ABCDEF0" ; |
| 735 | static const char infinityL[] = "infinity" ; |
| 736 | static const char infinityU[] = "INFINITY" ; |
| 737 | static const char NaNL[] = "nan" ; |
| 738 | static const char NaNU[] = "NAN" ; |
| 739 | |
| 740 | /* Write out an integerPart in hexadecimal, starting with the most |
| 741 | significant nibble. Write out exactly COUNT hexdigits, return |
| 742 | COUNT. */ |
| 743 | static unsigned int |
| 744 | partAsHex (char *dst, APFloatBase::integerPart part, unsigned int count, |
| 745 | const char *hexDigitChars) |
| 746 | { |
| 747 | unsigned int result = count; |
| 748 | |
| 749 | assert(count != 0 && count <= APFloatBase::integerPartWidth / 4); |
| 750 | |
| 751 | part >>= (APFloatBase::integerPartWidth - 4 * count); |
| 752 | while (count--) { |
| 753 | dst[count] = hexDigitChars[part & 0xf]; |
| 754 | part >>= 4; |
| 755 | } |
| 756 | |
| 757 | return result; |
| 758 | } |
| 759 | |
| 760 | /* Write out an unsigned decimal integer. */ |
| 761 | static char * |
| 762 | writeUnsignedDecimal (char *dst, unsigned int n) |
| 763 | { |
| 764 | char buff[40], *p; |
| 765 | |
| 766 | p = buff; |
| 767 | do |
| 768 | *p++ = '0' + n % 10; |
| 769 | while (n /= 10); |
| 770 | |
| 771 | do |
| 772 | *dst++ = *--p; |
| 773 | while (p != buff); |
| 774 | |
| 775 | return dst; |
| 776 | } |
| 777 | |
| 778 | /* Write out a signed decimal integer. */ |
| 779 | static char * |
| 780 | writeSignedDecimal (char *dst, int value) |
| 781 | { |
| 782 | if (value < 0) { |
| 783 | *dst++ = '-'; |
| 784 | dst = writeUnsignedDecimal(dst, n: -(unsigned) value); |
| 785 | } else { |
| 786 | dst = writeUnsignedDecimal(dst, n: value); |
| 787 | } |
| 788 | |
| 789 | return dst; |
| 790 | } |
| 791 | |
| 792 | // Compute the ULP of the input using a definition from: |
| 793 | // Jean-Michel Muller. On the definition of ulp(x). [Research Report] RR-5504, |
| 794 | // LIP RR-2005-09, INRIA, LIP. 2005, pp.16. inria-00070503 |
| 795 | static APFloat harrisonUlp(const APFloat &X) { |
| 796 | const fltSemantics &Sem = X.getSemantics(); |
| 797 | switch (X.getCategory()) { |
| 798 | case APFloat::fcNaN: |
| 799 | return APFloat::getQNaN(Sem); |
| 800 | case APFloat::fcInfinity: |
| 801 | return APFloat::getInf(Sem); |
| 802 | case APFloat::fcZero: |
| 803 | return APFloat::getSmallest(Sem); |
| 804 | case APFloat::fcNormal: |
| 805 | break; |
| 806 | } |
| 807 | if (X.isDenormal() || X.isSmallestNormalized()) |
| 808 | return APFloat::getSmallest(Sem); |
| 809 | int Exp = ilogb(Arg: X); |
| 810 | if (X.getExactLog2() != INT_MIN) |
| 811 | Exp -= 1; |
| 812 | return scalbn(X: APFloat::getOne(Sem), Exp: Exp - (Sem.precision - 1), |
| 813 | RM: APFloat::rmNearestTiesToEven); |
| 814 | } |
| 815 | |
| 816 | namespace detail { |
| 817 | /* Constructors. */ |
| 818 | void IEEEFloat::initialize(const fltSemantics *ourSemantics) { |
| 819 | unsigned int count; |
| 820 | |
| 821 | semantics = ourSemantics; |
| 822 | count = partCount(); |
| 823 | if (count > 1) |
| 824 | significand.parts = new integerPart[count]; |
| 825 | } |
| 826 | |
| 827 | void IEEEFloat::freeSignificand() { |
| 828 | if (needsCleanup()) |
| 829 | delete [] significand.parts; |
| 830 | } |
| 831 | |
| 832 | void IEEEFloat::assign(const IEEEFloat &rhs) { |
| 833 | assert(semantics == rhs.semantics); |
| 834 | |
| 835 | sign = rhs.sign; |
| 836 | category = rhs.category; |
| 837 | exponent = rhs.exponent; |
| 838 | if (isFiniteNonZero() || category == fcNaN) |
| 839 | copySignificand(rhs); |
| 840 | } |
| 841 | |
| 842 | void IEEEFloat::copySignificand(const IEEEFloat &rhs) { |
| 843 | assert(isFiniteNonZero() || category == fcNaN); |
| 844 | assert(rhs.partCount() >= partCount()); |
| 845 | |
| 846 | APInt::tcAssign(significandParts(), rhs.significandParts(), |
| 847 | partCount()); |
| 848 | } |
| 849 | |
| 850 | /* Make this number a NaN, with an arbitrary but deterministic value |
| 851 | for the significand. If double or longer, this is a signalling NaN, |
| 852 | which may not be ideal. If float, this is QNaN(0). */ |
| 853 | void IEEEFloat::makeNaN(bool SNaN, bool Negative, const APInt *fill) { |
| 854 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::FiniteOnly) |
| 855 | llvm_unreachable("This floating point format does not support NaN" ); |
| 856 | |
| 857 | if (Negative && !semantics->hasSignedRepr) |
| 858 | llvm_unreachable( |
| 859 | "This floating point format does not support signed values" ); |
| 860 | |
| 861 | category = fcNaN; |
| 862 | sign = Negative; |
| 863 | exponent = exponentNaN(); |
| 864 | |
| 865 | integerPart *significand = significandParts(); |
| 866 | unsigned numParts = partCount(); |
| 867 | |
| 868 | APInt fill_storage; |
| 869 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { |
| 870 | // Finite-only types do not distinguish signalling and quiet NaN, so |
| 871 | // make them all signalling. |
| 872 | SNaN = false; |
| 873 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) { |
| 874 | sign = true; |
| 875 | fill_storage = APInt::getZero(numBits: semantics->precision - 1); |
| 876 | } else { |
| 877 | fill_storage = APInt::getAllOnes(numBits: semantics->precision - 1); |
| 878 | } |
| 879 | fill = &fill_storage; |
| 880 | } |
| 881 | |
| 882 | // Set the significand bits to the fill. |
| 883 | if (!fill || fill->getNumWords() < numParts) |
| 884 | APInt::tcSet(significand, 0, numParts); |
| 885 | if (fill) { |
| 886 | APInt::tcAssign(significand, fill->getRawData(), |
| 887 | std::min(a: fill->getNumWords(), b: numParts)); |
| 888 | |
| 889 | // Zero out the excess bits of the significand. |
| 890 | unsigned bitsToPreserve = semantics->precision - 1; |
| 891 | unsigned part = bitsToPreserve / 64; |
| 892 | bitsToPreserve %= 64; |
| 893 | significand[part] &= ((1ULL << bitsToPreserve) - 1); |
| 894 | for (part++; part != numParts; ++part) |
| 895 | significand[part] = 0; |
| 896 | } |
| 897 | |
| 898 | unsigned QNaNBit = |
| 899 | (semantics->precision >= 2) ? (semantics->precision - 2) : 0; |
| 900 | |
| 901 | if (SNaN) { |
| 902 | // We always have to clear the QNaN bit to make it an SNaN. |
| 903 | APInt::tcClearBit(significand, bit: QNaNBit); |
| 904 | |
| 905 | // If there are no bits set in the payload, we have to set |
| 906 | // *something* to make it a NaN instead of an infinity; |
| 907 | // conventionally, this is the next bit down from the QNaN bit. |
| 908 | if (APInt::tcIsZero(significand, numParts)) |
| 909 | APInt::tcSetBit(significand, bit: QNaNBit - 1); |
| 910 | } else if (semantics->nanEncoding == fltNanEncoding::NegativeZero) { |
| 911 | // The only NaN is a quiet NaN, and it has no bits sets in the significand. |
| 912 | // Do nothing. |
| 913 | } else { |
| 914 | // We always have to set the QNaN bit to make it a QNaN. |
| 915 | APInt::tcSetBit(significand, bit: QNaNBit); |
| 916 | } |
| 917 | |
| 918 | // For x87 extended precision, we want to make a NaN, not a |
| 919 | // pseudo-NaN. Maybe we should expose the ability to make |
| 920 | // pseudo-NaNs? |
| 921 | if (semantics == &APFloatBase::semX87DoubleExtended) |
| 922 | APInt::tcSetBit(significand, bit: QNaNBit + 1); |
| 923 | } |
| 924 | |
| 925 | IEEEFloat &IEEEFloat::operator=(const IEEEFloat &rhs) { |
| 926 | if (this != &rhs) { |
| 927 | if (semantics != rhs.semantics) { |
| 928 | freeSignificand(); |
| 929 | initialize(ourSemantics: rhs.semantics); |
| 930 | } |
| 931 | assign(rhs); |
| 932 | } |
| 933 | |
| 934 | return *this; |
| 935 | } |
| 936 | |
| 937 | IEEEFloat &IEEEFloat::operator=(IEEEFloat &&rhs) { |
| 938 | freeSignificand(); |
| 939 | |
| 940 | semantics = rhs.semantics; |
| 941 | significand = rhs.significand; |
| 942 | exponent = rhs.exponent; |
| 943 | category = rhs.category; |
| 944 | sign = rhs.sign; |
| 945 | |
| 946 | rhs.semantics = &APFloatBase::semBogus; |
| 947 | return *this; |
| 948 | } |
| 949 | |
| 950 | bool IEEEFloat::isDenormal() const { |
| 951 | return isFiniteNonZero() && (exponent == semantics->minExponent) && |
| 952 | (APInt::tcExtractBit(significandParts(), |
| 953 | bit: semantics->precision - 1) == 0); |
| 954 | } |
| 955 | |
| 956 | bool IEEEFloat::isSmallest() const { |
| 957 | // The smallest number by magnitude in our format will be the smallest |
| 958 | // denormal, i.e. the floating point number with exponent being minimum |
| 959 | // exponent and significand bitwise equal to 1 (i.e. with MSB equal to 0). |
| 960 | return isFiniteNonZero() && exponent == semantics->minExponent && |
| 961 | significandMSB() == 0; |
| 962 | } |
| 963 | |
| 964 | bool IEEEFloat::isSmallestNormalized() const { |
| 965 | return getCategory() == fcNormal && exponent == semantics->minExponent && |
| 966 | isSignificandAllZerosExceptMSB(); |
| 967 | } |
| 968 | |
| 969 | unsigned int IEEEFloat::getNumHighBits() const { |
| 970 | const unsigned int PartCount = partCountForBits(bits: semantics->precision); |
| 971 | const unsigned int Bits = PartCount * integerPartWidth; |
| 972 | |
| 973 | // Compute how many bits are used in the final word. |
| 974 | // When precision is just 1, it represents the 'Pth' |
| 975 | // Precision bit and not the actual significand bit. |
| 976 | const unsigned int NumHighBits = (semantics->precision > 1) |
| 977 | ? (Bits - semantics->precision + 1) |
| 978 | : (Bits - semantics->precision); |
| 979 | return NumHighBits; |
| 980 | } |
| 981 | |
| 982 | bool IEEEFloat::isSignificandAllOnes() const { |
| 983 | // Test if the significand excluding the integral bit is all ones. This allows |
| 984 | // us to test for binade boundaries. |
| 985 | const integerPart *Parts = significandParts(); |
| 986 | const unsigned PartCount = partCountForBits(bits: semantics->precision); |
| 987 | for (unsigned i = 0; i < PartCount - 1; i++) |
| 988 | if (~Parts[i]) |
| 989 | return false; |
| 990 | |
| 991 | // Set the unused high bits to all ones when we compare. |
| 992 | const unsigned NumHighBits = getNumHighBits(); |
| 993 | assert(NumHighBits <= integerPartWidth && NumHighBits > 0 && |
| 994 | "Can not have more high bits to fill than integerPartWidth" ); |
| 995 | const integerPart HighBitFill = |
| 996 | ~integerPart(0) << (integerPartWidth - NumHighBits); |
| 997 | if ((semantics->precision <= 1) || (~(Parts[PartCount - 1] | HighBitFill))) |
| 998 | return false; |
| 999 | |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
| 1003 | bool IEEEFloat::isSignificandAllOnesExceptLSB() const { |
| 1004 | // Test if the significand excluding the integral bit is all ones except for |
| 1005 | // the least significant bit. |
| 1006 | const integerPart *Parts = significandParts(); |
| 1007 | |
| 1008 | if (Parts[0] & 1) |
| 1009 | return false; |
| 1010 | |
| 1011 | const unsigned PartCount = partCountForBits(bits: semantics->precision); |
| 1012 | for (unsigned i = 0; i < PartCount - 1; i++) { |
| 1013 | if (~Parts[i] & ~unsigned{!i}) |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | // Set the unused high bits to all ones when we compare. |
| 1018 | const unsigned NumHighBits = getNumHighBits(); |
| 1019 | assert(NumHighBits <= integerPartWidth && NumHighBits > 0 && |
| 1020 | "Can not have more high bits to fill than integerPartWidth" ); |
| 1021 | const integerPart HighBitFill = ~integerPart(0) |
| 1022 | << (integerPartWidth - NumHighBits); |
| 1023 | if (~(Parts[PartCount - 1] | HighBitFill | 0x1)) |
| 1024 | return false; |
| 1025 | |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
| 1029 | bool IEEEFloat::isSignificandAllZeros() const { |
| 1030 | // Test if the significand excluding the integral bit is all zeros. This |
| 1031 | // allows us to test for binade boundaries. |
| 1032 | const integerPart *Parts = significandParts(); |
| 1033 | const unsigned PartCount = partCountForBits(bits: semantics->precision); |
| 1034 | |
| 1035 | for (unsigned i = 0; i < PartCount - 1; i++) |
| 1036 | if (Parts[i]) |
| 1037 | return false; |
| 1038 | |
| 1039 | // Compute how many bits are used in the final word. |
| 1040 | const unsigned NumHighBits = getNumHighBits(); |
| 1041 | assert(NumHighBits < integerPartWidth && "Can not have more high bits to " |
| 1042 | "clear than integerPartWidth" ); |
| 1043 | const integerPart HighBitMask = ~integerPart(0) >> NumHighBits; |
| 1044 | |
| 1045 | if ((semantics->precision > 1) && (Parts[PartCount - 1] & HighBitMask)) |
| 1046 | return false; |
| 1047 | |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
| 1051 | bool IEEEFloat::isSignificandAllZerosExceptMSB() const { |
| 1052 | const integerPart *Parts = significandParts(); |
| 1053 | const unsigned PartCount = partCountForBits(bits: semantics->precision); |
| 1054 | |
| 1055 | for (unsigned i = 0; i < PartCount - 1; i++) { |
| 1056 | if (Parts[i]) |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | const unsigned NumHighBits = getNumHighBits(); |
| 1061 | const integerPart MSBMask = integerPart(1) |
| 1062 | << (integerPartWidth - NumHighBits); |
| 1063 | return ((semantics->precision <= 1) || (Parts[PartCount - 1] == MSBMask)); |
| 1064 | } |
| 1065 | |
| 1066 | bool IEEEFloat::isLargest() const { |
| 1067 | bool IsMaxExp = isFiniteNonZero() && exponent == semantics->maxExponent; |
| 1068 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly && |
| 1069 | semantics->nanEncoding == fltNanEncoding::AllOnes) { |
| 1070 | // The largest number by magnitude in our format will be the floating point |
| 1071 | // number with maximum exponent and with significand that is all ones except |
| 1072 | // the LSB. |
| 1073 | return (IsMaxExp && APFloat::hasSignificand(Sem: *semantics)) |
| 1074 | ? isSignificandAllOnesExceptLSB() |
| 1075 | : IsMaxExp; |
| 1076 | } else { |
| 1077 | // The largest number by magnitude in our format will be the floating point |
| 1078 | // number with maximum exponent and with significand that is all ones. |
| 1079 | return IsMaxExp && isSignificandAllOnes(); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | bool IEEEFloat::isInteger() const { |
| 1084 | // This could be made more efficient; I'm going for obviously correct. |
| 1085 | if (!isFinite()) return false; |
| 1086 | IEEEFloat truncated = *this; |
| 1087 | truncated.roundToIntegral(rmTowardZero); |
| 1088 | return compare(truncated) == cmpEqual; |
| 1089 | } |
| 1090 | |
| 1091 | bool IEEEFloat::bitwiseIsEqual(const IEEEFloat &rhs) const { |
| 1092 | if (this == &rhs) |
| 1093 | return true; |
| 1094 | if (semantics != rhs.semantics || |
| 1095 | category != rhs.category || |
| 1096 | sign != rhs.sign) |
| 1097 | return false; |
| 1098 | if (category==fcZero || category==fcInfinity) |
| 1099 | return true; |
| 1100 | |
| 1101 | if (isFiniteNonZero() && exponent != rhs.exponent) |
| 1102 | return false; |
| 1103 | |
| 1104 | return std::equal(first1: significandParts(), last1: significandParts() + partCount(), |
| 1105 | first2: rhs.significandParts()); |
| 1106 | } |
| 1107 | |
| 1108 | IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, integerPart value) { |
| 1109 | initialize(ourSemantics: &ourSemantics); |
| 1110 | sign = 0; |
| 1111 | category = fcNormal; |
| 1112 | zeroSignificand(); |
| 1113 | exponent = ourSemantics.precision - 1; |
| 1114 | significandParts()[0] = value; |
| 1115 | normalize(rmNearestTiesToEven, lfExactlyZero); |
| 1116 | } |
| 1117 | |
| 1118 | IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics) { |
| 1119 | initialize(ourSemantics: &ourSemantics); |
| 1120 | // The Float8E8MOFNU format does not have a representation |
| 1121 | // for zero. So, use the closest representation instead. |
| 1122 | // Moreover, the all-zero encoding represents a valid |
| 1123 | // normal value (which is the smallestNormalized here). |
| 1124 | // Hence, we call makeSmallestNormalized (where category is |
| 1125 | // 'fcNormal') instead of makeZero (where category is 'fcZero'). |
| 1126 | ourSemantics.hasZero ? makeZero(Neg: false) : makeSmallestNormalized(Negative: false); |
| 1127 | } |
| 1128 | |
| 1129 | // Delegate to the previous constructor, because later copy constructor may |
| 1130 | // actually inspects category, which can't be garbage. |
| 1131 | IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, uninitializedTag tag) |
| 1132 | : IEEEFloat(ourSemantics) {} |
| 1133 | |
| 1134 | IEEEFloat::IEEEFloat(const IEEEFloat &rhs) { |
| 1135 | initialize(ourSemantics: rhs.semantics); |
| 1136 | assign(rhs); |
| 1137 | } |
| 1138 | |
| 1139 | IEEEFloat::IEEEFloat(IEEEFloat &&rhs) : semantics(&APFloatBase::semBogus) { |
| 1140 | *this = std::move(rhs); |
| 1141 | } |
| 1142 | |
| 1143 | IEEEFloat::~IEEEFloat() { freeSignificand(); } |
| 1144 | |
| 1145 | unsigned int IEEEFloat::partCount() const { |
| 1146 | return partCountForBits(bits: semantics->precision + 1); |
| 1147 | } |
| 1148 | |
| 1149 | const APFloat::integerPart *IEEEFloat::significandParts() const { |
| 1150 | return const_cast<IEEEFloat *>(this)->significandParts(); |
| 1151 | } |
| 1152 | |
| 1153 | APFloat::integerPart *IEEEFloat::significandParts() { |
| 1154 | if (partCount() > 1) |
| 1155 | return significand.parts; |
| 1156 | else |
| 1157 | return &significand.part; |
| 1158 | } |
| 1159 | |
| 1160 | void IEEEFloat::zeroSignificand() { |
| 1161 | APInt::tcSet(significandParts(), 0, partCount()); |
| 1162 | } |
| 1163 | |
| 1164 | /* Increment an fcNormal floating point number's significand. */ |
| 1165 | void IEEEFloat::incrementSignificand() { |
| 1166 | integerPart carry; |
| 1167 | |
| 1168 | carry = APInt::tcIncrement(dst: significandParts(), parts: partCount()); |
| 1169 | |
| 1170 | /* Our callers should never cause us to overflow. */ |
| 1171 | assert(carry == 0); |
| 1172 | (void)carry; |
| 1173 | } |
| 1174 | |
| 1175 | /* Add the significand of the RHS. Returns the carry flag. */ |
| 1176 | APFloat::integerPart IEEEFloat::addSignificand(const IEEEFloat &rhs) { |
| 1177 | integerPart *parts; |
| 1178 | |
| 1179 | parts = significandParts(); |
| 1180 | |
| 1181 | assert(semantics == rhs.semantics); |
| 1182 | assert(exponent == rhs.exponent); |
| 1183 | |
| 1184 | return APInt::tcAdd(parts, rhs.significandParts(), carry: 0, partCount()); |
| 1185 | } |
| 1186 | |
| 1187 | /* Subtract the significand of the RHS with a borrow flag. Returns |
| 1188 | the borrow flag. */ |
| 1189 | APFloat::integerPart IEEEFloat::subtractSignificand(const IEEEFloat &rhs, |
| 1190 | integerPart borrow) { |
| 1191 | integerPart *parts; |
| 1192 | |
| 1193 | parts = significandParts(); |
| 1194 | |
| 1195 | assert(semantics == rhs.semantics); |
| 1196 | assert(exponent == rhs.exponent); |
| 1197 | |
| 1198 | return APInt::tcSubtract(parts, rhs.significandParts(), carry: borrow, |
| 1199 | partCount()); |
| 1200 | } |
| 1201 | |
| 1202 | /* Multiply the significand of the RHS. If ADDEND is non-NULL, add it |
| 1203 | on to the full-precision result of the multiplication. Returns the |
| 1204 | lost fraction. */ |
| 1205 | lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs, |
| 1206 | IEEEFloat addend, |
| 1207 | bool ignoreAddend) { |
| 1208 | unsigned int omsb; // One, not zero, based MSB. |
| 1209 | unsigned int partsCount, newPartsCount, precision; |
| 1210 | integerPart *lhsSignificand; |
| 1211 | integerPart scratch[4]; |
| 1212 | integerPart *fullSignificand; |
| 1213 | lostFraction lost_fraction; |
| 1214 | bool ignored; |
| 1215 | |
| 1216 | assert(semantics == rhs.semantics); |
| 1217 | |
| 1218 | precision = semantics->precision; |
| 1219 | |
| 1220 | // Allocate space for twice as many bits as the original significand, plus one |
| 1221 | // extra bit for the addition to overflow into. |
| 1222 | newPartsCount = partCountForBits(bits: precision * 2 + 1); |
| 1223 | |
| 1224 | if (newPartsCount > 4) |
| 1225 | fullSignificand = new integerPart[newPartsCount]; |
| 1226 | else |
| 1227 | fullSignificand = scratch; |
| 1228 | |
| 1229 | lhsSignificand = significandParts(); |
| 1230 | partsCount = partCount(); |
| 1231 | |
| 1232 | APInt::tcFullMultiply(fullSignificand, lhsSignificand, |
| 1233 | rhs.significandParts(), partsCount, partsCount); |
| 1234 | |
| 1235 | lost_fraction = lfExactlyZero; |
| 1236 | omsb = APInt::tcMSB(parts: fullSignificand, n: newPartsCount) + 1; |
| 1237 | exponent += rhs.exponent; |
| 1238 | |
| 1239 | // Assume the operands involved in the multiplication are single-precision |
| 1240 | // FP, and the two multiplicants are: |
| 1241 | // *this = a23 . a22 ... a0 * 2^e1 |
| 1242 | // rhs = b23 . b22 ... b0 * 2^e2 |
| 1243 | // the result of multiplication is: |
| 1244 | // *this = c48 c47 c46 . c45 ... c0 * 2^(e1+e2) |
| 1245 | // Note that there are three significant bits at the left-hand side of the |
| 1246 | // radix point: two for the multiplication, and an overflow bit for the |
| 1247 | // addition (that will always be zero at this point). Move the radix point |
| 1248 | // toward left by two bits, and adjust exponent accordingly. |
| 1249 | exponent += 2; |
| 1250 | |
| 1251 | if (!ignoreAddend && addend.isNonZero()) { |
| 1252 | // The intermediate result of the multiplication has "2 * precision" |
| 1253 | // signicant bit; adjust the addend to be consistent with mul result. |
| 1254 | // |
| 1255 | Significand savedSignificand = significand; |
| 1256 | const fltSemantics *savedSemantics = semantics; |
| 1257 | fltSemantics extendedSemantics; |
| 1258 | opStatus status; |
| 1259 | unsigned int extendedPrecision; |
| 1260 | |
| 1261 | // Normalize our MSB to one below the top bit to allow for overflow. |
| 1262 | extendedPrecision = 2 * precision + 1; |
| 1263 | if (omsb != extendedPrecision - 1) { |
| 1264 | assert(extendedPrecision > omsb); |
| 1265 | APInt::tcShiftLeft(fullSignificand, Words: newPartsCount, |
| 1266 | Count: (extendedPrecision - 1) - omsb); |
| 1267 | exponent -= (extendedPrecision - 1) - omsb; |
| 1268 | } |
| 1269 | |
| 1270 | /* Create new semantics. */ |
| 1271 | extendedSemantics = *semantics; |
| 1272 | extendedSemantics.precision = extendedPrecision; |
| 1273 | |
| 1274 | if (newPartsCount == 1) |
| 1275 | significand.part = fullSignificand[0]; |
| 1276 | else |
| 1277 | significand.parts = fullSignificand; |
| 1278 | semantics = &extendedSemantics; |
| 1279 | |
| 1280 | // Make a copy so we can convert it to the extended semantics. |
| 1281 | // Note that we cannot convert the addend directly, as the extendedSemantics |
| 1282 | // is a local variable (which we take a reference to). |
| 1283 | IEEEFloat extendedAddend(addend); |
| 1284 | status = extendedAddend.convert(extendedSemantics, APFloat::rmTowardZero, |
| 1285 | &ignored); |
| 1286 | assert(status == APFloat::opOK); |
| 1287 | (void)status; |
| 1288 | |
| 1289 | // Shift the significand of the addend right by one bit. This guarantees |
| 1290 | // that the high bit of the significand is zero (same as fullSignificand), |
| 1291 | // so the addition will overflow (if it does overflow at all) into the top bit. |
| 1292 | lost_fraction = extendedAddend.shiftSignificandRight(1); |
| 1293 | assert(lost_fraction == lfExactlyZero && |
| 1294 | "Lost precision while shifting addend for fused-multiply-add." ); |
| 1295 | |
| 1296 | lost_fraction = addOrSubtractSignificand(extendedAddend, subtract: false); |
| 1297 | |
| 1298 | /* Restore our state. */ |
| 1299 | if (newPartsCount == 1) |
| 1300 | fullSignificand[0] = significand.part; |
| 1301 | significand = savedSignificand; |
| 1302 | semantics = savedSemantics; |
| 1303 | |
| 1304 | omsb = APInt::tcMSB(parts: fullSignificand, n: newPartsCount) + 1; |
| 1305 | } |
| 1306 | |
| 1307 | // Convert the result having "2 * precision" significant-bits back to the one |
| 1308 | // having "precision" significant-bits. First, move the radix point from |
| 1309 | // poision "2*precision - 1" to "precision - 1". The exponent need to be |
| 1310 | // adjusted by "2*precision - 1" - "precision - 1" = "precision". |
| 1311 | exponent -= precision + 1; |
| 1312 | |
| 1313 | // In case MSB resides at the left-hand side of radix point, shift the |
| 1314 | // mantissa right by some amount to make sure the MSB reside right before |
| 1315 | // the radix point (i.e. "MSB . rest-significant-bits"). |
| 1316 | // |
| 1317 | // Note that the result is not normalized when "omsb < precision". So, the |
| 1318 | // caller needs to call IEEEFloat::normalize() if normalized value is |
| 1319 | // expected. |
| 1320 | if (omsb > precision) { |
| 1321 | unsigned int bits, significantParts; |
| 1322 | lostFraction lf; |
| 1323 | |
| 1324 | bits = omsb - precision; |
| 1325 | significantParts = partCountForBits(bits: omsb); |
| 1326 | lf = shiftRight(dst: fullSignificand, parts: significantParts, bits); |
| 1327 | lost_fraction = combineLostFractions(moreSignificant: lf, lessSignificant: lost_fraction); |
| 1328 | exponent += bits; |
| 1329 | } |
| 1330 | |
| 1331 | APInt::tcAssign(lhsSignificand, fullSignificand, partsCount); |
| 1332 | |
| 1333 | if (newPartsCount > 4) |
| 1334 | delete [] fullSignificand; |
| 1335 | |
| 1336 | return lost_fraction; |
| 1337 | } |
| 1338 | |
| 1339 | lostFraction IEEEFloat::multiplySignificand(const IEEEFloat &rhs) { |
| 1340 | // When the given semantics has zero, the addend here is a zero. |
| 1341 | // i.e . it belongs to the 'fcZero' category. |
| 1342 | // But when the semantics does not support zero, we need to |
| 1343 | // explicitly convey that this addend should be ignored |
| 1344 | // for multiplication. |
| 1345 | return multiplySignificand(rhs, addend: IEEEFloat(*semantics), ignoreAddend: !semantics->hasZero); |
| 1346 | } |
| 1347 | |
| 1348 | /* Multiply the significands of LHS and RHS to DST. */ |
| 1349 | lostFraction IEEEFloat::divideSignificand(const IEEEFloat &rhs) { |
| 1350 | unsigned int bit, i, partsCount; |
| 1351 | const integerPart *rhsSignificand; |
| 1352 | integerPart *lhsSignificand, *dividend, *divisor; |
| 1353 | integerPart scratch[4]; |
| 1354 | lostFraction lost_fraction; |
| 1355 | |
| 1356 | assert(semantics == rhs.semantics); |
| 1357 | |
| 1358 | lhsSignificand = significandParts(); |
| 1359 | rhsSignificand = rhs.significandParts(); |
| 1360 | partsCount = partCount(); |
| 1361 | |
| 1362 | if (partsCount > 2) |
| 1363 | dividend = new integerPart[partsCount * 2]; |
| 1364 | else |
| 1365 | dividend = scratch; |
| 1366 | |
| 1367 | divisor = dividend + partsCount; |
| 1368 | |
| 1369 | /* Copy the dividend and divisor as they will be modified in-place. */ |
| 1370 | for (i = 0; i < partsCount; i++) { |
| 1371 | dividend[i] = lhsSignificand[i]; |
| 1372 | divisor[i] = rhsSignificand[i]; |
| 1373 | lhsSignificand[i] = 0; |
| 1374 | } |
| 1375 | |
| 1376 | exponent -= rhs.exponent; |
| 1377 | |
| 1378 | unsigned int precision = semantics->precision; |
| 1379 | |
| 1380 | /* Normalize the divisor. */ |
| 1381 | bit = precision - APInt::tcMSB(parts: divisor, n: partsCount) - 1; |
| 1382 | if (bit) { |
| 1383 | exponent += bit; |
| 1384 | APInt::tcShiftLeft(divisor, Words: partsCount, Count: bit); |
| 1385 | } |
| 1386 | |
| 1387 | /* Normalize the dividend. */ |
| 1388 | bit = precision - APInt::tcMSB(parts: dividend, n: partsCount) - 1; |
| 1389 | if (bit) { |
| 1390 | exponent -= bit; |
| 1391 | APInt::tcShiftLeft(dividend, Words: partsCount, Count: bit); |
| 1392 | } |
| 1393 | |
| 1394 | /* Ensure the dividend >= divisor initially for the loop below. |
| 1395 | Incidentally, this means that the division loop below is |
| 1396 | guaranteed to set the integer bit to one. */ |
| 1397 | if (APInt::tcCompare(dividend, divisor, partsCount) < 0) { |
| 1398 | exponent--; |
| 1399 | APInt::tcShiftLeft(dividend, Words: partsCount, Count: 1); |
| 1400 | assert(APInt::tcCompare(dividend, divisor, partsCount) >= 0); |
| 1401 | } |
| 1402 | |
| 1403 | /* Long division. */ |
| 1404 | for (bit = precision; bit; bit -= 1) { |
| 1405 | if (APInt::tcCompare(dividend, divisor, partsCount) >= 0) { |
| 1406 | APInt::tcSubtract(dividend, divisor, carry: 0, partsCount); |
| 1407 | APInt::tcSetBit(lhsSignificand, bit: bit - 1); |
| 1408 | } |
| 1409 | |
| 1410 | APInt::tcShiftLeft(dividend, Words: partsCount, Count: 1); |
| 1411 | } |
| 1412 | |
| 1413 | /* Figure out the lost fraction. */ |
| 1414 | int cmp = APInt::tcCompare(dividend, divisor, partsCount); |
| 1415 | |
| 1416 | if (cmp > 0) |
| 1417 | lost_fraction = lfMoreThanHalf; |
| 1418 | else if (cmp == 0) |
| 1419 | lost_fraction = lfExactlyHalf; |
| 1420 | else if (APInt::tcIsZero(dividend, partsCount)) |
| 1421 | lost_fraction = lfExactlyZero; |
| 1422 | else |
| 1423 | lost_fraction = lfLessThanHalf; |
| 1424 | |
| 1425 | if (partsCount > 2) |
| 1426 | delete [] dividend; |
| 1427 | |
| 1428 | return lost_fraction; |
| 1429 | } |
| 1430 | |
| 1431 | unsigned int IEEEFloat::significandMSB() const { |
| 1432 | return APInt::tcMSB(parts: significandParts(), n: partCount()); |
| 1433 | } |
| 1434 | |
| 1435 | unsigned int IEEEFloat::significandLSB() const { |
| 1436 | return APInt::tcLSB(significandParts(), n: partCount()); |
| 1437 | } |
| 1438 | |
| 1439 | /* Note that a zero result is NOT normalized to fcZero. */ |
| 1440 | lostFraction IEEEFloat::shiftSignificandRight(unsigned int bits) { |
| 1441 | /* Our exponent should not overflow. */ |
| 1442 | assert((ExponentType) (exponent + bits) >= exponent); |
| 1443 | |
| 1444 | exponent += bits; |
| 1445 | |
| 1446 | return shiftRight(dst: significandParts(), parts: partCount(), bits); |
| 1447 | } |
| 1448 | |
| 1449 | /* Shift the significand left BITS bits, subtract BITS from its exponent. */ |
| 1450 | void IEEEFloat::shiftSignificandLeft(unsigned int bits) { |
| 1451 | assert(bits < semantics->precision || |
| 1452 | (semantics->precision == 1 && bits <= 1)); |
| 1453 | |
| 1454 | if (bits) { |
| 1455 | unsigned int partsCount = partCount(); |
| 1456 | |
| 1457 | APInt::tcShiftLeft(significandParts(), Words: partsCount, Count: bits); |
| 1458 | exponent -= bits; |
| 1459 | |
| 1460 | assert(!APInt::tcIsZero(significandParts(), partsCount)); |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | APFloat::cmpResult IEEEFloat::compareAbsoluteValue(const IEEEFloat &rhs) const { |
| 1465 | int compare; |
| 1466 | |
| 1467 | assert(semantics == rhs.semantics); |
| 1468 | assert(isFiniteNonZero()); |
| 1469 | assert(rhs.isFiniteNonZero()); |
| 1470 | |
| 1471 | compare = exponent - rhs.exponent; |
| 1472 | |
| 1473 | /* If exponents are equal, do an unsigned bignum comparison of the |
| 1474 | significands. */ |
| 1475 | if (compare == 0) |
| 1476 | compare = APInt::tcCompare(significandParts(), rhs.significandParts(), |
| 1477 | partCount()); |
| 1478 | |
| 1479 | if (compare > 0) |
| 1480 | return cmpGreaterThan; |
| 1481 | else if (compare < 0) |
| 1482 | return cmpLessThan; |
| 1483 | else |
| 1484 | return cmpEqual; |
| 1485 | } |
| 1486 | |
| 1487 | /* Set the least significant BITS bits of a bignum, clear the |
| 1488 | rest. */ |
| 1489 | static void tcSetLeastSignificantBits(APInt::WordType *dst, unsigned parts, |
| 1490 | unsigned bits) { |
| 1491 | unsigned i = 0; |
| 1492 | while (bits > APInt::APINT_BITS_PER_WORD) { |
| 1493 | dst[i++] = ~(APInt::WordType)0; |
| 1494 | bits -= APInt::APINT_BITS_PER_WORD; |
| 1495 | } |
| 1496 | |
| 1497 | if (bits) |
| 1498 | dst[i++] = ~(APInt::WordType)0 >> (APInt::APINT_BITS_PER_WORD - bits); |
| 1499 | |
| 1500 | while (i < parts) |
| 1501 | dst[i++] = 0; |
| 1502 | } |
| 1503 | |
| 1504 | /* Handle overflow. Sign is preserved. We either become infinity or |
| 1505 | the largest finite number. */ |
| 1506 | APFloat::opStatus IEEEFloat::handleOverflow(roundingMode rounding_mode) { |
| 1507 | if (semantics->nonFiniteBehavior != fltNonfiniteBehavior::FiniteOnly) { |
| 1508 | /* Infinity? */ |
| 1509 | if (rounding_mode == rmNearestTiesToEven || |
| 1510 | rounding_mode == rmNearestTiesToAway || |
| 1511 | (rounding_mode == rmTowardPositive && !sign) || |
| 1512 | (rounding_mode == rmTowardNegative && sign)) { |
| 1513 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) |
| 1514 | makeNaN(SNaN: false, Negative: sign); |
| 1515 | else |
| 1516 | category = fcInfinity; |
| 1517 | return static_cast<opStatus>(opOverflow | opInexact); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | /* Otherwise we become the largest finite number. */ |
| 1522 | category = fcNormal; |
| 1523 | exponent = semantics->maxExponent; |
| 1524 | tcSetLeastSignificantBits(dst: significandParts(), parts: partCount(), |
| 1525 | bits: semantics->precision); |
| 1526 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly && |
| 1527 | semantics->nanEncoding == fltNanEncoding::AllOnes) |
| 1528 | APInt::tcClearBit(significandParts(), bit: 0); |
| 1529 | |
| 1530 | return opInexact; |
| 1531 | } |
| 1532 | |
| 1533 | /* Returns TRUE if, when truncating the current number, with BIT the |
| 1534 | new LSB, with the given lost fraction and rounding mode, the result |
| 1535 | would need to be rounded away from zero (i.e., by increasing the |
| 1536 | signficand). This routine must work for fcZero of both signs, and |
| 1537 | fcNormal numbers. */ |
| 1538 | bool IEEEFloat::roundAwayFromZero(roundingMode rounding_mode, |
| 1539 | lostFraction lost_fraction, |
| 1540 | unsigned int bit) const { |
| 1541 | /* NaNs and infinities should not have lost fractions. */ |
| 1542 | assert(isFiniteNonZero() || category == fcZero); |
| 1543 | |
| 1544 | /* Current callers never pass this so we don't handle it. */ |
| 1545 | assert(lost_fraction != lfExactlyZero); |
| 1546 | |
| 1547 | switch (rounding_mode) { |
| 1548 | case rmNearestTiesToAway: |
| 1549 | return lost_fraction == lfExactlyHalf || lost_fraction == lfMoreThanHalf; |
| 1550 | |
| 1551 | case rmNearestTiesToEven: |
| 1552 | if (lost_fraction == lfMoreThanHalf) |
| 1553 | return true; |
| 1554 | |
| 1555 | /* Our zeroes don't have a significand to test. */ |
| 1556 | if (lost_fraction == lfExactlyHalf && category != fcZero) |
| 1557 | return APInt::tcExtractBit(significandParts(), bit); |
| 1558 | |
| 1559 | return false; |
| 1560 | |
| 1561 | case rmTowardZero: |
| 1562 | return false; |
| 1563 | |
| 1564 | case rmTowardPositive: |
| 1565 | return !sign; |
| 1566 | |
| 1567 | case rmTowardNegative: |
| 1568 | return sign; |
| 1569 | |
| 1570 | default: |
| 1571 | break; |
| 1572 | } |
| 1573 | llvm_unreachable("Invalid rounding mode found" ); |
| 1574 | } |
| 1575 | |
| 1576 | APFloat::opStatus IEEEFloat::normalize(roundingMode rounding_mode, |
| 1577 | lostFraction lost_fraction) { |
| 1578 | unsigned int omsb; /* One, not zero, based MSB. */ |
| 1579 | int exponentChange; |
| 1580 | |
| 1581 | if (!isFiniteNonZero()) |
| 1582 | return opOK; |
| 1583 | |
| 1584 | /* Before rounding normalize the exponent of fcNormal numbers. */ |
| 1585 | omsb = significandMSB() + 1; |
| 1586 | |
| 1587 | // Only skip this `if` if the value is exactly zero. |
| 1588 | if (omsb || lost_fraction != lfExactlyZero) { |
| 1589 | /* OMSB is numbered from 1. We want to place it in the integer |
| 1590 | bit numbered PRECISION if possible, with a compensating change in |
| 1591 | the exponent. */ |
| 1592 | exponentChange = omsb - semantics->precision; |
| 1593 | |
| 1594 | /* If the resulting exponent is too high, overflow according to |
| 1595 | the rounding mode. */ |
| 1596 | if (exponent + exponentChange > semantics->maxExponent) |
| 1597 | return handleOverflow(rounding_mode); |
| 1598 | |
| 1599 | /* Subnormal numbers have exponent minExponent, and their MSB |
| 1600 | is forced based on that. */ |
| 1601 | if (exponent + exponentChange < semantics->minExponent) |
| 1602 | exponentChange = semantics->minExponent - exponent; |
| 1603 | |
| 1604 | /* Shifting left is easy as we don't lose precision. */ |
| 1605 | if (exponentChange < 0) { |
| 1606 | assert(lost_fraction == lfExactlyZero); |
| 1607 | |
| 1608 | shiftSignificandLeft(bits: -exponentChange); |
| 1609 | |
| 1610 | return opOK; |
| 1611 | } |
| 1612 | |
| 1613 | if (exponentChange > 0) { |
| 1614 | lostFraction lf; |
| 1615 | |
| 1616 | /* Shift right and capture any new lost fraction. */ |
| 1617 | lf = shiftSignificandRight(bits: exponentChange); |
| 1618 | |
| 1619 | lost_fraction = combineLostFractions(moreSignificant: lf, lessSignificant: lost_fraction); |
| 1620 | |
| 1621 | /* Keep OMSB up-to-date. */ |
| 1622 | if (omsb > (unsigned) exponentChange) |
| 1623 | omsb -= exponentChange; |
| 1624 | else |
| 1625 | omsb = 0; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | // The all-ones values is an overflow if NaN is all ones. If NaN is |
| 1630 | // represented by negative zero, then it is a valid finite value. |
| 1631 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly && |
| 1632 | semantics->nanEncoding == fltNanEncoding::AllOnes && |
| 1633 | exponent == semantics->maxExponent && isSignificandAllOnes()) |
| 1634 | return handleOverflow(rounding_mode); |
| 1635 | |
| 1636 | /* Now round the number according to rounding_mode given the lost |
| 1637 | fraction. */ |
| 1638 | |
| 1639 | /* As specified in IEEE 754, since we do not trap we do not report |
| 1640 | underflow for exact results. */ |
| 1641 | if (lost_fraction == lfExactlyZero) { |
| 1642 | /* Canonicalize zeroes. */ |
| 1643 | if (omsb == 0) { |
| 1644 | category = fcZero; |
| 1645 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 1646 | sign = false; |
| 1647 | if (!semantics->hasZero) |
| 1648 | makeSmallestNormalized(Negative: false); |
| 1649 | } |
| 1650 | |
| 1651 | return opOK; |
| 1652 | } |
| 1653 | |
| 1654 | /* Increment the significand if we're rounding away from zero. */ |
| 1655 | if (roundAwayFromZero(rounding_mode, lost_fraction, bit: 0)) { |
| 1656 | if (omsb == 0) |
| 1657 | exponent = semantics->minExponent; |
| 1658 | |
| 1659 | incrementSignificand(); |
| 1660 | omsb = significandMSB() + 1; |
| 1661 | |
| 1662 | /* Did the significand increment overflow? */ |
| 1663 | if (omsb == (unsigned) semantics->precision + 1) { |
| 1664 | /* Renormalize by incrementing the exponent and shifting our |
| 1665 | significand right one. However if we already have the |
| 1666 | maximum exponent we overflow to infinity. */ |
| 1667 | if (exponent == semantics->maxExponent) |
| 1668 | // Invoke overflow handling with a rounding mode that will guarantee |
| 1669 | // that the result gets turned into the correct infinity representation. |
| 1670 | // This is needed instead of just setting the category to infinity to |
| 1671 | // account for 8-bit floating point types that have no inf, only NaN. |
| 1672 | return handleOverflow(rounding_mode: sign ? rmTowardNegative : rmTowardPositive); |
| 1673 | |
| 1674 | shiftSignificandRight(bits: 1); |
| 1675 | |
| 1676 | return opInexact; |
| 1677 | } |
| 1678 | |
| 1679 | // The all-ones values is an overflow if NaN is all ones. If NaN is |
| 1680 | // represented by negative zero, then it is a valid finite value. |
| 1681 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly && |
| 1682 | semantics->nanEncoding == fltNanEncoding::AllOnes && |
| 1683 | exponent == semantics->maxExponent && isSignificandAllOnes()) |
| 1684 | return handleOverflow(rounding_mode); |
| 1685 | } |
| 1686 | |
| 1687 | /* The normal case - we were and are not denormal, and any |
| 1688 | significand increment above didn't overflow. */ |
| 1689 | if (omsb == semantics->precision) |
| 1690 | return opInexact; |
| 1691 | |
| 1692 | /* We have a non-zero denormal. */ |
| 1693 | assert(omsb < semantics->precision); |
| 1694 | |
| 1695 | /* Canonicalize zeroes. */ |
| 1696 | if (omsb == 0) { |
| 1697 | category = fcZero; |
| 1698 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 1699 | sign = false; |
| 1700 | // This condition handles the case where the semantics |
| 1701 | // does not have zero but uses the all-zero encoding |
| 1702 | // to represent the smallest normal value. |
| 1703 | if (!semantics->hasZero) |
| 1704 | makeSmallestNormalized(Negative: false); |
| 1705 | } |
| 1706 | |
| 1707 | /* The fcZero case is a denormal that underflowed to zero. */ |
| 1708 | return (opStatus) (opUnderflow | opInexact); |
| 1709 | } |
| 1710 | |
| 1711 | APFloat::opStatus IEEEFloat::addOrSubtractSpecials(const IEEEFloat &rhs, |
| 1712 | bool subtract) { |
| 1713 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
| 1714 | default: |
| 1715 | llvm_unreachable(nullptr); |
| 1716 | |
| 1717 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1718 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1719 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
| 1720 | assign(rhs); |
| 1721 | [[fallthrough]]; |
| 1722 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 1723 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 1724 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 1725 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 1726 | if (isSignaling()) { |
| 1727 | makeQuiet(); |
| 1728 | return opInvalidOp; |
| 1729 | } |
| 1730 | return rhs.isSignaling() ? opInvalidOp : opOK; |
| 1731 | |
| 1732 | case PackCategoriesIntoKey(fcNormal, fcZero): |
| 1733 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 1734 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
| 1735 | return opOK; |
| 1736 | |
| 1737 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
| 1738 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 1739 | category = fcInfinity; |
| 1740 | sign = rhs.sign ^ subtract; |
| 1741 | return opOK; |
| 1742 | |
| 1743 | case PackCategoriesIntoKey(fcZero, fcNormal): |
| 1744 | assign(rhs); |
| 1745 | sign = rhs.sign ^ subtract; |
| 1746 | return opOK; |
| 1747 | |
| 1748 | case PackCategoriesIntoKey(fcZero, fcZero): |
| 1749 | /* Sign depends on rounding mode; handled by caller. */ |
| 1750 | return opOK; |
| 1751 | |
| 1752 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
| 1753 | /* Differently signed infinities can only be validly |
| 1754 | subtracted. */ |
| 1755 | if (((sign ^ rhs.sign)!=0) != subtract) { |
| 1756 | makeNaN(); |
| 1757 | return opInvalidOp; |
| 1758 | } |
| 1759 | |
| 1760 | return opOK; |
| 1761 | |
| 1762 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
| 1763 | return opDivByZero; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | /* Add or subtract two normal numbers. */ |
| 1768 | lostFraction IEEEFloat::addOrSubtractSignificand(const IEEEFloat &rhs, |
| 1769 | bool subtract) { |
| 1770 | integerPart carry = 0; |
| 1771 | lostFraction lost_fraction; |
| 1772 | int bits; |
| 1773 | |
| 1774 | /* Determine if the operation on the absolute values is effectively |
| 1775 | an addition or subtraction. */ |
| 1776 | subtract ^= static_cast<bool>(sign ^ rhs.sign); |
| 1777 | |
| 1778 | /* Are we bigger exponent-wise than the RHS? */ |
| 1779 | bits = exponent - rhs.exponent; |
| 1780 | |
| 1781 | /* Subtraction is more subtle than one might naively expect. */ |
| 1782 | if (subtract) { |
| 1783 | if ((bits < 0) && !semantics->hasSignedRepr) |
| 1784 | llvm_unreachable( |
| 1785 | "This floating point format does not support signed values" ); |
| 1786 | |
| 1787 | IEEEFloat temp_rhs(rhs); |
| 1788 | bool lost_fraction_is_from_rhs = false; |
| 1789 | |
| 1790 | if (bits == 0) |
| 1791 | lost_fraction = lfExactlyZero; |
| 1792 | else if (bits > 0) { |
| 1793 | lost_fraction = temp_rhs.shiftSignificandRight(bits: bits - 1); |
| 1794 | lost_fraction_is_from_rhs = true; |
| 1795 | shiftSignificandLeft(bits: 1); |
| 1796 | } else { |
| 1797 | lost_fraction = shiftSignificandRight(bits: -bits - 1); |
| 1798 | temp_rhs.shiftSignificandLeft(bits: 1); |
| 1799 | } |
| 1800 | |
| 1801 | // Should we reverse the subtraction. |
| 1802 | cmpResult cmp_result = compareAbsoluteValue(rhs: temp_rhs); |
| 1803 | if (cmp_result == cmpLessThan) { |
| 1804 | bool borrow = |
| 1805 | lost_fraction != lfExactlyZero && !lost_fraction_is_from_rhs; |
| 1806 | if (borrow) { |
| 1807 | // The lost fraction is being subtracted, borrow from the significand |
| 1808 | // and invert `lost_fraction`. |
| 1809 | if (lost_fraction == lfLessThanHalf) |
| 1810 | lost_fraction = lfMoreThanHalf; |
| 1811 | else if (lost_fraction == lfMoreThanHalf) |
| 1812 | lost_fraction = lfLessThanHalf; |
| 1813 | } |
| 1814 | carry = temp_rhs.subtractSignificand(rhs: *this, borrow); |
| 1815 | copySignificand(rhs: temp_rhs); |
| 1816 | sign = !sign; |
| 1817 | } else if (cmp_result == cmpGreaterThan) { |
| 1818 | bool borrow = lost_fraction != lfExactlyZero && lost_fraction_is_from_rhs; |
| 1819 | if (borrow) { |
| 1820 | // The lost fraction is being subtracted, borrow from the significand |
| 1821 | // and invert `lost_fraction`. |
| 1822 | if (lost_fraction == lfLessThanHalf) |
| 1823 | lost_fraction = lfMoreThanHalf; |
| 1824 | else if (lost_fraction == lfMoreThanHalf) |
| 1825 | lost_fraction = lfLessThanHalf; |
| 1826 | } |
| 1827 | carry = subtractSignificand(rhs: temp_rhs, borrow); |
| 1828 | } else { // cmpEqual |
| 1829 | zeroSignificand(); |
| 1830 | if (lost_fraction != lfExactlyZero && lost_fraction_is_from_rhs) { |
| 1831 | // rhs is slightly larger due to the lost fraction, flip the sign. |
| 1832 | sign = !sign; |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | /* The code above is intended to ensure that no borrow is |
| 1837 | necessary. */ |
| 1838 | assert(!carry); |
| 1839 | (void)carry; |
| 1840 | } else { |
| 1841 | if (bits > 0) { |
| 1842 | IEEEFloat temp_rhs(rhs); |
| 1843 | |
| 1844 | lost_fraction = temp_rhs.shiftSignificandRight(bits); |
| 1845 | carry = addSignificand(rhs: temp_rhs); |
| 1846 | } else { |
| 1847 | lost_fraction = shiftSignificandRight(bits: -bits); |
| 1848 | carry = addSignificand(rhs); |
| 1849 | } |
| 1850 | |
| 1851 | /* We have a guard bit; generating a carry cannot happen. */ |
| 1852 | assert(!carry); |
| 1853 | (void)carry; |
| 1854 | } |
| 1855 | |
| 1856 | return lost_fraction; |
| 1857 | } |
| 1858 | |
| 1859 | APFloat::opStatus IEEEFloat::multiplySpecials(const IEEEFloat &rhs) { |
| 1860 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
| 1861 | default: |
| 1862 | llvm_unreachable(nullptr); |
| 1863 | |
| 1864 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1865 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1866 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
| 1867 | assign(rhs); |
| 1868 | sign = false; |
| 1869 | [[fallthrough]]; |
| 1870 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 1871 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 1872 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 1873 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 1874 | sign ^= rhs.sign; // restore the original sign |
| 1875 | if (isSignaling()) { |
| 1876 | makeQuiet(); |
| 1877 | return opInvalidOp; |
| 1878 | } |
| 1879 | return rhs.isSignaling() ? opInvalidOp : opOK; |
| 1880 | |
| 1881 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
| 1882 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 1883 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
| 1884 | category = fcInfinity; |
| 1885 | return opOK; |
| 1886 | |
| 1887 | case PackCategoriesIntoKey(fcZero, fcNormal): |
| 1888 | case PackCategoriesIntoKey(fcNormal, fcZero): |
| 1889 | case PackCategoriesIntoKey(fcZero, fcZero): |
| 1890 | category = fcZero; |
| 1891 | return opOK; |
| 1892 | |
| 1893 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 1894 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
| 1895 | makeNaN(); |
| 1896 | return opInvalidOp; |
| 1897 | |
| 1898 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
| 1899 | return opOK; |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | APFloat::opStatus IEEEFloat::divideSpecials(const IEEEFloat &rhs) { |
| 1904 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
| 1905 | default: |
| 1906 | llvm_unreachable(nullptr); |
| 1907 | |
| 1908 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1909 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1910 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
| 1911 | assign(rhs); |
| 1912 | sign = false; |
| 1913 | [[fallthrough]]; |
| 1914 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 1915 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 1916 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 1917 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 1918 | sign ^= rhs.sign; // restore the original sign |
| 1919 | if (isSignaling()) { |
| 1920 | makeQuiet(); |
| 1921 | return opInvalidOp; |
| 1922 | } |
| 1923 | return rhs.isSignaling() ? opInvalidOp : opOK; |
| 1924 | |
| 1925 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
| 1926 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 1927 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 1928 | case PackCategoriesIntoKey(fcZero, fcNormal): |
| 1929 | return opOK; |
| 1930 | |
| 1931 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
| 1932 | category = fcZero; |
| 1933 | return opOK; |
| 1934 | |
| 1935 | case PackCategoriesIntoKey(fcNormal, fcZero): |
| 1936 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) |
| 1937 | makeNaN(SNaN: false, Negative: sign); |
| 1938 | else |
| 1939 | category = fcInfinity; |
| 1940 | return opDivByZero; |
| 1941 | |
| 1942 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
| 1943 | case PackCategoriesIntoKey(fcZero, fcZero): |
| 1944 | makeNaN(); |
| 1945 | return opInvalidOp; |
| 1946 | |
| 1947 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
| 1948 | return opOK; |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | APFloat::opStatus IEEEFloat::modSpecials(const IEEEFloat &rhs) { |
| 1953 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
| 1954 | default: |
| 1955 | llvm_unreachable(nullptr); |
| 1956 | |
| 1957 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1958 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1959 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
| 1960 | assign(rhs); |
| 1961 | [[fallthrough]]; |
| 1962 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 1963 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 1964 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 1965 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 1966 | if (isSignaling()) { |
| 1967 | makeQuiet(); |
| 1968 | return opInvalidOp; |
| 1969 | } |
| 1970 | return rhs.isSignaling() ? opInvalidOp : opOK; |
| 1971 | |
| 1972 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 1973 | case PackCategoriesIntoKey(fcZero, fcNormal): |
| 1974 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
| 1975 | return opOK; |
| 1976 | |
| 1977 | case PackCategoriesIntoKey(fcNormal, fcZero): |
| 1978 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
| 1979 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 1980 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
| 1981 | case PackCategoriesIntoKey(fcZero, fcZero): |
| 1982 | makeNaN(); |
| 1983 | return opInvalidOp; |
| 1984 | |
| 1985 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
| 1986 | return opOK; |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | APFloat::opStatus IEEEFloat::remainderSpecials(const IEEEFloat &rhs) { |
| 1991 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
| 1992 | default: |
| 1993 | llvm_unreachable(nullptr); |
| 1994 | |
| 1995 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 1996 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 1997 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
| 1998 | assign(rhs); |
| 1999 | [[fallthrough]]; |
| 2000 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 2001 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 2002 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 2003 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 2004 | if (isSignaling()) { |
| 2005 | makeQuiet(); |
| 2006 | return opInvalidOp; |
| 2007 | } |
| 2008 | return rhs.isSignaling() ? opInvalidOp : opOK; |
| 2009 | |
| 2010 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 2011 | case PackCategoriesIntoKey(fcZero, fcNormal): |
| 2012 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
| 2013 | return opOK; |
| 2014 | |
| 2015 | case PackCategoriesIntoKey(fcNormal, fcZero): |
| 2016 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
| 2017 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 2018 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
| 2019 | case PackCategoriesIntoKey(fcZero, fcZero): |
| 2020 | makeNaN(); |
| 2021 | return opInvalidOp; |
| 2022 | |
| 2023 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
| 2024 | return opDivByZero; // fake status, indicating this is not a special case |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | /* Change sign. */ |
| 2029 | void IEEEFloat::changeSign() { |
| 2030 | // With NaN-as-negative-zero, neither NaN or negative zero can change |
| 2031 | // their signs. |
| 2032 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero && |
| 2033 | (isZero() || isNaN())) |
| 2034 | return; |
| 2035 | /* Look mummy, this one's easy. */ |
| 2036 | sign = !sign; |
| 2037 | } |
| 2038 | |
| 2039 | /* Normalized addition or subtraction. */ |
| 2040 | APFloat::opStatus IEEEFloat::addOrSubtract(const IEEEFloat &rhs, |
| 2041 | roundingMode rounding_mode, |
| 2042 | bool subtract) { |
| 2043 | opStatus fs; |
| 2044 | |
| 2045 | fs = addOrSubtractSpecials(rhs, subtract); |
| 2046 | |
| 2047 | /* This return code means it was not a simple case. */ |
| 2048 | if (fs == opDivByZero) { |
| 2049 | lostFraction lost_fraction; |
| 2050 | |
| 2051 | lost_fraction = addOrSubtractSignificand(rhs, subtract); |
| 2052 | fs = normalize(rounding_mode, lost_fraction); |
| 2053 | |
| 2054 | /* Can only be zero if we lost no fraction. */ |
| 2055 | assert(category != fcZero || lost_fraction == lfExactlyZero); |
| 2056 | } |
| 2057 | |
| 2058 | /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a |
| 2059 | positive zero unless rounding to minus infinity, except that |
| 2060 | adding two like-signed zeroes gives that zero. */ |
| 2061 | if (category == fcZero) { |
| 2062 | if (rhs.category != fcZero || (sign == rhs.sign) == subtract) |
| 2063 | sign = (rounding_mode == rmTowardNegative); |
| 2064 | // NaN-in-negative-zero means zeros need to be normalized to +0. |
| 2065 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 2066 | sign = false; |
| 2067 | } |
| 2068 | |
| 2069 | return fs; |
| 2070 | } |
| 2071 | |
| 2072 | /* Normalized addition. */ |
| 2073 | APFloat::opStatus IEEEFloat::add(const IEEEFloat &rhs, |
| 2074 | roundingMode rounding_mode) { |
| 2075 | return addOrSubtract(rhs, rounding_mode, subtract: false); |
| 2076 | } |
| 2077 | |
| 2078 | /* Normalized subtraction. */ |
| 2079 | APFloat::opStatus IEEEFloat::subtract(const IEEEFloat &rhs, |
| 2080 | roundingMode rounding_mode) { |
| 2081 | return addOrSubtract(rhs, rounding_mode, subtract: true); |
| 2082 | } |
| 2083 | |
| 2084 | /* Normalized multiply. */ |
| 2085 | APFloat::opStatus IEEEFloat::multiply(const IEEEFloat &rhs, |
| 2086 | roundingMode rounding_mode) { |
| 2087 | opStatus fs; |
| 2088 | |
| 2089 | sign ^= rhs.sign; |
| 2090 | fs = multiplySpecials(rhs); |
| 2091 | |
| 2092 | if (isZero() && semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 2093 | sign = false; |
| 2094 | if (isFiniteNonZero()) { |
| 2095 | lostFraction lost_fraction = multiplySignificand(rhs); |
| 2096 | fs = normalize(rounding_mode, lost_fraction); |
| 2097 | if (lost_fraction != lfExactlyZero) |
| 2098 | fs = (opStatus) (fs | opInexact); |
| 2099 | } |
| 2100 | |
| 2101 | return fs; |
| 2102 | } |
| 2103 | |
| 2104 | /* Normalized divide. */ |
| 2105 | APFloat::opStatus IEEEFloat::divide(const IEEEFloat &rhs, |
| 2106 | roundingMode rounding_mode) { |
| 2107 | opStatus fs; |
| 2108 | |
| 2109 | sign ^= rhs.sign; |
| 2110 | fs = divideSpecials(rhs); |
| 2111 | |
| 2112 | if (isZero() && semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 2113 | sign = false; |
| 2114 | if (isFiniteNonZero()) { |
| 2115 | lostFraction lost_fraction = divideSignificand(rhs); |
| 2116 | fs = normalize(rounding_mode, lost_fraction); |
| 2117 | if (lost_fraction != lfExactlyZero) |
| 2118 | fs = (opStatus) (fs | opInexact); |
| 2119 | } |
| 2120 | |
| 2121 | return fs; |
| 2122 | } |
| 2123 | |
| 2124 | /* Normalized remainder. */ |
| 2125 | APFloat::opStatus IEEEFloat::remainder(const IEEEFloat &rhs) { |
| 2126 | opStatus fs; |
| 2127 | unsigned int origSign = sign; |
| 2128 | |
| 2129 | // First handle the special cases. |
| 2130 | fs = remainderSpecials(rhs); |
| 2131 | if (fs != opDivByZero) |
| 2132 | return fs; |
| 2133 | |
| 2134 | fs = opOK; |
| 2135 | |
| 2136 | // Make sure the current value is less than twice the denom. If the addition |
| 2137 | // did not succeed (an overflow has happened), which means that the finite |
| 2138 | // value we currently posses must be less than twice the denom (as we are |
| 2139 | // using the same semantics). |
| 2140 | IEEEFloat P2 = rhs; |
| 2141 | if (P2.add(rhs, rounding_mode: rmNearestTiesToEven) == opOK) { |
| 2142 | fs = mod(P2); |
| 2143 | assert(fs == opOK); |
| 2144 | } |
| 2145 | |
| 2146 | // Lets work with absolute numbers. |
| 2147 | IEEEFloat P = rhs; |
| 2148 | P.sign = false; |
| 2149 | sign = false; |
| 2150 | |
| 2151 | // |
| 2152 | // To calculate the remainder we use the following scheme. |
| 2153 | // |
| 2154 | // The remainder is defained as follows: |
| 2155 | // |
| 2156 | // remainder = numer - rquot * denom = x - r * p |
| 2157 | // |
| 2158 | // Where r is the result of: x/p, rounded toward the nearest integral value |
| 2159 | // (with halfway cases rounded toward the even number). |
| 2160 | // |
| 2161 | // Currently, (after x mod 2p): |
| 2162 | // r is the number of 2p's present inside x, which is inherently, an even |
| 2163 | // number of p's. |
| 2164 | // |
| 2165 | // We may split the remaining calculation into 4 options: |
| 2166 | // - if x < 0.5p then we round to the nearest number with is 0, and are done. |
| 2167 | // - if x == 0.5p then we round to the nearest even number which is 0, and we |
| 2168 | // are done as well. |
| 2169 | // - if 0.5p < x < p then we round to nearest number which is 1, and we have |
| 2170 | // to subtract 1p at least once. |
| 2171 | // - if x >= p then we must subtract p at least once, as x must be a |
| 2172 | // remainder. |
| 2173 | // |
| 2174 | // By now, we were done, or we added 1 to r, which in turn, now an odd number. |
| 2175 | // |
| 2176 | // We can now split the remaining calculation to the following 3 options: |
| 2177 | // - if x < 0.5p then we round to the nearest number with is 0, and are done. |
| 2178 | // - if x == 0.5p then we round to the nearest even number. As r is odd, we |
| 2179 | // must round up to the next even number. so we must subtract p once more. |
| 2180 | // - if x > 0.5p (and inherently x < p) then we must round r up to the next |
| 2181 | // integral, and subtract p once more. |
| 2182 | // |
| 2183 | |
| 2184 | // Extend the semantics to prevent an overflow/underflow or inexact result. |
| 2185 | bool losesInfo; |
| 2186 | fltSemantics extendedSemantics = *semantics; |
| 2187 | extendedSemantics.maxExponent++; |
| 2188 | extendedSemantics.minExponent--; |
| 2189 | extendedSemantics.precision += 2; |
| 2190 | |
| 2191 | IEEEFloat VEx = *this; |
| 2192 | fs = VEx.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); |
| 2193 | assert(fs == opOK && !losesInfo); |
| 2194 | IEEEFloat PEx = P; |
| 2195 | fs = PEx.convert(extendedSemantics, rmNearestTiesToEven, &losesInfo); |
| 2196 | assert(fs == opOK && !losesInfo); |
| 2197 | |
| 2198 | // It is simpler to work with 2x instead of 0.5p, and we do not need to lose |
| 2199 | // any fraction. |
| 2200 | fs = VEx.add(rhs: VEx, rounding_mode: rmNearestTiesToEven); |
| 2201 | assert(fs == opOK); |
| 2202 | |
| 2203 | if (VEx.compare(PEx) == cmpGreaterThan) { |
| 2204 | fs = subtract(rhs: P, rounding_mode: rmNearestTiesToEven); |
| 2205 | assert(fs == opOK); |
| 2206 | |
| 2207 | // Make VEx = this.add(this), but because we have different semantics, we do |
| 2208 | // not want to `convert` again, so we just subtract PEx twice (which equals |
| 2209 | // to the desired value). |
| 2210 | fs = VEx.subtract(rhs: PEx, rounding_mode: rmNearestTiesToEven); |
| 2211 | assert(fs == opOK); |
| 2212 | fs = VEx.subtract(rhs: PEx, rounding_mode: rmNearestTiesToEven); |
| 2213 | assert(fs == opOK); |
| 2214 | |
| 2215 | cmpResult result = VEx.compare(PEx); |
| 2216 | if (result == cmpGreaterThan || result == cmpEqual) { |
| 2217 | fs = subtract(rhs: P, rounding_mode: rmNearestTiesToEven); |
| 2218 | assert(fs == opOK); |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | if (isZero()) { |
| 2223 | sign = origSign; // IEEE754 requires this |
| 2224 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 2225 | // But some 8-bit floats only have positive 0. |
| 2226 | sign = false; |
| 2227 | } |
| 2228 | |
| 2229 | else |
| 2230 | sign ^= origSign; |
| 2231 | return fs; |
| 2232 | } |
| 2233 | |
| 2234 | /* Normalized llvm frem (C fmod). */ |
| 2235 | APFloat::opStatus IEEEFloat::mod(const IEEEFloat &rhs) { |
| 2236 | opStatus fs; |
| 2237 | fs = modSpecials(rhs); |
| 2238 | unsigned int origSign = sign; |
| 2239 | |
| 2240 | while (isFiniteNonZero() && rhs.isFiniteNonZero() && |
| 2241 | compareAbsoluteValue(rhs) != cmpLessThan) { |
| 2242 | int Exp = ilogb(Arg: *this) - ilogb(Arg: rhs); |
| 2243 | IEEEFloat V = scalbn(X: rhs, Exp, rmNearestTiesToEven); |
| 2244 | // V can overflow to NaN with fltNonfiniteBehavior::NanOnly, so explicitly |
| 2245 | // check for it. |
| 2246 | if (V.isNaN() || compareAbsoluteValue(rhs: V) == cmpLessThan) |
| 2247 | V = scalbn(X: rhs, Exp: Exp - 1, rmNearestTiesToEven); |
| 2248 | V.sign = sign; |
| 2249 | |
| 2250 | fs = subtract(rhs: V, rounding_mode: rmNearestTiesToEven); |
| 2251 | |
| 2252 | // When the semantics supports zero, this loop's |
| 2253 | // exit-condition is handled by the 'isFiniteNonZero' |
| 2254 | // category check above. However, when the semantics |
| 2255 | // does not have 'fcZero' and we have reached the |
| 2256 | // minimum possible value, (and any further subtract |
| 2257 | // will underflow to the same value) explicitly |
| 2258 | // provide an exit-path here. |
| 2259 | if (!semantics->hasZero && this->isSmallest()) |
| 2260 | break; |
| 2261 | |
| 2262 | assert(fs==opOK); |
| 2263 | } |
| 2264 | if (isZero()) { |
| 2265 | sign = origSign; // fmod requires this |
| 2266 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 2267 | sign = false; |
| 2268 | } |
| 2269 | return fs; |
| 2270 | } |
| 2271 | |
| 2272 | /* Normalized fused-multiply-add. */ |
| 2273 | APFloat::opStatus IEEEFloat::fusedMultiplyAdd(const IEEEFloat &multiplicand, |
| 2274 | const IEEEFloat &addend, |
| 2275 | roundingMode rounding_mode) { |
| 2276 | opStatus fs; |
| 2277 | |
| 2278 | /* Post-multiplication sign, before addition. */ |
| 2279 | sign ^= multiplicand.sign; |
| 2280 | |
| 2281 | /* If and only if all arguments are normal do we need to do an |
| 2282 | extended-precision calculation. */ |
| 2283 | if (isFiniteNonZero() && |
| 2284 | multiplicand.isFiniteNonZero() && |
| 2285 | addend.isFinite()) { |
| 2286 | lostFraction lost_fraction; |
| 2287 | |
| 2288 | lost_fraction = multiplySignificand(rhs: multiplicand, addend); |
| 2289 | fs = normalize(rounding_mode, lost_fraction); |
| 2290 | if (lost_fraction != lfExactlyZero) |
| 2291 | fs = (opStatus) (fs | opInexact); |
| 2292 | |
| 2293 | /* If two numbers add (exactly) to zero, IEEE 754 decrees it is a |
| 2294 | positive zero unless rounding to minus infinity, except that |
| 2295 | adding two like-signed zeroes gives that zero. */ |
| 2296 | if (category == fcZero && !(fs & opUnderflow) && sign != addend.sign) { |
| 2297 | sign = (rounding_mode == rmTowardNegative); |
| 2298 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 2299 | sign = false; |
| 2300 | } |
| 2301 | } else { |
| 2302 | fs = multiplySpecials(rhs: multiplicand); |
| 2303 | |
| 2304 | /* FS can only be opOK or opInvalidOp. There is no more work |
| 2305 | to do in the latter case. The IEEE-754R standard says it is |
| 2306 | implementation-defined in this case whether, if ADDEND is a |
| 2307 | quiet NaN, we raise invalid op; this implementation does so. |
| 2308 | |
| 2309 | If we need to do the addition we can do so with normal |
| 2310 | precision. */ |
| 2311 | if (fs == opOK) |
| 2312 | fs = addOrSubtract(rhs: addend, rounding_mode, subtract: false); |
| 2313 | } |
| 2314 | |
| 2315 | return fs; |
| 2316 | } |
| 2317 | |
| 2318 | /* Rounding-mode correct round to integral value. */ |
| 2319 | APFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) { |
| 2320 | opStatus fs; |
| 2321 | |
| 2322 | if (isInfinity()) |
| 2323 | // [IEEE Std 754-2008 6.1]: |
| 2324 | // The behavior of infinity in floating-point arithmetic is derived from the |
| 2325 | // limiting cases of real arithmetic with operands of arbitrarily |
| 2326 | // large magnitude, when such a limit exists. |
| 2327 | // ... |
| 2328 | // Operations on infinite operands are usually exact and therefore signal no |
| 2329 | // exceptions ... |
| 2330 | return opOK; |
| 2331 | |
| 2332 | if (isNaN()) { |
| 2333 | if (isSignaling()) { |
| 2334 | // [IEEE Std 754-2008 6.2]: |
| 2335 | // Under default exception handling, any operation signaling an invalid |
| 2336 | // operation exception and for which a floating-point result is to be |
| 2337 | // delivered shall deliver a quiet NaN. |
| 2338 | makeQuiet(); |
| 2339 | // [IEEE Std 754-2008 6.2]: |
| 2340 | // Signaling NaNs shall be reserved operands that, under default exception |
| 2341 | // handling, signal the invalid operation exception(see 7.2) for every |
| 2342 | // general-computational and signaling-computational operation except for |
| 2343 | // the conversions described in 5.12. |
| 2344 | return opInvalidOp; |
| 2345 | } else { |
| 2346 | // [IEEE Std 754-2008 6.2]: |
| 2347 | // For an operation with quiet NaN inputs, other than maximum and minimum |
| 2348 | // operations, if a floating-point result is to be delivered the result |
| 2349 | // shall be a quiet NaN which should be one of the input NaNs. |
| 2350 | // ... |
| 2351 | // Every general-computational and quiet-computational operation involving |
| 2352 | // one or more input NaNs, none of them signaling, shall signal no |
| 2353 | // exception, except fusedMultiplyAdd might signal the invalid operation |
| 2354 | // exception(see 7.2). |
| 2355 | return opOK; |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | if (isZero()) { |
| 2360 | // [IEEE Std 754-2008 6.3]: |
| 2361 | // ... the sign of the result of conversions, the quantize operation, the |
| 2362 | // roundToIntegral operations, and the roundToIntegralExact(see 5.3.1) is |
| 2363 | // the sign of the first or only operand. |
| 2364 | return opOK; |
| 2365 | } |
| 2366 | |
| 2367 | // If the exponent is large enough, we know that this value is already |
| 2368 | // integral, and the arithmetic below would potentially cause it to saturate |
| 2369 | // to +/-Inf. Bail out early instead. |
| 2370 | if (exponent + 1 >= (int)APFloat::semanticsPrecision(semantics: *semantics)) |
| 2371 | return opOK; |
| 2372 | |
| 2373 | // The algorithm here is quite simple: we add 2^(p-1), where p is the |
| 2374 | // precision of our format, and then subtract it back off again. The choice |
| 2375 | // of rounding modes for the addition/subtraction determines the rounding mode |
| 2376 | // for our integral rounding as well. |
| 2377 | // NOTE: When the input value is negative, we do subtraction followed by |
| 2378 | // addition instead. |
| 2379 | APInt IntegerConstant(NextPowerOf2(A: APFloat::semanticsPrecision(semantics: *semantics)), |
| 2380 | 1); |
| 2381 | IntegerConstant <<= APFloat::semanticsPrecision(semantics: *semantics) - 1; |
| 2382 | IEEEFloat MagicConstant(*semantics); |
| 2383 | fs = MagicConstant.convertFromAPInt(IntegerConstant, false, |
| 2384 | rmNearestTiesToEven); |
| 2385 | assert(fs == opOK); |
| 2386 | MagicConstant.sign = sign; |
| 2387 | |
| 2388 | // Preserve the input sign so that we can handle the case of zero result |
| 2389 | // correctly. |
| 2390 | bool inputSign = isNegative(); |
| 2391 | |
| 2392 | fs = add(rhs: MagicConstant, rounding_mode); |
| 2393 | |
| 2394 | // Current value and 'MagicConstant' are both integers, so the result of the |
| 2395 | // subtraction is always exact according to Sterbenz' lemma. |
| 2396 | subtract(rhs: MagicConstant, rounding_mode); |
| 2397 | |
| 2398 | // Restore the input sign. |
| 2399 | if (inputSign != isNegative()) |
| 2400 | changeSign(); |
| 2401 | |
| 2402 | return fs; |
| 2403 | } |
| 2404 | |
| 2405 | /* Comparison requires normalized numbers. */ |
| 2406 | APFloat::cmpResult IEEEFloat::compare(const IEEEFloat &rhs) const { |
| 2407 | cmpResult result; |
| 2408 | |
| 2409 | assert(semantics == rhs.semantics); |
| 2410 | |
| 2411 | switch (PackCategoriesIntoKey(category, rhs.category)) { |
| 2412 | default: |
| 2413 | llvm_unreachable(nullptr); |
| 2414 | |
| 2415 | case PackCategoriesIntoKey(fcNaN, fcZero): |
| 2416 | case PackCategoriesIntoKey(fcNaN, fcNormal): |
| 2417 | case PackCategoriesIntoKey(fcNaN, fcInfinity): |
| 2418 | case PackCategoriesIntoKey(fcNaN, fcNaN): |
| 2419 | case PackCategoriesIntoKey(fcZero, fcNaN): |
| 2420 | case PackCategoriesIntoKey(fcNormal, fcNaN): |
| 2421 | case PackCategoriesIntoKey(fcInfinity, fcNaN): |
| 2422 | return cmpUnordered; |
| 2423 | |
| 2424 | case PackCategoriesIntoKey(fcInfinity, fcNormal): |
| 2425 | case PackCategoriesIntoKey(fcInfinity, fcZero): |
| 2426 | case PackCategoriesIntoKey(fcNormal, fcZero): |
| 2427 | if (sign) |
| 2428 | return cmpLessThan; |
| 2429 | else |
| 2430 | return cmpGreaterThan; |
| 2431 | |
| 2432 | case PackCategoriesIntoKey(fcNormal, fcInfinity): |
| 2433 | case PackCategoriesIntoKey(fcZero, fcInfinity): |
| 2434 | case PackCategoriesIntoKey(fcZero, fcNormal): |
| 2435 | if (rhs.sign) |
| 2436 | return cmpGreaterThan; |
| 2437 | else |
| 2438 | return cmpLessThan; |
| 2439 | |
| 2440 | case PackCategoriesIntoKey(fcInfinity, fcInfinity): |
| 2441 | if (sign == rhs.sign) |
| 2442 | return cmpEqual; |
| 2443 | else if (sign) |
| 2444 | return cmpLessThan; |
| 2445 | else |
| 2446 | return cmpGreaterThan; |
| 2447 | |
| 2448 | case PackCategoriesIntoKey(fcZero, fcZero): |
| 2449 | return cmpEqual; |
| 2450 | |
| 2451 | case PackCategoriesIntoKey(fcNormal, fcNormal): |
| 2452 | break; |
| 2453 | } |
| 2454 | |
| 2455 | /* Two normal numbers. Do they have the same sign? */ |
| 2456 | if (sign != rhs.sign) { |
| 2457 | if (sign) |
| 2458 | result = cmpLessThan; |
| 2459 | else |
| 2460 | result = cmpGreaterThan; |
| 2461 | } else { |
| 2462 | /* Compare absolute values; invert result if negative. */ |
| 2463 | result = compareAbsoluteValue(rhs); |
| 2464 | |
| 2465 | if (sign) { |
| 2466 | if (result == cmpLessThan) |
| 2467 | result = cmpGreaterThan; |
| 2468 | else if (result == cmpGreaterThan) |
| 2469 | result = cmpLessThan; |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | return result; |
| 2474 | } |
| 2475 | |
| 2476 | /// IEEEFloat::convert - convert a value of one floating point type to another. |
| 2477 | /// The return value corresponds to the IEEE754 exceptions. *losesInfo |
| 2478 | /// records whether the transformation lost information, i.e. whether |
| 2479 | /// converting the result back to the original type will produce the |
| 2480 | /// original value (this is almost the same as return value==fsOK, but there |
| 2481 | /// are edge cases where this is not so). |
| 2482 | |
| 2483 | APFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics, |
| 2484 | roundingMode rounding_mode, |
| 2485 | bool *losesInfo) { |
| 2486 | lostFraction lostFraction; |
| 2487 | unsigned int newPartCount, oldPartCount; |
| 2488 | opStatus fs; |
| 2489 | int shift; |
| 2490 | const fltSemantics &fromSemantics = *semantics; |
| 2491 | bool is_signaling = isSignaling(); |
| 2492 | |
| 2493 | lostFraction = lfExactlyZero; |
| 2494 | newPartCount = partCountForBits(bits: toSemantics.precision + 1); |
| 2495 | oldPartCount = partCount(); |
| 2496 | shift = toSemantics.precision - fromSemantics.precision; |
| 2497 | |
| 2498 | bool X86SpecialNan = false; |
| 2499 | if (&fromSemantics == &APFloatBase::semX87DoubleExtended && |
| 2500 | &toSemantics != &APFloatBase::semX87DoubleExtended && category == fcNaN && |
| 2501 | (!(*significandParts() & 0x8000000000000000ULL) || |
| 2502 | !(*significandParts() & 0x4000000000000000ULL))) { |
| 2503 | // x86 has some unusual NaNs which cannot be represented in any other |
| 2504 | // format; note them here. |
| 2505 | X86SpecialNan = true; |
| 2506 | } |
| 2507 | |
| 2508 | // If this is a truncation of a denormal number, and the target semantics |
| 2509 | // has larger exponent range than the source semantics (this can happen |
| 2510 | // when truncating from PowerPC double-double to double format), the |
| 2511 | // right shift could lose result mantissa bits. Adjust exponent instead |
| 2512 | // of performing excessive shift. |
| 2513 | // Also do a similar trick in case shifting denormal would produce zero |
| 2514 | // significand as this case isn't handled correctly by normalize. |
| 2515 | if (shift < 0 && isFiniteNonZero()) { |
| 2516 | int omsb = significandMSB() + 1; |
| 2517 | int exponentChange = omsb - fromSemantics.precision; |
| 2518 | if (exponent + exponentChange < toSemantics.minExponent) |
| 2519 | exponentChange = toSemantics.minExponent - exponent; |
| 2520 | exponentChange = std::max(a: exponentChange, b: shift); |
| 2521 | if (exponentChange < 0) { |
| 2522 | shift -= exponentChange; |
| 2523 | exponent += exponentChange; |
| 2524 | } else if (omsb <= -shift) { |
| 2525 | exponentChange = omsb + shift - 1; // leave at least one bit set |
| 2526 | shift -= exponentChange; |
| 2527 | exponent += exponentChange; |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | // If this is a truncation, perform the shift before we narrow the storage. |
| 2532 | if (shift < 0 && (isFiniteNonZero() || |
| 2533 | (category == fcNaN && semantics->nonFiniteBehavior != |
| 2534 | fltNonfiniteBehavior::NanOnly))) |
| 2535 | lostFraction = shiftRight(dst: significandParts(), parts: oldPartCount, bits: -shift); |
| 2536 | |
| 2537 | // Fix the storage so it can hold to new value. |
| 2538 | if (newPartCount > oldPartCount) { |
| 2539 | // The new type requires more storage; make it available. |
| 2540 | integerPart *newParts; |
| 2541 | newParts = new integerPart[newPartCount]; |
| 2542 | APInt::tcSet(newParts, 0, newPartCount); |
| 2543 | if (isFiniteNonZero() || category==fcNaN) |
| 2544 | APInt::tcAssign(newParts, significandParts(), oldPartCount); |
| 2545 | freeSignificand(); |
| 2546 | significand.parts = newParts; |
| 2547 | } else if (newPartCount == 1 && oldPartCount != 1) { |
| 2548 | // Switch to built-in storage for a single part. |
| 2549 | integerPart newPart = 0; |
| 2550 | if (isFiniteNonZero() || category==fcNaN) |
| 2551 | newPart = significandParts()[0]; |
| 2552 | freeSignificand(); |
| 2553 | significand.part = newPart; |
| 2554 | } |
| 2555 | |
| 2556 | // Now that we have the right storage, switch the semantics. |
| 2557 | semantics = &toSemantics; |
| 2558 | |
| 2559 | // If this is an extension, perform the shift now that the storage is |
| 2560 | // available. |
| 2561 | if (shift > 0 && (isFiniteNonZero() || category==fcNaN)) |
| 2562 | APInt::tcShiftLeft(significandParts(), Words: newPartCount, Count: shift); |
| 2563 | |
| 2564 | if (isFiniteNonZero()) { |
| 2565 | fs = normalize(rounding_mode, lost_fraction: lostFraction); |
| 2566 | *losesInfo = (fs != opOK); |
| 2567 | } else if (category == fcNaN) { |
| 2568 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { |
| 2569 | *losesInfo = |
| 2570 | fromSemantics.nonFiniteBehavior != fltNonfiniteBehavior::NanOnly; |
| 2571 | makeNaN(SNaN: false, Negative: sign); |
| 2572 | return is_signaling ? opInvalidOp : opOK; |
| 2573 | } |
| 2574 | |
| 2575 | // If NaN is negative zero, we need to create a new NaN to avoid converting |
| 2576 | // NaN to -Inf. |
| 2577 | if (fromSemantics.nanEncoding == fltNanEncoding::NegativeZero && |
| 2578 | semantics->nanEncoding != fltNanEncoding::NegativeZero) |
| 2579 | makeNaN(SNaN: false, Negative: false); |
| 2580 | |
| 2581 | *losesInfo = lostFraction != lfExactlyZero || X86SpecialNan; |
| 2582 | |
| 2583 | // For x87 extended precision, we want to make a NaN, not a special NaN if |
| 2584 | // the input wasn't special either. |
| 2585 | if (!X86SpecialNan && semantics == &APFloatBase::semX87DoubleExtended) |
| 2586 | APInt::tcSetBit(significandParts(), bit: semantics->precision - 1); |
| 2587 | |
| 2588 | // Convert of sNaN creates qNaN and raises an exception (invalid op). |
| 2589 | // This also guarantees that a sNaN does not become Inf on a truncation |
| 2590 | // that loses all payload bits. |
| 2591 | if (is_signaling) { |
| 2592 | makeQuiet(); |
| 2593 | fs = opInvalidOp; |
| 2594 | } else { |
| 2595 | fs = opOK; |
| 2596 | } |
| 2597 | } else if (category == fcInfinity && |
| 2598 | semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { |
| 2599 | makeNaN(SNaN: false, Negative: sign); |
| 2600 | *losesInfo = true; |
| 2601 | fs = opInexact; |
| 2602 | } else if (category == fcZero && |
| 2603 | semantics->nanEncoding == fltNanEncoding::NegativeZero) { |
| 2604 | // Negative zero loses info, but positive zero doesn't. |
| 2605 | *losesInfo = |
| 2606 | fromSemantics.nanEncoding != fltNanEncoding::NegativeZero && sign; |
| 2607 | fs = *losesInfo ? opInexact : opOK; |
| 2608 | // NaN is negative zero means -0 -> +0, which can lose information |
| 2609 | sign = false; |
| 2610 | } else { |
| 2611 | *losesInfo = false; |
| 2612 | fs = opOK; |
| 2613 | } |
| 2614 | |
| 2615 | if (category == fcZero && !semantics->hasZero) |
| 2616 | makeSmallestNormalized(Negative: false); |
| 2617 | return fs; |
| 2618 | } |
| 2619 | |
| 2620 | /* Convert a floating point number to an integer according to the |
| 2621 | rounding mode. If the rounded integer value is out of range this |
| 2622 | returns an invalid operation exception and the contents of the |
| 2623 | destination parts are unspecified. If the rounded value is in |
| 2624 | range but the floating point number is not the exact integer, the C |
| 2625 | standard doesn't require an inexact exception to be raised. IEEE |
| 2626 | 854 does require it so we do that. |
| 2627 | |
| 2628 | Note that for conversions to integer type the C standard requires |
| 2629 | round-to-zero to always be used. */ |
| 2630 | APFloat::opStatus IEEEFloat::convertToSignExtendedInteger( |
| 2631 | MutableArrayRef<integerPart> parts, unsigned int width, bool isSigned, |
| 2632 | roundingMode rounding_mode, bool *isExact) const { |
| 2633 | lostFraction lost_fraction; |
| 2634 | const integerPart *src; |
| 2635 | unsigned int dstPartsCount, truncatedBits; |
| 2636 | |
| 2637 | *isExact = false; |
| 2638 | |
| 2639 | /* Handle the three special cases first. */ |
| 2640 | if (category == fcInfinity || category == fcNaN) |
| 2641 | return opInvalidOp; |
| 2642 | |
| 2643 | dstPartsCount = partCountForBits(bits: width); |
| 2644 | assert(dstPartsCount <= parts.size() && "Integer too big" ); |
| 2645 | |
| 2646 | if (category == fcZero) { |
| 2647 | APInt::tcSet(parts.data(), 0, dstPartsCount); |
| 2648 | // Negative zero can't be represented as an int. |
| 2649 | *isExact = !sign; |
| 2650 | return opOK; |
| 2651 | } |
| 2652 | |
| 2653 | src = significandParts(); |
| 2654 | |
| 2655 | /* Step 1: place our absolute value, with any fraction truncated, in |
| 2656 | the destination. */ |
| 2657 | if (exponent < 0) { |
| 2658 | /* Our absolute value is less than one; truncate everything. */ |
| 2659 | APInt::tcSet(parts.data(), 0, dstPartsCount); |
| 2660 | /* For exponent -1 the integer bit represents .5, look at that. |
| 2661 | For smaller exponents leftmost truncated bit is 0. */ |
| 2662 | truncatedBits = semantics->precision -1U - exponent; |
| 2663 | } else { |
| 2664 | /* We want the most significant (exponent + 1) bits; the rest are |
| 2665 | truncated. */ |
| 2666 | unsigned int bits = exponent + 1U; |
| 2667 | |
| 2668 | /* Hopelessly large in magnitude? */ |
| 2669 | if (bits > width) |
| 2670 | return opInvalidOp; |
| 2671 | |
| 2672 | if (bits < semantics->precision) { |
| 2673 | /* We truncate (semantics->precision - bits) bits. */ |
| 2674 | truncatedBits = semantics->precision - bits; |
| 2675 | APInt::tcExtract(parts.data(), dstCount: dstPartsCount, src, srcBits: bits, srcLSB: truncatedBits); |
| 2676 | } else { |
| 2677 | /* We want at least as many bits as are available. */ |
| 2678 | APInt::tcExtract(parts.data(), dstCount: dstPartsCount, src, srcBits: semantics->precision, |
| 2679 | srcLSB: 0); |
| 2680 | APInt::tcShiftLeft(parts.data(), Words: dstPartsCount, |
| 2681 | Count: bits - semantics->precision); |
| 2682 | truncatedBits = 0; |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | /* Step 2: work out any lost fraction, and increment the absolute |
| 2687 | value if we would round away from zero. */ |
| 2688 | if (truncatedBits) { |
| 2689 | lost_fraction = lostFractionThroughTruncation(parts: src, partCount: partCount(), |
| 2690 | bits: truncatedBits); |
| 2691 | if (lost_fraction != lfExactlyZero && |
| 2692 | roundAwayFromZero(rounding_mode, lost_fraction, bit: truncatedBits)) { |
| 2693 | if (APInt::tcIncrement(dst: parts.data(), parts: dstPartsCount)) |
| 2694 | return opInvalidOp; /* Overflow. */ |
| 2695 | } |
| 2696 | } else { |
| 2697 | lost_fraction = lfExactlyZero; |
| 2698 | } |
| 2699 | |
| 2700 | /* Step 3: check if we fit in the destination. */ |
| 2701 | unsigned int omsb = APInt::tcMSB(parts: parts.data(), n: dstPartsCount) + 1; |
| 2702 | |
| 2703 | if (sign) { |
| 2704 | if (!isSigned) { |
| 2705 | /* Negative numbers cannot be represented as unsigned. */ |
| 2706 | if (omsb != 0) |
| 2707 | return opInvalidOp; |
| 2708 | } else { |
| 2709 | /* It takes omsb bits to represent the unsigned integer value. |
| 2710 | We lose a bit for the sign, but care is needed as the |
| 2711 | maximally negative integer is a special case. */ |
| 2712 | if (omsb == width && |
| 2713 | APInt::tcLSB(parts.data(), n: dstPartsCount) + 1 != omsb) |
| 2714 | return opInvalidOp; |
| 2715 | |
| 2716 | /* This case can happen because of rounding. */ |
| 2717 | if (omsb > width) |
| 2718 | return opInvalidOp; |
| 2719 | } |
| 2720 | |
| 2721 | APInt::tcNegate (parts.data(), dstPartsCount); |
| 2722 | } else { |
| 2723 | if (omsb >= width + !isSigned) |
| 2724 | return opInvalidOp; |
| 2725 | } |
| 2726 | |
| 2727 | if (lost_fraction == lfExactlyZero) { |
| 2728 | *isExact = true; |
| 2729 | return opOK; |
| 2730 | } |
| 2731 | return opInexact; |
| 2732 | } |
| 2733 | |
| 2734 | /* Same as convertToSignExtendedInteger, except we provide |
| 2735 | deterministic values in case of an invalid operation exception, |
| 2736 | namely zero for NaNs and the minimal or maximal value respectively |
| 2737 | for underflow or overflow. |
| 2738 | The *isExact output tells whether the result is exact, in the sense |
| 2739 | that converting it back to the original floating point type produces |
| 2740 | the original value. This is almost equivalent to result==opOK, |
| 2741 | except for negative zeroes. |
| 2742 | */ |
| 2743 | APFloat::opStatus |
| 2744 | IEEEFloat::convertToInteger(MutableArrayRef<integerPart> parts, |
| 2745 | unsigned int width, bool isSigned, |
| 2746 | roundingMode rounding_mode, bool *isExact) const { |
| 2747 | opStatus fs; |
| 2748 | |
| 2749 | fs = convertToSignExtendedInteger(parts, width, isSigned, rounding_mode, |
| 2750 | isExact); |
| 2751 | |
| 2752 | if (fs == opInvalidOp) { |
| 2753 | unsigned int bits, dstPartsCount; |
| 2754 | |
| 2755 | dstPartsCount = partCountForBits(bits: width); |
| 2756 | assert(dstPartsCount <= parts.size() && "Integer too big" ); |
| 2757 | |
| 2758 | if (category == fcNaN) |
| 2759 | bits = 0; |
| 2760 | else if (sign) |
| 2761 | bits = isSigned; |
| 2762 | else |
| 2763 | bits = width - isSigned; |
| 2764 | |
| 2765 | tcSetLeastSignificantBits(dst: parts.data(), parts: dstPartsCount, bits); |
| 2766 | if (sign && isSigned) |
| 2767 | APInt::tcShiftLeft(parts.data(), Words: dstPartsCount, Count: width - 1); |
| 2768 | } |
| 2769 | |
| 2770 | return fs; |
| 2771 | } |
| 2772 | |
| 2773 | /* Convert an unsigned integer SRC to a floating point number, |
| 2774 | rounding according to ROUNDING_MODE. The sign of the floating |
| 2775 | point number is not modified. */ |
| 2776 | APFloat::opStatus IEEEFloat::convertFromUnsignedParts( |
| 2777 | const integerPart *src, unsigned int srcCount, roundingMode rounding_mode) { |
| 2778 | unsigned int omsb, precision, dstCount; |
| 2779 | integerPart *dst; |
| 2780 | lostFraction lost_fraction; |
| 2781 | |
| 2782 | category = fcNormal; |
| 2783 | omsb = APInt::tcMSB(parts: src, n: srcCount) + 1; |
| 2784 | dst = significandParts(); |
| 2785 | dstCount = partCount(); |
| 2786 | precision = semantics->precision; |
| 2787 | |
| 2788 | /* We want the most significant PRECISION bits of SRC. There may not |
| 2789 | be that many; extract what we can. */ |
| 2790 | if (precision <= omsb) { |
| 2791 | exponent = omsb - 1; |
| 2792 | lost_fraction = lostFractionThroughTruncation(parts: src, partCount: srcCount, |
| 2793 | bits: omsb - precision); |
| 2794 | APInt::tcExtract(dst, dstCount, src, srcBits: precision, srcLSB: omsb - precision); |
| 2795 | } else { |
| 2796 | exponent = precision - 1; |
| 2797 | lost_fraction = lfExactlyZero; |
| 2798 | APInt::tcExtract(dst, dstCount, src, srcBits: omsb, srcLSB: 0); |
| 2799 | } |
| 2800 | |
| 2801 | return normalize(rounding_mode, lost_fraction); |
| 2802 | } |
| 2803 | |
| 2804 | APFloat::opStatus IEEEFloat::convertFromAPInt(const APInt &Val, bool isSigned, |
| 2805 | roundingMode rounding_mode) { |
| 2806 | unsigned int partCount = Val.getNumWords(); |
| 2807 | APInt api = Val; |
| 2808 | |
| 2809 | sign = false; |
| 2810 | if (isSigned && api.isNegative()) { |
| 2811 | sign = true; |
| 2812 | api = -api; |
| 2813 | } |
| 2814 | |
| 2815 | return convertFromUnsignedParts(src: api.getRawData(), srcCount: partCount, rounding_mode); |
| 2816 | } |
| 2817 | |
| 2818 | Expected<APFloat::opStatus> |
| 2819 | IEEEFloat::convertFromHexadecimalString(StringRef s, |
| 2820 | roundingMode rounding_mode) { |
| 2821 | lostFraction lost_fraction = lfExactlyZero; |
| 2822 | |
| 2823 | category = fcNormal; |
| 2824 | zeroSignificand(); |
| 2825 | exponent = 0; |
| 2826 | |
| 2827 | integerPart *significand = significandParts(); |
| 2828 | unsigned partsCount = partCount(); |
| 2829 | unsigned bitPos = partsCount * integerPartWidth; |
| 2830 | bool computedTrailingFraction = false; |
| 2831 | |
| 2832 | // Skip leading zeroes and any (hexa)decimal point. |
| 2833 | StringRef::iterator begin = s.begin(); |
| 2834 | StringRef::iterator end = s.end(); |
| 2835 | StringRef::iterator dot; |
| 2836 | auto PtrOrErr = skipLeadingZeroesAndAnyDot(begin, end, dot: &dot); |
| 2837 | if (!PtrOrErr) |
| 2838 | return PtrOrErr.takeError(); |
| 2839 | StringRef::iterator p = *PtrOrErr; |
| 2840 | StringRef::iterator firstSignificantDigit = p; |
| 2841 | |
| 2842 | while (p != end) { |
| 2843 | integerPart hex_value; |
| 2844 | |
| 2845 | if (*p == '.') { |
| 2846 | if (dot != end) |
| 2847 | return createError(Err: "String contains multiple dots" ); |
| 2848 | dot = p++; |
| 2849 | continue; |
| 2850 | } |
| 2851 | |
| 2852 | hex_value = hexDigitValue(C: *p); |
| 2853 | if (hex_value == UINT_MAX) |
| 2854 | break; |
| 2855 | |
| 2856 | p++; |
| 2857 | |
| 2858 | // Store the number while we have space. |
| 2859 | if (bitPos) { |
| 2860 | bitPos -= 4; |
| 2861 | hex_value <<= bitPos % integerPartWidth; |
| 2862 | significand[bitPos / integerPartWidth] |= hex_value; |
| 2863 | } else if (!computedTrailingFraction) { |
| 2864 | auto FractOrErr = trailingHexadecimalFraction(p, end, digitValue: hex_value); |
| 2865 | if (!FractOrErr) |
| 2866 | return FractOrErr.takeError(); |
| 2867 | lost_fraction = *FractOrErr; |
| 2868 | computedTrailingFraction = true; |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | /* Hex floats require an exponent but not a hexadecimal point. */ |
| 2873 | if (p == end) |
| 2874 | return createError(Err: "Hex strings require an exponent" ); |
| 2875 | if (*p != 'p' && *p != 'P') |
| 2876 | return createError(Err: "Invalid character in significand" ); |
| 2877 | if (p == begin) |
| 2878 | return createError(Err: "Significand has no digits" ); |
| 2879 | if (dot != end && p - begin == 1) |
| 2880 | return createError(Err: "Significand has no digits" ); |
| 2881 | |
| 2882 | /* Ignore the exponent if we are zero. */ |
| 2883 | if (p != firstSignificantDigit) { |
| 2884 | int expAdjustment; |
| 2885 | |
| 2886 | /* Implicit hexadecimal point? */ |
| 2887 | if (dot == end) |
| 2888 | dot = p; |
| 2889 | |
| 2890 | /* Calculate the exponent adjustment implicit in the number of |
| 2891 | significant digits. */ |
| 2892 | expAdjustment = static_cast<int>(dot - firstSignificantDigit); |
| 2893 | if (expAdjustment < 0) |
| 2894 | expAdjustment++; |
| 2895 | expAdjustment = expAdjustment * 4 - 1; |
| 2896 | |
| 2897 | /* Adjust for writing the significand starting at the most |
| 2898 | significant nibble. */ |
| 2899 | expAdjustment += semantics->precision; |
| 2900 | expAdjustment -= partsCount * integerPartWidth; |
| 2901 | |
| 2902 | /* Adjust for the given exponent. */ |
| 2903 | auto ExpOrErr = totalExponent(p: p + 1, end, exponentAdjustment: expAdjustment); |
| 2904 | if (!ExpOrErr) |
| 2905 | return ExpOrErr.takeError(); |
| 2906 | exponent = *ExpOrErr; |
| 2907 | } |
| 2908 | |
| 2909 | return normalize(rounding_mode, lost_fraction); |
| 2910 | } |
| 2911 | |
| 2912 | APFloat::opStatus |
| 2913 | IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts, |
| 2914 | unsigned sigPartCount, int exp, |
| 2915 | roundingMode rounding_mode) { |
| 2916 | unsigned int parts, pow5PartCount; |
| 2917 | fltSemantics calcSemantics = { .maxExponent: 32767, .minExponent: -32767, .precision: 0, .sizeInBits: 0 }; |
| 2918 | integerPart pow5Parts[maxPowerOfFiveParts]; |
| 2919 | bool isNearest; |
| 2920 | |
| 2921 | isNearest = (rounding_mode == rmNearestTiesToEven || |
| 2922 | rounding_mode == rmNearestTiesToAway); |
| 2923 | |
| 2924 | parts = partCountForBits(bits: semantics->precision + 11); |
| 2925 | |
| 2926 | /* Calculate pow(5, abs(exp)). */ |
| 2927 | pow5PartCount = powerOf5(dst: pow5Parts, power: exp >= 0 ? exp: -exp); |
| 2928 | |
| 2929 | for (;; parts *= 2) { |
| 2930 | opStatus sigStatus, powStatus; |
| 2931 | unsigned int excessPrecision, truncatedBits; |
| 2932 | |
| 2933 | calcSemantics.precision = parts * integerPartWidth - 1; |
| 2934 | excessPrecision = calcSemantics.precision - semantics->precision; |
| 2935 | truncatedBits = excessPrecision; |
| 2936 | |
| 2937 | IEEEFloat decSig(calcSemantics, uninitialized); |
| 2938 | decSig.makeZero(Neg: sign); |
| 2939 | IEEEFloat pow5(calcSemantics); |
| 2940 | |
| 2941 | sigStatus = decSig.convertFromUnsignedParts(src: decSigParts, srcCount: sigPartCount, |
| 2942 | rounding_mode: rmNearestTiesToEven); |
| 2943 | powStatus = pow5.convertFromUnsignedParts(src: pow5Parts, srcCount: pow5PartCount, |
| 2944 | rounding_mode: rmNearestTiesToEven); |
| 2945 | /* Add exp, as 10^n = 5^n * 2^n. */ |
| 2946 | decSig.exponent += exp; |
| 2947 | |
| 2948 | lostFraction calcLostFraction; |
| 2949 | integerPart HUerr, HUdistance; |
| 2950 | unsigned int powHUerr; |
| 2951 | |
| 2952 | if (exp >= 0) { |
| 2953 | /* multiplySignificand leaves the precision-th bit set to 1. */ |
| 2954 | calcLostFraction = decSig.multiplySignificand(rhs: pow5); |
| 2955 | powHUerr = powStatus != opOK; |
| 2956 | } else { |
| 2957 | calcLostFraction = decSig.divideSignificand(rhs: pow5); |
| 2958 | /* Denormal numbers have less precision. */ |
| 2959 | if (decSig.exponent < semantics->minExponent) { |
| 2960 | excessPrecision += (semantics->minExponent - decSig.exponent); |
| 2961 | truncatedBits = excessPrecision; |
| 2962 | excessPrecision = std::min(a: excessPrecision, b: calcSemantics.precision); |
| 2963 | } |
| 2964 | /* Extra half-ulp lost in reciprocal of exponent. */ |
| 2965 | powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2; |
| 2966 | } |
| 2967 | |
| 2968 | /* Both multiplySignificand and divideSignificand return the |
| 2969 | result with the integer bit set. */ |
| 2970 | assert(APInt::tcExtractBit |
| 2971 | (decSig.significandParts(), calcSemantics.precision - 1) == 1); |
| 2972 | |
| 2973 | HUerr = HUerrBound(inexactMultiply: calcLostFraction != lfExactlyZero, HUerr1: sigStatus != opOK, |
| 2974 | HUerr2: powHUerr); |
| 2975 | HUdistance = 2 * ulpsFromBoundary(parts: decSig.significandParts(), |
| 2976 | bits: excessPrecision, isNearest); |
| 2977 | |
| 2978 | /* Are we guaranteed to round correctly if we truncate? */ |
| 2979 | if (HUdistance >= HUerr) { |
| 2980 | APInt::tcExtract(significandParts(), dstCount: partCount(), decSig.significandParts(), |
| 2981 | srcBits: calcSemantics.precision - excessPrecision, |
| 2982 | srcLSB: excessPrecision); |
| 2983 | /* Take the exponent of decSig. If we tcExtract-ed less bits |
| 2984 | above we must adjust our exponent to compensate for the |
| 2985 | implicit right shift. */ |
| 2986 | exponent = (decSig.exponent + semantics->precision |
| 2987 | - (calcSemantics.precision - excessPrecision)); |
| 2988 | calcLostFraction = lostFractionThroughTruncation(parts: decSig.significandParts(), |
| 2989 | partCount: decSig.partCount(), |
| 2990 | bits: truncatedBits); |
| 2991 | return normalize(rounding_mode, lost_fraction: calcLostFraction); |
| 2992 | } |
| 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | Expected<APFloat::opStatus> |
| 2997 | IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) { |
| 2998 | decimalInfo D; |
| 2999 | opStatus fs; |
| 3000 | |
| 3001 | /* Scan the text. */ |
| 3002 | StringRef::iterator p = str.begin(); |
| 3003 | if (Error Err = interpretDecimal(begin: p, end: str.end(), D: &D)) |
| 3004 | return std::move(Err); |
| 3005 | |
| 3006 | /* Handle the quick cases. First the case of no significant digits, |
| 3007 | i.e. zero, and then exponents that are obviously too large or too |
| 3008 | small. Writing L for log 10 / log 2, a number d.ddddd*10^exp |
| 3009 | definitely overflows if |
| 3010 | |
| 3011 | (exp - 1) * L >= maxExponent |
| 3012 | |
| 3013 | and definitely underflows to zero where |
| 3014 | |
| 3015 | (exp + 1) * L <= minExponent - precision |
| 3016 | |
| 3017 | With integer arithmetic the tightest bounds for L are |
| 3018 | |
| 3019 | 93/28 < L < 196/59 [ numerator <= 256 ] |
| 3020 | 42039/12655 < L < 28738/8651 [ numerator <= 65536 ] |
| 3021 | */ |
| 3022 | |
| 3023 | // Test if we have a zero number allowing for strings with no null terminators |
| 3024 | // and zero decimals with non-zero exponents. |
| 3025 | // |
| 3026 | // We computed firstSigDigit by ignoring all zeros and dots. Thus if |
| 3027 | // D->firstSigDigit equals str.end(), every digit must be a zero and there can |
| 3028 | // be at most one dot. On the other hand, if we have a zero with a non-zero |
| 3029 | // exponent, then we know that D.firstSigDigit will be non-numeric. |
| 3030 | if (D.firstSigDigit == str.end() || decDigitValue(c: *D.firstSigDigit) >= 10U) { |
| 3031 | category = fcZero; |
| 3032 | fs = opOK; |
| 3033 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 3034 | sign = false; |
| 3035 | if (!semantics->hasZero) |
| 3036 | makeSmallestNormalized(Negative: false); |
| 3037 | |
| 3038 | /* Check whether the normalized exponent is high enough to overflow |
| 3039 | max during the log-rebasing in the max-exponent check below. */ |
| 3040 | } else if (D.normalizedExponent - 1 > INT_MAX / 42039) { |
| 3041 | fs = handleOverflow(rounding_mode); |
| 3042 | |
| 3043 | /* If it wasn't, then it also wasn't high enough to overflow max |
| 3044 | during the log-rebasing in the min-exponent check. Check that it |
| 3045 | won't overflow min in either check, then perform the min-exponent |
| 3046 | check. */ |
| 3047 | } else if (D.normalizedExponent - 1 < INT_MIN / 42039 || |
| 3048 | (D.normalizedExponent + 1) * 28738 <= |
| 3049 | 8651 * (semantics->minExponent - (int) semantics->precision)) { |
| 3050 | /* Underflow to zero and round. */ |
| 3051 | category = fcNormal; |
| 3052 | zeroSignificand(); |
| 3053 | fs = normalize(rounding_mode, lost_fraction: lfLessThanHalf); |
| 3054 | |
| 3055 | /* We can finally safely perform the max-exponent check. */ |
| 3056 | } else if ((D.normalizedExponent - 1) * 42039 |
| 3057 | >= 12655 * semantics->maxExponent) { |
| 3058 | /* Overflow and round. */ |
| 3059 | fs = handleOverflow(rounding_mode); |
| 3060 | } else { |
| 3061 | integerPart *decSignificand; |
| 3062 | unsigned int partCount; |
| 3063 | |
| 3064 | /* A tight upper bound on number of bits required to hold an |
| 3065 | N-digit decimal integer is N * 196 / 59. Allocate enough space |
| 3066 | to hold the full significand, and an extra part required by |
| 3067 | tcMultiplyPart. */ |
| 3068 | partCount = static_cast<unsigned int>(D.lastSigDigit - D.firstSigDigit) + 1; |
| 3069 | partCount = partCountForBits(bits: 1 + 196 * partCount / 59); |
| 3070 | decSignificand = new integerPart[partCount + 1]; |
| 3071 | partCount = 0; |
| 3072 | |
| 3073 | /* Convert to binary efficiently - we do almost all multiplication |
| 3074 | in an integerPart. When this would overflow do we do a single |
| 3075 | bignum multiplication, and then revert again to multiplication |
| 3076 | in an integerPart. */ |
| 3077 | do { |
| 3078 | integerPart decValue, val, multiplier; |
| 3079 | |
| 3080 | val = 0; |
| 3081 | multiplier = 1; |
| 3082 | |
| 3083 | do { |
| 3084 | if (*p == '.') { |
| 3085 | p++; |
| 3086 | if (p == str.end()) { |
| 3087 | break; |
| 3088 | } |
| 3089 | } |
| 3090 | decValue = decDigitValue(c: *p++); |
| 3091 | if (decValue >= 10U) { |
| 3092 | delete[] decSignificand; |
| 3093 | return createError(Err: "Invalid character in significand" ); |
| 3094 | } |
| 3095 | multiplier *= 10; |
| 3096 | val = val * 10 + decValue; |
| 3097 | /* The maximum number that can be multiplied by ten with any |
| 3098 | digit added without overflowing an integerPart. */ |
| 3099 | } while (p <= D.lastSigDigit && multiplier <= (~ (integerPart) 0 - 9) / 10); |
| 3100 | |
| 3101 | /* Multiply out the current part. */ |
| 3102 | APInt::tcMultiplyPart(dst: decSignificand, src: decSignificand, multiplier, carry: val, |
| 3103 | srcParts: partCount, dstParts: partCount + 1, add: false); |
| 3104 | |
| 3105 | /* If we used another part (likely but not guaranteed), increase |
| 3106 | the count. */ |
| 3107 | if (decSignificand[partCount]) |
| 3108 | partCount++; |
| 3109 | } while (p <= D.lastSigDigit); |
| 3110 | |
| 3111 | category = fcNormal; |
| 3112 | fs = roundSignificandWithExponent(decSigParts: decSignificand, sigPartCount: partCount, |
| 3113 | exp: D.exponent, rounding_mode); |
| 3114 | |
| 3115 | delete [] decSignificand; |
| 3116 | } |
| 3117 | |
| 3118 | return fs; |
| 3119 | } |
| 3120 | |
| 3121 | bool IEEEFloat::convertFromStringSpecials(StringRef str) { |
| 3122 | const size_t MIN_NAME_SIZE = 3; |
| 3123 | |
| 3124 | if (str.size() < MIN_NAME_SIZE) |
| 3125 | return false; |
| 3126 | |
| 3127 | if (str == "inf" || str == "INFINITY" || str == "+Inf" ) { |
| 3128 | makeInf(Neg: false); |
| 3129 | return true; |
| 3130 | } |
| 3131 | |
| 3132 | bool IsNegative = str.consume_front(Prefix: "-" ); |
| 3133 | if (IsNegative) { |
| 3134 | if (str.size() < MIN_NAME_SIZE) |
| 3135 | return false; |
| 3136 | |
| 3137 | if (str == "inf" || str == "INFINITY" || str == "Inf" ) { |
| 3138 | makeInf(Neg: true); |
| 3139 | return true; |
| 3140 | } |
| 3141 | } |
| 3142 | |
| 3143 | // If we have a 's' (or 'S') prefix, then this is a Signaling NaN. |
| 3144 | bool IsSignaling = str.consume_front_insensitive(Prefix: "s" ); |
| 3145 | if (IsSignaling) { |
| 3146 | if (str.size() < MIN_NAME_SIZE) |
| 3147 | return false; |
| 3148 | } |
| 3149 | |
| 3150 | if (str.consume_front(Prefix: "nan" ) || str.consume_front(Prefix: "NaN" )) { |
| 3151 | // A NaN without payload. |
| 3152 | if (str.empty()) { |
| 3153 | makeNaN(SNaN: IsSignaling, Negative: IsNegative); |
| 3154 | return true; |
| 3155 | } |
| 3156 | |
| 3157 | // Allow the payload to be inside parentheses. |
| 3158 | if (str.front() == '(') { |
| 3159 | // Parentheses should be balanced (and not empty). |
| 3160 | if (str.size() <= 2 || str.back() != ')') |
| 3161 | return false; |
| 3162 | |
| 3163 | str = str.slice(Start: 1, End: str.size() - 1); |
| 3164 | } |
| 3165 | |
| 3166 | // Determine the payload number's radix. |
| 3167 | unsigned Radix = 10; |
| 3168 | if (str[0] == '0') { |
| 3169 | if (str.size() > 1 && tolower(c: str[1]) == 'x') { |
| 3170 | str = str.drop_front(N: 2); |
| 3171 | Radix = 16; |
| 3172 | } else { |
| 3173 | Radix = 8; |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | // Parse the payload and make the NaN. |
| 3178 | APInt Payload; |
| 3179 | if (!str.getAsInteger(Radix, Result&: Payload)) { |
| 3180 | makeNaN(SNaN: IsSignaling, Negative: IsNegative, fill: &Payload); |
| 3181 | return true; |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | return false; |
| 3186 | } |
| 3187 | |
| 3188 | Expected<APFloat::opStatus> |
| 3189 | IEEEFloat::convertFromString(StringRef str, roundingMode rounding_mode) { |
| 3190 | if (str.empty()) |
| 3191 | return createError(Err: "Invalid string length" ); |
| 3192 | |
| 3193 | // Handle special cases. |
| 3194 | if (convertFromStringSpecials(str)) |
| 3195 | return opOK; |
| 3196 | |
| 3197 | /* Handle a leading minus sign. */ |
| 3198 | StringRef::iterator p = str.begin(); |
| 3199 | size_t slen = str.size(); |
| 3200 | sign = *p == '-' ? 1 : 0; |
| 3201 | if (sign && !semantics->hasSignedRepr) |
| 3202 | llvm_unreachable( |
| 3203 | "This floating point format does not support signed values" ); |
| 3204 | |
| 3205 | if (*p == '-' || *p == '+') { |
| 3206 | p++; |
| 3207 | slen--; |
| 3208 | if (!slen) |
| 3209 | return createError(Err: "String has no digits" ); |
| 3210 | } |
| 3211 | |
| 3212 | if (slen >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { |
| 3213 | if (slen == 2) |
| 3214 | return createError(Err: "Invalid string" ); |
| 3215 | return convertFromHexadecimalString(s: StringRef(p + 2, slen - 2), |
| 3216 | rounding_mode); |
| 3217 | } |
| 3218 | |
| 3219 | return convertFromDecimalString(str: StringRef(p, slen), rounding_mode); |
| 3220 | } |
| 3221 | |
| 3222 | /* Write out a hexadecimal representation of the floating point value |
| 3223 | to DST, which must be of sufficient size, in the C99 form |
| 3224 | [-]0xh.hhhhp[+-]d. Return the number of characters written, |
| 3225 | excluding the terminating NUL. |
| 3226 | |
| 3227 | If UPPERCASE, the output is in upper case, otherwise in lower case. |
| 3228 | |
| 3229 | HEXDIGITS digits appear altogether, rounding the value if |
| 3230 | necessary. If HEXDIGITS is 0, the minimal precision to display the |
| 3231 | number precisely is used instead. If nothing would appear after |
| 3232 | the decimal point it is suppressed. |
| 3233 | |
| 3234 | The decimal exponent is always printed and has at least one digit. |
| 3235 | Zero values display an exponent of zero. Infinities and NaNs |
| 3236 | appear as "infinity" or "nan" respectively. |
| 3237 | |
| 3238 | The above rules are as specified by C99. There is ambiguity about |
| 3239 | what the leading hexadecimal digit should be. This implementation |
| 3240 | uses whatever is necessary so that the exponent is displayed as |
| 3241 | stored. This implies the exponent will fall within the IEEE format |
| 3242 | range, and the leading hexadecimal digit will be 0 (for denormals), |
| 3243 | 1 (normal numbers) or 2 (normal numbers rounded-away-from-zero with |
| 3244 | any other digits zero). |
| 3245 | */ |
| 3246 | unsigned int IEEEFloat::convertToHexString(char *dst, unsigned int hexDigits, |
| 3247 | bool upperCase, |
| 3248 | roundingMode rounding_mode) const { |
| 3249 | char *p; |
| 3250 | |
| 3251 | p = dst; |
| 3252 | if (sign) |
| 3253 | *dst++ = '-'; |
| 3254 | |
| 3255 | switch (category) { |
| 3256 | case fcInfinity: |
| 3257 | memcpy (dest: dst, src: upperCase ? infinityU: infinityL, n: sizeof infinityU - 1); |
| 3258 | dst += sizeof infinityL - 1; |
| 3259 | break; |
| 3260 | |
| 3261 | case fcNaN: |
| 3262 | memcpy (dest: dst, src: upperCase ? NaNU: NaNL, n: sizeof NaNU - 1); |
| 3263 | dst += sizeof NaNU - 1; |
| 3264 | break; |
| 3265 | |
| 3266 | case fcZero: |
| 3267 | *dst++ = '0'; |
| 3268 | *dst++ = upperCase ? 'X': 'x'; |
| 3269 | *dst++ = '0'; |
| 3270 | if (hexDigits > 1) { |
| 3271 | *dst++ = '.'; |
| 3272 | memset (s: dst, c: '0', n: hexDigits - 1); |
| 3273 | dst += hexDigits - 1; |
| 3274 | } |
| 3275 | *dst++ = upperCase ? 'P': 'p'; |
| 3276 | *dst++ = '0'; |
| 3277 | break; |
| 3278 | |
| 3279 | case fcNormal: |
| 3280 | dst = convertNormalToHexString (dst, hexDigits, upperCase, rounding_mode); |
| 3281 | break; |
| 3282 | } |
| 3283 | |
| 3284 | *dst = 0; |
| 3285 | |
| 3286 | return static_cast<unsigned int>(dst - p); |
| 3287 | } |
| 3288 | |
| 3289 | /* Does the hard work of outputting the correctly rounded hexadecimal |
| 3290 | form of a normal floating point number with the specified number of |
| 3291 | hexadecimal digits. If HEXDIGITS is zero the minimum number of |
| 3292 | digits necessary to print the value precisely is output. */ |
| 3293 | char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits, |
| 3294 | bool upperCase, |
| 3295 | roundingMode rounding_mode) const { |
| 3296 | unsigned int count, valueBits, shift, partsCount, outputDigits; |
| 3297 | const char *hexDigitChars; |
| 3298 | const integerPart *significand; |
| 3299 | char *p; |
| 3300 | bool roundUp; |
| 3301 | |
| 3302 | *dst++ = '0'; |
| 3303 | *dst++ = upperCase ? 'X': 'x'; |
| 3304 | |
| 3305 | roundUp = false; |
| 3306 | hexDigitChars = upperCase ? hexDigitsUpper: hexDigitsLower; |
| 3307 | |
| 3308 | significand = significandParts(); |
| 3309 | partsCount = partCount(); |
| 3310 | |
| 3311 | /* +3 because the first digit only uses the single integer bit, so |
| 3312 | we have 3 virtual zero most-significant-bits. */ |
| 3313 | valueBits = semantics->precision + 3; |
| 3314 | shift = integerPartWidth - valueBits % integerPartWidth; |
| 3315 | |
| 3316 | /* The natural number of digits required ignoring trailing |
| 3317 | insignificant zeroes. */ |
| 3318 | outputDigits = (valueBits - significandLSB () + 3) / 4; |
| 3319 | |
| 3320 | /* hexDigits of zero means use the required number for the |
| 3321 | precision. Otherwise, see if we are truncating. If we are, |
| 3322 | find out if we need to round away from zero. */ |
| 3323 | if (hexDigits) { |
| 3324 | if (hexDigits < outputDigits) { |
| 3325 | /* We are dropping non-zero bits, so need to check how to round. |
| 3326 | "bits" is the number of dropped bits. */ |
| 3327 | unsigned int bits; |
| 3328 | lostFraction fraction; |
| 3329 | |
| 3330 | bits = valueBits - hexDigits * 4; |
| 3331 | fraction = lostFractionThroughTruncation (parts: significand, partCount: partsCount, bits); |
| 3332 | roundUp = roundAwayFromZero(rounding_mode, lost_fraction: fraction, bit: bits); |
| 3333 | } |
| 3334 | outputDigits = hexDigits; |
| 3335 | } |
| 3336 | |
| 3337 | /* Write the digits consecutively, and start writing in the location |
| 3338 | of the hexadecimal point. We move the most significant digit |
| 3339 | left and add the hexadecimal point later. */ |
| 3340 | p = ++dst; |
| 3341 | |
| 3342 | count = (valueBits + integerPartWidth - 1) / integerPartWidth; |
| 3343 | |
| 3344 | while (outputDigits && count) { |
| 3345 | integerPart part; |
| 3346 | |
| 3347 | /* Put the most significant integerPartWidth bits in "part". */ |
| 3348 | if (--count == partsCount) |
| 3349 | part = 0; /* An imaginary higher zero part. */ |
| 3350 | else |
| 3351 | part = significand[count] << shift; |
| 3352 | |
| 3353 | if (count && shift) |
| 3354 | part |= significand[count - 1] >> (integerPartWidth - shift); |
| 3355 | |
| 3356 | /* Convert as much of "part" to hexdigits as we can. */ |
| 3357 | unsigned int curDigits = integerPartWidth / 4; |
| 3358 | |
| 3359 | curDigits = std::min(a: curDigits, b: outputDigits); |
| 3360 | dst += partAsHex (dst, part, count: curDigits, hexDigitChars); |
| 3361 | outputDigits -= curDigits; |
| 3362 | } |
| 3363 | |
| 3364 | if (roundUp) { |
| 3365 | char *q = dst; |
| 3366 | |
| 3367 | /* Note that hexDigitChars has a trailing '0'. */ |
| 3368 | do { |
| 3369 | q--; |
| 3370 | *q = hexDigitChars[hexDigitValue (C: *q) + 1]; |
| 3371 | } while (*q == '0'); |
| 3372 | assert(q >= p); |
| 3373 | } else { |
| 3374 | /* Add trailing zeroes. */ |
| 3375 | memset (s: dst, c: '0', n: outputDigits); |
| 3376 | dst += outputDigits; |
| 3377 | } |
| 3378 | |
| 3379 | /* Move the most significant digit to before the point, and if there |
| 3380 | is something after the decimal point add it. This must come |
| 3381 | after rounding above. */ |
| 3382 | p[-1] = p[0]; |
| 3383 | if (dst -1 == p) |
| 3384 | dst--; |
| 3385 | else |
| 3386 | p[0] = '.'; |
| 3387 | |
| 3388 | /* Finally output the exponent. */ |
| 3389 | *dst++ = upperCase ? 'P': 'p'; |
| 3390 | |
| 3391 | return writeSignedDecimal (dst, value: exponent); |
| 3392 | } |
| 3393 | |
| 3394 | hash_code hash_value(const IEEEFloat &Arg) { |
| 3395 | if (!Arg.isFiniteNonZero()) |
| 3396 | return hash_combine(args: (uint8_t)Arg.category, |
| 3397 | // NaN has no sign, fix it at zero. |
| 3398 | args: Arg.isNaN() ? (uint8_t)0 : (uint8_t)Arg.sign, |
| 3399 | args: Arg.semantics->precision); |
| 3400 | |
| 3401 | // Normal floats need their exponent and significand hashed. |
| 3402 | return hash_combine(args: (uint8_t)Arg.category, args: (uint8_t)Arg.sign, |
| 3403 | args: Arg.semantics->precision, args: Arg.exponent, |
| 3404 | args: hash_combine_range( |
| 3405 | first: Arg.significandParts(), |
| 3406 | last: Arg.significandParts() + Arg.partCount())); |
| 3407 | } |
| 3408 | |
| 3409 | // Conversion from APFloat to/from host float/double. It may eventually be |
| 3410 | // possible to eliminate these and have everybody deal with APFloats, but that |
| 3411 | // will take a while. This approach will not easily extend to long double. |
| 3412 | // Current implementation requires integerPartWidth==64, which is correct at |
| 3413 | // the moment but could be made more general. |
| 3414 | |
| 3415 | // Denormals have exponent minExponent in APFloat, but minExponent-1 in |
| 3416 | // the actual IEEE respresentations. We compensate for that here. |
| 3417 | |
| 3418 | APInt IEEEFloat::convertF80LongDoubleAPFloatToAPInt() const { |
| 3419 | assert(semantics == |
| 3420 | (const llvm::fltSemantics *)&APFloatBase::semX87DoubleExtended); |
| 3421 | assert(partCount()==2); |
| 3422 | |
| 3423 | uint64_t myexponent, mysignificand; |
| 3424 | |
| 3425 | if (isFiniteNonZero()) { |
| 3426 | myexponent = exponent+16383; //bias |
| 3427 | mysignificand = significandParts()[0]; |
| 3428 | if (myexponent==1 && !(mysignificand & 0x8000000000000000ULL)) |
| 3429 | myexponent = 0; // denormal |
| 3430 | } else if (category==fcZero) { |
| 3431 | myexponent = 0; |
| 3432 | mysignificand = 0; |
| 3433 | } else if (category==fcInfinity) { |
| 3434 | myexponent = 0x7fff; |
| 3435 | mysignificand = 0x8000000000000000ULL; |
| 3436 | } else { |
| 3437 | assert(category == fcNaN && "Unknown category" ); |
| 3438 | myexponent = 0x7fff; |
| 3439 | mysignificand = significandParts()[0]; |
| 3440 | } |
| 3441 | |
| 3442 | uint64_t words[2]; |
| 3443 | words[0] = mysignificand; |
| 3444 | words[1] = ((uint64_t)(sign & 1) << 15) | |
| 3445 | (myexponent & 0x7fffLL); |
| 3446 | return APInt(80, words); |
| 3447 | } |
| 3448 | |
| 3449 | APInt IEEEFloat::convertPPCDoubleDoubleLegacyAPFloatToAPInt() const { |
| 3450 | assert(semantics == |
| 3451 | (const llvm::fltSemantics *)&APFloatBase::semPPCDoubleDoubleLegacy); |
| 3452 | assert(partCount()==2); |
| 3453 | |
| 3454 | uint64_t words[2]; |
| 3455 | opStatus fs; |
| 3456 | bool losesInfo; |
| 3457 | |
| 3458 | // Convert number to double. To avoid spurious underflows, we re- |
| 3459 | // normalize against the "double" minExponent first, and only *then* |
| 3460 | // truncate the mantissa. The result of that second conversion |
| 3461 | // may be inexact, but should never underflow. |
| 3462 | // Declare fltSemantics before APFloat that uses it (and |
| 3463 | // saves pointer to it) to ensure correct destruction order. |
| 3464 | fltSemantics extendedSemantics = *semantics; |
| 3465 | extendedSemantics.minExponent = APFloatBase::semIEEEdouble.minExponent; |
| 3466 | IEEEFloat extended(*this); |
| 3467 | fs = extended.convert(toSemantics: extendedSemantics, rounding_mode: rmNearestTiesToEven, losesInfo: &losesInfo); |
| 3468 | assert(fs == opOK && !losesInfo); |
| 3469 | (void)fs; |
| 3470 | |
| 3471 | IEEEFloat u(extended); |
| 3472 | fs = u.convert(toSemantics: APFloatBase::semIEEEdouble, rounding_mode: rmNearestTiesToEven, losesInfo: &losesInfo); |
| 3473 | assert(fs == opOK || fs == opInexact); |
| 3474 | (void)fs; |
| 3475 | words[0] = *u.convertDoubleAPFloatToAPInt().getRawData(); |
| 3476 | |
| 3477 | // If conversion was exact or resulted in a special case, we're done; |
| 3478 | // just set the second double to zero. Otherwise, re-convert back to |
| 3479 | // the extended format and compute the difference. This now should |
| 3480 | // convert exactly to double. |
| 3481 | if (u.isFiniteNonZero() && losesInfo) { |
| 3482 | fs = u.convert(toSemantics: extendedSemantics, rounding_mode: rmNearestTiesToEven, losesInfo: &losesInfo); |
| 3483 | assert(fs == opOK && !losesInfo); |
| 3484 | (void)fs; |
| 3485 | |
| 3486 | IEEEFloat v(extended); |
| 3487 | v.subtract(rhs: u, rounding_mode: rmNearestTiesToEven); |
| 3488 | fs = v.convert(toSemantics: APFloatBase::semIEEEdouble, rounding_mode: rmNearestTiesToEven, losesInfo: &losesInfo); |
| 3489 | assert(fs == opOK && !losesInfo); |
| 3490 | (void)fs; |
| 3491 | words[1] = *v.convertDoubleAPFloatToAPInt().getRawData(); |
| 3492 | } else { |
| 3493 | words[1] = 0; |
| 3494 | } |
| 3495 | |
| 3496 | return APInt(128, words); |
| 3497 | } |
| 3498 | |
| 3499 | template <const fltSemantics &S> |
| 3500 | APInt IEEEFloat::convertIEEEFloatToAPInt() const { |
| 3501 | assert(semantics == &S); |
| 3502 | const int bias = (semantics == &APFloatBase::semFloat8E8M0FNU) |
| 3503 | ? -S.minExponent |
| 3504 | : -(S.minExponent - 1); |
| 3505 | constexpr unsigned int trailing_significand_bits = S.precision - 1; |
| 3506 | constexpr int integer_bit_part = trailing_significand_bits / integerPartWidth; |
| 3507 | constexpr integerPart integer_bit = |
| 3508 | integerPart{1} << (trailing_significand_bits % integerPartWidth); |
| 3509 | constexpr uint64_t significand_mask = integer_bit - 1; |
| 3510 | constexpr unsigned int exponent_bits = |
| 3511 | trailing_significand_bits ? (S.sizeInBits - 1 - trailing_significand_bits) |
| 3512 | : S.sizeInBits; |
| 3513 | static_assert(exponent_bits < 64); |
| 3514 | constexpr uint64_t exponent_mask = (uint64_t{1} << exponent_bits) - 1; |
| 3515 | |
| 3516 | uint64_t myexponent; |
| 3517 | std::array<integerPart, partCountForBits(bits: trailing_significand_bits)> |
| 3518 | mysignificand; |
| 3519 | |
| 3520 | if (isFiniteNonZero()) { |
| 3521 | myexponent = exponent + bias; |
| 3522 | std::copy_n(significandParts(), mysignificand.size(), |
| 3523 | mysignificand.begin()); |
| 3524 | if (myexponent == 1 && |
| 3525 | !(significandParts()[integer_bit_part] & integer_bit)) |
| 3526 | myexponent = 0; // denormal |
| 3527 | } else if (category == fcZero) { |
| 3528 | if (!S.hasZero) |
| 3529 | llvm_unreachable("semantics does not support zero!" ); |
| 3530 | myexponent = ::exponentZero(semantics: S) + bias; |
| 3531 | mysignificand.fill(0); |
| 3532 | } else if (category == fcInfinity) { |
| 3533 | if (S.nonFiniteBehavior == fltNonfiniteBehavior::NanOnly || |
| 3534 | S.nonFiniteBehavior == fltNonfiniteBehavior::FiniteOnly) |
| 3535 | llvm_unreachable("semantics don't support inf!" ); |
| 3536 | myexponent = ::exponentInf(semantics: S) + bias; |
| 3537 | mysignificand.fill(0); |
| 3538 | } else { |
| 3539 | assert(category == fcNaN && "Unknown category!" ); |
| 3540 | if (S.nonFiniteBehavior == fltNonfiniteBehavior::FiniteOnly) |
| 3541 | llvm_unreachable("semantics don't support NaN!" ); |
| 3542 | myexponent = ::exponentNaN(semantics: S) + bias; |
| 3543 | std::copy_n(significandParts(), mysignificand.size(), |
| 3544 | mysignificand.begin()); |
| 3545 | } |
| 3546 | std::array<uint64_t, (S.sizeInBits + 63) / 64> words; |
| 3547 | auto words_iter = |
| 3548 | std::copy_n(mysignificand.begin(), mysignificand.size(), words.begin()); |
| 3549 | if constexpr (significand_mask != 0 || trailing_significand_bits == 0) { |
| 3550 | // Clear the integer bit. |
| 3551 | words[mysignificand.size() - 1] &= significand_mask; |
| 3552 | } |
| 3553 | std::fill(words_iter, words.end(), uint64_t{0}); |
| 3554 | constexpr size_t last_word = words.size() - 1; |
| 3555 | uint64_t shifted_sign = static_cast<uint64_t>(sign & 1) |
| 3556 | << ((S.sizeInBits - 1) % 64); |
| 3557 | words[last_word] |= shifted_sign; |
| 3558 | uint64_t shifted_exponent = (myexponent & exponent_mask) |
| 3559 | << (trailing_significand_bits % 64); |
| 3560 | words[last_word] |= shifted_exponent; |
| 3561 | if constexpr (last_word == 0) { |
| 3562 | return APInt(S.sizeInBits, words[0]); |
| 3563 | } |
| 3564 | return APInt(S.sizeInBits, words); |
| 3565 | } |
| 3566 | |
| 3567 | APInt IEEEFloat::convertQuadrupleAPFloatToAPInt() const { |
| 3568 | assert(partCount() == 2); |
| 3569 | return convertIEEEFloatToAPInt<APFloatBase::semIEEEquad>(); |
| 3570 | } |
| 3571 | |
| 3572 | APInt IEEEFloat::convertDoubleAPFloatToAPInt() const { |
| 3573 | assert(partCount()==1); |
| 3574 | return convertIEEEFloatToAPInt<APFloatBase::semIEEEdouble>(); |
| 3575 | } |
| 3576 | |
| 3577 | APInt IEEEFloat::convertFloatAPFloatToAPInt() const { |
| 3578 | assert(partCount()==1); |
| 3579 | return convertIEEEFloatToAPInt<APFloatBase::semIEEEsingle>(); |
| 3580 | } |
| 3581 | |
| 3582 | APInt IEEEFloat::convertBFloatAPFloatToAPInt() const { |
| 3583 | assert(partCount() == 1); |
| 3584 | return convertIEEEFloatToAPInt<APFloatBase::semBFloat>(); |
| 3585 | } |
| 3586 | |
| 3587 | APInt IEEEFloat::convertHalfAPFloatToAPInt() const { |
| 3588 | assert(partCount()==1); |
| 3589 | return convertIEEEFloatToAPInt<APFloatBase::APFloatBase::semIEEEhalf>(); |
| 3590 | } |
| 3591 | |
| 3592 | APInt IEEEFloat::convertFloat8E5M2APFloatToAPInt() const { |
| 3593 | assert(partCount() == 1); |
| 3594 | return convertIEEEFloatToAPInt<APFloatBase::semFloat8E5M2>(); |
| 3595 | } |
| 3596 | |
| 3597 | APInt IEEEFloat::convertFloat8E5M2FNUZAPFloatToAPInt() const { |
| 3598 | assert(partCount() == 1); |
| 3599 | return convertIEEEFloatToAPInt<APFloatBase::semFloat8E5M2FNUZ>(); |
| 3600 | } |
| 3601 | |
| 3602 | APInt IEEEFloat::convertFloat8E4M3APFloatToAPInt() const { |
| 3603 | assert(partCount() == 1); |
| 3604 | return convertIEEEFloatToAPInt<APFloatBase::semFloat8E4M3>(); |
| 3605 | } |
| 3606 | |
| 3607 | APInt IEEEFloat::convertFloat8E4M3FNAPFloatToAPInt() const { |
| 3608 | assert(partCount() == 1); |
| 3609 | return convertIEEEFloatToAPInt<APFloatBase::semFloat8E4M3FN>(); |
| 3610 | } |
| 3611 | |
| 3612 | APInt IEEEFloat::convertFloat8E4M3FNUZAPFloatToAPInt() const { |
| 3613 | assert(partCount() == 1); |
| 3614 | return convertIEEEFloatToAPInt<APFloatBase::semFloat8E4M3FNUZ>(); |
| 3615 | } |
| 3616 | |
| 3617 | APInt IEEEFloat::convertFloat8E4M3B11FNUZAPFloatToAPInt() const { |
| 3618 | assert(partCount() == 1); |
| 3619 | return convertIEEEFloatToAPInt<APFloatBase::semFloat8E4M3B11FNUZ>(); |
| 3620 | } |
| 3621 | |
| 3622 | APInt IEEEFloat::convertFloat8E3M4APFloatToAPInt() const { |
| 3623 | assert(partCount() == 1); |
| 3624 | return convertIEEEFloatToAPInt<APFloatBase::semFloat8E3M4>(); |
| 3625 | } |
| 3626 | |
| 3627 | APInt IEEEFloat::convertFloatTF32APFloatToAPInt() const { |
| 3628 | assert(partCount() == 1); |
| 3629 | return convertIEEEFloatToAPInt<APFloatBase::semFloatTF32>(); |
| 3630 | } |
| 3631 | |
| 3632 | APInt IEEEFloat::convertFloat8E8M0FNUAPFloatToAPInt() const { |
| 3633 | assert(partCount() == 1); |
| 3634 | return convertIEEEFloatToAPInt<APFloatBase::semFloat8E8M0FNU>(); |
| 3635 | } |
| 3636 | |
| 3637 | APInt IEEEFloat::convertFloat6E3M2FNAPFloatToAPInt() const { |
| 3638 | assert(partCount() == 1); |
| 3639 | return convertIEEEFloatToAPInt<APFloatBase::semFloat6E3M2FN>(); |
| 3640 | } |
| 3641 | |
| 3642 | APInt IEEEFloat::convertFloat6E2M3FNAPFloatToAPInt() const { |
| 3643 | assert(partCount() == 1); |
| 3644 | return convertIEEEFloatToAPInt<APFloatBase::semFloat6E2M3FN>(); |
| 3645 | } |
| 3646 | |
| 3647 | APInt IEEEFloat::convertFloat4E2M1FNAPFloatToAPInt() const { |
| 3648 | assert(partCount() == 1); |
| 3649 | return convertIEEEFloatToAPInt<APFloatBase::semFloat4E2M1FN>(); |
| 3650 | } |
| 3651 | |
| 3652 | // This function creates an APInt that is just a bit map of the floating |
| 3653 | // point constant as it would appear in memory. It is not a conversion, |
| 3654 | // and treating the result as a normal integer is unlikely to be useful. |
| 3655 | |
| 3656 | APInt IEEEFloat::bitcastToAPInt() const { |
| 3657 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEhalf) |
| 3658 | return convertHalfAPFloatToAPInt(); |
| 3659 | |
| 3660 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semBFloat) |
| 3661 | return convertBFloatAPFloatToAPInt(); |
| 3662 | |
| 3663 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEsingle) |
| 3664 | return convertFloatAPFloatToAPInt(); |
| 3665 | |
| 3666 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEdouble) |
| 3667 | return convertDoubleAPFloatToAPInt(); |
| 3668 | |
| 3669 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEquad) |
| 3670 | return convertQuadrupleAPFloatToAPInt(); |
| 3671 | |
| 3672 | if (semantics == |
| 3673 | (const llvm::fltSemantics *)&APFloatBase::semPPCDoubleDoubleLegacy) |
| 3674 | return convertPPCDoubleDoubleLegacyAPFloatToAPInt(); |
| 3675 | |
| 3676 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E5M2) |
| 3677 | return convertFloat8E5M2APFloatToAPInt(); |
| 3678 | |
| 3679 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E5M2FNUZ) |
| 3680 | return convertFloat8E5M2FNUZAPFloatToAPInt(); |
| 3681 | |
| 3682 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E4M3) |
| 3683 | return convertFloat8E4M3APFloatToAPInt(); |
| 3684 | |
| 3685 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E4M3FN) |
| 3686 | return convertFloat8E4M3FNAPFloatToAPInt(); |
| 3687 | |
| 3688 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E4M3FNUZ) |
| 3689 | return convertFloat8E4M3FNUZAPFloatToAPInt(); |
| 3690 | |
| 3691 | if (semantics == |
| 3692 | (const llvm::fltSemantics *)&APFloatBase::semFloat8E4M3B11FNUZ) |
| 3693 | return convertFloat8E4M3B11FNUZAPFloatToAPInt(); |
| 3694 | |
| 3695 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E3M4) |
| 3696 | return convertFloat8E3M4APFloatToAPInt(); |
| 3697 | |
| 3698 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloatTF32) |
| 3699 | return convertFloatTF32APFloatToAPInt(); |
| 3700 | |
| 3701 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat8E8M0FNU) |
| 3702 | return convertFloat8E8M0FNUAPFloatToAPInt(); |
| 3703 | |
| 3704 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat6E3M2FN) |
| 3705 | return convertFloat6E3M2FNAPFloatToAPInt(); |
| 3706 | |
| 3707 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat6E2M3FN) |
| 3708 | return convertFloat6E2M3FNAPFloatToAPInt(); |
| 3709 | |
| 3710 | if (semantics == (const llvm::fltSemantics *)&APFloatBase::semFloat4E2M1FN) |
| 3711 | return convertFloat4E2M1FNAPFloatToAPInt(); |
| 3712 | |
| 3713 | assert(semantics == |
| 3714 | (const llvm::fltSemantics *)&APFloatBase::semX87DoubleExtended && |
| 3715 | "unknown format!" ); |
| 3716 | return convertF80LongDoubleAPFloatToAPInt(); |
| 3717 | } |
| 3718 | |
| 3719 | float IEEEFloat::convertToFloat() const { |
| 3720 | assert(semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEsingle && |
| 3721 | "Float semantics are not IEEEsingle" ); |
| 3722 | APInt api = bitcastToAPInt(); |
| 3723 | return api.bitsToFloat(); |
| 3724 | } |
| 3725 | |
| 3726 | double IEEEFloat::convertToDouble() const { |
| 3727 | assert(semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEdouble && |
| 3728 | "Float semantics are not IEEEdouble" ); |
| 3729 | APInt api = bitcastToAPInt(); |
| 3730 | return api.bitsToDouble(); |
| 3731 | } |
| 3732 | |
| 3733 | #ifdef HAS_IEE754_FLOAT128 |
| 3734 | float128 IEEEFloat::convertToQuad() const { |
| 3735 | assert(semantics == (const llvm::fltSemantics *)&APFloatBase::semIEEEquad && |
| 3736 | "Float semantics are not IEEEquads" ); |
| 3737 | APInt api = bitcastToAPInt(); |
| 3738 | return api.bitsToQuad(); |
| 3739 | } |
| 3740 | #endif |
| 3741 | |
| 3742 | /// Integer bit is explicit in this format. Intel hardware (387 and later) |
| 3743 | /// does not support these bit patterns: |
| 3744 | /// exponent = all 1's, integer bit 0, significand 0 ("pseudoinfinity") |
| 3745 | /// exponent = all 1's, integer bit 0, significand nonzero ("pseudoNaN") |
| 3746 | /// exponent!=0 nor all 1's, integer bit 0 ("unnormal") |
| 3747 | /// exponent = 0, integer bit 1 ("pseudodenormal") |
| 3748 | /// At the moment, the first three are treated as NaNs, the last one as Normal. |
| 3749 | void IEEEFloat::initFromF80LongDoubleAPInt(const APInt &api) { |
| 3750 | uint64_t i1 = api.getRawData()[0]; |
| 3751 | uint64_t i2 = api.getRawData()[1]; |
| 3752 | uint64_t myexponent = (i2 & 0x7fff); |
| 3753 | uint64_t mysignificand = i1; |
| 3754 | uint8_t myintegerbit = mysignificand >> 63; |
| 3755 | |
| 3756 | initialize(ourSemantics: &APFloatBase::semX87DoubleExtended); |
| 3757 | assert(partCount()==2); |
| 3758 | |
| 3759 | sign = static_cast<unsigned int>(i2>>15); |
| 3760 | if (myexponent == 0 && mysignificand == 0) { |
| 3761 | makeZero(Neg: sign); |
| 3762 | } else if (myexponent==0x7fff && mysignificand==0x8000000000000000ULL) { |
| 3763 | makeInf(Neg: sign); |
| 3764 | } else if ((myexponent == 0x7fff && mysignificand != 0x8000000000000000ULL) || |
| 3765 | (myexponent != 0x7fff && myexponent != 0 && myintegerbit == 0)) { |
| 3766 | category = fcNaN; |
| 3767 | exponent = exponentNaN(); |
| 3768 | significandParts()[0] = mysignificand; |
| 3769 | significandParts()[1] = 0; |
| 3770 | } else { |
| 3771 | category = fcNormal; |
| 3772 | exponent = myexponent - 16383; |
| 3773 | significandParts()[0] = mysignificand; |
| 3774 | significandParts()[1] = 0; |
| 3775 | if (myexponent==0) // denormal |
| 3776 | exponent = -16382; |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | void IEEEFloat::initFromPPCDoubleDoubleLegacyAPInt(const APInt &api) { |
| 3781 | uint64_t i1 = api.getRawData()[0]; |
| 3782 | uint64_t i2 = api.getRawData()[1]; |
| 3783 | opStatus fs; |
| 3784 | bool losesInfo; |
| 3785 | |
| 3786 | // Get the first double and convert to our format. |
| 3787 | initFromDoubleAPInt(api: APInt(64, i1)); |
| 3788 | fs = convert(toSemantics: APFloatBase::semPPCDoubleDoubleLegacy, rounding_mode: rmNearestTiesToEven, |
| 3789 | losesInfo: &losesInfo); |
| 3790 | assert(fs == opOK && !losesInfo); |
| 3791 | (void)fs; |
| 3792 | |
| 3793 | // Unless we have a special case, add in second double. |
| 3794 | if (isFiniteNonZero()) { |
| 3795 | IEEEFloat v(APFloatBase::semIEEEdouble, APInt(64, i2)); |
| 3796 | fs = v.convert(toSemantics: APFloatBase::semPPCDoubleDoubleLegacy, rounding_mode: rmNearestTiesToEven, |
| 3797 | losesInfo: &losesInfo); |
| 3798 | assert(fs == opOK && !losesInfo); |
| 3799 | (void)fs; |
| 3800 | |
| 3801 | add(rhs: v, rounding_mode: rmNearestTiesToEven); |
| 3802 | } |
| 3803 | } |
| 3804 | |
| 3805 | // The E8M0 format has the following characteristics: |
| 3806 | // It is an 8-bit unsigned format with only exponents (no actual significand). |
| 3807 | // No encodings for {zero, infinities or denorms}. |
| 3808 | // NaN is represented by all 1's. |
| 3809 | // Bias is 127. |
| 3810 | void IEEEFloat::initFromFloat8E8M0FNUAPInt(const APInt &api) { |
| 3811 | const uint64_t exponent_mask = 0xff; |
| 3812 | uint64_t val = api.getRawData()[0]; |
| 3813 | uint64_t myexponent = (val & exponent_mask); |
| 3814 | |
| 3815 | initialize(ourSemantics: &APFloatBase::semFloat8E8M0FNU); |
| 3816 | assert(partCount() == 1); |
| 3817 | |
| 3818 | // This format has unsigned representation only |
| 3819 | sign = 0; |
| 3820 | |
| 3821 | // Set the significand |
| 3822 | // This format does not have any significand but the 'Pth' precision bit is |
| 3823 | // always set to 1 for consistency in APFloat's internal representation. |
| 3824 | uint64_t mysignificand = 1; |
| 3825 | significandParts()[0] = mysignificand; |
| 3826 | |
| 3827 | // This format can either have a NaN or fcNormal |
| 3828 | // All 1's i.e. 255 is a NaN |
| 3829 | if (val == exponent_mask) { |
| 3830 | category = fcNaN; |
| 3831 | exponent = exponentNaN(); |
| 3832 | return; |
| 3833 | } |
| 3834 | // Handle fcNormal... |
| 3835 | category = fcNormal; |
| 3836 | exponent = myexponent - 127; // 127 is bias |
| 3837 | } |
| 3838 | template <const fltSemantics &S> |
| 3839 | void IEEEFloat::initFromIEEEAPInt(const APInt &api) { |
| 3840 | assert(api.getBitWidth() == S.sizeInBits); |
| 3841 | constexpr integerPart integer_bit = integerPart{1} |
| 3842 | << ((S.precision - 1) % integerPartWidth); |
| 3843 | constexpr uint64_t significand_mask = integer_bit - 1; |
| 3844 | constexpr unsigned int trailing_significand_bits = S.precision - 1; |
| 3845 | constexpr unsigned int stored_significand_parts = |
| 3846 | partCountForBits(bits: trailing_significand_bits); |
| 3847 | constexpr unsigned int exponent_bits = |
| 3848 | S.sizeInBits - 1 - trailing_significand_bits; |
| 3849 | static_assert(exponent_bits < 64); |
| 3850 | constexpr uint64_t exponent_mask = (uint64_t{1} << exponent_bits) - 1; |
| 3851 | constexpr int bias = -(S.minExponent - 1); |
| 3852 | |
| 3853 | // Copy the bits of the significand. We need to clear out the exponent and |
| 3854 | // sign bit in the last word. |
| 3855 | std::array<integerPart, stored_significand_parts> mysignificand; |
| 3856 | std::copy_n(api.getRawData(), mysignificand.size(), mysignificand.begin()); |
| 3857 | if constexpr (significand_mask != 0) { |
| 3858 | mysignificand[mysignificand.size() - 1] &= significand_mask; |
| 3859 | } |
| 3860 | |
| 3861 | // We assume the last word holds the sign bit, the exponent, and potentially |
| 3862 | // some of the trailing significand field. |
| 3863 | uint64_t last_word = api.getRawData()[api.getNumWords() - 1]; |
| 3864 | uint64_t myexponent = |
| 3865 | (last_word >> (trailing_significand_bits % 64)) & exponent_mask; |
| 3866 | |
| 3867 | initialize(ourSemantics: &S); |
| 3868 | assert(partCount() == mysignificand.size()); |
| 3869 | |
| 3870 | sign = static_cast<unsigned int>(last_word >> ((S.sizeInBits - 1) % 64)); |
| 3871 | |
| 3872 | bool all_zero_significand = llvm::all_of(mysignificand, equal_to(Arg: 0)); |
| 3873 | |
| 3874 | bool is_zero = myexponent == 0 && all_zero_significand; |
| 3875 | |
| 3876 | if constexpr (S.nonFiniteBehavior == fltNonfiniteBehavior::IEEE754) { |
| 3877 | if (myexponent - bias == ::exponentInf(semantics: S) && all_zero_significand) { |
| 3878 | makeInf(Neg: sign); |
| 3879 | return; |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | bool is_nan = false; |
| 3884 | |
| 3885 | if constexpr (S.nanEncoding == fltNanEncoding::IEEE) { |
| 3886 | is_nan = myexponent - bias == ::exponentNaN(semantics: S) && !all_zero_significand; |
| 3887 | } else if constexpr (S.nanEncoding == fltNanEncoding::AllOnes) { |
| 3888 | bool all_ones_significand = |
| 3889 | std::all_of(mysignificand.begin(), mysignificand.end() - 1, |
| 3890 | [](integerPart bits) { return bits == ~integerPart{0}; }) && |
| 3891 | (!significand_mask || |
| 3892 | mysignificand[mysignificand.size() - 1] == significand_mask); |
| 3893 | is_nan = myexponent - bias == ::exponentNaN(semantics: S) && all_ones_significand; |
| 3894 | } else if constexpr (S.nanEncoding == fltNanEncoding::NegativeZero) { |
| 3895 | is_nan = is_zero && sign; |
| 3896 | } |
| 3897 | |
| 3898 | if (is_nan) { |
| 3899 | category = fcNaN; |
| 3900 | exponent = ::exponentNaN(semantics: S); |
| 3901 | std::copy_n(mysignificand.begin(), mysignificand.size(), |
| 3902 | significandParts()); |
| 3903 | return; |
| 3904 | } |
| 3905 | |
| 3906 | if (is_zero) { |
| 3907 | makeZero(Neg: sign); |
| 3908 | return; |
| 3909 | } |
| 3910 | |
| 3911 | category = fcNormal; |
| 3912 | exponent = myexponent - bias; |
| 3913 | std::copy_n(mysignificand.begin(), mysignificand.size(), significandParts()); |
| 3914 | if (myexponent == 0) // denormal |
| 3915 | exponent = S.minExponent; |
| 3916 | else |
| 3917 | significandParts()[mysignificand.size()-1] |= integer_bit; // integer bit |
| 3918 | } |
| 3919 | |
| 3920 | void IEEEFloat::initFromQuadrupleAPInt(const APInt &api) { |
| 3921 | initFromIEEEAPInt<APFloatBase::semIEEEquad>(api); |
| 3922 | } |
| 3923 | |
| 3924 | void IEEEFloat::initFromDoubleAPInt(const APInt &api) { |
| 3925 | initFromIEEEAPInt<APFloatBase::semIEEEdouble>(api); |
| 3926 | } |
| 3927 | |
| 3928 | void IEEEFloat::initFromFloatAPInt(const APInt &api) { |
| 3929 | initFromIEEEAPInt<APFloatBase::semIEEEsingle>(api); |
| 3930 | } |
| 3931 | |
| 3932 | void IEEEFloat::initFromBFloatAPInt(const APInt &api) { |
| 3933 | initFromIEEEAPInt<APFloatBase::semBFloat>(api); |
| 3934 | } |
| 3935 | |
| 3936 | void IEEEFloat::initFromHalfAPInt(const APInt &api) { |
| 3937 | initFromIEEEAPInt<APFloatBase::semIEEEhalf>(api); |
| 3938 | } |
| 3939 | |
| 3940 | void IEEEFloat::initFromFloat8E5M2APInt(const APInt &api) { |
| 3941 | initFromIEEEAPInt<APFloatBase::semFloat8E5M2>(api); |
| 3942 | } |
| 3943 | |
| 3944 | void IEEEFloat::initFromFloat8E5M2FNUZAPInt(const APInt &api) { |
| 3945 | initFromIEEEAPInt<APFloatBase::semFloat8E5M2FNUZ>(api); |
| 3946 | } |
| 3947 | |
| 3948 | void IEEEFloat::initFromFloat8E4M3APInt(const APInt &api) { |
| 3949 | initFromIEEEAPInt<APFloatBase::semFloat8E4M3>(api); |
| 3950 | } |
| 3951 | |
| 3952 | void IEEEFloat::initFromFloat8E4M3FNAPInt(const APInt &api) { |
| 3953 | initFromIEEEAPInt<APFloatBase::semFloat8E4M3FN>(api); |
| 3954 | } |
| 3955 | |
| 3956 | void IEEEFloat::initFromFloat8E4M3FNUZAPInt(const APInt &api) { |
| 3957 | initFromIEEEAPInt<APFloatBase::semFloat8E4M3FNUZ>(api); |
| 3958 | } |
| 3959 | |
| 3960 | void IEEEFloat::initFromFloat8E4M3B11FNUZAPInt(const APInt &api) { |
| 3961 | initFromIEEEAPInt<APFloatBase::semFloat8E4M3B11FNUZ>(api); |
| 3962 | } |
| 3963 | |
| 3964 | void IEEEFloat::initFromFloat8E3M4APInt(const APInt &api) { |
| 3965 | initFromIEEEAPInt<APFloatBase::semFloat8E3M4>(api); |
| 3966 | } |
| 3967 | |
| 3968 | void IEEEFloat::initFromFloatTF32APInt(const APInt &api) { |
| 3969 | initFromIEEEAPInt<APFloatBase::semFloatTF32>(api); |
| 3970 | } |
| 3971 | |
| 3972 | void IEEEFloat::initFromFloat6E3M2FNAPInt(const APInt &api) { |
| 3973 | initFromIEEEAPInt<APFloatBase::semFloat6E3M2FN>(api); |
| 3974 | } |
| 3975 | |
| 3976 | void IEEEFloat::initFromFloat6E2M3FNAPInt(const APInt &api) { |
| 3977 | initFromIEEEAPInt<APFloatBase::semFloat6E2M3FN>(api); |
| 3978 | } |
| 3979 | |
| 3980 | void IEEEFloat::initFromFloat4E2M1FNAPInt(const APInt &api) { |
| 3981 | initFromIEEEAPInt<APFloatBase::semFloat4E2M1FN>(api); |
| 3982 | } |
| 3983 | |
| 3984 | /// Treat api as containing the bits of a floating point number. |
| 3985 | void IEEEFloat::initFromAPInt(const fltSemantics *Sem, const APInt &api) { |
| 3986 | assert(api.getBitWidth() == Sem->sizeInBits); |
| 3987 | if (Sem == &APFloatBase::semIEEEhalf) |
| 3988 | return initFromHalfAPInt(api); |
| 3989 | if (Sem == &APFloatBase::semBFloat) |
| 3990 | return initFromBFloatAPInt(api); |
| 3991 | if (Sem == &APFloatBase::semIEEEsingle) |
| 3992 | return initFromFloatAPInt(api); |
| 3993 | if (Sem == &APFloatBase::semIEEEdouble) |
| 3994 | return initFromDoubleAPInt(api); |
| 3995 | if (Sem == &APFloatBase::semX87DoubleExtended) |
| 3996 | return initFromF80LongDoubleAPInt(api); |
| 3997 | if (Sem == &APFloatBase::semIEEEquad) |
| 3998 | return initFromQuadrupleAPInt(api); |
| 3999 | if (Sem == &APFloatBase::semPPCDoubleDoubleLegacy) |
| 4000 | return initFromPPCDoubleDoubleLegacyAPInt(api); |
| 4001 | if (Sem == &APFloatBase::semFloat8E5M2) |
| 4002 | return initFromFloat8E5M2APInt(api); |
| 4003 | if (Sem == &APFloatBase::semFloat8E5M2FNUZ) |
| 4004 | return initFromFloat8E5M2FNUZAPInt(api); |
| 4005 | if (Sem == &APFloatBase::semFloat8E4M3) |
| 4006 | return initFromFloat8E4M3APInt(api); |
| 4007 | if (Sem == &APFloatBase::semFloat8E4M3FN) |
| 4008 | return initFromFloat8E4M3FNAPInt(api); |
| 4009 | if (Sem == &APFloatBase::semFloat8E4M3FNUZ) |
| 4010 | return initFromFloat8E4M3FNUZAPInt(api); |
| 4011 | if (Sem == &APFloatBase::semFloat8E4M3B11FNUZ) |
| 4012 | return initFromFloat8E4M3B11FNUZAPInt(api); |
| 4013 | if (Sem == &APFloatBase::semFloat8E3M4) |
| 4014 | return initFromFloat8E3M4APInt(api); |
| 4015 | if (Sem == &APFloatBase::semFloatTF32) |
| 4016 | return initFromFloatTF32APInt(api); |
| 4017 | if (Sem == &APFloatBase::semFloat8E8M0FNU) |
| 4018 | return initFromFloat8E8M0FNUAPInt(api); |
| 4019 | if (Sem == &APFloatBase::semFloat6E3M2FN) |
| 4020 | return initFromFloat6E3M2FNAPInt(api); |
| 4021 | if (Sem == &APFloatBase::semFloat6E2M3FN) |
| 4022 | return initFromFloat6E2M3FNAPInt(api); |
| 4023 | if (Sem == &APFloatBase::semFloat4E2M1FN) |
| 4024 | return initFromFloat4E2M1FNAPInt(api); |
| 4025 | |
| 4026 | llvm_unreachable("unsupported semantics" ); |
| 4027 | } |
| 4028 | |
| 4029 | /// Make this number the largest magnitude normal number in the given |
| 4030 | /// semantics. |
| 4031 | void IEEEFloat::makeLargest(bool Negative) { |
| 4032 | if (Negative && !semantics->hasSignedRepr) |
| 4033 | llvm_unreachable( |
| 4034 | "This floating point format does not support signed values" ); |
| 4035 | // We want (in interchange format): |
| 4036 | // sign = {Negative} |
| 4037 | // exponent = 1..10 |
| 4038 | // significand = 1..1 |
| 4039 | category = fcNormal; |
| 4040 | sign = Negative; |
| 4041 | exponent = semantics->maxExponent; |
| 4042 | |
| 4043 | // Use memset to set all but the highest integerPart to all ones. |
| 4044 | integerPart *significand = significandParts(); |
| 4045 | unsigned PartCount = partCount(); |
| 4046 | memset(s: significand, c: 0xFF, n: sizeof(integerPart)*(PartCount - 1)); |
| 4047 | |
| 4048 | // Set the high integerPart especially setting all unused top bits for |
| 4049 | // internal consistency. |
| 4050 | const unsigned NumUnusedHighBits = |
| 4051 | PartCount*integerPartWidth - semantics->precision; |
| 4052 | significand[PartCount - 1] = (NumUnusedHighBits < integerPartWidth) |
| 4053 | ? (~integerPart(0) >> NumUnusedHighBits) |
| 4054 | : 0; |
| 4055 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly && |
| 4056 | semantics->nanEncoding == fltNanEncoding::AllOnes && |
| 4057 | (semantics->precision > 1)) |
| 4058 | significand[0] &= ~integerPart(1); |
| 4059 | } |
| 4060 | |
| 4061 | /// Make this number the smallest magnitude denormal number in the given |
| 4062 | /// semantics. |
| 4063 | void IEEEFloat::makeSmallest(bool Negative) { |
| 4064 | if (Negative && !semantics->hasSignedRepr) |
| 4065 | llvm_unreachable( |
| 4066 | "This floating point format does not support signed values" ); |
| 4067 | // We want (in interchange format): |
| 4068 | // sign = {Negative} |
| 4069 | // exponent = 0..0 |
| 4070 | // significand = 0..01 |
| 4071 | category = fcNormal; |
| 4072 | sign = Negative; |
| 4073 | exponent = semantics->minExponent; |
| 4074 | APInt::tcSet(significandParts(), 1, partCount()); |
| 4075 | } |
| 4076 | |
| 4077 | void IEEEFloat::makeSmallestNormalized(bool Negative) { |
| 4078 | if (Negative && !semantics->hasSignedRepr) |
| 4079 | llvm_unreachable( |
| 4080 | "This floating point format does not support signed values" ); |
| 4081 | // We want (in interchange format): |
| 4082 | // sign = {Negative} |
| 4083 | // exponent = 0..0 |
| 4084 | // significand = 10..0 |
| 4085 | |
| 4086 | category = fcNormal; |
| 4087 | zeroSignificand(); |
| 4088 | sign = Negative; |
| 4089 | exponent = semantics->minExponent; |
| 4090 | APInt::tcSetBit(significandParts(), bit: semantics->precision - 1); |
| 4091 | } |
| 4092 | |
| 4093 | IEEEFloat::IEEEFloat(const fltSemantics &Sem, const APInt &API) { |
| 4094 | initFromAPInt(Sem: &Sem, api: API); |
| 4095 | } |
| 4096 | |
| 4097 | IEEEFloat::IEEEFloat(float f) { |
| 4098 | initFromAPInt(Sem: &APFloatBase::semIEEEsingle, api: APInt::floatToBits(V: f)); |
| 4099 | } |
| 4100 | |
| 4101 | IEEEFloat::IEEEFloat(double d) { |
| 4102 | initFromAPInt(Sem: &APFloatBase::semIEEEdouble, api: APInt::doubleToBits(V: d)); |
| 4103 | } |
| 4104 | |
| 4105 | namespace { |
| 4106 | void append(SmallVectorImpl<char> &Buffer, StringRef Str) { |
| 4107 | Buffer.append(in_start: Str.begin(), in_end: Str.end()); |
| 4108 | } |
| 4109 | |
| 4110 | /// Removes data from the given significand until it is no more |
| 4111 | /// precise than is required for the desired precision. |
| 4112 | void AdjustToPrecision(APInt &significand, |
| 4113 | int &exp, unsigned FormatPrecision) { |
| 4114 | unsigned bits = significand.getActiveBits(); |
| 4115 | |
| 4116 | // 196/59 is a very slight overestimate of lg_2(10). |
| 4117 | unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59; |
| 4118 | |
| 4119 | if (bits <= bitsRequired) return; |
| 4120 | |
| 4121 | unsigned tensRemovable = (bits - bitsRequired) * 59 / 196; |
| 4122 | if (!tensRemovable) return; |
| 4123 | |
| 4124 | exp += tensRemovable; |
| 4125 | |
| 4126 | APInt divisor(significand.getBitWidth(), 1); |
| 4127 | APInt powten(significand.getBitWidth(), 10); |
| 4128 | while (true) { |
| 4129 | if (tensRemovable & 1) |
| 4130 | divisor *= powten; |
| 4131 | tensRemovable >>= 1; |
| 4132 | if (!tensRemovable) break; |
| 4133 | powten *= powten; |
| 4134 | } |
| 4135 | |
| 4136 | significand = significand.udiv(RHS: divisor); |
| 4137 | |
| 4138 | // Truncate the significand down to its active bit count. |
| 4139 | significand = significand.trunc(width: significand.getActiveBits()); |
| 4140 | } |
| 4141 | |
| 4142 | |
| 4143 | void AdjustToPrecision(SmallVectorImpl<char> &buffer, |
| 4144 | int &exp, unsigned FormatPrecision) { |
| 4145 | unsigned N = buffer.size(); |
| 4146 | if (N <= FormatPrecision) return; |
| 4147 | |
| 4148 | // The most significant figures are the last ones in the buffer. |
| 4149 | unsigned FirstSignificant = N - FormatPrecision; |
| 4150 | |
| 4151 | // Round. |
| 4152 | // FIXME: this probably shouldn't use 'round half up'. |
| 4153 | |
| 4154 | // Rounding down is just a truncation, except we also want to drop |
| 4155 | // trailing zeros from the new result. |
| 4156 | if (buffer[FirstSignificant - 1] < '5') { |
| 4157 | while (FirstSignificant < N && buffer[FirstSignificant] == '0') |
| 4158 | FirstSignificant++; |
| 4159 | |
| 4160 | exp += FirstSignificant; |
| 4161 | buffer.erase(CS: &buffer[0], CE: &buffer[FirstSignificant]); |
| 4162 | return; |
| 4163 | } |
| 4164 | |
| 4165 | // Rounding up requires a decimal add-with-carry. If we continue |
| 4166 | // the carry, the newly-introduced zeros will just be truncated. |
| 4167 | for (unsigned I = FirstSignificant; I != N; ++I) { |
| 4168 | if (buffer[I] == '9') { |
| 4169 | FirstSignificant++; |
| 4170 | } else { |
| 4171 | buffer[I]++; |
| 4172 | break; |
| 4173 | } |
| 4174 | } |
| 4175 | |
| 4176 | // If we carried through, we have exactly one digit of precision. |
| 4177 | if (FirstSignificant == N) { |
| 4178 | exp += FirstSignificant; |
| 4179 | buffer.clear(); |
| 4180 | buffer.push_back(Elt: '1'); |
| 4181 | return; |
| 4182 | } |
| 4183 | |
| 4184 | exp += FirstSignificant; |
| 4185 | buffer.erase(CS: &buffer[0], CE: &buffer[FirstSignificant]); |
| 4186 | } |
| 4187 | |
| 4188 | void toStringImpl(SmallVectorImpl<char> &Str, const bool isNeg, int exp, |
| 4189 | APInt significand, unsigned FormatPrecision, |
| 4190 | unsigned FormatMaxPadding, bool TruncateZero) { |
| 4191 | const int semanticsPrecision = significand.getBitWidth(); |
| 4192 | |
| 4193 | if (isNeg) |
| 4194 | Str.push_back(Elt: '-'); |
| 4195 | |
| 4196 | // Set FormatPrecision if zero. We want to do this before we |
| 4197 | // truncate trailing zeros, as those are part of the precision. |
| 4198 | if (!FormatPrecision) { |
| 4199 | // We use enough digits so the number can be round-tripped back to an |
| 4200 | // APFloat. The formula comes from "How to Print Floating-Point Numbers |
| 4201 | // Accurately" by Steele and White. |
| 4202 | // FIXME: Using a formula based purely on the precision is conservative; |
| 4203 | // we can print fewer digits depending on the actual value being printed. |
| 4204 | |
| 4205 | // FormatPrecision = 2 + floor(significandBits / lg_2(10)) |
| 4206 | FormatPrecision = 2 + semanticsPrecision * 59 / 196; |
| 4207 | } |
| 4208 | |
| 4209 | // Ignore trailing binary zeros. |
| 4210 | int trailingZeros = significand.countr_zero(); |
| 4211 | exp += trailingZeros; |
| 4212 | significand.lshrInPlace(ShiftAmt: trailingZeros); |
| 4213 | |
| 4214 | // Change the exponent from 2^e to 10^e. |
| 4215 | if (exp == 0) { |
| 4216 | // Nothing to do. |
| 4217 | } else if (exp > 0) { |
| 4218 | // Just shift left. |
| 4219 | significand = significand.zext(width: semanticsPrecision + exp); |
| 4220 | significand <<= exp; |
| 4221 | exp = 0; |
| 4222 | } else { /* exp < 0 */ |
| 4223 | int texp = -exp; |
| 4224 | |
| 4225 | // We transform this using the identity: |
| 4226 | // (N)(2^-e) == (N)(5^e)(10^-e) |
| 4227 | // This means we have to multiply N (the significand) by 5^e. |
| 4228 | // To avoid overflow, we have to operate on numbers large |
| 4229 | // enough to store N * 5^e: |
| 4230 | // log2(N * 5^e) == log2(N) + e * log2(5) |
| 4231 | // <= semantics->precision + e * 137 / 59 |
| 4232 | // (log_2(5) ~ 2.321928 < 2.322034 ~ 137/59) |
| 4233 | |
| 4234 | unsigned precision = semanticsPrecision + (137 * texp + 136) / 59; |
| 4235 | |
| 4236 | // Multiply significand by 5^e. |
| 4237 | // N * 5^0101 == N * 5^(1*1) * 5^(0*2) * 5^(1*4) * 5^(0*8) |
| 4238 | significand = significand.zext(width: precision); |
| 4239 | APInt five_to_the_i(precision, 5); |
| 4240 | while (true) { |
| 4241 | if (texp & 1) |
| 4242 | significand *= five_to_the_i; |
| 4243 | |
| 4244 | texp >>= 1; |
| 4245 | if (!texp) |
| 4246 | break; |
| 4247 | five_to_the_i *= five_to_the_i; |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | AdjustToPrecision(significand, exp, FormatPrecision); |
| 4252 | |
| 4253 | SmallVector<char, 256> buffer; |
| 4254 | |
| 4255 | // Fill the buffer. |
| 4256 | unsigned precision = significand.getBitWidth(); |
| 4257 | if (precision < 4) { |
| 4258 | // We need enough precision to store the value 10. |
| 4259 | precision = 4; |
| 4260 | significand = significand.zext(width: precision); |
| 4261 | } |
| 4262 | APInt ten(precision, 10); |
| 4263 | APInt digit(precision, 0); |
| 4264 | |
| 4265 | bool inTrail = true; |
| 4266 | while (significand != 0) { |
| 4267 | // digit <- significand % 10 |
| 4268 | // significand <- significand / 10 |
| 4269 | APInt::udivrem(LHS: significand, RHS: ten, Quotient&: significand, Remainder&: digit); |
| 4270 | |
| 4271 | unsigned d = digit.getZExtValue(); |
| 4272 | |
| 4273 | // Drop trailing zeros. |
| 4274 | if (inTrail && !d) |
| 4275 | exp++; |
| 4276 | else { |
| 4277 | buffer.push_back(Elt: (char) ('0' + d)); |
| 4278 | inTrail = false; |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 4282 | assert(!buffer.empty() && "no characters in buffer!" ); |
| 4283 | |
| 4284 | // Drop down to FormatPrecision. |
| 4285 | // TODO: don't do more precise calculations above than are required. |
| 4286 | AdjustToPrecision(buffer, exp, FormatPrecision); |
| 4287 | |
| 4288 | unsigned NDigits = buffer.size(); |
| 4289 | |
| 4290 | // Check whether we should use scientific notation. |
| 4291 | bool FormatScientific; |
| 4292 | if (!FormatMaxPadding) |
| 4293 | FormatScientific = true; |
| 4294 | else { |
| 4295 | if (exp >= 0) { |
| 4296 | // 765e3 --> 765000 |
| 4297 | // ^^^ |
| 4298 | // But we shouldn't make the number look more precise than it is. |
| 4299 | FormatScientific = ((unsigned) exp > FormatMaxPadding || |
| 4300 | NDigits + (unsigned) exp > FormatPrecision); |
| 4301 | } else { |
| 4302 | // Power of the most significant digit. |
| 4303 | int MSD = exp + (int) (NDigits - 1); |
| 4304 | if (MSD >= 0) { |
| 4305 | // 765e-2 == 7.65 |
| 4306 | FormatScientific = false; |
| 4307 | } else { |
| 4308 | // 765e-5 == 0.00765 |
| 4309 | // ^ ^^ |
| 4310 | FormatScientific = ((unsigned) -MSD) > FormatMaxPadding; |
| 4311 | } |
| 4312 | } |
| 4313 | } |
| 4314 | |
| 4315 | // Scientific formatting is pretty straightforward. |
| 4316 | if (FormatScientific) { |
| 4317 | exp += (NDigits - 1); |
| 4318 | |
| 4319 | Str.push_back(Elt: buffer[NDigits-1]); |
| 4320 | Str.push_back(Elt: '.'); |
| 4321 | if (NDigits == 1 && TruncateZero) |
| 4322 | Str.push_back(Elt: '0'); |
| 4323 | else |
| 4324 | for (unsigned I = 1; I != NDigits; ++I) |
| 4325 | Str.push_back(Elt: buffer[NDigits-1-I]); |
| 4326 | // Fill with zeros up to FormatPrecision. |
| 4327 | if (!TruncateZero && FormatPrecision > NDigits - 1) |
| 4328 | Str.append(NumInputs: FormatPrecision - NDigits + 1, Elt: '0'); |
| 4329 | // For !TruncateZero we use lower 'e'. |
| 4330 | Str.push_back(Elt: TruncateZero ? 'E' : 'e'); |
| 4331 | |
| 4332 | Str.push_back(Elt: exp >= 0 ? '+' : '-'); |
| 4333 | if (exp < 0) |
| 4334 | exp = -exp; |
| 4335 | SmallVector<char, 6> expbuf; |
| 4336 | do { |
| 4337 | expbuf.push_back(Elt: (char) ('0' + (exp % 10))); |
| 4338 | exp /= 10; |
| 4339 | } while (exp); |
| 4340 | // Exponent always at least two digits if we do not truncate zeros. |
| 4341 | if (!TruncateZero && expbuf.size() < 2) |
| 4342 | expbuf.push_back(Elt: '0'); |
| 4343 | for (unsigned I = 0, E = expbuf.size(); I != E; ++I) |
| 4344 | Str.push_back(Elt: expbuf[E-1-I]); |
| 4345 | return; |
| 4346 | } |
| 4347 | |
| 4348 | // Non-scientific, positive exponents. |
| 4349 | if (exp >= 0) { |
| 4350 | for (unsigned I = 0; I != NDigits; ++I) |
| 4351 | Str.push_back(Elt: buffer[NDigits-1-I]); |
| 4352 | for (unsigned I = 0; I != (unsigned) exp; ++I) |
| 4353 | Str.push_back(Elt: '0'); |
| 4354 | return; |
| 4355 | } |
| 4356 | |
| 4357 | // Non-scientific, negative exponents. |
| 4358 | |
| 4359 | // The number of digits to the left of the decimal point. |
| 4360 | int NWholeDigits = exp + (int) NDigits; |
| 4361 | |
| 4362 | unsigned I = 0; |
| 4363 | if (NWholeDigits > 0) { |
| 4364 | for (; I != (unsigned) NWholeDigits; ++I) |
| 4365 | Str.push_back(Elt: buffer[NDigits-I-1]); |
| 4366 | Str.push_back(Elt: '.'); |
| 4367 | } else { |
| 4368 | unsigned NZeros = 1 + (unsigned) -NWholeDigits; |
| 4369 | |
| 4370 | Str.push_back(Elt: '0'); |
| 4371 | Str.push_back(Elt: '.'); |
| 4372 | for (unsigned Z = 1; Z != NZeros; ++Z) |
| 4373 | Str.push_back(Elt: '0'); |
| 4374 | } |
| 4375 | |
| 4376 | for (; I != NDigits; ++I) |
| 4377 | Str.push_back(Elt: buffer[NDigits-I-1]); |
| 4378 | |
| 4379 | } |
| 4380 | } // namespace |
| 4381 | |
| 4382 | void IEEEFloat::toString(SmallVectorImpl<char> &Str, unsigned FormatPrecision, |
| 4383 | unsigned FormatMaxPadding, bool TruncateZero) const { |
| 4384 | switch (category) { |
| 4385 | case fcInfinity: |
| 4386 | if (isNegative()) |
| 4387 | return append(Buffer&: Str, Str: "-Inf" ); |
| 4388 | else |
| 4389 | return append(Buffer&: Str, Str: "+Inf" ); |
| 4390 | |
| 4391 | case fcNaN: return append(Buffer&: Str, Str: "NaN" ); |
| 4392 | |
| 4393 | case fcZero: |
| 4394 | if (isNegative()) |
| 4395 | Str.push_back(Elt: '-'); |
| 4396 | |
| 4397 | if (!FormatMaxPadding) { |
| 4398 | if (TruncateZero) |
| 4399 | append(Buffer&: Str, Str: "0.0E+0" ); |
| 4400 | else { |
| 4401 | append(Buffer&: Str, Str: "0.0" ); |
| 4402 | if (FormatPrecision > 1) |
| 4403 | Str.append(NumInputs: FormatPrecision - 1, Elt: '0'); |
| 4404 | append(Buffer&: Str, Str: "e+00" ); |
| 4405 | } |
| 4406 | } else { |
| 4407 | Str.push_back(Elt: '0'); |
| 4408 | } |
| 4409 | return; |
| 4410 | |
| 4411 | case fcNormal: |
| 4412 | break; |
| 4413 | } |
| 4414 | |
| 4415 | // Decompose the number into an APInt and an exponent. |
| 4416 | int exp = exponent - ((int) semantics->precision - 1); |
| 4417 | APInt significand( |
| 4418 | semantics->precision, |
| 4419 | ArrayRef(significandParts(), partCountForBits(bits: semantics->precision))); |
| 4420 | |
| 4421 | toStringImpl(Str, isNeg: isNegative(), exp, significand, FormatPrecision, |
| 4422 | FormatMaxPadding, TruncateZero); |
| 4423 | |
| 4424 | } |
| 4425 | |
| 4426 | int IEEEFloat::getExactLog2Abs() const { |
| 4427 | if (!isFinite() || isZero()) |
| 4428 | return INT_MIN; |
| 4429 | |
| 4430 | const integerPart *Parts = significandParts(); |
| 4431 | const int PartCount = partCountForBits(bits: semantics->precision); |
| 4432 | |
| 4433 | int PopCount = 0; |
| 4434 | for (int i = 0; i < PartCount; ++i) { |
| 4435 | PopCount += llvm::popcount(Value: Parts[i]); |
| 4436 | if (PopCount > 1) |
| 4437 | return INT_MIN; |
| 4438 | } |
| 4439 | |
| 4440 | if (exponent != semantics->minExponent) |
| 4441 | return exponent; |
| 4442 | |
| 4443 | int CountrParts = 0; |
| 4444 | for (int i = 0; i < PartCount; |
| 4445 | ++i, CountrParts += APInt::APINT_BITS_PER_WORD) { |
| 4446 | if (Parts[i] != 0) { |
| 4447 | return exponent - semantics->precision + CountrParts + |
| 4448 | llvm::countr_zero(Val: Parts[i]) + 1; |
| 4449 | } |
| 4450 | } |
| 4451 | |
| 4452 | llvm_unreachable("didn't find the set bit" ); |
| 4453 | } |
| 4454 | |
| 4455 | bool IEEEFloat::isSignaling() const { |
| 4456 | if (!isNaN()) |
| 4457 | return false; |
| 4458 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly || |
| 4459 | semantics->nonFiniteBehavior == fltNonfiniteBehavior::FiniteOnly) |
| 4460 | return false; |
| 4461 | |
| 4462 | // IEEE-754R 2008 6.2.1: A signaling NaN bit string should be encoded with the |
| 4463 | // first bit of the trailing significand being 0. |
| 4464 | return !APInt::tcExtractBit(significandParts(), bit: semantics->precision - 2); |
| 4465 | } |
| 4466 | |
| 4467 | /// IEEE-754R 2008 5.3.1: nextUp/nextDown. |
| 4468 | /// |
| 4469 | /// *NOTE* since nextDown(x) = -nextUp(-x), we only implement nextUp with |
| 4470 | /// appropriate sign switching before/after the computation. |
| 4471 | APFloat::opStatus IEEEFloat::next(bool nextDown) { |
| 4472 | // If we are performing nextDown, swap sign so we have -x. |
| 4473 | if (nextDown) |
| 4474 | changeSign(); |
| 4475 | |
| 4476 | // Compute nextUp(x) |
| 4477 | opStatus result = opOK; |
| 4478 | |
| 4479 | // Handle each float category separately. |
| 4480 | switch (category) { |
| 4481 | case fcInfinity: |
| 4482 | // nextUp(+inf) = +inf |
| 4483 | if (!isNegative()) |
| 4484 | break; |
| 4485 | // nextUp(-inf) = -getLargest() |
| 4486 | makeLargest(Negative: true); |
| 4487 | break; |
| 4488 | case fcNaN: |
| 4489 | // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag. |
| 4490 | // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not |
| 4491 | // change the payload. |
| 4492 | if (isSignaling()) { |
| 4493 | result = opInvalidOp; |
| 4494 | // For consistency, propagate the sign of the sNaN to the qNaN. |
| 4495 | makeNaN(SNaN: false, Negative: isNegative(), fill: nullptr); |
| 4496 | } |
| 4497 | break; |
| 4498 | case fcZero: |
| 4499 | // nextUp(pm 0) = +getSmallest() |
| 4500 | makeSmallest(Negative: false); |
| 4501 | break; |
| 4502 | case fcNormal: |
| 4503 | // nextUp(-getSmallest()) = -0 |
| 4504 | if (isSmallest() && isNegative()) { |
| 4505 | APInt::tcSet(significandParts(), 0, partCount()); |
| 4506 | category = fcZero; |
| 4507 | exponent = 0; |
| 4508 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) |
| 4509 | sign = false; |
| 4510 | if (!semantics->hasZero) |
| 4511 | makeSmallestNormalized(Negative: false); |
| 4512 | break; |
| 4513 | } |
| 4514 | |
| 4515 | if (isLargest() && !isNegative()) { |
| 4516 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { |
| 4517 | // nextUp(getLargest()) == NAN |
| 4518 | makeNaN(); |
| 4519 | break; |
| 4520 | } else if (semantics->nonFiniteBehavior == |
| 4521 | fltNonfiniteBehavior::FiniteOnly) { |
| 4522 | // nextUp(getLargest()) == getLargest() |
| 4523 | break; |
| 4524 | } else { |
| 4525 | // nextUp(getLargest()) == INFINITY |
| 4526 | APInt::tcSet(significandParts(), 0, partCount()); |
| 4527 | category = fcInfinity; |
| 4528 | exponent = semantics->maxExponent + 1; |
| 4529 | break; |
| 4530 | } |
| 4531 | } |
| 4532 | |
| 4533 | // nextUp(normal) == normal + inc. |
| 4534 | if (isNegative()) { |
| 4535 | // If we are negative, we need to decrement the significand. |
| 4536 | |
| 4537 | // We only cross a binade boundary that requires adjusting the exponent |
| 4538 | // if: |
| 4539 | // 1. exponent != semantics->minExponent. This implies we are not in the |
| 4540 | // smallest binade or are dealing with denormals. |
| 4541 | // 2. Our significand excluding the integral bit is all zeros. |
| 4542 | bool WillCrossBinadeBoundary = |
| 4543 | exponent != semantics->minExponent && isSignificandAllZeros(); |
| 4544 | |
| 4545 | // Decrement the significand. |
| 4546 | // |
| 4547 | // We always do this since: |
| 4548 | // 1. If we are dealing with a non-binade decrement, by definition we |
| 4549 | // just decrement the significand. |
| 4550 | // 2. If we are dealing with a normal -> normal binade decrement, since |
| 4551 | // we have an explicit integral bit the fact that all bits but the |
| 4552 | // integral bit are zero implies that subtracting one will yield a |
| 4553 | // significand with 0 integral bit and 1 in all other spots. Thus we |
| 4554 | // must just adjust the exponent and set the integral bit to 1. |
| 4555 | // 3. If we are dealing with a normal -> denormal binade decrement, |
| 4556 | // since we set the integral bit to 0 when we represent denormals, we |
| 4557 | // just decrement the significand. |
| 4558 | integerPart *Parts = significandParts(); |
| 4559 | APInt::tcDecrement(dst: Parts, parts: partCount()); |
| 4560 | |
| 4561 | if (WillCrossBinadeBoundary) { |
| 4562 | // Our result is a normal number. Do the following: |
| 4563 | // 1. Set the integral bit to 1. |
| 4564 | // 2. Decrement the exponent. |
| 4565 | APInt::tcSetBit(Parts, bit: semantics->precision - 1); |
| 4566 | exponent--; |
| 4567 | } |
| 4568 | } else { |
| 4569 | // If we are positive, we need to increment the significand. |
| 4570 | |
| 4571 | // We only cross a binade boundary that requires adjusting the exponent if |
| 4572 | // the input is not a denormal and all of said input's significand bits |
| 4573 | // are set. If all of said conditions are true: clear the significand, set |
| 4574 | // the integral bit to 1, and increment the exponent. If we have a |
| 4575 | // denormal always increment since moving denormals and the numbers in the |
| 4576 | // smallest normal binade have the same exponent in our representation. |
| 4577 | // If there are only exponents, any increment always crosses the |
| 4578 | // BinadeBoundary. |
| 4579 | bool WillCrossBinadeBoundary = !APFloat::hasSignificand(Sem: *semantics) || |
| 4580 | (!isDenormal() && isSignificandAllOnes()); |
| 4581 | |
| 4582 | if (WillCrossBinadeBoundary) { |
| 4583 | integerPart *Parts = significandParts(); |
| 4584 | APInt::tcSet(Parts, 0, partCount()); |
| 4585 | APInt::tcSetBit(Parts, bit: semantics->precision - 1); |
| 4586 | assert(exponent != semantics->maxExponent && |
| 4587 | "We can not increment an exponent beyond the maxExponent allowed" |
| 4588 | " by the given floating point semantics." ); |
| 4589 | exponent++; |
| 4590 | } else { |
| 4591 | incrementSignificand(); |
| 4592 | } |
| 4593 | } |
| 4594 | break; |
| 4595 | } |
| 4596 | |
| 4597 | // If we are performing nextDown, swap sign so we have -nextUp(-x) |
| 4598 | if (nextDown) |
| 4599 | changeSign(); |
| 4600 | |
| 4601 | return result; |
| 4602 | } |
| 4603 | |
| 4604 | APFloatBase::ExponentType IEEEFloat::exponentNaN() const { |
| 4605 | return ::exponentNaN(semantics: *semantics); |
| 4606 | } |
| 4607 | |
| 4608 | APFloatBase::ExponentType IEEEFloat::exponentInf() const { |
| 4609 | return ::exponentInf(semantics: *semantics); |
| 4610 | } |
| 4611 | |
| 4612 | APFloatBase::ExponentType IEEEFloat::exponentZero() const { |
| 4613 | return ::exponentZero(semantics: *semantics); |
| 4614 | } |
| 4615 | |
| 4616 | void IEEEFloat::makeInf(bool Negative) { |
| 4617 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::FiniteOnly) |
| 4618 | llvm_unreachable("This floating point format does not support Inf" ); |
| 4619 | |
| 4620 | if (semantics->nonFiniteBehavior == fltNonfiniteBehavior::NanOnly) { |
| 4621 | // There is no Inf, so make NaN instead. |
| 4622 | makeNaN(SNaN: false, Negative); |
| 4623 | return; |
| 4624 | } |
| 4625 | category = fcInfinity; |
| 4626 | sign = Negative; |
| 4627 | exponent = exponentInf(); |
| 4628 | APInt::tcSet(significandParts(), 0, partCount()); |
| 4629 | } |
| 4630 | |
| 4631 | void IEEEFloat::makeZero(bool Negative) { |
| 4632 | if (!semantics->hasZero) |
| 4633 | llvm_unreachable("This floating point format does not support Zero" ); |
| 4634 | |
| 4635 | category = fcZero; |
| 4636 | sign = Negative; |
| 4637 | if (semantics->nanEncoding == fltNanEncoding::NegativeZero) { |
| 4638 | // Merge negative zero to positive because 0b10000...000 is used for NaN |
| 4639 | sign = false; |
| 4640 | } |
| 4641 | exponent = exponentZero(); |
| 4642 | APInt::tcSet(significandParts(), 0, partCount()); |
| 4643 | } |
| 4644 | |
| 4645 | void IEEEFloat::makeQuiet() { |
| 4646 | assert(isNaN()); |
| 4647 | if (semantics->nonFiniteBehavior != fltNonfiniteBehavior::NanOnly) |
| 4648 | APInt::tcSetBit(significandParts(), bit: semantics->precision - 2); |
| 4649 | } |
| 4650 | |
| 4651 | int ilogb(const IEEEFloat &Arg) { |
| 4652 | if (Arg.isNaN()) |
| 4653 | return APFloat::IEK_NaN; |
| 4654 | if (Arg.isZero()) |
| 4655 | return APFloat::IEK_Zero; |
| 4656 | if (Arg.isInfinity()) |
| 4657 | return APFloat::IEK_Inf; |
| 4658 | if (!Arg.isDenormal()) |
| 4659 | return Arg.exponent; |
| 4660 | |
| 4661 | IEEEFloat Normalized(Arg); |
| 4662 | int SignificandBits = Arg.getSemantics().precision - 1; |
| 4663 | |
| 4664 | Normalized.exponent += SignificandBits; |
| 4665 | Normalized.normalize(rounding_mode: APFloat::rmNearestTiesToEven, lost_fraction: lfExactlyZero); |
| 4666 | return Normalized.exponent - SignificandBits; |
| 4667 | } |
| 4668 | |
| 4669 | IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode RoundingMode) { |
| 4670 | auto MaxExp = X.getSemantics().maxExponent; |
| 4671 | auto MinExp = X.getSemantics().minExponent; |
| 4672 | |
| 4673 | // If Exp is wildly out-of-scale, simply adding it to X.exponent will |
| 4674 | // overflow; clamp it to a safe range before adding, but ensure that the range |
| 4675 | // is large enough that the clamp does not change the result. The range we |
| 4676 | // need to support is the difference between the largest possible exponent and |
| 4677 | // the normalized exponent of half the smallest denormal. |
| 4678 | |
| 4679 | int SignificandBits = X.getSemantics().precision - 1; |
| 4680 | int MaxIncrement = MaxExp - (MinExp - SignificandBits) + 1; |
| 4681 | |
| 4682 | // Clamp to one past the range ends to let normalize handle overlflow. |
| 4683 | X.exponent += std::clamp(val: Exp, lo: -MaxIncrement - 1, hi: MaxIncrement); |
| 4684 | X.normalize(rounding_mode: RoundingMode, lost_fraction: lfExactlyZero); |
| 4685 | if (X.isNaN()) |
| 4686 | X.makeQuiet(); |
| 4687 | return X; |
| 4688 | } |
| 4689 | |
| 4690 | IEEEFloat frexp(const IEEEFloat &Val, int &Exp, roundingMode RM) { |
| 4691 | Exp = ilogb(Arg: Val); |
| 4692 | |
| 4693 | // Quiet signalling nans. |
| 4694 | if (Exp == APFloat::IEK_NaN) { |
| 4695 | IEEEFloat Quiet(Val); |
| 4696 | Quiet.makeQuiet(); |
| 4697 | return Quiet; |
| 4698 | } |
| 4699 | |
| 4700 | if (Exp == APFloat::IEK_Inf) |
| 4701 | return Val; |
| 4702 | |
| 4703 | // 1 is added because frexp is defined to return a normalized fraction in |
| 4704 | // +/-[0.5, 1.0), rather than the usual +/-[1.0, 2.0). |
| 4705 | Exp = Exp == APFloat::IEK_Zero ? 0 : Exp + 1; |
| 4706 | return scalbn(X: Val, Exp: -Exp, RoundingMode: RM); |
| 4707 | } |
| 4708 | |
| 4709 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S) |
| 4710 | : Semantics(&S), |
| 4711 | Floats(new APFloat[2]{APFloat(APFloatBase::semIEEEdouble), |
| 4712 | APFloat(APFloatBase::semIEEEdouble)}) { |
| 4713 | assert(Semantics == &APFloatBase::semPPCDoubleDouble); |
| 4714 | } |
| 4715 | |
| 4716 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, uninitializedTag) |
| 4717 | : Semantics(&S), Floats(new APFloat[2]{ |
| 4718 | APFloat(APFloatBase::semIEEEdouble, uninitialized), |
| 4719 | APFloat(APFloatBase::semIEEEdouble, uninitialized)}) { |
| 4720 | assert(Semantics == &APFloatBase::semPPCDoubleDouble); |
| 4721 | } |
| 4722 | |
| 4723 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, integerPart I) |
| 4724 | : Semantics(&S), |
| 4725 | Floats(new APFloat[2]{APFloat(APFloatBase::semIEEEdouble, I), |
| 4726 | APFloat(APFloatBase::semIEEEdouble)}) { |
| 4727 | assert(Semantics == &APFloatBase::semPPCDoubleDouble); |
| 4728 | } |
| 4729 | |
| 4730 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, const APInt &I) |
| 4731 | : Semantics(&S), |
| 4732 | Floats(new APFloat[2]{ |
| 4733 | APFloat(APFloatBase::semIEEEdouble, APInt(64, I.getRawData()[0])), |
| 4734 | APFloat(APFloatBase::semIEEEdouble, APInt(64, I.getRawData()[1]))}) { |
| 4735 | assert(Semantics == &APFloatBase::semPPCDoubleDouble); |
| 4736 | } |
| 4737 | |
| 4738 | DoubleAPFloat::DoubleAPFloat(const fltSemantics &S, APFloat &&First, |
| 4739 | APFloat &&Second) |
| 4740 | : Semantics(&S), |
| 4741 | Floats(new APFloat[2]{std::move(First), std::move(Second)}) { |
| 4742 | assert(Semantics == &APFloatBase::semPPCDoubleDouble); |
| 4743 | assert(&Floats[0].getSemantics() == &APFloatBase::semIEEEdouble); |
| 4744 | assert(&Floats[1].getSemantics() == &APFloatBase::semIEEEdouble); |
| 4745 | } |
| 4746 | |
| 4747 | DoubleAPFloat::DoubleAPFloat(const DoubleAPFloat &RHS) |
| 4748 | : Semantics(RHS.Semantics), |
| 4749 | Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]), |
| 4750 | APFloat(RHS.Floats[1])} |
| 4751 | : nullptr) { |
| 4752 | assert(Semantics == &APFloatBase::semPPCDoubleDouble); |
| 4753 | } |
| 4754 | |
| 4755 | DoubleAPFloat::DoubleAPFloat(DoubleAPFloat &&RHS) |
| 4756 | : Semantics(RHS.Semantics), Floats(RHS.Floats) { |
| 4757 | RHS.Semantics = &APFloatBase::semBogus; |
| 4758 | RHS.Floats = nullptr; |
| 4759 | assert(Semantics == &APFloatBase::semPPCDoubleDouble); |
| 4760 | } |
| 4761 | |
| 4762 | DoubleAPFloat &DoubleAPFloat::operator=(const DoubleAPFloat &RHS) { |
| 4763 | if (Semantics == RHS.Semantics && RHS.Floats) { |
| 4764 | Floats[0] = RHS.Floats[0]; |
| 4765 | Floats[1] = RHS.Floats[1]; |
| 4766 | } else if (this != &RHS) { |
| 4767 | this->~DoubleAPFloat(); |
| 4768 | new (this) DoubleAPFloat(RHS); |
| 4769 | } |
| 4770 | return *this; |
| 4771 | } |
| 4772 | |
| 4773 | // Returns a result such that: |
| 4774 | // 1. abs(Lo) <= ulp(Hi)/2 |
| 4775 | // 2. Hi == RTNE(Hi + Lo) |
| 4776 | // 3. Hi + Lo == X + Y |
| 4777 | // |
| 4778 | // Requires that log2(X) >= log2(Y). |
| 4779 | static std::pair<APFloat, APFloat> fastTwoSum(APFloat X, APFloat Y) { |
| 4780 | if (!X.isFinite()) |
| 4781 | return {X, APFloat::getZero(Sem: X.getSemantics(), /*Negative=*/false)}; |
| 4782 | APFloat Hi = X + Y; |
| 4783 | APFloat Delta = Hi - X; |
| 4784 | APFloat Lo = Y - Delta; |
| 4785 | return {Hi, Lo}; |
| 4786 | } |
| 4787 | |
| 4788 | // Implement addition, subtraction, multiplication and division based on: |
| 4789 | // "Software for Doubled-Precision Floating-Point Computations", |
| 4790 | // by Seppo Linnainmaa, ACM TOMS vol 7 no 3, September 1981, pages 272-283. |
| 4791 | APFloat::opStatus DoubleAPFloat::addImpl(const APFloat &a, const APFloat &aa, |
| 4792 | const APFloat &c, const APFloat &cc, |
| 4793 | roundingMode RM) { |
| 4794 | int Status = opOK; |
| 4795 | APFloat z = a; |
| 4796 | Status |= z.add(RHS: c, RM); |
| 4797 | if (!z.isFinite()) { |
| 4798 | if (!z.isInfinity()) { |
| 4799 | Floats[0] = std::move(z); |
| 4800 | Floats[1].makeZero(/* Neg = */ false); |
| 4801 | return (opStatus)Status; |
| 4802 | } |
| 4803 | Status = opOK; |
| 4804 | auto AComparedToC = a.compareAbsoluteValue(RHS: c); |
| 4805 | z = cc; |
| 4806 | Status |= z.add(RHS: aa, RM); |
| 4807 | if (AComparedToC == APFloat::cmpGreaterThan) { |
| 4808 | // z = cc + aa + c + a; |
| 4809 | Status |= z.add(RHS: c, RM); |
| 4810 | Status |= z.add(RHS: a, RM); |
| 4811 | } else { |
| 4812 | // z = cc + aa + a + c; |
| 4813 | Status |= z.add(RHS: a, RM); |
| 4814 | Status |= z.add(RHS: c, RM); |
| 4815 | } |
| 4816 | if (!z.isFinite()) { |
| 4817 | Floats[0] = std::move(z); |
| 4818 | Floats[1].makeZero(/* Neg = */ false); |
| 4819 | return (opStatus)Status; |
| 4820 | } |
| 4821 | Floats[0] = z; |
| 4822 | APFloat zz = aa; |
| 4823 | Status |= zz.add(RHS: cc, RM); |
| 4824 | if (AComparedToC == APFloat::cmpGreaterThan) { |
| 4825 | // Floats[1] = a - z + c + zz; |
| 4826 | Floats[1] = a; |
| 4827 | Status |= Floats[1].subtract(RHS: z, RM); |
| 4828 | Status |= Floats[1].add(RHS: c, RM); |
| 4829 | Status |= Floats[1].add(RHS: zz, RM); |
| 4830 | } else { |
| 4831 | // Floats[1] = c - z + a + zz; |
| 4832 | Floats[1] = c; |
| 4833 | Status |= Floats[1].subtract(RHS: z, RM); |
| 4834 | Status |= Floats[1].add(RHS: a, RM); |
| 4835 | Status |= Floats[1].add(RHS: zz, RM); |
| 4836 | } |
| 4837 | } else { |
| 4838 | // q = a - z; |
| 4839 | APFloat q = a; |
| 4840 | Status |= q.subtract(RHS: z, RM); |
| 4841 | |
| 4842 | // zz = q + c + (a - (q + z)) + aa + cc; |
| 4843 | // Compute a - (q + z) as -((q + z) - a) to avoid temporary copies. |
| 4844 | auto zz = q; |
| 4845 | Status |= zz.add(RHS: c, RM); |
| 4846 | Status |= q.add(RHS: z, RM); |
| 4847 | Status |= q.subtract(RHS: a, RM); |
| 4848 | q.changeSign(); |
| 4849 | Status |= zz.add(RHS: q, RM); |
| 4850 | Status |= zz.add(RHS: aa, RM); |
| 4851 | Status |= zz.add(RHS: cc, RM); |
| 4852 | if (zz.isZero() && !zz.isNegative()) { |
| 4853 | Floats[0] = std::move(z); |
| 4854 | Floats[1].makeZero(/* Neg = */ false); |
| 4855 | return opOK; |
| 4856 | } |
| 4857 | Floats[0] = z; |
| 4858 | Status |= Floats[0].add(RHS: zz, RM); |
| 4859 | if (!Floats[0].isFinite()) { |
| 4860 | Floats[1].makeZero(/* Neg = */ false); |
| 4861 | return (opStatus)Status; |
| 4862 | } |
| 4863 | Floats[1] = std::move(z); |
| 4864 | Status |= Floats[1].subtract(RHS: Floats[0], RM); |
| 4865 | Status |= Floats[1].add(RHS: zz, RM); |
| 4866 | } |
| 4867 | return (opStatus)Status; |
| 4868 | } |
| 4869 | |
| 4870 | APFloat::opStatus DoubleAPFloat::addWithSpecial(const DoubleAPFloat &LHS, |
| 4871 | const DoubleAPFloat &RHS, |
| 4872 | DoubleAPFloat &Out, |
| 4873 | roundingMode RM) { |
| 4874 | if (LHS.getCategory() == fcNaN) { |
| 4875 | Out = LHS; |
| 4876 | return opOK; |
| 4877 | } |
| 4878 | if (RHS.getCategory() == fcNaN) { |
| 4879 | Out = RHS; |
| 4880 | return opOK; |
| 4881 | } |
| 4882 | if (LHS.getCategory() == fcZero) { |
| 4883 | Out = RHS; |
| 4884 | return opOK; |
| 4885 | } |
| 4886 | if (RHS.getCategory() == fcZero) { |
| 4887 | Out = LHS; |
| 4888 | return opOK; |
| 4889 | } |
| 4890 | if (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcInfinity && |
| 4891 | LHS.isNegative() != RHS.isNegative()) { |
| 4892 | Out.makeNaN(SNaN: false, Neg: Out.isNegative(), fill: nullptr); |
| 4893 | return opInvalidOp; |
| 4894 | } |
| 4895 | if (LHS.getCategory() == fcInfinity) { |
| 4896 | Out = LHS; |
| 4897 | return opOK; |
| 4898 | } |
| 4899 | if (RHS.getCategory() == fcInfinity) { |
| 4900 | Out = RHS; |
| 4901 | return opOK; |
| 4902 | } |
| 4903 | assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal); |
| 4904 | |
| 4905 | APFloat A(LHS.Floats[0]), AA(LHS.Floats[1]), C(RHS.Floats[0]), |
| 4906 | CC(RHS.Floats[1]); |
| 4907 | assert(&A.getSemantics() == &APFloatBase::semIEEEdouble); |
| 4908 | assert(&AA.getSemantics() == &APFloatBase::semIEEEdouble); |
| 4909 | assert(&C.getSemantics() == &APFloatBase::semIEEEdouble); |
| 4910 | assert(&CC.getSemantics() == &APFloatBase::semIEEEdouble); |
| 4911 | assert(&Out.Floats[0].getSemantics() == &APFloatBase::semIEEEdouble); |
| 4912 | assert(&Out.Floats[1].getSemantics() == &APFloatBase::semIEEEdouble); |
| 4913 | return Out.addImpl(a: A, aa: AA, c: C, cc: CC, RM); |
| 4914 | } |
| 4915 | |
| 4916 | APFloat::opStatus DoubleAPFloat::add(const DoubleAPFloat &RHS, |
| 4917 | roundingMode RM) { |
| 4918 | return addWithSpecial(LHS: *this, RHS, Out&: *this, RM); |
| 4919 | } |
| 4920 | |
| 4921 | APFloat::opStatus DoubleAPFloat::subtract(const DoubleAPFloat &RHS, |
| 4922 | roundingMode RM) { |
| 4923 | changeSign(); |
| 4924 | auto Ret = add(RHS, RM); |
| 4925 | changeSign(); |
| 4926 | return Ret; |
| 4927 | } |
| 4928 | |
| 4929 | APFloat::opStatus DoubleAPFloat::multiply(const DoubleAPFloat &RHS, |
| 4930 | APFloat::roundingMode RM) { |
| 4931 | const auto &LHS = *this; |
| 4932 | auto &Out = *this; |
| 4933 | /* Interesting observation: For special categories, finding the lowest |
| 4934 | common ancestor of the following layered graph gives the correct |
| 4935 | return category: |
| 4936 | |
| 4937 | NaN |
| 4938 | / \ |
| 4939 | Zero Inf |
| 4940 | \ / |
| 4941 | Normal |
| 4942 | |
| 4943 | e.g. NaN * NaN = NaN |
| 4944 | Zero * Inf = NaN |
| 4945 | Normal * Zero = Zero |
| 4946 | Normal * Inf = Inf |
| 4947 | */ |
| 4948 | if (LHS.getCategory() == fcNaN) { |
| 4949 | Out = LHS; |
| 4950 | return opOK; |
| 4951 | } |
| 4952 | if (RHS.getCategory() == fcNaN) { |
| 4953 | Out = RHS; |
| 4954 | return opOK; |
| 4955 | } |
| 4956 | if ((LHS.getCategory() == fcZero && RHS.getCategory() == fcInfinity) || |
| 4957 | (LHS.getCategory() == fcInfinity && RHS.getCategory() == fcZero)) { |
| 4958 | Out.makeNaN(SNaN: false, Neg: false, fill: nullptr); |
| 4959 | return opOK; |
| 4960 | } |
| 4961 | if (LHS.getCategory() == fcZero || LHS.getCategory() == fcInfinity) { |
| 4962 | Out = LHS; |
| 4963 | return opOK; |
| 4964 | } |
| 4965 | if (RHS.getCategory() == fcZero || RHS.getCategory() == fcInfinity) { |
| 4966 | Out = RHS; |
| 4967 | return opOK; |
| 4968 | } |
| 4969 | assert(LHS.getCategory() == fcNormal && RHS.getCategory() == fcNormal && |
| 4970 | "Special cases not handled exhaustively" ); |
| 4971 | |
| 4972 | int Status = opOK; |
| 4973 | APFloat A = Floats[0], B = Floats[1], C = RHS.Floats[0], D = RHS.Floats[1]; |
| 4974 | // t = a * c |
| 4975 | APFloat T = A; |
| 4976 | Status |= T.multiply(RHS: C, RM); |
| 4977 | if (!T.isFiniteNonZero()) { |
| 4978 | Floats[0] = T; |
| 4979 | Floats[1].makeZero(/* Neg = */ false); |
| 4980 | return (opStatus)Status; |
| 4981 | } |
| 4982 | |
| 4983 | // tau = fmsub(a, c, t), that is -fmadd(-a, c, t). |
| 4984 | APFloat Tau = A; |
| 4985 | T.changeSign(); |
| 4986 | Status |= Tau.fusedMultiplyAdd(Multiplicand: C, Addend: T, RM); |
| 4987 | T.changeSign(); |
| 4988 | { |
| 4989 | // v = a * d |
| 4990 | APFloat V = A; |
| 4991 | Status |= V.multiply(RHS: D, RM); |
| 4992 | // w = b * c |
| 4993 | APFloat W = B; |
| 4994 | Status |= W.multiply(RHS: C, RM); |
| 4995 | Status |= V.add(RHS: W, RM); |
| 4996 | // tau += v + w |
| 4997 | Status |= Tau.add(RHS: V, RM); |
| 4998 | } |
| 4999 | // u = t + tau |
| 5000 | APFloat U = T; |
| 5001 | Status |= U.add(RHS: Tau, RM); |
| 5002 | |
| 5003 | Floats[0] = U; |
| 5004 | if (!U.isFinite()) { |
| 5005 | Floats[1].makeZero(/* Neg = */ false); |
| 5006 | } else { |
| 5007 | // Floats[1] = (t - u) + tau |
| 5008 | Status |= T.subtract(RHS: U, RM); |
| 5009 | Status |= T.add(RHS: Tau, RM); |
| 5010 | Floats[1] = T; |
| 5011 | } |
| 5012 | return (opStatus)Status; |
| 5013 | } |
| 5014 | |
| 5015 | APFloat::opStatus DoubleAPFloat::divide(const DoubleAPFloat &RHS, |
| 5016 | APFloat::roundingMode RM) { |
| 5017 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5018 | "Unexpected Semantics" ); |
| 5019 | APFloat Tmp(APFloatBase::semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 5020 | auto Ret = Tmp.divide( |
| 5021 | RHS: APFloat(APFloatBase::semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt()), RM); |
| 5022 | *this = DoubleAPFloat(APFloatBase::semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 5023 | return Ret; |
| 5024 | } |
| 5025 | |
| 5026 | APFloat::opStatus DoubleAPFloat::remainder(const DoubleAPFloat &RHS) { |
| 5027 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5028 | "Unexpected Semantics" ); |
| 5029 | APFloat Tmp(APFloatBase::semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 5030 | auto Ret = Tmp.remainder( |
| 5031 | RHS: APFloat(APFloatBase::semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); |
| 5032 | *this = DoubleAPFloat(APFloatBase::semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 5033 | return Ret; |
| 5034 | } |
| 5035 | |
| 5036 | APFloat::opStatus DoubleAPFloat::mod(const DoubleAPFloat &RHS) { |
| 5037 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5038 | "Unexpected Semantics" ); |
| 5039 | APFloat Tmp(APFloatBase::semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 5040 | auto Ret = Tmp.mod( |
| 5041 | RHS: APFloat(APFloatBase::semPPCDoubleDoubleLegacy, RHS.bitcastToAPInt())); |
| 5042 | *this = DoubleAPFloat(APFloatBase::semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 5043 | return Ret; |
| 5044 | } |
| 5045 | |
| 5046 | APFloat::opStatus |
| 5047 | DoubleAPFloat::fusedMultiplyAdd(const DoubleAPFloat &Multiplicand, |
| 5048 | const DoubleAPFloat &Addend, |
| 5049 | APFloat::roundingMode RM) { |
| 5050 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5051 | "Unexpected Semantics" ); |
| 5052 | APFloat Tmp(APFloatBase::semPPCDoubleDoubleLegacy, bitcastToAPInt()); |
| 5053 | auto Ret = Tmp.fusedMultiplyAdd( |
| 5054 | Multiplicand: APFloat(APFloatBase::semPPCDoubleDoubleLegacy, |
| 5055 | Multiplicand.bitcastToAPInt()), |
| 5056 | Addend: APFloat(APFloatBase::semPPCDoubleDoubleLegacy, Addend.bitcastToAPInt()), |
| 5057 | RM); |
| 5058 | *this = DoubleAPFloat(APFloatBase::semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 5059 | return Ret; |
| 5060 | } |
| 5061 | |
| 5062 | APFloat::opStatus DoubleAPFloat::roundToIntegral(APFloat::roundingMode RM) { |
| 5063 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5064 | "Unexpected Semantics" ); |
| 5065 | const APFloat &Hi = getFirst(); |
| 5066 | const APFloat &Lo = getSecond(); |
| 5067 | |
| 5068 | APFloat RoundedHi = Hi; |
| 5069 | const opStatus HiStatus = RoundedHi.roundToIntegral(RM); |
| 5070 | |
| 5071 | // We can reduce the problem to just the high part if the input: |
| 5072 | // 1. Represents a non-finite value. |
| 5073 | // 2. Has a component which is zero. |
| 5074 | if (!Hi.isFiniteNonZero() || Lo.isZero()) { |
| 5075 | Floats[0] = std::move(RoundedHi); |
| 5076 | Floats[1].makeZero(/*Neg=*/false); |
| 5077 | return HiStatus; |
| 5078 | } |
| 5079 | |
| 5080 | // Adjust `Rounded` in the direction of `TieBreaker` if `ToRound` was at a |
| 5081 | // halfway point. |
| 5082 | auto RoundToNearestHelper = [](APFloat ToRound, APFloat Rounded, |
| 5083 | APFloat TieBreaker) { |
| 5084 | // RoundingError tells us which direction we rounded: |
| 5085 | // - RoundingError > 0: we rounded up. |
| 5086 | // - RoundingError < 0: we rounded down. |
| 5087 | // Sterbenz' lemma ensures that RoundingError is exact. |
| 5088 | const APFloat RoundingError = Rounded - ToRound; |
| 5089 | if (TieBreaker.isNonZero() && |
| 5090 | TieBreaker.isNegative() != RoundingError.isNegative() && |
| 5091 | abs(X: RoundingError).isExactlyValue(V: 0.5)) |
| 5092 | Rounded.add( |
| 5093 | RHS: APFloat::getOne(Sem: Rounded.getSemantics(), Negative: TieBreaker.isNegative()), |
| 5094 | RM: rmNearestTiesToEven); |
| 5095 | return Rounded; |
| 5096 | }; |
| 5097 | |
| 5098 | // Case 1: Hi is not an integer. |
| 5099 | // Special cases are for rounding modes that are sensitive to ties. |
| 5100 | if (RoundedHi != Hi) { |
| 5101 | // We need to consider the case where Hi was between two integers and the |
| 5102 | // rounding mode broke the tie when, in fact, Lo may have had a different |
| 5103 | // sign than Hi. |
| 5104 | if (RM == rmNearestTiesToAway || RM == rmNearestTiesToEven) |
| 5105 | RoundedHi = RoundToNearestHelper(Hi, RoundedHi, Lo); |
| 5106 | |
| 5107 | Floats[0] = std::move(RoundedHi); |
| 5108 | Floats[1].makeZero(/*Neg=*/false); |
| 5109 | return HiStatus; |
| 5110 | } |
| 5111 | |
| 5112 | // Case 2: Hi is an integer. |
| 5113 | // Special cases are for rounding modes which are rounding towards or away from zero. |
| 5114 | RoundingMode LoRoundingMode; |
| 5115 | if (RM == rmTowardZero) |
| 5116 | // When our input is positive, we want the Lo component rounded toward |
| 5117 | // negative infinity to get the smallest result magnitude. Likewise, |
| 5118 | // negative inputs want the Lo component rounded toward positive infinity. |
| 5119 | LoRoundingMode = isNegative() ? rmTowardPositive : rmTowardNegative; |
| 5120 | else |
| 5121 | LoRoundingMode = RM; |
| 5122 | |
| 5123 | APFloat RoundedLo = Lo; |
| 5124 | const opStatus LoStatus = RoundedLo.roundToIntegral(RM: LoRoundingMode); |
| 5125 | if (LoRoundingMode == rmNearestTiesToAway) |
| 5126 | // We need to consider the case where Lo was between two integers and the |
| 5127 | // rounding mode broke the tie when, in fact, Hi may have had a different |
| 5128 | // sign than Lo. |
| 5129 | RoundedLo = RoundToNearestHelper(Lo, RoundedLo, Hi); |
| 5130 | |
| 5131 | // We must ensure that the final result has no overlap between the two APFloat values. |
| 5132 | std::tie(args&: RoundedHi, args&: RoundedLo) = fastTwoSum(X: RoundedHi, Y: RoundedLo); |
| 5133 | |
| 5134 | Floats[0] = std::move(RoundedHi); |
| 5135 | Floats[1] = std::move(RoundedLo); |
| 5136 | return LoStatus; |
| 5137 | } |
| 5138 | |
| 5139 | void DoubleAPFloat::changeSign() { |
| 5140 | Floats[0].changeSign(); |
| 5141 | Floats[1].changeSign(); |
| 5142 | } |
| 5143 | |
| 5144 | APFloat::cmpResult |
| 5145 | DoubleAPFloat::compareAbsoluteValue(const DoubleAPFloat &RHS) const { |
| 5146 | // Compare absolute values of the high parts. |
| 5147 | const cmpResult HiPartCmp = Floats[0].compareAbsoluteValue(RHS: RHS.Floats[0]); |
| 5148 | if (HiPartCmp != cmpEqual) |
| 5149 | return HiPartCmp; |
| 5150 | |
| 5151 | // Zero, regardless of sign, is equal. |
| 5152 | if (Floats[1].isZero() && RHS.Floats[1].isZero()) |
| 5153 | return cmpEqual; |
| 5154 | |
| 5155 | // At this point, |this->Hi| == |RHS.Hi|. |
| 5156 | // The magnitude is |Hi+Lo| which is Hi+|Lo| if signs of Hi and Lo are the |
| 5157 | // same, and Hi-|Lo| if signs are different. |
| 5158 | const bool ThisIsSubtractive = |
| 5159 | Floats[0].isNegative() != Floats[1].isNegative(); |
| 5160 | const bool RHSIsSubtractive = |
| 5161 | RHS.Floats[0].isNegative() != RHS.Floats[1].isNegative(); |
| 5162 | |
| 5163 | // Case 1: The low part of 'this' is zero. |
| 5164 | if (Floats[1].isZero()) |
| 5165 | // We are comparing |Hi| vs. |Hi| ± |RHS.Lo|. |
| 5166 | // If RHS is subtractive, its magnitude is smaller. |
| 5167 | // If RHS is additive, its magnitude is larger. |
| 5168 | return RHSIsSubtractive ? cmpGreaterThan : cmpLessThan; |
| 5169 | |
| 5170 | // Case 2: The low part of 'RHS' is zero (and we know 'this' is not). |
| 5171 | if (RHS.Floats[1].isZero()) |
| 5172 | // We are comparing |Hi| ± |This.Lo| vs. |Hi|. |
| 5173 | // If 'this' is subtractive, its magnitude is smaller. |
| 5174 | // If 'this' is additive, its magnitude is larger. |
| 5175 | return ThisIsSubtractive ? cmpLessThan : cmpGreaterThan; |
| 5176 | |
| 5177 | // If their natures differ, the additive one is larger. |
| 5178 | if (ThisIsSubtractive != RHSIsSubtractive) |
| 5179 | return ThisIsSubtractive ? cmpLessThan : cmpGreaterThan; |
| 5180 | |
| 5181 | // Case 3: Both are additive (Hi+|Lo|) or both are subtractive (Hi-|Lo|). |
| 5182 | // The comparison now depends on the magnitude of the low parts. |
| 5183 | const cmpResult LoPartCmp = Floats[1].compareAbsoluteValue(RHS: RHS.Floats[1]); |
| 5184 | |
| 5185 | if (ThisIsSubtractive) { |
| 5186 | // Both are subtractive (Hi-|Lo|), so the comparison of |Lo| is inverted. |
| 5187 | if (LoPartCmp == cmpLessThan) |
| 5188 | return cmpGreaterThan; |
| 5189 | if (LoPartCmp == cmpGreaterThan) |
| 5190 | return cmpLessThan; |
| 5191 | } |
| 5192 | |
| 5193 | // If additive, the comparison of |Lo| is direct. |
| 5194 | // If equal, they are equal. |
| 5195 | return LoPartCmp; |
| 5196 | } |
| 5197 | |
| 5198 | APFloat::fltCategory DoubleAPFloat::getCategory() const { |
| 5199 | return Floats[0].getCategory(); |
| 5200 | } |
| 5201 | |
| 5202 | bool DoubleAPFloat::isNegative() const { return Floats[0].isNegative(); } |
| 5203 | |
| 5204 | void DoubleAPFloat::makeInf(bool Neg) { |
| 5205 | Floats[0].makeInf(Neg); |
| 5206 | Floats[1].makeZero(/* Neg = */ false); |
| 5207 | } |
| 5208 | |
| 5209 | void DoubleAPFloat::makeZero(bool Neg) { |
| 5210 | Floats[0].makeZero(Neg); |
| 5211 | Floats[1].makeZero(/* Neg = */ false); |
| 5212 | } |
| 5213 | |
| 5214 | void DoubleAPFloat::makeLargest(bool Neg) { |
| 5215 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5216 | "Unexpected Semantics" ); |
| 5217 | Floats[0] = |
| 5218 | APFloat(APFloatBase::semIEEEdouble, APInt(64, 0x7fefffffffffffffull)); |
| 5219 | Floats[1] = |
| 5220 | APFloat(APFloatBase::semIEEEdouble, APInt(64, 0x7c8ffffffffffffeull)); |
| 5221 | if (Neg) |
| 5222 | changeSign(); |
| 5223 | } |
| 5224 | |
| 5225 | void DoubleAPFloat::makeSmallest(bool Neg) { |
| 5226 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5227 | "Unexpected Semantics" ); |
| 5228 | Floats[0].makeSmallest(Neg); |
| 5229 | Floats[1].makeZero(/* Neg = */ false); |
| 5230 | } |
| 5231 | |
| 5232 | void DoubleAPFloat::makeSmallestNormalized(bool Neg) { |
| 5233 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5234 | "Unexpected Semantics" ); |
| 5235 | Floats[0] = |
| 5236 | APFloat(APFloatBase::semIEEEdouble, APInt(64, 0x0360000000000000ull)); |
| 5237 | if (Neg) |
| 5238 | Floats[0].changeSign(); |
| 5239 | Floats[1].makeZero(/* Neg = */ false); |
| 5240 | } |
| 5241 | |
| 5242 | void DoubleAPFloat::makeNaN(bool SNaN, bool Neg, const APInt *fill) { |
| 5243 | Floats[0].makeNaN(SNaN, Neg, fill); |
| 5244 | Floats[1].makeZero(/* Neg = */ false); |
| 5245 | } |
| 5246 | |
| 5247 | APFloat::cmpResult DoubleAPFloat::compare(const DoubleAPFloat &RHS) const { |
| 5248 | auto Result = Floats[0].compare(RHS: RHS.Floats[0]); |
| 5249 | // |Float[0]| > |Float[1]| |
| 5250 | if (Result == APFloat::cmpEqual) |
| 5251 | return Floats[1].compare(RHS: RHS.Floats[1]); |
| 5252 | return Result; |
| 5253 | } |
| 5254 | |
| 5255 | bool DoubleAPFloat::bitwiseIsEqual(const DoubleAPFloat &RHS) const { |
| 5256 | return Floats[0].bitwiseIsEqual(RHS: RHS.Floats[0]) && |
| 5257 | Floats[1].bitwiseIsEqual(RHS: RHS.Floats[1]); |
| 5258 | } |
| 5259 | |
| 5260 | hash_code hash_value(const DoubleAPFloat &Arg) { |
| 5261 | if (Arg.Floats) |
| 5262 | return hash_combine(args: hash_value(Arg: Arg.Floats[0]), args: hash_value(Arg: Arg.Floats[1])); |
| 5263 | return hash_combine(args: Arg.Semantics); |
| 5264 | } |
| 5265 | |
| 5266 | APInt DoubleAPFloat::bitcastToAPInt() const { |
| 5267 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5268 | "Unexpected Semantics" ); |
| 5269 | uint64_t Data[] = { |
| 5270 | Floats[0].bitcastToAPInt().getRawData()[0], |
| 5271 | Floats[1].bitcastToAPInt().getRawData()[0], |
| 5272 | }; |
| 5273 | return APInt(128, Data); |
| 5274 | } |
| 5275 | |
| 5276 | Expected<APFloat::opStatus> DoubleAPFloat::convertFromString(StringRef S, |
| 5277 | roundingMode RM) { |
| 5278 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5279 | "Unexpected Semantics" ); |
| 5280 | APFloat Tmp(APFloatBase::semPPCDoubleDoubleLegacy); |
| 5281 | auto Ret = Tmp.convertFromString(S, RM); |
| 5282 | *this = DoubleAPFloat(APFloatBase::semPPCDoubleDouble, Tmp.bitcastToAPInt()); |
| 5283 | return Ret; |
| 5284 | } |
| 5285 | |
| 5286 | // The double-double lattice of values corresponds to numbers which obey: |
| 5287 | // - abs(lo) <= 1/2 * ulp(hi) |
| 5288 | // - roundTiesToEven(hi + lo) == hi |
| 5289 | // |
| 5290 | // nextUp must choose the smallest output > input that follows these rules. |
| 5291 | // nexDown must choose the largest output < input that follows these rules. |
| 5292 | APFloat::opStatus DoubleAPFloat::next(bool nextDown) { |
| 5293 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5294 | "Unexpected Semantics" ); |
| 5295 | // nextDown(x) = -nextUp(-x) |
| 5296 | if (nextDown) { |
| 5297 | changeSign(); |
| 5298 | APFloat::opStatus Result = next(/*nextDown=*/false); |
| 5299 | changeSign(); |
| 5300 | return Result; |
| 5301 | } |
| 5302 | switch (getCategory()) { |
| 5303 | case fcInfinity: |
| 5304 | // nextUp(+inf) = +inf |
| 5305 | // nextUp(-inf) = -getLargest() |
| 5306 | if (isNegative()) |
| 5307 | makeLargest(Neg: true); |
| 5308 | return opOK; |
| 5309 | |
| 5310 | case fcNaN: |
| 5311 | // IEEE-754R 2008 6.2 Par 2: nextUp(sNaN) = qNaN. Set Invalid flag. |
| 5312 | // IEEE-754R 2008 6.2: nextUp(qNaN) = qNaN. Must be identity so we do not |
| 5313 | // change the payload. |
| 5314 | if (getFirst().isSignaling()) { |
| 5315 | // For consistency, propagate the sign of the sNaN to the qNaN. |
| 5316 | makeNaN(SNaN: false, Neg: isNegative(), fill: nullptr); |
| 5317 | return opInvalidOp; |
| 5318 | } |
| 5319 | return opOK; |
| 5320 | |
| 5321 | case fcZero: |
| 5322 | // nextUp(pm 0) = +getSmallest() |
| 5323 | makeSmallest(Neg: false); |
| 5324 | return opOK; |
| 5325 | |
| 5326 | case fcNormal: |
| 5327 | break; |
| 5328 | } |
| 5329 | |
| 5330 | const APFloat &HiOld = getFirst(); |
| 5331 | const APFloat &LoOld = getSecond(); |
| 5332 | |
| 5333 | APFloat NextLo = LoOld; |
| 5334 | NextLo.next(/*nextDown=*/false); |
| 5335 | |
| 5336 | // We want to admit values where: |
| 5337 | // 1. abs(Lo) <= ulp(Hi)/2 |
| 5338 | // 2. Hi == RTNE(Hi + lo) |
| 5339 | auto InLattice = [](const APFloat &Hi, const APFloat &Lo) { |
| 5340 | return Hi + Lo == Hi; |
| 5341 | }; |
| 5342 | |
| 5343 | // Check if (HiOld, nextUp(LoOld) is in the lattice. |
| 5344 | if (InLattice(HiOld, NextLo)) { |
| 5345 | // Yes, the result is (HiOld, nextUp(LoOld)). |
| 5346 | Floats[1] = std::move(NextLo); |
| 5347 | |
| 5348 | // TODO: Because we currently rely on semPPCDoubleDoubleLegacy, our maximum |
| 5349 | // value is defined to have exactly 106 bits of precision. This limitation |
| 5350 | // results in semPPCDoubleDouble being unable to reach its maximum canonical |
| 5351 | // value. |
| 5352 | DoubleAPFloat Largest{*Semantics, uninitialized}; |
| 5353 | Largest.makeLargest(/*Neg=*/false); |
| 5354 | if (compare(RHS: Largest) == cmpGreaterThan) |
| 5355 | makeInf(/*Neg=*/false); |
| 5356 | |
| 5357 | return opOK; |
| 5358 | } |
| 5359 | |
| 5360 | // Now we need to handle the cases where (HiOld, nextUp(LoOld)) is not the |
| 5361 | // correct result. We know the new hi component will be nextUp(HiOld) but our |
| 5362 | // lattice rules make it a little ambiguous what the correct NextLo must be. |
| 5363 | APFloat NextHi = HiOld; |
| 5364 | NextHi.next(/*nextDown=*/false); |
| 5365 | |
| 5366 | // nextUp(getLargest()) == INFINITY |
| 5367 | if (NextHi.isInfinity()) { |
| 5368 | makeInf(/*Neg=*/false); |
| 5369 | return opOK; |
| 5370 | } |
| 5371 | |
| 5372 | // IEEE 754-2019 5.3.1: |
| 5373 | // "If x is the negative number of least magnitude in x's format, nextUp(x) is |
| 5374 | // -0." |
| 5375 | if (NextHi.isZero()) { |
| 5376 | makeZero(/*Neg=*/true); |
| 5377 | return opOK; |
| 5378 | } |
| 5379 | |
| 5380 | // abs(NextLo) must be <= ulp(NextHi)/2. We want NextLo to be as close to |
| 5381 | // negative infinity as possible. |
| 5382 | NextLo = neg(X: scalbn(X: harrisonUlp(X: NextHi), Exp: -1, RM: rmTowardZero)); |
| 5383 | if (!InLattice(NextHi, NextLo)) |
| 5384 | // RTNE may mean that Lo must be < ulp(NextHi) / 2 so we bump NextLo. |
| 5385 | NextLo.next(/*nextDown=*/false); |
| 5386 | |
| 5387 | Floats[0] = std::move(NextHi); |
| 5388 | Floats[1] = std::move(NextLo); |
| 5389 | |
| 5390 | return opOK; |
| 5391 | } |
| 5392 | |
| 5393 | APFloat::opStatus DoubleAPFloat::convertToSignExtendedInteger( |
| 5394 | MutableArrayRef<integerPart> Input, unsigned int Width, bool IsSigned, |
| 5395 | roundingMode RM, bool *IsExact) const { |
| 5396 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5397 | "Unexpected Semantics" ); |
| 5398 | |
| 5399 | // If Hi is not finite, or Lo is zero, the value is entirely represented |
| 5400 | // by Hi. Delegate to the simpler single-APFloat conversion. |
| 5401 | if (!getFirst().isFiniteNonZero() || getSecond().isZero()) |
| 5402 | return getFirst().convertToInteger(Input, Width, IsSigned, RM, IsExact); |
| 5403 | |
| 5404 | // First, round the full double-double value to an integral value. This |
| 5405 | // simplifies the rest of the function, as we no longer need to consider |
| 5406 | // fractional parts. |
| 5407 | *IsExact = false; |
| 5408 | DoubleAPFloat Integral = *this; |
| 5409 | const opStatus RoundStatus = Integral.roundToIntegral(RM); |
| 5410 | if (RoundStatus == opInvalidOp) |
| 5411 | return opInvalidOp; |
| 5412 | const APFloat &IntegralHi = Integral.getFirst(); |
| 5413 | const APFloat &IntegralLo = Integral.getSecond(); |
| 5414 | |
| 5415 | // If rounding results in either component being zero, the sum is trivial. |
| 5416 | // Delegate to the simpler single-APFloat conversion. |
| 5417 | bool HiIsExact; |
| 5418 | if (IntegralHi.isZero() || IntegralLo.isZero()) { |
| 5419 | const opStatus HiStatus = |
| 5420 | IntegralHi.convertToInteger(Input, Width, IsSigned, RM, IsExact: &HiIsExact); |
| 5421 | // The conversion from an integer-valued float to an APInt may fail if the |
| 5422 | // result would be out of range. Regardless, taking this path is only |
| 5423 | // possible if rounding occurred during the initial `roundToIntegral`. |
| 5424 | return HiStatus == opOK ? opInexact : HiStatus; |
| 5425 | } |
| 5426 | |
| 5427 | // A negative number cannot be represented by an unsigned integer. |
| 5428 | // Since a double-double is canonical, if Hi is negative, the sum is negative. |
| 5429 | if (!IsSigned && IntegralHi.isNegative()) |
| 5430 | return opInvalidOp; |
| 5431 | |
| 5432 | // Handle the special boundary case where |Hi| is exactly the power of two |
| 5433 | // that marks the edge of the integer's range (e.g., 2^63 for int64_t). In |
| 5434 | // this situation, Hi itself won't fit, but the sum Hi + Lo might. |
| 5435 | // `PositiveOverflowWidth` is the bit number for this boundary (N-1 for |
| 5436 | // signed, N for unsigned). |
| 5437 | bool LoIsExact; |
| 5438 | const int HiExactLog2 = IntegralHi.getExactLog2Abs(); |
| 5439 | const unsigned PositiveOverflowWidth = IsSigned ? Width - 1 : Width; |
| 5440 | if (HiExactLog2 >= 0 && |
| 5441 | static_cast<unsigned>(HiExactLog2) == PositiveOverflowWidth) { |
| 5442 | // If Hi and Lo have the same sign, |Hi + Lo| > |Hi|, so the sum is |
| 5443 | // guaranteed to overflow. E.g., for uint128_t, (2^128, 1) overflows. |
| 5444 | if (IntegralHi.isNegative() == IntegralLo.isNegative()) |
| 5445 | return opInvalidOp; |
| 5446 | |
| 5447 | // If the signs differ, the sum will fit. We can compute the result using |
| 5448 | // properties of two's complement arithmetic without a wide intermediate |
| 5449 | // integer. E.g., for uint128_t, (2^128, -1) should be 2^128 - 1. |
| 5450 | const opStatus LoStatus = IntegralLo.convertToInteger( |
| 5451 | Input, Width, /*IsSigned=*/true, RM, IsExact: &LoIsExact); |
| 5452 | if (LoStatus == opInvalidOp) |
| 5453 | return opInvalidOp; |
| 5454 | |
| 5455 | // Adjust the bit pattern of Lo to account for Hi's value: |
| 5456 | // - For unsigned (Hi=2^Width): `2^Width + Lo` in `Width`-bit |
| 5457 | // arithmetic is equivalent to just `Lo`. The conversion of `Lo` above |
| 5458 | // already produced the correct final bit pattern. |
| 5459 | // - For signed (Hi=2^(Width-1)): The sum `2^(Width-1) + Lo` (where Lo<0) |
| 5460 | // can be computed by taking the two's complement pattern for `Lo` and |
| 5461 | // clearing the sign bit. |
| 5462 | if (IsSigned && !IntegralHi.isNegative()) |
| 5463 | APInt::tcClearBit(Input.data(), bit: PositiveOverflowWidth); |
| 5464 | *IsExact = RoundStatus == opOK; |
| 5465 | return RoundStatus; |
| 5466 | } |
| 5467 | |
| 5468 | // Convert Hi into an integer. This may not fit but that is OK: we know that |
| 5469 | // Hi + Lo would not fit either in this situation. |
| 5470 | const opStatus HiStatus = IntegralHi.convertToInteger( |
| 5471 | Input, Width, IsSigned, RM: rmTowardZero, IsExact: &HiIsExact); |
| 5472 | if (HiStatus == opInvalidOp) |
| 5473 | return HiStatus; |
| 5474 | |
| 5475 | // Convert Lo into a temporary integer of the same width. |
| 5476 | APSInt LoResult{Width, /*isUnsigned=*/!IsSigned}; |
| 5477 | const opStatus LoStatus = |
| 5478 | IntegralLo.convertToInteger(Result&: LoResult, RM: rmTowardZero, IsExact: &LoIsExact); |
| 5479 | if (LoStatus == opInvalidOp) |
| 5480 | return LoStatus; |
| 5481 | |
| 5482 | // Add Lo to Hi. This addition is guaranteed not to overflow because of the |
| 5483 | // double-double canonicalization rule (`|Lo| <= ulp(Hi)/2`). The only case |
| 5484 | // where the sum could cross the integer type's boundary is when Hi is a |
| 5485 | // power of two, which is handled by the special case block above. |
| 5486 | APInt::tcAdd(Input.data(), LoResult.getRawData(), /*carry=*/0, Input.size()); |
| 5487 | |
| 5488 | *IsExact = RoundStatus == opOK; |
| 5489 | return RoundStatus; |
| 5490 | } |
| 5491 | |
| 5492 | APFloat::opStatus |
| 5493 | DoubleAPFloat::convertToInteger(MutableArrayRef<integerPart> Input, |
| 5494 | unsigned int Width, bool IsSigned, |
| 5495 | roundingMode RM, bool *IsExact) const { |
| 5496 | opStatus FS = |
| 5497 | convertToSignExtendedInteger(Input, Width, IsSigned, RM, IsExact); |
| 5498 | |
| 5499 | if (FS == opInvalidOp) { |
| 5500 | const unsigned DstPartsCount = partCountForBits(bits: Width); |
| 5501 | assert(DstPartsCount <= Input.size() && "Integer too big" ); |
| 5502 | |
| 5503 | unsigned Bits; |
| 5504 | if (getCategory() == fcNaN) |
| 5505 | Bits = 0; |
| 5506 | else if (isNegative()) |
| 5507 | Bits = IsSigned; |
| 5508 | else |
| 5509 | Bits = Width - IsSigned; |
| 5510 | |
| 5511 | tcSetLeastSignificantBits(dst: Input.data(), parts: DstPartsCount, bits: Bits); |
| 5512 | if (isNegative() && IsSigned) |
| 5513 | APInt::tcShiftLeft(Input.data(), Words: DstPartsCount, Count: Width - 1); |
| 5514 | } |
| 5515 | |
| 5516 | return FS; |
| 5517 | } |
| 5518 | |
| 5519 | APFloat::opStatus DoubleAPFloat::handleOverflow(roundingMode RM) { |
| 5520 | switch (RM) { |
| 5521 | case APFloat::rmTowardZero: |
| 5522 | makeLargest(/*Neg=*/isNegative()); |
| 5523 | break; |
| 5524 | case APFloat::rmTowardNegative: |
| 5525 | if (isNegative()) |
| 5526 | makeInf(/*Neg=*/true); |
| 5527 | else |
| 5528 | makeLargest(/*Neg=*/false); |
| 5529 | break; |
| 5530 | case APFloat::rmTowardPositive: |
| 5531 | if (isNegative()) |
| 5532 | makeLargest(/*Neg=*/true); |
| 5533 | else |
| 5534 | makeInf(/*Neg=*/false); |
| 5535 | break; |
| 5536 | case APFloat::rmNearestTiesToAway: |
| 5537 | case APFloat::rmNearestTiesToEven: |
| 5538 | makeInf(/*Neg=*/isNegative()); |
| 5539 | break; |
| 5540 | default: |
| 5541 | llvm_unreachable("Invalid rounding mode found" ); |
| 5542 | } |
| 5543 | opStatus S = opInexact; |
| 5544 | if (!getFirst().isFinite()) |
| 5545 | S = static_cast<opStatus>(S | opOverflow); |
| 5546 | return S; |
| 5547 | } |
| 5548 | |
| 5549 | APFloat::opStatus DoubleAPFloat::convertFromUnsignedParts( |
| 5550 | const integerPart *Src, unsigned int SrcCount, roundingMode RM) { |
| 5551 | // Find the most significant bit of the source integer. APInt::tcMSB returns |
| 5552 | // UINT_MAX for a zero value. |
| 5553 | const unsigned SrcMSB = APInt::tcMSB(parts: Src, n: SrcCount); |
| 5554 | if (SrcMSB == UINT_MAX) { |
| 5555 | // The source integer is 0. |
| 5556 | makeZero(/*Neg=*/false); |
| 5557 | return opOK; |
| 5558 | } |
| 5559 | |
| 5560 | // Create a minimally-sized APInt to represent the source value. |
| 5561 | const unsigned SrcBitWidth = SrcMSB + 1; |
| 5562 | APSInt SrcInt{APInt{/*numBits=*/SrcBitWidth, ArrayRef(Src, SrcCount)}, |
| 5563 | /*isUnsigned=*/true}; |
| 5564 | |
| 5565 | // Stage 1: Initial Approximation. |
| 5566 | // Convert the source integer SrcInt to the Hi part of the DoubleAPFloat. |
| 5567 | // We use round-to-nearest because it minimizes the initial error, which is |
| 5568 | // crucial for the subsequent steps. |
| 5569 | APFloat Hi{getFirst().getSemantics()}; |
| 5570 | Hi.convertFromAPInt(Input: SrcInt, /*IsSigned=*/false, RM: rmNearestTiesToEven); |
| 5571 | |
| 5572 | // If the first approximation already overflows, the number is too large. |
| 5573 | // NOTE: The underlying semantics are *more* conservative when choosing to |
| 5574 | // overflow because their notion of ULP is much larger. As such, it is always |
| 5575 | // safe to overflow at the DoubleAPFloat level if the APFloat overflows. |
| 5576 | if (!Hi.isFinite()) |
| 5577 | return handleOverflow(RM); |
| 5578 | |
| 5579 | // Stage 2: Exact Error Calculation. |
| 5580 | // Calculate the exact error of the first approximation: Error = SrcInt - Hi. |
| 5581 | // This is done by converting Hi back to an integer and subtracting it from |
| 5582 | // the original source. |
| 5583 | bool HiAsIntIsExact; |
| 5584 | // Create an integer representation of Hi. Its width is determined by the |
| 5585 | // exponent of Hi, ensuring it's just large enough. This width can exceed |
| 5586 | // SrcBitWidth if the conversion to Hi rounded up to a power of two. |
| 5587 | // accurately when converted back to an integer. |
| 5588 | APSInt HiAsInt{static_cast<uint32_t>(ilogb(Arg: Hi) + 1), /*isUnsigned=*/true}; |
| 5589 | Hi.convertToInteger(Result&: HiAsInt, RM: rmNearestTiesToEven, IsExact: &HiAsIntIsExact); |
| 5590 | const APInt Error = SrcInt.zext(width: HiAsInt.getBitWidth()) - HiAsInt; |
| 5591 | |
| 5592 | // Stage 3: Error Approximation and Rounding. |
| 5593 | // Convert the integer error into the Lo part of the DoubleAPFloat. This step |
| 5594 | // captures the remainder of the original number. The rounding mode for this |
| 5595 | // conversion (LoRM) may need to be adjusted from the user-requested RM to |
| 5596 | // ensure the final sum (Hi + Lo) rounds correctly. |
| 5597 | roundingMode LoRM = RM; |
| 5598 | // Adjustments are only necessary when the initial approximation Hi was an |
| 5599 | // overestimate, making the Error negative. |
| 5600 | if (Error.isNegative()) { |
| 5601 | if (RM == rmNearestTiesToAway) { |
| 5602 | // For rmNearestTiesToAway, a tie should round away from zero. Since |
| 5603 | // SrcInt is positive, this means rounding toward +infinity. |
| 5604 | // A standard conversion of a negative Error would round ties toward |
| 5605 | // -infinity, causing the final sum Hi + Lo to be smaller. To |
| 5606 | // counteract this, we detect the tie case and override the rounding |
| 5607 | // mode for Lo to rmTowardPositive. |
| 5608 | const unsigned ErrorActiveBits = Error.getSignificantBits() - 1; |
| 5609 | const unsigned LoPrecision = getSecond().getSemantics().precision; |
| 5610 | if (ErrorActiveBits > LoPrecision) { |
| 5611 | const unsigned RoundingBoundary = ErrorActiveBits - LoPrecision; |
| 5612 | // A tie occurs when the bits to be truncated are of the form 100...0. |
| 5613 | // This is detected by checking if the number of trailing zeros is |
| 5614 | // exactly one less than the number of bits being truncated. |
| 5615 | if (Error.countTrailingZeros() == RoundingBoundary - 1) |
| 5616 | LoRM = rmTowardPositive; |
| 5617 | } |
| 5618 | } else if (RM == rmTowardZero) { |
| 5619 | // For rmTowardZero, the final positive result must be truncated (rounded |
| 5620 | // down). When Hi is an overestimate, Error is negative. A standard |
| 5621 | // rmTowardZero conversion of Error would make it *less* negative, |
| 5622 | // effectively rounding the final sum Hi + Lo *up*. To ensure the sum |
| 5623 | // rounds down correctly, we force Lo to round toward -infinity. |
| 5624 | LoRM = rmTowardNegative; |
| 5625 | } |
| 5626 | } |
| 5627 | |
| 5628 | APFloat Lo{getSecond().getSemantics()}; |
| 5629 | opStatus Status = Lo.convertFromAPInt(Input: Error, /*IsSigned=*/true, RM: LoRM); |
| 5630 | |
| 5631 | // Renormalize the pair (Hi, Lo) into a canonical DoubleAPFloat form where the |
| 5632 | // components do not overlap. fastTwoSum performs this operation. |
| 5633 | std::tie(args&: Hi, args&: Lo) = fastTwoSum(X: Hi, Y: Lo); |
| 5634 | Floats[0] = std::move(Hi); |
| 5635 | Floats[1] = std::move(Lo); |
| 5636 | |
| 5637 | // A final check for overflow is needed because fastTwoSum can cause a |
| 5638 | // carry-out from Lo that pushes Hi to infinity. |
| 5639 | if (!getFirst().isFinite()) |
| 5640 | return handleOverflow(RM); |
| 5641 | |
| 5642 | // The largest DoubleAPFloat must be canonical. Values which are larger are |
| 5643 | // not canonical and are equivalent to overflow. |
| 5644 | if (getFirst().isFiniteNonZero() && Floats[0].isLargest()) { |
| 5645 | DoubleAPFloat Largest{*Semantics}; |
| 5646 | Largest.makeLargest(/*Neg=*/false); |
| 5647 | if (compare(RHS: Largest) == APFloat::cmpGreaterThan) |
| 5648 | return handleOverflow(RM); |
| 5649 | } |
| 5650 | |
| 5651 | // The final status of the operation is determined by the conversion of the |
| 5652 | // error term. If Lo could represent Error exactly, the entire conversion |
| 5653 | // is exact. Otherwise, it's inexact. |
| 5654 | return Status; |
| 5655 | } |
| 5656 | |
| 5657 | APFloat::opStatus DoubleAPFloat::convertFromAPInt(const APInt &Input, |
| 5658 | bool IsSigned, |
| 5659 | roundingMode RM) { |
| 5660 | const bool NegateInput = IsSigned && Input.isNegative(); |
| 5661 | APInt API = Input; |
| 5662 | if (NegateInput) |
| 5663 | API.negate(); |
| 5664 | |
| 5665 | const APFloat::opStatus Status = |
| 5666 | convertFromUnsignedParts(Src: API.getRawData(), SrcCount: API.getNumWords(), RM); |
| 5667 | if (NegateInput) |
| 5668 | changeSign(); |
| 5669 | return Status; |
| 5670 | } |
| 5671 | |
| 5672 | unsigned int DoubleAPFloat::convertToHexString(char *DST, |
| 5673 | unsigned int HexDigits, |
| 5674 | bool UpperCase, |
| 5675 | roundingMode RM) const { |
| 5676 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5677 | "Unexpected Semantics" ); |
| 5678 | return APFloat(APFloatBase::semPPCDoubleDoubleLegacy, bitcastToAPInt()) |
| 5679 | .convertToHexString(DST, HexDigits, UpperCase, RM); |
| 5680 | } |
| 5681 | |
| 5682 | bool DoubleAPFloat::isDenormal() const { |
| 5683 | return getCategory() == fcNormal && |
| 5684 | (Floats[0].isDenormal() || Floats[1].isDenormal() || |
| 5685 | // (double)(Hi + Lo) == Hi defines a normal number. |
| 5686 | Floats[0] != Floats[0] + Floats[1]); |
| 5687 | } |
| 5688 | |
| 5689 | bool DoubleAPFloat::isSmallest() const { |
| 5690 | if (getCategory() != fcNormal) |
| 5691 | return false; |
| 5692 | DoubleAPFloat Tmp(*this); |
| 5693 | Tmp.makeSmallest(Neg: this->isNegative()); |
| 5694 | return Tmp.compare(RHS: *this) == cmpEqual; |
| 5695 | } |
| 5696 | |
| 5697 | bool DoubleAPFloat::isSmallestNormalized() const { |
| 5698 | if (getCategory() != fcNormal) |
| 5699 | return false; |
| 5700 | |
| 5701 | DoubleAPFloat Tmp(*this); |
| 5702 | Tmp.makeSmallestNormalized(Neg: this->isNegative()); |
| 5703 | return Tmp.compare(RHS: *this) == cmpEqual; |
| 5704 | } |
| 5705 | |
| 5706 | bool DoubleAPFloat::isLargest() const { |
| 5707 | if (getCategory() != fcNormal) |
| 5708 | return false; |
| 5709 | DoubleAPFloat Tmp(*this); |
| 5710 | Tmp.makeLargest(Neg: this->isNegative()); |
| 5711 | return Tmp.compare(RHS: *this) == cmpEqual; |
| 5712 | } |
| 5713 | |
| 5714 | bool DoubleAPFloat::isInteger() const { |
| 5715 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5716 | "Unexpected Semantics" ); |
| 5717 | return Floats[0].isInteger() && Floats[1].isInteger(); |
| 5718 | } |
| 5719 | |
| 5720 | void DoubleAPFloat::toString(SmallVectorImpl<char> &Str, |
| 5721 | unsigned FormatPrecision, |
| 5722 | unsigned FormatMaxPadding, |
| 5723 | bool TruncateZero) const { |
| 5724 | assert(Semantics == &APFloatBase::semPPCDoubleDouble && |
| 5725 | "Unexpected Semantics" ); |
| 5726 | APFloat(APFloatBase::semPPCDoubleDoubleLegacy, bitcastToAPInt()) |
| 5727 | .toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero); |
| 5728 | } |
| 5729 | |
| 5730 | int DoubleAPFloat::getExactLog2Abs() const { |
| 5731 | // In order for Hi + Lo to be a power of two, the following must be true: |
| 5732 | // 1. Hi must be a power of two. |
| 5733 | // 2. Lo must be zero. |
| 5734 | if (getSecond().isNonZero()) |
| 5735 | return INT_MIN; |
| 5736 | return getFirst().getExactLog2Abs(); |
| 5737 | } |
| 5738 | |
| 5739 | int ilogb(const DoubleAPFloat &Arg) { |
| 5740 | const APFloat &Hi = Arg.getFirst(); |
| 5741 | const APFloat &Lo = Arg.getSecond(); |
| 5742 | int IlogbResult = ilogb(Arg: Hi); |
| 5743 | // Zero and non-finite values can delegate to ilogb(Hi). |
| 5744 | if (Arg.getCategory() != fcNormal) |
| 5745 | return IlogbResult; |
| 5746 | // If Lo can't change the binade, we can delegate to ilogb(Hi). |
| 5747 | if (Lo.isZero() || Hi.isNegative() == Lo.isNegative()) |
| 5748 | return IlogbResult; |
| 5749 | if (Hi.getExactLog2Abs() == INT_MIN) |
| 5750 | return IlogbResult; |
| 5751 | // Numbers of the form 2^a - 2^b or -2^a + 2^b are almost powers of two but |
| 5752 | // get nudged out of the binade by the low component. |
| 5753 | return IlogbResult - 1; |
| 5754 | } |
| 5755 | |
| 5756 | DoubleAPFloat scalbn(const DoubleAPFloat &Arg, int Exp, |
| 5757 | APFloat::roundingMode RM) { |
| 5758 | assert(Arg.Semantics == &APFloatBase::PPCDoubleDouble() && |
| 5759 | "Unexpected Semantics" ); |
| 5760 | return DoubleAPFloat(APFloatBase::PPCDoubleDouble(), |
| 5761 | scalbn(X: Arg.Floats[0], Exp, RM), |
| 5762 | scalbn(X: Arg.Floats[1], Exp, RM)); |
| 5763 | } |
| 5764 | |
| 5765 | DoubleAPFloat frexp(const DoubleAPFloat &Arg, int &Exp, |
| 5766 | APFloat::roundingMode RM) { |
| 5767 | assert(Arg.Semantics == &APFloatBase::PPCDoubleDouble() && |
| 5768 | "Unexpected Semantics" ); |
| 5769 | |
| 5770 | // Get the unbiased exponent e of the number, where |Arg| = m * 2^e for m in |
| 5771 | // [1.0, 2.0). |
| 5772 | Exp = ilogb(Arg); |
| 5773 | |
| 5774 | // For NaNs, quiet any signaling NaN and return the result, as per standard |
| 5775 | // practice. |
| 5776 | if (Exp == APFloat::IEK_NaN) { |
| 5777 | DoubleAPFloat Quiet{Arg}; |
| 5778 | Quiet.getFirst() = Quiet.getFirst().makeQuiet(); |
| 5779 | return Quiet; |
| 5780 | } |
| 5781 | |
| 5782 | // For infinity, return it unchanged. The exponent remains IEK_Inf. |
| 5783 | if (Exp == APFloat::IEK_Inf) |
| 5784 | return Arg; |
| 5785 | |
| 5786 | // For zero, the fraction is zero and the standard requires the exponent be 0. |
| 5787 | if (Exp == APFloat::IEK_Zero) { |
| 5788 | Exp = 0; |
| 5789 | return Arg; |
| 5790 | } |
| 5791 | |
| 5792 | const APFloat &Hi = Arg.getFirst(); |
| 5793 | const APFloat &Lo = Arg.getSecond(); |
| 5794 | |
| 5795 | // frexp requires the fraction's absolute value to be in [0.5, 1.0). |
| 5796 | // ilogb provides an exponent for an absolute value in [1.0, 2.0). |
| 5797 | // Increment the exponent to ensure the fraction is in the correct range. |
| 5798 | ++Exp; |
| 5799 | |
| 5800 | const bool SignsDisagree = Hi.isNegative() != Lo.isNegative(); |
| 5801 | APFloat Second = Lo; |
| 5802 | if (Arg.getCategory() == APFloat::fcNormal && Lo.isFiniteNonZero()) { |
| 5803 | roundingMode LoRoundingMode; |
| 5804 | // The interpretation of rmTowardZero depends on the sign of the combined |
| 5805 | // Arg rather than the sign of the component. |
| 5806 | if (RM == rmTowardZero) |
| 5807 | LoRoundingMode = Arg.isNegative() ? rmTowardPositive : rmTowardNegative; |
| 5808 | // For rmNearestTiesToAway, we face a similar problem. If signs disagree, |
| 5809 | // Lo is a correction *toward* zero relative to Hi. Rounding Lo |
| 5810 | // "away from zero" based on its own sign would move the value in the |
| 5811 | // wrong direction. As a safe proxy, we use rmNearestTiesToEven, which is |
| 5812 | // direction-agnostic. We only need to bother with this if Lo is scaled |
| 5813 | // down. |
| 5814 | else if (RM == rmNearestTiesToAway && SignsDisagree && Exp > 0) |
| 5815 | LoRoundingMode = rmNearestTiesToEven; |
| 5816 | else |
| 5817 | LoRoundingMode = RM; |
| 5818 | Second = scalbn(X: Lo, Exp: -Exp, RM: LoRoundingMode); |
| 5819 | // The rmNearestTiesToEven proxy is correct most of the time, but it |
| 5820 | // differs from rmNearestTiesToAway when the scaled value of Lo is an |
| 5821 | // exact midpoint. |
| 5822 | // NOTE: This is morally equivalent to roundTiesTowardZero. |
| 5823 | if (RM == rmNearestTiesToAway && LoRoundingMode == rmNearestTiesToEven) { |
| 5824 | // Re-scale the result back to check if rounding occurred. |
| 5825 | const APFloat RecomposedLo = scalbn(X: Second, Exp, RM: rmNearestTiesToEven); |
| 5826 | if (RecomposedLo != Lo) { |
| 5827 | // RoundingError tells us which direction we rounded: |
| 5828 | // - RoundingError > 0: we rounded up. |
| 5829 | // - RoundingError < 0: we down up. |
| 5830 | const APFloat RoundingError = RecomposedLo - Lo; |
| 5831 | // Determine if scalbn(Lo, -Exp) landed exactly on a midpoint. |
| 5832 | // We do this by checking if the absolute rounding error is exactly |
| 5833 | // half a ULP of the result. |
| 5834 | const APFloat UlpOfSecond = harrisonUlp(X: Second); |
| 5835 | const APFloat ScaledUlpOfSecond = |
| 5836 | scalbn(X: UlpOfSecond, Exp: Exp - 1, RM: rmNearestTiesToEven); |
| 5837 | const bool IsMidpoint = abs(X: RoundingError) == ScaledUlpOfSecond; |
| 5838 | const bool RoundedLoAway = |
| 5839 | Second.isNegative() == RoundingError.isNegative(); |
| 5840 | // The sign of Hi and Lo disagree and we rounded Lo away: we must |
| 5841 | // decrease the magnitude of Second to increase the magnitude |
| 5842 | // First+Second. |
| 5843 | if (IsMidpoint && RoundedLoAway) |
| 5844 | Second.next(/*nextDown=*/!Second.isNegative()); |
| 5845 | } |
| 5846 | } |
| 5847 | // Handle a tricky edge case where Arg is slightly less than a power of two |
| 5848 | // (e.g., Arg = 2^k - epsilon). In this situation: |
| 5849 | // 1. Hi is 2^k, and Lo is a small negative value -epsilon. |
| 5850 | // 2. ilogb(Arg) correctly returns k-1. |
| 5851 | // 3. Our initial Exp becomes (k-1) + 1 = k. |
| 5852 | // 4. Scaling Hi (2^k) by 2^-k would yield a magnitude of 1.0 and |
| 5853 | // scaling Lo by 2^-k would yield zero. This would make the result 1.0 |
| 5854 | // which is an invalid fraction, as the required interval is [0.5, 1.0). |
| 5855 | // We detect this specific case by checking if Hi is a power of two and if |
| 5856 | // the scaled Lo underflowed to zero. The fix: Increment Exp to k+1. This |
| 5857 | // adjusts the scale factor, causing Hi to be scaled to 0.5, which is a |
| 5858 | // valid fraction. |
| 5859 | if (Second.isZero() && SignsDisagree && Hi.getExactLog2Abs() != INT_MIN) |
| 5860 | ++Exp; |
| 5861 | } |
| 5862 | |
| 5863 | APFloat First = scalbn(X: Hi, Exp: -Exp, RM); |
| 5864 | return DoubleAPFloat(APFloatBase::PPCDoubleDouble(), std::move(First), |
| 5865 | std::move(Second)); |
| 5866 | } |
| 5867 | |
| 5868 | } // namespace detail |
| 5869 | |
| 5870 | APFloat::Storage::Storage(IEEEFloat F, const fltSemantics &Semantics) { |
| 5871 | if (usesLayout<IEEEFloat>(Semantics)) { |
| 5872 | new (&IEEE) IEEEFloat(std::move(F)); |
| 5873 | return; |
| 5874 | } |
| 5875 | if (usesLayout<DoubleAPFloat>(Semantics)) { |
| 5876 | const fltSemantics& S = F.getSemantics(); |
| 5877 | new (&Double) DoubleAPFloat(Semantics, APFloat(std::move(F), S), |
| 5878 | APFloat(APFloatBase::IEEEdouble())); |
| 5879 | return; |
| 5880 | } |
| 5881 | llvm_unreachable("Unexpected semantics" ); |
| 5882 | } |
| 5883 | |
| 5884 | Expected<APFloat::opStatus> APFloat::convertFromString(StringRef Str, |
| 5885 | roundingMode RM) { |
| 5886 | APFLOAT_DISPATCH_ON_SEMANTICS(convertFromString(Str, RM)); |
| 5887 | } |
| 5888 | |
| 5889 | hash_code hash_value(const APFloat &Arg) { |
| 5890 | if (APFloat::usesLayout<detail::IEEEFloat>(Semantics: Arg.getSemantics())) |
| 5891 | return hash_value(Arg: Arg.U.IEEE); |
| 5892 | if (APFloat::usesLayout<detail::DoubleAPFloat>(Semantics: Arg.getSemantics())) |
| 5893 | return hash_value(Arg: Arg.U.Double); |
| 5894 | llvm_unreachable("Unexpected semantics" ); |
| 5895 | } |
| 5896 | |
| 5897 | APFloat::APFloat(const fltSemantics &Semantics, StringRef S) |
| 5898 | : APFloat(Semantics) { |
| 5899 | auto StatusOrErr = convertFromString(Str: S, RM: rmNearestTiesToEven); |
| 5900 | assert(StatusOrErr && "Invalid floating point representation" ); |
| 5901 | consumeError(Err: StatusOrErr.takeError()); |
| 5902 | } |
| 5903 | |
| 5904 | FPClassTest APFloat::classify() const { |
| 5905 | if (isZero()) |
| 5906 | return isNegative() ? fcNegZero : fcPosZero; |
| 5907 | if (isNormal()) |
| 5908 | return isNegative() ? fcNegNormal : fcPosNormal; |
| 5909 | if (isDenormal()) |
| 5910 | return isNegative() ? fcNegSubnormal : fcPosSubnormal; |
| 5911 | if (isInfinity()) |
| 5912 | return isNegative() ? fcNegInf : fcPosInf; |
| 5913 | assert(isNaN() && "Other class of FP constant" ); |
| 5914 | return isSignaling() ? fcSNan : fcQNan; |
| 5915 | } |
| 5916 | |
| 5917 | bool APFloat::getExactInverse(APFloat *Inv) const { |
| 5918 | // Only finite, non-zero numbers can have a useful, representable inverse. |
| 5919 | // This check filters out +/- zero, +/- infinity, and NaN. |
| 5920 | if (!isFiniteNonZero()) |
| 5921 | return false; |
| 5922 | |
| 5923 | // Historically, this function rejects subnormal inputs. One reason why this |
| 5924 | // might be important is that subnormals may behave differently under FTZ/DAZ |
| 5925 | // runtime behavior. |
| 5926 | if (isDenormal()) |
| 5927 | return false; |
| 5928 | |
| 5929 | // A number has an exact, representable inverse if and only if it is a power |
| 5930 | // of two. |
| 5931 | // |
| 5932 | // Mathematical Rationale: |
| 5933 | // 1. A binary floating-point number x is a dyadic rational, meaning it can |
| 5934 | // be written as x = M / 2^k for integers M (the significand) and k. |
| 5935 | // 2. The inverse is 1/x = 2^k / M. |
| 5936 | // 3. For 1/x to also be a dyadic rational (and thus exactly representable |
| 5937 | // in binary), its denominator M must also be a power of two. |
| 5938 | // Let's say M = 2^m. |
| 5939 | // 4. Substituting this back into the formula for x, we get |
| 5940 | // x = (2^m) / (2^k) = 2^(m-k). |
| 5941 | // |
| 5942 | // This proves that x must be a power of two. |
| 5943 | |
| 5944 | // getExactLog2Abs() returns the integer exponent if the number is a power of |
| 5945 | // two or INT_MIN if it is not. |
| 5946 | const int Exp = getExactLog2Abs(); |
| 5947 | if (Exp == INT_MIN) |
| 5948 | return false; |
| 5949 | |
| 5950 | // The inverse of +/- 2^Exp is +/- 2^(-Exp). We can compute this by |
| 5951 | // scaling 1.0 by the negated exponent. |
| 5952 | APFloat Reciprocal = |
| 5953 | scalbn(X: APFloat::getOne(Sem: getSemantics(), /*Negative=*/isNegative()), Exp: -Exp, |
| 5954 | RM: rmTowardZero); |
| 5955 | |
| 5956 | // scalbn might round if the resulting exponent -Exp is outside the |
| 5957 | // representable range, causing overflow (to infinity) or underflow. We |
| 5958 | // must verify that the result is still the exact power of two we expect. |
| 5959 | if (Reciprocal.getExactLog2Abs() != -Exp) |
| 5960 | return false; |
| 5961 | |
| 5962 | // Avoid multiplication with a subnormal, it is not safe on all platforms and |
| 5963 | // may be slower than a normal division. |
| 5964 | if (Reciprocal.isDenormal()) |
| 5965 | return false; |
| 5966 | |
| 5967 | assert(Reciprocal.isFiniteNonZero()); |
| 5968 | |
| 5969 | if (Inv) |
| 5970 | *Inv = std::move(Reciprocal); |
| 5971 | |
| 5972 | return true; |
| 5973 | } |
| 5974 | |
| 5975 | APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics, |
| 5976 | roundingMode RM, bool *losesInfo) { |
| 5977 | if (&getSemantics() == &ToSemantics) { |
| 5978 | *losesInfo = false; |
| 5979 | return opOK; |
| 5980 | } |
| 5981 | if (usesLayout<IEEEFloat>(Semantics: getSemantics()) && |
| 5982 | usesLayout<IEEEFloat>(Semantics: ToSemantics)) |
| 5983 | return U.IEEE.convert(toSemantics: ToSemantics, rounding_mode: RM, losesInfo); |
| 5984 | if (usesLayout<IEEEFloat>(Semantics: getSemantics()) && |
| 5985 | usesLayout<DoubleAPFloat>(Semantics: ToSemantics)) { |
| 5986 | assert(&ToSemantics == &APFloatBase::semPPCDoubleDouble); |
| 5987 | auto Ret = |
| 5988 | U.IEEE.convert(toSemantics: APFloatBase::semPPCDoubleDoubleLegacy, rounding_mode: RM, losesInfo); |
| 5989 | *this = APFloat(ToSemantics, U.IEEE.bitcastToAPInt()); |
| 5990 | return Ret; |
| 5991 | } |
| 5992 | if (usesLayout<DoubleAPFloat>(Semantics: getSemantics()) && |
| 5993 | usesLayout<IEEEFloat>(Semantics: ToSemantics)) { |
| 5994 | auto Ret = getIEEE().convert(toSemantics: ToSemantics, rounding_mode: RM, losesInfo); |
| 5995 | *this = APFloat(std::move(getIEEE()), ToSemantics); |
| 5996 | return Ret; |
| 5997 | } |
| 5998 | llvm_unreachable("Unexpected semantics" ); |
| 5999 | } |
| 6000 | |
| 6001 | APFloat APFloat::getAllOnesValue(const fltSemantics &Semantics) { |
| 6002 | return APFloat(Semantics, APInt::getAllOnes(numBits: Semantics.sizeInBits)); |
| 6003 | } |
| 6004 | |
| 6005 | void APFloat::print(raw_ostream &OS) const { |
| 6006 | SmallVector<char, 16> Buffer; |
| 6007 | toString(Str&: Buffer); |
| 6008 | OS << Buffer; |
| 6009 | } |
| 6010 | |
| 6011 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 6012 | LLVM_DUMP_METHOD void APFloat::dump() const { |
| 6013 | print(dbgs()); |
| 6014 | dbgs() << '\n'; |
| 6015 | } |
| 6016 | #endif |
| 6017 | |
| 6018 | void APFloat::Profile(FoldingSetNodeID &NID) const { |
| 6019 | NID.Add(x: bitcastToAPInt()); |
| 6020 | } |
| 6021 | |
| 6022 | APFloat::opStatus APFloat::convertToInteger(APSInt &result, |
| 6023 | roundingMode rounding_mode, |
| 6024 | bool *isExact) const { |
| 6025 | unsigned bitWidth = result.getBitWidth(); |
| 6026 | SmallVector<uint64_t, 4> parts(result.getNumWords()); |
| 6027 | opStatus status = convertToInteger(Input: parts, Width: bitWidth, IsSigned: result.isSigned(), |
| 6028 | RM: rounding_mode, IsExact: isExact); |
| 6029 | // Keeps the original signed-ness. |
| 6030 | result = APInt(bitWidth, parts); |
| 6031 | return status; |
| 6032 | } |
| 6033 | |
| 6034 | double APFloat::convertToDouble() const { |
| 6035 | if (&getSemantics() == |
| 6036 | (const llvm::fltSemantics *)&APFloatBase::semIEEEdouble) |
| 6037 | return getIEEE().convertToDouble(); |
| 6038 | assert(isRepresentableBy(getSemantics(), semIEEEdouble) && |
| 6039 | "Float semantics is not representable by IEEEdouble" ); |
| 6040 | APFloat Temp = *this; |
| 6041 | bool LosesInfo; |
| 6042 | opStatus St = |
| 6043 | Temp.convert(ToSemantics: APFloatBase::semIEEEdouble, RM: rmNearestTiesToEven, losesInfo: &LosesInfo); |
| 6044 | assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision" ); |
| 6045 | (void)St; |
| 6046 | return Temp.getIEEE().convertToDouble(); |
| 6047 | } |
| 6048 | |
| 6049 | #ifdef HAS_IEE754_FLOAT128 |
| 6050 | float128 APFloat::convertToQuad() const { |
| 6051 | if (&getSemantics() == (const llvm::fltSemantics *)&APFloatBase::semIEEEquad) |
| 6052 | return getIEEE().convertToQuad(); |
| 6053 | assert(isRepresentableBy(getSemantics(), semIEEEquad) && |
| 6054 | "Float semantics is not representable by IEEEquad" ); |
| 6055 | APFloat Temp = *this; |
| 6056 | bool LosesInfo; |
| 6057 | opStatus St = |
| 6058 | Temp.convert(ToSemantics: APFloatBase::semIEEEquad, RM: rmNearestTiesToEven, losesInfo: &LosesInfo); |
| 6059 | assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision" ); |
| 6060 | (void)St; |
| 6061 | return Temp.getIEEE().convertToQuad(); |
| 6062 | } |
| 6063 | #endif |
| 6064 | |
| 6065 | float APFloat::convertToFloat() const { |
| 6066 | if (&getSemantics() == |
| 6067 | (const llvm::fltSemantics *)&APFloatBase::semIEEEsingle) |
| 6068 | return getIEEE().convertToFloat(); |
| 6069 | assert(isRepresentableBy(getSemantics(), semIEEEsingle) && |
| 6070 | "Float semantics is not representable by IEEEsingle" ); |
| 6071 | APFloat Temp = *this; |
| 6072 | bool LosesInfo; |
| 6073 | opStatus St = |
| 6074 | Temp.convert(ToSemantics: APFloatBase::semIEEEsingle, RM: rmNearestTiesToEven, losesInfo: &LosesInfo); |
| 6075 | assert(!(St & opInexact) && !LosesInfo && "Unexpected imprecision" ); |
| 6076 | (void)St; |
| 6077 | return Temp.getIEEE().convertToFloat(); |
| 6078 | } |
| 6079 | |
| 6080 | bool APFloatBase::isValidArbitraryFPFormat(StringRef Format) { |
| 6081 | static constexpr StringLiteral ValidFormats[] = { |
| 6082 | "Float8E5M2" , "Float8E5M2FNUZ" , "Float8E4M3" , "Float8E4M3FN" , |
| 6083 | "Float8E4M3FNUZ" , "Float8E4M3B11FNUZ" , "Float8E3M4" , "Float8E8M0FNU" , |
| 6084 | "Float6E3M2FN" , "Float6E2M3FN" , "Float4E2M1FN" }; |
| 6085 | return llvm::is_contained(Range: ValidFormats, Element: Format); |
| 6086 | } |
| 6087 | |
| 6088 | APFloat::Storage::~Storage() { |
| 6089 | if (usesLayout<IEEEFloat>(Semantics: *semantics)) { |
| 6090 | IEEE.~IEEEFloat(); |
| 6091 | return; |
| 6092 | } |
| 6093 | if (usesLayout<DoubleAPFloat>(Semantics: *semantics)) { |
| 6094 | Double.~DoubleAPFloat(); |
| 6095 | return; |
| 6096 | } |
| 6097 | llvm_unreachable("Unexpected semantics" ); |
| 6098 | } |
| 6099 | |
| 6100 | APFloat::Storage::Storage(const APFloat::Storage &RHS) { |
| 6101 | if (usesLayout<IEEEFloat>(Semantics: *RHS.semantics)) { |
| 6102 | new (this) IEEEFloat(RHS.IEEE); |
| 6103 | return; |
| 6104 | } |
| 6105 | if (usesLayout<DoubleAPFloat>(Semantics: *RHS.semantics)) { |
| 6106 | new (this) DoubleAPFloat(RHS.Double); |
| 6107 | return; |
| 6108 | } |
| 6109 | llvm_unreachable("Unexpected semantics" ); |
| 6110 | } |
| 6111 | |
| 6112 | APFloat::Storage::Storage(APFloat::Storage &&RHS) { |
| 6113 | if (usesLayout<IEEEFloat>(Semantics: *RHS.semantics)) { |
| 6114 | new (this) IEEEFloat(std::move(RHS.IEEE)); |
| 6115 | return; |
| 6116 | } |
| 6117 | if (usesLayout<DoubleAPFloat>(Semantics: *RHS.semantics)) { |
| 6118 | new (this) DoubleAPFloat(std::move(RHS.Double)); |
| 6119 | return; |
| 6120 | } |
| 6121 | llvm_unreachable("Unexpected semantics" ); |
| 6122 | } |
| 6123 | |
| 6124 | APFloat::Storage &APFloat::Storage::operator=(const APFloat::Storage &RHS) { |
| 6125 | if (usesLayout<IEEEFloat>(Semantics: *semantics) && |
| 6126 | usesLayout<IEEEFloat>(Semantics: *RHS.semantics)) { |
| 6127 | IEEE = RHS.IEEE; |
| 6128 | } else if (usesLayout<DoubleAPFloat>(Semantics: *semantics) && |
| 6129 | usesLayout<DoubleAPFloat>(Semantics: *RHS.semantics)) { |
| 6130 | Double = RHS.Double; |
| 6131 | } else if (this != &RHS) { |
| 6132 | this->~Storage(); |
| 6133 | new (this) Storage(RHS); |
| 6134 | } |
| 6135 | return *this; |
| 6136 | } |
| 6137 | |
| 6138 | APFloat::Storage &APFloat::Storage::operator=(APFloat::Storage &&RHS) { |
| 6139 | if (usesLayout<IEEEFloat>(Semantics: *semantics) && |
| 6140 | usesLayout<IEEEFloat>(Semantics: *RHS.semantics)) { |
| 6141 | IEEE = std::move(RHS.IEEE); |
| 6142 | } else if (usesLayout<DoubleAPFloat>(Semantics: *semantics) && |
| 6143 | usesLayout<DoubleAPFloat>(Semantics: *RHS.semantics)) { |
| 6144 | Double = std::move(RHS.Double); |
| 6145 | } else if (this != &RHS) { |
| 6146 | this->~Storage(); |
| 6147 | new (this) Storage(std::move(RHS)); |
| 6148 | } |
| 6149 | return *this; |
| 6150 | } |
| 6151 | |
| 6152 | } // namespace llvm |
| 6153 | |
| 6154 | #undef APFLOAT_DISPATCH_ON_SEMANTICS |
| 6155 | |