Fast float to int conversion and floating point precision on ARM (iPhone 3GS/4)

五迷三道 提交于 2019-12-07 06:07:14

问题


I read (http://www.stereopsis.com/FPU.html) mentioned in (What is the fastest way to convert float to int on x86). Does anyone know if the slow simple cast (see snippet below) does apply to ARM architecture, too?

inline int Convert(float x)
{
  int i = (int) x;
  return i;
}

To apply some tricks mentioned in the FPU article you have to set the precision for floating point operations. How do I do that on ARM?

What is the fastest float to int conversion on ARM architecture?

Thanks!


回答1:


Short version, "no".

That article is ancient and doesn't even apply to modern x86 systems, let alone ARM. A simple cast to integer is reasonably fast on ARMv7 (iPhone 3GS/4), though there is a modest stall moving data from the VFP/NEON registers to the general purpose registers. However, given that your float data is probably coming from a computation done in VFP/NEON registers, you will have to pay for that move no matter how you do the conversion.

I don't think that this is a profitable path for optimization unless you have traces showing that this is a major bottleneck for your program. Even then, the fastest conversion is the conversion you don't do; you will almost always be better off finding algorithmic ways to eliminate conversions from your program.

If you do genuinely need to optimize conversions, look into the vcvt.i32.f32 instruction, which converts a vector of two or four floating point numbers to a vector of two or four integers without moving the data out of the NEON registers (and therefore, without incurring the stall that I mentioned). Of course, you will need to do your subsequent integer computations on the NEON unit for this to be a profitable optimization.

Question: What are you really trying to do? Why do you think you need a faster float->int conversion?



来源:https://stackoverflow.com/questions/3483670/fast-float-to-int-conversion-and-floating-point-precision-on-arm-iphone-3gs-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!