[clang] [clang][bytecode] Implement __builtin_f{maximum, minimum}_num (PR #112335)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 15 02:01:49 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/112335.diff
3 Files Affected:
- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+46-19)
- (modified) clang/test/Sema/constant-builtins-fmaximum-num.cpp (+1-1)
- (modified) clang/test/Sema/constant-builtins-fminimum-num.cpp (+1-1)
``````````diff
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index ec27aebf84bd80..65c7b4e5306d72 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -333,39 +333,48 @@ static bool interp__builtin_copysign(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_fmin(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *F) {
+ const InterpFrame *Frame, const Function *F,
+ bool IsNumBuiltin) {
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 negative.
- if (LHS.isZero() && RHS.isZero() && RHS.isNegative())
- Result = RHS;
- else if (LHS.isNan() || RHS < LHS)
- Result = RHS;
- else
- Result = LHS;
+ if (IsNumBuiltin) {
+ Result = llvm::minimumnum(LHS.getAPFloat(), RHS.getAPFloat());
+ } else {
+ // When comparing zeroes, return -0.0 if one of the zeroes is negative.
+ if (LHS.isZero() && RHS.isZero() && RHS.isNegative())
+ Result = RHS;
+ else if (LHS.isNan() || RHS < LHS)
+ Result = RHS;
+ else
+ Result = LHS;
+ }
S.Stk.push<Floating>(Result);
return true;
}
static bool interp__builtin_fmax(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame,
- const Function *Func) {
+ const InterpFrame *Frame, const Function *Func,
+ bool IsNumBuiltin) {
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;
+ if (IsNumBuiltin) {
+ Result = llvm::maximumnum(LHS.getAPFloat(), RHS.getAPFloat());
+ } else {
+ // 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;
@@ -1701,7 +1710,16 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
case Builtin::BI__builtin_fminl:
case Builtin::BI__builtin_fminf16:
case Builtin::BI__builtin_fminf128:
- if (!interp__builtin_fmin(S, OpPC, Frame, F))
+ if (!interp__builtin_fmin(S, OpPC, Frame, F, /*IsNumBuiltin=*/false))
+ return false;
+ break;
+
+ case Builtin::BI__builtin_fminimum_num:
+ case Builtin::BI__builtin_fminimum_numf:
+ case Builtin::BI__builtin_fminimum_numl:
+ case Builtin::BI__builtin_fminimum_numf16:
+ case Builtin::BI__builtin_fminimum_numf128:
+ if (!interp__builtin_fmin(S, OpPC, Frame, F, /*IsNumBuiltin=*/true))
return false;
break;
@@ -1710,7 +1728,16 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
case Builtin::BI__builtin_fmaxl:
case Builtin::BI__builtin_fmaxf16:
case Builtin::BI__builtin_fmaxf128:
- if (!interp__builtin_fmax(S, OpPC, Frame, F))
+ if (!interp__builtin_fmax(S, OpPC, Frame, F, /*IsNumBuiltin=*/false))
+ return false;
+ break;
+
+ case Builtin::BI__builtin_fmaximum_num:
+ case Builtin::BI__builtin_fmaximum_numf:
+ case Builtin::BI__builtin_fmaximum_numl:
+ case Builtin::BI__builtin_fmaximum_numf16:
+ case Builtin::BI__builtin_fmaximum_numf128:
+ if (!interp__builtin_fmax(S, OpPC, Frame, F, /*IsNumBuiltin=*/true))
return false;
break;
diff --git a/clang/test/Sema/constant-builtins-fmaximum-num.cpp b/clang/test/Sema/constant-builtins-fmaximum-num.cpp
index 206be5d407654c..32d415ca9ee984 100644
--- a/clang/test/Sema/constant-builtins-fmaximum-num.cpp
+++ b/clang/test/Sema/constant-builtins-fmaximum-num.cpp
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
-// FIXME: %clang_cc1 -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
// expected-no-diagnostics
constexpr double NaN = __builtin_nan("");
diff --git a/clang/test/Sema/constant-builtins-fminimum-num.cpp b/clang/test/Sema/constant-builtins-fminimum-num.cpp
index 7ec7427dba72b9..d54f1ec84ad6fc 100644
--- a/clang/test/Sema/constant-builtins-fminimum-num.cpp
+++ b/clang/test/Sema/constant-builtins-fminimum-num.cpp
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
-// FIXME: %clang_cc1 -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s
// expected-no-diagnostics
constexpr double NaN = __builtin_nan("");
``````````
</details>
https://github.com/llvm/llvm-project/pull/112335
More information about the cfe-commits
mailing list