快速 int 到 float 转换

Fast int to float conversion

我正在使用 floats 在 Cuda 中进行计算。因为我们在 GPU 上没有足够的内存,所以我们在 GPU 上将原始数据存储为 uint16_tint16_t。因此,在使用此数据之前,我必须将其转换为 floats。 int 的数量并没有那么大(大约 12k 的 uint16_t 和相同数量的 int16_t)。分析显示转换数字需要相当长的时间(大约 5-10%)。其余的计算不能再优化了。 因此我的 3+1 问题是:

  • 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?

因为许多人不明白 floatint 类型之间的区别,以及为什么他们在转换 floatdouble 类型时会降低精度到 int 类型。
在您的情况下,您无需担心。