// compiler_intrinsics__assume.cpp
#ifdef DEBUG
# define ASSERT(e) ( ((e) || assert(__FILE__, __LINE__) )
#else
# define ASSERT(e) ( __assume(e) )
#endif
void func1(int i)
{
}
int main(int p)
{
switch(p){
case 1:
func1(1);
break;
case 2:
func1(-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 whether p 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 __assume, that code might not run correctly if the expression inside the __assume statement is false at run time. If you are not sure that the expression will always be true at run time, you can use the assert function to protect the code:
# define ASSERT(e) ( ((e) || assert(__FILE__, __LINE__)), __assume(e) )
Unfortunately, this use of assert prevents the compiler from performing the default-case optimization previously shown. Therefore, you might want to use a separate macro instead:
#ifdef DEBUG
# define NODEFAULT ASSERT(0)
#else
# define NODEFAULT __assume(0)
#endif
default:
NODEFAULT;
When the following code example is compiled with the /Og compiler option, the identifier x is assumed to have a value of 1 wherever it appears after the __assume statement. The preincrement operation in the __assume statement does not actually occur and does not affect the value of x. The assumed value is printed and returned.
When the example is compiled with the /Od compiler option, the default, no assumptions are made about the value of x and its actual value is printed and returned.
#include <stdio.h>
int main()
{
int x=100;
__assume( (++x) == 1 );
printf("%d", x);
return x;
}
This example yields the following results:
Compiler option
|
Prints and returns
|
|---|
/Og (enable global optimizations)
|
1
|
/Od (disable optimizations)
|
100
|