快速 int 到 float 转换
Fast int to float conversion
我正在使用 float
s 在 Cuda 中进行计算。因为我们在 GPU 上没有足够的内存,所以我们在 GPU 上将原始数据存储为 uint16_t
和 int16_t
。因此,在使用此数据之前,我必须将其转换为 float
s。
int
的数量并没有那么大(大约 12k 的 uint16_t
和相同数量的 int16_t
)。分析显示转换数字需要相当长的时间(大约 5-10%)。其余的计算不能再优化了。
因此我的 3+1 问题是:
- 将
int
s 转换为 float
s 的最快方法是什么。
- 转换
int16_t
或uint16_t
时是否有实质性差异。
- 转换较大的
int
类型时是否存在实质性差异,例如int32
或 int64
.
- 为什么所有关于将
float
转换为 int
的问题。这是平时不会做的事吗?
- What is the fastest way to convert ints to floats.
简单的赋值。有硬件 type conversion instructions,CUDA 编译器将在您不执行任何操作的情况下自动发出这些硬件。硬件转换包括正确的 IEEE 舍入模式。
- Is there a substantial difference when converting
int16_t
or uint16_t
.
没有
- Is there a substantial difference when converting larger int types, e.g.
int32
or int64
.
没有。有。类型转换指令的指令吞吐量为documented。 32 位和 16 位整数到浮点数转换指令具有相同的吞吐量。在大多数架构上,64 位转换指令比 16 位和 32 位转换指令慢得多。
- Why are all questions on SO about converting floats to ints. Is this something one usually does not do?
因为许多人不明白 float
和 int
类型之间的区别,以及为什么他们在转换 float
或 double
类型时会降低精度到 int
类型。
在您的情况下,您无需担心。
我正在使用 float
s 在 Cuda 中进行计算。因为我们在 GPU 上没有足够的内存,所以我们在 GPU 上将原始数据存储为 uint16_t
和 int16_t
。因此,在使用此数据之前,我必须将其转换为 float
s。
int
的数量并没有那么大(大约 12k 的 uint16_t
和相同数量的 int16_t
)。分析显示转换数字需要相当长的时间(大约 5-10%)。其余的计算不能再优化了。
因此我的 3+1 问题是:
- 将
int
s 转换为float
s 的最快方法是什么。 - 转换
int16_t
或uint16_t
时是否有实质性差异。 - 转换较大的
int
类型时是否存在实质性差异,例如int32
或int64
. - 为什么所有关于将
float
转换为int
的问题。这是平时不会做的事吗?
- What is the fastest way to convert ints to floats.
简单的赋值。有硬件 type conversion instructions,CUDA 编译器将在您不执行任何操作的情况下自动发出这些硬件。硬件转换包括正确的 IEEE 舍入模式。
- Is there a substantial difference when converting
int16_t
oruint16_t
.
没有
- Is there a substantial difference when converting larger int types, e.g.
int32
orint64
.
没有。有。类型转换指令的指令吞吐量为documented。 32 位和 16 位整数到浮点数转换指令具有相同的吞吐量。在大多数架构上,64 位转换指令比 16 位和 32 位转换指令慢得多。
- Why are all questions on SO about converting floats to ints. Is this something one usually does not do?
因为许多人不明白 float
和 int
类型之间的区别,以及为什么他们在转换 float
或 double
类型时会降低精度到 int
类型。
在您的情况下,您无需担心。