Skip to content

Commit 3a5b9da

Browse files
authored
[clang][bytecode] Implement fixed-point-to-float casts (#110369)
1 parent 5bc673d commit 3a5b9da

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,12 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
691691
std::memcpy(&I, &Sem, sizeof(Sem));
692692
return this->emitCastFloatingFixedPoint(I, CE);
693693
}
694+
case CK_FixedPointToFloating: {
695+
if (!this->visit(SubExpr))
696+
return false;
697+
const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
698+
return this->emitCastFixedPointFloating(TargetSemantics, CE);
699+
}
694700

695701
case CK_ToVoid:
696702
return discard(SubExpr);

clang/lib/AST/ByteCode/FixedPoint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class FixedPoint final {
5252
unsigned bitWidth() const { return V.getWidth(); }
5353
bool isSigned() const { return V.isSigned(); }
5454

55+
llvm::APFloat toFloat(const llvm::fltSemantics *Sem) const {
56+
return V.convertToFloat(*Sem);
57+
}
58+
5559
ComparisonCategoryResult compare(const FixedPoint &Other) const {
5660
if (Other.V == V)
5761
return ComparisonCategoryResult::Equal;

clang/lib/AST/ByteCode/Interp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,14 @@ static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
23672367
return true;
23682368
}
23692369

2370+
static inline bool CastFixedPointFloating(InterpState &S, CodePtr OpPC,
2371+
const llvm::fltSemantics *Sem) {
2372+
const auto &Fixed = S.Stk.pop<FixedPoint>();
2373+
2374+
S.Stk.push<Floating>(Fixed.toFloat(Sem));
2375+
return true;
2376+
}
2377+
23702378
static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
23712379
const auto &Ptr = S.Stk.peek<Pointer>();
23722380

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ def CastIntegralFixedPoint : Opcode {
682682
def CastFloatingFixedPoint : Opcode {
683683
let Args = [ArgUint32];
684684
}
685+
def CastFixedPointFloating : Opcode {
686+
let Args = [ArgFltSemantics];
687+
}
685688

686689
def PtrPtrCast : Opcode {
687690
let Args = [ArgBool];

clang/test/AST/ByteCode/fixed-point.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ namespace FloatToFixedPointCast {
3131
// both-note {{outside the range of representable values of type 'const _Fract'}}
3232

3333
constexpr _Fract sf2 = 0.5;
34+
static_assert(sf2 == 0.5);
35+
constexpr float sf2f = sf2;
36+
static_assert(sf2f == 0.5);
3437
}

0 commit comments

Comments
 (0)