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

How to speed up floating-point to integer number conversion?(如何加速浮点到整数的转换?)
本文介绍了如何加速浮点到整数的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)