Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 553
Lars Schneider Avatar asked Aug 14 '10 14:08

Lars Schneider


1 Answers

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?

like image 98
Stephen Canon Avatar answered Oct 17 '22 21:10

Stephen Canon