]> git.proxmox.com Git - ceph.git/blame - ceph/src/include/byteorder.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / include / byteorder.h
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2
3#pragma once
4
20effc67 5#include <boost/endian/conversion.hpp>
7c673cae 6
20effc67 7#include "int_types.h"
7c673cae 8
7c673cae 9template<typename T>
20effc67
TL
10inline T swab(T val) {
11 return boost::endian::endian_reverse(val);
7c673cae 12}
7c673cae
FG
13
14template<typename T>
15struct ceph_le {
9f95a23c 16private:
7c673cae 17 T v;
9f95a23c 18public:
20effc67
TL
19 ceph_le() = default;
20 explicit ceph_le(T nv)
21 : v{boost::endian::native_to_little(nv)}
22 {}
7c673cae 23 ceph_le<T>& operator=(T nv) {
20effc67 24 v = boost::endian::native_to_little(nv);
7c673cae
FG
25 return *this;
26 }
20effc67 27 operator T() const { return boost::endian::little_to_native(v); }
9f95a23c
TL
28 friend inline bool operator==(ceph_le a, ceph_le b) {
29 return a.v == b.v;
30 }
7c673cae
FG
31} __attribute__ ((packed));
32
7c673cae
FG
33using ceph_le64 = ceph_le<__u64>;
34using ceph_le32 = ceph_le<__u32>;
35using ceph_le16 = ceph_le<__u16>;
36
f67539c2
TL
37using ceph_les64 = ceph_le<__s64>;
38using ceph_les32 = ceph_le<__s32>;
39using ceph_les16 = ceph_le<__s16>;
40
f67539c2
TL
41inline ceph_les64 init_les64(__s64 x) {
42 ceph_les64 v;
43 v = x;
44 return v;
45}
46inline ceph_les32 init_les32(__s32 x) {
47 ceph_les32 v;
48 v = x;
49 return v;
50}
51inline ceph_les16 init_les16(__s16 x) {
52 ceph_les16 v;
53 v = x;
54 return v;
55}