CTR Pia  4.11.3
Game Communication Engine
common_ByteOrder.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: common_ByteOrder.h
4 
5  Copyright Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 
14 
15 #pragma once
16 
17 #include <pia/common/common_definitions.h>
18 
19 
20 namespace nn
21 {
22 namespace pia
23 {
24 namespace common
25 {
26 
27 
28 /*!
29 @brief Converts the byte order.
30 
31 @date 2012-04-04 Initial version.
32 */
33 class ByteOrder
34 {
35 public:
36  static inline u16 Swap16(u16 v)
37  {
38  return (v << 8) | (v >> 8);
39  }
40 
41  static inline u32 Swap32(u32 v)
42  {
43  u32 w = ((v & 0xff00ff00) >> 8) | ((v & 0x00ff00ff) << 8);
44  return (w >> 16) | (w << 16);
45  }
46 
47  static inline u64 Swap64(u64 v)
48  {
49  u64 w = ((v & 0xff00ff00ff00ff00) >> 8) | ((v & 0x00ff00ff00ff00ff) << 8);
50  u64 x = ((w & 0xffff0000ffff0000) >> 16) | ((w & 0x0000ffff0000ffff) << 16);
51  return (x >> 32) | (x << 32);
52  }
53 
54 /*!
55 @brief Converts a <tt>u16</tt> value from network byte order to host byte order.
56 
57 @param[in] net Specifies the <tt>u16</tt> value in network byte order.
58 
59 @return Returns the <tt>u16</tt> value in host byte order.
60 */
61  static inline u16 NetworkToHost16(u16 net)
62  {
63 #if NN_PIA_ENDIAN_BIG
64  return net;
65 #else
66  return Swap16(net);
67 #endif
68  }
69 
70 
71 /*!
72 @brief Converts a <tt>u32</tt> value from network byte order to host byte order.
73 
74 @param[in] net Specifies the <tt>u32</tt> value in network byte order.
75 
76 @return Returns the <tt>u32</tt> value in host byte order.
77 */
78  static inline u32 NetworkToHost32(u32 net)
79  {
80 #if NN_PIA_ENDIAN_BIG
81  return net;
82 #else
83  return Swap32(net);
84 #endif
85  }
86 
87 
88 /*!
89 @brief Converts a <tt>u64</tt> value from network byte order to host byte order.
90 
91 @param[in] net Specifies the <tt>u64</tt> value in network byte order.
92 
93 @return Returns the <tt>u64</tt> value in host byte order.
94 */
95  static inline u64 NetworkToHost64(u64 net)
96  {
97 #if NN_PIA_ENDIAN_BIG
98  return net;
99 #else
100  return Swap64(net);
101 #endif
102  }
103 
104 
105 /*!
106 @brief Converts a <tt>u16</tt> value from host byte order to network byte order.
107 
108 @param[in] host Specifies the <tt>u16</tt> value in host byte order.
109 
110 @return Returns the <tt>u16</tt> value in network byte order.
111 */
112  static inline u16 HostToNetwork16(u16 host)
113  {
114 #if NN_PIA_ENDIAN_BIG
115  return host;
116 #else
117  return Swap16(host);
118 #endif
119  }
120 
121 
122 /*!
123 @brief Converts a <tt>u32</tt> value from host byte order to network byte order.
124 
125 @param[in] host Specifies the <tt>u32</tt> value in host byte order.
126 
127 @return Returns the <tt>u32</tt> value in network byte order.
128 */
129  static inline u32 HostToNetwork32(u32 host)
130  {
131 #if NN_PIA_ENDIAN_BIG
132  return host;
133 #else
134  return Swap32(host);
135 #endif
136  }
137 
138 
139 /*!
140 @brief Converts a <tt>u64</tt> value from host byte order to network byte order.
141 
142 @param[in] host Specifies the <tt>u64</tt> value in host byte order.
143 
144 @return Returns the <tt>u64</tt> value in network byte order.
145 */
146  static inline u64 HostToNetwork64(u64 host)
147  {
148 #if NN_PIA_ENDIAN_BIG
149  return host;
150 #else
151  return Swap64(host);
152 #endif
153  }
154 
155 
156 /*!
157 @cond PRIVATE
158 @brief Reverses the order of bytes in the specified byte array.
159 @tparam Size Specifies the number of bytes in the byte array being reversed. You can only specify one of <tt>1</tt>, <tt>2</tt>, <tt>4</tt>, or <tt>8</tt>.
160 @param[out] pTo Specifies the address to write the result of conversion to.
161 @param[in] cpFrom Specifies the byte array to be converted.
162 */
163  template <size_t Size>
164  static inline void Swap(void* pTo, const void* cpFrom);
165  //! @endcond
166 };
167 
168 
169 //! @cond
170 template <>
171 inline void ByteOrder::Swap<1>(void* pTo, const void* cpFrom)
172 {
173  *reinterpret_cast<u8*>(pTo) = *reinterpret_cast<const u8*>(cpFrom);
174 }
175 
176 template <>
177 inline void ByteOrder::Swap<2>(void* pTo, const void* cpFrom)
178 {
179  *reinterpret_cast<u16*>(pTo) = Swap16(*reinterpret_cast<const u16*>(cpFrom));
180 }
181 
182 template <>
183 inline void ByteOrder::Swap<4>(void* pTo, const void* cpFrom)
184 {
185  *reinterpret_cast<u32*>(pTo) = Swap32(*reinterpret_cast<const u32*>(cpFrom));
186 }
187 
188 template <>
189 inline void ByteOrder::Swap<8>(void* pTo, const void* cpFrom)
190 {
191  reinterpret_cast<u32*>(pTo)[0] = Swap32(reinterpret_cast<const u32*>(cpFrom)[1]);
192  reinterpret_cast<u32*>(pTo)[1] = Swap32(reinterpret_cast<const u32*>(cpFrom)[0]);
193 }
194 //! @endcond
195 }
196 }
197 } // end of namespace nn::pia::common
static u16 HostToNetwork16(u16 host)
Converts a u16 value from host byte order to network byte order.
Definition: common_ByteOrder.h:112
static u64 HostToNetwork64(u64 host)
Converts a u64 value from host byte order to network byte order.
Definition: common_ByteOrder.h:146
Definition: assert.h:115
Converts the byte order.
Definition: common_ByteOrder.h:33
static u32 NetworkToHost32(u32 net)
Converts a u32 value from network byte order to host byte order.
Definition: common_ByteOrder.h:78
static u64 NetworkToHost64(u64 net)
Converts a u64 value from network byte order to host byte order.
Definition: common_ByteOrder.h:95
static u32 HostToNetwork32(u32 host)
Converts a u32 value from host byte order to network byte order.
Definition: common_ByteOrder.h:129
static u16 NetworkToHost16(u16 net)
Converts a u16 value from network byte order to host byte order.
Definition: common_ByteOrder.h:61