如何加速浮点到整数的转换?

时间:2023-05-09
本文介绍了如何加速浮点到整数的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我们在项目中进行了大量的浮点到整数转换.基本上是这样的

We're doing a great deal of floating-point to integer number conversions in our project. Basically, something like this

for(int i = 0; i < HUGE_NUMBER; i++)
     int_array[i] = float_array[i];

执行转换的默认 C 函数非常耗时.

The default C function which performs the conversion turns out to be quite time consuming.

是否有任何解决方法(可能是手动调整功能)可以稍微加快进程?我们不太关心精度.

Is there any work around (maybe a hand tuned function) which can speed up the process a little bit? We don't care much about a precision.

推荐答案

这里的大多数其他答案只是试图消除循环开销.

Most of the other answers here just try to eliminate loop overhead.

只有 deft_code 的答案 触及了真正问题的核心——将浮点数转换为整数是在 x86 处理器上价格高得惊人.deft_code 的解决方案是正确的,尽管他没有给出引用或解释.

Only deft_code's answer gets to the heart of what is likely the real problem -- that converting floating point to integers is shockingly expensive on an x86 processor. deft_code's solution is correct, though he gives no citation or explanation.

这里是技巧的来源,有一些解释以及特定于您是要向上、向下还是向零舍入的版本:了解您的 FPU

Here is the source of the trick, with some explanation and also versions specific to whether you want to round up, down, or toward zero: Know your FPU

很抱歉提供一个链接,但实际上这里写的任何内容,除了复制那篇出色的文章,都不会使事情变得清晰.

Sorry to provide a link, but really anything written here, short of reproducing that excellent article, is not going to make things clear.

这篇关于如何加速浮点到整数的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如果我们已经有了 RVO,移动语义会提供什么优化? 下一篇:便携式分支预测提示

相关文章