[clang] [HIP] fix host min/max in header (PR #82956)
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 26 07:36:54 PST 2024
================
@@ -1306,14 +1306,50 @@ float min(float __x, float __y) { return __builtin_fminf(__x, __y); }
__DEVICE__
double min(double __x, double __y) { return __builtin_fmin(__x, __y); }
+// Define host min/max functions.
+
#if !defined(__HIPCC_RTC__) && !defined(__OPENMP_AMDGCN__)
-__host__ inline static int min(int __arg1, int __arg2) {
- return __arg1 < __arg2 ? __arg1 : __arg2;
-}
-__host__ inline static int max(int __arg1, int __arg2) {
- return __arg1 > __arg2 ? __arg1 : __arg2;
-}
+#pragma push_macro("DEFINE_MIN_MAX_FUNCTIONS")
+#define DEFINE_MIN_MAX_FUNCTIONS(type1, type2) \
+static inline auto min(const type1 __a, const type2 __b) \
+ -> typename std::remove_reference<decltype(__a < __b ? __a : __b)>::type { \
+ return (__a < __b) ? __a : __b; \
+} \
+static inline auto max(const type1 __a, const type2 __b) \
+ -> typename std::remove_reference<decltype(__a > __b ? __a : __b)>::type { \
+ return (__a > __b) ? __a : __b; \
+}
+
+// Define min and max functions for same type comparisons
+DEFINE_MIN_MAX_FUNCTIONS(int, int)
----------------
jhuber6 wrote:
Could we not do stuff like `static_assert` where we check if both types are the same modulo `unsigned` or `const`? I'm assuming that would be similar. I'd just prefer to avoid macro magic if possible, but it we really need it for compatibility reasons we can use it.
https://github.com/llvm/llvm-project/pull/82956
More information about the cfe-commits
mailing list