[endian] update the endian.h file to use builtins and tighten up a macro
Use compiler builtins for these and make the *E32SWAP routines a bit safer.
/*
* Copyright (c) 2008-2014 Travis Geiselbrecht
+ * Copyright 2016 The Fuchsia Authors
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
#define BYTE_ORDER __BYTE_ORDER__
// define a macro that unconditionally swaps
-#define SWAP_32(x) \
- (((uint32_t)(x) << 24) | (((uint32_t)(x) & 0xff00) << 8) |(((uint32_t)(x) & 0x00ff0000) >> 8) | ((uint32_t)(x) >> 24))
-#define SWAP_16(x) \
- ((((uint16_t)(x) & 0xff) << 8) | ((uint16_t)(x) >> 8))
-#define SWAP_64(x) ((((uint64_t)(SWAP_32((uint64_t)(x)))) << 32) | (SWAP_32(((uint64_t)(x)) >> 32)))
+static inline uint64_t SWAP_64(uint64_t x) { return __builtin_bswap64(x); }
+static inline uint32_t SWAP_32(uint32_t x) { return __builtin_bswap32(x); }
+static inline uint16_t SWAP_16(uint16_t x) { return __builtin_bswap16(x); }
// standard swap macros
#if BYTE_ORDER == BIG_ENDIAN
#define BE16(val) SWAP_16(val)
#endif
-#define LE32SWAP(var) (var) = LE32(var);
-#define LE16SWAP(var) (var) = LE16(var);
-#define BE32SWAP(var) (var) = BE32(var);
-#define BE16SWAP(var) (var) = BE16(var);
+#define LE32SWAP(var) do { (var) = LE32(var); } while (0)
+#define LE16SWAP(var) do { (var) = LE16(var); } while (0)
+#define BE32SWAP(var) do { (var) = BE32(var); } while (0)
+#define BE16SWAP(var) do { (var) = BE16(var); } while (0)
/* classic network byte swap stuff */
#define ntohs(n) BE16(n)
#define htobe64(h) BE64(h)
#define be64toh(b) BE64(b)
-// some memory access macros
-#if __POWERPC__
-#include <ppc_intrinsics.h>
-
-#define READ_MEM_WORD(ptr) __lwbrx((word *)(ptr), 0)
-#define READ_MEM_HALFWORD(ptr) __lhbrx((halfword *)(ptr), 0)
-#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
-#define WRITE_MEM_WORD(ptr, data) __stwbrx(data, (word *)(ptr), 0)
-#define WRITE_MEM_HALFWORD(ptr, data) __sthbrx(data, (halfword *)(ptr), 0)
-#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
-#else
-#define READ_MEM_WORD(ptr) SWAPIT_32(*(word *)(ptr))
-#define READ_MEM_HALFWORD(ptr) SWAPIT_16(*(halfword *)(ptr))
-#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
-#define WRITE_MEM_WORD(ptr, data) (*(word *)(ptr) = SWAPIT_32(data))
-#define WRITE_MEM_HALFWORD(ptr, data) (*(halfword *)(ptr) = SWAPIT_16(data))
-#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
-#endif
-
