]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavutil/bswap.h
cosmetics: sanitise asm() statements in bswap.h
[frescor/ffmpeg.git] / libavutil / bswap.h
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but 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 FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file bswap.h
23  * byte swapping routines
24  */
25
26 #ifndef FFMPEG_BSWAP_H
27 #define FFMPEG_BSWAP_H
28
29 #include <stdint.h>
30 #include "config.h"
31 #include "common.h"
32
33 #ifdef HAVE_BYTESWAP_H
34 #include <byteswap.h>
35 #else
36
37 #ifdef ARCH_X86_64
38 #  define LEGACY_REGS "=Q"
39 #else
40 #  define LEGACY_REGS "=q"
41 #endif
42
43 static av_always_inline uint16_t bswap_16(uint16_t x)
44 {
45 #if defined(ARCH_X86)
46     asm("rorw $8, %0" : LEGACY_REGS (x) : "0" (x));
47 #elif defined(ARCH_SH4)
48     asm("swap.b %0,%0" : "=r"(x) : "0"(x));
49 #else
50     x= (x>>8) | (x<<8);
51 #endif
52     return x;
53 }
54
55 static av_always_inline uint32_t bswap_32(uint32_t x)
56 {
57 #if defined(ARCH_X86)
58 #ifdef HAVE_BSWAP
59     asm("bswap   %0":
60           "=r" (x)    :
61 #else
62     asm("xchgb   %b0,%h0\n"
63         "rorl    $16,%0 \n"
64         "xchgb   %b0,%h0":
65         LEGACY_REGS (x)  :
66 #endif
67           "0" (x));
68 #elif defined(ARCH_SH4)
69     asm("swap.b %0,%0\n"
70         "swap.w %0,%0\n"
71         "swap.b %0,%0\n"
72         : "=r"(x) : "0"(x));
73 #elif defined(ARCH_ARM)
74     uint32_t t;
75     asm ("eor %1, %0, %0, ror #16 \n\t"
76          "bic %1, %1, #0xFF0000   \n\t"
77          "mov %0, %0, ror #8      \n\t"
78          "eor %0, %0, %1, lsr #8  \n\t"
79          : "+r"(x), "+r"(t));
80 #elif defined(ARCH_BFIN)
81     unsigned tmp;
82     asm("%1 = %0 >> 8 (V);      \n\t"
83         "%0 = %0 << 8 (V);      \n\t"
84         "%0 = %0 | %1;          \n\t"
85         "%0 = PACK(%0.L, %0.H); \n\t"
86         : "+d"(x), "=&d"(tmp));
87 #else
88     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
89     x= (x>>16) | (x<<16);
90 #endif
91     return x;
92 }
93
94 static inline uint64_t bswap_64(uint64_t x)
95 {
96 #if 0
97     x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
98     x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
99     return (x>>32) | (x<<32);
100 #elif defined(ARCH_X86_64)
101   asm("bswap  %0": "=r" (x) : "0" (x));
102   return x;
103 #else
104     union {
105         uint64_t ll;
106         uint32_t l[2];
107     } w, r;
108     w.ll = x;
109     r.l[0] = bswap_32 (w.l[1]);
110     r.l[1] = bswap_32 (w.l[0]);
111     return r.ll;
112 #endif
113 }
114
115 #endif  /* !HAVE_BYTESWAP_H */
116
117 // be2me ... BigEndian to MachineEndian
118 // le2me ... LittleEndian to MachineEndian
119
120 #ifdef WORDS_BIGENDIAN
121 #define be2me_16(x) (x)
122 #define be2me_32(x) (x)
123 #define be2me_64(x) (x)
124 #define le2me_16(x) bswap_16(x)
125 #define le2me_32(x) bswap_32(x)
126 #define le2me_64(x) bswap_64(x)
127 #else
128 #define be2me_16(x) bswap_16(x)
129 #define be2me_32(x) bswap_32(x)
130 #define be2me_64(x) bswap_64(x)
131 #define le2me_16(x) (x)
132 #define le2me_32(x) (x)
133 #define le2me_64(x) (x)
134 #endif
135
136 #endif /* FFMPEG_BSWAP_H */