Statistics
| Branch: | Revision:

ffmpeg / libavcodec / bswap.h @ 650cec0c

History | View | Annotate | Download (2.76 KB)

1
/**
2
 * @file bswap.h
3
 * byte swap.
4
 */
5

    
6
#ifndef __BSWAP_H__
7
#define __BSWAP_H__
8

    
9
#ifdef HAVE_BYTESWAP_H
10
#include <byteswap.h>
11
#else
12

    
13
#ifdef ARCH_X86_64
14
#  define LEGACY_REGS "=Q"
15
#else
16
#  define LEGACY_REGS "=q"
17
#endif
18

    
19
#if defined(ARCH_X86) || defined(ARCH_X86_64)
20
static inline uint16_t ByteSwap16(uint16_t x)
21
{
22
  __asm("xchgb %b0,%h0"        :
23
        LEGACY_REGS (x)        :
24
        "0" (x));
25
    return x;
26
}
27
#define bswap_16(x) ByteSwap16(x)
28

    
29
static inline uint32_t ByteSwap32(uint32_t x)
30
{
31
#if __CPU__ > 386
32
 __asm("bswap        %0":
33
      "=r" (x)     :
34
#else
35
 __asm("xchgb        %b0,%h0\n"
36
      "        rorl        $16,%0\n"
37
      "        xchgb        %b0,%h0":
38
      LEGACY_REGS (x)                :
39
#endif
40
      "0" (x));
41
  return x;
42
}
43
#define bswap_32(x) ByteSwap32(x)
44

    
45
static inline uint64_t ByteSwap64(uint64_t x)
46
{
47
#ifdef ARCH_X86_64
48
  __asm("bswap        %0":
49
        "=r" (x)     :
50
        "0" (x));
51
  return x;
52
#else
53
  register union { __extension__ uint64_t __ll;
54
          uint32_t __l[2]; } __x;
55
  asm("xchgl        %0,%1":
56
      "=r"(__x.__l[0]),"=r"(__x.__l[1]):
57
      "0"(bswap_32((uint32_t)x)),"1"(bswap_32((uint32_t)(x>>32))));
58
  return __x.__ll;
59
#endif
60
}
61
#define bswap_64(x) ByteSwap64(x)
62

    
63
#elif defined(ARCH_SH4)
64

    
65
static inline uint16_t ByteSwap16(uint16_t x) {
66
        __asm__("swap.b %0,%0":"=r"(x):"0"(x));
67
        return x;
68
}
69

    
70
static inline uint32_t ByteSwap32(uint32_t x) {
71
        __asm__(
72
        "swap.b %0,%0\n"
73
        "swap.w %0,%0\n"
74
        "swap.b %0,%0\n"
75
        :"=r"(x):"0"(x));
76
        return x;
77
}
78

    
79
#define bswap_16(x) ByteSwap16(x)
80
#define bswap_32(x) ByteSwap32(x)
81

    
82
static inline uint64_t ByteSwap64(uint64_t x)
83
{
84
    union { 
85
        uint64_t ll;
86
        struct {
87
           uint32_t l,h;
88
        } l;
89
    } r;
90
    r.l.l = bswap_32 (x);
91
    r.l.h = bswap_32 (x>>32);
92
    return r.ll;
93
}
94
#define bswap_64(x) ByteSwap64(x)
95

    
96
#else
97

    
98
#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
99
                        
100

    
101
// code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
102
#define bswap_32(x) \
103
     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
104
      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
105

    
106
static inline uint64_t ByteSwap64(uint64_t x)
107
{
108
    union { 
109
        uint64_t ll;
110
        uint32_t l[2]; 
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;
116
}
117
#define bswap_64(x) ByteSwap64(x)
118

    
119
#endif        /* !ARCH_X86 */
120

    
121
#endif        /* !HAVE_BYTESWAP_H */
122

    
123
// be2me ... BigEndian to MachineEndian
124
// le2me ... LittleEndian to MachineEndian
125

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

    
142
#endif /* __BSWAP_H__ */