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

Conversation

tbaederr
Copy link
Contributor

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 28, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 28, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/110369.diff

5 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Compiler.cpp (+6)
  • (modified) clang/lib/AST/ByteCode/FixedPoint.h (+4)
  • (modified) clang/lib/AST/ByteCode/Interp.h (+8)
  • (modified) clang/lib/AST/ByteCode/Opcodes.td (+3)
  • (modified) clang/test/AST/ByteCode/fixed-point.cpp (+3)
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);
 }

@tbaederr tbaederr merged commit 3a5b9da into llvm:main Sep 28, 2024
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants