Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ARM intrinsics in iOS?

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.

like image 227
Alexander Vasenin Avatar asked Oct 03 '22 13:10

Alexander Vasenin


1 Answers

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.
like image 76
Alexander Vasenin Avatar answered Oct 13 '22 09:10

Alexander Vasenin