Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top