[PATCH] D155371: [clang][Interp] Implement __builtin_isinf

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 15 08:49:29 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, shafik, cor3ntin.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155371

Files:
  clang/lib/AST/Interp/Floating.h
  clang/lib/AST/Interp/Function.h
  clang/lib/AST/Interp/InterpBuiltin.cpp
  clang/test/AST/Interp/builtin-functions.cpp


Index: clang/test/AST/Interp/builtin-functions.cpp
===================================================================
--- clang/test/AST/Interp/builtin-functions.cpp
+++ clang/test/AST/Interp/builtin-functions.cpp
@@ -62,3 +62,8 @@
   constexpr float Nan2 = __builtin_nans([](){return "0xAE98";}()); // ref-error {{must be initialized by a constant expression}}
 
 }
+
+namespace inf {
+  static_assert(__builtin_isinf(__builtin_inf()), "");
+  static_assert(!__builtin_isinf(1.0), "");
+}
Index: clang/lib/AST/Interp/InterpBuiltin.cpp
===================================================================
--- clang/lib/AST/Interp/InterpBuiltin.cpp
+++ clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -172,6 +172,20 @@
   return true;
 }
 
+static bool interp__builtin_isinf(InterpState &S, CodePtr OpPC,
+                                  const InterpFrame *Frame, const Function *F,
+                                  bool CheckSign) {
+  const Floating &Arg = S.Stk.peek<Floating>();
+  bool IsInf = Arg.isInf();
+
+  if (CheckSign)
+    S.Stk.push<Integral<32, true>>(
+        Integral<32, true>::from(IsInf ? (Arg.isNegative() ? -1 : 1) : 0));
+  else
+    S.Stk.push<Integral<32, true>>(Integral<32, true>::from(Arg.isInf()));
+  return true;
+}
+
 bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
   InterpFrame *Frame = S.Current;
   APValue Dummy;
@@ -232,6 +246,16 @@
       return Ret<PT_Sint32>(S, OpPC, Dummy);
     break;
 
+  case Builtin::BI__builtin_isinf:
+    if (interp__builtin_isinf(S, OpPC, Frame, F, /*Sign=*/false))
+      return Ret<PT_Sint32>(S, OpPC, Dummy);
+    break;
+
+  case Builtin::BI__builtin_isinf_sign:
+    if (interp__builtin_isinf(S, OpPC, Frame, F, /*Sign=*/true))
+      return Ret<PT_Sint32>(S, OpPC, Dummy);
+    break;
+
   default:
     return false;
   }
Index: clang/lib/AST/Interp/Function.h
===================================================================
--- clang/lib/AST/Interp/Function.h
+++ clang/lib/AST/Interp/Function.h
@@ -177,7 +177,7 @@
     if (!isBuiltin())
       return false;
     // FIXME: Is there a better way to get this information?
-    if (getName() == "__builtin_isnan")
+    if (getName() == "__builtin_isnan" || getName() == "__builtin_isinf")
       return true;
     return false;
   }
Index: clang/lib/AST/Interp/Floating.h
===================================================================
--- clang/lib/AST/Interp/Floating.h
+++ clang/lib/AST/Interp/Floating.h
@@ -94,6 +94,7 @@
   bool isMin() const { return F.isSmallest(); }
   bool isMinusOne() const { return F.isExactlyValue(-1.0); }
   bool isNan() const { return F.isNaN(); }
+  bool isInf() const { return F.isInfinity(); }
   bool isFinite() const { return F.isFinite(); }
 
   ComparisonCategoryResult compare(const Floating &RHS) const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155371.540690.patch
Type: text/x-patch
Size: 2805 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230715/9b14efce/attachment.bin>


More information about the cfe-commits mailing list