aboutsummaryrefslogtreecommitdiffstats
path: root/os/various
diff options
context:
space:
mode:
authorStephane D'Alu <sdalu@sdalu.com>2016-02-28 23:27:54 +0100
committerStephane D'Alu <sdalu@sdalu.com>2016-02-28 23:27:54 +0100
commit7d048079c60330ea49f825ece1f3ff62952c3e65 (patch)
tree5270addbade9b93e7eb8436e3d276410393d3dab /os/various
parent1548bca80f5af0fe3f79331ae5a3d51b18f1c077 (diff)
downloadChibiOS-Contrib-7d048079c60330ea49f825ece1f3ff62952c3e65.tar.gz
ChibiOS-Contrib-7d048079c60330ea49f825ece1f3ff62952c3e65.tar.bz2
ChibiOS-Contrib-7d048079c60330ea49f825ece1f3ff62952c3e65.zip
extend to 64 bits int
Diffstat (limited to 'os/various')
-rw-r--r--os/various/bswap.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/os/various/bswap.h b/os/various/bswap.h
index 30ae1d6..6448498 100644
--- a/os/various/bswap.h
+++ b/os/various/bswap.h
@@ -38,45 +38,70 @@ extern "C" {
(((x) & 0x00FF0000UL) >> 8UL) | \
(((x) & 0x0000FF00UL) << 8UL) | \
(((x) & 0x000000FFUL) << 24UL))
+#define BSWAP_64(x) \
+ (uint64_t)((((x) & 0xFF00000000000000UL) >> 56UL) | \
+ (((x) & 0x00FF000000000000UL) >> 40UL) | \
+ (((x) & 0x0000FF0000000000UL) >> 24UL) | \
+ (((x) & 0x000000FF00000000UL) >> 8UL) | \
+ (((x) & 0x00000000FF000000UL) << 8UL) | \
+ (((x) & 0x0000000000FF0000UL) << 24UL) | \
+ (((x) & 0x000000000000FF00UL) << 40UL) | \
+ (((x) & 0x00000000000000FFUL) << 56UL))
#if defined(ARCH_BIG_ENDIAN)
#define le16_to_cpu(x) bswap_16(x)
#define le32_to_cpu(x) bswap_32(x)
+#define le64_to_cpu(x) bswap_64(x)
#define be16_to_cpu(x) (x)
#define be32_to_cpu(x) (x)
+#define be64_to_cpu(x) (x)
#define cpu_to_le16(x) bswap_16(x)
#define cpu_to_le32(x) bswap_32(x)
+#define cpu_to_le64(x) bswap_64(x)
#define cpu_to_be16(x) (x)
#define cpu_to_be32(x) (x)
+#define cpu_to_be64(x) (x)
#define LE16_TO_CPU(x) BSWAP_16(x)
#define LE32_TO_CPU(x) BSWAP_32(x)
+#define LE64_TO_CPU(x) BSWAP_64(x)
#define BE16_TO_CPU(x) (x)
#define BE32_TO_CPU(x) (x)
+#define BE64_TO_CPU(x) (x)
#define CPU_TO_LE16(x) BSWAP_16(x)
#define CPU_TO_LE32(x) BSWAP_32(x)
+#define CPU_TO_LE64(x) BSWAP_64(x)
#define CPU_TO_BE16(x) (x)
#define CPU_TO_BE32(x) (x)
+#define CPU_TO_BE64(x) (x)
#endif
#if defined(ARCH_LITTLE_ENDIAN)
#define le16_to_cpu(x) (x)
#define le32_to_cpu(x) (x)
+#define le64_to_cpu(x) (x)
#define be16_to_cpu(x) bswap_16(x)
#define be32_to_cpu(x) bswap_32(x)
+#define be64_to_cpu(x) bswap_64(x)
#define cpu_to_le16(x) (x)
#define cpu_to_le32(x) (x)
+#define cpu_to_le64(x) (x)
#define cpu_to_be16(x) bswap_16(x)
#define cpu_to_be32(x) bswap_32(x)
+#define cpu_to_be64(x) bswap_64(x)
#define LE16_TO_CPU(x) (x)
#define LE32_TO_CPU(x) (x)
+#define LE64_TO_CPU(x) (x)
#define BE16_TO_CPU(x) BSWAP_16(x)
#define BE32_TO_CPU(x) BSWAP_32(x)
+#define BE64_TO_CPU(x) BSWAP_64(x)
#define CPU_TO_LE16(x) (x)
#define CPU_TO_LE32(x) (x)
+#define CPU_TO_LE64(x) (x)
#define CPU_TO_BE16(x) BSWAP_16(x)
#define CPU_TO_BE32(x) BSWAP_32(x)
+#define CPU_TO_BE64(x) BSWAP_64(x)
#endif
@@ -124,6 +149,36 @@ static inline uint32_t bswap_32(const uint32_t x) {
return data.x;
}
+static inline uint64_t bswap_64(const uint64_t x)
+ __attribute__ ((warn_unused_result))
+ __attribute__ ((const))
+ __attribute__ ((always_inline));
+
+
+static inline uint64_t bswap_64(const uint64_t x) {
+ if (__builtin_constant_p(x))
+ return BSWAP_64(x);
+
+ uint8_t tmp;
+ union { uint64_t x; uint8_t b[8]; } data;
+
+ data.x = x;
+ tmp = data.b[0];
+ data.b[0] = data.b[7];
+ data.b[7] = tmp;
+ tmp = data.b[1];
+ data.b[1] = data.b[6];
+ data.b[6] = tmp;
+ tmp = data.b[2];
+ data.b[2] = data.b[5];
+ data.b[5] = tmp;
+ tmp = data.b[3];
+ data.b[3] = data.b[4];
+ data.b[4] = tmp;
+
+ return data.x;
+}
+
static inline void bswap_n(void* const data, uint8_t len)
__attribute__ ((nonnull (1)));