LwIP byte swap optimization on ARM CPU
This is only useful on little-endian CPU architectures like ARM.
It makes use of the ARM byte-swap, word-swap machine instructions.
cc.h
/* Tell LwIP 2.x not to compile in it's byte-order functions */
#define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS
/* Override the LwIP 2.x byte-order functions */
#define lwip_htons(x) LWIP_PLATFORM_HTONS(x)
#define lwip_htonl(x) LWIP_PLATFORM_HTONL(x)
/* The "naked" attribute eliminates function preamble */
extern uint16_t __attribute__((naked)) LWIP_PLATFORM_HTONS(uint16_t x);
extern uint32_t __attribute__((naked)) LWIP_PLATFORM_HTONL(uint32_t x);
cc.c
/**
* @brief Reverse bytes order
* @param x Out of order bytes.
* @return Ordered bytes.
*/
uint16_t __attribute__((naked)) LWIP_PLATFORM_HTONS(uint16_t x)
{
__asm(" rev16 r0,r0\n"
" bx lr\n");
}
/**
* @brief Reverse byte order
* @param x Out of order bytes.
* @return Ordered bytes.
*/
uint32_t __attribute__((naked)) LWIP_PLATFORM_HTONL(uint32_t x)
{
__asm(" rev r0,r0\n"
" bx lr\n");
}