Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Endian.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_ENDIAN_H
21 #define GALOIS_ENDIAN_H
22 
23 #include <cstdint>
24 
25 #include "galois/config.h"
26 
27 namespace galois {
28 
29 static inline uint32_t bswap32(uint32_t x) {
30 #if defined(__GNUC__) || defined(__clang__)
31  return __builtin_bswap32(x);
32 #else
33  return ((x << 24) & 0xff000000) | ((x << 8) & 0x00ff0000) |
34  ((x >> 8) & 0x0000ff00) | ((x >> 24) & 0x000000ff);
35 #endif
36 }
37 
38 static inline uint64_t bswap64(uint64_t x) {
39 #if defined(__GNUC__) || defined(__clang__)
40  return __builtin_bswap64(x);
41 #else
42  return ((x << 56) & 0xff00000000000000UL) |
43  ((x << 40) & 0x00ff000000000000UL) |
44  ((x << 24) & 0x0000ff0000000000UL) |
45  ((x << 8) & 0x000000ff00000000UL) | ((x >> 8) & 0x00000000ff000000UL) |
46  ((x >> 24) & 0x0000000000ff0000UL) |
47  ((x >> 40) & 0x000000000000ff00UL) |
48  ((x >> 56) & 0x00000000000000ffUL);
49 #endif
50 }
51 
52 // NB: Wrap these standard functions with different names because
53 // sometimes le64toh and such are implemented as macros and we don't
54 // want any nasty surprises.
55 static inline uint64_t convert_le64toh(uint64_t x) {
56 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
57  return x;
58 #else
59  return bswap64(x);
60 #endif
61 }
62 
63 static inline uint32_t convert_le32toh(uint32_t x) {
64 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
65  return x;
66 #else
67  return bswap32(x);
68 #endif
69 }
70 
71 static inline uint64_t convert_htobe64(uint64_t x) {
72 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
73  return x;
74 #else
75  return bswap64(x);
76 #endif
77 }
78 
79 static inline uint32_t convert_htobe32(uint32_t x) {
80 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
81  return x;
82 #else
83  return bswap32(x);
84 #endif
85 }
86 
87 static inline uint64_t convert_htole64(uint64_t x) {
88 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
89  return x;
90 #else
91  return bswap64(x);
92 #endif
93 }
94 
95 static inline uint32_t convert_htole32(uint32_t x) {
96 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
97  return x;
98 #else
99  return bswap32(x);
100 #endif
101 }
102 
103 } // namespace galois
104 #endif