summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/misc.h15
1 files changed, 13 insertions, 2 deletions
diff --git a/include/misc.h b/include/misc.h
index cfe734f61..b34059161 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -270,6 +270,11 @@ void __attribute__((error("wrong sized variable passed to swap"))) wrong_size(vo
static inline void wrong_size(void)
{
}
+
+static inline void __builtin_constant_p(int x)
+{
+ return 0;
+}
#endif
/* byte swap a 32-bit value */
@@ -286,7 +291,10 @@ static inline void swap_uint32(uint32_t *x)
#define swapl(x) do { \
if (sizeof(*(x)) != 4) \
wrong_size(); \
- swap_uint32((uint32_t *)(x)); \
+ if (__builtin_constant_p((uintptr_t)(x) & 3) && ((uintptr_t)(x) & 3) == 0) \
+ *(x) = lswapl(*(x)); \
+ else \
+ swap_uint32((uint32_t *)(x)); \
} while (0)
/* byte swap a 16-bit value */
@@ -300,7 +308,10 @@ static inline void swap_uint16(uint16_t *x)
#define swaps(x) do { \
if (sizeof(*(x)) != 2) \
wrong_size(); \
- swap_uint16((uint16_t *)(x)); \
+ if (__builtin_constant_p((uintptr_t)(x) & 1) && ((uintptr_t)(x) & 1) == 0) \
+ *(x) = lswaps(*(x)); \
+ else \
+ swap_uint16((uint16_t *)(x)); \
} while (0)
/* copy 32-bit value from src to dst byteswapping on the way */