2 * Copyright 2012-2016 Nest Labs Inc. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * This file defines C++ functions for performing byte-swapping
20 * by value and in place by pointer for 16-, 32-, and 64-bit
25 #ifndef NLBYTEORDER_HPP
26 #define NLBYTEORDER_HPP
28 #include <nlbyteorder.h>
36 Unknown = nlByteOrderUnknown,
37 LittleEndian = nlByteOrderLittleEndian,
38 BigEndian = nlByteOrderBigEndian
42 * This represents a type for a byte ordering.
44 typedef nlByteOrder ByteOrder;
47 * This returns the byte order of the current system.
49 * @return The byte order of the current system.
51 inline ByteOrder GetCurrent(void)
53 return nlByteOrderGetCurrent();
57 * This unconditionally performs a byte order swap by value of the
58 * specified 16-bit value.
60 * @param[in] inValue The 16-bit value to be byte order swapped.
62 * @return The input value, byte order swapped.
64 inline uint16_t Swap16(uint16_t inValue)
66 return nlByteOrderValueSwap16(inValue);
70 * This unconditionally performs a byte order swap by value of the
71 * specified 32-bit value.
73 * @param[in] inValue The 32-bit value to be byte order swapped.
75 * @return The input value, byte order swapped.
77 inline uint32_t Swap32(uint32_t inValue)
79 return nlByteOrderValueSwap32(inValue);
83 * This unconditionally performs a byte order swap by value of the
84 * specified 64-bit value.
86 * @param[in] inValue The 64-bit value to be byte order swapped.
88 * @return The input value, byte order swapped.
90 inline uint64_t Swap64(uint64_t inValue)
92 return nlByteOrderValueSwap64(inValue);
96 * This unconditionally performs a byte order swap by pointer in place
97 * of the specified 16-bit value.
99 * @warning The input value is assumed to be on a natural alignment
100 * boundary for the target system. It is the responsibility of the
101 * caller to perform any necessary alignment to avoid system faults
102 * for systems that do not support unaligned accesses.
104 * @param[inout] inValue A pointer to the 16-bit value to be byte
107 inline void Swap16(uint16_t *inValue)
109 nlByteOrderPointerSwap16(inValue);
113 * This unconditionally performs a byte order swap by pointer in place
114 * of the specified 32-bit value.
116 * @warning The input value is assumed to be on a natural alignment
117 * boundary for the target system. It is the responsibility of the
118 * caller to perform any necessary alignment to avoid system faults
119 * for systems that do not support unaligned accesses.
121 * @param[inout] inValue A pointer to the 32-bit value to be byte
124 inline void Swap32(uint32_t *inValue)
126 nlByteOrderPointerSwap32(inValue);
130 * This unconditionally performs a byte order swap by pointer in place
131 * of the specified 64-bit value.
133 * @warning The input value is assumed to be on a natural alignment
134 * boundary for the target system. It is the responsibility of the
135 * caller to perform any necessary alignment to avoid system faults
136 * for systems that do not support unaligned accesses.
138 * @param[inout] inValue A pointer to the 64-bit value to be byte
141 inline void Swap64(uint64_t *inValue)
143 nlByteOrderPointerSwap64(inValue);
147 * This conditionally performs, as necessary for the target system, a
148 * byte order swap by value of the specified 16-bit value, presumed to
149 * be in little endian byte ordering to the target system (i.e. host)
152 * Consequently, on little endian target systems, this is a no-op and
153 * on big endian target systems, this performs a reordering.
155 * @param[in] inValue The 16-bit value to be byte order swapped.
157 * @return The input value, byte order swapped.
159 inline uint16_t Swap16LittleToHost(uint16_t inValue)
161 return nlByteOrderSwap16LittleToHost(inValue);
165 * This conditionally performs, as necessary for the target system, a
166 * byte order swap by value of the specified 32-bit value, presumed to
167 * be in little endian byte ordering to the target system (i.e. host)
170 * Consequently, on little endian target systems, this is a no-op and
171 * on big endian target systems, this performs a reordering.
173 * @param[in] inValue The 32-bit value to be byte order swapped.
175 * @return The input value, byte order swapped.
177 inline uint32_t Swap32LittleToHost(uint32_t inValue)
179 return nlByteOrderSwap32LittleToHost(inValue);
183 * This conditionally performs, as necessary for the target system, a
184 * byte order swap by value of the specified 64-bit value, presumed to
185 * be in little endian byte ordering to the target system (i.e. host)
188 * Consequently, on little endian target systems, this is a no-op and
189 * on big endian target systems, this performs a reordering.
191 * @param[in] inValue The 64-bit value to be byte order swapped.
193 * @return The input value, byte order swapped.
195 inline uint64_t Swap64LittleToHost(uint64_t inValue)
197 return nlByteOrderSwap64LittleToHost(inValue);
201 * This conditionally performs, as necessary for the target system, a
202 * byte order swap by value of the specified 16-bit value, presumed to
203 * be in target system (i.e. host) byte ordering to little endian byte
206 * Consequently, on little endian target systems, this is a no-op and
207 * on big endian target systems, this performs a reordering.
209 * @param[in] inValue The 16-bit value to be byte order swapped.
211 * @return The input value, byte order swapped.
213 inline uint16_t Swap16HostToLittle(uint16_t inValue)
215 return nlByteOrderSwap16HostToLittle(inValue);
219 * This conditionally performs, as necessary for the target system, a
220 * byte order swap by value of the specified 32-bit value, presumed to
221 * be in target system (i.e. host) byte ordering to little endian byte
224 * Consequently, on little endian target systems, this is a no-op and
225 * on big endian target systems, this performs a reordering.
227 * @param[in] inValue The 32-bit value to be byte order swapped.
229 * @return The input value, byte order swapped.
231 inline uint32_t Swap32HostToLittle(uint32_t inValue)
233 return nlByteOrderSwap32HostToLittle(inValue);
237 * This conditionally performs, as necessary for the target system, a
238 * byte order swap by value of the specified 64-bit value, presumed to
239 * be in target system (i.e. host) byte ordering to little endian byte
242 * Consequently, on little endian target systems, this is a no-op and
243 * on big endian target systems, this performs a reordering.
245 * @param[in] inValue The 64-bit value to be byte order swapped.
247 * @return The input value, byte order swapped.
249 inline uint64_t Swap64HostToLittle(uint64_t inValue)
251 return nlByteOrderSwap64HostToLittle(inValue);
255 * This conditionally performs, as necessary for the target system, a
256 * byte order swap by value of the specified 16-bit value, presumed to
257 * be in big endian byte ordering to the target system (i.e. host)
260 * Consequently, on little endian target systems, this performs a
261 * reordering and on big endian target systems, this is a no-op.
263 * @param[in] inValue The 16-bit value to be byte order swapped.
265 * @return The input value, byte order swapped.
267 inline uint16_t Swap16BigToHost(uint16_t inValue)
269 return nlByteOrderSwap16BigToHost(inValue);
273 * This conditionally performs, as necessary for the target system, a
274 * byte order swap by value of the specified 32-bit value, presumed to
275 * be in big endian byte ordering to the target system (i.e. host)
278 * Consequently, on little endian target systems, this performs a
279 * reordering and on big endian target systems, this is a no-op.
281 * @param[in] inValue The 32-bit value to be byte order swapped.
283 * @return The input value, byte order swapped.
285 inline uint32_t Swap32BigToHost(uint32_t inValue)
287 return nlByteOrderSwap32BigToHost(inValue);
291 * This conditionally performs, as necessary for the target system, a
292 * byte order swap by value of the specified 64-bit value, presumed to
293 * be in big endian byte ordering to the target system (i.e. host)
296 * Consequently, on little endian target systems, this performs a
297 * reordering and on big endian target systems, this is a no-op.
299 * @param[in] inValue The 64-bit value to be byte order swapped.
301 * @return The input value, byte order swapped.
303 inline uint64_t Swap64BigToHost(uint64_t inValue)
305 return nlByteOrderSwap64BigToHost(inValue);
309 * This conditionally performs, as necessary for the target system, a
310 * byte order swap by value of the specified 16-bit value, presumed to
311 * be in target system (i.e. host) byte ordering to big endian byte
314 * Consequently, on little endian target systems, this performs a
315 * reordering and on big endian target systems, this is a no-op.
317 * @param[in] inValue The 16-bit value to be byte order swapped.
319 * @return The input value, byte order swapped.
321 inline uint16_t Swap16HostToBig(uint16_t inValue)
323 return nlByteOrderSwap16HostToBig(inValue);
327 * This conditionally performs, as necessary for the target system, a
328 * byte order swap by value of the specified 32-bit value, presumed to
329 * be in target system (i.e. host) byte ordering to big endian byte
332 * Consequently, on little endian target systems, this performs a
333 * reordering and on big endian target systems, this is a no-op.
335 * @param[in] inValue The 32-bit value to be byte order swapped.
337 * @return The input value, byte order swapped.
339 inline uint32_t Swap32HostToBig(uint32_t inValue)
341 return nlByteOrderSwap32HostToBig(inValue);
345 * This conditionally performs, as necessary for the target system, a
346 * byte order swap by value of the specified 64-bit value, presumed to
347 * be in target system (i.e. host) byte ordering to big endian byte
350 * Consequently, on little endian target systems, this performs a
351 * reordering and on big endian target systems, this is a no-op.
353 * @param[in] inValue The 64-bit value to be byte order swapped.
355 * @return The input value, byte order swapped.
357 inline uint64_t Swap64HostToBig(uint64_t inValue)
359 return nlByteOrderSwap64HostToBig(inValue);
362 } // namespace ByteOrder
366 #endif // NLBYTEORDER_HPP