views:

909

answers:

1

I am currently trying to optimize some DSP related code with Shark and found that I am wasting a lot of time in a float to integer conversion:

SInt16 nextInt = nextFloat * 32768.0f + 0.5f;

As the iPhone seems to have an ARM11 FP co-processor, I am wondering if I can replace my code with the FTOSI instruction. There is some documentation available at the ARM website, but I have no experience in inlining hand optimized assembly. Has someone done that before?
I think I could inline the code with

__asm__ volatile

But how do I check if the instruction is available?
How can I pass my value in?

EDIT1: As Louis already pointed out, I forgot to mention that I am compiling with "Compile for Thumb" turned off.

EDIT2: As I want to convert float to signed Int16 and not unsigned Int, I changed the ARM instruction from FTOUI to FTOSI. This was a mistake in the original post.

+3  A: 

This may be an an obvious question, but are you sure you are targeting ARM? By default the iPhone SDK compiles all apps for THUMB, which uses software floating point everything (including float/int conversion).

Anyway, if the device has a VFP coprocessor it has the instruction. You can check if it has an appropriate coprocessor by reading the FPSID register and making sure it is a supported model.

I suspect it is safe to assume all iPhones support it. Among other things Apple's assembler has support for the opcode, and the LLVM ARM backend uses it for the type conversions, which means when Apple eventually supports LLVM on the phone their compiler is going to generate FTOUI instructions.

Louis Gerbarg
As my code is floating point intensive, I turned "Compile for Thumb" off. (I think this sets the "-marm" compiler switch)
weichsel
You are correct, just wanted to make sure. It seems kind of shocking GCC does not use FTOUI for the conversions itself and requires inline asm, which is why I was checking. I am adding what should be an answer to what I think you are asking.
Louis Gerbarg
I think GCC has no chance to detect that I am trying to do an float to int conversion as it's not a simple cast but a composite of mul, add and a cast. I will investigate the asm that GCC outputs later and post it here.
weichsel