Mercurial > libavcodec.hg
annotate bswap.h @ 538:7d45f7c1937e libavcodec
10L
| author | nickols_k |
|---|---|
| date | Thu, 11 Jul 2002 15:54:10 +0000 |
| parents | 133d2867d4b2 |
| children | 1e39f273ecd6 |
| rev | line source |
|---|---|
| 424 | 1 #ifndef __BSWAP_H__ |
| 2 #define __BSWAP_H__ | |
| 3 | |
| 4 #ifdef HAVE_BYTESWAP_H | |
| 5 #include <byteswap.h> | |
| 6 #else | |
| 7 | |
| 8 #ifdef ARCH_X86 | |
| 9 inline static unsigned short ByteSwap16(unsigned short x) | |
| 10 { | |
| 11 __asm("xchgb %b0,%h0" : | |
| 12 "=q" (x) : | |
| 13 "0" (x)); | |
| 14 return x; | |
| 15 } | |
| 16 #define bswap_16(x) ByteSwap16(x) | |
| 17 | |
| 18 inline static unsigned int ByteSwap32(unsigned int x) | |
| 19 { | |
| 20 #if __CPU__ > 386 | |
| 21 __asm("bswap %0": | |
| 22 "=r" (x) : | |
| 23 #else | |
| 24 __asm("xchgb %b0,%h0\n" | |
| 25 " rorl $16,%0\n" | |
| 26 " xchgb %b0,%h0": | |
| 27 "=q" (x) : | |
| 28 #endif | |
| 29 "0" (x)); | |
| 30 return x; | |
| 31 } | |
| 32 #define bswap_32(x) ByteSwap32(x) | |
| 33 | |
| 34 inline static unsigned long long int ByteSwap64(unsigned long long int x) | |
| 35 { | |
|
433
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
36 register union { __extension__ uint64_t __ll; |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
37 uint32_t __l[2]; } __x; |
| 424 | 38 asm("xchgl %0,%1": |
| 39 "=r"(__x.__l[0]),"=r"(__x.__l[1]): | |
| 40 "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32)))); | |
| 41 return __x.__ll; | |
| 42 } | |
| 43 #define bswap_64(x) ByteSwap64(x) | |
| 44 | |
| 45 #else | |
| 46 | |
| 47 #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8) | |
| 48 | |
| 49 | |
| 50 // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc. | |
| 51 #define bswap_32(x) \ | |
| 52 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ | |
| 53 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) | |
| 54 | |
|
433
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
55 inline static uint64_t ByteSwap64(uint64_t x) |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
56 { |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
57 union { |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
58 uint64_t ll; |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
59 uint32_t l[2]; |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
60 } w, r; |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
61 w.ll = x; |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
62 r.l[0] = bswap_32 (w.l[1]); |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
63 r.l[1] = bswap_32 (w.l[0]); |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
64 return r.ll; |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
65 } |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
66 #define bswap_64(x) ByteSwap64(x) |
|
133d2867d4b2
don't use inttypes.h - use inline function for bswap_64()
bellard
parents:
430
diff
changeset
|
67 |
| 424 | 68 #endif /* !ARCH_X86 */ |
| 69 | |
| 70 #endif /* !HAVE_BYTESWAP_H */ | |
| 71 | |
| 72 // be2me ... BigEndian to MachineEndian | |
| 73 // le2me ... LittleEndian to MachineEndian | |
| 74 | |
| 75 #ifdef WORDS_BIGENDIAN | |
| 76 #define be2me_16(x) (x) | |
| 77 #define be2me_32(x) (x) | |
| 78 #define be2me_64(x) (x) | |
| 79 #define le2me_16(x) bswap_16(x) | |
| 80 #define le2me_32(x) bswap_32(x) | |
| 81 #define le2me_64(x) bswap_64(x) | |
| 82 #else | |
| 83 #define be2me_16(x) bswap_16(x) | |
| 84 #define be2me_32(x) bswap_32(x) | |
| 85 #define be2me_64(x) bswap_64(x) | |
| 86 #define le2me_16(x) (x) | |
| 87 #define le2me_32(x) (x) | |
| 88 #define le2me_64(x) (x) | |
| 89 #endif | |
| 90 | |
| 91 #endif /* __BSWAP_H__ */ |
