X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Finclude%2Fbyteorder.h;fp=src%2Fceph%2Fsrc%2Finclude%2Fbyteorder.h;h=8674860166864936da9aa7db9bfc8b293a1630a0;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/include/byteorder.h b/src/ceph/src/include/byteorder.h new file mode 100644 index 0000000..8674860 --- /dev/null +++ b/src/ceph/src/include/byteorder.h @@ -0,0 +1,103 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- + +#pragma once + +#include +#include "acconfig.h" +#include "int_types.h" + + +#ifdef __GNUC__ +template +inline typename std::enable_if::type +swab(T val) { + return __builtin_bswap16(val); +} +template +inline typename std::enable_if::type +swab(T val) { + return __builtin_bswap32(val); +} +template +inline typename std::enable_if::type +swab(T val) { + return __builtin_bswap64(val); +} +#else +template +inline typename std::enable_if::type +swab(T val) { + return (val >> 8) | (val << 8); +} +template +inline typename std::enable_if::type +swab(T val) { + return (( val >> 24) | + ((val >> 8) & 0xff00) | + ((val << 8) & 0xff0000) | + ((val << 24))); +} +template +inline typename std::enable_if::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 +inline T mswab(T val) { + return swab(val); +} +#else +template +inline T mswab(T val) { + return val; +} +#endif + +template +struct ceph_le { + T v; + ceph_le& operator=(T nv) { + v = mswab(nv); + return *this; + } + operator T() const { return mswab(v); } +} __attribute__ ((packed)); + +template +inline bool operator==(ceph_le a, ceph_le 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 __u64 init_le64(__u64 x) { + return mswab<__u64>(x); +} +inline __u32 init_le32(__u32 x) { + return mswab<__u32>(x); +} +inline __u16 init_le16(__u16 x) { + return mswab<__u16>(x); +} + + /* +#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)