tutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  copyright : (C) 2013 by Tsuda Kageyu
3  email : tsuda.kageyu@gmail.com
4  ***************************************************************************/
5 
6 /***************************************************************************
7  * This library is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU Lesser General Public License version *
9  * 2.1 as published by the Free Software Foundation. *
10  * *
11  * This library is distributed in the hope that it will be useful, but *
12  * WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the Free Software *
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
19  * 02110-1301 USA *
20  * *
21  * Alternatively, this file is available under the Mozilla Public *
22  * License Version 1.1. You may obtain a copy of the License at *
23  * http://www.mozilla.org/MPL/ *
24  ***************************************************************************/
25 
26 #ifndef TAGLIB_TUTILS_H
27 #define TAGLIB_TUTILS_H
28 
29 // THIS FILE IS NOT A PART OF THE TAGLIB API
30 
31 #ifndef DO_NOT_DOCUMENT // tell Doxygen not to document this header
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36 
37 #if defined(HAVE_MSC_BYTESWAP)
38 # include <stdlib.h>
39 #elif defined(HAVE_GLIBC_BYTESWAP)
40 # include <byteswap.h>
41 #elif defined(HAVE_MAC_BYTESWAP)
42 # include <libkern/OSByteOrder.h>
43 #elif defined(HAVE_OPENBSD_BYTESWAP)
44 # include <sys/endian.h>
45 #endif
46 
47 namespace TagLib
48 {
49  namespace Utils
50  {
51  inline ushort byteSwap(ushort x)
52  {
53 #if defined(HAVE_GCC_BYTESWAP_16)
54 
55  return __builtin_bswap16(x);
56 
57 #elif defined(HAVE_MSC_BYTESWAP)
58 
59  return _byteswap_ushort(x);
60 
61 #elif defined(HAVE_GLIBC_BYTESWAP)
62 
63  return __bswap_16(x);
64 
65 #elif defined(HAVE_MAC_BYTESWAP)
66 
67  return OSSwapInt16(x);
68 
69 #elif defined(HAVE_OPENBSD_BYTESWAP)
70 
71  return swap16(x);
72 
73 #else
74 
75  return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
76 
77 #endif
78  }
79 
80  inline uint byteSwap(uint x)
81  {
82 #if defined(HAVE_GCC_BYTESWAP_32)
83 
84  return __builtin_bswap32(x);
85 
86 #elif defined(HAVE_MSC_BYTESWAP)
87 
88  return _byteswap_ulong(x);
89 
90 #elif defined(HAVE_GLIBC_BYTESWAP)
91 
92  return __bswap_32(x);
93 
94 #elif defined(HAVE_MAC_BYTESWAP)
95 
96  return OSSwapInt32(x);
97 
98 #elif defined(HAVE_OPENBSD_BYTESWAP)
99 
100  return swap32(x);
101 
102 #else
103 
104  return ((x & 0xff000000u) >> 24)
105  | ((x & 0x00ff0000u) >> 8)
106  | ((x & 0x0000ff00u) << 8)
107  | ((x & 0x000000ffu) << 24);
108 
109 #endif
110  }
111 
112  inline ulonglong byteSwap(ulonglong x)
113  {
114 #if defined(HAVE_GCC_BYTESWAP_64)
115 
116  return __builtin_bswap64(x);
117 
118 #elif defined(HAVE_MSC_BYTESWAP)
119 
120  return _byteswap_uint64(x);
121 
122 #elif defined(HAVE_GLIBC_BYTESWAP)
123 
124  return __bswap_64(x);
125 
126 #elif defined(HAVE_MAC_BYTESWAP)
127 
128  return OSSwapInt64(x);
129 
130 #elif defined(HAVE_OPENBSD_BYTESWAP)
131 
132  return swap64(x);
133 
134 #else
135 
136  return ((x & 0xff00000000000000ull) >> 56)
137  | ((x & 0x00ff000000000000ull) >> 40)
138  | ((x & 0x0000ff0000000000ull) >> 24)
139  | ((x & 0x000000ff00000000ull) >> 8)
140  | ((x & 0x00000000ff000000ull) << 8)
141  | ((x & 0x0000000000ff0000ull) << 24)
142  | ((x & 0x000000000000ff00ull) << 40)
143  | ((x & 0x00000000000000ffull) << 56);
144 
145 #endif
146  }
147 
148  enum ByteOrder
149  {
150  LittleEndian,
151  BigEndian
152  };
153 
154 #ifdef SYSTEM_BYTEORDER
155 
156 # if SYSTEM_BYTEORDER == 1
157 
158  const ByteOrder SystemByteOrder = LittleEndian;
159 
160 # else
161 
162  const ByteOrder SystemByteOrder = BigEndian;
163 
164 # endif
165 
166 #else
167 
168  inline ByteOrder systemByteOrder()
169  {
170  union {
171  int i;
172  char c;
173  } u;
174 
175  u.i = 1;
176  if(u.c == 1)
177  return LittleEndian;
178  else
179  return BigEndian;
180  }
181 
182  const ByteOrder SystemByteOrder = systemByteOrder();
183 
184 #endif
185  }
186 }
187 
188 #endif
189 
190 #endif