summaryrefslogtreecommitdiffstats
path: root/src/include/byteorder.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/include/byteorder.h
parentInitial commit. (diff)
downloadceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz
ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--src/include/byteorder.h109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/include/byteorder.h b/src/include/byteorder.h
new file mode 100644
index 00000000..85268543
--- /dev/null
+++ b/src/include/byteorder.h
@@ -0,0 +1,109 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+
+#pragma once
+
+#include <type_traits>
+#include "acconfig.h"
+#include "int_types.h"
+
+
+#ifdef __GNUC__
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint16_t), T>::type
+swab(T val) {
+ return __builtin_bswap16(val);
+}
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint32_t), T>::type
+swab(T val) {
+ return __builtin_bswap32(val);
+}
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint64_t), T>::type
+swab(T val) {
+ return __builtin_bswap64(val);
+}
+#else
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint16_t), T>::type
+swab(T val) {
+ return (val >> 8) | (val << 8);
+}
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint32_t), T>::type
+swab(T val) {
+ return (( val >> 24) |
+ ((val >> 8) & 0xff00) |
+ ((val << 8) & 0xff0000) |
+ ((val << 24)));
+}
+template<typename T>
+inline typename std::enable_if<sizeof(T) == sizeof(uint64_t), T>::type
+swab(T val) {
+ return (( val >> 56) |
+ ((val >> 40) & 0xff00ull) |
+ ((val >> 24) & 0xff0000ull) |
+ ((val >> 8) & 0xff000000ull) |
+ ((val << 8) & 0xff00000000ull) |
+ ((val << 24) & 0xff0000000000ull) |
+ ((val << 40) & 0xff000000000000ull) |
+ ((val << 56)));
+}
+#endif
+
+// mswab == maybe swab (if not LE)
+#ifdef CEPH_BIG_ENDIAN
+template<typename T>
+inline T mswab(T val) {
+ return swab(val);
+}
+#else
+template<typename T>
+inline T mswab(T val) {
+ return val;
+}
+#endif
+
+template<typename T>
+struct ceph_le {
+ T v;
+ ceph_le<T>& operator=(T nv) {
+ v = mswab(nv);
+ return *this;
+ }
+ operator T() const { return mswab(v); }
+} __attribute__ ((packed));
+
+template<typename T>
+inline bool operator==(ceph_le<T> a, ceph_le<T> b) {
+ return a.v == b.v;
+}
+
+using ceph_le64 = ceph_le<__u64>;
+using ceph_le32 = ceph_le<__u32>;
+using ceph_le16 = ceph_le<__u16>;
+
+inline ceph_le64 init_le64(__u64 x) {
+ ceph_le64 v;
+ v = x;
+ return v;
+}
+inline ceph_le32 init_le32(__u32 x) {
+ ceph_le32 v;
+ v = x;
+ return v;
+}
+inline ceph_le16 init_le16(__u16 x) {
+ ceph_le16 v;
+ v = x;
+ return v;
+}
+
+ /*
+#define cpu_to_le64(x) (x)
+#define cpu_to_le32(x) (x)
+#define cpu_to_le16(x) (x)
+ */
+#define le64_to_cpu(x) ((uint64_t)x)
+#define le32_to_cpu(x) ((__u32)x)
+#define le16_to_cpu(x) ((__u16)x)