Skip to content

Decimal float impl #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ endif()
# Must go below project(..)
include(GNUInstallDirs)

list(APPEND CMAKE_REQUIRED_INCLUDES "/Users/shafik/Downloads/IntelRDFPMathLib20U2/LIBRARY/src")
list(APPEND CMAKE_REQUIRED_LIBRARIES "/Users/shafik/Downloads/IntelRDFPMathLib20U2/LIBRARY/libbid.a")

if(CLANG_BUILT_STANDALONE)
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
set(CMAKE_CXX_STANDARD_REQUIRED YES)
Expand Down Expand Up @@ -998,6 +1001,8 @@ endif()

set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change. The one below too.

configure_file(
${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
${CLANG_BINARY_DIR}/include/clang/Config/config.h)

7 changes: 6 additions & 1 deletion clang/include/clang-c/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,12 @@ enum CXCursorKind {
*/
CXCursor_RequiresExpr = 154,

CXCursor_LastExpr = CXCursor_RequiresExpr,
/**
* Decimal Float literal
*/
CXCursor_DecimalFloatLiteral = 155,

CXCursor_LastExpr = CXCursor_DecimalFloatLiteral,

/* Statements */
CXCursor_FirstStmt = 200,
Expand Down
26 changes: 25 additions & 1 deletion clang/include/clang/AST/APValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define LLVM_CLANG_AST_APVALUE_H

#include "clang/Basic/LLVM.h"
#include "llvm/ADT/APDecimalFloat.h"
#include "llvm/ADT/APFixedPoint.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APSInt.h"
Expand Down Expand Up @@ -123,6 +124,7 @@ class APValue {
typedef llvm::APFixedPoint APFixedPoint;
typedef llvm::APSInt APSInt;
typedef llvm::APFloat APFloat;
typedef llvm::APDecimalFloat APDecimalFloat;
public:
enum ValueKind {
/// There is no such object (it's outside its lifetime).
Expand All @@ -140,7 +142,8 @@ class APValue {
Struct,
Union,
MemberPointer,
AddrLabelDiff
AddrLabelDiff,
DecimalFloat
};

class LValueBase {
Expand Down Expand Up @@ -316,6 +319,9 @@ class APValue {
explicit APValue(APFixedPoint FX) : Kind(None) {
MakeFixedPoint(std::move(FX));
}
explicit APValue(APDecimalFloat DF) : Kind(None) {
MakeDecimalFloat(std::move(DF));
}
explicit APValue(const APValue *E, unsigned N) : Kind(None) {
MakeVector(); setVector(E, N);
}
Expand Down Expand Up @@ -402,6 +408,7 @@ class APValue {
bool isUnion() const { return Kind == Union; }
bool isMemberPointer() const { return Kind == MemberPointer; }
bool isAddrLabelDiff() const { return Kind == AddrLabelDiff; }
bool isDecimalFloat() const { return Kind == DecimalFloat; }

void dump() const;
void dump(raw_ostream &OS, const ASTContext &Context) const;
Expand Down Expand Up @@ -442,6 +449,14 @@ class APValue {
return const_cast<APValue *>(this)->getFixedPoint();
}

APDecimalFloat &getDecimalFloat() {
assert(isDecimalFloat() && "Invalid accessor");
return *(APDecimalFloat *)(char *)&Data;
}
const APDecimalFloat &getDecimalFloat() const {
return const_cast<APValue *>(this)->getDecimalFloat();
}

APSInt &getComplexIntReal() {
assert(isComplexInt() && "Invalid accessor");
return ((ComplexAPSInt *)(char *)&Data)->Real;
Expand Down Expand Up @@ -589,6 +604,10 @@ class APValue {
assert(isFixedPoint() && "Invalid accessor");
*(APFixedPoint *)(char *)&Data = std::move(FX);
}
void setDecimalFloat(APDecimalFloat FX) {
assert(isDecimalFloat() && "Invalid accessor");
*(APDecimalFloat *)(char *)&Data = std::move(FX);
}
void setVector(const APValue *E, unsigned N) {
MutableArrayRef<APValue> InternalElts = setVectorUninit(N);
for (unsigned i = 0; i != N; ++i)
Expand Down Expand Up @@ -637,6 +656,11 @@ class APValue {
new ((void *)(char *)&Data) APFixedPoint(std::move(FX));
Kind = FixedPoint;
}
void MakeDecimalFloat(APDecimalFloat &&DF) {
assert(isAbsent() && "Bad state change");
new ((void *)(char *)&Data) APDecimalFloat(std::move(DF));
Kind = DecimalFloat;
}
void MakeVector() {
assert(isAbsent() && "Bad state change");
new ((void *)(char *)&Data) Vec();
Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@
namespace llvm {

class APFixedPoint;
class APDecimalFloat;
class FixedPointSemantics;
struct fltSemantics;
struct decFltSemantics;
template <typename T, unsigned N> class SmallPtrSet;

} // namespace llvm
Expand Down Expand Up @@ -1117,6 +1119,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
CanQualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
CanQualType UnsignedLongLongTy, UnsignedInt128Ty;
CanQualType FloatTy, DoubleTy, LongDoubleTy, Float128Ty, Ibm128Ty;
CanQualType DecimalFloat32Ty, DecimalFloat64Ty, DecimalFloat128Ty; // ISO/IEC TS 18661-2:2015 C23 conditionally supported
// ISO/IEC TR 24733:2011 C++ support
Comment on lines +1122 to +1123
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per https://llvm.org/docs/CodingStandards.html#source-code-width, limit line length to 80 characters.

CanQualType ShortAccumTy, AccumTy,
LongAccumTy; // ISO/IEC JTC1 SC22 WG14 N1169 Extension
CanQualType UnsignedShortAccumTy, UnsignedAccumTy, UnsignedLongAccumTy;
Expand Down Expand Up @@ -2187,6 +2191,10 @@ class ASTContext : public RefCountedBase<ASTContext> {
llvm::APFixedPoint getFixedPointMax(QualType Ty) const;
llvm::APFixedPoint getFixedPointMin(QualType Ty) const;

llvm::decFltSemantics getDecimalFloatSemantics(QualType Ty) const;
llvm::APDecimalFloat getDecimalFloatMax(QualType Ty) const;
llvm::APDecimalFloat getDecimalFloatMin(QualType Ty) const;

DeclarationNameInfo getNameForTemplate(TemplateName Name,
SourceLocation NameLoc) const;

Expand Down
10 changes: 10 additions & 0 deletions clang/include/clang/AST/AbstractBasicReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ class DataStreamBasicReader : public BasicReaderBase<Impl> {
hasUnsignedPadding);
}

llvm::decFltSemantics readdecFltSemantics() {
unsigned width = asImpl().readUInt32();
unsigned scale = asImpl().readUInt32();
unsigned isSigned = asImpl().readBool();
unsigned isSat = asImpl().readBool();
unsigned HasPadding = asImpl().readBool();

return llvm::decFltSemantics(width, scale, isSigned, isSat, HasPadding);
}

APValue::LValuePathSerializationHelper readLValuePathSerializationHelper(
SmallVectorImpl<APValue::LValuePathEntry> &path) {
auto elemTy = asImpl().readQualType();
Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/AST/AbstractBasicWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ class DataStreamBasicWriter : public BasicWriterBase<Impl> {
sema.hasUnsignedPadding() << 2);
}

void writedecFltSemantics(const llvm::decFltSemantics &sema) {
asImpl().writeUInt32(sema.getWidth());
asImpl().writeUInt32(sema.getScale());
asImpl().writeBool(sema.isSigned());
asImpl().writeBool(sema.isSaturated());
asImpl().writeBool(sema.hasUnsignedPadding());
}

void writeLValuePathSerializationHelper(
APValue::LValuePathSerializationHelper lvaluePath) {
ArrayRef<APValue::LValuePathEntry> path = lvaluePath.Path;
Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/AST/BuiltinTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ FLOATING_TYPE(Float128, Float128Ty)
// '__ibm128'
FLOATING_TYPE(Ibm128, Ibm128Ty)

// Putting Decimal Float types below IBM128Ty so that BuiltinType::isFloatingPoint() does
// not return true
//
// '_Decimal32'
SIGNED_TYPE(DecimalFloat32, DecimalFloat32Ty)

// '_Decimal64'
SIGNED_TYPE(DecimalFloat64, DecimalFloat64Ty)

// '_Decimal128'
SIGNED_TYPE(DecimalFloat128, DecimalFloat128Ty)

//===- Language-specific types --------------------------------------------===//

// This is the type of C++0x 'nullptr'.
Expand Down
77 changes: 77 additions & 0 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,28 @@ class APFloatStorage : private APNumericStorage {
}
};

class APDecimalFloatStorage : private APNumericStorage {
public:
// llvm::APDecimalFloat
llvm::APInt getValue(const llvm::decFltSemantics &Semantics) const {
// return llvm::APDecimalFloat(getIntValue(),Semantics);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this anymore, right? Same below.

return getIntValue();
}

llvm::APInt getValue() const {
// return llvm::APDecimalFloat(getIntValue(),Semantics);
return getIntValue();
}

void setValue(const ASTContext &C, const llvm::APDecimalFloat &Val) {
setIntValue(C, Val.bitcastToAPInt());
}
void setValue(const ASTContext &C, const llvm::APInt &Val) {
setIntValue(C, Val);
}
};


class IntegerLiteral : public Expr, public APIntStorage {
SourceLocation Loc;

Expand Down Expand Up @@ -1590,6 +1612,61 @@ class FixedPointLiteral : public Expr, public APIntStorage {
}
};

class DecimalFloatLiteral : public Expr, public APDecimalFloatStorage {
SourceLocation Loc;
unsigned width = 32;
unsigned scale = 1;
bool isSigned = false;

/// \brief Construct an empty fixed-point literal.
explicit DecimalFloatLiteral(EmptyShell Empty)
: Expr(DecimalFloatLiteralClass, Empty) {}

public:
DecimalFloatLiteral(const ASTContext &C, const llvm::APDecimalFloat &V, QualType type,
SourceLocation l, unsigned width = 32 );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to have width to be default?



/// Returns an empty fixed-point literal.
static DecimalFloatLiteral *Create(const ASTContext &C, EmptyShell Empty);
static DecimalFloatLiteral *
CreateFromAPDecimalFloat(const ASTContext &C, llvm::APDecimalFloat Val,
QualType type, SourceLocation l, unsigned width);

SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }

/// \brief Retrieve the location of the literal.
SourceLocation getLocation() const { return Loc; }

void setLocation(SourceLocation Location) { Loc = Location; }

llvm::decFltSemantics getSemantics() const {
return {width, scale, isSigned, false, false};
}

// llvm::APDecimalFloat getValue() const {
// return APDecimalFloatStorage::getValue(getSemantics());
// }

void setWidth(unsigned Width) { width = Width;}
unsigned getWidth() { return width; }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need here get/set scale too.

static bool classof(const Stmt *T) {
return T->getStmtClass() == DecimalFloatLiteralClass;
}

std::string getValueAsString() const;

// Iterators
child_range children() {
return child_range(child_iterator(), child_iterator());
}
const_child_range children() const {
return const_child_range(const_child_iterator(), const_child_iterator());
}
};

class CharacterLiteral : public Expr {
public:
enum CharacterKind {
Expand Down
27 changes: 26 additions & 1 deletion clang/include/clang/AST/OperationKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,35 @@ CAST_OPERATION(FixedPointToIntegral)
/// (_Accum) 2
CAST_OPERATION(IntegralToFixedPoint)

/// CK_FixedPointToBoolean - Fixed point to boolean.
/// CK_FixedPointToBoolean - Decimal Float to boolean.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to this comment doesn't seem right.

/// (bool) 0.5r
CAST_OPERATION(FixedPointToBoolean)

/// CK_FloatingToDecimalFloat - Floating to Decimal Float.
/// _Accum a = f;
CAST_OPERATION(FloatingToDecimalFloat)

/// CK_DecimalFloatToFloating - Decimal Float to floating.
/// (float) 2.5k
CAST_OPERATION(DecimalFloatToFloating)

/// CK_DecimalFloatCast - Decimal Float to Decimal Float.
/// (_Accum) 0.5r
CAST_OPERATION(DecimalFloatCast)

/// CK_DecimalFloatToIntegral - Decimal Float to integral.
/// (int) 2.0k
CAST_OPERATION(DecimalFloatToIntegral)

/// CK_IntegralToDecimalFloat - Integral to a Decimal Float.
/// (_Accum) 2
CAST_OPERATION(IntegralToDecimalFloat)

/// CK_DecimalFloatToBoolean - Decimal Float to boolean.
/// (bool) 0.5r
CAST_OPERATION(DecimalFloatToBoolean)
Comment on lines +232 to +254
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example cast operations in the comments are still for fixed point types and should be switched to operations on DFP types and literals.



/// CK_FloatingToIntegral - Floating point to integral. Rounds
/// towards zero, discarding any fractional component.
/// (int) f
Expand Down
10 changes: 10 additions & 0 deletions clang/include/clang/AST/OptionalDiagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "clang/AST/APValue.h"
#include "clang/Basic/PartialDiagnostic.h"
#include "llvm/ADT/APDecimalFloat.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -71,6 +72,15 @@ class OptionalDiagnostic {
}
return *this;
}

OptionalDiagnostic &operator<<(const llvm::APDecimalFloat &DF) {
if (Diag) {
SmallVector<char, 32> Buffer;
DF.toString(Buffer);
*Diag << StringRef(Buffer.data(), Buffer.size());
}
return *this;
}
};

} // namespace clang
Expand Down
14 changes: 14 additions & 0 deletions clang/include/clang/AST/PropertiesBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def ExtParameterInfo : PropertyType<"FunctionProtoType::ExtParameterInfo">;
def FixedPointSemantics : PropertyType<"llvm::FixedPointSemantics"> {
let PassByReference = 1;
}
def decFltSemantics : PropertyType<"llvm::decFltSemantics"> {
let PassByReference = 1;
}
def Identifier : RefPropertyType<"IdentifierInfo"> { let ConstWhenWriting = 1; }
def LValuePathEntry : PropertyType<"APValue::LValuePathEntry">;
def LValuePathSerializationHelper :
Expand Down Expand Up @@ -293,6 +296,17 @@ let Class = PropertyTypeCase<APValue, "FixedPoint"> in {
return APValue(llvm::APFixedPoint(std::move(value), semantics));
}]>;
}
let Class = PropertyTypeCase<APValue, "DecimalFloat"> in {
def : Property<"semantics", decFltSemantics> {
let Read = [{ node.getDecimalFloat().getSemantics() }];
}
def : Property<"value", APInt> {
let Read = [{ node.getDecimalFloat().bitcastToAPInt() }];
}
def : Creator<[{
return APValue(llvm::APDecimalFloat(std::move(value), semantics));
}]>;
}
let Class = PropertyTypeCase<APValue, "ComplexInt"> in {
def : Property<"real", APSInt> {
let Read = [{ node.getComplexIntReal() }];
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2905,6 +2905,7 @@ DEF_TRAVERSE_STMT(RequiresExpr, {
// These literals (all of them) do not need any action.
DEF_TRAVERSE_STMT(IntegerLiteral, {})
DEF_TRAVERSE_STMT(FixedPointLiteral, {})
DEF_TRAVERSE_STMT(DecimalFloatLiteral, {})
DEF_TRAVERSE_STMT(CharacterLiteral, {})
DEF_TRAVERSE_STMT(FloatingLiteral, {})
DEF_TRAVERSE_STMT(ImaginaryLiteral, {})
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/AST/TextNodeDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class TextNodeDumper
void VisitCharacterLiteral(const CharacterLiteral *Node);
void VisitIntegerLiteral(const IntegerLiteral *Node);
void VisitFixedPointLiteral(const FixedPointLiteral *Node);
void VisitDecimalFloatLiteral(const DecimalFloatLiteral *Node);
void VisitFloatingLiteral(const FloatingLiteral *Node);
void VisitStringLiteral(const StringLiteral *Str);
void VisitInitListExpr(const InitListExpr *ILE);
Expand Down
Loading