Mercurial > libavcodec.hg
comparison bswap.h @ 2659:4023235edd2e libavcodec
change macros to inline functions
remove redundant operations
remove silly 2 layer bswap_XX/ByteSwapXX system
author | michael |
---|---|
date | Tue, 10 May 2005 10:05:44 +0000 |
parents | 15cfba1b97b5 |
children | e26dc8cf7069 |
comparison
equal
deleted
inserted
replaced
2658:d1609cfeb1d0 | 2659:4023235edd2e |
---|---|
15 #else | 15 #else |
16 # define LEGACY_REGS "=q" | 16 # define LEGACY_REGS "=q" |
17 #endif | 17 #endif |
18 | 18 |
19 #if defined(ARCH_X86) || defined(ARCH_X86_64) | 19 #if defined(ARCH_X86) || defined(ARCH_X86_64) |
20 static inline uint16_t ByteSwap16(uint16_t x) | 20 static always_inline uint16_t bswap_16(uint16_t x) |
21 { | 21 { |
22 __asm("xchgb %b0,%h0" : | 22 __asm("xchgb %b0,%h0" : |
23 LEGACY_REGS (x) : | 23 LEGACY_REGS (x) : |
24 "0" (x)); | 24 "0" (x)); |
25 return x; | 25 return x; |
26 } | 26 } |
27 #define bswap_16(x) ByteSwap16(x) | |
28 | 27 |
29 static inline uint32_t ByteSwap32(uint32_t x) | 28 static always_inline uint32_t bswap_32(uint32_t x) |
30 { | 29 { |
31 #if __CPU__ > 386 | 30 #if __CPU__ > 386 |
32 __asm("bswap %0": | 31 __asm("bswap %0": |
33 "=r" (x) : | 32 "=r" (x) : |
34 #else | 33 #else |
38 LEGACY_REGS (x) : | 37 LEGACY_REGS (x) : |
39 #endif | 38 #endif |
40 "0" (x)); | 39 "0" (x)); |
41 return x; | 40 return x; |
42 } | 41 } |
43 #define bswap_32(x) ByteSwap32(x) | |
44 | 42 |
45 static inline uint64_t ByteSwap64(uint64_t x) | 43 static inline uint64_t bswap_64(uint64_t x) |
46 { | 44 { |
47 #ifdef ARCH_X86_64 | 45 #ifdef ARCH_X86_64 |
48 __asm("bswap %0": | 46 __asm("bswap %0": |
49 "=r" (x) : | 47 "=r" (x) : |
50 "0" (x)); | 48 "0" (x)); |
56 "=r"(__x.__l[0]),"=r"(__x.__l[1]): | 54 "=r"(__x.__l[0]),"=r"(__x.__l[1]): |
57 "0"(bswap_32((uint32_t)x)),"1"(bswap_32((uint32_t)(x>>32)))); | 55 "0"(bswap_32((uint32_t)x)),"1"(bswap_32((uint32_t)(x>>32)))); |
58 return __x.__ll; | 56 return __x.__ll; |
59 #endif | 57 #endif |
60 } | 58 } |
61 #define bswap_64(x) ByteSwap64(x) | |
62 | 59 |
63 #elif defined(ARCH_SH4) | 60 #elif defined(ARCH_SH4) |
64 | 61 |
65 static inline uint16_t ByteSwap16(uint16_t x) { | 62 static always_inline uint16_t bswap_16(uint16_t x) { |
66 __asm__("swap.b %0,%0":"=r"(x):"0"(x)); | 63 __asm__("swap.b %0,%0":"=r"(x):"0"(x)); |
67 return x; | 64 return x; |
68 } | 65 } |
69 | 66 |
70 static inline uint32_t ByteSwap32(uint32_t x) { | 67 static always_inline uint32_t bswap_32(uint32_t x) { |
71 __asm__( | 68 __asm__( |
72 "swap.b %0,%0\n" | 69 "swap.b %0,%0\n" |
73 "swap.w %0,%0\n" | 70 "swap.w %0,%0\n" |
74 "swap.b %0,%0\n" | 71 "swap.b %0,%0\n" |
75 :"=r"(x):"0"(x)); | 72 :"=r"(x):"0"(x)); |
76 return x; | 73 return x; |
77 } | 74 } |
78 | 75 |
79 #define bswap_16(x) ByteSwap16(x) | 76 static inline uint64_t bswap_64(uint64_t x) |
80 #define bswap_32(x) ByteSwap32(x) | |
81 | |
82 static inline uint64_t ByteSwap64(uint64_t x) | |
83 { | 77 { |
84 union { | 78 union { |
85 uint64_t ll; | 79 uint64_t ll; |
86 struct { | 80 struct { |
87 uint32_t l,h; | 81 uint32_t l,h; |
89 } r; | 83 } r; |
90 r.l.l = bswap_32 (x); | 84 r.l.l = bswap_32 (x); |
91 r.l.h = bswap_32 (x>>32); | 85 r.l.h = bswap_32 (x>>32); |
92 return r.ll; | 86 return r.ll; |
93 } | 87 } |
94 #define bswap_64(x) ByteSwap64(x) | |
95 | |
96 #else | 88 #else |
97 | 89 |
98 #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8) | 90 static always_inline uint16_t bswap_16(uint16_t x){ |
99 | 91 return (x>>8) | (x<<8); |
92 } | |
100 | 93 |
101 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc. | 94 static always_inline uint32_t bswap_32(uint32_t x){ |
102 #define bswap_32(x) \ | 95 return (x >> 24) | ((x & 0x00ff0000) >> 8) | ((x & 0x0000ff00) << 8) | (x << 24); |
103 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ | 96 } |
104 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) | |
105 | 97 |
106 static inline uint64_t ByteSwap64(uint64_t x) | 98 static inline uint64_t bswap_64(uint64_t x) |
107 { | 99 { |
108 union { | 100 union { |
109 uint64_t ll; | 101 uint64_t ll; |
110 uint32_t l[2]; | 102 uint32_t l[2]; |
111 } w, r; | 103 } w, r; |
112 w.ll = x; | 104 w.ll = x; |
113 r.l[0] = bswap_32 (w.l[1]); | 105 r.l[0] = bswap_32 (w.l[1]); |
114 r.l[1] = bswap_32 (w.l[0]); | 106 r.l[1] = bswap_32 (w.l[0]); |
115 return r.ll; | 107 return r.ll; |
116 } | 108 } |
117 #define bswap_64(x) ByteSwap64(x) | |
118 | |
119 #endif /* !ARCH_X86 */ | 109 #endif /* !ARCH_X86 */ |
120 | 110 |
121 #endif /* !HAVE_BYTESWAP_H */ | 111 #endif /* !HAVE_BYTESWAP_H */ |
122 | 112 |
123 // be2me ... BigEndian to MachineEndian | 113 // be2me ... BigEndian to MachineEndian |