From 2f8a515c8a7147b8e6cf6ab643bb275d73b030dd Mon Sep 17 00:00:00 2001 From: rasky Date: Fri, 24 Sep 2010 11:04:16 +0000 Subject: [PATCH] CPU: optimize generic SWAB32 implementation, and prefer it over builtin for Cortex-M3 git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4289 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cpu/byteorder.h | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/bertos/cpu/byteorder.h b/bertos/cpu/byteorder.h index b560559c..ff1237ad 100644 --- a/bertos/cpu/byteorder.h +++ b/bertos/cpu/byteorder.h @@ -40,6 +40,8 @@ #include #include +#include +#include /** * Swap upper and lower bytes in a 16-bit value. @@ -47,22 +49,27 @@ #define SWAB16(x) ((uint16_t)(ROTR((x), 8) + \ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t)))) -#if GNUC_PREREQ(4, 3) +/* + * On Cortex-M3, GCC 4.4 builtin implementation is slower than our own + * rot-based implementation. + */ +#if GNUC_PREREQ(4, 3) && !CPU_CM3 #define SWAB32(x) ((uint32_t)(__builtin_bswap32((x) + \ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t))))) -#define SWAB64(x) ((uint64_t)(__builtin_bswap64((x) + \ - STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t))))) #else /** * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412). */ -#define SWAB32(x) ((uint32_t)( \ - (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \ - (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \ - (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \ - (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) + \ - STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t)))) +#define SWAB32(x) ((uint32_t)(( \ + (ROTR(x, 8) & 0xFF00FF00) | \ + (ROTL(x, 8) & 0x00FF00FF))) + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t))) +#endif +#if GNUC_PREREQ(4, 3) +#define SWAB64(x) ((uint64_t)(__builtin_bswap64((x) + \ + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t))))) +#else /** * Reverse bytes in a 64-bit value. */ -- 2.25.1