Linux应用程序之likely and unlikely宏定义
在很多代码里可以看到likely和unlikely宏定义,其定义为:#define likely(x)__builtin_expect(!!(x), 1)#define unlikely(x)__builtin_expect(!!(x), 0)这两个宏定义使用了GCC内置函数,即便使用了-fno-buildin参数,这些函数仍然会被编译。GCC i
·
在很多代码里可以看到likely和unlikely宏定义,其定义为:
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
这两个宏定义使用了GCC内置函数,即便使用了-fno-buildin参数,这些函数仍然会被编译。
抽取这两个宏定义,并结合使用定义如下出错提示定义:
#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内置函数列表 |
1 | int __builtin_types_compatible_p (type1, type2) |
2 | type __builtin_choose_expr (const_exp, exp1, exp2) |
3 | type __builtin_complex (real, imag) |
4 | int __builtin_constant_p (exp) |
5 | long __builtin_expect (long exp, long c) |
6 | void __builtin_trap (void) |
7 | void __builtin_unreachable (void) |
8 | void *__builtin_assume_aligned (const void *exp, size_t align, ...) |
9 | void __builtin___clear_cache (char *begin, char *end) |
10 | void __builtin_prefetch (const void *addr, ...) |
11 | double __builtin_huge_val (void) |
12 | float __builtin_huge_valf (void) |
13 | long double __builtin_huge_vall (void) |
14 | int __builtin_fpclassify (int, int, int, int, int, ...) |
15 | double __builtin_inf (void) |
16 | _Decimal32 __builtin_infd32 (void) |
17 | _Decimal64 __builtin_infd64 (void) |
18 | _Decimal128 __builtin_infd128 (void) |
19 | float __builtin_inff (void) |
20 | long double __builtin_infl (void) |
21 | int __builtin_isinf_sign (...) |
22 | double __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) |
26 | float __builtin_nanf (const char *str) |
27 | long double __builtin_nanl (const char *str) |
28 | double __builtin_nans (const char *str) |
29 | float __builtin_nansf (const char *str) |
30 | long double __builtin_nansl (const char *str) |
31 | int __builtin_ffs (unsigned int x) |
32 | int __builtin_clz (unsigned int x) |
33 | int __builtin_ctz (unsigned int x) |
34 | int __builtin_clrsb (int x) |
35 | int __builtin_popcount (unsigned int x) |
36 | int __builtin_parity (unsigned int x) |
37 | int __builtin_ffsl (unsigned long) |
38 | int __builtin_clzl (unsigned long) |
39 | int __builtin_ctzl (unsigned long) |
40 | int __builtin_clrsbl (long) |
41 | int __builtin_popcountl (unsigned long) |
42 | int __builtin_parityl (unsigned long) |
43 | int __builtin_ffsll (unsigned long long) |
44 | int __builtin_clzll (unsigned long long) |
45 | int __builtin_ctzll (unsigned long long) |
46 | int __builtin_clrsbll (long long) |
47 | int __builtin_popcountll (unsigned long long) |
48 | int __builtin_parityll (unsigned long long) |
49 | double __builtin_powi (double, int) |
50 | float __builtin_powif (float, int) |
51 | long double __builtin_powil (long double, int) |
52 | int32_t __builtin_bswap32 (int32_t x) |
53 | int64_t __builtin_bswap64 (int64_t x) |
参考资料:
【1】gcc内置函数
更多推荐
已为社区贡献2条内容
所有评论(0)