I have been using this little function under gcc/x86 to do fast byte-swapping of int16_t's:
static inline int16_t MySwapInt16t(int16_t val)
{
__asm__ ("xchgb %b0, %h0" : "=Q" (val) : "0" (val));
return val;
}
... today while browsing through someone else's code on line, I saw he had a similar function that looks like this:
static inline int16_t HisSwapInt16t(int16_t val)
{
__asm__ ("xchgb %h0, %b0" : "+Q" (val));
return val;
}
The differences between my function and his function (AFAICT) are:
- He specifies "+Q" instead of "=Q"
- The positions of %h0 and %b0 are exchanged
- He has no : "0" (val) clause at the end of his function
As far as I can tell, both implementations seem to work correctly, but inline assembly is a bit of a black art to me, so can someone knowledgable explain:
- What is the significance of the three differences listed above?
- Is one of the two implementations better or faster than the other, or will they both perform equivalently?