bswap.h

Go to the documentation of this file.
00001 /*
00002  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but 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 FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 /**
00022  * @file bswap.h
00023  * byte swap.
00024  */
00025 
00026 #ifndef __BSWAP_H__
00027 #define __BSWAP_H__
00028 
00029 #ifdef HAVE_BYTESWAP_H
00030 #include <byteswap.h>
00031 #else
00032 
00033 #undef ROCKBOX
00034 #ifdef ROCKBOX
00035 
00036 /* rockbox' optimised inline functions */
00037 #define bswap_16(x) swap16(x)
00038 #define bswap_32(x) swap32(x)
00039 
00040 static inline uint64_t ByteSwap64(uint64_t x)
00041 {
00042     union { 
00043         uint64_t ll;
00044         struct {
00045            uint32_t l,h;
00046         } l;
00047     } r;
00048     r.l.l = bswap_32 (x);
00049     r.l.h = bswap_32 (x>>32);
00050     return r.ll;
00051 }
00052 #define bswap_64(x) ByteSwap64(x)
00053 
00054 #elif defined(ARCH_X86)
00055 static inline unsigned short ByteSwap16(unsigned short x)
00056 {
00057   __asm("xchgb %b0,%h0" :
00058         "=q" (x)    :
00059         "0" (x));
00060     return x;
00061 }
00062 #define bswap_16(x) ByteSwap16(x)
00063 
00064 static inline unsigned int ByteSwap32(unsigned int x)
00065 {
00066 #if __CPU__ > 386
00067  __asm("bswap   %0":
00068       "=r" (x)     :
00069 #else
00070  __asm("xchgb   %b0,%h0\n"
00071       " rorl    $16,%0\n"
00072       " xchgb   %b0,%h0":
00073       "=q" (x)      :
00074 #endif
00075       "0" (x));
00076   return x;
00077 }
00078 #define bswap_32(x) ByteSwap32(x)
00079 
00080 static inline unsigned long long int ByteSwap64(unsigned long long int x)
00081 {
00082   register union { __extension__ uint64_t __ll;
00083           uint32_t __l[2]; } __x;
00084   asm("xchgl    %0,%1":
00085       "=r"(__x.__l[0]),"=r"(__x.__l[1]):
00086       "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
00087   return __x.__ll;
00088 }
00089 #define bswap_64(x) ByteSwap64(x)
00090 
00091 #elif defined(ARCH_SH4)
00092 
00093 static inline uint16_t ByteSwap16(uint16_t x) {
00094     __asm__("swap.b %0,%0":"=r"(x):"0"(x));
00095     return x;
00096 }
00097 
00098 static inline uint32_t ByteSwap32(uint32_t x) {
00099     __asm__(
00100     "swap.b %0,%0\n"
00101     "swap.w %0,%0\n"
00102     "swap.b %0,%0\n"
00103     :"=r"(x):"0"(x));
00104     return x;
00105 }
00106 
00107 #define bswap_16(x) ByteSwap16(x)
00108 #define bswap_32(x) ByteSwap32(x)
00109 
00110 static inline uint64_t ByteSwap64(uint64_t x)
00111 {
00112     union { 
00113         uint64_t ll;
00114         struct {
00115            uint32_t l,h;
00116         } l;
00117     } r;
00118     r.l.l = bswap_32 (x);
00119     r.l.h = bswap_32 (x>>32);
00120     return r.ll;
00121 }
00122 #define bswap_64(x) ByteSwap64(x)
00123 
00124 #else
00125 
00126 #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
00127 
00128 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
00129 #define bswap_32(x) \
00130      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
00131       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
00132 
00133 static inline uint64_t ByteSwap64(uint64_t x)
00134 {
00135     union { 
00136         uint64_t ll;
00137         uint32_t l[2]; 
00138     } w, r;
00139     w.ll = x;
00140     r.l[0] = bswap_32 (w.l[1]);
00141     r.l[1] = bswap_32 (w.l[0]);
00142     return r.ll;
00143 }
00144 #define bswap_64(x) ByteSwap64(x)
00145 
00146 #endif  /* !ARCH_X86 */
00147 
00148 #endif  /* !HAVE_BYTESWAP_H */
00149 
00150 // be2me ... BigEndian to MachineEndian
00151 // le2me ... LittleEndian to MachineEndian
00152 
00153 #ifdef WORDS_BIGENDIAN
00154 #define be2me_16(x) (x)
00155 #define be2me_32(x) (x)
00156 #define be2me_64(x) (x)
00157 #define le2me_16(x) bswap_16(x)
00158 #define le2me_32(x) bswap_32(x)
00159 #define le2me_64(x) bswap_64(x)
00160 #else
00161 #define be2me_16(x) bswap_16(x)
00162 #define be2me_32(x) bswap_32(x)
00163 #define be2me_64(x) bswap_64(x)
00164 #define le2me_16(x) (x)
00165 #define le2me_32(x) (x)
00166 #define le2me_64(x) (x)
00167 #endif
00168 
00169 #endif /* __BSWAP_H__ */

Generated on Tue May 25 08:04:54 2010 for VLC by  doxygen 1.5.6