Statistics
| Branch: | Revision:

ffmpeg / libavutil / bswap.h @ 699b3f99

History | View | Annotate | Download (3.29 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 99545457 Måns Rullgård
#include <stdint.h>
30
#include "common.h"
31
32 a9b3f630 Nick Kurshev
#ifdef HAVE_BYTESWAP_H
33
#include <byteswap.h>
34
#else
35
36 053dea12 Aurelien Jacobs
#ifdef ARCH_X86_64
37
#  define LEGACY_REGS "=Q"
38
#else
39
#  define LEGACY_REGS "=q"
40
#endif
41
42 849f1035 Måns Rullgård
static av_always_inline uint16_t bswap_16(uint16_t x)
43 a9b3f630 Nick Kurshev
{
44 bf5d7db5 Ramiro Polla
#if defined(ARCH_X86)
45 bb270c08 Diego Biurrun
  __asm("rorw $8, %0"   :
46
        LEGACY_REGS (x) :
47 a9b3f630 Nick Kurshev
        "0" (x));
48 bf5d7db5 Ramiro Polla
#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 a9b3f630 Nick Kurshev
    return x;
54
}
55
56 849f1035 Måns Rullgård
static av_always_inline uint32_t bswap_32(uint32_t x)
57 a9b3f630 Nick Kurshev
{
58 bf5d7db5 Ramiro Polla
#if defined(ARCH_X86)
59 afe6a4a0 Ivan Kalvachev
#if __CPU__ != 386
60 bb270c08 Diego Biurrun
 __asm("bswap   %0":
61 a9b3f630 Nick Kurshev
      "=r" (x)     :
62
#else
63 bb270c08 Diego Biurrun
 __asm("xchgb   %b0,%h0\n"
64
      "         rorl    $16,%0\n"
65
      "         xchgb   %b0,%h0":
66
      LEGACY_REGS (x)                :
67 a9b3f630 Nick Kurshev
#endif
68
      "0" (x));
69 0c6bd2ea BERO
#elif defined(ARCH_SH4)
70 bb270c08 Diego Biurrun
        __asm__(
71
        "swap.b %0,%0\n"
72
        "swap.w %0,%0\n"
73
        "swap.b %0,%0\n"
74
        :"=r"(x):"0"(x));
75 bf5d7db5 Ramiro Polla
#elif defined(ARCH_ARM)
76 6ad1fa5a Bernhard Rosenkränzer
    uint32_t t;
77
    __asm__ (
78
      "eor %1, %0, %0, ror #16 \n\t"
79
      "bic %1, %1, #0xFF0000   \n\t"
80
      "mov %0, %0, ror #8      \n\t"
81
      "eor %0, %0, %1, lsr #8  \n\t"
82
      : "+r"(x), "+r"(t));
83 20d45dc2 Marc Hoffman
#elif defined(ARCH_BFIN)
84
    unsigned tmp;
85
    asm("%1 = %0 >> 8 (V);\n\t"
86
        "%0 = %0 << 8 (V);\n\t"
87
        "%0 = %0 | %1;\n\t"
88
        "%0 = PACK(%0.L, %0.H);\n\t"
89
        : "+d"(x), "=&d"(tmp));
90 6ad1fa5a Bernhard Rosenkränzer
#else
91 33f72572 Michael Niedermayer
    x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
92 853d15c2 Ramiro Polla
    x= (x>>16) | (x<<16);
93 bf5d7db5 Ramiro Polla
#endif
94 853d15c2 Ramiro Polla
    return x;
95 24dea064 Michael Niedermayer
}
96 a9b3f630 Nick Kurshev
97 24dea064 Michael Niedermayer
static inline uint64_t bswap_64(uint64_t x)
98 74fc9989 Fabrice Bellard
{
99 33f72572 Michael Niedermayer
#if 0
100
    x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
101
    x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
102
    return (x>>32) | (x<<32);
103 bf5d7db5 Ramiro Polla
#elif defined(ARCH_X86_64)
104
  __asm("bswap  %0":
105
        "=r" (x)   :
106
        "0" (x));
107
  return x;
108 33f72572 Michael Niedermayer
#else
109 115329f1 Diego Biurrun
    union {
110 74fc9989 Fabrice Bellard
        uint64_t ll;
111 115329f1 Diego Biurrun
        uint32_t l[2];
112 74fc9989 Fabrice Bellard
    } w, r;
113
    w.ll = x;
114
    r.l[0] = bswap_32 (w.l[1]);
115
    r.l[1] = bswap_32 (w.l[0]);
116
    return r.ll;
117 33f72572 Michael Niedermayer
#endif
118 74fc9989 Fabrice Bellard
}
119 a9b3f630 Nick Kurshev
120 bb270c08 Diego Biurrun
#endif  /* !HAVE_BYTESWAP_H */
121 a9b3f630 Nick Kurshev
122
// be2me ... BigEndian to MachineEndian
123
// le2me ... LittleEndian to MachineEndian
124
125
#ifdef WORDS_BIGENDIAN
126
#define be2me_16(x) (x)
127
#define be2me_32(x) (x)
128
#define be2me_64(x) (x)
129
#define le2me_16(x) bswap_16(x)
130
#define le2me_32(x) bswap_32(x)
131
#define le2me_64(x) bswap_64(x)
132
#else
133
#define be2me_16(x) bswap_16(x)
134
#define be2me_32(x) bswap_32(x)
135
#define be2me_64(x) bswap_64(x)
136
#define le2me_16(x) (x)
137
#define le2me_32(x) (x)
138
#define le2me_64(x) (x)
139
#endif
140
141 b17c92d1 Fabrice Bellard
#endif /* __BSWAP_H__ */