[clang] [C++23] [CLANG] Adding C++23 constexpr math functions: fmin and frexp. (PR #88978)

Zahira Ammarguellat via cfe-commits cfe-commits at lists.llvm.org
Wed May 8 10:29:01 PDT 2024


https://github.com/zahiraam updated https://github.com/llvm/llvm-project/pull/88978

>From 3acc848f4fcc68445dfc849f9c6f8d384d3692af Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Tue, 16 Apr 2024 13:09:58 -0700
Subject: [PATCH 1/4] Adding C23 constexpr math functions fmin and frexp.

---
 clang/include/clang/Basic/Builtins.td |  4 +--
 clang/lib/AST/ExprConstant.cpp        | 16 ++++++++-
 clang/test/CodeGen/constexpr-math.cpp | 51 +++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeGen/constexpr-math.cpp

diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 52c0dd52c28b1..a35c77286229f 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -3440,7 +3440,7 @@ def Fmod : FPMathTemplate, LibBuiltin<"math.h"> {
 
 def Frexp : FPMathTemplate, LibBuiltin<"math.h"> {
   let Spellings = ["frexp"];
-  let Attributes = [NoThrow];
+  let Attributes = [NoThrow, Constexpr];
   let Prototype = "T(T, int*)";
   let AddBuiltinPrefixedAlias = 1;
 }
@@ -3618,7 +3618,7 @@ def Fmax : FPMathTemplate, LibBuiltin<"math.h"> {
 
 def Fmin : FPMathTemplate, LibBuiltin<"math.h"> {
   let Spellings = ["fmin"];
-  let Attributes = [NoThrow, Const];
+  let Attributes = [NoThrow, Const, Constexpr];
   let Prototype = "T(T, T)";
   let AddBuiltinPrefixedAlias = 1;
   let OnlyBuiltinPrefixedAliasIsConstexpr = 1;
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 5a36621dc5cce..506621ac7e9c1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2922,7 +2922,7 @@ static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E,
   //   If during the evaluation of an expression, the result is not
   //   mathematically defined [...], the behavior is undefined.
   // FIXME: C++ rules require us to not conform to IEEE 754 here.
-  if (LHS.isNaN()) {
+  if (!Info.getLangOpts().CPlusPlus23 && LHS.isNaN()) {
     Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN();
     return Info.noteUndefinedBehavior();
   }
@@ -14547,6 +14547,18 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
   default:
     return false;
 
+  case Builtin::BI__builtin_frexpf:
+  case Builtin::BI__builtin_frexp: {
+    LValue Pointer;
+    if (!EvaluateFloat(E->getArg(0), Result, Info) ||
+        !EvaluatePointer(E->getArg(1), Pointer, Info))
+      return false;
+    llvm::RoundingMode RM =
+        E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).getRoundingMode();
+    int FrexpExp;
+    Result = llvm::frexp(Result, FrexpExp, RM);
+    return true;
+  }
   case Builtin::BI__builtin_huge_val:
   case Builtin::BI__builtin_huge_valf:
   case Builtin::BI__builtin_huge_vall:
@@ -14638,6 +14650,8 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
     return true;
   }
 
+  case Builtin::BIfmin:
+  case Builtin::BIfminf:
   case Builtin::BI__builtin_fmin:
   case Builtin::BI__builtin_fminf:
   case Builtin::BI__builtin_fminl:
diff --git a/clang/test/CodeGen/constexpr-math.cpp b/clang/test/CodeGen/constexpr-math.cpp
new file mode 100644
index 0000000000000..446bf3f4f7a50
--- /dev/null
+++ b/clang/test/CodeGen/constexpr-math.cpp
@@ -0,0 +1,51 @@
+// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -std=c++23 \
+// RUN: -emit-llvm -o - %s | FileCheck %s
+
+// RUN %clang_cc1 -x c++ -triple x86_64-linux-gnu -emit-llvm -o - %s \
+// RUN -std=c++23
+
+#define INFINITY ((float)(1e+300 * 1e+300))
+#define NAN      (-(float)(INFINITY * 0.0F))
+
+//constexpr double frexp ( double num, int* exp );
+//constexpr float foo ( float num, int* exp );
+
+int func()
+{
+  int i;
+
+  // fmin
+  constexpr double f1 = __builtin_fmin(15.24, 1.3);
+  constexpr double f2 = __builtin_fmin(-0.0, +0.0);
+  constexpr double f3 = __builtin_fmin(+0.0, -0.0);
+  constexpr float f4 = __builtin_fminf(NAN, NAN);
+  constexpr float f5 = __builtin_fminf(NAN, -1);
+  constexpr float f6 = __builtin_fminf(-INFINITY, 0);
+  constexpr float f7 = __builtin_fminf(INFINITY, 0);
+
+  // frexp
+  constexpr double f8 = __builtin_frexp(123.45, &i);
+  constexpr double f9 = __builtin_frexp(0.0, &i);
+  constexpr double f10 = __builtin_frexp(-0.0, &i);
+  constexpr double f11 = __builtin_frexpf(NAN, &i);
+  constexpr double f12 = __builtin_frexpf(-NAN, &i);
+  constexpr double f13 = __builtin_frexpf(INFINITY, &i);
+  constexpr double f14 = __builtin_frexpf(INFINITY, &i);
+
+  return 0;
+}
+
+// CHECK: store double 1.300000e+00, ptr {{.*}}
+// CHECK: store double -0.000000e+00, ptr {{.*}}
+// CHECK: store double -0.000000e+00, ptr {{.*}}
+// CHECK: store float 0xFFF8000000000000, ptr {{.*}}
+// CHECK: store float -1.000000e+00, ptr {{.*}}
+// CHECK: store float 0xFFF0000000000000, ptr {{.*}}
+
+// CHECK: store double 0x3FEEDCCCCCCCCCCD, ptr {{.*}}
+// CHECK: store double 0.000000e+00, ptr {{.*}}
+// CHECK: store double -0.000000e+00, ptr {{.*}}
+// CHECK: store double 0xFFF8000000000000, ptr {{.*}}
+// CHECK: store double 0x7FF8000000000000, ptr {{.*}}
+// CHECK: store double 0x7FF0000000000000, ptr {{.*}}
+// CHECK: store double 0x7FF0000000000000, ptr {{.*}}

>From d7050b53a75a23024502a9e4f56b385256c4722f Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Mon, 22 Apr 2024 13:32:56 -0700
Subject: [PATCH 2/4] Addressed review comments.

---
 clang/lib/AST/ExprConstant.cpp                |  3 +-
 .../constexpr-math.cpp                        | 20 ++++---
 clang/test/SemaCXX/constexpr-math.cpp         | 57 +++++++++++++++++++
 3 files changed, 70 insertions(+), 10 deletions(-)
 rename clang/test/{CodeGen => CodeGenCXX}/constexpr-math.cpp (75%)
 create mode 100644 clang/test/SemaCXX/constexpr-math.cpp

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 506621ac7e9c1..1a6abb386071c 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14553,8 +14553,7 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
     if (!EvaluateFloat(E->getArg(0), Result, Info) ||
         !EvaluatePointer(E->getArg(1), Pointer, Info))
       return false;
-    llvm::RoundingMode RM =
-        E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).getRoundingMode();
+    llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
     int FrexpExp;
     Result = llvm::frexp(Result, FrexpExp, RM);
     return true;
diff --git a/clang/test/CodeGen/constexpr-math.cpp b/clang/test/CodeGenCXX/constexpr-math.cpp
similarity index 75%
rename from clang/test/CodeGen/constexpr-math.cpp
rename to clang/test/CodeGenCXX/constexpr-math.cpp
index 446bf3f4f7a50..ad7b6779a4ae0 100644
--- a/clang/test/CodeGen/constexpr-math.cpp
+++ b/clang/test/CodeGenCXX/constexpr-math.cpp
@@ -1,14 +1,16 @@
 // RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -std=c++23 \
-// RUN: -emit-llvm -o - %s | FileCheck %s
+// RUN: -DWIN -emit-llvm -o - %s | FileCheck %s --check-prefixes=WIN
 
-// RUN %clang_cc1 -x c++ -triple x86_64-linux-gnu -emit-llvm -o - %s \
-// RUN -std=c++23
+// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -std=c++23 \
+// RUN: -emit-llvm -o - %s | FileCheck %s --check-prefixes=LNX
 
+#ifdef WIN
 #define INFINITY ((float)(1e+300 * 1e+300))
 #define NAN      (-(float)(INFINITY * 0.0F))
-
-//constexpr double frexp ( double num, int* exp );
-//constexpr float foo ( float num, int* exp );
+#else
+#define NAN (__builtin_nanf(""))
+#define INFINITY (__builtin_inff())
+#endif
 
 int func()
 {
@@ -38,7 +40,8 @@ int func()
 // CHECK: store double 1.300000e+00, ptr {{.*}}
 // CHECK: store double -0.000000e+00, ptr {{.*}}
 // CHECK: store double -0.000000e+00, ptr {{.*}}
-// CHECK: store float 0xFFF8000000000000, ptr {{.*}}
+// WIN: store float 0xFFF8000000000000, ptr {{.*}}
+// LNX: store float 0x7FF8000000000000, ptr {{.*}}
 // CHECK: store float -1.000000e+00, ptr {{.*}}
 // CHECK: store float 0xFFF0000000000000, ptr {{.*}}
 
@@ -46,6 +49,7 @@ int func()
 // CHECK: store double 0.000000e+00, ptr {{.*}}
 // CHECK: store double -0.000000e+00, ptr {{.*}}
 // CHECK: store double 0xFFF8000000000000, ptr {{.*}}
-// CHECK: store double 0x7FF8000000000000, ptr {{.*}}
+// WIN: store double 0x7FF8000000000000, ptr {{.*}}
+// LNX: store double 0xFFF8000000000000, ptr {{.*}}
 // CHECK: store double 0x7FF0000000000000, ptr {{.*}}
 // CHECK: store double 0x7FF0000000000000, ptr {{.*}}
diff --git a/clang/test/SemaCXX/constexpr-math.cpp b/clang/test/SemaCXX/constexpr-math.cpp
new file mode 100644
index 0000000000000..f51ea1bcaf7ba
--- /dev/null
+++ b/clang/test/SemaCXX/constexpr-math.cpp
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -DWIN -verify -std=c++23 -fsyntax-only  %s
+// RUN: %clang_cc1 -verify -std=c++23 -fsyntax-only  %s
+
+// expected-no-diagnostics
+
+
+#ifdef WIN
+#define INFINITY ((float)(1e+300 * 1e+300))
+#define NAN      (-(float)(INFINITY * 0.0F))
+#else
+#define NAN (__builtin_nanf(""))
+#define INFINITY (__builtin_inff())
+#endif
+
+extern "C" void abort() noexcept;
+extern "C" int write(int, const void*, unsigned long);
+
+#define assert(condition) \
+  do {			    \
+    if (!(condition)) {					\
+      write(2, "Assertion failed: ", 18);		\
+      write(2, #condition, sizeof(#condition) - 1);	\
+      write(2, "\n", 1);				\
+      abort();						\
+    }							\
+  } while (false)
+
+int main() {
+    int i;
+
+    // fmin
+    static_assert(__builtin_fmin(15.24, 1.3) == 1.3, "");
+    static_assert(__builtin_fmin(-0.0, +0.0) == -0.0, "");
+    static_assert(__builtin_fmin(+0.0, -0.0) == -0.0, "");
+    assert(__builtin_isnan(__builtin_fminf(NAN,NAN)));
+    assert(__builtin_isnan(__builtin_fminf(NAN, -1)));
+    assert(__builtin_isnan(__builtin_fminf(-INFINITY, 0)));
+    assert(__builtin_iszero(__builtin_fminf(+INFINITY, 0)));
+
+    // frexp
+    static_assert(__builtin_frexp(123.45, &i) == 0.96445312500000002);
+    static_assert(!__builtin_isnan(__builtin_frexp(123.45, &i)), "");
+    assert(i==0);
+    static_assert(__builtin_iszero(__builtin_frexp(0.0, &i)), "");
+    assert(i==0);
+    static_assert(__builtin_iszero(__builtin_frexp(-0.0, &i)), "");
+    assert(i==0);
+    assert(__builtin_isnan(__builtin_frexp(NAN, &i)));
+    assert(i==0);
+    assert(__builtin_isnan(__builtin_frexp(-NAN, &i)));
+    assert(i==0);
+    assert(!__builtin_isfinite(__builtin_frexp(INFINITY, &i)));
+    assert(i==0);
+    assert(!__builtin_isfinite(__builtin_frexp(-INFINITY, &i)));
+    assert(i==0);
+    return 0;
+}

>From 625028aed5ad85922f082f5192abd5ceedb0b68d Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Wed, 1 May 2024 08:44:50 -0700
Subject: [PATCH 3/4] Responded to review comments.

---
 clang/include/clang/Basic/Builtins.td    |  1 +
 clang/lib/AST/ExprConstant.cpp           |  8 +++--
 clang/test/CodeGenCXX/constexpr-math.cpp | 16 ++++++----
 clang/test/SemaCXX/constexpr-math.cpp    | 37 ++++++------------------
 4 files changed, 26 insertions(+), 36 deletions(-)

diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index a35c77286229f..419e623c7bc04 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -3443,6 +3443,7 @@ def Frexp : FPMathTemplate, LibBuiltin<"math.h"> {
   let Attributes = [NoThrow, Constexpr];
   let Prototype = "T(T, int*)";
   let AddBuiltinPrefixedAlias = 1;
+  let OnlyBuiltinPrefixedAliasIsConstexpr = 1;
 }
 
 def Ldexp : FPMathTemplate, LibBuiltin<"math.h"> {
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 1a6abb386071c..4e664905969ee 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14547,15 +14547,18 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
   default:
     return false;
 
+  case Builtin::BI__builtin_frexp:
   case Builtin::BI__builtin_frexpf:
-  case Builtin::BI__builtin_frexp: {
+  case Builtin::BI__builtin_frexpl: {
     LValue Pointer;
     if (!EvaluateFloat(E->getArg(0), Result, Info) ||
         !EvaluatePointer(E->getArg(1), Pointer, Info))
       return false;
     llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
     int FrexpExp;
-    Result = llvm::frexp(Result, FrexpExp, RM);
+    FrexpExp = ilogb(Result);
+    FrexpExp = FrexpExp == llvm::detail::IEEEFloat::IEK_Zero ? 0 : FrexpExp + 1;
+    Result =  scalbn(Result, -FrexpExp, RM);
     return true;
   }
   case Builtin::BI__builtin_huge_val:
@@ -14651,6 +14654,7 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
 
   case Builtin::BIfmin:
   case Builtin::BIfminf:
+  case Builtin::BIfminl:
   case Builtin::BI__builtin_fmin:
   case Builtin::BI__builtin_fminf:
   case Builtin::BI__builtin_fminl:
diff --git a/clang/test/CodeGenCXX/constexpr-math.cpp b/clang/test/CodeGenCXX/constexpr-math.cpp
index ad7b6779a4ae0..a48c312d05af0 100644
--- a/clang/test/CodeGenCXX/constexpr-math.cpp
+++ b/clang/test/CodeGenCXX/constexpr-math.cpp
@@ -24,15 +24,17 @@ int func()
   constexpr float f5 = __builtin_fminf(NAN, -1);
   constexpr float f6 = __builtin_fminf(-INFINITY, 0);
   constexpr float f7 = __builtin_fminf(INFINITY, 0);
+  constexpr long double f8 = __builtin_fminl(123.456L, 789.012L);
 
   // frexp
-  constexpr double f8 = __builtin_frexp(123.45, &i);
-  constexpr double f9 = __builtin_frexp(0.0, &i);
-  constexpr double f10 = __builtin_frexp(-0.0, &i);
-  constexpr double f11 = __builtin_frexpf(NAN, &i);
-  constexpr double f12 = __builtin_frexpf(-NAN, &i);
-  constexpr double f13 = __builtin_frexpf(INFINITY, &i);
+  constexpr double f9 = __builtin_frexp(123.45, &i);
+  constexpr double f10 = __builtin_frexp(0.0, &i);
+  constexpr double f11 = __builtin_frexp(-0.0, &i);
+  constexpr double f12 = __builtin_frexpf(NAN, &i);
+  constexpr double f13 = __builtin_frexpf(-NAN, &i);
   constexpr double f14 = __builtin_frexpf(INFINITY, &i);
+  constexpr double f15 = __builtin_frexpf(INFINITY, &i);
+  constexpr long double f16 = __builtin_frexpl(259.328L, &i);
 
   return 0;
 }
@@ -44,6 +46,7 @@ int func()
 // LNX: store float 0x7FF8000000000000, ptr {{.*}}
 // CHECK: store float -1.000000e+00, ptr {{.*}}
 // CHECK: store float 0xFFF0000000000000, ptr {{.*}}
+// CHECK: store double 1.234560e+02, ptr {{.*}}
 
 // CHECK: store double 0x3FEEDCCCCCCCCCCD, ptr {{.*}}
 // CHECK: store double 0.000000e+00, ptr {{.*}}
@@ -53,3 +56,4 @@ int func()
 // LNX: store double 0xFFF8000000000000, ptr {{.*}}
 // CHECK: store double 0x7FF0000000000000, ptr {{.*}}
 // CHECK: store double 0x7FF0000000000000, ptr {{.*}}
+// CHECK: store double 5.065000e-01, ptr {{.*}}
diff --git a/clang/test/SemaCXX/constexpr-math.cpp b/clang/test/SemaCXX/constexpr-math.cpp
index f51ea1bcaf7ba..24e5bf70e3535 100644
--- a/clang/test/SemaCXX/constexpr-math.cpp
+++ b/clang/test/SemaCXX/constexpr-math.cpp
@@ -12,19 +12,6 @@
 #define INFINITY (__builtin_inff())
 #endif
 
-extern "C" void abort() noexcept;
-extern "C" int write(int, const void*, unsigned long);
-
-#define assert(condition) \
-  do {			    \
-    if (!(condition)) {					\
-      write(2, "Assertion failed: ", 18);		\
-      write(2, #condition, sizeof(#condition) - 1);	\
-      write(2, "\n", 1);				\
-      abort();						\
-    }							\
-  } while (false)
-
 int main() {
     int i;
 
@@ -32,26 +19,20 @@ int main() {
     static_assert(__builtin_fmin(15.24, 1.3) == 1.3, "");
     static_assert(__builtin_fmin(-0.0, +0.0) == -0.0, "");
     static_assert(__builtin_fmin(+0.0, -0.0) == -0.0, "");
-    assert(__builtin_isnan(__builtin_fminf(NAN,NAN)));
-    assert(__builtin_isnan(__builtin_fminf(NAN, -1)));
-    assert(__builtin_isnan(__builtin_fminf(-INFINITY, 0)));
-    assert(__builtin_iszero(__builtin_fminf(+INFINITY, 0)));
+    static_assert(__builtin_fminf(NAN, -1) == -1, "");
+    static_assert(__builtin_fminf(+INFINITY, 0) == 0, "");
+    static_assert(__builtin_isinf(__builtin_fminf(-INFINITY, 0)), "");
+    static_assert(__builtin_isnan(__builtin_fminf(NAN,NAN)), "");
 
     // frexp
     static_assert(__builtin_frexp(123.45, &i) == 0.96445312500000002);
     static_assert(!__builtin_isnan(__builtin_frexp(123.45, &i)), "");
-    assert(i==0);
     static_assert(__builtin_iszero(__builtin_frexp(0.0, &i)), "");
-    assert(i==0);
     static_assert(__builtin_iszero(__builtin_frexp(-0.0, &i)), "");
-    assert(i==0);
-    assert(__builtin_isnan(__builtin_frexp(NAN, &i)));
-    assert(i==0);
-    assert(__builtin_isnan(__builtin_frexp(-NAN, &i)));
-    assert(i==0);
-    assert(!__builtin_isfinite(__builtin_frexp(INFINITY, &i)));
-    assert(i==0);
-    assert(!__builtin_isfinite(__builtin_frexp(-INFINITY, &i)));
-    assert(i==0);
+    static_assert(__builtin_isnan(__builtin_frexp(NAN, &i)));
+    static_assert(__builtin_isnan(__builtin_frexp(-NAN, &i)));
+    static_assert(!__builtin_isfinite(__builtin_frexp(INFINITY, &i)));
+    static_assert(!__builtin_isfinite(__builtin_frexp(-INFINITY, &i)));
+
     return 0;
 }

>From 319fdd0867bdd9e7a9decfba8dc2f1be6e375a19 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Wed, 8 May 2024 10:27:54 -0700
Subject: [PATCH 4/4] Added code to store the exponent in Pointer. Edited LIT
 tests as requested by reviewer.

---
 clang/lib/AST/ExprConstant.cpp           |  22 ++++-
 clang/test/CodeGenCXX/constexpr-math.cpp | 105 ++++++++++++++++++-----
 clang/test/SemaCXX/constexpr-math.cpp    |  42 ++++-----
 3 files changed, 123 insertions(+), 46 deletions(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 4e664905969ee..be68ef11efb38 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14481,6 +14481,8 @@ class FloatExprEvaluator
     return true;
   }
 
+  void StoreExponent(LValue Pointer, int exp);
+
   bool VisitCallExpr(const CallExpr *E);
 
   bool VisitUnaryOperator(const UnaryOperator *E);
@@ -14539,6 +14541,19 @@ static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
   return true;
 }
 
+void FloatExprEvaluator::StoreExponent(LValue Pointer, int exp) {
+  const APValue::LValueBase Base = Pointer.getLValueBase();
+  auto *VD = const_cast<ValueDecl *>(Base.dyn_cast<const ValueDecl *>());
+  if (auto *VarD = dyn_cast<VarDecl>(VD)) {
+    clang::IntegerLiteral *IL =
+        clang::IntegerLiteral::Create(Info.Ctx, llvm::APSInt(32, exp),
+                                      Info.Ctx.IntTy, clang::SourceLocation());
+    VarD->setInit(IL);
+  } else {
+    llvm_unreachable("expecting a VarDecl for an exponent");
+  }
+}
+
 bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
   if (!IsConstantEvaluatedBuiltinCall(E))
     return ExprEvaluatorBaseTy::VisitCallExpr(E);
@@ -14554,11 +14569,10 @@ bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
     if (!EvaluateFloat(E->getArg(0), Result, Info) ||
         !EvaluatePointer(E->getArg(1), Pointer, Info))
       return false;
-    llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
     int FrexpExp;
-    FrexpExp = ilogb(Result);
-    FrexpExp = FrexpExp == llvm::detail::IEEEFloat::IEK_Zero ? 0 : FrexpExp + 1;
-    Result =  scalbn(Result, -FrexpExp, RM);
+    llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
+    Result = llvm::frexp(Result, FrexpExp, RM);
+    StoreExponent(Pointer, FrexpExp);
     return true;
   }
   case Builtin::BI__builtin_huge_val:
diff --git a/clang/test/CodeGenCXX/constexpr-math.cpp b/clang/test/CodeGenCXX/constexpr-math.cpp
index a48c312d05af0..039d2f288ddfa 100644
--- a/clang/test/CodeGenCXX/constexpr-math.cpp
+++ b/clang/test/CodeGenCXX/constexpr-math.cpp
@@ -1,3 +1,4 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 4
 // RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -std=c++23 \
 // RUN: -DWIN -emit-llvm -o - %s | FileCheck %s --check-prefixes=WIN
 
@@ -12,6 +13,84 @@
 #define INFINITY (__builtin_inff())
 #endif
 
+// WIN-LABEL: define dso_local noundef i32 @_Z4funcv(
+// WIN-SAME: ) #[[ATTR0:[0-9]+]] {
+// WIN-NEXT:  entry:
+// WIN-NEXT:    [[I:%.*]] = alloca i32, align 4
+// WIN-NEXT:    [[F1:%.*]] = alloca double, align 8
+// WIN-NEXT:    [[F2:%.*]] = alloca double, align 8
+// WIN-NEXT:    [[F3:%.*]] = alloca double, align 8
+// WIN-NEXT:    [[F4:%.*]] = alloca float, align 4
+// WIN-NEXT:    [[F5:%.*]] = alloca float, align 4
+// WIN-NEXT:    [[F6:%.*]] = alloca float, align 4
+// WIN-NEXT:    [[F7:%.*]] = alloca float, align 4
+// WIN-NEXT:    [[F8:%.*]] = alloca x86_fp80, align 16
+// WIN-NEXT:    [[F9:%.*]] = alloca double, align 8
+// WIN-NEXT:    [[F10:%.*]] = alloca double, align 8
+// WIN-NEXT:    [[F11:%.*]] = alloca double, align 8
+// WIN-NEXT:    [[F12:%.*]] = alloca float, align 4
+// WIN-NEXT:    [[F13:%.*]] = alloca float, align 4
+// WIN-NEXT:    [[F14:%.*]] = alloca float, align 4
+// WIN-NEXT:    [[F15:%.*]] = alloca float, align 4
+// WIN-NEXT:    [[F16:%.*]] = alloca x86_fp80, align 16
+// WIN-NEXT:    store i32 0, ptr [[I]], align 4
+// WIN-NEXT:    store double 1.300000e+00, ptr [[F1]], align 8
+// WIN-NEXT:    store double -0.000000e+00, ptr [[F2]], align 8
+// WIN-NEXT:    store double -0.000000e+00, ptr [[F3]], align 8
+// WIN-NEXT:    store float 0xFFF8000000000000, ptr [[F4]], align 4
+// WIN-NEXT:    store float -1.000000e+00, ptr [[F5]], align 4
+// WIN-NEXT:    store float 0xFFF0000000000000, ptr [[F6]], align 4
+// WIN-NEXT:    store float 0.000000e+00, ptr [[F7]], align 4
+// WIN-NEXT:    store x86_fp80 0xK4005F6E978D4FDF3B646, ptr [[F8]], align 16
+// WIN-NEXT:    store double 0x3FEEDCCCCCCCCCCD, ptr [[F9]], align 8
+// WIN-NEXT:    store double 0.000000e+00, ptr [[F10]], align 8
+// WIN-NEXT:    store double -0.000000e+00, ptr [[F11]], align 8
+// WIN-NEXT:    store float 0xFFF8000000000000, ptr [[F12]], align 4
+// WIN-NEXT:    store float 0x7FF8000000000000, ptr [[F13]], align 4
+// WIN-NEXT:    store float 0x7FF0000000000000, ptr [[F14]], align 4
+// WIN-NEXT:    store float 0x7FF0000000000000, ptr [[F15]], align 4
+// WIN-NEXT:    store x86_fp80 0xK3FFE81A9FBE76C8B4396, ptr [[F16]], align 16
+// WIN-NEXT:    ret i32 0
+//
+// LNX-LABEL: define dso_local noundef i32 @_Z4funcv(
+// LNX-SAME: ) #[[ATTR0:[0-9]+]] {
+// LNX-NEXT:  entry:
+// LNX-NEXT:    [[I:%.*]] = alloca i32, align 4
+// LNX-NEXT:    [[F1:%.*]] = alloca double, align 8
+// LNX-NEXT:    [[F2:%.*]] = alloca double, align 8
+// LNX-NEXT:    [[F3:%.*]] = alloca double, align 8
+// LNX-NEXT:    [[F4:%.*]] = alloca float, align 4
+// LNX-NEXT:    [[F5:%.*]] = alloca float, align 4
+// LNX-NEXT:    [[F6:%.*]] = alloca float, align 4
+// LNX-NEXT:    [[F7:%.*]] = alloca float, align 4
+// LNX-NEXT:    [[F8:%.*]] = alloca x86_fp80, align 16
+// LNX-NEXT:    [[F9:%.*]] = alloca double, align 8
+// LNX-NEXT:    [[F10:%.*]] = alloca double, align 8
+// LNX-NEXT:    [[F11:%.*]] = alloca double, align 8
+// LNX-NEXT:    [[F12:%.*]] = alloca float, align 4
+// LNX-NEXT:    [[F13:%.*]] = alloca float, align 4
+// LNX-NEXT:    [[F14:%.*]] = alloca float, align 4
+// LNX-NEXT:    [[F15:%.*]] = alloca float, align 4
+// LNX-NEXT:    [[F16:%.*]] = alloca x86_fp80, align 16
+// LNX-NEXT:    store i32 0, ptr [[I]], align 4
+// LNX-NEXT:    store double 1.300000e+00, ptr [[F1]], align 8
+// LNX-NEXT:    store double -0.000000e+00, ptr [[F2]], align 8
+// LNX-NEXT:    store double -0.000000e+00, ptr [[F3]], align 8
+// LNX-NEXT:    store float 0x7FF8000000000000, ptr [[F4]], align 4
+// LNX-NEXT:    store float -1.000000e+00, ptr [[F5]], align 4
+// LNX-NEXT:    store float 0xFFF0000000000000, ptr [[F6]], align 4
+// LNX-NEXT:    store float 0.000000e+00, ptr [[F7]], align 4
+// LNX-NEXT:    store x86_fp80 0xK4005F6E978D4FDF3B646, ptr [[F8]], align 16
+// LNX-NEXT:    store double 0x3FEEDCCCCCCCCCCD, ptr [[F9]], align 8
+// LNX-NEXT:    store double 0.000000e+00, ptr [[F10]], align 8
+// LNX-NEXT:    store double -0.000000e+00, ptr [[F11]], align 8
+// LNX-NEXT:    store float 0x7FF8000000000000, ptr [[F12]], align 4
+// LNX-NEXT:    store float 0xFFF8000000000000, ptr [[F13]], align 4
+// LNX-NEXT:    store float 0x7FF0000000000000, ptr [[F14]], align 4
+// LNX-NEXT:    store float 0x7FF0000000000000, ptr [[F15]], align 4
+// LNX-NEXT:    store x86_fp80 0xK3FFE81A9FBE76C8B4396, ptr [[F16]], align 16
+// LNX-NEXT:    ret i32 0
+//
 int func()
 {
   int i;
@@ -30,30 +109,12 @@ int func()
   constexpr double f9 = __builtin_frexp(123.45, &i);
   constexpr double f10 = __builtin_frexp(0.0, &i);
   constexpr double f11 = __builtin_frexp(-0.0, &i);
-  constexpr double f12 = __builtin_frexpf(NAN, &i);
-  constexpr double f13 = __builtin_frexpf(-NAN, &i);
-  constexpr double f14 = __builtin_frexpf(INFINITY, &i);
-  constexpr double f15 = __builtin_frexpf(INFINITY, &i);
+  constexpr float f12 = __builtin_frexpf(NAN, &i);
+  constexpr float f13 = __builtin_frexpf(-NAN, &i);
+  constexpr float f14 = __builtin_frexpf(INFINITY, &i);
+  constexpr float f15 = __builtin_frexpf(INFINITY, &i);
   constexpr long double f16 = __builtin_frexpl(259.328L, &i);
 
   return 0;
 }
 
-// CHECK: store double 1.300000e+00, ptr {{.*}}
-// CHECK: store double -0.000000e+00, ptr {{.*}}
-// CHECK: store double -0.000000e+00, ptr {{.*}}
-// WIN: store float 0xFFF8000000000000, ptr {{.*}}
-// LNX: store float 0x7FF8000000000000, ptr {{.*}}
-// CHECK: store float -1.000000e+00, ptr {{.*}}
-// CHECK: store float 0xFFF0000000000000, ptr {{.*}}
-// CHECK: store double 1.234560e+02, ptr {{.*}}
-
-// CHECK: store double 0x3FEEDCCCCCCCCCCD, ptr {{.*}}
-// CHECK: store double 0.000000e+00, ptr {{.*}}
-// CHECK: store double -0.000000e+00, ptr {{.*}}
-// CHECK: store double 0xFFF8000000000000, ptr {{.*}}
-// WIN: store double 0x7FF8000000000000, ptr {{.*}}
-// LNX: store double 0xFFF8000000000000, ptr {{.*}}
-// CHECK: store double 0x7FF0000000000000, ptr {{.*}}
-// CHECK: store double 0x7FF0000000000000, ptr {{.*}}
-// CHECK: store double 5.065000e-01, ptr {{.*}}
diff --git a/clang/test/SemaCXX/constexpr-math.cpp b/clang/test/SemaCXX/constexpr-math.cpp
index 24e5bf70e3535..2c4c29c1c6542 100644
--- a/clang/test/SemaCXX/constexpr-math.cpp
+++ b/clang/test/SemaCXX/constexpr-math.cpp
@@ -12,27 +12,29 @@
 #define INFINITY (__builtin_inff())
 #endif
 
-int main() {
-    int i;
+template <auto, auto> constexpr bool is_same_val = false;
+template <auto X> constexpr bool is_same_val<X, X> = true;
 
-    // fmin
-    static_assert(__builtin_fmin(15.24, 1.3) == 1.3, "");
-    static_assert(__builtin_fmin(-0.0, +0.0) == -0.0, "");
-    static_assert(__builtin_fmin(+0.0, -0.0) == -0.0, "");
-    static_assert(__builtin_fminf(NAN, -1) == -1, "");
-    static_assert(__builtin_fminf(+INFINITY, 0) == 0, "");
-    static_assert(__builtin_isinf(__builtin_fminf(-INFINITY, 0)), "");
-    static_assert(__builtin_isnan(__builtin_fminf(NAN,NAN)), "");
+int main() {
+  int i;
 
-    // frexp
-    static_assert(__builtin_frexp(123.45, &i) == 0.96445312500000002);
-    static_assert(!__builtin_isnan(__builtin_frexp(123.45, &i)), "");
-    static_assert(__builtin_iszero(__builtin_frexp(0.0, &i)), "");
-    static_assert(__builtin_iszero(__builtin_frexp(-0.0, &i)), "");
-    static_assert(__builtin_isnan(__builtin_frexp(NAN, &i)));
-    static_assert(__builtin_isnan(__builtin_frexp(-NAN, &i)));
-    static_assert(!__builtin_isfinite(__builtin_frexp(INFINITY, &i)));
-    static_assert(!__builtin_isfinite(__builtin_frexp(-INFINITY, &i)));
+  // fmin
+  static_assert(is_same_val<__builtin_fmin(15.24, 1.3), 1.3>);
+  static_assert(is_same_val<__builtin_fmin(-0.0, +0.0), -0.0>);
+  static_assert(is_same_val<__builtin_fmin(+0.0, -0.0), -0.0>);
+  static_assert(is_same_val<__builtin_fminf(NAN, -1), -1.f>);
+  static_assert(is_same_val<__builtin_fminf(+INFINITY, 0), 0.f>);
+  static_assert(is_same_val<__builtin_fminf(-INFINITY, 0), -INFINITY>);
+  static_assert(is_same_val<__builtin_fminf(NAN, NAN), NAN>);
 
-    return 0;
+  // frexp
+  static_assert(is_same_val<__builtin_frexp(123.45, &i), 123.45/128>);
+  static_assert(is_same_val<__builtin_frexp(0.0, &i), 0.0>);
+  static_assert(is_same_val<__builtin_frexp(-0.0, &i), -0.0>);
+  static_assert(is_same_val<__builtin_frexpf(NAN, &i), NAN>);
+  static_assert(is_same_val<__builtin_frexpf(-NAN, &i), -NAN>);
+  static_assert(is_same_val<__builtin_frexpf(INFINITY, &i), INFINITY>);
+  static_assert(is_same_val<__builtin_frexpf(-INFINITY, &i), -INFINITY>);
+  
+  return 0;
 }



More information about the cfe-commits mailing list