Use G_LIKELY. (#69022)
authorMatthias Clasen <matthiasc@src.gnome.org>
Wed, 20 Nov 2002 21:50:16 +0000 (21:50 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Wed, 20 Nov 2002 21:50:16 +0000 (21:50 +0000)
* glib/gmessages.h (g_return_if_fail):
(g_return_val_if_fail): Use G_LIKELY.  (#69022)

* glib/gmacros.h (G_LIKELY):
(G_UNLIKELY): New macros for hinting the compiler about the
expected result of expressions. For gcc 3.x, define these
using __builtin_expect.  (#69022)

* glib/tmpl/macros_misc.sgml: Document G_LIKELY, G_UNLIKELY.

* glib/glib-sections.txt: Add G_LIKELY, G_UNLIKELY.

12 files changed:
ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
docs/reference/ChangeLog
docs/reference/glib/glib-sections.txt
docs/reference/glib/tmpl/macros_misc.sgml
glib/gmacros.h
glib/gmessages.h

index 9d25c46..db7f3ea 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2002-11-20  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gmessages.h (g_return_if_fail): 
+       (g_return_val_if_fail): Use G_LIKELY.  (#69022)
+
+       * glib/gmacros.h (G_LIKELY): 
+       (G_UNLIKELY): New macros for hinting the compiler about the
+       expected result of expressions. For gcc 3.x, define these
+       using __builtin_expect.  (#69022)
+
 Tue Nov 19 14:38:18 2002  Owen Taylor  <otaylor@redhat.com>
 
        * glib/gstrfuncs.c (g_ascii_strtoull): Fix 
index 9d25c46..db7f3ea 100644 (file)
@@ -1,3 +1,13 @@
+2002-11-20  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gmessages.h (g_return_if_fail): 
+       (g_return_val_if_fail): Use G_LIKELY.  (#69022)
+
+       * glib/gmacros.h (G_LIKELY): 
+       (G_UNLIKELY): New macros for hinting the compiler about the
+       expected result of expressions. For gcc 3.x, define these
+       using __builtin_expect.  (#69022)
+
 Tue Nov 19 14:38:18 2002  Owen Taylor  <otaylor@redhat.com>
 
        * glib/gstrfuncs.c (g_ascii_strtoull): Fix 
index 9d25c46..db7f3ea 100644 (file)
@@ -1,3 +1,13 @@
+2002-11-20  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gmessages.h (g_return_if_fail): 
+       (g_return_val_if_fail): Use G_LIKELY.  (#69022)
+
+       * glib/gmacros.h (G_LIKELY): 
+       (G_UNLIKELY): New macros for hinting the compiler about the
+       expected result of expressions. For gcc 3.x, define these
+       using __builtin_expect.  (#69022)
+
 Tue Nov 19 14:38:18 2002  Owen Taylor  <otaylor@redhat.com>
 
        * glib/gstrfuncs.c (g_ascii_strtoull): Fix 
index 9d25c46..db7f3ea 100644 (file)
@@ -1,3 +1,13 @@
+2002-11-20  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gmessages.h (g_return_if_fail): 
+       (g_return_val_if_fail): Use G_LIKELY.  (#69022)
+
+       * glib/gmacros.h (G_LIKELY): 
+       (G_UNLIKELY): New macros for hinting the compiler about the
+       expected result of expressions. For gcc 3.x, define these
+       using __builtin_expect.  (#69022)
+
 Tue Nov 19 14:38:18 2002  Owen Taylor  <otaylor@redhat.com>
 
        * glib/gstrfuncs.c (g_ascii_strtoull): Fix 
index 9d25c46..db7f3ea 100644 (file)
@@ -1,3 +1,13 @@
+2002-11-20  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gmessages.h (g_return_if_fail): 
+       (g_return_val_if_fail): Use G_LIKELY.  (#69022)
+
+       * glib/gmacros.h (G_LIKELY): 
+       (G_UNLIKELY): New macros for hinting the compiler about the
+       expected result of expressions. For gcc 3.x, define these
+       using __builtin_expect.  (#69022)
+
 Tue Nov 19 14:38:18 2002  Owen Taylor  <otaylor@redhat.com>
 
        * glib/gstrfuncs.c (g_ascii_strtoull): Fix 
index 9d25c46..db7f3ea 100644 (file)
@@ -1,3 +1,13 @@
+2002-11-20  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gmessages.h (g_return_if_fail): 
+       (g_return_val_if_fail): Use G_LIKELY.  (#69022)
+
+       * glib/gmacros.h (G_LIKELY): 
+       (G_UNLIKELY): New macros for hinting the compiler about the
+       expected result of expressions. For gcc 3.x, define these
+       using __builtin_expect.  (#69022)
+
 Tue Nov 19 14:38:18 2002  Owen Taylor  <otaylor@redhat.com>
 
        * glib/gstrfuncs.c (g_ascii_strtoull): Fix 
index 9d25c46..db7f3ea 100644 (file)
@@ -1,3 +1,13 @@
+2002-11-20  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gmessages.h (g_return_if_fail): 
+       (g_return_val_if_fail): Use G_LIKELY.  (#69022)
+
+       * glib/gmacros.h (G_LIKELY): 
+       (G_UNLIKELY): New macros for hinting the compiler about the
+       expected result of expressions. For gcc 3.x, define these
+       using __builtin_expect.  (#69022)
+
 Tue Nov 19 14:38:18 2002  Owen Taylor  <otaylor@redhat.com>
 
        * glib/gstrfuncs.c (g_ascii_strtoull): Fix 
index ac144a8..9183d5c 100644 (file)
@@ -1,3 +1,9 @@
+2002-11-20  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/tmpl/macros_misc.sgml: Document G_LIKELY, G_UNLIKELY.
+
+       * glib/glib-sections.txt: Add G_LIKELY, G_UNLIKELY.
+
 2002-10-20  Matthias Clasen  <maclas@gmx.de>
 
        * gobject/Makefile.am (SCANOBJ_FILES): 
index 4fd6fcb..80e4085 100644 (file)
@@ -297,6 +297,10 @@ G_GNUC_PRETTY_FUNCTION
 G_GNUC_NO_INSTRUMENT
 
 <SUBSECTION>
+G_LIKELY
+G_UNLIKELY
+
+<SUBSECTION>
 G_STRLOC
 
 <SUBSECTION>
index bdccfc6..d16bc17 100644 (file)
@@ -237,6 +237,32 @@ See the GNU C documentation for details.
 
 
 
+<!-- ##### MACRO G_LIKELY ##### -->
+<para>
+Hints the compiler that the expression is likely to evaluate to a true
+value. The compiler may use this information for optimizations.
+</para>
+<informalexample><programlisting>
+if (G_LIKELY (random () != 1))
+  g_print ("not one");
+</programlisting></informalexample>
+
+@expr: the expression
+
+
+<!-- ##### MACRO G_UNLIKELY ##### -->
+<para>
+Hints the compiler that the expression is unlikely to evaluate to a true
+value. The compiler may use this information for optimizations.
+</para>
+<informalexample><programlisting>
+if (G_UNLIKELY (random () == 1))
+  g_print ("a random one");
+</programlisting></informalexample>
+
+@expr: the expression
+
+
 <!-- ##### MACRO G_STRLOC ##### -->
 <para>
 Expands to a string identifying the current code position. 
index 549cd16..0e285b2 100644 (file)
 #define G_CONST_RETURN const
 #endif
 
+/*
+ * The G_LIKELY and G_UNLIKELY macros let the programmer give hints to 
+ * the compiler about the expected result of an expression. Some compilers
+ * can use this information for optimizations.
+ */
+#if defined(__GNUC__) && (__GNUC__ > 2)
+#define G_LIKELY(expr) __builtin_expect (!!(expr), 1)
+#define G_UNLIKELY(expr) __builtin_expect (!!(expr), 0)
+#else
+#define G_LIKELY(expr) expr
+#define G_UNLIKELY(expr) expr
+#endif
+
 #endif /* __G_MACROS_H__ */
+
+
index 346a4b0..ecfcbca 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <stdarg.h>
 #include <glib/gtypes.h>
+#include <glib/gmacros.h>
 
 /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
  */
@@ -252,7 +253,7 @@ GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
 #ifdef __GNUC__
 
 #define g_return_if_fail(expr)         G_STMT_START{                   \
-     if (expr) { } else                                                        \
+     if (G_LIKELY (expr)) { } else                                     \
        {                                                               \
         g_log (G_LOG_DOMAIN,                                           \
                G_LOG_LEVEL_CRITICAL,                                   \
@@ -265,7 +266,7 @@ GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
        };                              }G_STMT_END
 
 #define g_return_val_if_fail(expr,val) G_STMT_START{                   \
-     if (expr) { } else                                                        \
+     if (G_LIKELY (expr)) { } else                                                     \
        {                                                               \
         g_log (G_LOG_DOMAIN,                                           \
                G_LOG_LEVEL_CRITICAL,                                   \
@@ -298,7 +299,7 @@ GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
 #else /* !__GNUC__ */
 
 #define g_return_if_fail(expr)         G_STMT_START{           \
-     if (expr) { } else                                                \
+     if (G_LIKELY (expr)) { } else                                             \
        {                                                       \
         g_log (G_LOG_DOMAIN,                                   \
                G_LOG_LEVEL_CRITICAL,                           \
@@ -310,7 +311,7 @@ GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
        };                              }G_STMT_END
 
 #define g_return_val_if_fail(expr, val)        G_STMT_START{           \
-     if (expr) { } else                                                \
+     if (G_LIKELY (expr)) { } else                                             \
        {                                                       \
         g_log (G_LOG_DOMAIN,                                   \
                G_LOG_LEVEL_CRITICAL,                           \