Gcc的内建函数 __builtin_constant_p

来源:互联网 发布:学校网络运行管理制度 编辑:程序博客网 时间:2024/06/06 01:18

Gcc的内建函数 __builtin_constant_p 用于判断一个值是否为编译时常数,如果参数EXP 的值是常数,函数返回 1,否则返回 0。例如: 
++++ include/asm-i386/bitops.h 
249: #define test_bit(nr,addr) / 
250: (__builtin_constant_p(nr) ? / 
251: constant_test_bit((nr),(addr)) : / 
252: variable_test_bit((nr),(addr))) 
  很多计算或操作在参数为常数时有更优化的实现,在 GNU C 中用上面的方法可以根据参数是否为常数,只编译常数版本或非常数版本,这样既不失通用性,又能在参数是常数时编译出最优化的代码

转自:http://blog.csdn.net/lights_joy/archive/2008/05/08/2415410.aspx

 

I believe that this asks the compiler whether its argument evaluates 
to a constant value. For instance, __builtin_constant_p(1234) has a 
value of 1, whereas __builtin_constant_p(x), where `x` is a variable, 
has a value of 0.This is sometimes useful when writing highly-optimized code.

0 0
原创粉丝点击