comparison bswap.h @ 10481:1911eb291dfb

sync with libavcodec (sh4 optim)
author alex
date Sun, 27 Jul 2003 21:24:33 +0000
parents adaa83002b22
children 821f464b4d90
comparison
equal deleted inserted replaced
10480:66aa563cd3e0 10481:1911eb291dfb
1 #ifndef __BSWAP_H__ 1 #ifndef __BSWAP_H__
2 #define __BSWAP_H__ 2 #define __BSWAP_H__
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7 3
8 #ifdef HAVE_BYTESWAP_H 4 #ifdef HAVE_BYTESWAP_H
9 #include <byteswap.h> 5 #include <byteswap.h>
10 #else 6 #else
11 7
12 #include <inttypes.h> 8 #include <inttypes.h>
13 9
14 #ifdef ARCH_X86 10 #ifdef ARCH_X86
15 inline static unsigned short ByteSwap16(unsigned short x) 11 static inline unsigned short ByteSwap16(unsigned short x)
16 { 12 {
17 __asm("xchgb %b0,%h0" : 13 __asm("xchgb %b0,%h0" :
18 "=q" (x) : 14 "=q" (x) :
19 "0" (x)); 15 "0" (x));
20 return x; 16 return x;
21 } 17 }
22 #define bswap_16(x) ByteSwap16(x) 18 #define bswap_16(x) ByteSwap16(x)
23 19
24 inline static unsigned int ByteSwap32(unsigned int x) 20 static inline unsigned int ByteSwap32(unsigned int x)
25 { 21 {
26 #if __CPU__ > 386 22 #if __CPU__ > 386
27 __asm("bswap %0": 23 __asm("bswap %0":
28 "=r" (x) : 24 "=r" (x) :
29 #else 25 #else
35 "0" (x)); 31 "0" (x));
36 return x; 32 return x;
37 } 33 }
38 #define bswap_32(x) ByteSwap32(x) 34 #define bswap_32(x) ByteSwap32(x)
39 35
40 inline static unsigned long long int ByteSwap64(unsigned long long int x) 36 static inline unsigned long long int ByteSwap64(unsigned long long int x)
41 { 37 {
42 register union { __extension__ unsigned long long int __ll; 38 register union { __extension__ uint64_t __ll;
43 unsigned int __l[2]; } __x; 39 uint32_t __l[2]; } __x;
44 asm("xchgl %0,%1": 40 asm("xchgl %0,%1":
45 "=r"(__x.__l[0]),"=r"(__x.__l[1]): 41 "=r"(__x.__l[0]),"=r"(__x.__l[1]):
46 "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32)))); 42 "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
47 return __x.__ll; 43 return __x.__ll;
44 }
45 #define bswap_64(x) ByteSwap64(x)
46
47 #elif defined(ARCH_SH4)
48
49 static inline uint16_t ByteSwap16(uint16_t x) {
50 __asm__("swap.b %0,%0":"=r"(x):"0"(x));
51 return x;
52 }
53
54 static inline uint32_t ByteSwap32(uint32_t x) {
55 __asm__(
56 "swap.b %0,%0\n"
57 "swap.w %0,%0\n"
58 "swap.b %0,%0\n"
59 :"=r"(x):"0"(x));
60 return x;
61 }
62
63 #define bswap_16(x) ByteSwap16(x)
64 #define bswap_32(x) ByteSwap32(x)
65
66 static inline uint64_t ByteSwap64(uint64_t x)
67 {
68 union {
69 uint64_t ll;
70 struct {
71 uint32_t l,h;
72 } l;
73 } r;
74 r.l.l = bswap_32 (x);
75 r.l.h = bswap_32 (x>>32);
76 return r.ll;
48 } 77 }
49 #define bswap_64(x) ByteSwap64(x) 78 #define bswap_64(x) ByteSwap64(x)
50 79
51 #else 80 #else
52 81
56 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc. 85 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
57 #define bswap_32(x) \ 86 #define bswap_32(x) \
58 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ 87 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
59 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) 88 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
60 89
61 #define bswap_64(x) \ 90 static inline uint64_t ByteSwap64(uint64_t x)
62 (__extension__ \ 91 {
63 ({ union { __extension__ unsigned long long int __ll; \ 92 union {
64 unsigned int __l[2]; } __w, __r; \ 93 uint64_t ll;
65 __w.__ll = (x); \ 94 uint32_t l[2];
66 __r.__l[0] = bswap_32 (__w.__l[1]); \ 95 } w, r;
67 __r.__l[1] = bswap_32 (__w.__l[0]); \ 96 w.ll = x;
68 __r.__ll; })) 97 r.l[0] = bswap_32 (w.l[1]);
98 r.l[1] = bswap_32 (w.l[0]);
99 return r.ll;
100 }
101 #define bswap_64(x) ByteSwap64(x)
102
69 #endif /* !ARCH_X86 */ 103 #endif /* !ARCH_X86 */
70 104
71 #endif /* !HAVE_BYTESWAP_H */ 105 #endif /* !HAVE_BYTESWAP_H */
72 106
73 // be2me ... BigEndian to MachineEndian 107 // be2me ... BigEndian to MachineEndian
87 #define le2me_16(x) (x) 121 #define le2me_16(x) (x)
88 #define le2me_32(x) (x) 122 #define le2me_32(x) (x)
89 #define le2me_64(x) (x) 123 #define le2me_64(x) (x)
90 #endif 124 #endif
91 125
92 #endif 126 #endif /* __BSWAP_H__ */