Skip to content

[clang][bytecode] Implement fixed point negation #110237

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

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
4 changes: 2 additions & 2 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,9 @@ bool Compiler<Emitter>::VisitFixedPointLiteral(const FixedPointLiteral *E) {
assert(E->getType()->isFixedPointType());
assert(classifyPrim(E) == PT_FixedPoint);

// FIXME: Semantics.
auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
APInt Value = E->getValue();
return this->emitConstFixedPoint(Value, E);
return this->emitConstFixedPoint(FixedPoint(Value, Sem), E);
}

template <class Emitter>
Expand Down
16 changes: 13 additions & 3 deletions clang/lib/AST/ByteCode/FixedPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ namespace clang {
namespace interp {

using APInt = llvm::APInt;
using APSInt = llvm::APSInt;

/// Wrapper around fixed point types.
class FixedPoint final {
private:
llvm::APFixedPoint V;
FixedPoint(llvm::APFixedPoint &&V) : V(std::move(V)) {}

public:
FixedPoint(APInt V)
: V(V,
llvm::FixedPointSemantics(V.getBitWidth(), 0, false, false, false)) {}
FixedPoint(APInt V, llvm::FixedPointSemantics Sem) : V(V, Sem) {}
// This needs to be default-constructible so llvm::endian::read works.
FixedPoint()
: V(APInt(0, 0ULL, false),
Expand All @@ -42,12 +42,22 @@ class FixedPoint final {
void print(llvm::raw_ostream &OS) const { OS << V; }

APValue toAPValue(const ASTContext &) const { return APValue(V); }
APSInt toAPSInt(unsigned BitWidth) const { return V.getValue(); }

unsigned bitWidth() const { return V.getWidth(); }
bool isSigned() const { return V.isSigned(); }

ComparisonCategoryResult compare(const FixedPoint &Other) const {
if (Other.V == V)
return ComparisonCategoryResult::Equal;
return ComparisonCategoryResult::Unordered;
}

static bool neg(const FixedPoint &A, FixedPoint *R) {
bool Overflow = false;
*R = FixedPoint(A.V.negate(&Overflow));
return Overflow;
}
};

inline FixedPoint getSwappedBytes(FixedPoint F) { return F; }
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def PtrTypeClass : TypeClass {
}

def NonPtrTypeClass : TypeClass {
let Types = !listconcat(IntegerTypeClass.Types, [Bool], [Float]);
let Types = !listconcat(IntegerTypeClass.Types, [Bool], [Float], [FixedPoint]);
}

def AllTypeClass : TypeClass {
Expand Down
12 changes: 6 additions & 6 deletions clang/lib/AST/ByteCode/PrimType.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ enum PrimType : unsigned {
PT_IntAP = 8,
PT_IntAPS = 9,
PT_Bool = 10,
PT_Float = 11,
PT_Ptr = 12,
PT_FnPtr = 13,
PT_MemberPtr = 14,
PT_FixedPoint = 15,
PT_FixedPoint = 11,
PT_Float = 12,
PT_Ptr = 13,
PT_FnPtr = 14,
PT_MemberPtr = 15,
};

inline constexpr bool isPtrType(PrimType T) {
Expand All @@ -71,7 +71,7 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
return OS;
}

constexpr bool isIntegralType(PrimType T) { return T <= PT_Bool; }
constexpr bool isIntegralType(PrimType T) { return T <= PT_FixedPoint; }

/// Mapping from primitive types to their representation.
template <PrimType T> struct PrimConv;
Expand Down
2 changes: 2 additions & 0 deletions clang/test/AST/ByteCode/fixed-point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ static_assert((bool)0.0k); // both-error {{static assertion failed}}

static_assert(1.0k == 1.0k);
static_assert(1.0k != 1.0k); // both-error {{failed due to requirement '1.0k != 1.0k'}}
static_assert(-12.0k == -(-(-12.0k)));

Loading