CTR-Pia  5.4.3
Game Communication Engine
 全て クラス ネームスペース 関数 変数 型定義 列挙型 列挙型の値 ページ
common_ByteOrder.h
1 /*--------------------------------------------------------------------------------*
2  Copyright (C)Nintendo All rights reserved.
3 
4  These coded instructions, statements, and computer programs contain proprietary
5  information of Nintendo and/or its licensed developers and are protected by
6  national and international copyright laws. They may not be disclosed to third
7  parties or copied or duplicated in any form, in whole or in part, without the
8  prior written consent of Nintendo.
9 
10  The content herein is highly confidential and should be handled accordingly.
11  *--------------------------------------------------------------------------------*/
12 
13 
14 #pragma once
15 
16 #include <nn/pia/common/common_Definitions.h>
17 
18 
19 namespace nn
20 {
21 namespace pia
22 {
23 namespace common
24 {
25 
26 
27 /*!
28  @brief バイトオーダー変換
29 
30  */
31 class ByteOrder
32 {
33 public:
34  static inline uint16_t Swap16(uint16_t v)
35  {
36  return (v << 8) | (v >> 8);
37  }
38 
39  static inline uint32_t Swap32(uint32_t v)
40  {
41  uint32_t w = ((v & 0xff00ff00) >> 8) | ((v & 0x00ff00ff) << 8);
42  return (w >> 16) | (w << 16);
43  }
44 
45  static inline uint64_t Swap64(uint64_t v)
46  {
47  uint64_t w = ((v & 0xff00ff00ff00ff00) >> 8) | ((v & 0x00ff00ff00ff00ff) << 8);
48  uint64_t x = ((w & 0xffff0000ffff0000) >> 16) | ((w & 0x0000ffff0000ffff) << 16);
49  return (x >> 32) | (x << 32);
50  }
51 
52  /*!
53  @brief uint16_t の値をネットワークバイトオーダーからホストバイトオーダーに変換
54 
55  @param[in] net ネットワークバイトオーダーの uint16_t の値です。
56 
57  @return ホストバイトオーダーの uint16_t の値です。
58  */
59  static inline uint16_t NetworkToHost16(uint16_t net)
60  {
61 #if NN_PIA_ENDIAN_BIG
62  return net;
63 #else
64  return Swap16(net);
65 #endif
66  }
67 
68 
69  /*!
70  @brief uint32_t の値をネットワークバイトオーダーからホストバイトオーダーに変換
71 
72  @param[in] net ネットワークバイトオーダーの uint32_t の値です。
73 
74  @return ホストバイトオーダーの uint32_t の値です。
75  */
76  static inline uint32_t NetworkToHost32(uint32_t net)
77  {
78 #if NN_PIA_ENDIAN_BIG
79  return net;
80 #else
81  return Swap32(net);
82 #endif
83  }
84 
85 
86  /*!
87  @brief uint64_t の値をネットワークバイトオーダーからホストバイトオーダーに変換
88 
89  @param[in] net ネットワークバイトオーダーの uint64_t の値です。
90 
91  @return ホストバイトオーダーの uint64_t の値です。
92  */
93  static inline uint64_t NetworkToHost64(uint64_t net)
94  {
95 #if NN_PIA_ENDIAN_BIG
96  return net;
97 #else
98  return Swap64(net);
99 #endif
100  }
101 
102 
103  /*!
104  @brief uint16_t の値をホストバイトオーダーからネットワークバイトオーダーに変換
105 
106  @param[in] host ホストバイトオーダーの uint16_t の値です。
107 
108  @return ネットワークバイトオーダーの uint16_t の値です。
109  */
110  static inline uint16_t HostToNetwork16(uint16_t host)
111  {
112 #if NN_PIA_ENDIAN_BIG
113  return host;
114 #else
115  return Swap16(host);
116 #endif
117  }
118 
119 
120  /*!
121  @brief uint32_t の値をホストバイトオーダーからネットワークバイトオーダーに変換
122 
123  @param[in] host ホストバイトオーダーの uint32_t の値です。
124 
125  @return ネットワークバイトオーダーの uint32_t の値です。
126  */
127  static inline uint32_t HostToNetwork32(uint32_t host)
128  {
129 #if NN_PIA_ENDIAN_BIG
130  return host;
131 #else
132  return Swap32(host);
133 #endif
134  }
135 
136 
137  /*!
138  @brief uint64_t の値をホストバイトオーダーからネットワークバイトオーダーに変換
139 
140  @param[in] host ホストバイトオーダーの uint64_t の値です。
141 
142  @return ネットワークバイトオーダーの uint64_t の値です。
143  */
144  static inline uint64_t HostToNetwork64(uint64_t host)
145  {
146 #if NN_PIA_ENDIAN_BIG
147  return host;
148 #else
149  return Swap64(host);
150 #endif
151  }
152 
153  /*!
154  @brief ネットワークバイトオーダー(ビッグエンディアン)にシリアライズします
155  */
156  static inline void serializeU64(uint8_t* pData, uint64_t a)
157  {
158  pData[0] = static_cast<uint8_t>((a >> 56) & 0xff);
159  pData[1] = static_cast<uint8_t>((a >> 48) & 0xff);
160  pData[2] = static_cast<uint8_t>((a >> 40) & 0xff);
161  pData[3] = static_cast<uint8_t>((a >> 32) & 0xff);
162  pData[4] = static_cast<uint8_t>((a >> 24) & 0xff);
163  pData[5] = static_cast<uint8_t>((a >> 16) & 0xff);
164  pData[6] = static_cast<uint8_t>((a >> 8) & 0xff);
165  pData[7] = static_cast<uint8_t>((a >> 0) & 0xff);
166  }
167 
168  static inline uint64_t deserializeU64(const uint8_t* pData)
169  {
170  uint64_t tmp = 0;
171  tmp |= static_cast<uint64_t>(pData[0]) << 56;
172  tmp |= static_cast<uint64_t>(pData[1]) << 48;
173  tmp |= static_cast<uint64_t>(pData[2]) << 40;
174  tmp |= static_cast<uint64_t>(pData[3]) << 32;
175  tmp |= static_cast<uint64_t>(pData[4]) << 24;
176  tmp |= static_cast<uint64_t>(pData[5]) << 16;
177  tmp |= static_cast<uint64_t>(pData[6]) << 8;
178  tmp |= static_cast<uint64_t>(pData[7]) << 0;
179  return tmp;
180  }
181 
182  static inline void serializeU32(uint8_t* pData, uint32_t a)
183  {
184  pData[0] = static_cast<uint8_t>((a & 0xff000000) >> 24);
185  pData[1] = static_cast<uint8_t>((a & 0x00ff0000) >> 16);
186  pData[2] = static_cast<uint8_t>((a & 0x0000ff00) >> 8);
187  pData[3] = static_cast<uint8_t>((a & 0x000000ff) >> 0);
188  }
189 
190  static inline uint32_t deserializeU32(const uint8_t* pData)
191  {
192  return (static_cast<uint32_t>(pData[0]) << 24) |
193  (static_cast<uint32_t>(pData[1]) << 16) |
194  (static_cast<uint32_t>(pData[2]) << 8) |
195  (static_cast<uint32_t>(pData[3]) << 0);
196  }
197 
198  static inline void serializeU16(uint8_t* pData, uint16_t a)
199  {
200  pData[0] = static_cast<uint8_t>((a & 0xff00) >> 8);
201  pData[1] = static_cast<uint8_t>((a & 0x00ff) >> 0);
202  }
203 
204  static inline uint16_t deserializeU16(const uint8_t* pData)
205  {
206  return (static_cast<uint16_t>(pData[0]) << 8) |
207  (static_cast<uint16_t>(pData[1]) << 0);
208  }
209 
210  //n1589:1 byteはシリアライズ関数なくても問題ないが、コーディングを楽にするため
211  static inline void serializeU8(uint8_t* pData, uint8_t a)
212  {
213  pData[0] = a;
214  }
215 
216  static inline uint8_t deserializeU8(const uint8_t* pData)
217  {
218  return pData[0];
219  }
220 
221  /*!
222  @cond PRIVATE
223  @brief 指定したバイト列をバイト反転させます。
224  @tparam Size 反転させるバイト列のバイト数です。1,2,4,8のいずれかのみ指定できます。
225  @param[out] pTo 反転させた結果を書き込むアドレスを指定します。
226  @param[in] cpFrom 反転させるバイト列を指定します。
227  */
228  template <uint32_t Size>
229  static inline void Swap(void* pTo, const void* cpFrom);
230  //! @endcond
231 };
232 
233 
234 //! @cond
235 template <>
236 inline void ByteOrder::Swap<1>(void* pTo, const void* cpFrom)
237 {
238  *reinterpret_cast<uint8_t*>(pTo) = *reinterpret_cast<const uint8_t*>(cpFrom);
239 }
240 
241 template <>
242 inline void ByteOrder::Swap<2>(void* pTo, const void* cpFrom)
243 {
244  *reinterpret_cast<uint16_t*>(pTo) = Swap16(*reinterpret_cast<const uint16_t*>(cpFrom));
245 }
246 
247 template <>
248 inline void ByteOrder::Swap<4>(void* pTo, const void* cpFrom)
249 {
250  *reinterpret_cast<uint32_t*>(pTo) = Swap32(*reinterpret_cast<const uint32_t*>(cpFrom));
251 }
252 
253 template <>
254 inline void ByteOrder::Swap<8>(void* pTo, const void* cpFrom)
255 {
256  reinterpret_cast<uint32_t*>(pTo)[0] = Swap32(reinterpret_cast<const uint32_t*>(cpFrom)[1]);
257  reinterpret_cast<uint32_t*>(pTo)[1] = Swap32(reinterpret_cast<const uint32_t*>(cpFrom)[0]);
258 }
259 //! @endcond
260 }
261 }
262 } // end of namespace nn::pia::common