]> git.sesse.net Git - ffmpeg/blob - libavutil/bswap.h
Cosmetics: do not align with now removed next instruction
[ffmpeg] / 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 static av_always_inline av_const uint16_t bswap_16(uint16_t x)
38 {
39 #if defined(ARCH_X86)
40     asm("rorw $8, %0" : "+r"(x));
41 #elif defined(ARCH_SH4)
42     asm("swap.b %0,%0" : "=r"(x) : "0"(x));
43 #elif defined(HAVE_ARMV6)
44     asm("rev16 %0, %0" : "+r"(x));
45 #else
46     x= (x>>8) | (x<<8);
47 #endif
48     return x;
49 }
50
51 static av_always_inline av_const uint32_t bswap_32(uint32_t x)
52 {
53 #if defined(ARCH_X86)
54 #ifdef HAVE_BSWAP
55     asm("bswap   %0" : "+r" (x));
56 #else
57     asm("rorw    $8,  %w0 \n\t"
58         "rorl    $16, %0  \n\t"
59         "rorw    $8,  %w0"
60         : "+r"(x));
61 #endif
62 #elif defined(ARCH_SH4)
63     asm("swap.b %0,%0\n"
64         "swap.w %0,%0\n"
65         "swap.b %0,%0\n"
66         : "=r"(x) : "0"(x));
67 #elif defined(HAVE_ARMV6)
68     asm("rev %0, %0" : "+r"(x));
69 #elif defined(ARCH_ARMV4L)
70     uint32_t t;
71     asm ("eor %1, %0, %0, ror #16 \n\t"
72          "bic %1, %1, #0xFF0000   \n\t"
73          "mov %0, %0, ror #8      \n\t"
74          "eor %0, %0, %1, lsr #8  \n\t"
75          : "+r"(x), "+r"(t));
76 #elif defined(ARCH_BFIN)
77     unsigned tmp;
78     asm("%1 = %0 >> 8 (V);      \n\t"
79         "%0 = %0 << 8 (V);      \n\t"
80         "%0 = %0 | %1;          \n\t"
81         "%0 = PACK(%0.L, %0.H); \n\t"
82         : "+d"(x), "=&d"(tmp));
83 #else
84     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
85     x= (x>>16) | (x<<16);
86 #endif
87     return x;
88 }
89
90 static inline uint64_t av_const bswap_64(uint64_t x)
91 {
92 #if 0
93     x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
94     x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
95     return (x>>32) | (x<<32);
96 #elif defined(ARCH_X86_64)
97   asm("bswap  %0": "=r" (x) : "0" (x));
98   return x;
99 #else
100     union {
101         uint64_t ll;
102         uint32_t l[2];
103     } w, r;
104     w.ll = x;
105     r.l[0] = bswap_32 (w.l[1]);
106     r.l[1] = bswap_32 (w.l[0]);
107     return r.ll;
108 #endif
109 }
110
111 #endif  /* !HAVE_BYTESWAP_H */
112
113 // be2me ... BigEndian to MachineEndian
114 // le2me ... LittleEndian to MachineEndian
115
116 #ifdef WORDS_BIGENDIAN
117 #define be2me_16(x) (x)
118 #define be2me_32(x) (x)
119 #define be2me_64(x) (x)
120 #define le2me_16(x) bswap_16(x)
121 #define le2me_32(x) bswap_32(x)
122 #define le2me_64(x) bswap_64(x)
123 #else
124 #define be2me_16(x) bswap_16(x)
125 #define be2me_32(x) bswap_32(x)
126 #define be2me_64(x) bswap_64(x)
127 #define le2me_16(x) (x)
128 #define le2me_32(x) (x)
129 #define le2me_64(x) (x)
130 #endif
131
132 #endif /* FFMPEG_BSWAP_H */