Windows下C中__assume的作用
来源:互联网 发布:macbook专业修图软件 编辑:程序博客网 时间:2024/05/02 04:43
Microsoft Specific
__assume(expression)
The __assume keyword passes a hint to the optimizer. The optimizer assumes that the condition represented byexpression is true at the point where the keyword appears and remains true untilexpression is altered (for example, by assignment to a variable). Selective use of hints passed to the optimizer by__assume can improve optimization.
The most common use of __assume is with the default case of a switch statement, as shown below.
Example
#ifdef DEGUG# define ASSERT(e) ( ((e) || assert(__FILE__, __LINE__) )#else# define ASSERT(e) ( __assume(e) )#endifvoid gloo(int p){ switch(p){ case 1: blah(1); break; case 2: blah(-1); break; default: __assume(0); // This tells the optimizer that the default // cannot be reached. As so it does not have to generate // the extra code to check that 'p' has a value // not represented by a case arm. This makes the switch // run faster. }}
The use of __assume(0) tells the optimizer that the default case cannot be reached. As a result, the compiler does not generate code to test whetherp has a value not represented in a case statement. Note that__assume(0) must be the first statement in the body of the default case for this to work.
Because the compiler generates code based on the __assume statement, that code may not correctly if the expression inside the__assume statement is false at runtime. If you are not sure that the expression will always be true at runtime, you can use theassert function to protect the code:
Example
# define ASSERT(e) ( ((e) || assert(__FILE__, __LINE__)), __assume(e) )
Unfortunately, this use of assert prevents the compiler from performing the default-case optimization shown above. Therefore, you may want to use a separate macro instead:
Example
#ifdef DEBUG# define NODEFAULT ASSERT(0)#else# define NODEFAULT __assume(0)#endif default: NODEFAULT;
END Microsoft Specific
- Windows下C中__assume的作用
- C:/WINDOWS/system32/drivers/etc下的文件作用?
- windows 下 hosts 的作用
- Windows中C:\WINDOWS\system32\drivers\etc\hosts文件的作用
- 标准C函数 system 在windows 和 linux 下的作用
- c中static的作用
- c中static的作用
- c 中static的作用
- C中static的作用
- C中static的作用
- c中static的作用
- C中static的作用
- C语言中#、##的作用
- c中static的作用
- C语言中##的作用
- C语言中##的作用
- c中static的作用
- c中volatile的作用
- timer
- Java Web Project 转MVN Project
- split()中参数不能直接用.
- [C#]在WinForm下使用HttpWebRequest上传文件并显示进度
- 《CLR Via C# 第3版》笔记之(十四) - 泛型高级
- Windows下C中__assume的作用
- 小孩拉肚子不能吃什么?
- UIImageview 点击事件
- 使用Cygwin模拟Linux环境安装配置运行基于单机的Hadoop
- 2011年8月份总结
- C#.net Winform获取文件路径
- HTML跳转方法大全
- format:自己写的format函数
- sqlite3 常用操作