Skip to content

Commit 987e1b2

Browse files
authored
[CUDA][HIP] Fix std::min in wrapper header (#93976)
The std::min behaves like 'a<b?a:b', which does not match libstdc++/libc++ behavior like 'b<a?b:a' when input is NaN. Make it consistent with libstdc++/libc++. Fixes: #93962 Fixes: ROCm/hip#3502
1 parent 83de21d commit 987e1b2

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

clang/lib/Headers/cuda_wrappers/algorithm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ template <class __T>
9999
__attribute__((enable_if(true, "")))
100100
inline _CPP14_CONSTEXPR __host__ __device__ const __T &
101101
min(const __T &__a, const __T &__b) {
102-
return __a < __b ? __a : __b;
102+
return __b < __a ? __b : __a;
103103
}
104104

105105
#pragma pop_macro("_CPP14_CONSTEXPR")
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
2+
3+
// RUN: %clang_cc1 \
4+
// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \
5+
// RUN: -internal-isystem %S/Inputs/include \
6+
// RUN: -triple x86_64-unknown-unknown \
7+
// RUN: -emit-llvm %s -O1 -o - \
8+
// RUN: | FileCheck %s
9+
10+
#define __host__ __attribute__((host))
11+
#define __device__ __attribute__((device))
12+
13+
#include <algorithm>
14+
15+
extern "C" bool cmp(double a, double b) { return a<b; }
16+
17+
// CHECK-LABEL: @test_std_min(
18+
// CHECK-NEXT: entry:
19+
// CHECK-NEXT: ret double 0x7FF8000000000000
20+
//
21+
extern "C" double test_std_min() {
22+
return std::min(__builtin_nan(""), 0.0);
23+
}
24+
25+
// CHECK-LABEL: @test_std_min_cmp(
26+
// CHECK-NEXT: entry:
27+
// CHECK-NEXT: ret double 0x7FF8000000000000
28+
//
29+
extern "C" double test_std_min_cmp() {
30+
return std::min(__builtin_nan(""), 0.0, cmp);
31+
}
32+
33+
// CHECK-LABEL: @test_std_max(
34+
// CHECK-NEXT: entry:
35+
// CHECK-NEXT: ret double 0x7FF8000000000000
36+
//
37+
extern "C" double test_std_max() {
38+
return std::max(__builtin_nan(""), 0.0);
39+
}
40+
41+
// CHECK-LABEL: @test_std_max_cmp(
42+
// CHECK-NEXT: entry:
43+
// CHECK-NEXT: ret double 0x7FF8000000000000
44+
//
45+
extern "C" double test_std_max_cmp() {
46+
return std::max(__builtin_nan(""), 0.0, cmp);
47+
}
48+

0 commit comments

Comments
 (0)