Skip to content

[clang][bytecode] Implement floating-to-fixed-point casts #110361

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 28, 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
9 changes: 9 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,15 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()), I,
CE);
}
case CK_FloatingToFixedPoint: {
if (!this->visit(SubExpr))
return false;

auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
uint32_t I;
std::memcpy(&I, &Sem, sizeof(Sem));
return this->emitCastFloatingFixedPoint(I, CE);
}

case CK_ToVoid:
return discard(SubExpr);
Expand Down
35 changes: 31 additions & 4 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2321,22 +2321,49 @@ static inline bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC,
std::memcpy(&Sem, &FPS, sizeof(Sem));

bool Overflow;
llvm::APFixedPoint IntResult =
llvm::APFixedPoint Result =
llvm::APFixedPoint::getFromIntValue(Int.toAPSInt(), Sem, &Overflow);

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

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

static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
uint32_t FPS) {
const auto &Float = S.Stk.pop<Floating>();

FixedPointSemantics Sem(0, 0, false, false, false);
std::memcpy(&Sem, &FPS, sizeof(Sem));

bool Overflow;
llvm::APFixedPoint Result =
llvm::APFixedPoint::getFromFloatValue(Float.getAPFloat(), Sem, &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.toString() << E->getType();
}
S.CCEDiag(E, diag::note_constexpr_overflow) << Result << E->getType();
if (!S.noteUndefinedBehavior())
return false;
}

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

Expand Down
3 changes: 3 additions & 0 deletions clang/lib/AST/ByteCode/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ def CastIntegralFixedPoint : Opcode {
let Args = [ArgUint32];
let HasGroup = 1;
}
def CastFloatingFixedPoint : Opcode {
let Args = [ArgUint32];
}

def PtrPtrCast : Opcode {
let Args = [ArgBool];
Expand Down
7 changes: 7 additions & 0 deletions clang/test/AST/ByteCode/fixed-point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ namespace IntToFixedPointCast {
static_assert(sf == -1.0k);
static_assert(sf == -1);
}

namespace FloatToFixedPointCast {
constexpr _Fract sf = 1.0; // both-error {{must be initialized by a constant expression}} \
// both-note {{outside the range of representable values of type 'const _Fract'}}

constexpr _Fract sf2 = 0.5;
}
Loading