2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #ifdef HAVE_BYTESWAP_H
32 # define LEGACY_REGS "=Q"
34 # define LEGACY_REGS "=q"
37 #if defined(ARCH_X86) || defined(ARCH_X86_64)
38 static always_inline uint16_t bswap_16(uint16_t x)
46 static always_inline uint32_t bswap_32(uint32_t x)
52 __asm("xchgb %b0,%h0\n"
61 static inline uint64_t bswap_64(uint64_t x)
76 r.l.h = bswap_32 (x>>32);
81 #elif defined(ARCH_SH4)
83 static always_inline uint16_t bswap_16(uint16_t x) {
84 __asm__("swap.b %0,%0":"=r"(x):"0"(x));
88 static always_inline uint32_t bswap_32(uint32_t x) {
97 static inline uint64_t bswap_64(uint64_t x)
105 r.l.l = bswap_32 (x);
106 r.l.h = bswap_32 (x>>32);
111 static always_inline uint16_t bswap_16(uint16_t x){
112 return (x>>8) | (x<<8);
116 static always_inline uint32_t bswap_32(uint32_t x){
119 "eor %1, %0, %0, ror #16 \n\t"
120 "bic %1, %1, #0xFF0000 \n\t"
121 "mov %0, %0, ror #8 \n\t"
122 "eor %0, %0, %1, lsr #8 \n\t"
127 static always_inline uint32_t bswap_32(uint32_t x){
128 x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
129 return (x>>16) | (x<<16);
133 static inline uint64_t bswap_64(uint64_t x)
136 x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
137 x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
138 return (x>>32) | (x<<32);
145 r.l[0] = bswap_32 (w.l[1]);
146 r.l[1] = bswap_32 (w.l[0]);
150 #endif /* !ARCH_X86 */
152 #endif /* !HAVE_BYTESWAP_H */
154 // be2me ... BigEndian to MachineEndian
155 // le2me ... LittleEndian to MachineEndian
157 #ifdef WORDS_BIGENDIAN
158 #define be2me_16(x) (x)
159 #define be2me_32(x) (x)
160 #define be2me_64(x) (x)
161 #define le2me_16(x) bswap_16(x)
162 #define le2me_32(x) bswap_32(x)
163 #define le2me_64(x) bswap_64(x)
165 #define be2me_16(x) bswap_16(x)
166 #define be2me_32(x) bswap_32(x)
167 #define be2me_64(x) bswap_64(x)
168 #define le2me_16(x) (x)
169 #define le2me_32(x) (x)
170 #define le2me_64(x) (x)
173 #endif /* __BSWAP_H__ */