|
|
@@ -4,6 +4,59 @@
|
|
|
#include <stdint.h>
|
|
|
#include "config.h"
|
|
|
|
|
|
+/**
|
|
|
+ * BSWAP_16_CONST - reverse bytes in a constant uint16_t value.
|
|
|
+ * @val: constant value whose bytes to swap.
|
|
|
+ *
|
|
|
+ * Designed to be usable in constant-requiring initializers.
|
|
|
+ *
|
|
|
+ * Example:
|
|
|
+ * struct mystruct {
|
|
|
+ * char buf[BSWAP_16_CONST(0x1234)];
|
|
|
+ * };
|
|
|
+ */
|
|
|
+#define BSWAP_16_CONST(val) \
|
|
|
+ ((((uint16_t)val & 0x00ff) << 8) \
|
|
|
+ | (((uint16_t)val & 0xff00) >> 8))
|
|
|
+
|
|
|
+/**
|
|
|
+ * BSWAP_32_CONST - reverse bytes in a constant uint32_t value.
|
|
|
+ * @val: constant value whose bytes to swap.
|
|
|
+ *
|
|
|
+ * Designed to be usable in constant-requiring initializers.
|
|
|
+ *
|
|
|
+ * Example:
|
|
|
+ * struct mystruct {
|
|
|
+ * char buf[BSWAP_32_CONST(0xff000000)];
|
|
|
+ * };
|
|
|
+ */
|
|
|
+#define BSWAP_32_CONST(val) \
|
|
|
+ ((((uint32_t)val & 0x000000ff) << 24) \
|
|
|
+ | (((uint32_t)val & 0x0000ff00) << 8) \
|
|
|
+ | (((uint32_t)val & 0x00ff0000) >> 8) \
|
|
|
+ | (((uint32_t)val & 0xff000000) >> 24))
|
|
|
+
|
|
|
+/**
|
|
|
+ * BSWAP_64_CONST - reverse bytes in a constant uint64_t value.
|
|
|
+ * @val: constantvalue whose bytes to swap.
|
|
|
+ *
|
|
|
+ * Designed to be usable in constant-requiring initializers.
|
|
|
+ *
|
|
|
+ * Example:
|
|
|
+ * struct mystruct {
|
|
|
+ * char buf[BSWAP_64_CONST(0xff00000000000000ULL)];
|
|
|
+ * };
|
|
|
+ */
|
|
|
+#define BSWAP_64_CONST(val) \
|
|
|
+ ((((uint64_t)val & 0x00000000000000ffULL) << 56) \
|
|
|
+ | (((uint64_t)val & 0x000000000000ff00ULL) << 40) \
|
|
|
+ | (((uint64_t)val & 0x0000000000ff0000ULL) << 24) \
|
|
|
+ | (((uint64_t)val & 0x00000000ff000000ULL) << 8) \
|
|
|
+ | (((uint64_t)val & 0x000000ff00000000ULL) >> 8) \
|
|
|
+ | (((uint64_t)val & 0x0000ff0000000000ULL) >> 24) \
|
|
|
+ | (((uint64_t)val & 0x00ff000000000000ULL) >> 40) \
|
|
|
+ | (((uint64_t)val & 0xff00000000000000ULL) >> 56))
|
|
|
+
|
|
|
#if HAVE_BYTESWAP_H
|
|
|
#include <byteswap.h>
|
|
|
#else
|
|
|
@@ -17,8 +70,7 @@
|
|
|
*/
|
|
|
static inline uint16_t bswap_16(uint16_t val)
|
|
|
{
|
|
|
- return ((val & (uint16_t)0x00ffU) << 8)
|
|
|
- | ((val & (uint16_t)0xff00U) >> 8);
|
|
|
+ return BSWAP_16_CONST(val);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -31,10 +83,7 @@ static inline uint16_t bswap_16(uint16_t val)
|
|
|
*/
|
|
|
static inline uint32_t bswap_32(uint32_t val)
|
|
|
{
|
|
|
- return ((val & (uint32_t)0x000000ffUL) << 24)
|
|
|
- | ((val & (uint32_t)0x0000ff00UL) << 8)
|
|
|
- | ((val & (uint32_t)0x00ff0000UL) >> 8)
|
|
|
- | ((val & (uint32_t)0xff000000UL) >> 24);
|
|
|
+ return BSWAP_32_CONST(val);
|
|
|
}
|
|
|
#endif /* !HAVE_BYTESWAP_H */
|
|
|
|
|
|
@@ -50,14 +99,7 @@ static inline uint32_t bswap_32(uint32_t val)
|
|
|
*/
|
|
|
static inline uint64_t bswap_64(uint64_t val)
|
|
|
{
|
|
|
- return ((val & (uint64_t)0x00000000000000ffULL) << 56)
|
|
|
- | ((val & (uint64_t)0x000000000000ff00ULL) << 40)
|
|
|
- | ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
|
|
|
- | ((val & (uint64_t)0x00000000ff000000ULL) << 8)
|
|
|
- | ((val & (uint64_t)0x000000ff00000000ULL) >> 8)
|
|
|
- | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
|
|
|
- | ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
|
|
|
- | ((val & (uint64_t)0xff00000000000000ULL) >> 56);
|
|
|
+ return BSWAP_64_CONST(val);
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
@@ -68,17 +110,106 @@ static inline uint64_t bswap_64(uint64_t val)
|
|
|
#error "Can't compile for both big and little endian."
|
|
|
#endif
|
|
|
|
|
|
+#if HAVE_LITTLE_ENDIAN
|
|
|
+/**
|
|
|
+ * CPU_TO_LE64_CONST - convert a constant uint64_t value to little-endian
|
|
|
+ * @native: constant to convert
|
|
|
+ */
|
|
|
+#define CPU_TO_LE64_CONST(native) (native)
|
|
|
+
|
|
|
+/**
|
|
|
+ * CPU_TO_LE32_CONST - convert a constant uint32_t value to little-endian
|
|
|
+ * @native: constant to convert
|
|
|
+ */
|
|
|
+#define CPU_TO_LE32_CONST(native) (native)
|
|
|
+
|
|
|
+/**
|
|
|
+ * CPU_TO_LE16_CONST - convert a constant uint16_t value to little-endian
|
|
|
+ * @native: constant to convert
|
|
|
+ */
|
|
|
+#define CPU_TO_LE16_CONST(native) (native)
|
|
|
+
|
|
|
+/**
|
|
|
+ * LE64_TO_CPU_CONST - convert a little-endian uint64_t constant
|
|
|
+ * @le_val: little-endian constant to convert
|
|
|
+ */
|
|
|
+#define LE64_TO_CPU_CONST(le_val) (le_val)
|
|
|
+
|
|
|
+/**
|
|
|
+ * LE32_TO_CPU_CONST - convert a little-endian uint32_t constant
|
|
|
+ * @le_val: little-endian constant to convert
|
|
|
+ */
|
|
|
+#define LE32_TO_CPU_CONST(le_val) (le_val)
|
|
|
+
|
|
|
+/**
|
|
|
+ * LE16_TO_CPU_CONST - convert a little-endian uint16_t constant
|
|
|
+ * @le_val: little-endian constant to convert
|
|
|
+ */
|
|
|
+#define LE16_TO_CPU_CONST(le_val) (le_val)
|
|
|
+
|
|
|
+#else /* ... HAVE_BIG_ENDIAN */
|
|
|
+#define CPU_TO_LE64_CONST(native) BSWAP_64_CONST(native)
|
|
|
+#define CPU_TO_LE32_CONST(native) BSWAP_32_CONST(native)
|
|
|
+#define CPU_TO_LE16_CONST(native) BSWAP_16_CONST(native)
|
|
|
+#define LE64_TO_CPU_CONST(le_val) BSWAP_64_CONST(le_val)
|
|
|
+#define LE32_TO_CPU_CONST(le_val) BSWAP_32_CONST(le_val)
|
|
|
+#define LE16_TO_CPU_CONST(le_val) BSWAP_16_CONST(le_val)
|
|
|
+#endif /* HAVE_BIG_ENDIAN */
|
|
|
+
|
|
|
+#if HAVE_BIG_ENDIAN
|
|
|
+/**
|
|
|
+ * CPU_TO_BE64_CONST - convert a constant uint64_t value to big-endian
|
|
|
+ * @native: constant to convert
|
|
|
+ */
|
|
|
+#define CPU_TO_BE64_CONST(native) (native)
|
|
|
+
|
|
|
+/**
|
|
|
+ * CPU_TO_BE32_CONST - convert a constant uint32_t value to big-endian
|
|
|
+ * @native: constant to convert
|
|
|
+ */
|
|
|
+#define CPU_TO_BE32_CONST(native) (native)
|
|
|
+
|
|
|
+/**
|
|
|
+ * CPU_TO_BE16_CONST - convert a constant uint16_t value to big-endian
|
|
|
+ * @native: constant to convert
|
|
|
+ */
|
|
|
+#define CPU_TO_BE16_CONST(native) (native)
|
|
|
+
|
|
|
+/**
|
|
|
+ * BE64_TO_CPU_CONST - convert a big-endian uint64_t constant
|
|
|
+ * @le_val: big-endian constant to convert
|
|
|
+ */
|
|
|
+#define BE64_TO_CPU_CONST(le_val) (le_val)
|
|
|
+
|
|
|
+/**
|
|
|
+ * BE32_TO_CPU_CONST - convert a big-endian uint32_t constant
|
|
|
+ * @le_val: big-endian constant to convert
|
|
|
+ */
|
|
|
+#define BE32_TO_CPU_CONST(le_val) (le_val)
|
|
|
+
|
|
|
+/**
|
|
|
+ * BE16_TO_CPU_CONST - convert a big-endian uint16_t constant
|
|
|
+ * @le_val: big-endian constant to convert
|
|
|
+ */
|
|
|
+#define BE16_TO_CPU_CONST(le_val) (le_val)
|
|
|
+
|
|
|
+#else /* ... HAVE_LITTLE_ENDIAN */
|
|
|
+#define CPU_TO_BE64_CONST(native) BSWAP_64_CONST(native)
|
|
|
+#define CPU_TO_BE32_CONST(native) BSWAP_32_CONST(native)
|
|
|
+#define CPU_TO_BE16_CONST(native) BSWAP_16_CONST(native)
|
|
|
+#define BE64_TO_CPU_CONST(le_val) BSWAP_64_CONST(le_val)
|
|
|
+#define BE32_TO_CPU_CONST(le_val) BSWAP_32_CONST(le_val)
|
|
|
+#define BE16_TO_CPU_CONST(le_val) BSWAP_16_CONST(le_val)
|
|
|
+#endif /* HAVE_LITTE_ENDIAN */
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* cpu_to_le64 - convert a uint64_t value to little-endian
|
|
|
* @native: value to convert
|
|
|
*/
|
|
|
static inline uint64_t cpu_to_le64(uint64_t native)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return native;
|
|
|
-#else
|
|
|
- return bswap_64(native);
|
|
|
-#endif
|
|
|
+ return CPU_TO_LE64_CONST(native);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -87,11 +218,7 @@ static inline uint64_t cpu_to_le64(uint64_t native)
|
|
|
*/
|
|
|
static inline uint32_t cpu_to_le32(uint32_t native)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return native;
|
|
|
-#else
|
|
|
- return bswap_32(native);
|
|
|
-#endif
|
|
|
+ return CPU_TO_LE32_CONST(native);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -100,11 +227,7 @@ static inline uint32_t cpu_to_le32(uint32_t native)
|
|
|
*/
|
|
|
static inline uint16_t cpu_to_le16(uint16_t native)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return native;
|
|
|
-#else
|
|
|
- return bswap_16(native);
|
|
|
-#endif
|
|
|
+ return CPU_TO_LE16_CONST(native);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -113,11 +236,7 @@ static inline uint16_t cpu_to_le16(uint16_t native)
|
|
|
*/
|
|
|
static inline uint64_t le64_to_cpu(uint64_t le_val)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return le_val;
|
|
|
-#else
|
|
|
- return bswap_64(le_val);
|
|
|
-#endif
|
|
|
+ return LE64_TO_CPU_CONST(le_val);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -126,11 +245,7 @@ static inline uint64_t le64_to_cpu(uint64_t le_val)
|
|
|
*/
|
|
|
static inline uint32_t le32_to_cpu(uint32_t le_val)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return le_val;
|
|
|
-#else
|
|
|
- return bswap_32(le_val);
|
|
|
-#endif
|
|
|
+ return LE32_TO_CPU_CONST(le_val);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -139,11 +254,7 @@ static inline uint32_t le32_to_cpu(uint32_t le_val)
|
|
|
*/
|
|
|
static inline uint16_t le16_to_cpu(uint16_t le_val)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return le_val;
|
|
|
-#else
|
|
|
- return bswap_16(le_val);
|
|
|
-#endif
|
|
|
+ return LE16_TO_CPU_CONST(le_val);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -152,11 +263,7 @@ static inline uint16_t le16_to_cpu(uint16_t le_val)
|
|
|
*/
|
|
|
static inline uint64_t cpu_to_be64(uint64_t native)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return bswap_64(native);
|
|
|
-#else
|
|
|
- return native;
|
|
|
-#endif
|
|
|
+ return CPU_TO_BE64_CONST(native);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -165,11 +272,7 @@ static inline uint64_t cpu_to_be64(uint64_t native)
|
|
|
*/
|
|
|
static inline uint32_t cpu_to_be32(uint32_t native)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return bswap_32(native);
|
|
|
-#else
|
|
|
- return native;
|
|
|
-#endif
|
|
|
+ return CPU_TO_BE32_CONST(native);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -178,11 +281,7 @@ static inline uint32_t cpu_to_be32(uint32_t native)
|
|
|
*/
|
|
|
static inline uint16_t cpu_to_be16(uint16_t native)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return bswap_16(native);
|
|
|
-#else
|
|
|
- return native;
|
|
|
-#endif
|
|
|
+ return CPU_TO_BE16_CONST(native);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -191,11 +290,7 @@ static inline uint16_t cpu_to_be16(uint16_t native)
|
|
|
*/
|
|
|
static inline uint64_t be64_to_cpu(uint64_t be_val)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return bswap_64(be_val);
|
|
|
-#else
|
|
|
- return be_val;
|
|
|
-#endif
|
|
|
+ return BE64_TO_CPU_CONST(be_val);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -204,11 +299,7 @@ static inline uint64_t be64_to_cpu(uint64_t be_val)
|
|
|
*/
|
|
|
static inline uint32_t be32_to_cpu(uint32_t be_val)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return bswap_32(be_val);
|
|
|
-#else
|
|
|
- return be_val;
|
|
|
-#endif
|
|
|
+ return BE32_TO_CPU_CONST(be_val);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -217,11 +308,7 @@ static inline uint32_t be32_to_cpu(uint32_t be_val)
|
|
|
*/
|
|
|
static inline uint16_t be16_to_cpu(uint16_t be_val)
|
|
|
{
|
|
|
-#if HAVE_LITTLE_ENDIAN
|
|
|
- return bswap_16(be_val);
|
|
|
-#else
|
|
|
- return be_val;
|
|
|
-#endif
|
|
|
+ return BE16_TO_CPU_CONST(be_val);
|
|
|
}
|
|
|
|
|
|
#endif /* CCAN_ENDIAN_H */
|