From: Kefu Chai Date: Wed, 10 Mar 2021 05:05:32 +0000 (+0800) Subject: include/byteorder.h: rewrite swab() using boost::endian X-Git-Tag: v17.1.0~2643^2~7 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=144e49052bdb2eacd9424115532fc4338801877b;p=ceph.git include/byteorder.h: rewrite swab() using boost::endian boost::endian also uses the compiler intrinsics when available, so no performance regressions. and the readability is improved. Signed-off-by: Kefu Chai --- diff --git a/src/include/byteorder.h b/src/include/byteorder.h index 4062c2d4c2e9..f934145cb7ba 100644 --- a/src/include/byteorder.h +++ b/src/include/byteorder.h @@ -2,67 +2,14 @@ #pragma once -#include -#include "acconfig.h" -#include "int_types.h" - +#include -#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 +#include "int_types.h" -// 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; +inline T swab(T val) { + return boost::endian::endian_reverse(val); } -#endif template struct ceph_le { @@ -70,10 +17,10 @@ private: T v; public: ceph_le& operator=(T nv) { - v = mswab(nv); + v = boost::endian::native_to_little(nv); return *this; } - operator T() const { return mswab(v); } + operator T() const { return boost::endian::little_to_native(v); } friend inline bool operator==(ceph_le a, ceph_le b) { return a.v == b.v; }