<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/93962>93962</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [CUDA][HIP] std::min/max differs from libstdc++/libc++ about NaN
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          yxsamliu
      </td>
    </tr>
</table>

<pre>
    CUDA/HIP defines overloaded version of host device std::min/max with higher precedence than those in standard C++ headers:

https://github.com/llvm/llvm-project/blob/6ee845d2401b7f0e5f385fc0e3a8cb44afd667dc/clang/lib/Headers/cuda_wrappers/algorithm#L101

https://github.com/gcc-mirror/gcc/blob/d9c90c82d900fdae95df4499bf5f0a4ecb903b53/libstdc%2B%2B-v3/include/bits/stl_algobase.h#L233 

https://github.com/llvm/llvm-project/blob/6ee845d2401b7f0e5f385fc0e3a8cb44afd667dc/libcxx/include/__algorithm/min.h#L36

However, there is a subtle difference of the CUDA/HIP version compared to libstdc++/libc++ version.

CUDA/HIP version is like `__a < __b ? __a : __b`. If there is NaN, it chooese `__b`.

libstdc++/libc++ version is like `__b < __a ? __b : __a`. If there is NaN, it chooses `__a`.

This difference causes annoyance for PyTorch

https://github.com/ROCm/HIP/issues/2209#issuecomment-2141205965

according to https://en.cppreference.com/w/cpp/algorithm/min

 std::min(a,b) returns the smaller of a and b. If the values are equivalent, returns a. It seems libstdc++/libc++ behavior is correct. CUDA/HIP std::min/max needs to be fixed.

@Artem-B 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8lVFv4ygQxz8NeRk1wmA78UMemlTWVjrtrU57z9EA45g7bHKA0_bbn3DcbbqqdvdeTooSDYHhx3_-DBijPY1EO1btWfWwwin1PuxeniMOzk4r5c3L7vDnwz0T7afHL2CosyNF8BcKzqMhAxcK0foRfAe9jwkMXawmiMkw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