Search code examples
c++cudaprecisionc++-conceptshalf-precision-float

std::floating_point concept in CUDA for all IEE754 types


I would like to know if CUDA provides a concept similar to std::floating_point but including all IEE754 types including e.g. __half. I provide below a sample code that test that __half template functions do not compile with std::floating_point.

#include <concepts>
#include <iostream>
#include <cuda_fp16.h>

template<std::floating_point T>
__host__ __device__ bool use_floating_point() {
    return True;
}

int main() {
  std::cout << "Is float a floating-point type?" << use_floating_point<float>() << '\n';
  std::cout << "Is __half a floating-point type?" << use_floating_point<__half>() << '\n'; // Compilation error
  return 0;
}

Solution

  • I think what you are looking for is std::is_iec559, because is_floating_point covers only a limited set of types, while is_iec559, to my understanding is supposed to match any type that implements IEE754.

    However, as @Artyer points out in a comment. std::is_iec559<__half> is false.

    If you merely need std::is_flaoting_point plus __half, you can write your custom concept

    template <typename T>
    concept floating_or_half = std::floating_point<T> || std::same_as<T,__half>;
    
    
    template<floating_or_half T>
    bool use_floating_point() {
        return true;
    }
    

    Live Demo