Question

I need to compute MSB (most significant bit) on millions of 32-bit integers on iPad very fast. I have my own (ugly) implementation of MSB written on plain C, which is slow. ARM processors have CLZ (count leading zeroes) hardware command, which can be very useful for that. According to ARM reference there is an intrinsic C function __CLZ. How can I add support of ARM intrinsic functions to my Xcode project?

P.S. I've managed to find the way of accessing hardware CLZ from NEON (by including arm_neon.h), but that's not what I need, because it's only works with vector, but I need scalar MSB.

Était-ce utile?

La solution

I found ARM intrinsic functions names on page 44 of ARM C language extensions. Some of them works in Xcode. This prints 31, as expected:

NSLog(@"%u", __builtin_clz(1));

Notes:

  • I haven't found any references of this in Apple docs. Most likely Xcode inherited those functions from LLVM or CLANG.
  • You don't need to include any special headers or frameworks to use those functions. Xcode IDE autocomplete doesn't know about them.
  • Only a few functions from extensions list are implemented. According to pages 12-13 of the same document it should be two header files: arm_acle.h for non-NEON intrinsics and arm_neon.h for NEON intrinsics. Xcode have only the second file, but some of the functions from the first file declared somewhere else.

Autres conseils

This may be obvious, but if if you use ARM-specific instructions, you will not be able to run your app in the iOS simulator. The simulator uses the native x86-64 hardware of your Mac.

You could create a wrapper function that uses a compiler directive to use the ARM command or fall back to the "ugly" code if you don't have support.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top