在很多代码里可以看到likely和unlikely宏定义,其定义为:

#define likely(x)        __builtin_expect(!!(x), 1)
#define unlikely(x)      __builtin_expect(!!(x), 0)

这两个宏定义使用了GCC内置函数,即便使用了-fno-buildin参数,这些函数仍然会被编译。

GCC includes built-in versions of many of the functions in the standard C library. The versions prefixed with __builtin_ will always be treated as having the same meaning as the C library function even if you specify the -fno-builtin option. 


抽取这两个宏定义,并结合使用定义如下出错提示定义:

#define BUG_ALERT_FALSE(condition) do \
{ \
	if (unlikely((condition) != 0)) \
		printf("BUG: failure at %s:%d/%s()!\n", \
		       __FILE__, __LINE__, __FUNCTION__); \
} while(0)

#define BUG_ALERT_TRUE(condition) do \
{ \
    if (likely((condition) != 0)) \
        printf("BUG: failure at %s:%d/%s()!\n", \
               __FILE__, __LINE__, __FUNCTION__); \
} while(0)

有了提示函数BUG_ALERT_FALSE和BUG_ALERT_TRUE,这里简单验证下效果:



验证代码如下:

int main(void)
{
	int i = 7;

	BUG_ALERT_FALSE(i!=10);
	BUG_ALERT_TRUE(i==7);

	return 0;
}

从这里我们可以看到这两个ALERT定义是如何使用的了吧。 ^_^


当然这样类似的内置函数,GCC提供了很多,其开头为__builtin_xxx,在项目中合理的使用这些内置函数,将大有裨益。

系列函数为:

编号gcc内置函数列表
1int __builtin_types_compatible_p (type1, type2)
2type __builtin_choose_expr (const_exp, exp1, exp2)
3type __builtin_complex (real, imag)
4int __builtin_constant_p (exp)
5long __builtin_expect (long exp, long c)
6void __builtin_trap (void)
7void __builtin_unreachable (void)
8void *__builtin_assume_aligned (const void *exp, size_t align, ...)
9void __builtin___clear_cache (char *begin, char *end)
10void __builtin_prefetch (const void *addr, ...)
11double __builtin_huge_val (void)
12float __builtin_huge_valf (void)
13long double __builtin_huge_vall (void)
14int __builtin_fpclassify (int, int, int, int, int, ...)
15double __builtin_inf (void)
16_Decimal32 __builtin_infd32 (void)
17_Decimal64 __builtin_infd64 (void)
18_Decimal128 __builtin_infd128 (void)
19float __builtin_inff (void)
20long double __builtin_infl (void)
21int __builtin_isinf_sign (...)
22double __builtin_nan (const char *str)
23_Decimal32 __builtin_nand32 (const char *str)
24_Decimal64 __builtin_nand64 (const char *str)
25_Decimal128 __builtin_nand128 (const char *str)
26float __builtin_nanf (const char *str)
27long double __builtin_nanl (const char *str)
28double __builtin_nans (const char *str)
29float __builtin_nansf (const char *str)
30long double __builtin_nansl (const char *str)
31int __builtin_ffs (unsigned int x)
32int __builtin_clz (unsigned int x)
33int __builtin_ctz (unsigned int x)
34int __builtin_clrsb (int x)
35int __builtin_popcount (unsigned int x)
36int __builtin_parity (unsigned int x)
37int __builtin_ffsl (unsigned long)
38int __builtin_clzl (unsigned long)
39int __builtin_ctzl (unsigned long)
40int __builtin_clrsbl (long)
41int __builtin_popcountl (unsigned long)
42int __builtin_parityl (unsigned long)
43int __builtin_ffsll (unsigned long long)
44int __builtin_clzll (unsigned long long)
45int __builtin_ctzll (unsigned long long)
46int __builtin_clrsbll (long long)
47int __builtin_popcountll (unsigned long long)
48int __builtin_parityll (unsigned long long)
49double __builtin_powi (double, int)
50float __builtin_powif (float, int)
51long double __builtin_powil (long double, int)
52int32_t __builtin_bswap32 (int32_t x)
53int64_t __builtin_bswap64 (int64_t x)

参考资料:

【1】gcc内置函数


Logo

更多推荐