xdrpp
RFC4506 XDR compiler and message library
endian.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 /** \file endian.h Low-level byteswap and miscellaneous OS
4  * compatibility routines. */
5 
6 #ifndef _XDRC_ENDIAN_H_HEADER_INCLUDED_
7 #define _XDRC_ENDIAN_H_HEADER_INCLUDED_ 1
8 
9 #include <cstdint>
10 #include <string>
11 
12 namespace xdr {
13 
14 #if defined(_MSC_VER) && !defined(MSVC)
15 #define MSVC 1
16 #endif // _MSC_VER && !MSVC
17 
18 #if MSVC
19 #define Constexpr
20 // Dtor implicitly deleted due to base class deleted/inaccessible dtor:
21 #pragma warning(disable: 4624)
22 // Dtor will not be implicitly called if an exn is thrown:
23 #pragma warning(disable: 4594)
24 
25 inline std::string
26 errstr(int no)
27 {
28  char buf[1024];
29  if (strerror_s(buf, sizeof(buf), no) == 0) {
30  return std::string(buf);
31  }
32  return std::string();
33 }
34 #define xdr_strerror xdr::errstr
35 
36 #else // !MSVC
37 #define Constexpr constexpr
38 #define xdr_strerror std::strerror
39 #endif // !MSVC
40 
41 #ifndef XDRPP_WORDS_BIGENDIAN
42 #if MSVC
43 #define XDRPP_WORDS_BIGENDIAN 0
44 #else // !MSVC
45 #include <xdrpp/build_endian.h>
46 #endif // !MSVC
47 #endif // !XDRPP_WORDS_BIGENDIAN
48 
49 //! True on big endian machines, false on little endian machines.
50 #if XDRPP_WORDS_BIGENDIAN
51 Constexpr const bool is_big_endian = true;
52 #else // !XDRPP_WORDS_BIGENDIAN
53 Constexpr const bool is_big_endian = false;
54 #endif // !XDRPP_WORDS_BIGENDIAN
55 
56 // Needed on OpenBSD
57 #undef swap32
58 #undef swap64
59 
60 //! Byteswap 32-bit number.
61 Constexpr inline std::uint32_t
62 swap32(std::uint32_t v)
63 {
64  return v << 24 | (v & 0xff00) << 8 | (v >> 8 & 0xff00) | v >> 24;
65 }
66 
67 //! Byteswap 64-bit number.
68 Constexpr inline std::uint64_t
69 swap64(std::uint64_t v)
70 {
71  return std::uint64_t(swap32(std::uint32_t(v)))<<32 |
72  std::uint64_t(swap32(std::uint32_t(v>>32)));
73 }
74 
75 //! Byteswap 32-bit value only on little-endian machines, identity
76 //! function on big-endian machines.
77 Constexpr inline std::uint32_t
78 swap32le(std::uint32_t v)
79 {
80  return xdr::is_big_endian ? v : swap32(v);
81 }
82 
83 //! Byteswap 32-bit value only on big-endian machines.
84 Constexpr inline std::uint32_t
85 swap32be(std::uint32_t v)
86 {
87  return xdr::is_big_endian ? swap32(v) : v;
88 }
89 
90 //! Byteswap 64-bit value only on little-endian machines.
91 Constexpr inline std::uint64_t
92 swap64le(std::uint64_t v)
93 {
94  return xdr::is_big_endian ? v : swap64(v);
95 }
96 
97 //! Byteswap 64-bit value only on big-endian machines.
98 Constexpr inline std::uint64_t
99 swap64be(std::uint64_t v)
100 {
101  return xdr::is_big_endian ? swap64(v) : v;
102 }
103 
104 }
105 
106 #endif // !_XDRC_ENDIAN_H_HEADER_INCLUDED_
Constexpr const bool is_big_endian
True on big endian machines, false on little endian machines.
Definition: endian.h:53
Constexpr std::uint32_t swap32(std::uint32_t v)
Byteswap 32-bit number.
Definition: endian.h:62
Constexpr std::uint32_t swap32be(std::uint32_t v)
Byteswap 32-bit value only on big-endian machines.
Definition: endian.h:85
Endianness of build machine.
Most of the xdrpp library is encapsulated in the xdr namespace.
Definition: arpc.cc:4
Constexpr std::uint32_t swap32le(std::uint32_t v)
Byteswap 32-bit value only on little-endian machines, identity function on big-endian machines...
Definition: endian.h:78
Constexpr std::uint64_t swap64be(std::uint64_t v)
Byteswap 64-bit value only on big-endian machines.
Definition: endian.h:99
Constexpr std::uint64_t swap64le(std::uint64_t v)
Byteswap 64-bit value only on little-endian machines.
Definition: endian.h:92
Constexpr std::uint64_t swap64(std::uint64_t v)
Byteswap 64-bit number.
Definition: endian.h:69