PS2SDK
PS2 Homebrew Libraries
byteorder.h
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
16 #ifndef BYTEORDER_H
17 #define BYTEORDER_H
18 
19 #include <stdint.h>
20 
21 #ifdef BIG_ENDIAN
22 inline uint64_t ntohll(x) { return x; }
23 inline uint32_t ntohl(x) { return x; }
24 inline uint16_t ntohs(x) { return x; }
25 #else
26 // LITTLE_ENDIAN
27 #ifndef htonll
28 inline uint64_t
29 htonll(uint64_t x)
30 {
31  return ((x & 0x00000000000000ffull) << 56) |
32  ((x & 0x000000000000ff00ull) << 40) |
33  ((x & 0x0000000000ff0000ull) << 24) |
34  ((x & 0x00000000ff000000ull) << 8) |
35  ((x & 0x000000ff00000000ull) >> 8) |
36  ((x & 0x0000ff0000000000ull) >> 24) |
37  ((x & 0x00ff000000000000ull) >> 40) |
38  ((x & 0xff00000000000000ull) >> 56);
39 }
40 #endif
41 
42 #ifndef htonl
43 inline uint32_t
44 htonl(uint32_t x)
45 {
46  return ((x & 0x000000ff) << 24) |
47  ((x & 0x0000ff00) << 8) |
48  ((x & 0x00ff0000) >> 8) |
49  ((x & 0xff000000) >> 24);
50 }
51 #endif
52 
53 #ifndef htons
54 inline uint16_t
55 htons(uint16_t x)
56 {
57  return ((x & 0x00ff) << 8) |
58  ((x & 0xff00) >> 8);
59 }
60 #endif
61 
62 #endif
63 
64 #endif /* BYTEORDER_H */