[clang] c14c34d - [clang][Interp] Implement __builtin_fmin
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 28 11:19:13 PDT 2023
Author: Timm Bäder
Date: 2023-07-28T20:18:57+02:00
New Revision: c14c34de4571cf3268e3599f5d1fc7318c42aac7
URL: https://github.com/llvm/llvm-project/commit/c14c34de4571cf3268e3599f5d1fc7318c42aac7
DIFF: https://github.com/llvm/llvm-project/commit/c14c34de4571cf3268e3599f5d1fc7318c42aac7.diff
LOG: [clang][Interp] Implement __builtin_fmin
Differential Revision: https://reviews.llvm.org/D155546
Added:
Modified:
clang/lib/AST/Interp/InterpBuiltin.cpp
clang/test/AST/Interp/builtin-functions.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/InterpBuiltin.cpp b/clang/lib/AST/Interp/InterpBuiltin.cpp
index e4c0091452e399..b023d09df48784 100644
--- a/clang/lib/AST/Interp/InterpBuiltin.cpp
+++ b/clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -143,6 +143,25 @@ static bool interp__builtin_copysign(InterpState &S, CodePtr OpPC,
return true;
}
+static bool interp__builtin_fmin(InterpState &S, CodePtr OpPC,
+ const InterpFrame *Frame, const Function *F) {
+ 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;
+
+ S.Stk.push<Floating>(Result);
+ return true;
+}
+
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
InterpFrame *Frame = S.Current;
APValue Dummy;
@@ -195,6 +214,15 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
return Ret<PT_Float>(S, OpPC, Dummy);
break;
+ case Builtin::BI__builtin_fmin:
+ case Builtin::BI__builtin_fminf:
+ case Builtin::BI__builtin_fminl:
+ case Builtin::BI__builtin_fminf16:
+ case Builtin::BI__builtin_fminf128:
+ if (interp__builtin_fmin(S, OpPC, Frame, F))
+ return Ret<PT_Float>(S, OpPC, Dummy);
+ break;
+
default:
return false;
}
diff --git a/clang/test/AST/Interp/builtin-functions.cpp b/clang/test/AST/Interp/builtin-functions.cpp
index eec6c9ec4bbf81..8ff717217e4728 100644
--- a/clang/test/AST/Interp/builtin-functions.cpp
+++ b/clang/test/AST/Interp/builtin-functions.cpp
@@ -60,3 +60,15 @@ namespace nan {
// expected-note {{read of dereferenced one-past-the-end pointer}} \
// expected-note {{in call to}}
}
+
+namespace fmin {
+ constexpr float f1 = __builtin_fmin(1.0, 2.0f);
+ static_assert(f1 == 1.0f, "");
+
+ constexpr float min = __builtin_fmin(__builtin_nan(""), 1);
+ static_assert(min == 1, "");
+ constexpr float min2 = __builtin_fmin(1, __builtin_nan(""));
+ static_assert(min2 == 1, "");
+ constexpr float min3 = __builtin_fmin(__builtin_inf(), __builtin_nan(""));
+ static_assert(min3 == __builtin_inf(), "");
+}
More information about the cfe-commits
mailing list