Skip to content

[clang][bytecode] Implement fixed point casts #110409

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
8 changes: 8 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,14 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
return this->emitCastFixedPointFloating(TargetSemantics, CE);
}
case CK_FixedPointCast: {
if (!this->visit(SubExpr))
return false;
auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
uint32_t I;
std::memcpy(&I, &Sem, sizeof(Sem));
return this->emitCastFixedPoint(I, CE);
}

case CK_ToVoid:
return discard(SubExpr);
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/ByteCode/FixedPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class FixedPoint final {

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

FixedPoint toSemantics(const llvm::FixedPointSemantics &Sem,
bool *Overflow) const {
return FixedPoint(V.convert(Sem, Overflow));
}

llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const {
return V.convertToFloat(*Sem);
}
Expand Down
25 changes: 25 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,31 @@ inline bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem,
return true;
}

inline bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS) {
FixedPointSemantics TargetSemantics(0, 0, false, false, false);
std::memcpy(&TargetSemantics, &FPS, sizeof(TargetSemantics));

const auto &Source = S.Stk.pop<FixedPoint>();

bool Overflow;
FixedPoint Result = Source.toSemantics(TargetSemantics, &Overflow);

if (Overflow) {
const Expr *E = S.Current->getExpr(OpPC);
if (S.checkingForUndefinedBehavior()) {
S.getASTContext().getDiagnostics().Report(
E->getExprLoc(), diag::warn_fixedpoint_constant_overflow)
<< Result.toDiagnosticString(S.getASTContext()) << E->getType();
}
S.CCEDiag(E, diag::note_constexpr_overflow) << Result << E->getType();
if (!S.noteUndefinedBehavior())
return false;
}

S.Stk.push<FixedPoint>(Result);
return true;
}

/// Like Cast(), but we cast to an arbitrary-bitwidth integral, so we need
/// to know what bitwidth the result should be.
template <PrimType Name, class T = typename PrimConv<Name>::T>
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ByteCode/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,10 @@ def CastFP : Opcode {
let Args = [ArgFltSemantics, ArgRoundingMode];
}

def CastFixedPoint : Opcode {
let Args = [ArgUint32];
}

def FixedSizeIntegralTypes : TypeClass {
let Types = [Uint8, Sint8, Uint16, Sint16, Uint32, Sint32, Uint64, Sint64, Bool];
}
Expand Down
6 changes: 6 additions & 0 deletions clang/test/AST/ByteCode/fixed-point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ namespace BinOps {
// ref-error {{is not an integral constant expression}} \
// ref-note {{is outside the range of representable values}}
}

namespace FixedPointCasts {
constexpr _Fract B = 0.3;
constexpr _Accum A = B;
constexpr _Fract C = A;
}
Loading