]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavutil/bswap.h
Add official LGPL license headers to the files that were missing them.
[frescor/ffmpeg.git] / libavutil / bswap.h
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file bswap.h
21  * byte swap.
22  */
23
24 #ifndef __BSWAP_H__
25 #define __BSWAP_H__
26
27 #ifdef HAVE_BYTESWAP_H
28 #include <byteswap.h>
29 #else
30
31 #ifdef ARCH_X86_64
32 #  define LEGACY_REGS "=Q"
33 #else
34 #  define LEGACY_REGS "=q"
35 #endif
36
37 #if defined(ARCH_X86) || defined(ARCH_X86_64)
38 static always_inline uint16_t bswap_16(uint16_t x)
39 {
40   __asm("rorw $8, %0"   :
41         LEGACY_REGS (x) :
42         "0" (x));
43     return x;
44 }
45
46 static always_inline uint32_t bswap_32(uint32_t x)
47 {
48 #if __CPU__ != 386
49  __asm("bswap   %0":
50       "=r" (x)     :
51 #else
52  __asm("xchgb   %b0,%h0\n"
53       "         rorl    $16,%0\n"
54       "         xchgb   %b0,%h0":
55       LEGACY_REGS (x)                :
56 #endif
57       "0" (x));
58   return x;
59 }
60
61 static inline uint64_t bswap_64(uint64_t x)
62 {
63 #ifdef ARCH_X86_64
64   __asm("bswap  %0":
65         "=r" (x)   :
66         "0" (x));
67   return x;
68 #else
69     union {
70         uint64_t ll;
71         struct {
72            uint32_t l,h;
73         } l;
74     } r;
75     r.l.l = bswap_32 (x);
76     r.l.h = bswap_32 (x>>32);
77     return r.ll;
78 #endif
79 }
80
81 #elif defined(ARCH_SH4)
82
83 static always_inline uint16_t bswap_16(uint16_t x) {
84         __asm__("swap.b %0,%0":"=r"(x):"0"(x));
85         return x;
86 }
87
88 static always_inline uint32_t bswap_32(uint32_t x) {
89         __asm__(
90         "swap.b %0,%0\n"
91         "swap.w %0,%0\n"
92         "swap.b %0,%0\n"
93         :"=r"(x):"0"(x));
94         return x;
95 }
96
97 static inline uint64_t bswap_64(uint64_t x)
98 {
99     union {
100         uint64_t ll;
101         struct {
102            uint32_t l,h;
103         } l;
104     } r;
105     r.l.l = bswap_32 (x);
106     r.l.h = bswap_32 (x>>32);
107     return r.ll;
108 }
109 #else
110
111 static always_inline uint16_t bswap_16(uint16_t x){
112     return (x>>8) | (x<<8);
113 }
114
115 #ifdef ARCH_ARM
116 static always_inline uint32_t bswap_32(uint32_t x){
117     uint32_t t;
118     __asm__ (
119       "eor %1, %0, %0, ror #16 \n\t"
120       "bic %1, %1, #0xFF0000   \n\t"
121       "mov %0, %0, ror #8      \n\t"
122       "eor %0, %0, %1, lsr #8  \n\t"
123       : "+r"(x), "+r"(t));
124     return x;
125 }
126 #else
127 static always_inline uint32_t bswap_32(uint32_t x){
128     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
129     return (x>>16) | (x<<16);
130 }
131 #endif
132
133 static inline uint64_t bswap_64(uint64_t x)
134 {
135 #if 0
136     x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
137     x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
138     return (x>>32) | (x<<32);
139 #else
140     union {
141         uint64_t ll;
142         uint32_t l[2];
143     } w, r;
144     w.ll = x;
145     r.l[0] = bswap_32 (w.l[1]);
146     r.l[1] = bswap_32 (w.l[0]);
147     return r.ll;
148 #endif
149 }
150 #endif  /* !ARCH_X86 */
151
152 #endif  /* !HAVE_BYTESWAP_H */
153
154 // be2me ... BigEndian to MachineEndian
155 // le2me ... LittleEndian to MachineEndian
156
157 #ifdef WORDS_BIGENDIAN
158 #define be2me_16(x) (x)
159 #define be2me_32(x) (x)
160 #define be2me_64(x) (x)
161 #define le2me_16(x) bswap_16(x)
162 #define le2me_32(x) bswap_32(x)
163 #define le2me_64(x) bswap_64(x)
164 #else
165 #define be2me_16(x) bswap_16(x)
166 #define be2me_32(x) bswap_32(x)
167 #define be2me_64(x) bswap_64(x)
168 #define le2me_16(x) (x)
169 #define le2me_32(x) (x)
170 #define le2me_64(x) (x)
171 #endif
172
173 #endif /* __BSWAP_H__ */