Skip to content

[clang][bytecode] Implement fixed-point add #110405

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 29, 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
3 changes: 3 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,9 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
case BO_GE:
return this->emitGEFixedPoint(E);
#endif
case BO_Add:
return this->emitAddFixedPoint(E);

default:
return this->emitInvalid(E);
}
Expand Down
36 changes: 35 additions & 1 deletion clang/lib/AST/ByteCode/FixedPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,28 @@ 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(); }
APSInt toAPSInt(unsigned BitWidth = 0) const { return V.getValue(); }

unsigned bitWidth() const { return V.getWidth(); }
bool isSigned() const { return V.isSigned(); }
bool isZero() const { return V.getValue().isZero(); }
bool isNegative() const { return V.getValue().isNegative(); }
bool isPositive() const { return V.getValue().isNonNegative(); }
bool isMin() const {
return V.getValue() == APSInt::getMinValue(V.getSemantics().getWidth(),
!V.getSemantics().isSigned());
}

FixedPoint truncate(unsigned BitWidth) const { return *this; }

llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const {
return V.convertToFloat(*Sem);
}

std::string toDiagnosticString(const ASTContext &Ctx) const {
return V.toString();
}

ComparisonCategoryResult compare(const FixedPoint &Other) const {
if (Other.V == V)
return ComparisonCategoryResult::Equal;
Expand All @@ -67,6 +80,27 @@ class FixedPoint final {
*R = FixedPoint(A.V.negate(&Overflow));
return Overflow;
}

static bool add(const FixedPoint A, const FixedPoint B, unsigned Bits,
FixedPoint *R) {
bool Overflow = false;
*R = FixedPoint(A.V.add(B.V, &Overflow));
return Overflow;
}
static bool sub(const FixedPoint A, const FixedPoint B, unsigned Bits,
FixedPoint *R) {
return true;
}
static bool mul(const FixedPoint A, const FixedPoint B, unsigned Bits,
FixedPoint *R) {
return true;
}
static bool div(const FixedPoint A, const FixedPoint B, unsigned Bits,
FixedPoint *R) {
return true;
}
static bool increment(const FixedPoint &A, FixedPoint *R) { return true; }
static bool decrement(const FixedPoint &A, FixedPoint *R) { return true; }
};

inline FixedPoint getSwappedBytes(FixedPoint F) { return F; }
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/ByteCode/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def FloatTypeClass : TypeClass {
}

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

def PtrTypeClass : TypeClass {
Expand All @@ -110,7 +110,7 @@ def NonPtrTypeClass : TypeClass {
}

def AllTypeClass : TypeClass {
let Types = !listconcat(AluTypeClass.Types, PtrTypeClass.Types, FloatTypeClass.Types, [FixedPoint]);
let Types = !listconcat(AluTypeClass.Types, PtrTypeClass.Types, FloatTypeClass.Types);
}

def ComparableTypeClass : TypeClass {
Expand Down
12 changes: 12 additions & 0 deletions clang/test/AST/ByteCode/fixed-point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ namespace FloatToFixedPointCast {
constexpr float sf2f = sf2;
static_assert(sf2f == 0.5);
}

namespace BinOps {
constexpr _Accum A = 13;
static_assert(A + 1 == 14.0k);
static_assert(1 + A == 14.0k);
static_assert((A + A) == 26);

/// FIXME: Conversion between fixed point semantics.
static_assert(A + 100000 == 14.0k); // expected-error {{static assertion failed}} \
// ref-error {{is not an integral constant expression}} \
// ref-note {{is outside the range of representable values}}
}
Loading