Skip to content

Commit

Permalink
Avoid using __builtin_bswap32/64() before GCC 4.3.
Browse files Browse the repository at this point in the history
bits/byteswap.h seems to only use these built-ins on GCC 4.3 and later.
Assume there were some problems on earlier GCC versions. Add fallback
implementations and a similar version check.
  • Loading branch information
foo86 committed May 6, 2015
1 parent c1069c6 commit 7dcdfa3
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions libdcadec/common.h
Expand Up @@ -57,14 +57,10 @@
} \
} while (false)

#define AT_LEAST_GCC(major, minor) \
(defined __GNUC__) && ((__GNUC__ > (major)) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))

#ifdef __GNUC__
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
#define dca_bswap16(x) __builtin_bswap16(x)
#else
static inline uint16_t dca_bswap16(uint16_t x) { return (x << 8) | (x >> 8); }
#endif
#define dca_bswap32(x) __builtin_bswap32(x)
#define dca_bswap64(x) __builtin_bswap64(x)
#define dca_clz32(x) __builtin_clz(x)
#define dca_clz64(x) __builtin_clzll(x)
#else
Expand All @@ -86,6 +82,33 @@ static inline int dca_popcount(uint32_t x)

#define dca_countof(x) (sizeof(x) / sizeof((x)[0]))

#if AT_LEAST_GCC(4, 8)
#define dca_bswap16(x) __builtin_bswap16(x)
#else
static inline uint16_t dca_bswap16(uint16_t x)
{
return (x << 8) | (x >> 8);
}
#endif

#if AT_LEAST_GCC(4, 3)
#define dca_bswap32(x) __builtin_bswap32(x)
#define dca_bswap64(x) __builtin_bswap64(x)
#else
static inline uint32_t dca_bswap32(uint32_t x)
{
x = ((x & 0x00ff00ff) << 8) | ((x & 0xff00ff00) >> 8);
return (x << 16) | (x >> 16);
}

static inline uint64_t dca_bswap64(uint64_t x)
{
x = ((x & 0x00ff00ff00ff00ff) << 8) | ((x & 0xff00ff00ff00ff00) >> 8);
x = ((x & 0x0000ffff0000ffff) << 16) | ((x & 0xffff0000ffff0000) >> 16);
return (x << 32) | (x >> 32);
}
#endif

#define DCA_BSWAP16_C(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
#define DCA_BSWAP32_C(x) ((DCA_BSWAP16_C(x) << 16) | (DCA_BSWAP16_C(x >> 16)))
#define DCA_BSWAP64_C(x) ((DCA_BSWAP32_C(x) << 32) | (DCA_BSWAP32_C(x >> 32)))
Expand Down

0 comments on commit 7dcdfa3

Please sign in to comment.