Statistics
| Branch: | Revision:

ffmpeg / libavutil / bswap.h @ 27af15dc

History | View | Annotate | Download (3.34 KB)

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 swap.
24
 */
25

    
26
#ifndef FFMPEG_BSWAP_H
27
#define FFMPEG_BSWAP_H
28

    
29
#include <stdint.h>
30
#include "common.h"
31

    
32
#ifdef HAVE_BYTESWAP_H
33
#include <byteswap.h>
34
#else
35

    
36
#ifdef ARCH_X86_64
37
#  define LEGACY_REGS "=Q"
38
#else
39
#  define LEGACY_REGS "=q"
40
#endif
41

    
42
static av_always_inline uint16_t bswap_16(uint16_t x)
43
{
44
#if defined(ARCH_X86)
45
    __asm("rorw $8, %0"   :
46
          LEGACY_REGS (x) :
47
          "0" (x));
48
#elif defined(ARCH_SH4)
49
    __asm__("swap.b %0,%0":"=r"(x):"0"(x));
50
#else
51
    x= (x>>8) | (x<<8);
52
#endif
53
    return x;
54
}
55

    
56
static av_always_inline uint32_t bswap_32(uint32_t x)
57
{
58
#if defined(ARCH_X86)
59
#if __CPU__ != 386
60
    __asm("bswap   %0":
61
          "=r" (x)    :
62
#else
63
    __asm("xchgb   %b0,%h0\n"
64
          "rorl    $16,%0 \n"
65
          "xchgb   %b0,%h0":
66
          LEGACY_REGS (x)  :
67
#endif
68
          "0" (x));
69
#elif defined(ARCH_SH4)
70
    __asm__("swap.b %0,%0\n"
71
            "swap.w %0,%0\n"
72
            "swap.b %0,%0\n"
73
            :"=r"(x):"0"(x));
74
#elif defined(ARCH_ARM)
75
    uint32_t t;
76
    __asm__ ("eor %1, %0, %0, ror #16 \n\t"
77
             "bic %1, %1, #0xFF0000   \n\t"
78
             "mov %0, %0, ror #8      \n\t"
79
             "eor %0, %0, %1, lsr #8  \n\t"
80
             : "+r"(x), "+r"(t));
81
#elif defined(ARCH_BFIN)
82
    unsigned tmp;
83
    asm("%1 = %0 >> 8 (V);      \n\t"
84
        "%0 = %0 << 8 (V);      \n\t"
85
        "%0 = %0 | %1;          \n\t"
86
        "%0 = PACK(%0.L, %0.H); \n\t"
87
        : "+d"(x), "=&d"(tmp));
88
#else
89
    x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
90
    x= (x>>16) | (x<<16);
91
#endif
92
    return x;
93
}
94

    
95
static inline uint64_t bswap_64(uint64_t x)
96
{
97
#if 0
98
    x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
99
    x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
100
    return (x>>32) | (x<<32);
101
#elif defined(ARCH_X86_64)
102
  __asm("bswap  %0":
103
        "=r" (x)   :
104
        "0" (x));
105
  return x;
106
#else
107
    union {
108
        uint64_t ll;
109
        uint32_t l[2];
110
    } w, r;
111
    w.ll = x;
112
    r.l[0] = bswap_32 (w.l[1]);
113
    r.l[1] = bswap_32 (w.l[0]);
114
    return r.ll;
115
#endif
116
}
117

    
118
#endif  /* !HAVE_BYTESWAP_H */
119

    
120
// be2me ... BigEndian to MachineEndian
121
// le2me ... LittleEndian to MachineEndian
122

    
123
#ifdef WORDS_BIGENDIAN
124
#define be2me_16(x) (x)
125
#define be2me_32(x) (x)
126
#define be2me_64(x) (x)
127
#define le2me_16(x) bswap_16(x)
128
#define le2me_32(x) bswap_32(x)
129
#define le2me_64(x) bswap_64(x)
130
#else
131
#define be2me_16(x) bswap_16(x)
132
#define be2me_32(x) bswap_32(x)
133
#define be2me_64(x) bswap_64(x)
134
#define le2me_16(x) (x)
135
#define le2me_32(x) (x)
136
#define le2me_64(x) (x)
137
#endif
138

    
139
#endif /* FFMPEG_BSWAP_H */