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