3

It's pretty common for compilers to have builtin intrinsic functions for processor features, but I'm having trouble finding them. Is there one to get at the 'REV' (reverse byte order of a word) instruction in ARM?

Where can I find the list of builtin functions?

joeforker
  • 40,459
  • 37
  • 151
  • 246

1 Answers1

10

Is there one to get at the 'REV' (reverse byte order of a word) instruction in ARM?

There is a more 'portable' form that is available on all architectures. It is __builtin_bswap32. For example the compiler explorer has,

unsigned int foo(unsigned int a)
{
  return __builtin_bswap32(a);
}

Giving,

foo(unsigned int):
        rev     r0, r0
        bx      lr

This is better than __builtin_rev would be as it will only be available on certain ARM targets (and certainly only ARM CPUs). You can use __builtin_bswap32 even on PowerPC, x86, etc.

artless noise
  • 21,212
  • 6
  • 68
  • 105
  • 1
    There's also `__builtin_bswap16` to swap around a 16 bit integer (compiles to rev16 arm instruction) – nos Feb 01 '16 at 15:20
  • For completeness, there's also [the matter of the peephole optimiser on -O2 and higher](https://goo.gl/MNbb1K), too ;) – Notlikethat Feb 01 '16 at 15:25
  • Where is the list of __builtin_*? – joeforker Feb 01 '16 at 18:10
  • @joe https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Other-Builtins.html#Other-Builtins – Notlikethat Feb 01 '16 at 18:24
  • You need to add a '-mcpu=' line to get the rev instruction. Without it gcc will use the lowest generic CPU instructions, where rev doesn't exist. You can try 'cortex-a5' Also if you add -march=armv7 -mthumb then you can get GCC ARM to produce the rev r0,r0. Without it you get 4 instructions. ARM64 works just fine since all supported CPUs have rev built in – shimpossible May 11 '19 at 12:04