Partager via


__assume (Windows CE 5.0)

Send Feedback

The __assume intrinsic function passes a hint to the optimizer.

__assume(expression)

Remarks

The optimizer assumes that the condition represented by expression is true at the point where the keyword appears and remains true until expression is altered (for example, by assignment to a variable). Selective use of hints passed to the optimizer by __assume can improve optimization.

Example

The most common use of __assume is with the default case of a switch statement, as shown in the following code example.

#ifdef DEGUG
# define ASSERT(e)   ( ((e) || assert(__FILE__, __LINE__) )
#else
# define ASSERT(e)   ( __assume(e) )
#endif

void 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 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 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 the assert function to protect the code:

Requirements

OS Versions: Windows CE 5.0 and later.
Header: Cmnintrin.h.

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.