[MPlayer-dev-eng] [PATCH] bswap.h which takes care of cpu's > 386 by using different asm commands

Dirk noisyb at gmx.net
Sun Aug 4 14:55:52 CEST 2002


this is a jazzed up version of bswap.h i'd lend the "if cpu>386 then
other asm cmd's" from somewhere else.. so dont ask me asm questions...
i'd test it a lot and it didn't seem to break anything... i also
couldn't see any speedups (what might depend).. so i wont be hurt if
this hits the trashbin.. :)

Dirk

-------------- next part --------------
Index: bswap.h
===================================================================
RCS file: /cvsroot/mplayer/main/bswap.h,v
retrieving revision 1.2
diff -u -r1.2 bswap.h
--- bswap.h	30 Jul 2001 09:08:23 -0000	1.2
+++ bswap.h	4 Aug 2002 13:18:52 -0000
@@ -11,62 +11,104 @@
 
 #include <inttypes.h>
 
-#ifdef ARCH_X86
+
 inline static unsigned short ByteSwap16(unsigned short x)
 {
-  __asm("xchgb %b0,%h0"	:
-        "=q" (x)	:
-        "0" (x));
-    return x;
+#ifdef ARCH_X86
+  __asm__ __volatile__
+  ("xchgb %b0, %h0"
+    : "=q" (x)
+    : "0" (x)
+  );
+#else
+  unsigned char *ptr = (unsigned char *) &x, tmp;
+  tmp = ptr[0];
+  ptr[0] = ptr[1];
+  ptr[1] = tmp;
+//  return (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8);
+#endif // ARCH_X86
+  return x;
 }
-#define bswap_16(x) ByteSwap16(x)
+
 
 inline static unsigned int ByteSwap32(unsigned int x)
 {
-#if __CPU__ > 386
- __asm("bswap	%0":
-      "=r" (x)     :
+#ifdef ARCH_X86
+  __asm__ __volatile__
+#if     __CPU__ > 386
+  ("bswap %0"
+    : "=r" (x)
+    : "0" (x)
 #else
- __asm("xchgb	%b0,%h0\n"
-      "	rorl	$16,%0\n"
-      "	xchgb	%b0,%h0":
-      "=q" (x)		:
+  ("
+    xchgb %b0, %h0
+    rorl $16, %0
+    xchgb %b0, %h0
+   "
+    : "=q" (x)
+    : "0" (x)
 #endif
-      "0" (x));
+  );
+#else
+  unsigned char *ptr = (unsigned char *) &x, tmp;
+  tmp = ptr[0];
+  ptr[0] = ptr[3];
+  ptr[3] = tmp;
+  tmp = ptr[1];
+  ptr[1] = ptr[2];
+  ptr[2] = tmp;
+//  return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |
+//          (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24));
+#endif // ARCH_X86
   return x;
 }
-#define bswap_32(x) ByteSwap32(x)
+
 
 inline static unsigned long long int ByteSwap64(unsigned long long int x)
 {
-  register union { __extension__ unsigned long long int __ll;
-          unsigned long int __l[2]; } __x;
-  asm("xchgl	%0,%1":
-      "=r"(__x.__l[0]),"=r"(__x.__l[1]):
-      "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
-  return __x.__ll;
-}
-#define bswap_64(x) ByteSwap64(x)
-
+#ifdef ARCH_X86
+  __asm__ __volatile__
+#if     __CPU__ > 386
+  ("
+    bswap %%eax
+    bswap %%edx
+    xchgl %%eax, %%edx
+   "
 #else
+  ("
+    xchgb %%al, %%ah
+    rorl $16, %%eax
+    xchgb %%al, %%ah
+    xchgb %%dl, %%dh
+    rorl $16, %%edx
+    xchgb %%dl, %%dh
+    xchgl %%eax, %%edx
+   "
+#endif
+    : "=A" (x)
+    : "A" (x)
+  );
+#else
+  unsigned char *ptr = (unsigned char *) &x, tmp;
+  tmp = ptr[0];
+  ptr[0] = ptr[7];
+  ptr[7] = tmp;
+  tmp = ptr[1];
+  ptr[1] = ptr[6];
+  ptr[6] = tmp;
+  tmp = ptr[2];
+  ptr[2] = ptr[5];
+  ptr[5] = tmp;
+  tmp = ptr[3];
+  ptr[3] = ptr[4];
+  ptr[4] = tmp;
+#endif // ARCH_X86
+  return x;
+}
 
-#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
-			
-
-// code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
-#define bswap_32(x) \
-     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
-      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
-
-#define bswap_64(x) \
-     (__extension__						\
-      ({ union { __extension__ unsigned long long int __ll;	\
-                 unsigned long int __l[2]; } __w, __r;		\
-         __w.__ll = (x);					\
-         __r.__l[0] = bswap_32 (__w.__l[1]);			\
-         __r.__l[1] = bswap_32 (__w.__l[0]);			\
-         __r.__ll; }))
-#endif	/* !ARCH_X86 */
+#define bswap_16(x) ByteSwap16(x)
+#define bswap_32(x) ByteSwap32(x)
+#define bswap_64(x) ByteSwap64(x)
 
 #endif	/* !HAVE_BYTESWAP_H */
 


More information about the MPlayer-dev-eng mailing list