1*9712c20fSFrederick Mayle // -*- mode: c++ -*-
2*9712c20fSFrederick Mayle
3*9712c20fSFrederick Mayle // Copyright 2010 Google LLC
4*9712c20fSFrederick Mayle //
5*9712c20fSFrederick Mayle // Redistribution and use in source and binary forms, with or without
6*9712c20fSFrederick Mayle // modification, are permitted provided that the following conditions are
7*9712c20fSFrederick Mayle // met:
8*9712c20fSFrederick Mayle //
9*9712c20fSFrederick Mayle // * Redistributions of source code must retain the above copyright
10*9712c20fSFrederick Mayle // notice, this list of conditions and the following disclaimer.
11*9712c20fSFrederick Mayle // * Redistributions in binary form must reproduce the above
12*9712c20fSFrederick Mayle // copyright notice, this list of conditions and the following disclaimer
13*9712c20fSFrederick Mayle // in the documentation and/or other materials provided with the
14*9712c20fSFrederick Mayle // distribution.
15*9712c20fSFrederick Mayle // * Neither the name of Google LLC nor the names of its
16*9712c20fSFrederick Mayle // contributors may be used to endorse or promote products derived from
17*9712c20fSFrederick Mayle // this software without specific prior written permission.
18*9712c20fSFrederick Mayle //
19*9712c20fSFrederick Mayle // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*9712c20fSFrederick Mayle // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*9712c20fSFrederick Mayle // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22*9712c20fSFrederick Mayle // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23*9712c20fSFrederick Mayle // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24*9712c20fSFrederick Mayle // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25*9712c20fSFrederick Mayle // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26*9712c20fSFrederick Mayle // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27*9712c20fSFrederick Mayle // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28*9712c20fSFrederick Mayle // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29*9712c20fSFrederick Mayle // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*9712c20fSFrederick Mayle
31*9712c20fSFrederick Mayle // Original author: Jim Blandy <[email protected]> <[email protected]>
32*9712c20fSFrederick Mayle
33*9712c20fSFrederick Mayle // byteswap.h: Overloaded functions for conveniently byteswapping values.
34*9712c20fSFrederick Mayle
35*9712c20fSFrederick Mayle #ifndef COMMON_MAC_BYTESWAP_H_
36*9712c20fSFrederick Mayle #define COMMON_MAC_BYTESWAP_H_
37*9712c20fSFrederick Mayle
38*9712c20fSFrederick Mayle #ifdef __APPLE__
39*9712c20fSFrederick Mayle #include <libkern/OSByteOrder.h>
40*9712c20fSFrederick Mayle
ByteSwap(uint16_t v)41*9712c20fSFrederick Mayle static inline uint16_t ByteSwap(uint16_t v) { return OSSwapInt16(v); }
ByteSwap(uint32_t v)42*9712c20fSFrederick Mayle static inline uint32_t ByteSwap(uint32_t v) { return OSSwapInt32(v); }
ByteSwap(uint64_t v)43*9712c20fSFrederick Mayle static inline uint64_t ByteSwap(uint64_t v) { return OSSwapInt64(v); }
ByteSwap(int16_t v)44*9712c20fSFrederick Mayle static inline int16_t ByteSwap(int16_t v) { return OSSwapInt16(v); }
ByteSwap(int32_t v)45*9712c20fSFrederick Mayle static inline int32_t ByteSwap(int32_t v) { return OSSwapInt32(v); }
ByteSwap(int64_t v)46*9712c20fSFrederick Mayle static inline int64_t ByteSwap(int64_t v) { return OSSwapInt64(v); }
47*9712c20fSFrederick Mayle
48*9712c20fSFrederick Mayle #elif defined(__linux__)
49*9712c20fSFrederick Mayle // For NXByteOrder
50*9712c20fSFrederick Mayle #include <architecture/byte_order.h>
51*9712c20fSFrederick Mayle #include <stdint.h>
52*9712c20fSFrederick Mayle #include <endian.h>
53*9712c20fSFrederick Mayle #include_next <byteswap.h>
54*9712c20fSFrederick Mayle
ByteSwap(uint16_t v)55*9712c20fSFrederick Mayle static inline uint16_t ByteSwap(uint16_t v) { return bswap_16(v); }
ByteSwap(uint32_t v)56*9712c20fSFrederick Mayle static inline uint32_t ByteSwap(uint32_t v) { return bswap_32(v); }
ByteSwap(uint64_t v)57*9712c20fSFrederick Mayle static inline uint64_t ByteSwap(uint64_t v) { return bswap_64(v); }
ByteSwap(int16_t v)58*9712c20fSFrederick Mayle static inline int16_t ByteSwap(int16_t v) { return bswap_16(v); }
ByteSwap(int32_t v)59*9712c20fSFrederick Mayle static inline int32_t ByteSwap(int32_t v) { return bswap_32(v); }
ByteSwap(int64_t v)60*9712c20fSFrederick Mayle static inline int64_t ByteSwap(int64_t v) { return bswap_64(v); }
61*9712c20fSFrederick Mayle
NXHostByteOrder()62*9712c20fSFrederick Mayle static inline NXByteOrder NXHostByteOrder() {
63*9712c20fSFrederick Mayle #ifdef __LITTLE_ENDIAN
64*9712c20fSFrederick Mayle return NX_LittleEndian;
65*9712c20fSFrederick Mayle #else
66*9712c20fSFrederick Mayle return NX_BigEndian;
67*9712c20fSFrederick Mayle #endif
68*9712c20fSFrederick Mayle }
69*9712c20fSFrederick Mayle
70*9712c20fSFrederick Mayle #endif // __APPLE__
71*9712c20fSFrederick Mayle
72*9712c20fSFrederick Mayle #endif // COMMON_MAC_BYTESWAP_H_
73