comparison bswap.h @ 330:2e7209604069 libavutil

reorder bswap functions into bit-depth, special-casing inside the functions. patch by Ramiro Polla ramiro lisha ufsc br
author benoit
date Thu, 26 Apr 2007 08:20:21 +0000
parents ecb4776617e1
children f21d1907d47c
comparison
equal deleted inserted replaced
329:ecb4776617e1 330:2e7209604069
34 # define LEGACY_REGS "=Q" 34 # define LEGACY_REGS "=Q"
35 #else 35 #else
36 # define LEGACY_REGS "=q" 36 # define LEGACY_REGS "=q"
37 #endif 37 #endif
38 38
39 #if defined(ARCH_X86)
40 static av_always_inline uint16_t bswap_16(uint16_t x) 39 static av_always_inline uint16_t bswap_16(uint16_t x)
41 { 40 {
41 #if defined(ARCH_X86)
42 __asm("rorw $8, %0" : 42 __asm("rorw $8, %0" :
43 LEGACY_REGS (x) : 43 LEGACY_REGS (x) :
44 "0" (x)); 44 "0" (x));
45 #elif defined(ARCH_SH4)
46 __asm__("swap.b %0,%0":"=r"(x):"0"(x));
47 #else
48 x= (x>>8) | (x<<8);
49 #endif
45 return x; 50 return x;
46 } 51 }
47 52
48 static av_always_inline uint32_t bswap_32(uint32_t x) 53 static av_always_inline uint32_t bswap_32(uint32_t x)
49 { 54 {
55 #if defined(ARCH_X86)
50 #if __CPU__ != 386 56 #if __CPU__ != 386
51 __asm("bswap %0": 57 __asm("bswap %0":
52 "=r" (x) : 58 "=r" (x) :
53 #else 59 #else
54 __asm("xchgb %b0,%h0\n" 60 __asm("xchgb %b0,%h0\n"
55 " rorl $16,%0\n" 61 " rorl $16,%0\n"
56 " xchgb %b0,%h0": 62 " xchgb %b0,%h0":
57 LEGACY_REGS (x) : 63 LEGACY_REGS (x) :
58 #endif 64 #endif
59 "0" (x)); 65 "0" (x));
60 return x;
61 }
62
63 static inline uint64_t bswap_64(uint64_t x)
64 {
65 #ifdef ARCH_X86_64
66 __asm("bswap %0":
67 "=r" (x) :
68 "0" (x));
69 return x;
70 #else
71 union {
72 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 #endif
81 }
82
83 #elif defined(ARCH_SH4) 66 #elif defined(ARCH_SH4)
84
85 static av_always_inline uint16_t bswap_16(uint16_t x) {
86 __asm__("swap.b %0,%0":"=r"(x):"0"(x));
87 return x;
88 }
89
90 static av_always_inline uint32_t bswap_32(uint32_t x) {
91 __asm__( 67 __asm__(
92 "swap.b %0,%0\n" 68 "swap.b %0,%0\n"
93 "swap.w %0,%0\n" 69 "swap.w %0,%0\n"
94 "swap.b %0,%0\n" 70 "swap.b %0,%0\n"
95 :"=r"(x):"0"(x)); 71 :"=r"(x):"0"(x));
96 return x; 72 #elif defined(ARCH_ARM)
97 }
98
99 static inline uint64_t bswap_64(uint64_t x)
100 {
101 union {
102 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 #else
112
113 static av_always_inline uint16_t bswap_16(uint16_t x){
114 x= (x>>8) | (x<<8);
115 return x;
116 }
117
118 #ifdef ARCH_ARM
119 static av_always_inline uint32_t bswap_32(uint32_t x){
120 uint32_t t; 73 uint32_t t;
121 __asm__ ( 74 __asm__ (
122 "eor %1, %0, %0, ror #16 \n\t" 75 "eor %1, %0, %0, ror #16 \n\t"
123 "bic %1, %1, #0xFF0000 \n\t" 76 "bic %1, %1, #0xFF0000 \n\t"
124 "mov %0, %0, ror #8 \n\t" 77 "mov %0, %0, ror #8 \n\t"
125 "eor %0, %0, %1, lsr #8 \n\t" 78 "eor %0, %0, %1, lsr #8 \n\t"
126 : "+r"(x), "+r"(t)); 79 : "+r"(x), "+r"(t));
127 return x;
128 }
129
130 #elif defined(ARCH_BFIN) 80 #elif defined(ARCH_BFIN)
131 static av_always_inline uint32_t bswap_32(uint32_t x){
132 unsigned tmp; 81 unsigned tmp;
133 asm("%1 = %0 >> 8 (V);\n\t" 82 asm("%1 = %0 >> 8 (V);\n\t"
134 "%0 = %0 << 8 (V);\n\t" 83 "%0 = %0 << 8 (V);\n\t"
135 "%0 = %0 | %1;\n\t" 84 "%0 = %0 | %1;\n\t"
136 "%0 = PACK(%0.L, %0.H);\n\t" 85 "%0 = PACK(%0.L, %0.H);\n\t"
137 : "+d"(x), "=&d"(tmp)); 86 : "+d"(x), "=&d"(tmp));
87 #else
88 x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
89 x= (x>>16) | (x<<16);
90 #endif
138 return x; 91 return x;
139 } 92 }
140
141 #else
142 static av_always_inline uint32_t bswap_32(uint32_t x){
143 x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
144 x= (x>>16) | (x<<16);
145 return x;
146 }
147 #endif
148 93
149 static inline uint64_t bswap_64(uint64_t x) 94 static inline uint64_t bswap_64(uint64_t x)
150 { 95 {
151 #if 0 96 #if 0
152 x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL); 97 x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
153 x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL); 98 x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
154 return (x>>32) | (x<<32); 99 return (x>>32) | (x<<32);
100 #elif defined(ARCH_X86_64)
101 __asm("bswap %0":
102 "=r" (x) :
103 "0" (x));
104 return x;
155 #else 105 #else
156 union { 106 union {
157 uint64_t ll; 107 uint64_t ll;
158 uint32_t l[2]; 108 uint32_t l[2];
159 } w, r; 109 } w, r;
161 r.l[0] = bswap_32 (w.l[1]); 111 r.l[0] = bswap_32 (w.l[1]);
162 r.l[1] = bswap_32 (w.l[0]); 112 r.l[1] = bswap_32 (w.l[0]);
163 return r.ll; 113 return r.ll;
164 #endif 114 #endif
165 } 115 }
166 #endif /* defined(ARCH_X86) */
167 116
168 #endif /* !HAVE_BYTESWAP_H */ 117 #endif /* !HAVE_BYTESWAP_H */
169 118
170 // be2me ... BigEndian to MachineEndian 119 // be2me ... BigEndian to MachineEndian
171 // le2me ... LittleEndian to MachineEndian 120 // le2me ... LittleEndian to MachineEndian