所有 IEE754 类型的 CUDA 中的 std::floating_point 概念

问题描述 投票:0回答:1

我想知道 CUDA 是否提供了类似于

std::floating_point
的概念,但包括所有 IEE754 类型,例如__一半。我在下面提供了一个示例代码,用于测试 __half 模板函数是否无法使用
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;
}
c++ cuda precision c++-concepts half-precision-float
1个回答
0
投票

我认为您正在寻找的是

std::is_iec559
,因为
is_floating_point
仅涵盖一组有限的类型,而
is_iec559
,据我了解应该匹配任何实现 IEE754 的类型。

然而,正如@Artyer 在评论中指出的那样。

std::is_iec559<__half>
false

如果您只需要

std::is_flaoting_point
加上
__half
,您可以编写您的自定义概念

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.