From dc645ce354f06c93f982a0177951c9f98627d751 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 20 Nov 2002 21:50:16 +0000 Subject: [PATCH] Use G_LIKELY. (#69022) * 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. --- ChangeLog | 10 ++++++++++ ChangeLog.pre-2-10 | 10 ++++++++++ ChangeLog.pre-2-12 | 10 ++++++++++ ChangeLog.pre-2-2 | 10 ++++++++++ ChangeLog.pre-2-4 | 10 ++++++++++ ChangeLog.pre-2-6 | 10 ++++++++++ ChangeLog.pre-2-8 | 10 ++++++++++ docs/reference/ChangeLog | 6 ++++++ docs/reference/glib/glib-sections.txt | 4 ++++ docs/reference/glib/tmpl/macros_misc.sgml | 26 ++++++++++++++++++++++++++ glib/gmacros.h | 15 +++++++++++++++ glib/gmessages.h | 9 +++++---- 12 files changed, 126 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9d25c46..db7f3ea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2002-11-20 Matthias Clasen + + * 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 * glib/gstrfuncs.c (g_ascii_strtoull): Fix diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 9d25c46..db7f3ea 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,13 @@ +2002-11-20 Matthias Clasen + + * 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 * glib/gstrfuncs.c (g_ascii_strtoull): Fix diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 9d25c46..db7f3ea 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,13 @@ +2002-11-20 Matthias Clasen + + * 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 * glib/gstrfuncs.c (g_ascii_strtoull): Fix diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 9d25c46..db7f3ea 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,13 @@ +2002-11-20 Matthias Clasen + + * 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 * glib/gstrfuncs.c (g_ascii_strtoull): Fix diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 9d25c46..db7f3ea 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,13 @@ +2002-11-20 Matthias Clasen + + * 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 * glib/gstrfuncs.c (g_ascii_strtoull): Fix diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 9d25c46..db7f3ea 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,13 @@ +2002-11-20 Matthias Clasen + + * 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 * glib/gstrfuncs.c (g_ascii_strtoull): Fix diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 9d25c46..db7f3ea 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,13 @@ +2002-11-20 Matthias Clasen + + * 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 * glib/gstrfuncs.c (g_ascii_strtoull): Fix diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index ac144a8..9183d5c 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,9 @@ +2002-11-20 Matthias Clasen + + * glib/tmpl/macros_misc.sgml: Document G_LIKELY, G_UNLIKELY. + + * glib/glib-sections.txt: Add G_LIKELY, G_UNLIKELY. + 2002-10-20 Matthias Clasen * gobject/Makefile.am (SCANOBJ_FILES): diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 4fd6fcb..80e4085 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -297,6 +297,10 @@ G_GNUC_PRETTY_FUNCTION G_GNUC_NO_INSTRUMENT +G_LIKELY +G_UNLIKELY + + G_STRLOC diff --git a/docs/reference/glib/tmpl/macros_misc.sgml b/docs/reference/glib/tmpl/macros_misc.sgml index bdccfc6..d16bc17 100644 --- a/docs/reference/glib/tmpl/macros_misc.sgml +++ b/docs/reference/glib/tmpl/macros_misc.sgml @@ -237,6 +237,32 @@ See the GNU C documentation for details. + + +Hints the compiler that the expression is likely to evaluate to a true +value. The compiler may use this information for optimizations. + + +if (G_LIKELY (random () != 1)) + g_print ("not one"); + + +@expr: the expression + + + + +Hints the compiler that the expression is unlikely to evaluate to a true +value. The compiler may use this information for optimizations. + + +if (G_UNLIKELY (random () == 1)) + g_print ("a random one"); + + +@expr: the expression + + Expands to a string identifying the current code position. diff --git a/glib/gmacros.h b/glib/gmacros.h index 549cd16..0e285b2 100644 --- a/glib/gmacros.h +++ b/glib/gmacros.h @@ -197,4 +197,19 @@ #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__ */ + + diff --git a/glib/gmessages.h b/glib/gmessages.h index 346a4b0..ecfcbca 100644 --- a/glib/gmessages.h +++ b/glib/gmessages.h @@ -29,6 +29,7 @@ #include #include +#include /* 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, \ -- 2.7.4