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

This commit is contained in:
Timm Baeder 2024-09-28 19:11:49 +02:00 committed by GitHub
parent 5bc673d371
commit 3a5b9da115
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 24 additions and 0 deletions

View File

@ -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);

View File

@ -52,6 +52,10 @@ public:
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;

View File

@ -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>();

View File

@ -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];

View File

@ -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);
}