[clang] 89e44e3 - [clang][Interp] Implement __builtin_fmax

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 1 08:45:57 PDT 2023


Author: Timm Bäder
Date: 2023-08-01T17:45:24+02:00
New Revision: 89e44e33edeca2fdfa1992096cd5faf98a1f7dae

URL: https://github.com/llvm/llvm-project/commit/89e44e33edeca2fdfa1992096cd5faf98a1f7dae
DIFF: https://github.com/llvm/llvm-project/commit/89e44e33edeca2fdfa1992096cd5faf98a1f7dae.diff

LOG: [clang][Interp] Implement __builtin_fmax

Differential Revision: https://reviews.llvm.org/D155401

Added: 
    

Modified: 
    clang/lib/AST/Interp/InterpBuiltin.cpp
    clang/test/Sema/constant-builtins-fmax.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/InterpBuiltin.cpp b/clang/lib/AST/Interp/InterpBuiltin.cpp
index 0f269ae5a329df..2632ec7224379e 100644
--- a/clang/lib/AST/Interp/InterpBuiltin.cpp
+++ b/clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -173,6 +173,26 @@ static bool interp__builtin_fmin(InterpState &S, CodePtr OpPC,
   return true;
 }
 
+static bool interp__builtin_fmax(InterpState &S, CodePtr OpPC,
+                                 const InterpFrame *Frame,
+                                 const Function *Func) {
+  const Floating &LHS = getParam<Floating>(Frame, 0);
+  const Floating &RHS = getParam<Floating>(Frame, 1);
+
+  Floating Result;
+
+  // When comparing zeroes, return +0.0 if one of the zeroes is positive.
+  if (LHS.isZero() && RHS.isZero() && LHS.isNegative())
+    Result = RHS;
+  else if (LHS.isNan() || RHS > LHS)
+    Result = RHS;
+  else
+    Result = LHS;
+
+  S.Stk.push<Floating>(Result);
+  return true;
+}
+
 /// Defined as __builtin_isnan(...), to accommodate the fact that it can
 /// take a float, double, long double, etc.
 /// But for us, that's all a Floating anyway.
@@ -341,6 +361,15 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
       return Ret<PT_Float>(S, OpPC, Dummy);
     break;
 
+  case Builtin::BI__builtin_fmax:
+  case Builtin::BI__builtin_fmaxf:
+  case Builtin::BI__builtin_fmaxl:
+  case Builtin::BI__builtin_fmaxf16:
+  case Builtin::BI__builtin_fmaxf128:
+    if (interp__builtin_fmax(S, OpPC, Frame, F))
+      return Ret<PT_Float>(S, OpPC, Dummy);
+    break;
+
   case Builtin::BI__builtin_isnan:
     if (interp__builtin_isnan(S, OpPC, Frame, F))
       return Ret<PT_Sint32>(S, OpPC, Dummy);

diff  --git a/clang/test/Sema/constant-builtins-fmax.cpp b/clang/test/Sema/constant-builtins-fmax.cpp
index c2fd97a4df63a0..24ff482d0cdd4e 100644
--- a/clang/test/Sema/constant-builtins-fmax.cpp
+++ b/clang/test/Sema/constant-builtins-fmax.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
 // expected-no-diagnostics
 
 constexpr double NaN = __builtin_nan("");


        


More information about the cfe-commits mailing list