diff --git a/clang/include/clang/AST/APValue.h b/clang/include/clang/AST/APValue.h --- a/clang/include/clang/AST/APValue.h +++ b/clang/include/clang/AST/APValue.h @@ -13,8 +13,8 @@ #ifndef LLVM_CLANG_AST_APVALUE_H #define LLVM_CLANG_AST_APVALUE_H -#include "clang/Basic/FixedPoint.h" #include "clang/Basic/LLVM.h" +#include "llvm/ADT/APFixedPoint.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/APSInt.h" #include "llvm/ADT/PointerIntPair.h" @@ -32,6 +32,7 @@ struct PrintingPolicy; class Type; class ValueDecl; + class QualType; /// Symbolic representation of typeid(T) for some type T. class TypeInfoLValue { @@ -113,6 +114,7 @@ /// [APSInt] [APFloat], [Complex APSInt] [Complex APFloat], [Expr + Offset], /// [Vector: N * APValue], [Array: N * APValue] class APValue { + typedef llvm::APFixedPoint APFixedPoint; typedef llvm::APSInt APSInt; typedef llvm::APFloat APFloat; public: diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -74,6 +74,8 @@ namespace llvm { +class APFixedPoint; +class FixedPointSemantics; struct fltSemantics; template class SmallPtrSet; @@ -81,7 +83,6 @@ namespace clang { -class APFixedPoint; class APValue; class ASTMutationListener; class ASTRecordLayout; @@ -99,7 +100,6 @@ class DynTypedNode; class DynTypedNodeList; class Expr; -class FixedPointSemantics; class GlobalDecl; class MangleContext; class MangleNumberingContext; @@ -1985,9 +1985,9 @@ unsigned char getFixedPointScale(QualType Ty) const; unsigned char getFixedPointIBits(QualType Ty) const; - FixedPointSemantics getFixedPointSemantics(QualType Ty) const; - APFixedPoint getFixedPointMax(QualType Ty) const; - APFixedPoint getFixedPointMin(QualType Ty) const; + llvm::FixedPointSemantics getFixedPointSemantics(QualType Ty) const; + llvm::APFixedPoint getFixedPointMax(QualType Ty) const; + llvm::APFixedPoint getFixedPointMin(QualType Ty) const; DeclarationNameInfo getNameForTemplate(TemplateName Name, SourceLocation NameLoc) const; diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -24,7 +24,6 @@ #include "clang/AST/TemplateBase.h" #include "clang/AST/Type.h" #include "clang/Basic/CharInfo.h" -#include "clang/Basic/FixedPoint.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/SyncScope.h" #include "clang/Basic/TypeTraits.h" diff --git a/clang/include/clang/AST/OptionalDiagnostic.h b/clang/include/clang/AST/OptionalDiagnostic.h --- a/clang/include/clang/AST/OptionalDiagnostic.h +++ b/clang/include/clang/AST/OptionalDiagnostic.h @@ -63,7 +63,7 @@ return *this; } - OptionalDiagnostic &operator<<(const APFixedPoint &FX) { + OptionalDiagnostic &operator<<(const llvm::APFixedPoint &FX) { if (Diag) { SmallVector Buffer; FX.toString(Buffer); diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -51,7 +51,6 @@ #include "clang/Basic/Builtins.h" #include "clang/Basic/CommentOptions.h" #include "clang/Basic/ExceptionSpecificationType.h" -#include "clang/Basic/FixedPoint.h" #include "clang/Basic/IdentifierTable.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/LangOptions.h" @@ -65,6 +64,7 @@ #include "clang/Basic/TargetCXXABI.h" #include "clang/Basic/TargetInfo.h" #include "clang/Basic/XRayLists.h" +#include "llvm/ADT/APFixedPoint.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/APSInt.h" #include "llvm/ADT/ArrayRef.h" @@ -11182,29 +11182,30 @@ } } -FixedPointSemantics ASTContext::getFixedPointSemantics(QualType Ty) const { +llvm::FixedPointSemantics +ASTContext::getFixedPointSemantics(QualType Ty) const { assert((Ty->isFixedPointType() || Ty->isIntegerType()) && "Can only get the fixed point semantics for a " "fixed point or integer type."); if (Ty->isIntegerType()) - return FixedPointSemantics::GetIntegerSemantics(getIntWidth(Ty), - Ty->isSignedIntegerType()); + return llvm::FixedPointSemantics::GetIntegerSemantics( + getIntWidth(Ty), Ty->isSignedIntegerType()); bool isSigned = Ty->isSignedFixedPointType(); - return FixedPointSemantics( + return llvm::FixedPointSemantics( static_cast(getTypeSize(Ty)), getFixedPointScale(Ty), isSigned, Ty->isSaturatedFixedPointType(), !isSigned && getTargetInfo().doUnsignedFixedPointTypesHavePadding()); } -APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const { +llvm::APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const { assert(Ty->isFixedPointType()); - return APFixedPoint::getMax(getFixedPointSemantics(Ty)); + return llvm::APFixedPoint::getMax(getFixedPointSemantics(Ty)); } -APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const { +llvm::APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const { assert(Ty->isFixedPointType()); - return APFixedPoint::getMin(getFixedPointSemantics(Ty)); + return llvm::APFixedPoint::getMin(getFixedPointSemantics(Ty)); } QualType ASTContext::getCorrespondingSignedFixedPointType(QualType Ty) const { diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -50,8 +50,8 @@ #include "clang/AST/StmtVisitor.h" #include "clang/AST/TypeLoc.h" #include "clang/Basic/Builtins.h" -#include "clang/Basic/FixedPoint.h" #include "clang/Basic/TargetInfo.h" +#include "llvm/ADT/APFixedPoint.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallBitVector.h" #include "llvm/Support/Debug.h" @@ -63,9 +63,11 @@ #define DEBUG_TYPE "exprconstant" using namespace clang; +using llvm::APFixedPoint; using llvm::APInt; using llvm::APSInt; using llvm::APFloat; +using llvm::FixedPointSemantics; using llvm::Optional; namespace { diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -4337,10 +4337,10 @@ void clang::FixedPointValueToString(SmallVectorImpl &Str, llvm::APSInt Val, unsigned Scale) { - FixedPointSemantics FXSema(Val.getBitWidth(), Scale, Val.isSigned(), - /*IsSaturated=*/false, - /*HasUnsignedPadding=*/false); - APFixedPoint(Val, FXSema).toString(Str); + llvm::FixedPointSemantics FXSema(Val.getBitWidth(), Scale, Val.isSigned(), + /*IsSaturated=*/false, + /*HasUnsignedPadding=*/false); + llvm::APFixedPoint(Val, FXSema).toString(Str); } AutoType::AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword, diff --git a/clang/lib/Basic/CMakeLists.txt b/clang/lib/Basic/CMakeLists.txt --- a/clang/lib/Basic/CMakeLists.txt +++ b/clang/lib/Basic/CMakeLists.txt @@ -48,7 +48,6 @@ ExpressionTraits.cpp FileManager.cpp FileSystemStatCache.cpp - FixedPoint.cpp IdentifierTable.cpp LangOptions.cpp LangStandards.cpp diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -26,8 +26,8 @@ #include "clang/AST/RecordLayout.h" #include "clang/AST/StmtVisitor.h" #include "clang/Basic/CodeGenOptions.h" -#include "clang/Basic/FixedPoint.h" #include "clang/Basic/TargetInfo.h" +#include "llvm/ADT/APFixedPoint.h" #include "llvm/ADT/Optional.h" #include "llvm/IR/CFG.h" #include "llvm/IR/Constants.h" @@ -356,8 +356,9 @@ /// and an integer. Value *EmitFixedPointConversion(Value *Src, QualType SrcTy, QualType DstTy, SourceLocation Loc); - Value *EmitFixedPointConversion(Value *Src, FixedPointSemantics &SrcFixedSema, - FixedPointSemantics &DstFixedSema, + Value *EmitFixedPointConversion(Value *Src, + llvm::FixedPointSemantics &SrcFixedSema, + llvm::FixedPointSemantics &DstFixedSema, SourceLocation Loc, bool DstIsInteger = false); @@ -1444,17 +1445,17 @@ Value *ScalarExprEmitter::EmitFixedPointConversion(Value *Src, QualType SrcTy, QualType DstTy, SourceLocation Loc) { - FixedPointSemantics SrcFPSema = - CGF.getContext().getFixedPointSemantics(SrcTy); - FixedPointSemantics DstFPSema = - CGF.getContext().getFixedPointSemantics(DstTy); + auto SrcFPSema = CGF.getContext().getFixedPointSemantics(SrcTy); + auto DstFPSema = CGF.getContext().getFixedPointSemantics(DstTy); return EmitFixedPointConversion(Src, SrcFPSema, DstFPSema, Loc, DstTy->isIntegerType()); } Value *ScalarExprEmitter::EmitFixedPointConversion( - Value *Src, FixedPointSemantics &SrcFPSema, FixedPointSemantics &DstFPSema, + Value *Src, llvm::FixedPointSemantics &SrcFPSema, + llvm::FixedPointSemantics &DstFPSema, SourceLocation Loc, bool DstIsInteger) { + using llvm::APFixedPoint; using llvm::APInt; using llvm::ConstantInt; using llvm::Value; @@ -2667,12 +2668,10 @@ // Now, convert from our invented integer literal to the type of the unary // op. This will upscale and saturate if necessary. This value can become // undef in some cases. - FixedPointSemantics SrcSema = - FixedPointSemantics::GetIntegerSemantics(value->getType() - ->getScalarSizeInBits(), - /*IsSigned=*/true); - FixedPointSemantics DstSema = - CGF.getContext().getFixedPointSemantics(Info.Ty); + auto SrcSema = + llvm::FixedPointSemantics::GetIntegerSemantics( + value->getType()->getScalarSizeInBits(), /*IsSigned=*/true); + auto DstSema = CGF.getContext().getFixedPointSemantics(Info.Ty); Info.RHS = EmitFixedPointConversion(Info.RHS, SrcSema, DstSema, E->getExprLoc()); value = EmitFixedPointBinOp(Info); diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -11843,9 +11843,9 @@ Expr::EvalResult Result; if (E->EvaluateAsFixedPoint(Result, S.Context, Expr::SE_AllowSideEffects, S.isConstantEvaluated())) { - APFixedPoint Value = Result.Val.getFixedPoint(); - APFixedPoint MaxVal = S.Context.getFixedPointMax(T); - APFixedPoint MinVal = S.Context.getFixedPointMin(T); + llvm::APFixedPoint Value = Result.Val.getFixedPoint(); + llvm::APFixedPoint MaxVal = S.Context.getFixedPointMax(T); + llvm::APFixedPoint MinVal = S.Context.getFixedPointMin(T); if (Value > MaxVal || Value < MinVal) { S.DiagRuntimeBehavior(E->getExprLoc(), E, S.PDiag(diag::warn_impcast_fixed_point_range) @@ -11860,7 +11860,7 @@ if (!S.isConstantEvaluated() && E->EvaluateAsFixedPoint(Result, S.Context, Expr::SE_AllowSideEffects)) { - APFixedPoint FXResult = Result.Val.getFixedPoint(); + llvm::APFixedPoint FXResult = Result.Val.getFixedPoint(); bool Overflowed; llvm::APSInt IntResult = FXResult.convertToInt( @@ -11885,7 +11885,7 @@ llvm::APSInt Value = Result.Val.getInt(); bool Overflowed; - APFixedPoint IntResult = APFixedPoint::getFromIntValue( + llvm::APFixedPoint IntResult = llvm::APFixedPoint::getFromIntValue( Value, S.Context.getFixedPointSemantics(T), &Overflowed); if (Overflowed) { diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -27,7 +27,6 @@ #include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/TypeLoc.h" #include "clang/Basic/Builtins.h" -#include "clang/Basic/FixedPoint.h" #include "clang/Basic/PartialDiagnostic.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/TargetInfo.h" @@ -10610,7 +10609,7 @@ if (LHSExprType->isExtIntType()) LeftSize = S.Context.getIntWidth(LHSExprType); else if (LHSExprType->isFixedPointType()) { - FixedPointSemantics FXSema = S.Context.getFixedPointSemantics(LHSExprType); + auto FXSema = S.Context.getFixedPointSemantics(LHSExprType); LeftSize = FXSema.getWidth() - (unsigned)FXSema.hasUnsignedPadding(); } llvm::APInt LeftBits(Right.getBitWidth(), LeftSize); diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -8941,7 +8941,7 @@ return SourceRange(beg, end); } -static FixedPointSemantics +static llvm::FixedPointSemantics ReadFixedPointSemantics(const SmallVectorImpl &Record, unsigned &Idx) { unsigned Width = Record[Idx++]; @@ -8950,8 +8950,8 @@ bool IsSigned = Tmp & 0x1; bool IsSaturated = Tmp & 0x2; bool HasUnsignedPadding = Tmp & 0x4; - return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated, - HasUnsignedPadding); + return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated, + HasUnsignedPadding); } static const llvm::fltSemantics & @@ -8974,8 +8974,8 @@ return APValue(readAPFloat(FloatSema)); } case APValue::FixedPoint: { - FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx); - return APValue(APFixedPoint(readAPInt(), FPSema)); + llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx); + return APValue(llvm::APFixedPoint(readAPInt(), FPSema)); } case APValue::ComplexInt: { llvm::APSInt First = readAPSInt(); diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -5127,7 +5127,7 @@ } static void WriteFixedPointSemantics(ASTRecordWriter &Record, - FixedPointSemantics FPSema) { + llvm::FixedPointSemantics FPSema) { Record.push_back(FPSema.getWidth()); Record.push_back(FPSema.getScale()); Record.push_back(FPSema.isSigned() | FPSema.isSaturated() << 1 | diff --git a/clang/unittests/Basic/CMakeLists.txt b/clang/unittests/Basic/CMakeLists.txt --- a/clang/unittests/Basic/CMakeLists.txt +++ b/clang/unittests/Basic/CMakeLists.txt @@ -6,7 +6,6 @@ CharInfoTest.cpp DiagnosticTest.cpp FileManagerTest.cpp - FixedPointTest.cpp SourceManagerTest.cpp ) diff --git a/clang/include/clang/Basic/FixedPoint.h b/llvm/include/llvm/ADT/APFixedPoint.h rename from clang/include/clang/Basic/FixedPoint.h rename to llvm/include/llvm/ADT/APFixedPoint.h --- a/clang/include/clang/Basic/FixedPoint.h +++ b/llvm/include/llvm/ADT/APFixedPoint.h @@ -1,4 +1,4 @@ -//===- FixedPoint.h - Fixed point constant handling -------------*- C++ -*-===// +//===- APFixedPoint.h - Fixed point constant handling -----------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -9,23 +9,20 @@ /// \file /// Defines the fixed point number interface. /// This is a class for abstracting various operations performed on fixed point -/// types described in ISO/IEC JTC1 SC22 WG14 N1169 starting at clause 4. +/// types. // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_BASIC_FIXEDPOINT_H -#define LLVM_CLANG_BASIC_FIXEDPOINT_H +#ifndef LLVM_ADT_APFIXEDPOINT_H +#define LLVM_ADT_APFIXEDPOINT_H #include "llvm/ADT/APSInt.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/raw_ostream.h" -namespace clang { - -class ASTContext; -class QualType; +namespace llvm { -/// The fixed point semantics work similarly to llvm::fltSemantics. The width +/// The fixed point semantics work similarly to fltSemantics. The width /// specifies the whole bit width of the underlying scaled integer (with padding /// if any). The scale represents the number of fractional bits in this type. /// When HasUnsignedPadding is true and this type is unsigned, the first bit @@ -88,26 +85,21 @@ /// info about the fixed point type's width, sign, scale, and saturation, and /// provides different operations that would normally be performed on fixed point /// types. -/// -/// Semantically this does not represent any existing C type other than fixed -/// point types and should eventually be moved to LLVM if fixed point types gain -/// native IR support. class APFixedPoint { public: - APFixedPoint(const llvm::APInt &Val, const FixedPointSemantics &Sema) + APFixedPoint(const APInt &Val, const FixedPointSemantics &Sema) : Val(Val, !Sema.isSigned()), Sema(Sema) { assert(Val.getBitWidth() == Sema.getWidth() && "The value should have a bit width that matches the Sema width"); } APFixedPoint(uint64_t Val, const FixedPointSemantics &Sema) - : APFixedPoint(llvm::APInt(Sema.getWidth(), Val, Sema.isSigned()), - Sema) {} + : APFixedPoint(APInt(Sema.getWidth(), Val, Sema.isSigned()), Sema) {} // Zero initialization. APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {} - llvm::APSInt getValue() const { return llvm::APSInt(Val, !Sema.isSigned()); } + APSInt getValue() const { return APSInt(Val, !Sema.isSigned()); } inline unsigned getWidth() const { return Sema.getWidth(); } inline unsigned getScale() const { return Sema.getScale(); } inline bool isSaturated() const { return Sema.isSaturated(); } @@ -149,7 +141,7 @@ /// Return the integral part of this fixed point number, rounded towards /// zero. (-2.5k -> -2) - llvm::APSInt getIntPart() const { + APSInt getIntPart() const { if (Val < 0 && Val != -Val) // Cover the case when we have the min val return -(-Val >> getScale()); else @@ -164,12 +156,12 @@ /// /// If the overflow parameter is provided, set this value to true or false to /// indicate if this operation results in an overflow. - llvm::APSInt convertToInt(unsigned DstWidth, bool DstSign, - bool *Overflow = nullptr) const; + APSInt convertToInt(unsigned DstWidth, bool DstSign, + bool *Overflow = nullptr) const; - void toString(llvm::SmallVectorImpl &Str) const; + void toString(SmallVectorImpl &Str) const; std::string toString() const { - llvm::SmallString<40> S; + SmallString<40> S; toString(S); return std::string(S.str()); } @@ -198,21 +190,20 @@ /// and in the same semantics as the provided target semantics. If the value /// is not able to fit in the specified fixed point semantics, and the /// overflow parameter is provided, it is set to true. - static APFixedPoint getFromIntValue(const llvm::APSInt &Value, + static APFixedPoint getFromIntValue(const APSInt &Value, const FixedPointSemantics &DstFXSema, bool *Overflow = nullptr); private: - llvm::APSInt Val; + APSInt Val; FixedPointSemantics Sema; }; -inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, - const APFixedPoint &FX) { +inline raw_ostream &operator<<(raw_ostream &OS, const APFixedPoint &FX) { OS << FX.toString(); return OS; } -} // namespace clang +} // namespace llvm #endif diff --git a/clang/lib/Basic/FixedPoint.cpp b/llvm/lib/Support/APFixedPoint.cpp rename from clang/lib/Basic/FixedPoint.cpp rename to llvm/lib/Support/APFixedPoint.cpp --- a/clang/lib/Basic/FixedPoint.cpp +++ b/llvm/lib/Support/APFixedPoint.cpp @@ -1,4 +1,4 @@ -//===- FixedPoint.cpp - Fixed point constant handling -----------*- C++ -*-===// +//===- APFixedPoint.cpp - Fixed point constant handling ---------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -11,13 +11,13 @@ // //===----------------------------------------------------------------------===// -#include "clang/Basic/FixedPoint.h" +#include "llvm/ADT/APFixedPoint.h" -namespace clang { +namespace llvm { APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema, bool *Overflow) const { - llvm::APSInt NewVal = Val; + APSInt NewVal = Val; unsigned DstWidth = DstSema.getWidth(); unsigned DstScale = DstSema.getScale(); bool Upscaling = DstScale > getScale(); @@ -31,10 +31,10 @@ NewVal >>= (getScale() - DstScale); } - auto Mask = llvm::APInt::getBitsSetFrom( + auto Mask = APInt::getBitsSetFrom( NewVal.getBitWidth(), std::min(DstScale + DstSema.getIntegralBits(), NewVal.getBitWidth())); - llvm::APInt Masked(NewVal & Mask); + APInt Masked(NewVal & Mask); // Change in the bits above the sign if (!(Masked == Mask || Masked == 0)) { @@ -61,8 +61,8 @@ } int APFixedPoint::compare(const APFixedPoint &Other) const { - llvm::APSInt ThisVal = getValue(); - llvm::APSInt OtherVal = Other.getValue(); + APSInt ThisVal = getValue(); + APSInt OtherVal = Other.getValue(); bool ThisSigned = Val.isSigned(); bool OtherSigned = OtherVal.isSigned(); unsigned OtherScale = Other.getScale(); @@ -113,14 +113,14 @@ APFixedPoint APFixedPoint::getMax(const FixedPointSemantics &Sema) { bool IsUnsigned = !Sema.isSigned(); - auto Val = llvm::APSInt::getMaxValue(Sema.getWidth(), IsUnsigned); + auto Val = APSInt::getMaxValue(Sema.getWidth(), IsUnsigned); if (IsUnsigned && Sema.hasUnsignedPadding()) Val = Val.lshr(1); return APFixedPoint(Val, Sema); } APFixedPoint APFixedPoint::getMin(const FixedPointSemantics &Sema) { - auto Val = llvm::APSInt::getMinValue(Sema.getWidth(), !Sema.isSigned()); + auto Val = APSInt::getMinValue(Sema.getWidth(), !Sema.isSigned()); return APFixedPoint(Val, Sema); } @@ -154,11 +154,11 @@ auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics()); APFixedPoint ConvertedThis = convert(CommonFXSema); APFixedPoint ConvertedOther = Other.convert(CommonFXSema); - llvm::APSInt ThisVal = ConvertedThis.getValue(); - llvm::APSInt OtherVal = ConvertedOther.getValue(); + APSInt ThisVal = ConvertedThis.getValue(); + APSInt OtherVal = ConvertedOther.getValue(); bool Overflowed = false; - llvm::APSInt Result; + APSInt Result; if (CommonFXSema.isSaturated()) { Result = CommonFXSema.isSigned() ? ThisVal.sadd_sat(OtherVal) : ThisVal.uadd_sat(OtherVal); @@ -178,11 +178,11 @@ auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics()); APFixedPoint ConvertedThis = convert(CommonFXSema); APFixedPoint ConvertedOther = Other.convert(CommonFXSema); - llvm::APSInt ThisVal = ConvertedThis.getValue(); - llvm::APSInt OtherVal = ConvertedOther.getValue(); + APSInt ThisVal = ConvertedThis.getValue(); + APSInt OtherVal = ConvertedOther.getValue(); bool Overflowed = false; - llvm::APSInt Result; + APSInt Result; if (CommonFXSema.isSaturated()) { Result = CommonFXSema.isSigned() ? ThisVal.ssub_sat(OtherVal) : ThisVal.usub_sat(OtherVal); @@ -202,8 +202,8 @@ auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics()); APFixedPoint ConvertedThis = convert(CommonFXSema); APFixedPoint ConvertedOther = Other.convert(CommonFXSema); - llvm::APSInt ThisVal = ConvertedThis.getValue(); - llvm::APSInt OtherVal = ConvertedOther.getValue(); + APSInt ThisVal = ConvertedThis.getValue(); + APSInt OtherVal = ConvertedOther.getValue(); bool Overflowed = false; // Widen the LHS and RHS so we can perform a full multiplication. @@ -223,7 +223,7 @@ // outside the representable range. We interpret the spec as allowing us to // perform the rounding step first, avoiding the overflow case that would // arise. - llvm::APSInt Result; + APSInt Result; if (CommonFXSema.isSigned()) Result = ThisVal.smul_ov(OtherVal, Overflowed) .ashr(CommonFXSema.getScale()); @@ -235,10 +235,10 @@ // If our result lies outside of the representative range of the common // semantic, we either have overflow or saturation. - llvm::APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue() - .extOrTrunc(Wide); - llvm::APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue() - .extOrTrunc(Wide); + APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue() + .extOrTrunc(Wide); + APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue() + .extOrTrunc(Wide); if (CommonFXSema.isSaturated()) { if (Result < Min) Result = Min; @@ -259,8 +259,8 @@ auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics()); APFixedPoint ConvertedThis = convert(CommonFXSema); APFixedPoint ConvertedOther = Other.convert(CommonFXSema); - llvm::APSInt ThisVal = ConvertedThis.getValue(); - llvm::APSInt OtherVal = ConvertedOther.getValue(); + APSInt ThisVal = ConvertedThis.getValue(); + APSInt OtherVal = ConvertedOther.getValue(); bool Overflowed = false; // Widen the LHS and RHS so we can perform a full division. @@ -276,10 +276,10 @@ // Upscale to compensate for the loss of precision from division, and // perform the full division. ThisVal = ThisVal.shl(CommonFXSema.getScale()); - llvm::APSInt Result; + APSInt Result; if (CommonFXSema.isSigned()) { - llvm::APInt Rem; - llvm::APInt::sdivrem(ThisVal, OtherVal, Result, Rem); + APInt Rem; + APInt::sdivrem(ThisVal, OtherVal, Result, Rem); // If the quotient is negative and the remainder is nonzero, round // towards negative infinity by subtracting epsilon from the result. if (ThisVal.isNegative() != OtherVal.isNegative() && !Rem.isNullValue()) @@ -290,10 +290,10 @@ // If our result lies outside of the representative range of the common // semantic, we either have overflow or saturation. - llvm::APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue() - .extOrTrunc(Wide); - llvm::APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue() - .extOrTrunc(Wide); + APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue() + .extOrTrunc(Wide); + APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue() + .extOrTrunc(Wide); if (CommonFXSema.isSaturated()) { if (Result < Min) Result = Min; @@ -310,7 +310,7 @@ } APFixedPoint APFixedPoint::shl(unsigned Amt, bool *Overflow) const { - llvm::APSInt ThisVal = Val; + APSInt ThisVal = Val; bool Overflowed = false; // Widen the LHS. @@ -322,13 +322,13 @@ // Clamp the shift amount at the original width, and perform the shift. Amt = std::min(Amt, ThisVal.getBitWidth()); - llvm::APSInt Result = ThisVal << Amt; + APSInt Result = ThisVal << Amt; Result.setIsSigned(Sema.isSigned()); // If our result lies outside of the representative range of the // semantic, we either have overflow or saturation. - llvm::APSInt Max = APFixedPoint::getMax(Sema).getValue().extOrTrunc(Wide); - llvm::APSInt Min = APFixedPoint::getMin(Sema).getValue().extOrTrunc(Wide); + APSInt Max = APFixedPoint::getMax(Sema).getValue().extOrTrunc(Wide); + APSInt Min = APFixedPoint::getMin(Sema).getValue().extOrTrunc(Wide); if (Sema.isSaturated()) { if (Result < Min) Result = Min; @@ -343,8 +343,8 @@ return APFixedPoint(Result.sextOrTrunc(Sema.getWidth()), Sema); } -void APFixedPoint::toString(llvm::SmallVectorImpl &Str) const { - llvm::APSInt Val = getValue(); +void APFixedPoint::toString(SmallVectorImpl &Str) const { + APSInt Val = getValue(); unsigned Scale = getScale(); if (Val.isSigned() && Val.isNegative() && Val != -Val) { @@ -352,13 +352,13 @@ Str.push_back('-'); } - llvm::APSInt IntPart = Val >> Scale; + APSInt IntPart = Val >> Scale; // Add 4 digits to hold the value after multiplying 10 (the radix) unsigned Width = Val.getBitWidth() + 4; - llvm::APInt FractPart = Val.zextOrTrunc(Scale).zext(Width); - llvm::APInt FractPartMask = llvm::APInt::getAllOnesValue(Scale).zext(Width); - llvm::APInt RadixInt = llvm::APInt(Width, 10); + APInt FractPart = Val.zextOrTrunc(Scale).zext(Width); + APInt FractPartMask = APInt::getAllOnesValue(Scale).zext(Width); + APInt RadixInt = APInt(Width, 10); IntPart.toString(Str, /*Radix=*/10); Str.push_back('.'); @@ -388,13 +388,13 @@ return APFixedPoint(Sema); } -llvm::APSInt APFixedPoint::convertToInt(unsigned DstWidth, bool DstSign, - bool *Overflow) const { - llvm::APSInt Result = getIntPart(); +APSInt APFixedPoint::convertToInt(unsigned DstWidth, bool DstSign, + bool *Overflow) const { + APSInt Result = getIntPart(); unsigned SrcWidth = getWidth(); - llvm::APSInt DstMin = llvm::APSInt::getMinValue(DstWidth, !DstSign); - llvm::APSInt DstMax = llvm::APSInt::getMaxValue(DstWidth, !DstSign); + APSInt DstMin = APSInt::getMinValue(DstWidth, !DstSign); + APSInt DstMax = APSInt::getMaxValue(DstWidth, !DstSign); if (SrcWidth < DstWidth) { Result = Result.extend(DstWidth); @@ -417,7 +417,7 @@ return Result.extOrTrunc(DstWidth); } -APFixedPoint APFixedPoint::getFromIntValue(const llvm::APSInt &Value, +APFixedPoint APFixedPoint::getFromIntValue(const APSInt &Value, const FixedPointSemantics &DstFXSema, bool *Overflow) { FixedPointSemantics IntFXSema = FixedPointSemantics::GetIntegerSemantics( diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -67,6 +67,7 @@ ABIBreak.cpp ARMTargetParser.cpp AMDGPUMetadata.cpp + APFixedPoint.cpp APFloat.cpp APInt.cpp APSInt.cpp diff --git a/clang/unittests/Basic/FixedPointTest.cpp b/llvm/unittests/ADT/APFixedPointTest.cpp rename from clang/unittests/Basic/FixedPointTest.cpp rename to llvm/unittests/ADT/APFixedPointTest.cpp --- a/clang/unittests/Basic/FixedPointTest.cpp +++ b/llvm/unittests/ADT/APFixedPointTest.cpp @@ -1,4 +1,4 @@ -//===- unittests/Basic/FixedPointTest.cpp -- fixed point number tests -----===// +//===- unittests/ADT/FixedPointTest.cpp -- fixed point number tests -----===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,12 +6,12 @@ // //===----------------------------------------------------------------------===// -#include "clang/Basic/FixedPoint.h" +#include "llvm/ADT/APFixedPoint.h" #include "llvm/ADT/APSInt.h" #include "gtest/gtest.h" -using clang::APFixedPoint; -using clang::FixedPointSemantics; +using llvm::APFixedPoint; +using llvm::FixedPointSemantics; using llvm::APInt; using llvm::APSInt; diff --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt --- a/llvm/unittests/ADT/CMakeLists.txt +++ b/llvm/unittests/ADT/CMakeLists.txt @@ -4,6 +4,7 @@ add_llvm_unittest(ADTTests AnyTest.cpp + APFixedPointTest.cpp APFloatTest.cpp APIntTest.cpp APSIntTest.cpp