Re: snag of G_LIKELY()



Daniel Elstner <daniel elstner gmx net> writes:

> Hi,
> 
> while using in G_LIKELY() in g_return_if_fail() and friends reduces the
> debugging overhead, there is an unfortunate snag:  Users of gcc > 3
> won't get a warning anymore if they accidentally put an assignment in
> the asserted condition.
> 
> See this thread for info:
> http://mail.gnome.org/archives/gtk-devel-list/2002-February/msg00184.html
> 
> I'm not promoting to remove G_LIKELY() -- I just think everyone should
> be aware of this catch.  Also, there's no point in doing
> 
>     if (G_LIKELY (expr)) { } else
> 
> anymore since the empty if() branch was only there to trigger the GCC
> warning in case of an assignment in the condition.

Hmm, the gcc-3.2 does compile the following test:

====
#define G_LIKELY(expr) __builtin_expect (!!(expr), 1)
#define G_UNLIKELY(expr) __builtin_expect (!!(expr), 0)

#define WARN_CONDITIONAL(a) ({                              \
  int _g_tmp_conditional;                                   \
  if (a) _g_tmp_conditional = 1; else _g_conditional = 0;   \
   _g_conditional;                                          \
})

int bar (void);

int foo (int a)
{
  if (!G_LIKELY (WARN_CONDITIONAL (a == 5)))
    return bar();

  return 0;
}
===

Correctly with -O2. (That is, it generates the same instructions
as without the WARN_CONDITIONAL()). Whether it's good to rely
on that, I'm not sure.

Regards,
                                        Owen



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]