Statistics
| Branch: | Revision:

ffmpeg / libavutil / bswap.h @ 0b006599

History | View | Annotate | Download (3.79 KB)

1 04d7f601 Diego Biurrun
/*
2
 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3
 *
4 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7 04d7f601 Diego Biurrun
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
10 04d7f601 Diego Biurrun
 *
11 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
12 04d7f601 Diego Biurrun
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
18 04d7f601 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21 983e3246 Michael Niedermayer
/**
22
 * @file bswap.h
23
 * byte swap.
24
 */
25
26 a9b3f630 Nick Kurshev
#ifndef __BSWAP_H__
27
#define __BSWAP_H__
28
29
#ifdef HAVE_BYTESWAP_H
30
#include <byteswap.h>
31
#else
32
33 053dea12 Aurelien Jacobs
#ifdef ARCH_X86_64
34
#  define LEGACY_REGS "=Q"
35
#else
36
#  define LEGACY_REGS "=q"
37
#endif
38
39 3cd52279 Diego Biurrun
#if defined(ARCH_X86)
40 849f1035 Måns Rullgård
static av_always_inline uint16_t bswap_16(uint16_t x)
41 a9b3f630 Nick Kurshev
{
42 bb270c08 Diego Biurrun
  __asm("rorw $8, %0"   :
43
        LEGACY_REGS (x) :
44 a9b3f630 Nick Kurshev
        "0" (x));
45
    return x;
46
}
47
48 849f1035 Måns Rullgård
static av_always_inline uint32_t bswap_32(uint32_t x)
49 a9b3f630 Nick Kurshev
{
50 afe6a4a0 Ivan Kalvachev
#if __CPU__ != 386
51 bb270c08 Diego Biurrun
 __asm("bswap   %0":
52 a9b3f630 Nick Kurshev
      "=r" (x)     :
53
#else
54 bb270c08 Diego Biurrun
 __asm("xchgb   %b0,%h0\n"
55
      "         rorl    $16,%0\n"
56
      "         xchgb   %b0,%h0":
57
      LEGACY_REGS (x)                :
58 a9b3f630 Nick Kurshev
#endif
59
      "0" (x));
60
  return x;
61
}
62
63 24dea064 Michael Niedermayer
static inline uint64_t bswap_64(uint64_t x)
64 a9b3f630 Nick Kurshev
{
65 053dea12 Aurelien Jacobs
#ifdef ARCH_X86_64
66 bb270c08 Diego Biurrun
  __asm("bswap  %0":
67
        "=r" (x)   :
68 053dea12 Aurelien Jacobs
        "0" (x));
69
  return x;
70
#else
71 115329f1 Diego Biurrun
    union {
72 33f72572 Michael Niedermayer
        uint64_t ll;
73
        struct {
74
           uint32_t l,h;
75
        } l;
76
    } r;
77
    r.l.l = bswap_32 (x);
78
    r.l.h = bswap_32 (x>>32);
79
    return r.ll;
80 053dea12 Aurelien Jacobs
#endif
81 a9b3f630 Nick Kurshev
}
82
83 0c6bd2ea BERO
#elif defined(ARCH_SH4)
84
85 849f1035 Måns Rullgård
static av_always_inline uint16_t bswap_16(uint16_t x) {
86 bb270c08 Diego Biurrun
        __asm__("swap.b %0,%0":"=r"(x):"0"(x));
87
        return x;
88 0c6bd2ea BERO
}
89
90 849f1035 Måns Rullgård
static av_always_inline uint32_t bswap_32(uint32_t x) {
91 bb270c08 Diego Biurrun
        __asm__(
92
        "swap.b %0,%0\n"
93
        "swap.w %0,%0\n"
94
        "swap.b %0,%0\n"
95
        :"=r"(x):"0"(x));
96
        return x;
97 0c6bd2ea BERO
}
98
99 24dea064 Michael Niedermayer
static inline uint64_t bswap_64(uint64_t x)
100 0c6bd2ea BERO
{
101 115329f1 Diego Biurrun
    union {
102 0c6bd2ea BERO
        uint64_t ll;
103
        struct {
104
           uint32_t l,h;
105
        } l;
106
    } r;
107
    r.l.l = bswap_32 (x);
108
    r.l.h = bswap_32 (x>>32);
109
    return r.ll;
110
}
111 a9b3f630 Nick Kurshev
#else
112
113 849f1035 Måns Rullgård
static av_always_inline uint16_t bswap_16(uint16_t x){
114 24dea064 Michael Niedermayer
    return (x>>8) | (x<<8);
115
}
116 a9b3f630 Nick Kurshev
117 6ad1fa5a Bernhard Rosenkränzer
#ifdef ARCH_ARM
118 849f1035 Måns Rullgård
static av_always_inline uint32_t bswap_32(uint32_t x){
119 6ad1fa5a Bernhard Rosenkränzer
    uint32_t t;
120
    __asm__ (
121
      "eor %1, %0, %0, ror #16 \n\t"
122
      "bic %1, %1, #0xFF0000   \n\t"
123
      "mov %0, %0, ror #8      \n\t"
124
      "eor %0, %0, %1, lsr #8  \n\t"
125
      : "+r"(x), "+r"(t));
126
    return x;
127
}
128
#else
129 849f1035 Måns Rullgård
static av_always_inline uint32_t bswap_32(uint32_t x){
130 33f72572 Michael Niedermayer
    x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
131
    return (x>>16) | (x<<16);
132 24dea064 Michael Niedermayer
}
133 6ad1fa5a Bernhard Rosenkränzer
#endif
134 a9b3f630 Nick Kurshev
135 24dea064 Michael Niedermayer
static inline uint64_t bswap_64(uint64_t x)
136 74fc9989 Fabrice Bellard
{
137 33f72572 Michael Niedermayer
#if 0
138
    x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
139
    x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
140
    return (x>>32) | (x<<32);
141
#else
142 115329f1 Diego Biurrun
    union {
143 74fc9989 Fabrice Bellard
        uint64_t ll;
144 115329f1 Diego Biurrun
        uint32_t l[2];
145 74fc9989 Fabrice Bellard
    } w, r;
146
    w.ll = x;
147
    r.l[0] = bswap_32 (w.l[1]);
148
    r.l[1] = bswap_32 (w.l[0]);
149
    return r.ll;
150 33f72572 Michael Niedermayer
#endif
151 74fc9989 Fabrice Bellard
}
152 3cd52279 Diego Biurrun
#endif  /* defined(ARCH_X86) */
153 a9b3f630 Nick Kurshev
154 bb270c08 Diego Biurrun
#endif  /* !HAVE_BYTESWAP_H */
155 a9b3f630 Nick Kurshev
156
// be2me ... BigEndian to MachineEndian
157
// le2me ... LittleEndian to MachineEndian
158
159
#ifdef WORDS_BIGENDIAN
160
#define be2me_16(x) (x)
161
#define be2me_32(x) (x)
162
#define be2me_64(x) (x)
163
#define le2me_16(x) bswap_16(x)
164
#define le2me_32(x) bswap_32(x)
165
#define le2me_64(x) bswap_64(x)
166
#else
167
#define be2me_16(x) bswap_16(x)
168
#define be2me_32(x) bswap_32(x)
169
#define be2me_64(x) bswap_64(x)
170
#define le2me_16(x) (x)
171
#define le2me_32(x) (x)
172
#define le2me_64(x) (x)
173
#endif
174
175 b17c92d1 Fabrice Bellard
#endif /* __BSWAP_H__ */