https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/110369
None >From af2fe551a5324cd1274e1da8d6a14303646ae5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com> Date: Sat, 28 Sep 2024 16:02:56 +0200 Subject: [PATCH] [clang][bytecode] Implement fixed-point-to-float casts --- clang/lib/AST/ByteCode/Compiler.cpp | 6 ++++++ clang/lib/AST/ByteCode/FixedPoint.h | 4 ++++ clang/lib/AST/ByteCode/Interp.h | 8 ++++++++ clang/lib/AST/ByteCode/Opcodes.td | 3 +++ clang/test/AST/ByteCode/fixed-point.cpp | 3 +++ 5 files changed, 24 insertions(+) diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 40cb1b2dd80f96..77bfb3d8cd7f1a 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -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); diff --git a/clang/lib/AST/ByteCode/FixedPoint.h b/clang/lib/AST/ByteCode/FixedPoint.h index daa62945346aeb..fb8457a08e93cc 100644 --- a/clang/lib/AST/ByteCode/FixedPoint.h +++ b/clang/lib/AST/ByteCode/FixedPoint.h @@ -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; diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index 23405765d8de82..0a99d9440ff844 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -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>(); diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td index 240e00e59d97ec..4f11b927989004 100644 --- a/clang/lib/AST/ByteCode/Opcodes.td +++ b/clang/lib/AST/ByteCode/Opcodes.td @@ -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]; diff --git a/clang/test/AST/ByteCode/fixed-point.cpp b/clang/test/AST/ByteCode/fixed-point.cpp index 76da06a02e150c..68137618b5db60 100644 --- a/clang/test/AST/ByteCode/fixed-point.cpp +++ b/clang/test/AST/ByteCode/fixed-point.cpp @@ -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); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits