[clang][bytecode] Implement fixed-point-to-float casts (#110369)
This commit is contained in:
parent
5bc673d371
commit
3a5b9da115
@ -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);
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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>();
|
||||
|
||||
|
||||
@ -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];
|
||||
|
||||
@ -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…
x
Reference in New Issue
Block a user