Statistics
| Branch: | Revision:

ffmpeg / libavutil / bswap.h @ 94d85eaf

History | View | Annotate | Download (2.98 KB)

1 983e3246 Michael Niedermayer
/**
2
 * @file bswap.h
3
 * byte swap.
4
 */
5
6 a9b3f630 Nick Kurshev
#ifndef __BSWAP_H__
7
#define __BSWAP_H__
8
9
#ifdef HAVE_BYTESWAP_H
10
#include <byteswap.h>
11
#else
12
13 053dea12 Aurelien Jacobs
#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 24dea064 Michael Niedermayer
static always_inline uint16_t bswap_16(uint16_t x)
21 a9b3f630 Nick Kurshev
{
22 bb270c08 Diego Biurrun
  __asm("rorw $8, %0"   :
23
        LEGACY_REGS (x) :
24 a9b3f630 Nick Kurshev
        "0" (x));
25
    return x;
26
}
27
28 24dea064 Michael Niedermayer
static always_inline uint32_t bswap_32(uint32_t x)
29 a9b3f630 Nick Kurshev
{
30 afe6a4a0 Ivan Kalvachev
#if __CPU__ != 386
31 bb270c08 Diego Biurrun
 __asm("bswap   %0":
32 a9b3f630 Nick Kurshev
      "=r" (x)     :
33
#else
34 bb270c08 Diego Biurrun
 __asm("xchgb   %b0,%h0\n"
35
      "         rorl    $16,%0\n"
36
      "         xchgb   %b0,%h0":
37
      LEGACY_REGS (x)                :
38 a9b3f630 Nick Kurshev
#endif
39
      "0" (x));
40
  return x;
41
}
42
43 24dea064 Michael Niedermayer
static inline uint64_t bswap_64(uint64_t x)
44 a9b3f630 Nick Kurshev
{
45 053dea12 Aurelien Jacobs
#ifdef ARCH_X86_64
46 bb270c08 Diego Biurrun
  __asm("bswap  %0":
47
        "=r" (x)   :
48 053dea12 Aurelien Jacobs
        "0" (x));
49
  return x;
50
#else
51 115329f1 Diego Biurrun
    union {
52 33f72572 Michael Niedermayer
        uint64_t ll;
53
        struct {
54
           uint32_t l,h;
55
        } l;
56
    } r;
57
    r.l.l = bswap_32 (x);
58
    r.l.h = bswap_32 (x>>32);
59
    return r.ll;
60 053dea12 Aurelien Jacobs
#endif
61 a9b3f630 Nick Kurshev
}
62
63 0c6bd2ea BERO
#elif defined(ARCH_SH4)
64
65 24dea064 Michael Niedermayer
static always_inline uint16_t bswap_16(uint16_t x) {
66 bb270c08 Diego Biurrun
        __asm__("swap.b %0,%0":"=r"(x):"0"(x));
67
        return x;
68 0c6bd2ea BERO
}
69
70 24dea064 Michael Niedermayer
static always_inline uint32_t bswap_32(uint32_t x) {
71 bb270c08 Diego Biurrun
        __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 0c6bd2ea BERO
}
78
79 24dea064 Michael Niedermayer
static inline uint64_t bswap_64(uint64_t x)
80 0c6bd2ea BERO
{
81 115329f1 Diego Biurrun
    union {
82 0c6bd2ea BERO
        uint64_t ll;
83
        struct {
84
           uint32_t l,h;
85
        } l;
86
    } r;
87
    r.l.l = bswap_32 (x);
88
    r.l.h = bswap_32 (x>>32);
89
    return r.ll;
90
}
91 a9b3f630 Nick Kurshev
#else
92
93 24dea064 Michael Niedermayer
static always_inline uint16_t bswap_16(uint16_t x){
94
    return (x>>8) | (x<<8);
95
}
96 a9b3f630 Nick Kurshev
97 6ad1fa5a Bernhard Rosenkränzer
#ifdef ARCH_ARM
98
static always_inline uint32_t bswap_32(uint32_t x){
99
    uint32_t t;
100
    __asm__ (
101
      "eor %1, %0, %0, ror #16 \n\t"
102
      "bic %1, %1, #0xFF0000   \n\t"
103
      "mov %0, %0, ror #8      \n\t"
104
      "eor %0, %0, %1, lsr #8  \n\t"
105
      : "+r"(x), "+r"(t));
106
    return x;
107
}
108
#else
109 24dea064 Michael Niedermayer
static always_inline uint32_t bswap_32(uint32_t x){
110 33f72572 Michael Niedermayer
    x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
111
    return (x>>16) | (x<<16);
112 24dea064 Michael Niedermayer
}
113 6ad1fa5a Bernhard Rosenkränzer
#endif
114 a9b3f630 Nick Kurshev
115 24dea064 Michael Niedermayer
static inline uint64_t bswap_64(uint64_t x)
116 74fc9989 Fabrice Bellard
{
117 33f72572 Michael Niedermayer
#if 0
118
    x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
119
    x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
120
    return (x>>32) | (x<<32);
121
#else
122 115329f1 Diego Biurrun
    union {
123 74fc9989 Fabrice Bellard
        uint64_t ll;
124 115329f1 Diego Biurrun
        uint32_t l[2];
125 74fc9989 Fabrice Bellard
    } w, r;
126
    w.ll = x;
127
    r.l[0] = bswap_32 (w.l[1]);
128
    r.l[1] = bswap_32 (w.l[0]);
129
    return r.ll;
130 33f72572 Michael Niedermayer
#endif
131 74fc9989 Fabrice Bellard
}
132 bb270c08 Diego Biurrun
#endif  /* !ARCH_X86 */
133 a9b3f630 Nick Kurshev
134 bb270c08 Diego Biurrun
#endif  /* !HAVE_BYTESWAP_H */
135 a9b3f630 Nick Kurshev
136
// be2me ... BigEndian to MachineEndian
137
// le2me ... LittleEndian to MachineEndian
138
139
#ifdef WORDS_BIGENDIAN
140
#define be2me_16(x) (x)
141
#define be2me_32(x) (x)
142
#define be2me_64(x) (x)
143
#define le2me_16(x) bswap_16(x)
144
#define le2me_32(x) bswap_32(x)
145
#define le2me_64(x) bswap_64(x)
146
#else
147
#define be2me_16(x) bswap_16(x)
148
#define be2me_32(x) bswap_32(x)
149
#define be2me_64(x) bswap_64(x)
150
#define le2me_16(x) (x)
151
#define le2me_32(x) (x)
152
#define le2me_64(x) (x)
153
#endif
154
155 b17c92d1 Fabrice Bellard
#endif /* __BSWAP_H__ */