Skip to content

[clang][bytecode] Implement fixed-point-to-float casts #110369

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

case CK_ToVoid:
return discard(SubExpr);
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ByteCode/FixedPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class FixedPoint final {
unsigned bitWidth() const { return V.getWidth(); }
bool isSigned() const { return V.isSigned(); }

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

ComparisonCategoryResult compare(const FixedPoint &Other) const {
if (Other.V == V)
return ComparisonCategoryResult::Equal;
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,14 @@ static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
return true;
}

static inline bool CastFixedPointFloating(InterpState &S, CodePtr OpPC,
const llvm::fltSemantics *Sem) {
const auto &Fixed = S.Stk.pop<FixedPoint>();

S.Stk.push<Floating>(Fixed.toFloat(Sem));
return true;
}

static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
const auto &Ptr = S.Stk.peek<Pointer>();

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 @@ -682,6 +682,9 @@ def CastIntegralFixedPoint : Opcode {
def CastFloatingFixedPoint : Opcode {
let Args = [ArgUint32];
}
def CastFixedPointFloating : Opcode {
let Args = [ArgFltSemantics];
}

def PtrPtrCast : Opcode {
let Args = [ArgBool];
Expand Down
3 changes: 3 additions & 0 deletions clang/test/AST/ByteCode/fixed-point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ namespace FloatToFixedPointCast {
// both-note {{outside the range of representable values of type 'const _Fract'}}

constexpr _Fract sf2 = 0.5;
static_assert(sf2 == 0.5);
constexpr float sf2f = sf2;
static_assert(sf2f == 0.5);
}
Loading