Replace macros for byteswapping into static inline functions.

From: Diego 'Flameeyes' Pettenò <flameeyes@gmail.com>


---

 glib/gtypes.h |  337 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 203 insertions(+), 134 deletions(-)


diff --git a/glib/gtypes.h b/glib/gtypes.h
index 56cc648..4bb3012 100644
--- a/glib/gtypes.h
+++ b/glib/gtypes.h
@@ -126,151 +126,196 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
 
 /* Basic bit swapping functions
  */
-#define GUINT16_SWAP_LE_BE_CONSTANT(val)	((guint16) ( \
-    (guint16) ((guint16) (val) >> 8) |	\
-    (guint16) ((guint16) (val) << 8)))
-
-#define GUINT32_SWAP_LE_BE_CONSTANT(val)	((guint32) ( \
-    (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
-    (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
-    (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
-    (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
-
-#define GUINT64_SWAP_LE_BE_CONSTANT(val)	((guint64) ( \
-      (((guint64) (val) &						\
-	(guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) |	\
-      (((guint64) (val) &						\
-	(guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) |	\
-      (((guint64) (val) &						\
-	(guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) |	\
-      (((guint64) (val) &						\
-	(guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) <<  8) |	\
-      (((guint64) (val) &						\
-	(guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >>  8) |	\
-      (((guint64) (val) &						\
-	(guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) |	\
-      (((guint64) (val) &						\
-	(guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) |	\
-      (((guint64) (val) &						\
-	(guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)))
+static inline guint16
+GUINT16_SWAP_LE_BE_CONSTANT(guint16 val)
+{
+  return ((guint16) (
+    (guint16) ((guint16) (val) >> 8) |
+    (guint16) ((guint16) (val) << 8)));
+}
+
+static inline guint32
+GUINT32_SWAP_LE_BE_CONSTANT(guint32 val)
+{
+  return ((guint32) (
+    (((guint32) (val) & (guint32) 0x000000ffU) << 24) |
+    (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) |
+    (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) |
+    (((guint32) (val) & (guint32) 0xff000000U) >> 24)));
+}
+
+static inline guint64
+GUINT64_SWAP_LE_BE_CONSTANT(guint64 val)
+{
+  return ((guint64) (
+      (((guint64) (val) &
+	(guint64) G_GINT64_CONSTANT (0x00000000000000ffU)) << 56) |
+      (((guint64) (val) &
+	(guint64) G_GINT64_CONSTANT (0x000000000000ff00U)) << 40) |
+      (((guint64) (val) &
+	(guint64) G_GINT64_CONSTANT (0x0000000000ff0000U)) << 24) |
+      (((guint64) (val) &
+	(guint64) G_GINT64_CONSTANT (0x00000000ff000000U)) <<  8) |
+      (((guint64) (val) &
+	(guint64) G_GINT64_CONSTANT (0x000000ff00000000U)) >>  8) |
+      (((guint64) (val) &
+	(guint64) G_GINT64_CONSTANT (0x0000ff0000000000U)) >> 24) |
+      (((guint64) (val) &
+	(guint64) G_GINT64_CONSTANT (0x00ff000000000000U)) >> 40) |
+      (((guint64) (val) &
+	(guint64) G_GINT64_CONSTANT (0xff00000000000000U)) >> 56)));
+}
 
 /* Arch specific stuff for speed
  */
 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
+# define GUINT16_SWAP_LE_BE_IA32 GUINT16_SWAP_LE_BE_IA32
+# define GUINT32_SWAP_LE_BE_IA32 GUINT32_SWAP_LE_BE_IA32
+# define GUINT64_SWAP_LE_BE_IA32 GUINT64_SWAP_LE_BE_IA32
 #  if defined (__i386__)
-#    define GUINT16_SWAP_LE_BE_IA32(val) \
-       (__extension__						\
-	({ register guint16 __v, __x = ((guint16) (val));	\
-	   if (__builtin_constant_p (__x))			\
-	     __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);		\
-	   else							\
-	     __asm__ ("rorw $8, %w0"				\
-		      : "=r" (__v)				\
-		      : "0" (__x)				\
-		      : "cc");					\
-	    __v; }))
+static inline guint16
+GUINT16_SWAP_LE_BE_IA32(guint16 val)
+{
+  register guint16 v;
+  if (__builtin_constant_p (val))
+    v = GUINT16_SWAP_LE_BE_CONSTANT (val);
+  else
+    __asm__ ("rorw $8, %w0"
+	     : "=r" (v)
+	     : "0" (val)
+	     : "cc");
+  
+  return v;
+}
 #    if !defined (__i486__) && !defined (__i586__) \
 	&& !defined (__pentium__) && !defined (__i686__) \
 	&& !defined (__pentiumpro__) && !defined (__pentium4__)
-#       define GUINT32_SWAP_LE_BE_IA32(val) \
-	  (__extension__					\
-	   ({ register guint32 __v, __x = ((guint32) (val));	\
-	      if (__builtin_constant_p (__x))			\
-		__v = GUINT32_SWAP_LE_BE_CONSTANT (__x);	\
-	      else						\
-		__asm__ ("rorw $8, %w0\n\t"			\
-			 "rorl $16, %0\n\t"			\
-			 "rorw $8, %w0"				\
-			 : "=r" (__v)				\
-			 : "0" (__x)				\
-			 : "cc");				\
-	      __v; }))
+static inline guint32
+GUINT32_SWAP_LE_BE_IA32(guint32 val)
+{
+  register guint32 v;
+  if (__builtin_constant_p (val))
+    v = GUINT32_SWAP_LE_BE_CONSTANT (val);
+  else
+    __asm__ ("rorw $8, %w0\n\t"
+	     "rorl $16, %0\n\t"
+	     "rorw $8, %w0"
+	     : "=r" (v)
+	     : "0" (val)
+	     : "cc");
+  return v;
+}
 #    else /* 486 and higher has bswap */
-#       define GUINT32_SWAP_LE_BE_IA32(val) \
-	  (__extension__					\
-	   ({ register guint32 __v, __x = ((guint32) (val));	\
-	      if (__builtin_constant_p (__x))			\
-		__v = GUINT32_SWAP_LE_BE_CONSTANT (__x);	\
-	      else						\
-		__asm__ ("bswap %0"				\
-			 : "=r" (__v)				\
-			 : "0" (__x));				\
-	      __v; }))
+static inline guint32
+GUINT32_SWAP_LE_BE_IA32(guint32 val)
+{
+  register guint32 v;
+  if (__builtin_constant_p (val))
+    v = GUINT32_SWAP_LE_BE_CONSTANT (val);
+  else
+    __asm__ ("bswap %0"
+	     : "=r" (__v)
+	     : "0" (__x));
+  return v;
+}
 #    endif /* processor specific 32-bit stuff */
-#    define GUINT64_SWAP_LE_BE_IA32(val) \
-       (__extension__							\
-	({ union { guint64 __ll;					\
-		   guint32 __l[2]; } __w, __r;				\
-	   __w.__ll = ((guint64) (val));				\
-	   if (__builtin_constant_p (__w.__ll))				\
-	     __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (__w.__ll);		\
-	   else								\
-	     {								\
-	       __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);		\
-	       __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);		\
-	     }								\
-	   __r.__ll; }))
+static inline guint64
+GUINT64_SWAP_LE_BE_IA64(guint64 val)
+{
+  union { guint64 ll;
+          guint32 l[2]; } w,r;
+  w.ll = ((guint64) (val));
+  if (__builtin_constant_p (w.ll))
+    r.ll = GUINT64_SWAP_LE_BE_CONSTANT (w.ll);
+  else
+    {
+      r.l[0] = GUINT32_SWAP_LE_BE (w.l[1]);
+      r.l[1] = GUINT32_SWAP_LE_BE (w.l[0]);
+    }
+  return r.ll;
+}
      /* Possibly just use the constant version and let gcc figure it out? */
 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA32 (val))
 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA32 (val))
 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA32 (val))
 #  elif defined (__ia64__)
-#    define GUINT16_SWAP_LE_BE_IA64(val) \
-       (__extension__						\
-	({ register guint16 __v, __x = ((guint16) (val));	\
-	   if (__builtin_constant_p (__x))			\
-	     __v = GUINT16_SWAP_LE_BE_CONSTANT (__x);		\
-	   else							\
-	     __asm__ __volatile__ ("shl %0 = %1, 48 ;;"		\
-				   "mux1 %0 = %0, @rev ;;"	\
-				    : "=r" (__v)		\
-				    : "r" (__x));		\
-	    __v; }))
-#    define GUINT32_SWAP_LE_BE_IA64(val) \
-       (__extension__						\
-	 ({ register guint32 __v, __x = ((guint32) (val));	\
-	    if (__builtin_constant_p (__x))			\
-	      __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);		\
-	    else						\
-	     __asm__ __volatile__ ("shl %0 = %1, 32 ;;"		\
-				   "mux1 %0 = %0, @rev ;;"	\
-				    : "=r" (__v)		\
-				    : "r" (__x));		\
-	    __v; }))
-#    define GUINT64_SWAP_LE_BE_IA64(val) \
-       (__extension__						\
-	({ register guint64 __v, __x = ((guint64) (val));	\
-	   if (__builtin_constant_p (__x))			\
-	     __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);		\
-	   else							\
-	     __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;"	\
-				   : "=r" (__v)			\
-				   : "r" (__x));		\
-	   __v; }))
+# define GUINT16_SWAP_LE_BE_IA64 GUINT16_SWAP_LE_BE_IA64
+# define GUINT32_SWAP_LE_BE_IA64 GUINT32_SWAP_LE_BE_IA64
+# define GUINT64_SWAP_LE_BE_IA64 GUINT64_SWAP_LE_BE_IA64
+static inline guint16
+GUINT16_SWAP_LE_BE_IA64(guint16 val)
+{
+  register guint16 v;
+  if (__builtin_constant_p (val))
+    v = GUINT16_SWAP_LE_BE_CONSTANT (val);
+  else
+    __asm__ volatile__ ("shl %0 = %1, 48 ;;"
+			"mux1 %0 = %0, @rev ;;"
+			: "=r" (v)
+			: "r" (val));
+  return v;
+}
+
+static inline guint32
+GUINT32_SWAP_LE_BE_IA64(guint32 val)
+{
+  register guint32 v;
+  if (__builtin_constant_p (val))
+    v = GUINT32_SWAP_LE_BE_CONSTANT (val);
+  else
+    __asm__ volatile__ ("shl %0 = %1, 32 ;;"
+			"mux1 %0 = %0, @rev ;;"
+			: "=r" (v)
+			: "r" (val));
+  return v;
+}
+
+static inline guint64
+GUINT64_SWAP_LE_BE_IA64(guint64 val)
+{
+  register guint64 v;
+  if (__builtin_constant_p (val))
+    v = GUINT64_SWAP_LE_BE_CONSTANT (val);
+  else
+    __asm__ volatile__ ("mux1 %0 = %1, @rev ;;"
+			: "=r" (v)
+			: "r" (val));
+  return v;
+}
+
 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_IA64 (val))
 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_IA64 (val))
 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_IA64 (val))
 #  elif defined (__x86_64__)
-#    define GUINT32_SWAP_LE_BE_X86_64(val) \
-       (__extension__						\
-	 ({ register guint32 __v, __x = ((guint32) (val));	\
-	    if (__builtin_constant_p (__x))			\
-	      __v = GUINT32_SWAP_LE_BE_CONSTANT (__x);		\
-	    else						\
-	     __asm__ ("bswapl %0"				\
-		      : "=r" (__v)				\
-		      : "0" (__x));				\
-	    __v; }))
-#    define GUINT64_SWAP_LE_BE_X86_64(val) \
-       (__extension__						\
-	({ register guint64 __v, __x = ((guint64) (val));	\
-	   if (__builtin_constant_p (__x))			\
-	     __v = GUINT64_SWAP_LE_BE_CONSTANT (__x);		\
-	   else							\
-	     __asm__ ("bswapq %0"				\
-		      : "=r" (__v)				\
-		      : "0" (__x));				\
-	   __v; }))
+# define GUINT32_SWAP_LE_BE_X86_64 GUINT32_SWAP_LE_BE_X86_64
+# define GUINT64_SWAP_LE_BE_X86_64 GUINT64_SWAP_LE_BE_X86_64
+
+static inline guint32
+GUINT32_SWAP_LE_BE_X86_64(guint32 val)
+{
+  register guint32 v;
+  if (__builtin_constant_p (val))
+    v = GUINT32_SWAP_LE_BE_CONSTANT (val);
+  else
+    __asm__ ("bswapl %0"
+	     : "=r" (v)
+	     : "0" (val));
+  return v;
+}
+
+static inline guint64
+GUINT64_SWAP_LE_BE_X86_64(guint64 val)
+{
+  register guint64 v;
+  if (__builtin_constant_p (val))
+    v = GUINT64_SWAP_LE_BE_CONSTANT (val);
+  else
+    __asm__ ("bswapq %0"
+	     : "=r" (v)
+	     : "0" (val));
+  return v;
+}
+
      /* gcc seems to figure out optimal code for this on its own */
 #    define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
 #    define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86_64 (val))
@@ -286,14 +331,38 @@ typedef const gchar *   (*GTranslateFunc)       (const gchar   *str,
 #  define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT (val))
 #endif /* generic */
 
-#define GUINT16_SWAP_LE_PDP(val)	((guint16) (val))
-#define GUINT16_SWAP_BE_PDP(val)	(GUINT16_SWAP_LE_BE (val))
-#define GUINT32_SWAP_LE_PDP(val)	((guint32) ( \
-    (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
-    (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
-#define GUINT32_SWAP_BE_PDP(val)	((guint32) ( \
-    (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
-    (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
+static inline guint16
+GUINT16_SWAP_LE_PDP(guint16 val)
+{
+  return val;
+}
+
+static inline guint16
+GUINT16_SWAP_BE_PDP(guint16 val)
+{
+  return GUINT16_SWAP_LE_BE (val);
+}
+
+static inline guint32
+GUINT32_SWAP_LE_PDP(guint32 val)
+{
+  return ((guint32) (
+    (((guint32) (val) & (guint32) 0x0000ffffU) << 16) |
+    (((guint32) (val) & (guint32) 0xffff0000U) >> 16)));
+}
+
+static inline guint32
+GUINT32_SWAP_BE_PDP(guint32 val)
+{
+  return ((guint32) (
+    (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) |
+    (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)));
+}
+
+#define GUINT16_SWAP_LE_PDP GUINT16_SWAP_LE_PDP
+#define GUINT16_SWAP_BE_PDP GUINT16_SWAP_BE_PDP
+#define GUINT32_SWAP_LE_PDP GUINT32_SWAP_LE_PDP
+#define GUINT32_SWAP_BE_PDP GUINT32_SWAP_BE_PDP
 
 /* The G*_TO_?E() macros are defined in glibconfig.h.
  * The transformation is symmetric, so the FROM just maps to the TO.
