1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
//===--- Floating.h - Types for the constexpr VM ----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines the VM types and helpers operating on types.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_AST_INTERP_FLOATING_H
#define LLVM_CLANG_AST_INTERP_FLOATING_H
#include "Primitives.h"
#include "clang/AST/APValue.h"
#include "llvm/ADT/APFloat.h"
namespace clang {
namespace interp {
using APFloat = llvm::APFloat;
using APSInt = llvm::APSInt;
class Floating final {
private:
// The underlying value storage.
APFloat F;
public:
/// Zero-initializes a Floating.
Floating() : F(0.0f) {}
Floating(const APFloat &F) : F(F) {}
// Static constructors for special floating point values.
static Floating getInf(const llvm::fltSemantics &Sem) {
return Floating(APFloat::getInf(Sem));
}
const APFloat &getAPFloat() const { return F; }
bool operator<(Floating RHS) const { return F < RHS.F; }
bool operator>(Floating RHS) const { return F > RHS.F; }
bool operator<=(Floating RHS) const { return F <= RHS.F; }
bool operator>=(Floating RHS) const { return F >= RHS.F; }
bool operator==(Floating RHS) const { return F == RHS.F; }
bool operator!=(Floating RHS) const { return F != RHS.F; }
Floating operator-() const { return Floating(-F); }
APFloat::opStatus convertToInteger(APSInt &Result) const {
bool IsExact;
return F.convertToInteger(Result, llvm::APFloat::rmTowardZero, &IsExact);
}
Floating toSemantics(const llvm::fltSemantics *Sem,
llvm::RoundingMode RM) const {
APFloat Copy = F;
bool LosesInfo;
Copy.convert(*Sem, RM, &LosesInfo);
(void)LosesInfo;
return Floating(Copy);
}
/// Convert this Floating to one with the same semantics as \Other.
Floating toSemantics(const Floating &Other, llvm::RoundingMode RM) const {
return toSemantics(&Other.F.getSemantics(), RM);
}
APSInt toAPSInt(unsigned NumBits = 0) const {
return APSInt(F.bitcastToAPInt());
}
APValue toAPValue() const { return APValue(F); }
void print(llvm::raw_ostream &OS) const {
// Can't use APFloat::print() since it appends a newline.
SmallVector<char, 16> Buffer;
F.toString(Buffer);
OS << Buffer;
}
unsigned bitWidth() const { return F.semanticsSizeInBits(F.getSemantics()); }
bool isSigned() const { return true; }
bool isNegative() const { return F.isNegative(); }
bool isPositive() const { return !F.isNegative(); }
bool isZero() const { return F.isZero(); }
bool isNonZero() const { return F.isNonZero(); }
bool isMin() const { return F.isSmallest(); }
bool isMinusOne() const { return F.isExactlyValue(-1.0); }
bool isNan() const { return F.isNaN(); }
bool isFinite() const { return F.isFinite(); }
ComparisonCategoryResult compare(const Floating &RHS) const {
return Compare(F, RHS.F);
}
static APFloat::opStatus fromIntegral(APSInt Val,
const llvm::fltSemantics &Sem,
llvm::RoundingMode RM,
Floating &Result) {
APFloat F = APFloat(Sem);
APFloat::opStatus Status = F.convertFromAPInt(Val, Val.isSigned(), RM);
Result = Floating(F);
return Status;
}
// -------
static APFloat::opStatus add(const Floating &A, const Floating &B,
llvm::RoundingMode RM, Floating *R) {
*R = Floating(A.F);
return R->F.add(B.F, RM);
}
static APFloat::opStatus increment(const Floating &A, llvm::RoundingMode RM,
Floating *R) {
APFloat One(A.F.getSemantics(), 1);
*R = Floating(A.F);
return R->F.add(One, RM);
}
static APFloat::opStatus sub(const Floating &A, const Floating &B,
llvm::RoundingMode RM, Floating *R) {
*R = Floating(A.F);
return R->F.subtract(B.F, RM);
}
static APFloat::opStatus decrement(const Floating &A, llvm::RoundingMode RM,
Floating *R) {
APFloat One(A.F.getSemantics(), 1);
*R = Floating(A.F);
return R->F.subtract(One, RM);
}
static APFloat::opStatus mul(const Floating &A, const Floating &B,
llvm::RoundingMode RM, Floating *R) {
*R = Floating(A.F);
return R->F.multiply(B.F, RM);
}
static APFloat::opStatus div(const Floating &A, const Floating &B,
llvm::RoundingMode RM, Floating *R) {
*R = Floating(A.F);
return R->F.divide(B.F, RM);
}
static bool neg(const Floating &A, Floating *R) {
*R = -A;
return false;
}
};
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Floating F);
Floating getSwappedBytes(Floating F);
} // namespace interp
} // namespace clang
#endif
|