     const KeyValuePair<int, __half> &a,
     const KeyValuePair<int, __half> &b) const
 {
-#ifdef __CUDA_ARCH__
-    if (__hisnan(a.value))
-        return a;
-    else if (__hisnan(b.value))
-        return b;
-    else if ((b.value < a.value) || ((a.value == b.value) && (b.key < a.key)))
-        return b;
-    else
-        return a;
-#else
-    // TODO: avoid cast to float
-    if (isnan(__half2float(a.value)))
-        return a;
-    else if (isnan(__half2float(b.value)))
-        return b;
-    else if ((__half2float(b.value) < __half2float(a.value)) ||
-            ((__half2float(a.value) == __half2float(b.value)) && (b.key < a.key)))
-        return b;
-    else
-        return a;
-#endif
+    if (half_isnan(a.value))
+        return a;
+    else if (half_isnan(b.value))
+        return b;
+    else if ((half_greater_than(b.value, a.value)) ||
+             (half_equal(a.value, b.value) && (b.key < a.key)))
+        return b;
+    else
+        return a;
+
 }
 #endif
 /* ------------------------------------ End of "patches" ------------------------------------ */

    const KeyValuePair<int, __half> &a,
    const KeyValuePair<int, __half> &b) const
{
#ifdef __CUDA_ARCH__
    if (__hisnan(a.value))
        return a;
    else if (__hisnan(b.value))
        return b;
    else if ((b.value > a.value) || ((a.value == b.value) && (b.key < a.key)))
        return b;
    else
        return a;
#else
    // TODO: avoid cast to float
    if (isnan(__half2float(a.value)))
        return a;
    else if (isnan(__half2float(b.value)))
        return b;
    else if ((__half2float(b.value) > __half2float(a.value)) || 
            ((__half2float(a.value) == __half2float(b.value)) && (b.key < a.key)))
        return b;
    else
        return a;
#endif
}
#endif


template <>
__host__ __device__ __forceinline__ __half Min::operator()(const __half &a, const __half &b) const
{
    // NumPy behavior: NaN is always chosen!
    if (half_isnan(a)) {return a;}
    else if (half_isnan(b)) {return b;}
    else if (half_greater_than(a, b)) {return a;}
    else {return a;}
}
#endif

