首页 > 编程知识 正文

excel函数公式大全讲解,单调性英文

时间:2023-05-05 03:48:21 阅读:153523 作者:1732

内核源: linux-2.6.38.8.tar.bz2

参考文献: http://gcc.GNU.org/online docs/gcc-4.6.2/gcc/other-builtins.html # other-builtins

在Linux内核中,likely和unlikely函数有两种实现方式,实现原理略有不同,但作用相同。 以下内容结合linux-2.6.38.8版本的内核代码进行说明。

1、__builtin_expect的程序包

源代码如下所示。

/* Linux-2.6.38.8/include/Linux/compiler.h */# define likely (x ) __builtin_expect (! (x ),1 ) #defineunlikely(x ) __builtin_expect (! (x ),0 ) __builtin_expect是被包括在GCC中的函数,用于优化用于选择语句的判断条件,如果一个判断条件经常成立(例如likely ),或者不经常成立(例如uultin )

__builtin_expect的函数的原型是long__builtin_expect(longexp,long c ),返回值是完整的表达式exp的值,起到期望表达式exp的值等于c的作用

也可以在常规APP应用程序中使用__builtin_expect,如下例所示:

#includestdio.hintmain(void ) { int a; scanf('%d ',a ); if(_Builtin_expect(a,4 ) ) printf('if:a=%dn ',a ); ELSEprintf(ELSE:a=%dn ),a ); 返回0; }分别输入整数0~4进行5次测试,各自的输出如下所示。

else : a=0if : a=1if : a=2if : a=3if : a=4注意。 在上面的示例中,只有在输入整数0时才执行else后的打印语句。 即__builtin_expect(a,4 )

请注意,这些只是提高性能的优化方法,不会改变原始表达式的值。

2、使用__branch_check__函数

源代码如下所示。

/* Linux-2.6.38.8/include/Linux/compiler.h */# ifndef likely # define likely (x ) __builtin_constanny ! (x ) :__branch_check_(x,1 ) ) endif # ifndef unlikely # define unlikely ) x ) __builtin_constannany ! (x ) :_Branch_check_(x,0 ) ) Endif )1),首先使用内置函数__builtin_constant_p来忽略表达式x为常数

__builtin_constant_p也是GCC的嵌入函数,函数的原型是int_builtin_constant_p(exp ),为了确定表达式exp在编译时是否为常数如果不是,则函数值为整数1,否则为0,如下所示

# include stdio.h # include stdlib.h # define value 5int main (void ) { char *ptr=NULL; int num,count; ptr=malloc(20; num=__Builtin_constant_p(ptr )? 20 : 20 10; printf(num=%d(n ),num ); free(ptr ); count=_ _ builtin _ constant _ p (value )? 20值: 10; printf('count=%dn ',count ); 返回0; }示例的输出结果:

num=30count=25的示例中的ptr是指针变量,因此__builtin_constant_p(ptr )的值为0,num的值为30。

(2)、函数__branch_check__的实现

/* Linux-2.6.38.8/include/Linux/compiler.h */# define _ branch _ check _ (x,expect ) { _ int } ftrace_likely_update(______f,______r,expect ); ______r; ) )它用于检查决策条件,记录likely确定的预测信息,然后根据预测信息进行相应的优化以提高性能。

函数__branch_check__的返回值是______r的值,即参数x的值。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。