tutils.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     copyright            : (C) 2013 by Tsuda Kageyu
00003     email                : tsuda.kageyu@gmail.com
00004  ***************************************************************************/
00005 
00006 /***************************************************************************
00007  *   This library is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU Lesser General Public License version   *
00009  *   2.1 as published by the Free Software Foundation.                     *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful, but   *
00012  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the Free Software   *
00018  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
00019  *   02110-1301  USA                                                       *
00020  *                                                                         *
00021  *   Alternatively, this file is available under the Mozilla Public        *
00022  *   License Version 1.1.  You may obtain a copy of the License at         *
00023  *   http://www.mozilla.org/MPL/                                           *
00024  ***************************************************************************/
00025 
00026 #ifndef TAGLIB_TUTILS_H
00027 #define TAGLIB_TUTILS_H
00028 
00029 // THIS FILE IS NOT A PART OF THE TAGLIB API
00030 
00031 #ifndef DO_NOT_DOCUMENT  // tell Doxygen not to document this header
00032 
00033 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036 
00037 #if defined(HAVE_MSC_BYTESWAP)
00038 # include <stdlib.h>
00039 #elif defined(HAVE_GLIBC_BYTESWAP)
00040 # include <byteswap.h>
00041 #elif defined(HAVE_MAC_BYTESWAP)
00042 # include <libkern/OSByteOrder.h>
00043 #elif defined(HAVE_OPENBSD_BYTESWAP)
00044 # include <sys/endian.h>
00045 #endif
00046 
00047 namespace TagLib
00048 {
00049   namespace Utils
00050   {
00051     inline ushort byteSwap(ushort x)
00052     {
00053 #if defined(HAVE_GCC_BYTESWAP_16)
00054 
00055       return __builtin_bswap16(x);
00056 
00057 #elif defined(HAVE_MSC_BYTESWAP)
00058 
00059       return _byteswap_ushort(x);
00060 
00061 #elif defined(HAVE_GLIBC_BYTESWAP)
00062 
00063       return __bswap_16(x);
00064 
00065 #elif defined(HAVE_MAC_BYTESWAP)
00066 
00067       return OSSwapInt16(x);
00068 
00069 #elif defined(HAVE_OPENBSD_BYTESWAP)
00070 
00071       return swap16(x);
00072 
00073 #else
00074 
00075       return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
00076 
00077 #endif
00078     }
00079 
00080     inline uint byteSwap(uint x)
00081     {
00082 #if defined(HAVE_GCC_BYTESWAP_32)
00083 
00084       return __builtin_bswap32(x);
00085 
00086 #elif defined(HAVE_MSC_BYTESWAP)
00087 
00088       return _byteswap_ulong(x);
00089 
00090 #elif defined(HAVE_GLIBC_BYTESWAP)
00091 
00092       return __bswap_32(x);
00093 
00094 #elif defined(HAVE_MAC_BYTESWAP)
00095 
00096       return OSSwapInt32(x);
00097 
00098 #elif defined(HAVE_OPENBSD_BYTESWAP)
00099 
00100       return swap32(x);
00101 
00102 #else
00103 
00104       return ((x & 0xff000000u) >> 24)
00105         | ((x & 0x00ff0000u) >>  8)
00106         | ((x & 0x0000ff00u) <<  8)
00107         | ((x & 0x000000ffu) << 24);
00108 
00109 #endif
00110     }
00111 
00112     inline ulonglong byteSwap(ulonglong x)
00113     {
00114 #if defined(HAVE_GCC_BYTESWAP_64)
00115 
00116       return __builtin_bswap64(x);
00117 
00118 #elif defined(HAVE_MSC_BYTESWAP)
00119 
00120       return _byteswap_uint64(x);
00121 
00122 #elif defined(HAVE_GLIBC_BYTESWAP)
00123 
00124       return __bswap_64(x);
00125 
00126 #elif defined(HAVE_MAC_BYTESWAP)
00127 
00128       return OSSwapInt64(x);
00129 
00130 #elif defined(HAVE_OPENBSD_BYTESWAP)
00131 
00132       return swap64(x);
00133 
00134 #else
00135 
00136       return ((x & 0xff00000000000000ull) >> 56)
00137         | ((x & 0x00ff000000000000ull) >> 40)
00138         | ((x & 0x0000ff0000000000ull) >> 24)
00139         | ((x & 0x000000ff00000000ull) >> 8)
00140         | ((x & 0x00000000ff000000ull) << 8)
00141         | ((x & 0x0000000000ff0000ull) << 24)
00142         | ((x & 0x000000000000ff00ull) << 40)
00143         | ((x & 0x00000000000000ffull) << 56);
00144 
00145 #endif
00146     }
00147 
00148     enum ByteOrder
00149     {
00150       LittleEndian,
00151       BigEndian
00152     };
00153 
00154 #ifdef SYSTEM_BYTEORDER
00155 
00156 # if SYSTEM_BYTEORDER == 1
00157 
00158     const ByteOrder SystemByteOrder = LittleEndian; 
00159 
00160 # else
00161 
00162     const ByteOrder SystemByteOrder = BigEndian; 
00163 
00164 # endif
00165 
00166 #else
00167 
00168     inline ByteOrder systemByteOrder()
00169     {
00170       union {
00171         int  i;
00172         char c;
00173       } u;
00174 
00175       u.i = 1;
00176       if(u.c == 1)
00177         return LittleEndian;
00178       else
00179         return BigEndian;
00180     }
00181     
00182     const ByteOrder SystemByteOrder = systemByteOrder(); 
00183 
00184 #endif
00185   }
00186 }
00187 
00188 #endif
00189 
00190 #endif