| 1 | //===- llvm/Support/KnownFPClass.h - Stores known fplcass -------*- C++ -*-===// |
| 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 contains a class for representing known fpclasses used by |
| 10 | // computeKnownFPClass. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/KnownFPClass.h" |
| 15 | #include "llvm/ADT/APFloat.h" |
| 16 | #include "llvm/Support/ErrorHandling.h" |
| 17 | #include "llvm/Support/KnownBits.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | KnownFPClass::KnownFPClass(const APFloat &C) |
| 22 | : KnownFPClass(C.classify(), C.isNegative()) {} |
| 23 | |
| 24 | KnownFPClass KnownFPClass::applyInputDenormalMode(const KnownFPClass &KnownSrc, |
| 25 | DenormalMode Mode) { |
| 26 | KnownFPClass Known = KnownSrc; |
| 27 | switch (Mode.Input) { |
| 28 | case DenormalMode::IEEE: |
| 29 | return Known; |
| 30 | case DenormalMode::PreserveSign: |
| 31 | if (!KnownSrc.isKnownNever(Mask: fcPosSubnormal)) |
| 32 | Known |= fcPosZero; |
| 33 | if (!KnownSrc.isKnownNever(Mask: fcNegSubnormal)) |
| 34 | Known |= fcNegZero; |
| 35 | return Known; |
| 36 | case DenormalMode::PositiveZero: |
| 37 | if (!KnownSrc.isKnownNever(Mask: fcSubnormal)) |
| 38 | Known |= fcPosZero; |
| 39 | return Known; |
| 40 | default: |
| 41 | if (!KnownSrc.isKnownNever(Mask: fcSubnormal)) |
| 42 | Known |= fcPosZero; |
| 43 | if (!KnownSrc.isKnownNever(Mask: fcNegSubnormal)) |
| 44 | Known |= fcNegZero; |
| 45 | return Known; |
| 46 | } |
| 47 | |
| 48 | llvm_unreachable("covered switch over denormal mode" ); |
| 49 | } |
| 50 | |
| 51 | KnownFPClass KnownFPClass::applyOutputDenormalMode(const KnownFPClass &KnownSrc, |
| 52 | DenormalMode Mode) { |
| 53 | KnownFPClass Known = KnownSrc; |
| 54 | switch (Mode.Output) { |
| 55 | case DenormalMode::IEEE: |
| 56 | return Known; |
| 57 | case DenormalMode::PreserveSign: |
| 58 | if (!KnownSrc.isKnownNever(Mask: fcPosSubnormal)) |
| 59 | Known |= fcPosZero; |
| 60 | if (!KnownSrc.isKnownNever(Mask: fcNegSubnormal)) |
| 61 | Known |= fcNegZero; |
| 62 | return Known; |
| 63 | case DenormalMode::PositiveZero: |
| 64 | if (!KnownSrc.isKnownNever(Mask: fcSubnormal)) |
| 65 | Known |= fcPosZero; |
| 66 | return Known; |
| 67 | default: |
| 68 | if (!KnownSrc.isKnownNever(Mask: fcSubnormal)) |
| 69 | Known |= fcPosZero; |
| 70 | if (!KnownSrc.isKnownNever(Mask: fcNegSubnormal)) |
| 71 | Known |= fcNegZero; |
| 72 | return Known; |
| 73 | } |
| 74 | |
| 75 | llvm_unreachable("covered switch over denormal mode" ); |
| 76 | } |
| 77 | |
| 78 | bool KnownFPClass::isKnownNeverLogicalZero(DenormalMode Mode) const { |
| 79 | return applyInputDenormalMode(KnownSrc: *this, Mode).isKnownNever(Mask: fcZero); |
| 80 | } |
| 81 | |
| 82 | bool KnownFPClass::isKnownNeverLogicalNegZero(DenormalMode Mode) const { |
| 83 | return applyInputDenormalMode(KnownSrc: *this, Mode).isKnownNever(Mask: fcNegZero); |
| 84 | } |
| 85 | |
| 86 | bool KnownFPClass::isKnownNeverLogicalPosZero(DenormalMode Mode) const { |
| 87 | return applyInputDenormalMode(KnownSrc: *this, Mode).isKnownNever(Mask: fcPosZero); |
| 88 | } |
| 89 | |
| 90 | KnownFPClass KnownFPClass::minMaxLike(const KnownFPClass &KnownLHS_, |
| 91 | const KnownFPClass &KnownRHS_, |
| 92 | MinMaxKind Kind, DenormalMode Mode) { |
| 93 | KnownFPClass KnownLHS = applyInputDenormalMode(KnownSrc: KnownLHS_, Mode); |
| 94 | KnownFPClass KnownRHS = applyInputDenormalMode(KnownSrc: KnownRHS_, Mode); |
| 95 | |
| 96 | KnownFPClass Known; |
| 97 | |
| 98 | Known.propagateNonNaN(LHS: KnownLHS, RHS: KnownRHS); |
| 99 | |
| 100 | // The *num functions return the non-NaN operand if only one operand is NaN. |
| 101 | // FIXME: minnum/maxnum may return a NaN if either operand is a signaling NaN. |
| 102 | if ((KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN()) && |
| 103 | (Kind == MinMaxKind::minnum || Kind == MinMaxKind::maxnum || |
| 104 | Kind == MinMaxKind::minimumnum || Kind == MinMaxKind::maximumnum)) |
| 105 | Known.knownNot(RuleOut: fcNan); |
| 106 | |
| 107 | switch (Kind) { |
| 108 | case MinMaxKind::maxnum: { |
| 109 | if (KnownLHS.isKnownNeverNaN()) |
| 110 | Known.knownNot(RuleOut: orderedStrictlyLess(Mask: KnownLHS.getKnownFPClasses(), |
| 111 | /*OrderedZeroSign=*/false)); |
| 112 | if (KnownRHS.isKnownNeverNaN()) |
| 113 | Known.knownNot(RuleOut: orderedStrictlyLess(Mask: KnownRHS.getKnownFPClasses(), |
| 114 | /*OrderedZeroSign=*/false)); |
| 115 | break; |
| 116 | } |
| 117 | case MinMaxKind::minnum: { |
| 118 | if (KnownLHS.isKnownNeverNaN()) |
| 119 | Known.knownNot(RuleOut: orderedStrictlyGreater(Mask: KnownLHS.getKnownFPClasses(), |
| 120 | /*OrderedZeroSign=*/false)); |
| 121 | if (KnownRHS.isKnownNeverNaN()) |
| 122 | Known.knownNot(RuleOut: orderedStrictlyGreater(Mask: KnownRHS.getKnownFPClasses(), |
| 123 | /*OrderedZeroSign=*/false)); |
| 124 | break; |
| 125 | } |
| 126 | case MinMaxKind::maximumnum: { |
| 127 | if (KnownLHS.isKnownNeverNaN()) |
| 128 | Known.knownNot(RuleOut: orderedStrictlyLess(Mask: KnownLHS.getKnownFPClasses(), |
| 129 | /*OrderedZeroSign=*/true)); |
| 130 | if (KnownRHS.isKnownNeverNaN()) |
| 131 | Known.knownNot(RuleOut: orderedStrictlyLess(Mask: KnownRHS.getKnownFPClasses(), |
| 132 | /*OrderedZeroSign=*/true)); |
| 133 | break; |
| 134 | } |
| 135 | case MinMaxKind::minimumnum: { |
| 136 | if (KnownLHS.isKnownNeverNaN()) |
| 137 | Known.knownNot(RuleOut: orderedStrictlyGreater(Mask: KnownLHS.getKnownFPClasses(), |
| 138 | /*OrderedZeroSign=*/true)); |
| 139 | if (KnownRHS.isKnownNeverNaN()) |
| 140 | Known.knownNot(RuleOut: orderedStrictlyGreater(Mask: KnownRHS.getKnownFPClasses(), |
| 141 | /*OrderedZeroSign=*/true)); |
| 142 | break; |
| 143 | } |
| 144 | case MinMaxKind::maximum: { |
| 145 | Known.knownNot(RuleOut: orderedStrictlyLess(Mask: KnownLHS.getKnownFPClasses(), |
| 146 | /*OrderedZeroSign=*/true) | |
| 147 | orderedStrictlyLess(Mask: KnownRHS.getKnownFPClasses(), |
| 148 | /*OrderedZeroSign=*/true)); |
| 149 | break; |
| 150 | } |
| 151 | case MinMaxKind::minimum: { |
| 152 | Known.knownNot(RuleOut: orderedStrictlyGreater(Mask: KnownLHS.getKnownFPClasses(), |
| 153 | /*OrderedZeroSign=*/true) | |
| 154 | orderedStrictlyGreater(Mask: KnownRHS.getKnownFPClasses(), |
| 155 | /*OrderedZeroSign=*/true)); |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Remove impossible non-NaN classes. |
| 161 | FPClassTest PossibleClasses = |
| 162 | KnownLHS.getKnownFPClasses() | KnownRHS.getKnownFPClasses() | fcNan; |
| 163 | Known.knownNot(RuleOut: ~PossibleClasses); |
| 164 | |
| 165 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 166 | } |
| 167 | |
| 168 | KnownFPClass KnownFPClass::canonicalize(const KnownFPClass &KnownSrc, |
| 169 | const fltSemantics &Flt, |
| 170 | DenormalMode Mode) { |
| 171 | KnownFPClass Known; |
| 172 | |
| 173 | // TODO: implement for x86_fp80 |
| 174 | if (!APFloat::isIEEELikeFP(Flt)) |
| 175 | return Known; |
| 176 | |
| 177 | // Canonicalize may flush denormals to zero, so we have to consider the |
| 178 | // denormal mode to preserve known-not-0 knowledge. |
| 179 | Known.setKnownFPClasses(KnownSrc.getKnownFPClasses() | fcZero | fcQNan); |
| 180 | |
| 181 | // Stronger version of propagateNaN |
| 182 | // Canonicalize is guaranteed to quiet signaling nans. |
| 183 | if (KnownSrc.isKnownNeverNaN()) |
| 184 | Known.knownNot(RuleOut: fcNan); |
| 185 | else |
| 186 | Known.knownNot(RuleOut: fcSNan); |
| 187 | |
| 188 | // If the parent function flushes denormals, the canonical output cannot be a |
| 189 | // denormal. |
| 190 | if (Mode == DenormalMode::getIEEE()) { |
| 191 | if (KnownSrc.isKnownNever(Mask: fcPosZero)) |
| 192 | Known.knownNot(RuleOut: fcPosZero); |
| 193 | if (KnownSrc.isKnownNever(Mask: fcNegZero)) |
| 194 | Known.knownNot(RuleOut: fcNegZero); |
| 195 | return Known; |
| 196 | } |
| 197 | |
| 198 | if (Mode.inputsAreZero() || Mode.outputsAreZero()) |
| 199 | Known.knownNot(RuleOut: fcSubnormal); |
| 200 | |
| 201 | if (Mode == DenormalMode::getPreserveSign()) { |
| 202 | if (KnownSrc.isKnownNever(Mask: fcPosZero | fcPosSubnormal)) |
| 203 | Known.knownNot(RuleOut: fcPosZero); |
| 204 | if (KnownSrc.isKnownNever(Mask: fcNegZero | fcNegSubnormal)) |
| 205 | Known.knownNot(RuleOut: fcNegZero); |
| 206 | return Known; |
| 207 | } |
| 208 | |
| 209 | if (Mode.Input == DenormalMode::PositiveZero || |
| 210 | (Mode.Output == DenormalMode::PositiveZero && |
| 211 | Mode.Input == DenormalMode::IEEE)) { |
| 212 | // -0.0 is not a subnormal and should not be flushed. |
| 213 | if (KnownSrc.isKnownNever(Mask: fcNegZero)) |
| 214 | Known.knownNot(RuleOut: fcNegZero); |
| 215 | |
| 216 | if (KnownSrc.isKnownNever(Mask: fcPosZero | fcSubnormal)) |
| 217 | Known.knownNot(RuleOut: fcPosZero); |
| 218 | } |
| 219 | |
| 220 | return Known; |
| 221 | } |
| 222 | |
| 223 | KnownFPClass KnownFPClass::bitcast(const fltSemantics &FltSemantics, |
| 224 | const KnownBits &Bits) { |
| 225 | assert(FltSemantics.sizeInBits == Bits.getBitWidth() && |
| 226 | "Bitcast operand has incorrect bit width" ); |
| 227 | KnownFPClass Known; |
| 228 | |
| 229 | // Conflicting known bits do not describe a concrete value. Return unknown. |
| 230 | if (Bits.hasConflict()) |
| 231 | return Known; |
| 232 | |
| 233 | // Return unknown for types we have not validated. |
| 234 | auto IsSupported = [](const fltSemantics &Semantics) { |
| 235 | switch (APFloat::SemanticsToEnum(Sem: Semantics)) { |
| 236 | case APFloatBase::S_IEEEhalf: |
| 237 | case APFloatBase::S_BFloat: |
| 238 | case APFloatBase::S_IEEEsingle: |
| 239 | case APFloatBase::S_IEEEdouble: |
| 240 | case APFloatBase::S_IEEEquad: |
| 241 | case APFloatBase::S_x87DoubleExtended: |
| 242 | return true; |
| 243 | default: |
| 244 | return false; |
| 245 | } |
| 246 | }; |
| 247 | if (!IsSupported(FltSemantics)) |
| 248 | return Known; |
| 249 | |
| 250 | // Transfer information from the sign bit. |
| 251 | if (Bits.isNonNegative()) |
| 252 | Known.signBitMustBeZero(); |
| 253 | else if (Bits.isNegative()) |
| 254 | Known.signBitMustBeOne(); |
| 255 | |
| 256 | switch (APFloatBase::SemanticsToEnum(Sem: FltSemantics)) { |
| 257 | case APFloatBase::S_IEEEhalf: |
| 258 | case APFloatBase::S_BFloat: |
| 259 | case APFloatBase::S_IEEEsingle: |
| 260 | case APFloatBase::S_IEEEdouble: |
| 261 | case APFloatBase::S_IEEEquad: { |
| 262 | const unsigned MantissaBits = FltSemantics.precision - 1; |
| 263 | const APInt ExponentMask = APInt::getBitsSet( |
| 264 | numBits: FltSemantics.sizeInBits, loBit: MantissaBits, hiBit: FltSemantics.sizeInBits - 1); |
| 265 | const APInt MantissaMask = |
| 266 | APInt::getLowBitsSet(numBits: FltSemantics.sizeInBits, loBitsSet: MantissaBits); |
| 267 | |
| 268 | const bool ExponentKnownAllZeros = |
| 269 | (Bits.Zero & ExponentMask) == ExponentMask; |
| 270 | const bool ExponentKnownAllOnes = (Bits.One & ExponentMask) == ExponentMask; |
| 271 | const bool ExponentKnownNotAllZeros = !(Bits.One & ExponentMask).isZero(); |
| 272 | const bool ExponentKnownNotAllOnes = !(Bits.Zero & ExponentMask).isZero(); |
| 273 | |
| 274 | const bool MantissaKnownAllZeros = |
| 275 | (Bits.Zero & MantissaMask) == MantissaMask; |
| 276 | const bool MantissaKnownNotAllZeros = !(Bits.One & MantissaMask).isZero(); |
| 277 | |
| 278 | // Zero and subnormal require an exponent with all zero bits. |
| 279 | if (ExponentKnownNotAllZeros) |
| 280 | Known.knownNot(RuleOut: fcZero | fcSubnormal); |
| 281 | |
| 282 | // Infinity and NaN require an exponent with all one bits. |
| 283 | if (ExponentKnownNotAllOnes) |
| 284 | Known.knownNot(RuleOut: fcInf | fcNan); |
| 285 | |
| 286 | // Normal values have an exponent that is not all zeros or all ones. |
| 287 | if (ExponentKnownAllZeros || ExponentKnownAllOnes) |
| 288 | Known.knownNot(RuleOut: fcNormal); |
| 289 | |
| 290 | // Zero and infinity require a mantissa with all zero bits. |
| 291 | if (MantissaKnownNotAllZeros) |
| 292 | Known.knownNot(RuleOut: fcZero | fcInf); |
| 293 | |
| 294 | // Subnormal and NaN require a non-zero mantissa. |
| 295 | if (MantissaKnownAllZeros) |
| 296 | Known.knownNot(RuleOut: fcSubnormal | fcNan); |
| 297 | |
| 298 | const bool QuietBitKnownSet = Bits.One[MantissaBits - 1]; |
| 299 | const bool QuietBitKnownClear = Bits.Zero[MantissaBits - 1]; |
| 300 | |
| 301 | if (QuietBitKnownSet) |
| 302 | Known.knownNot(RuleOut: fcSNan); |
| 303 | else if (QuietBitKnownClear) |
| 304 | Known.knownNot(RuleOut: fcQNan); |
| 305 | |
| 306 | break; |
| 307 | } |
| 308 | // Invalid for Motorola's 80-bit floating-point format. |
| 309 | case APFloatBase::S_x87DoubleExtended: { |
| 310 | // Layout of the x87 floating point type: |
| 311 | // [0:62] fraction |
| 312 | // [63] explicit integer bit |
| 313 | // [64:78] exponent |
| 314 | // [79] sign bit |
| 315 | // APFloat treats the x87 pseudo encodings as documented in |
| 316 | // fltSemantics::hasExplicitIntegerBit. |
| 317 | constexpr unsigned BitWidth = 80; |
| 318 | constexpr unsigned FractionBits = 63; |
| 319 | constexpr unsigned IntegerBitIndex = FractionBits; |
| 320 | const APInt FractionMask = APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: FractionBits); |
| 321 | const APInt ExponentMask = |
| 322 | APInt::getBitsSet(numBits: BitWidth, loBit: FractionBits + 1, hiBit: BitWidth - 1); |
| 323 | |
| 324 | const bool ExponentKnownAllZeros = |
| 325 | (Bits.Zero & ExponentMask) == ExponentMask; |
| 326 | const bool ExponentKnownAllOnes = (Bits.One & ExponentMask) == ExponentMask; |
| 327 | const bool ExponentKnownNotAllZeros = !(Bits.One & ExponentMask).isZero(); |
| 328 | const bool ExponentKnownNotAllOnes = !(Bits.Zero & ExponentMask).isZero(); |
| 329 | const bool FractionKnownAllZeros = |
| 330 | (Bits.Zero & FractionMask) == FractionMask; |
| 331 | const bool FractionKnownNotAllZeros = !(Bits.One & FractionMask).isZero(); |
| 332 | const bool IntegerBitKnownSet = Bits.One[IntegerBitIndex]; |
| 333 | const bool IntegerBitKnownClear = Bits.Zero[IntegerBitIndex]; |
| 334 | |
| 335 | // Zero and subnormal require an exponent with all zero bits and a cleared |
| 336 | // integer bit. |
| 337 | if (ExponentKnownNotAllZeros || IntegerBitKnownSet) |
| 338 | Known.knownNot(RuleOut: fcZero | fcSubnormal); |
| 339 | |
| 340 | // Zero requires a zero fraction. |
| 341 | if (FractionKnownNotAllZeros) |
| 342 | Known.knownNot(RuleOut: fcZero); |
| 343 | |
| 344 | // Infinity requires an exponent with all one bits, a set integer bit, and |
| 345 | // a zero fraction. |
| 346 | if (ExponentKnownNotAllOnes || IntegerBitKnownClear || |
| 347 | FractionKnownNotAllZeros) |
| 348 | Known.knownNot(RuleOut: fcInf); |
| 349 | |
| 350 | // A normal x87 value has its integer bit set. A zero exponent with that |
| 351 | // bit set is the x87 pseudodenormal encoding, which APFloat treats as |
| 352 | // normal. |
| 353 | if (IntegerBitKnownClear || ExponentKnownAllOnes) |
| 354 | Known.knownNot(RuleOut: fcNormal); |
| 355 | |
| 356 | // A subnormal has a nonzero fraction; NaNs are possible with a zero |
| 357 | // fraction through pseudoinfinity and unnormal encodings. |
| 358 | if (FractionKnownAllZeros) |
| 359 | Known.knownNot(RuleOut: fcSubnormal); |
| 360 | |
| 361 | // We can rule out NaN if the value has all exponent bits set to zero, or |
| 362 | // if the value is a canonical normal or infinity. |
| 363 | if (ExponentKnownAllZeros || |
| 364 | (IntegerBitKnownSet && |
| 365 | (ExponentKnownNotAllOnes || FractionKnownAllZeros))) |
| 366 | Known.knownNot(RuleOut: fcNan); |
| 367 | |
| 368 | // Non-canonical NaN has a non-zero exponent with the integer bit cleared. |
| 369 | const bool NeverNonCanonicalNaN = |
| 370 | ExponentKnownAllZeros || IntegerBitKnownSet; |
| 371 | |
| 372 | // Be conservative when handling non-canonical NaN. |
| 373 | if (NeverNonCanonicalNaN) { |
| 374 | const bool QuietBitKnownSet = Bits.One[FractionBits - 1]; |
| 375 | const bool QuietBitKnownClear = Bits.Zero[FractionBits - 1]; |
| 376 | |
| 377 | if (QuietBitKnownSet) |
| 378 | Known.knownNot(RuleOut: fcSNan); |
| 379 | else if (QuietBitKnownClear) |
| 380 | Known.knownNot(RuleOut: fcQNan); |
| 381 | } |
| 382 | |
| 383 | break; |
| 384 | } |
| 385 | default: |
| 386 | llvm_unreachable("unhandled supported type" ); |
| 387 | } |
| 388 | |
| 389 | return Known; |
| 390 | } |
| 391 | |
| 392 | KnownBits KnownFPClass::toKnownBits(const fltSemantics &FltSemantics) const { |
| 393 | KnownBits Known(FltSemantics.sizeInBits); |
| 394 | const FPClassTest FPClasses = getKnownFPClasses(); |
| 395 | |
| 396 | // Return unknown if poison. |
| 397 | if (FPClasses == fcNone) |
| 398 | return Known; |
| 399 | |
| 400 | // Return unknown for types we have not validated. |
| 401 | auto IsSupported = [](const fltSemantics &Semantics) { |
| 402 | switch (APFloat::SemanticsToEnum(Sem: Semantics)) { |
| 403 | case APFloatBase::S_IEEEhalf: |
| 404 | case APFloatBase::S_BFloat: |
| 405 | case APFloatBase::S_IEEEsingle: |
| 406 | case APFloatBase::S_IEEEdouble: |
| 407 | case APFloatBase::S_IEEEquad: |
| 408 | case APFloatBase::S_x87DoubleExtended: |
| 409 | return true; |
| 410 | default: |
| 411 | return false; |
| 412 | } |
| 413 | }; |
| 414 | if (!IsSupported(FltSemantics)) |
| 415 | return Known; |
| 416 | |
| 417 | switch (APFloat::SemanticsToEnum(Sem: FltSemantics)) { |
| 418 | case APFloatBase::S_IEEEhalf: |
| 419 | case APFloatBase::S_BFloat: |
| 420 | case APFloatBase::S_IEEEsingle: |
| 421 | case APFloatBase::S_IEEEdouble: |
| 422 | case APFloatBase::S_IEEEquad: { |
| 423 | // For ieee types, we cannot deduce anything if the source could be normal. |
| 424 | if (FPClasses & fcNormal) |
| 425 | break; |
| 426 | |
| 427 | Known.setAllConflict(); |
| 428 | |
| 429 | const unsigned BitWidth = FltSemantics.sizeInBits; |
| 430 | const unsigned MantissaBits = FltSemantics.precision - 1; |
| 431 | const unsigned ExponentBits = BitWidth - MantissaBits - 1; |
| 432 | |
| 433 | APInt MantissaMask = APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: MantissaBits); |
| 434 | APInt ExponentMask = |
| 435 | APInt::getBitsSet(numBits: BitWidth, loBit: MantissaBits, hiBit: MantissaBits + ExponentBits); |
| 436 | |
| 437 | const unsigned QuietBitIndex = MantissaBits - 1; |
| 438 | APInt PayloadMask = MantissaMask; |
| 439 | PayloadMask.clearBit(BitPosition: QuietBitIndex); |
| 440 | |
| 441 | if (FPClasses & fcNan) { |
| 442 | // Exponent bits cannot be zeros. |
| 443 | Known.Zero &= ~ExponentMask; |
| 444 | // No individual payload bit is known. |
| 445 | Known.Zero &= ~PayloadMask; |
| 446 | Known.One &= ~PayloadMask; |
| 447 | |
| 448 | if (FPClasses & fcQNan) |
| 449 | Known.Zero.clearBit(BitPosition: QuietBitIndex); |
| 450 | if (FPClasses & fcSNan) |
| 451 | Known.One.clearBit(BitPosition: QuietBitIndex); |
| 452 | } |
| 453 | if (FPClasses & fcInf) { |
| 454 | // Exponent bits cannot be zeros. |
| 455 | Known.Zero &= ~ExponentMask; |
| 456 | // Mantissa bits cannot be ones. |
| 457 | Known.One &= ~MantissaMask; |
| 458 | } |
| 459 | if (FPClasses & fcSubnormal) { |
| 460 | // Exponent bits cannot be ones. |
| 461 | Known.One &= ~ExponentMask; |
| 462 | // Unknown mantissa. |
| 463 | Known.One &= ~MantissaMask; |
| 464 | Known.Zero &= ~MantissaMask; |
| 465 | } |
| 466 | if (FPClasses & fcZero) { |
| 467 | // Exponent bits cannot be ones. |
| 468 | Known.One &= ~ExponentMask; |
| 469 | // Mantissa cannot be ones. |
| 470 | Known.One &= ~MantissaMask; |
| 471 | } |
| 472 | |
| 473 | break; |
| 474 | } |
| 475 | case APFloatBase::S_x87DoubleExtended: { |
| 476 | if (!isKnownNever(Mask: fcNormal | fcSubnormal | fcNan)) |
| 477 | break; |
| 478 | |
| 479 | Known.setAllConflict(); |
| 480 | |
| 481 | if (FPClasses & fcInf) |
| 482 | Known = Known.intersectWith(RHS: KnownBits::makeConstant( |
| 483 | C: APFloat::getInf(Sem: FltSemantics).bitcastToAPInt())); |
| 484 | |
| 485 | if (FPClasses & fcZero) |
| 486 | Known = Known.intersectWith( |
| 487 | RHS: KnownBits::makeConstant(C: APInt::getZero(numBits: FltSemantics.sizeInBits))); |
| 488 | |
| 489 | break; |
| 490 | } |
| 491 | default: |
| 492 | llvm_unreachable("unhandled supported semantics" ); |
| 493 | } |
| 494 | |
| 495 | Known.Zero.clearSignBit(); |
| 496 | Known.One.clearSignBit(); |
| 497 | if (std::optional<bool> Sign = getSignBit()) { |
| 498 | if (*Sign) |
| 499 | Known.makeNegative(); |
| 500 | else |
| 501 | Known.makeNonNegative(); |
| 502 | } |
| 503 | |
| 504 | return Known; |
| 505 | } |
| 506 | |
| 507 | // Handle known sign bit and NaN cases for fadd and fsub. Invalid for fma. |
| 508 | static KnownFPClass fadd_impl(const KnownFPClass &KnownLHS, |
| 509 | const KnownFPClass &KnownRHS, RoundingMode RM) { |
| 510 | KnownFPClass Known; |
| 511 | |
| 512 | Known.propagateNonSNaN(LHS: KnownLHS, RHS: KnownRHS); |
| 513 | |
| 514 | // Adding positive and negative infinity produces NaN, but only if both |
| 515 | // opposite-sign infinity combinations are possible. |
| 516 | if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
| 517 | (KnownLHS.isKnownNever(Mask: fcPosInf) || KnownRHS.isKnownNever(Mask: fcNegInf)) && |
| 518 | (KnownLHS.isKnownNever(Mask: fcNegInf) || KnownRHS.isKnownNever(Mask: fcPosInf))) |
| 519 | Known.knownNot(RuleOut: fcNan); |
| 520 | |
| 521 | // Rule out infinity. |
| 522 | if (KnownLHS.isKnownNever(Mask: fcPosInf) && KnownRHS.isKnownNever(Mask: fcPosInf)) { |
| 523 | // Adding a non-zero finite positive value to a large finite positive value |
| 524 | // may overflow. |
| 525 | if ((KnownLHS.isKnownNever(Mask: fcPosNormal) || |
| 526 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal)) && |
| 527 | (KnownRHS.isKnownNever(Mask: fcPosNormal) || |
| 528 | KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal))) |
| 529 | Known.knownNot(RuleOut: fcPosInf); |
| 530 | } |
| 531 | if (KnownLHS.isKnownNever(Mask: fcNegInf) && KnownRHS.isKnownNever(Mask: fcNegInf)) { |
| 532 | // Adding a non-zero finite negative value to a large finite negative value |
| 533 | // may overflow. |
| 534 | if ((KnownLHS.isKnownNever(Mask: fcNegNormal) || |
| 535 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal)) && |
| 536 | (KnownRHS.isKnownNever(Mask: fcNegNormal) || |
| 537 | KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal))) |
| 538 | Known.knownNot(RuleOut: fcNegInf); |
| 539 | } |
| 540 | |
| 541 | // pnorm + finite and psub + psub may produce pnorm. |
| 542 | if (KnownLHS.isKnownNever(Mask: fcPosNormal) && |
| 543 | KnownRHS.isKnownNever(Mask: fcPosNormal) && |
| 544 | (KnownLHS.isKnownNever(Mask: fcPosSubnormal) || |
| 545 | KnownRHS.isKnownNever(Mask: fcPosSubnormal))) |
| 546 | Known.knownNot(RuleOut: fcPosNormal); |
| 547 | // nnorm + finite and nsub + nsub may produce nnorm. |
| 548 | if (KnownLHS.isKnownNever(Mask: fcNegNormal) && |
| 549 | KnownRHS.isKnownNever(Mask: fcNegNormal) && |
| 550 | (KnownLHS.isKnownNever(Mask: fcNegSubnormal) || |
| 551 | KnownRHS.isKnownNever(Mask: fcNegSubnormal))) |
| 552 | Known.knownNot(RuleOut: fcNegNormal); |
| 553 | |
| 554 | // psub + (zero | sub) and pnorm + (nsub | nnorm) may produce psub. |
| 555 | if ((KnownLHS.isKnownNever(Mask: fcPosSubnormal) || |
| 556 | KnownRHS.isKnownNever(Mask: fcZero | fcSubnormal)) && |
| 557 | (KnownRHS.isKnownNever(Mask: fcPosSubnormal) || |
| 558 | KnownLHS.isKnownNever(Mask: fcZero | fcSubnormal)) && |
| 559 | (KnownLHS.isKnownNever(Mask: fcPosNormal) || |
| 560 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal)) && |
| 561 | (KnownRHS.isKnownNever(Mask: fcPosNormal) || |
| 562 | KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal))) |
| 563 | Known.knownNot(RuleOut: fcPosSubnormal); |
| 564 | // nsub + (zero | sub) and nnorm + (psub | pnorm) may produce nsub. |
| 565 | if ((KnownLHS.isKnownNever(Mask: fcNegSubnormal) || |
| 566 | KnownRHS.isKnownNever(Mask: fcZero | fcSubnormal)) && |
| 567 | (KnownRHS.isKnownNever(Mask: fcNegSubnormal) || |
| 568 | KnownLHS.isKnownNever(Mask: fcZero | fcSubnormal)) && |
| 569 | (KnownLHS.isKnownNever(Mask: fcNegNormal) || |
| 570 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal)) && |
| 571 | (KnownRHS.isKnownNever(Mask: fcNegNormal) || |
| 572 | KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal))) |
| 573 | Known.knownNot(RuleOut: fcNegSubnormal); |
| 574 | |
| 575 | // x + y is zero if x == -y (cancellation) or if x and y are both zeros. |
| 576 | if ((KnownLHS.isKnownNever(Mask: fcPosNormal) || |
| 577 | KnownRHS.isKnownNever(Mask: fcNegNormal)) && |
| 578 | (KnownRHS.isKnownNever(Mask: fcPosNormal) || |
| 579 | KnownLHS.isKnownNever(Mask: fcNegNormal)) && |
| 580 | (KnownLHS.isKnownNever(Mask: fcPosSubnormal) || |
| 581 | KnownRHS.isKnownNever(Mask: fcNegSubnormal)) && |
| 582 | (KnownRHS.isKnownNever(Mask: fcPosSubnormal) || |
| 583 | KnownLHS.isKnownNever(Mask: fcNegSubnormal))) { |
| 584 | // zero + non-zero == non-zero. |
| 585 | if (KnownLHS.isKnownNever(Mask: fcZero) || KnownRHS.isKnownNever(Mask: fcZero)) |
| 586 | Known.knownNot(RuleOut: fcZero); |
| 587 | // -0.0 + -0.0 != +0.0 |
| 588 | if (KnownLHS.isKnownNever(Mask: fcPosZero) && KnownRHS.isKnownNever(Mask: fcPosZero)) |
| 589 | Known.knownNot(RuleOut: fcPosZero); |
| 590 | // +0.0 + +0.0 != -0.0 |
| 591 | if (KnownLHS.isKnownNever(Mask: fcNegZero) && KnownRHS.isKnownNever(Mask: fcNegZero)) |
| 592 | Known.knownNot(RuleOut: fcNegZero); |
| 593 | } |
| 594 | |
| 595 | switch (RM) { |
| 596 | case RoundingMode::NearestTiesToEven: |
| 597 | case RoundingMode::NearestTiesToAway: |
| 598 | case RoundingMode::TowardZero: |
| 599 | case RoundingMode::TowardPositive: |
| 600 | // Only -0.0 + -0.0 produces -0.0 for these rounding modes. |
| 601 | if (KnownLHS.isKnownNever(Mask: fcNegZero) || KnownRHS.isKnownNever(Mask: fcNegZero)) |
| 602 | Known.knownNot(RuleOut: fcNegZero); |
| 603 | break; |
| 604 | case RoundingMode::TowardNegative: |
| 605 | // Only +0.0 + +0.0 produces +0.0 when rounding towards negative infinity. |
| 606 | if (KnownLHS.isKnownNever(Mask: fcPosZero) || KnownRHS.isKnownNever(Mask: fcPosZero)) |
| 607 | Known.knownNot(RuleOut: fcPosZero); |
| 608 | break; |
| 609 | default: |
| 610 | break; |
| 611 | } |
| 612 | |
| 613 | return Known; |
| 614 | } |
| 615 | |
| 616 | KnownFPClass KnownFPClass::fadd(const KnownFPClass &KnownLHS_, |
| 617 | const KnownFPClass &KnownRHS_, RoundingMode RM, |
| 618 | DenormalMode Mode) { |
| 619 | KnownFPClass KnownLHS = applyInputDenormalMode(KnownSrc: KnownLHS_, Mode); |
| 620 | KnownFPClass KnownRHS = applyInputDenormalMode(KnownSrc: KnownRHS_, Mode); |
| 621 | |
| 622 | KnownFPClass Known = fadd_impl(KnownLHS, KnownRHS, RM); |
| 623 | |
| 624 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 625 | } |
| 626 | |
| 627 | KnownFPClass KnownFPClass::fadd(const KnownFPClass &KnownLHS_, |
| 628 | const APFloat &RHS, RoundingMode RM, |
| 629 | DenormalMode Mode) { |
| 630 | const auto &Semantics = RHS.getSemantics(); |
| 631 | |
| 632 | // Only handle fcNormal constants, and bail out for PPCDoubleDouble. |
| 633 | if (!RHS.isNormal() || &Semantics == &APFloat::PPCDoubleDouble()) |
| 634 | return fadd(KnownLHS_, KnownRHS_: KnownFPClass(RHS), RM, Mode); |
| 635 | |
| 636 | KnownFPClass KnownLHS = applyInputDenormalMode(KnownSrc: KnownLHS_, Mode); |
| 637 | KnownFPClass Known = |
| 638 | fadd(KnownLHS_: KnownLHS, KnownRHS_: KnownFPClass(RHS), RM, Mode: DenormalMode::getIEEE()); |
| 639 | |
| 640 | int MinExp = APFloat::semanticsMinExponent(Semantics); |
| 641 | int Precision = static_cast<int>(APFloat::semanticsPrecision(Semantics)); |
| 642 | int ThresholdExp = MinExp + Precision - 1; |
| 643 | |
| 644 | // If |C| > T, X + C cannot be subnormal under any rounding mode. |
| 645 | int Exponent = ilogb(Arg: RHS); |
| 646 | if (Exponent > ThresholdExp || |
| 647 | (Exponent == ThresholdExp && RHS.getExactLog2Abs() == INT_MIN)) |
| 648 | Known.knownNot(RuleOut: fcSubnormal); |
| 649 | |
| 650 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 651 | } |
| 652 | |
| 653 | KnownFPClass KnownFPClass::fadd_self(const KnownFPClass &KnownSrc_, |
| 654 | RoundingMode RM, DenormalMode Mode) { |
| 655 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 656 | |
| 657 | // TODO: Refine fadd_self for directed rounding modes. |
| 658 | |
| 659 | KnownFPClass Known; |
| 660 | |
| 661 | Known.propagateNonNaN(Src: KnownSrc); |
| 662 | |
| 663 | if (KnownSrc.isKnownNever(Mask: fcPosNormal | fcPosInf)) |
| 664 | Known.knownNot(RuleOut: fcPosInf); |
| 665 | if (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegInf)) |
| 666 | Known.knownNot(RuleOut: fcNegInf); |
| 667 | |
| 668 | if (KnownSrc.isKnownNever(Mask: fcPosSubnormal | fcPosNormal)) |
| 669 | Known.knownNot(RuleOut: fcPosNormal); |
| 670 | if (KnownSrc.isKnownNever(Mask: fcNegSubnormal | fcNegNormal)) |
| 671 | Known.knownNot(RuleOut: fcNegNormal); |
| 672 | |
| 673 | if (KnownSrc.isKnownNever(Mask: fcPosSubnormal)) |
| 674 | Known.knownNot(RuleOut: fcPosSubnormal); |
| 675 | if (KnownSrc.isKnownNever(Mask: fcNegSubnormal)) |
| 676 | Known.knownNot(RuleOut: fcNegSubnormal); |
| 677 | |
| 678 | // Doubling 0.0 will give the same 0.0. |
| 679 | if (KnownSrc.isKnownNever(Mask: fcPosZero)) |
| 680 | Known.knownNot(RuleOut: fcPosZero); |
| 681 | if (KnownSrc.isKnownNever(Mask: fcNegZero)) |
| 682 | Known.knownNot(RuleOut: fcNegZero); |
| 683 | |
| 684 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 685 | } |
| 686 | |
| 687 | KnownFPClass KnownFPClass::fsub(const KnownFPClass &KnownLHS_, |
| 688 | const KnownFPClass &KnownRHS_, RoundingMode RM, |
| 689 | DenormalMode Mode) { |
| 690 | KnownFPClass KnownLHS = applyInputDenormalMode(KnownSrc: KnownLHS_, Mode); |
| 691 | KnownFPClass KnownRHS = applyInputDenormalMode(KnownSrc: KnownRHS_, Mode); |
| 692 | |
| 693 | // fsub(x, y) == fadd(x, -y) as long as we negate after subnormals are |
| 694 | // flushed. |
| 695 | KnownRHS.fneg(); |
| 696 | |
| 697 | KnownFPClass Known = fadd_impl(KnownLHS, KnownRHS, RM); |
| 698 | |
| 699 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 700 | } |
| 701 | |
| 702 | KnownFPClass KnownFPClass::fmul(const KnownFPClass &KnownLHS_, |
| 703 | const KnownFPClass &KnownRHS_, |
| 704 | DenormalMode Mode) { |
| 705 | KnownFPClass KnownLHS = applyInputDenormalMode(KnownSrc: KnownLHS_, Mode); |
| 706 | KnownFPClass KnownRHS = applyInputDenormalMode(KnownSrc: KnownRHS_, Mode); |
| 707 | |
| 708 | KnownFPClass Known; |
| 709 | |
| 710 | // fmul will xor the sign-bit of non-NaN inputs: |
| 711 | // +X * +Y or -X * -Y => +Q |
| 712 | // +X * -Y or -X * +Y => -Q |
| 713 | |
| 714 | Known.propagateNonSNaN(LHS: KnownLHS, RHS: KnownRHS); |
| 715 | |
| 716 | // NaN is generated from 0.0 * inf and inf * 0.0. |
| 717 | if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
| 718 | (KnownRHS.isKnownNeverInfinity() || KnownLHS.isKnownNever(Mask: fcZero)) && |
| 719 | (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNever(Mask: fcZero))) |
| 720 | Known.knownNot(RuleOut: fcNan); |
| 721 | |
| 722 | // Infinity may be produced from two non-zero inputs. |
| 723 | if ((KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf) || |
| 724 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf)) && |
| 725 | (KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf) || |
| 726 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf))) |
| 727 | Known.knownNot(RuleOut: fcPosInf); |
| 728 | if ((KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf) || |
| 729 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf)) && |
| 730 | (KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf) || |
| 731 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf))) |
| 732 | Known.knownNot(RuleOut: fcNegInf); |
| 733 | |
| 734 | // Normal and subnormal results require two non-zero finite operands. |
| 735 | if ((KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal) || |
| 736 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal)) && |
| 737 | (KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal) || |
| 738 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal))) |
| 739 | Known.knownNot(RuleOut: fcPosSubnormal | fcPosNormal); |
| 740 | if ((KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal) || |
| 741 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal)) && |
| 742 | (KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal) || |
| 743 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal))) |
| 744 | Known.knownNot(RuleOut: fcNegSubnormal | fcNegNormal); |
| 745 | |
| 746 | // Zero may be produced from two finite inputs. |
| 747 | if ((KnownLHS.isKnownNever(Mask: fcPosFinite) || |
| 748 | KnownRHS.isKnownNever(Mask: fcPosFinite)) && |
| 749 | (KnownLHS.isKnownNever(Mask: fcNegFinite) || |
| 750 | KnownRHS.isKnownNever(Mask: fcNegFinite))) |
| 751 | Known.knownNot(RuleOut: fcPosZero); |
| 752 | if ((KnownLHS.isKnownNever(Mask: fcPosFinite) || |
| 753 | KnownRHS.isKnownNever(Mask: fcNegFinite)) && |
| 754 | (KnownLHS.isKnownNever(Mask: fcNegFinite) || |
| 755 | KnownRHS.isKnownNever(Mask: fcPosFinite))) |
| 756 | Known.knownNot(RuleOut: fcNegZero); |
| 757 | |
| 758 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 759 | } |
| 760 | |
| 761 | // TODO: This generalizes to known ranges |
| 762 | KnownFPClass KnownFPClass::fmul(const KnownFPClass &KnownLHS, |
| 763 | const APFloat &CRHS, DenormalMode Mode) { |
| 764 | // Match denormal scaling pattern, similar to the case in ldexp. If the |
| 765 | // constant's exponent is sufficiently large, the result cannot be subnormal. |
| 766 | |
| 767 | const fltSemantics &Flt = CRHS.getSemantics(); |
| 768 | unsigned Precision = APFloat::semanticsPrecision(Flt); |
| 769 | const int MantissaBits = Precision - 1; |
| 770 | |
| 771 | int MinKnownExponent = ilogb(Arg: CRHS); |
| 772 | bool CannotBeSubnormal = (MinKnownExponent >= MantissaBits); |
| 773 | |
| 774 | KnownFPClass Known = KnownFPClass::fmul(KnownLHS_: KnownLHS, KnownRHS_: KnownFPClass(CRHS), Mode); |
| 775 | if (CannotBeSubnormal) |
| 776 | Known.knownNot(RuleOut: fcSubnormal); |
| 777 | |
| 778 | // Multiply of values <= 1 cannot introduce overflow. |
| 779 | if (KnownLHS.isKnownNever(Mask: fcInf)) { |
| 780 | if (MinKnownExponent < 0) |
| 781 | Known.knownNot(RuleOut: fcInf); |
| 782 | else if (MinKnownExponent == 0 && CRHS.compareAbsoluteValue(RHS: APFloat::getOne( |
| 783 | Sem: Flt)) == APFloat::cmpEqual) |
| 784 | Known.knownNot(RuleOut: fcInf); |
| 785 | } |
| 786 | |
| 787 | return Known; |
| 788 | } |
| 789 | |
| 790 | KnownFPClass KnownFPClass::square(const KnownFPClass &KnownSrc_, |
| 791 | DenormalMode Mode) { |
| 792 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 793 | |
| 794 | KnownFPClass Known = fmul(KnownLHS_: KnownSrc, KnownRHS_: KnownSrc, Mode: DenormalMode::getIEEE()); |
| 795 | |
| 796 | // 0.0 * inf and inf * 0.0 are impossible. |
| 797 | Known.propagateNonNaN(Src: KnownSrc); |
| 798 | |
| 799 | // X * X is always non-negative or a NaN. |
| 800 | Known.knownNot(RuleOut: fcNegative); |
| 801 | |
| 802 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 803 | } |
| 804 | |
| 805 | KnownFPClass KnownFPClass::fdiv(const KnownFPClass &KnownLHS_, |
| 806 | const KnownFPClass &KnownRHS_, |
| 807 | DenormalMode Mode) { |
| 808 | KnownFPClass KnownLHS = applyInputDenormalMode(KnownSrc: KnownLHS_, Mode); |
| 809 | KnownFPClass KnownRHS = applyInputDenormalMode(KnownSrc: KnownRHS_, Mode); |
| 810 | |
| 811 | KnownFPClass Known; |
| 812 | |
| 813 | // fdiv will xor the sign-bit of non-NaN inputs: |
| 814 | // +X / +Y or -X / -Y => +Q |
| 815 | // +X / -Y or -X / +Y => -Q |
| 816 | |
| 817 | Known.propagateNonSNaN(LHS: KnownLHS, RHS: KnownRHS); |
| 818 | |
| 819 | // NaN is generated from 0.0 / 0.0 and inf / inf. |
| 820 | if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
| 821 | (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()) && |
| 822 | (KnownLHS.isKnownNever(Mask: fcZero) || KnownRHS.isKnownNever(Mask: fcZero))) |
| 823 | Known.knownNot(RuleOut: fcNan); |
| 824 | |
| 825 | // Infinity may be produced from non-zero / finite. |
| 826 | if ((KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf) || |
| 827 | KnownRHS.isKnownNever(Mask: fcPosFinite)) && |
| 828 | (KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf) || |
| 829 | KnownRHS.isKnownNever(Mask: fcNegFinite))) |
| 830 | Known.knownNot(RuleOut: fcPosInf); |
| 831 | if ((KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf) || |
| 832 | KnownRHS.isKnownNever(Mask: fcNegFinite)) && |
| 833 | (KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf) || |
| 834 | KnownRHS.isKnownNever(Mask: fcPosFinite))) |
| 835 | Known.knownNot(RuleOut: fcNegInf); |
| 836 | |
| 837 | // Normal and subnormal results require two non-zero finite operands. |
| 838 | if ((KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal) || |
| 839 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal)) && |
| 840 | (KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal) || |
| 841 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal))) |
| 842 | Known.knownNot(RuleOut: fcPosSubnormal | fcPosNormal); |
| 843 | if ((KnownLHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal) || |
| 844 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal)) && |
| 845 | (KnownLHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal) || |
| 846 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal))) |
| 847 | Known.knownNot(RuleOut: fcNegSubnormal | fcNegNormal); |
| 848 | |
| 849 | // Zero may be produced from finite / non-zero. |
| 850 | if ((KnownLHS.isKnownNever(Mask: fcPosFinite) || |
| 851 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf)) && |
| 852 | (KnownLHS.isKnownNever(Mask: fcNegFinite) || |
| 853 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf))) |
| 854 | Known.knownNot(RuleOut: fcPosZero); |
| 855 | if ((KnownLHS.isKnownNever(Mask: fcPosFinite) || |
| 856 | KnownRHS.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf)) && |
| 857 | (KnownLHS.isKnownNever(Mask: fcNegFinite) || |
| 858 | KnownRHS.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf))) |
| 859 | Known.knownNot(RuleOut: fcNegZero); |
| 860 | |
| 861 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 862 | } |
| 863 | |
| 864 | KnownFPClass KnownFPClass::fdiv_self(const KnownFPClass &KnownSrc_, |
| 865 | DenormalMode Mode) { |
| 866 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 867 | |
| 868 | // X / X is always exactly 1.0 or a NaN. |
| 869 | KnownFPClass Known(fcNan | fcPosNormal); |
| 870 | |
| 871 | Known.propagateNonSNaN(Src: KnownSrc); |
| 872 | |
| 873 | if (KnownSrc.isKnownNeverInfOrNaN() && KnownSrc.isKnownNever(Mask: fcZero)) |
| 874 | Known.knownNot(RuleOut: fcNan); |
| 875 | |
| 876 | // The result is never subnormal, so the output denormal mode does not matter. |
| 877 | return Known; |
| 878 | } |
| 879 | |
| 880 | KnownFPClass KnownFPClass::frem(const KnownFPClass &KnownLHS_, |
| 881 | const KnownFPClass &KnownRHS_, |
| 882 | DenormalMode Mode) { |
| 883 | KnownFPClass KnownLHS = applyInputDenormalMode(KnownSrc: KnownLHS_, Mode); |
| 884 | KnownFPClass KnownRHS = applyInputDenormalMode(KnownSrc: KnownRHS_, Mode); |
| 885 | KnownFPClass Known; |
| 886 | |
| 887 | // We are assuming that frem(x, Inf) is already handled elsewhere. |
| 888 | |
| 889 | Known.knownNot(RuleOut: fcInf); |
| 890 | |
| 891 | Known.propagateNonSNaN(LHS: KnownLHS, RHS: KnownRHS); |
| 892 | |
| 893 | // frem(Inf, x) and frem(x, 0) produce NaN. |
| 894 | if (KnownLHS.isKnownNeverInfOrNaN() && KnownRHS.isKnownNeverNaN() && |
| 895 | KnownRHS.isKnownNever(Mask: fcZero)) { |
| 896 | Known.knownNot(RuleOut: fcNan); |
| 897 | } |
| 898 | |
| 899 | // The sign for frem is the same as the first operand. |
| 900 | if (KnownLHS.isKnownNever(Mask: fcPosNormal | fcPosSubnormal)) |
| 901 | Known.knownNot(RuleOut: fcPosNormal | fcPosSubnormal); |
| 902 | if (KnownLHS.isKnownNever(Mask: fcNegNormal | fcNegSubnormal)) |
| 903 | Known.knownNot(RuleOut: fcNegNormal | fcNegSubnormal); |
| 904 | |
| 905 | // A negative zero result requires a negative finite first operand. |
| 906 | if (KnownLHS.isKnownNever(Mask: fcNegFinite)) |
| 907 | Known.knownNot(RuleOut: fcNegZero); |
| 908 | |
| 909 | // A positive zero result requires a positive finite first operand. |
| 910 | if (KnownLHS.isKnownNever(Mask: fcPosFinite)) |
| 911 | Known.knownNot(RuleOut: fcPosZero); |
| 912 | |
| 913 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 914 | } |
| 915 | |
| 916 | KnownFPClass KnownFPClass::frem_self(const KnownFPClass &KnownSrc_, |
| 917 | DenormalMode Mode) { |
| 918 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 919 | |
| 920 | // X % X is always exactly [+-]0.0 or a NaN. |
| 921 | KnownFPClass Known(fcNan | fcZero); |
| 922 | |
| 923 | Known.propagateNonSNaN(Src: KnownSrc); |
| 924 | |
| 925 | if (KnownSrc.isKnownNeverInfOrNaN() && KnownSrc.isKnownNever(Mask: fcZero)) |
| 926 | Known.knownNot(RuleOut: fcNan); |
| 927 | |
| 928 | // The sign of a zero result is the sign of the finite non-zero input. |
| 929 | if (KnownSrc.isKnownNever(Mask: fcPosNormal | fcPosSubnormal)) |
| 930 | Known.knownNot(RuleOut: fcPosZero); |
| 931 | if (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegSubnormal)) |
| 932 | Known.knownNot(RuleOut: fcNegZero); |
| 933 | |
| 934 | // The result is never subnormal, so the output denormal mode does not matter. |
| 935 | return Known; |
| 936 | } |
| 937 | |
| 938 | static KnownFPClass fma_impl(const KnownFPClass &KnownLHS, |
| 939 | const KnownFPClass &KnownRHS) { |
| 940 | KnownFPClass Known; |
| 941 | |
| 942 | if (KnownLHS.cannotBeOrderedLessThanZero() && |
| 943 | KnownRHS.cannotBeOrderedLessThanZero()) { |
| 944 | Known.knownNot(RuleOut: KnownFPClass::OrderedLessThanZeroMask); |
| 945 | |
| 946 | // This can't underflow if one of the operands is known normal. |
| 947 | if (KnownLHS.isKnownNever(Mask: fcZero | fcPosSubnormal) || |
| 948 | KnownRHS.isKnownNever(Mask: fcZero | fcPosSubnormal)) |
| 949 | Known.knownNot(RuleOut: fcZero | fcPosSubnormal); |
| 950 | } |
| 951 | |
| 952 | if (KnownLHS.cannotBeOrderedGreaterThanZero() && |
| 953 | KnownRHS.cannotBeOrderedGreaterThanZero()) { |
| 954 | Known.knownNot(RuleOut: KnownFPClass::OrderedGreaterThanZeroMask); |
| 955 | |
| 956 | // This can't underflow if one of the operands is known normal. |
| 957 | if (KnownLHS.isKnownNever(Mask: fcZero | fcNegSubnormal) || |
| 958 | KnownRHS.isKnownNever(Mask: fcZero | fcNegSubnormal)) |
| 959 | Known.knownNot(RuleOut: fcZero | fcNegSubnormal); |
| 960 | } |
| 961 | |
| 962 | return Known; |
| 963 | } |
| 964 | |
| 965 | KnownFPClass KnownFPClass::fma(const KnownFPClass &KnownLHS_, |
| 966 | const KnownFPClass &KnownRHS_, |
| 967 | const KnownFPClass &KnownAddend_, |
| 968 | DenormalMode Mode) { |
| 969 | KnownFPClass KnownLHS = applyInputDenormalMode(KnownSrc: KnownLHS_, Mode); |
| 970 | KnownFPClass KnownRHS = applyInputDenormalMode(KnownSrc: KnownRHS_, Mode); |
| 971 | KnownFPClass KnownAddend = applyInputDenormalMode(KnownSrc: KnownAddend_, Mode); |
| 972 | |
| 973 | KnownFPClass Mul = fmul(KnownLHS_: KnownLHS, KnownRHS_: KnownRHS, Mode: DenormalMode::getIEEE()); |
| 974 | |
| 975 | // FMA differs from the base fmul + fadd handling only in the treatment of -0 |
| 976 | // results. |
| 977 | // |
| 978 | // If the multiply is a -0 due to rounding, the final -0 + 0 will be -0, |
| 979 | // unlike for a separate fadd. |
| 980 | KnownFPClass Known = fma_impl(KnownLHS: Mul, KnownRHS: KnownAddend); |
| 981 | |
| 982 | // propagateNonSNaN for 3 arguments. |
| 983 | if (KnownLHS.isKnownNever(Mask: fcSNan) && KnownRHS.isKnownNever(Mask: fcSNan) && |
| 984 | KnownAddend.isKnownNever(Mask: fcSNan)) |
| 985 | Known.knownNot(RuleOut: fcSNan); |
| 986 | |
| 987 | // Rule out NaN. |
| 988 | if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() && |
| 989 | KnownAddend.isKnownNeverNaN()) { |
| 990 | // NaN is produced when zero is multiplied by infinity, or when adding two |
| 991 | // exact infinities of the opposite sign. An exact infinity requires at |
| 992 | // least one infinite operand. |
| 993 | |
| 994 | const bool NeverZeroMulInf = |
| 995 | (KnownLHS.isKnownNever(Mask: fcInf) || KnownRHS.isKnownNever(Mask: fcZero)) && |
| 996 | (KnownRHS.isKnownNever(Mask: fcInf) || KnownLHS.isKnownNever(Mask: fcZero)); |
| 997 | // Multiplying infinity by a non-zero finite may change the sign. |
| 998 | const bool NeverExactPosInf = |
| 999 | (KnownLHS.isKnownNever(Mask: fcPosInf) || |
| 1000 | KnownRHS.isKnownNever(Mask: fcPosInf | fcPosNormal | fcPosSubnormal)) && |
| 1001 | (KnownRHS.isKnownNever(Mask: fcPosInf) || |
| 1002 | KnownLHS.isKnownNever(Mask: fcPosInf | fcPosNormal | fcPosSubnormal)) && |
| 1003 | (KnownLHS.isKnownNever(Mask: fcNegInf) || |
| 1004 | KnownRHS.isKnownNever(Mask: fcNegInf | fcNegNormal | fcNegSubnormal)) && |
| 1005 | (KnownRHS.isKnownNever(Mask: fcNegInf) || |
| 1006 | KnownLHS.isKnownNever(Mask: fcNegInf | fcNegNormal | fcNegSubnormal)); |
| 1007 | const bool NeverExactNegInf = |
| 1008 | (KnownLHS.isKnownNever(Mask: fcPosInf) || |
| 1009 | KnownRHS.isKnownNever(Mask: fcNegInf | fcNegNormal | fcNegSubnormal)) && |
| 1010 | (KnownRHS.isKnownNever(Mask: fcPosInf) || |
| 1011 | KnownLHS.isKnownNever(Mask: fcNegInf | fcNegNormal | fcNegSubnormal)) && |
| 1012 | (KnownLHS.isKnownNever(Mask: fcNegInf) || |
| 1013 | KnownRHS.isKnownNever(Mask: fcPosInf | fcPosNormal | fcPosSubnormal)) && |
| 1014 | (KnownRHS.isKnownNever(Mask: fcNegInf) || |
| 1015 | KnownLHS.isKnownNever(Mask: fcPosInf | fcPosNormal | fcPosSubnormal)); |
| 1016 | if (NeverZeroMulInf && |
| 1017 | (KnownAddend.isKnownNever(Mask: fcPosInf) || NeverExactNegInf) && |
| 1018 | (KnownAddend.isKnownNever(Mask: fcNegInf) || NeverExactPosInf)) |
| 1019 | Known.knownNot(RuleOut: fcNan); |
| 1020 | } |
| 1021 | |
| 1022 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 1023 | } |
| 1024 | |
| 1025 | KnownFPClass KnownFPClass::fmuladd(const KnownFPClass &KnownLHS, |
| 1026 | const KnownFPClass &KnownRHS, |
| 1027 | const KnownFPClass &KnownAddend, |
| 1028 | DenormalMode Mode) { |
| 1029 | // fmuladd can lower to either fma(x, y, z) or fadd(fmul(x, y), z). Compute |
| 1030 | // the intersection of both possibilities. |
| 1031 | KnownFPClass FMA = fma(KnownLHS_: KnownLHS, KnownRHS_: KnownRHS, KnownAddend_: KnownAddend, Mode); |
| 1032 | KnownFPClass MulAdd = fadd(KnownLHS_: fmul(KnownLHS_: KnownLHS, KnownRHS_: KnownRHS, Mode), KnownRHS_: KnownAddend, |
| 1033 | RM: RoundingMode::NearestTiesToEven, Mode); |
| 1034 | return FMA.intersectWith(RHS: MulAdd); |
| 1035 | } |
| 1036 | |
| 1037 | KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared_, |
| 1038 | const KnownFPClass &KnownAddend_, |
| 1039 | DenormalMode Mode) { |
| 1040 | KnownFPClass KnownSquared = applyInputDenormalMode(KnownSrc: KnownSquared_, Mode); |
| 1041 | KnownFPClass KnownAddend = applyInputDenormalMode(KnownSrc: KnownAddend_, Mode); |
| 1042 | |
| 1043 | KnownFPClass Squared = square(KnownSrc_: KnownSquared, Mode: DenormalMode::getIEEE()); |
| 1044 | KnownFPClass Known = fma_impl(KnownLHS: Squared, KnownRHS: KnownAddend); |
| 1045 | |
| 1046 | Known.propagateNonSNaN(LHS: KnownSquared, RHS: KnownAddend); |
| 1047 | |
| 1048 | // Only fma(+inf, +inf, -inf) and fma(-inf, -inf, -inf) produce NaN when |
| 1049 | // squaring. |
| 1050 | if (KnownSquared.isKnownNeverNaN() && KnownAddend.isKnownNeverNaN() && |
| 1051 | (KnownSquared.isKnownNever(Mask: fcInf) || KnownAddend.isKnownNever(Mask: fcNegInf))) |
| 1052 | Known.knownNot(RuleOut: fcNan); |
| 1053 | |
| 1054 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 1055 | } |
| 1056 | |
| 1057 | KnownFPClass KnownFPClass::fmuladd_square(const KnownFPClass &KnownSquared, |
| 1058 | const KnownFPClass &KnownAddend, |
| 1059 | DenormalMode Mode) { |
| 1060 | KnownFPClass FMA = fma_square(KnownSquared_: KnownSquared, KnownAddend_: KnownAddend, Mode); |
| 1061 | KnownFPClass MulAdd = fadd(KnownLHS_: square(KnownSrc_: KnownSquared, Mode), KnownRHS_: KnownAddend, |
| 1062 | RM: RoundingMode::NearestTiesToEven, Mode); |
| 1063 | return FMA.intersectWith(RHS: MulAdd); |
| 1064 | } |
| 1065 | |
| 1066 | KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) { |
| 1067 | KnownFPClass Known; |
| 1068 | Known.knownNot(RuleOut: fcNegative); |
| 1069 | |
| 1070 | Known.propagateNonNaN(Src: KnownSrc); |
| 1071 | |
| 1072 | // The following deductions assume that exp10(-1.0) = +0.1 and |
| 1073 | // exp10(+1.0) = +10.0 are both finite normal values. |
| 1074 | |
| 1075 | // Zero can only be produced if x < -1.0. |
| 1076 | if (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegInf)) |
| 1077 | Known.knownNot(RuleOut: fcPosZero); |
| 1078 | |
| 1079 | // Subnormals can only be produced if x < -1.0 and x is finite. |
| 1080 | if (KnownSrc.isKnownNever(Mask: fcNegNormal)) |
| 1081 | Known.knownNot(RuleOut: fcPosSubnormal); |
| 1082 | |
| 1083 | // Infinity can only be produced if x > +1.0. |
| 1084 | if (KnownSrc.isKnownNever(Mask: fcPosNormal | fcPosInf)) |
| 1085 | Known.knownNot(RuleOut: fcPosInf); |
| 1086 | |
| 1087 | return Known; |
| 1088 | } |
| 1089 | |
| 1090 | KnownFPClass KnownFPClass::log(const KnownFPClass &KnownSrc_, DenormalMode Mode, |
| 1091 | bool IsMultiUnitFPType) { |
| 1092 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 1093 | KnownFPClass Known; |
| 1094 | |
| 1095 | // PPCDoubleDouble can represent the exact value of 1.0 + subnormal, and |
| 1096 | // log(1.0 + subnormal) = subnormal. |
| 1097 | if (!IsMultiUnitFPType) |
| 1098 | Known.knownNot(RuleOut: fcNegZero | fcSubnormal); |
| 1099 | |
| 1100 | Known.propagateNonSNaN(Src: KnownSrc); |
| 1101 | |
| 1102 | if (KnownSrc.isKnownNeverPosInfinity()) |
| 1103 | Known.knownNot(RuleOut: fcPosInf); |
| 1104 | |
| 1105 | if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero()) |
| 1106 | Known.knownNot(RuleOut: fcNan); |
| 1107 | |
| 1108 | if (KnownSrc.isKnownNever(Mask: fcZero)) |
| 1109 | Known.knownNot(RuleOut: fcNegInf); |
| 1110 | |
| 1111 | // This only matters for PPCDoubleDouble. |
| 1112 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 1113 | } |
| 1114 | |
| 1115 | KnownFPClass KnownFPClass::sqrt(const KnownFPClass &KnownSrc, |
| 1116 | DenormalMode Mode) { |
| 1117 | KnownFPClass Known; |
| 1118 | Known.knownNot(RuleOut: fcPosSubnormal); |
| 1119 | |
| 1120 | if (KnownSrc.isKnownNeverPosInfinity()) |
| 1121 | Known.knownNot(RuleOut: fcPosInf); |
| 1122 | |
| 1123 | Known.propagateNonSNaN(Src: KnownSrc); |
| 1124 | |
| 1125 | // Any negative value besides -0 returns a nan. |
| 1126 | if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero()) |
| 1127 | Known.knownNot(RuleOut: fcNan); |
| 1128 | |
| 1129 | // The only negative value that can be returned is -0 for -0 inputs. |
| 1130 | Known.knownNot(RuleOut: fcNegInf | fcNegSubnormal | fcNegNormal); |
| 1131 | |
| 1132 | // If the input denormal mode could be PreserveSign, a negative |
| 1133 | // subnormal input could produce a negative zero output. |
| 1134 | if (KnownSrc.isKnownNeverLogicalNegZero(Mode)) |
| 1135 | Known.knownNot(RuleOut: fcNegZero); |
| 1136 | |
| 1137 | return Known; |
| 1138 | } |
| 1139 | |
| 1140 | KnownFPClass KnownFPClass::sin(const KnownFPClass &KnownSrc) { |
| 1141 | KnownFPClass Known; |
| 1142 | |
| 1143 | Known.propagateNonSNaN(Src: KnownSrc); |
| 1144 | |
| 1145 | // Return NaN on infinite inputs. |
| 1146 | Known.knownNot(RuleOut: fcInf); |
| 1147 | if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity()) |
| 1148 | Known.knownNot(RuleOut: fcNan); |
| 1149 | |
| 1150 | return Known; |
| 1151 | } |
| 1152 | |
| 1153 | KnownFPClass KnownFPClass::cos(const KnownFPClass &KnownSrc) { |
| 1154 | return sin(KnownSrc); |
| 1155 | } |
| 1156 | |
| 1157 | KnownFPClass KnownFPClass::tan(const KnownFPClass &KnownSrc) { |
| 1158 | KnownFPClass Known; |
| 1159 | |
| 1160 | // tan never returns Inf (tan(+-Inf) = NaN; tan(finite) = finite). |
| 1161 | Known.knownNot(RuleOut: fcInf); |
| 1162 | |
| 1163 | Known.propagateNonSNaN(Src: KnownSrc); |
| 1164 | |
| 1165 | // NaN propagates. tan(+-Inf) is NaN. |
| 1166 | if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity()) |
| 1167 | Known.knownNot(RuleOut: fcNan); |
| 1168 | |
| 1169 | return Known; |
| 1170 | } |
| 1171 | |
| 1172 | KnownFPClass KnownFPClass::sinh(const KnownFPClass &KnownSrc) { |
| 1173 | KnownFPClass Known; |
| 1174 | |
| 1175 | // sinh is sign-preserving: sinh(x) < 0 iff x < 0. |
| 1176 | if (KnownSrc.isKnownNever(Mask: fcNegative)) |
| 1177 | Known.knownNot(RuleOut: fcNegative); |
| 1178 | |
| 1179 | // A nonzero positive result requires a nonzero positive input. |
| 1180 | // Zero remains possible: sinh(+/-0.0) = +/-0.0, and flushing a subnormal |
| 1181 | // may produce +0.0. |
| 1182 | if (KnownSrc.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf)) |
| 1183 | Known.knownNot(RuleOut: fcPosSubnormal | fcPosNormal | fcPosInf); |
| 1184 | |
| 1185 | Known.propagateNonNaN(Src: KnownSrc); |
| 1186 | |
| 1187 | return Known; |
| 1188 | } |
| 1189 | |
| 1190 | KnownFPClass KnownFPClass::cosh(const KnownFPClass &KnownSrc) { |
| 1191 | KnownFPClass Known; |
| 1192 | |
| 1193 | // cosh(x) >= 1 for all real x; cosh(+-Inf) = +Inf. Never negative, |
| 1194 | // zero, or subnormal. |
| 1195 | Known.knownNot(RuleOut: fcNegative | fcZero | fcSubnormal); |
| 1196 | |
| 1197 | Known.propagateNonNaN(Src: KnownSrc); |
| 1198 | |
| 1199 | return Known; |
| 1200 | } |
| 1201 | |
| 1202 | KnownFPClass KnownFPClass::tanh(const KnownFPClass &KnownSrc) { |
| 1203 | KnownFPClass Known; |
| 1204 | |
| 1205 | // tanh is bounded to (-1, 1), never Inf. |
| 1206 | Known.knownNot(RuleOut: fcInf); |
| 1207 | |
| 1208 | // tanh is sign-preserving: tanh(x) < 0 iff x < 0. |
| 1209 | if (KnownSrc.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf)) { |
| 1210 | Known.knownNot(RuleOut: fcNegSubnormal | fcNegNormal); |
| 1211 | |
| 1212 | // -0.0 is preserved: tanh(-0.0) = -0.0. |
| 1213 | if (KnownSrc.isKnownNever(Mask: fcNegZero)) |
| 1214 | Known.knownNot(RuleOut: fcNegZero); |
| 1215 | } |
| 1216 | |
| 1217 | // Likewise, tanh(x) > 0 iff x > 0. Zero is not ruled out: tanh(+-0.0) = |
| 1218 | // +-0.0, and a flushed subnormal may become +0.0. |
| 1219 | if (KnownSrc.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf)) |
| 1220 | Known.knownNot(RuleOut: fcPosSubnormal | fcPosNormal); |
| 1221 | |
| 1222 | Known.propagateNonNaN(Src: KnownSrc); |
| 1223 | |
| 1224 | return Known; |
| 1225 | } |
| 1226 | |
| 1227 | KnownFPClass KnownFPClass::asin(const KnownFPClass &KnownSrc) { |
| 1228 | KnownFPClass Known; |
| 1229 | |
| 1230 | // asin is bounded to [-pi/2, pi/2], never Inf. |
| 1231 | Known.knownNot(RuleOut: fcInf); |
| 1232 | |
| 1233 | Known.propagateNonSNaN(Src: KnownSrc); |
| 1234 | |
| 1235 | // asin is sign-preserving for finite arguments. |
| 1236 | if (KnownSrc.isKnownNever(Mask: fcNegFinite)) |
| 1237 | Known.knownNot(RuleOut: fcNegFinite); |
| 1238 | |
| 1239 | if (KnownSrc.isKnownNever(Mask: fcPosSubnormal | fcPosNormal)) |
| 1240 | Known.knownNot(RuleOut: fcPosSubnormal | fcPosNormal); |
| 1241 | |
| 1242 | // NaN propagates. asin(x) is also NaN for |x| > 1, so we cannot rule |
| 1243 | // out NaN without knowing the source is in [-1, 1]. |
| 1244 | return Known; |
| 1245 | } |
| 1246 | |
| 1247 | KnownFPClass KnownFPClass::acos(const KnownFPClass &KnownSrc) { |
| 1248 | KnownFPClass Known; |
| 1249 | |
| 1250 | // acos(x) is bounded to [0, pi] for -1 <= x <= 1, and is never negative, |
| 1251 | // infinite, or subnormal. The smallest non-zero value occurs when x is |
| 1252 | // close to 1.0, where acos(x) can be approximated by sqrt(2 * (1 - x)). |
| 1253 | // Since sqrt cannot produce a subnormal result, we can conclude that |
| 1254 | // acos(x) will also never produce a subnormal result. |
| 1255 | Known.knownNot(RuleOut: fcNegative | fcInf | fcSubnormal); |
| 1256 | |
| 1257 | // acos(x) == +0.0 iff x == +1.0 |
| 1258 | if (KnownSrc.isKnownNever(Mask: fcPosNormal)) |
| 1259 | Known.knownNot(RuleOut: fcZero); |
| 1260 | |
| 1261 | Known.propagateNonSNaN(Src: KnownSrc); |
| 1262 | |
| 1263 | // NaN propagates. acos(x) is also NaN for |x| > 1, so we cannot rule |
| 1264 | // out NaN without knowing the source is in [-1, 1]. |
| 1265 | return Known; |
| 1266 | } |
| 1267 | |
| 1268 | KnownFPClass KnownFPClass::atan(const KnownFPClass &KnownSrc) { |
| 1269 | KnownFPClass Known; |
| 1270 | |
| 1271 | // atan is bounded to (-pi/2, pi/2), never Inf. atan(+-Inf) = +-pi/2 (finite). |
| 1272 | Known.knownNot(RuleOut: fcInf); |
| 1273 | |
| 1274 | // Ruling out Subnormal/Normal from the result requires the |
| 1275 | // input to never be finite-nonzero and never inf, since |
| 1276 | // atan(+-inf) = +-pi/2 |
| 1277 | if (KnownSrc.isKnownNever(Mask: fcNegSubnormal | fcNegNormal | fcNegInf)) |
| 1278 | Known.knownNot(RuleOut: fcNegSubnormal | fcNegNormal); |
| 1279 | |
| 1280 | if (KnownSrc.isKnownNever(Mask: fcPosSubnormal | fcPosNormal | fcPosInf)) |
| 1281 | Known.knownNot(RuleOut: fcPosSubnormal | fcPosNormal); |
| 1282 | |
| 1283 | // atan(-0.0) = -0.0. Negative finite inputs may also produce -0.0 |
| 1284 | // if the result rounds to a negative subnormal that is flushed to zero. |
| 1285 | if (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegSubnormal | fcNegZero)) |
| 1286 | Known.knownNot(RuleOut: fcNegZero); |
| 1287 | |
| 1288 | Known.propagateNonNaN(Src: KnownSrc); |
| 1289 | |
| 1290 | return Known; |
| 1291 | } |
| 1292 | |
| 1293 | KnownFPClass KnownFPClass::atan2(const KnownFPClass &KnownY_, |
| 1294 | const KnownFPClass &KnownX_, |
| 1295 | DenormalMode Mode) { |
| 1296 | KnownFPClass KnownY = applyInputDenormalMode(KnownSrc: KnownY_, Mode); |
| 1297 | KnownFPClass KnownX = applyInputDenormalMode(KnownSrc: KnownX_, Mode); |
| 1298 | KnownFPClass Known; |
| 1299 | |
| 1300 | // Even though these deductions are correct, we are ignoring the following |
| 1301 | // potentially erroneous cases: |
| 1302 | // * atan2(y, inf) is not subnormal |
| 1303 | // * atan2(inf, x) is not zero or subnormal |
| 1304 | |
| 1305 | // atan2 result is in (-pi, pi], never Inf. |
| 1306 | Known.knownNot(RuleOut: fcInf); |
| 1307 | |
| 1308 | Known.propagateNonNaN(LHS: KnownY, RHS: KnownX); |
| 1309 | |
| 1310 | // If x <= -0.0, then |atan2(y, x)| >= pi/2 |
| 1311 | if (KnownX.isKnownNever(Mask: fcPositive)) |
| 1312 | Known.knownNot(RuleOut: fcZero | fcSubnormal); |
| 1313 | |
| 1314 | // If y >= +0.0, then atan2(y, x) >= +0.0 |
| 1315 | if (KnownY.isKnownNever(Mask: fcNegative)) |
| 1316 | Known.knownNot(RuleOut: fcNegative); |
| 1317 | |
| 1318 | // If y <= -0.0, then atan2(y, x) <= -0.0 |
| 1319 | if (KnownY.isKnownNever(Mask: fcPositive)) |
| 1320 | Known.knownNot(RuleOut: fcPositive); |
| 1321 | |
| 1322 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 1323 | } |
| 1324 | |
| 1325 | static KnownFPClass fpconvert(const KnownFPClass &KnownSrc_, |
| 1326 | const fltSemantics &DstSem, |
| 1327 | const fltSemantics &SrcSem, DenormalMode Mode) { |
| 1328 | KnownFPClass Known; |
| 1329 | APFloatBase::Semantics DstType = APFloatBase::SemanticsToEnum(Sem: DstSem); |
| 1330 | APFloatBase::Semantics SrcType = APFloatBase::SemanticsToEnum(Sem: SrcSem); |
| 1331 | auto IsSupported = [](APFloatBase::Semantics SemType) -> bool { |
| 1332 | switch (SemType) { |
| 1333 | case APFloatBase::S_IEEEhalf: |
| 1334 | case APFloatBase::S_BFloat: |
| 1335 | case APFloatBase::S_IEEEsingle: |
| 1336 | case APFloatBase::S_IEEEdouble: |
| 1337 | case APFloatBase::S_IEEEquad: |
| 1338 | case APFloatBase::S_x87DoubleExtended: |
| 1339 | return true; |
| 1340 | default: |
| 1341 | return false; |
| 1342 | } |
| 1343 | }; |
| 1344 | |
| 1345 | // Return unknown for types we have not validated. |
| 1346 | // TODO: Add support for lossless casts to PPCDoubleDouble. |
| 1347 | if (!IsSupported(SrcType) || !IsSupported(DstType)) |
| 1348 | return Known; |
| 1349 | |
| 1350 | KnownFPClass KnownSrc = KnownFPClass::applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 1351 | |
| 1352 | // TODO: Until we pass the rounding mode into fpconvert, the rounding mode is |
| 1353 | // assumed to be dynamic. This includes potential future rounding modes such |
| 1354 | // as roundAwayZero (not the same as roundNearestTiesAway) and roundToOdd |
| 1355 | // (jamming). |
| 1356 | |
| 1357 | Known.propagateNonNaN(Src: KnownSrc); |
| 1358 | |
| 1359 | const bool NormalSrcIsFiniteDst = DstSem.maxExponent > SrcSem.maxExponent || |
| 1360 | (DstSem.maxExponent == SrcSem.maxExponent && |
| 1361 | DstSem.precision >= SrcSem.precision); |
| 1362 | |
| 1363 | // True if the dst can represent all non-zero finite values of src. |
| 1364 | const bool NonZeroFiniteSrcIsNonZeroInDst = |
| 1365 | DstSem.minExponent <= SrcSem.minExponent && |
| 1366 | DstSem.precision >= SrcSem.precision; |
| 1367 | |
| 1368 | // For example: f32 = bf16, f128 = f80 |
| 1369 | const bool NormalIsNormalAndSubnormalIsSubnormal = |
| 1370 | DstSem.minExponent == SrcSem.minExponent && |
| 1371 | DstSem.maxExponent >= SrcSem.maxExponent && |
| 1372 | DstSem.precision >= SrcSem.precision; |
| 1373 | |
| 1374 | // Rule out infinity. |
| 1375 | // We are assuming that subnormal values have a magnitude less than 1.0. |
| 1376 | if (KnownSrc.isKnownNever(Mask: fcPosInf) && |
| 1377 | (KnownSrc.isKnownNever(Mask: fcPosNormal) || NormalSrcIsFiniteDst)) |
| 1378 | Known.knownNot(RuleOut: fcPosInf); |
| 1379 | if (KnownSrc.isKnownNever(Mask: fcNegInf) && |
| 1380 | (KnownSrc.isKnownNever(Mask: fcNegNormal) || NormalSrcIsFiniteDst)) |
| 1381 | Known.knownNot(RuleOut: fcNegInf); |
| 1382 | |
| 1383 | // Rule out non-zero finite. |
| 1384 | if (NormalIsNormalAndSubnormalIsSubnormal) { |
| 1385 | // Normal never becomes subnormal, and vice-versa. |
| 1386 | if (KnownSrc.isKnownNever(Mask: fcPosNormal)) |
| 1387 | Known.knownNot(RuleOut: fcPosNormal); |
| 1388 | if (KnownSrc.isKnownNever(Mask: fcNegNormal)) |
| 1389 | Known.knownNot(RuleOut: fcNegNormal); |
| 1390 | if (KnownSrc.isKnownNever(Mask: fcPosSubnormal)) |
| 1391 | Known.knownNot(RuleOut: fcPosSubnormal); |
| 1392 | if (KnownSrc.isKnownNever(Mask: fcNegSubnormal)) |
| 1393 | Known.knownNot(RuleOut: fcNegSubnormal); |
| 1394 | } else { |
| 1395 | if (KnownSrc.isKnownNever(Mask: fcPosNormal | fcPosSubnormal)) |
| 1396 | Known.knownNot(RuleOut: fcPosNormal | fcPosSubnormal); |
| 1397 | if (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegSubnormal)) |
| 1398 | Known.knownNot(RuleOut: fcNegNormal | fcNegSubnormal); |
| 1399 | } |
| 1400 | |
| 1401 | // Source subnormals cannot round to destination normals when the |
| 1402 | // destination's minimum normal exponent is larger. |
| 1403 | if (DstSem.minExponent > SrcSem.minExponent) { |
| 1404 | if (KnownSrc.isKnownNever(Mask: fcPosNormal)) |
| 1405 | Known.knownNot(RuleOut: fcPosNormal); |
| 1406 | if (KnownSrc.isKnownNever(Mask: fcNegNormal)) |
| 1407 | Known.knownNot(RuleOut: fcNegNormal); |
| 1408 | } |
| 1409 | |
| 1410 | // Rule out subnormal. |
| 1411 | if (APFloat::isRepresentableAsNormalIn(Src: SrcSem, Dst: DstSem)) |
| 1412 | Known.knownNot(RuleOut: fcSubnormal); |
| 1413 | |
| 1414 | // Rule out positive zero. |
| 1415 | if (KnownSrc.isKnownNever(Mask: fcPosZero) && |
| 1416 | (KnownSrc.isKnownNever(Mask: fcPosNormal | fcPosSubnormal) || |
| 1417 | NonZeroFiniteSrcIsNonZeroInDst)) |
| 1418 | Known.knownNot(RuleOut: fcPosZero); |
| 1419 | |
| 1420 | // Rule out negative zero. |
| 1421 | if (KnownSrc.isKnownNever(Mask: fcNegZero) && |
| 1422 | (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegSubnormal) || |
| 1423 | NonZeroFiniteSrcIsNonZeroInDst)) |
| 1424 | Known.knownNot(RuleOut: fcNegZero); |
| 1425 | |
| 1426 | return KnownFPClass::applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 1427 | } |
| 1428 | |
| 1429 | // Determine what the input and output denormal modes are for fpconvert from |
| 1430 | // the denormal modes for the dst and src types. |
| 1431 | // |
| 1432 | // TODO: We are currently conservative, and assume DenormalMode::getDynamic() if |
| 1433 | // the dst or src denormal mode is not IEEE. |
| 1434 | static DenormalMode getConvertDenormalMode(DenormalMode DstMode, |
| 1435 | DenormalMode SrcMode) { |
| 1436 | if (DstMode == DenormalMode::getIEEE() && SrcMode == DenormalMode::getIEEE()) |
| 1437 | return DenormalMode::getIEEE(); |
| 1438 | return DenormalMode::getDynamic(); |
| 1439 | } |
| 1440 | |
| 1441 | KnownFPClass KnownFPClass::fpext(const KnownFPClass &KnownSrc, |
| 1442 | const fltSemantics &DstTy, |
| 1443 | const fltSemantics &SrcTy, |
| 1444 | DenormalMode DstMode, DenormalMode SrcMode) { |
| 1445 | // Note that fpext may round for some bizarre type combinations. |
| 1446 | // Such as ieee_binary256 = fpext(PPCDoubleDouble) if we add support for |
| 1447 | // ieee_binary256 in the future. PPCDoubleDouble can represent values such as |
| 1448 | // DBL_MAX + DBL_TRUE_MIN which would normally require 2098 bits of precision |
| 1449 | // to represent. |
| 1450 | return fpconvert(KnownSrc_: KnownSrc, DstSem: DstTy, SrcSem: SrcTy, |
| 1451 | Mode: getConvertDenormalMode(DstMode, SrcMode)); |
| 1452 | } |
| 1453 | |
| 1454 | KnownFPClass KnownFPClass::fptrunc(const KnownFPClass &KnownSrc, |
| 1455 | const fltSemantics &DstTy, |
| 1456 | const fltSemantics &SrcTy, |
| 1457 | DenormalMode DstMode, DenormalMode SrcMode) { |
| 1458 | // TODO: pass rounding mode from fptrunc_round |
| 1459 | return fpconvert(KnownSrc_: KnownSrc, DstSem: DstTy, SrcSem: SrcTy, |
| 1460 | Mode: getConvertDenormalMode(DstMode, SrcMode)); |
| 1461 | } |
| 1462 | |
| 1463 | KnownFPClass KnownFPClass::roundToIntegral(const KnownFPClass &KnownSrc_, |
| 1464 | bool MayBeMultiUnitFPType, |
| 1465 | DenormalMode Mode) { |
| 1466 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 1467 | |
| 1468 | KnownFPClass Known; |
| 1469 | |
| 1470 | // Integer results cannot be subnormal. |
| 1471 | Known.knownNot(RuleOut: fcSubnormal); |
| 1472 | |
| 1473 | Known.propagateNonNaN(Src: KnownSrc); |
| 1474 | |
| 1475 | // Pass through infinities, except PPCDoubleDouble is a special case for |
| 1476 | // intrinsics. While it is possible to refine +-inf results for |
| 1477 | // PPCDoubleDouble (roundToIntegral(positive) cannot be -inf for example), it |
| 1478 | // is not worth implementing for a rarely used type. |
| 1479 | if (!MayBeMultiUnitFPType) { |
| 1480 | if (KnownSrc.isKnownNeverPosInfinity()) |
| 1481 | Known.knownNot(RuleOut: fcPosInf); |
| 1482 | if (KnownSrc.isKnownNeverNegInfinity()) |
| 1483 | Known.knownNot(RuleOut: fcNegInf); |
| 1484 | } |
| 1485 | |
| 1486 | if (KnownSrc.isKnownNever(Mask: fcPosNormal | fcPosSubnormal)) |
| 1487 | Known.knownNot(RuleOut: fcPosNormal); |
| 1488 | |
| 1489 | if (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegSubnormal)) |
| 1490 | Known.knownNot(RuleOut: fcNegNormal); |
| 1491 | |
| 1492 | // Negative round ups towards zero produce negative zero. |
| 1493 | if (KnownSrc.isKnownNever(Mask: fcNegFinite)) |
| 1494 | Known.knownNot(RuleOut: fcNegZero); |
| 1495 | |
| 1496 | if (KnownSrc.isKnownNever(Mask: fcPosFinite)) |
| 1497 | Known.knownNot(RuleOut: fcPosZero); |
| 1498 | |
| 1499 | // Output denormal mode does not matter here. |
| 1500 | return Known; |
| 1501 | } |
| 1502 | |
| 1503 | KnownFPClass KnownFPClass::trunc(const KnownFPClass &KnownSrc_, |
| 1504 | DenormalMode Mode) { |
| 1505 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 1506 | KnownFPClass Known; |
| 1507 | Known.knownNot(RuleOut: fcSubnormal); |
| 1508 | Known.propagateNonNaN(Src: KnownSrc); |
| 1509 | |
| 1510 | // PPCDoubleDouble will not overflow for trunc as the magnitude never |
| 1511 | // increases. |
| 1512 | |
| 1513 | // trunc(x) == -inf iff x == -inf |
| 1514 | if (KnownSrc.isKnownNeverNegInfinity()) |
| 1515 | Known.knownNot(RuleOut: fcNegInf); |
| 1516 | |
| 1517 | // trunc(x) <= -1.0 iff x <= -1.0 |
| 1518 | if (KnownSrc.isKnownNever(Mask: fcNegNormal)) |
| 1519 | Known.knownNot(RuleOut: fcNegNormal); |
| 1520 | |
| 1521 | // trunc(x) == -0.0 iff -1.0 < x <= -0.0 |
| 1522 | if (KnownSrc.isKnownNever(Mask: fcNegFinite)) |
| 1523 | Known.knownNot(RuleOut: fcNegZero); |
| 1524 | |
| 1525 | // trunc(x) == +0.0 iff +0.0 <= x < +1.0 |
| 1526 | if (KnownSrc.isKnownNever(Mask: fcPosFinite)) |
| 1527 | Known.knownNot(RuleOut: fcPosZero); |
| 1528 | |
| 1529 | // trunc(x) >= +1.0 iff x >= +1.0 |
| 1530 | if (KnownSrc.isKnownNever(Mask: fcPosNormal)) |
| 1531 | Known.knownNot(RuleOut: fcPosNormal); |
| 1532 | |
| 1533 | // trunc(x) == +inf iff x == +inf |
| 1534 | if (KnownSrc.isKnownNeverPosInfinity()) |
| 1535 | Known.knownNot(RuleOut: fcPosInf); |
| 1536 | |
| 1537 | return Known; |
| 1538 | } |
| 1539 | |
| 1540 | KnownFPClass KnownFPClass::floor(const KnownFPClass &KnownSrc_, |
| 1541 | bool IsMultiUnitFPType, DenormalMode Mode) { |
| 1542 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 1543 | KnownFPClass Known; |
| 1544 | Known.knownNot(RuleOut: fcSubnormal); |
| 1545 | Known.propagateNonNaN(Src: KnownSrc); |
| 1546 | |
| 1547 | // PPCDoubleDouble can overflow to negative infinity. See roundToIntegral for |
| 1548 | // rationale. |
| 1549 | if (!IsMultiUnitFPType && KnownSrc.isKnownNeverNegInfinity()) |
| 1550 | Known.knownNot(RuleOut: fcNegInf); |
| 1551 | |
| 1552 | // floor(x) <= -1.0 iff x < -0.0 |
| 1553 | if (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegSubnormal)) |
| 1554 | Known.knownNot(RuleOut: fcNegNormal); |
| 1555 | |
| 1556 | // floor(x) == -0.0 iff x == -0.0 |
| 1557 | if (KnownSrc.isKnownNever(Mask: fcNegZero)) |
| 1558 | Known.knownNot(RuleOut: fcNegZero); |
| 1559 | |
| 1560 | // floor(x) == +0.0 iff +0.0 <= x < +1.0 |
| 1561 | if (KnownSrc.isKnownNever(Mask: fcPosFinite)) |
| 1562 | Known.knownNot(RuleOut: fcPosZero); |
| 1563 | |
| 1564 | // floor(x) >= +1.0 iff x >= +1.0 |
| 1565 | if (KnownSrc.isKnownNever(Mask: fcPosNormal)) |
| 1566 | Known.knownNot(RuleOut: fcPosNormal); |
| 1567 | |
| 1568 | // floor(x) == +inf iff x == +inf |
| 1569 | if (KnownSrc.isKnownNeverPosInfinity()) |
| 1570 | Known.knownNot(RuleOut: fcPosInf); |
| 1571 | |
| 1572 | return Known; |
| 1573 | } |
| 1574 | |
| 1575 | KnownFPClass KnownFPClass::ceil(const KnownFPClass &KnownSrc_, |
| 1576 | bool IsMultiUnitFPType, DenormalMode Mode) { |
| 1577 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 1578 | KnownFPClass Known; |
| 1579 | Known.knownNot(RuleOut: fcSubnormal); |
| 1580 | Known.propagateNonNaN(Src: KnownSrc); |
| 1581 | |
| 1582 | // ceil(x) == -inf iff x == -inf |
| 1583 | if (KnownSrc.isKnownNeverNegInfinity()) |
| 1584 | Known.knownNot(RuleOut: fcNegInf); |
| 1585 | |
| 1586 | // ceil(x) <= -1.0 iff x <= -1.0 |
| 1587 | if (KnownSrc.isKnownNever(Mask: fcNegNormal)) |
| 1588 | Known.knownNot(RuleOut: fcNegNormal); |
| 1589 | |
| 1590 | // ceil(x) == -0.0 iff -1.0 < x <= -0.0 |
| 1591 | if (KnownSrc.isKnownNever(Mask: fcNegFinite)) |
| 1592 | Known.knownNot(RuleOut: fcNegZero); |
| 1593 | |
| 1594 | // ceil(x) == +0.0 iff x == +0.0 |
| 1595 | if (KnownSrc.isKnownNever(Mask: fcPosZero)) |
| 1596 | Known.knownNot(RuleOut: fcPosZero); |
| 1597 | |
| 1598 | // ceil(x) >= +1.0 iff x > +0.0 |
| 1599 | if (KnownSrc.isKnownNever(Mask: fcPosNormal | fcPosSubnormal)) |
| 1600 | Known.knownNot(RuleOut: fcPosNormal); |
| 1601 | |
| 1602 | // PPCDoubleDouble can overflow to positive infinity. See roundToIntegral for |
| 1603 | // rationale. |
| 1604 | if (!IsMultiUnitFPType && KnownSrc.isKnownNeverPosInfinity()) |
| 1605 | Known.knownNot(RuleOut: fcPosInf); |
| 1606 | |
| 1607 | return Known; |
| 1608 | } |
| 1609 | |
| 1610 | KnownFPClass KnownFPClass::frexp_mant(const KnownFPClass &KnownSrc_, |
| 1611 | DenormalMode Mode) { |
| 1612 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 1613 | KnownFPClass Known; |
| 1614 | |
| 1615 | Known.knownNot(RuleOut: fcSubnormal); |
| 1616 | |
| 1617 | Known.propagateNonNaN(Src: KnownSrc); |
| 1618 | |
| 1619 | if (KnownSrc.isKnownNeverPosInfinity()) |
| 1620 | Known.knownNot(RuleOut: fcPosInf); |
| 1621 | if (KnownSrc.isKnownNeverNegInfinity()) |
| 1622 | Known.knownNot(RuleOut: fcNegInf); |
| 1623 | |
| 1624 | if (KnownSrc.isKnownNever(Mask: fcPosZero)) |
| 1625 | Known.knownNot(RuleOut: fcPosZero); |
| 1626 | if (KnownSrc.isKnownNever(Mask: fcNegZero)) |
| 1627 | Known.knownNot(RuleOut: fcNegZero); |
| 1628 | |
| 1629 | if (KnownSrc.isKnownNever(Mask: fcPosNormal | fcPosSubnormal)) |
| 1630 | Known.knownNot(RuleOut: fcPosNormal); |
| 1631 | if (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegSubnormal)) |
| 1632 | Known.knownNot(RuleOut: fcNegNormal); |
| 1633 | |
| 1634 | return Known; |
| 1635 | } |
| 1636 | |
| 1637 | KnownFPClass KnownFPClass::ldexp(const KnownFPClass &KnownSrc_, |
| 1638 | const APInt &ConstantRangeExpMin, |
| 1639 | const APInt &ConstantRangeExpMax, |
| 1640 | const fltSemantics &Flt, DenormalMode Mode) { |
| 1641 | KnownFPClass Known; |
| 1642 | |
| 1643 | KnownFPClass KnownSrc = applyInputDenormalMode(KnownSrc: KnownSrc_, Mode); |
| 1644 | |
| 1645 | if (ConstantRangeExpMin.isZero() && ConstantRangeExpMax.isZero()) { |
| 1646 | // ldexp(x, 0) -> x |
| 1647 | // Here we make sure to properly propagate sNaN and denormals. |
| 1648 | Known.setKnownFPClasses(KnownSrc.getKnownFPClasses() | fcNan); |
| 1649 | Known.propagateNonNaN(Src: KnownSrc); |
| 1650 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 1651 | } |
| 1652 | |
| 1653 | Known.propagateNonNaN(Src: KnownSrc); |
| 1654 | |
| 1655 | // Sign is preserved, but underflows may produce zeroes. |
| 1656 | if (KnownSrc.isKnownNever(Mask: fcNegative)) |
| 1657 | Known.knownNot(RuleOut: fcNegative); |
| 1658 | else if (KnownSrc.cannotBeOrderedLessThanZero()) |
| 1659 | Known.knownNot(RuleOut: OrderedLessThanZeroMask); |
| 1660 | |
| 1661 | if (KnownSrc.isKnownNever(Mask: fcPositive)) |
| 1662 | Known.knownNot(RuleOut: fcPositive); |
| 1663 | else if (KnownSrc.cannotBeOrderedGreaterThanZero()) |
| 1664 | Known.knownNot(RuleOut: OrderedGreaterThanZeroMask); |
| 1665 | |
| 1666 | // TODO: determine when it is safe to rule out subnormal for ppcf128. |
| 1667 | if (&Flt != &APFloat::PPCDoubleDouble()) { |
| 1668 | unsigned Precision = APFloat::semanticsPrecision(Flt); |
| 1669 | const int MantissaBits = Precision - 1; |
| 1670 | if (ConstantRangeExpMin.sge(RHS: MantissaBits)) |
| 1671 | Known.knownNot(RuleOut: fcSubnormal); |
| 1672 | } |
| 1673 | |
| 1674 | if (ConstantRangeExpMax.isNonPositive()) { |
| 1675 | // If we know the power is <= 0, can't introduce inf |
| 1676 | if (KnownSrc.isKnownNeverPosInfinity()) |
| 1677 | Known.knownNot(RuleOut: fcPosInf); |
| 1678 | if (KnownSrc.isKnownNeverNegInfinity()) |
| 1679 | Known.knownNot(RuleOut: fcNegInf); |
| 1680 | } else if (ConstantRangeExpMin.isNonNegative()) { |
| 1681 | // If we know the power is >= 0, can't introduce subnormal or zero |
| 1682 | if (KnownSrc.isKnownNeverPosSubnormal()) |
| 1683 | Known.knownNot(RuleOut: fcPosSubnormal); |
| 1684 | if (KnownSrc.isKnownNeverNegSubnormal()) |
| 1685 | Known.knownNot(RuleOut: fcNegSubnormal); |
| 1686 | if (KnownSrc.isKnownNever(Mask: fcPosZero)) |
| 1687 | Known.knownNot(RuleOut: fcPosZero); |
| 1688 | if (KnownSrc.isKnownNever(Mask: fcNegZero)) |
| 1689 | Known.knownNot(RuleOut: fcNegZero); |
| 1690 | } |
| 1691 | |
| 1692 | return applyOutputDenormalMode(KnownSrc: Known, Mode); |
| 1693 | } |
| 1694 | |
| 1695 | KnownFPClass KnownFPClass::ldexp(const KnownFPClass &KnownSrc, |
| 1696 | const KnownBits &ExpBits, |
| 1697 | const fltSemantics &Flt, DenormalMode Mode) { |
| 1698 | return ldexp(KnownSrc_: KnownSrc, ConstantRangeExpMin: ExpBits.getSignedMinValue(), |
| 1699 | ConstantRangeExpMax: ExpBits.getSignedMaxValue(), Flt, Mode); |
| 1700 | } |
| 1701 | |
| 1702 | KnownFPClass KnownFPClass::pow(const KnownFPClass &KnownLHS, |
| 1703 | const KnownFPClass &KnownRHS) { |
| 1704 | KnownFPClass Known; |
| 1705 | |
| 1706 | Known.propagateNonSNaN(LHS: KnownLHS, RHS: KnownRHS); |
| 1707 | |
| 1708 | // pow may return NaN if one of the arguments is NaN. NaN may be produced from |
| 1709 | // a non-zero-finite-negative base and a non-integer exponent. |
| 1710 | if (KnownLHS.isKnownNever(Mask: fcNan | fcNegNormal | fcNegSubnormal) && |
| 1711 | KnownRHS.isKnownNeverNaN()) |
| 1712 | Known.knownNot(RuleOut: fcNan); |
| 1713 | |
| 1714 | // We could rule out negative and subnormal results when exponent is known to |
| 1715 | // never be a normal value, but having either argument being known to never be |
| 1716 | // normal is unlikely and not worth considering. |
| 1717 | |
| 1718 | // Only a negative base raised to an odd power returns a negative value. |
| 1719 | if (KnownLHS.isKnownNever(Mask: fcNegative)) { |
| 1720 | Known.knownNot(RuleOut: fcNegative); |
| 1721 | } else if (KnownLHS.isKnownNever(Mask: fcNegNormal | fcNegSubnormal)) { |
| 1722 | Known.knownNot(RuleOut: fcNegNormal | fcNegSubnormal); |
| 1723 | // See if we can also rule out -0.0 or -inf. |
| 1724 | // Here at least one of -0.0 or -inf is a possible base. |
| 1725 | |
| 1726 | // pow(-0.0, odd-positive) = -0.0 |
| 1727 | // pow(-inf, odd-negative) = -0.0 |
| 1728 | if ((KnownLHS.isKnownNever(Mask: fcNegZero) || |
| 1729 | KnownRHS.isKnownNever(Mask: fcPosNormal)) && |
| 1730 | (KnownLHS.isKnownNever(Mask: fcNegInf) || KnownRHS.isKnownNever(Mask: fcNegNormal))) |
| 1731 | Known.knownNot(RuleOut: fcNegZero); |
| 1732 | |
| 1733 | // pow(-0.0, odd-negative) = -inf |
| 1734 | // pow(-inf, odd-positive) = -inf |
| 1735 | if ((KnownLHS.isKnownNever(Mask: fcNegZero) || |
| 1736 | KnownRHS.isKnownNever(Mask: fcNegNormal)) && |
| 1737 | (KnownLHS.isKnownNever(Mask: fcNegInf) || KnownRHS.isKnownNever(Mask: fcPosNormal))) |
| 1738 | Known.knownNot(RuleOut: fcNegInf); |
| 1739 | } |
| 1740 | |
| 1741 | return Known; |
| 1742 | } |
| 1743 | |
| 1744 | KnownFPClass KnownFPClass::powi(const KnownFPClass &KnownSrc, |
| 1745 | const KnownBits &ExponentKnownBits) { |
| 1746 | KnownFPClass Known; |
| 1747 | Known.propagateNonNaN(Src: KnownSrc); |
| 1748 | |
| 1749 | if (ExponentKnownBits.isZero()) { |
| 1750 | // powi(QNaN, 0) returns 1.0, and powi(SNaN, 0) may non-deterministically |
| 1751 | // return 1.0 or a NaN. |
| 1752 | if (KnownSrc.isKnownNever(Mask: fcSNan)) { |
| 1753 | Known.knownNot(RuleOut: ~fcPosNormal); |
| 1754 | return Known; |
| 1755 | } |
| 1756 | |
| 1757 | Known.knownNot(RuleOut: ~(fcPosNormal | fcNan)); |
| 1758 | return Known; |
| 1759 | } |
| 1760 | |
| 1761 | // Given that exp is an integer, here are the |
| 1762 | // ways that powi can return a negative value: |
| 1763 | // |
| 1764 | // powi(x, exp) --> negative if exp is odd and x is negative. |
| 1765 | // powi(-0, exp) --> -inf if exp is negative odd. |
| 1766 | // powi(-0, exp) --> -0 if exp is positive odd. |
| 1767 | // powi(-inf, exp) --> -0 if exp is negative odd. |
| 1768 | // powi(-inf, exp) --> -inf if exp is positive odd. |
| 1769 | if (KnownSrc.isKnownNever(Mask: fcNegative) || ExponentKnownBits.isEven()) { |
| 1770 | Known.knownNot(RuleOut: fcNegative); |
| 1771 | } else if (KnownSrc.isKnownNever(Mask: fcNegNormal | fcNegSubnormal)) { |
| 1772 | Known.knownNot(RuleOut: fcNegNormal | fcNegSubnormal); |
| 1773 | // See if we can also rule out -0.0 or -inf. |
| 1774 | // Here at least one of -0.0 or -inf is a possible base. |
| 1775 | |
| 1776 | // We already know that ExponentKnownBits.isEven() is false here. |
| 1777 | const bool IsKnownNeverOddPositive = ExponentKnownBits.isNegative(); |
| 1778 | const bool IsKnownNeverOddNegative = ExponentKnownBits.isNonNegative(); |
| 1779 | |
| 1780 | // powi(-0.0, odd-positive) = -0.0 |
| 1781 | // powi(-inf, odd-negative) = -0.0 |
| 1782 | if ((KnownSrc.isKnownNever(Mask: fcNegZero) || IsKnownNeverOddPositive) && |
| 1783 | (KnownSrc.isKnownNever(Mask: fcNegInf) || IsKnownNeverOddNegative)) |
| 1784 | Known.knownNot(RuleOut: fcNegZero); |
| 1785 | |
| 1786 | // powi(-0.0, odd-negative) = -inf |
| 1787 | // powi(-inf, odd-positive) = -inf |
| 1788 | if ((KnownSrc.isKnownNever(Mask: fcNegZero) || IsKnownNeverOddNegative) && |
| 1789 | (KnownSrc.isKnownNever(Mask: fcNegInf) || IsKnownNeverOddPositive)) |
| 1790 | Known.knownNot(RuleOut: fcNegInf); |
| 1791 | } |
| 1792 | |
| 1793 | // powi(x, exp) --> inf |
| 1794 | // when: |
| 1795 | // * powi(inf, exp), exp > 0 |
| 1796 | // * powi(+/-0, exp), exp < 0 |
| 1797 | // * powi(finite, exp), |exp| > 1 |
| 1798 | // * powi(subnormal, -1) |
| 1799 | // TODO: |
| 1800 | // 1. This simple all or nothing approach. We can do better |
| 1801 | // and cover sign/parity and exp > 1 vs exp < -1 separately. |
| 1802 | // 2. powi(0/nan, exp), exp > 0 can be refinable |
| 1803 | // to fcNan | fcZero | fcPosNormal. |
| 1804 | { |
| 1805 | APInt MinExp = ExponentKnownBits.getSignedMinValue(); |
| 1806 | APInt MaxExp = ExponentKnownBits.getSignedMaxValue(); |
| 1807 | |
| 1808 | // powi(inf, exp), exp > 0 |
| 1809 | bool MayInfSrc = |
| 1810 | !KnownSrc.isKnownNever(Mask: fcInf) && MaxExp.isStrictlyPositive(); |
| 1811 | |
| 1812 | // powi(+/-0, exp), exp < 0 |
| 1813 | bool MayDivByZero = !KnownSrc.isKnownNever(Mask: fcZero) && MinExp.isNegative(); |
| 1814 | |
| 1815 | // powi(finite, exp), |exp| > 1 |
| 1816 | bool MayFinite = !KnownSrc.isKnownNever(Mask: fcNormal | fcSubnormal); |
| 1817 | bool MayAbsExpGT1 = MinExp.slt(RHS: -1) || MaxExp.sgt(RHS: 1); |
| 1818 | bool MayFiniteOverflow = MayFinite && MayAbsExpGT1; |
| 1819 | |
| 1820 | // powi(subnormal, -1) |
| 1821 | bool MayBeNegOne = ExponentKnownBits.Zero.isZero(); |
| 1822 | bool MaySubnormInv = !KnownSrc.isKnownNever(Mask: fcSubnormal) && MayBeNegOne; |
| 1823 | |
| 1824 | if (!MayInfSrc && !MayDivByZero && !MayFiniteOverflow && !MaySubnormInv) |
| 1825 | Known.knownNot(RuleOut: fcInf); |
| 1826 | } |
| 1827 | |
| 1828 | return Known; |
| 1829 | } |
| 1830 | |