Boost logo

Boost :

From: Alexander Nasonov (alnsn-boost_at_[hidden])
Date: 2005-09-22 05:40:40

Marcin Kalicinski <kalita <at>> writes:

> Hi All,
> Have you considered modifying BOOST_ASSERT to use __assume on MSVC?

I thought about optimizing assert in this way some time ago but I never looked
for implementation.

> Briefly speaking __assume(expr) is a hint for optimizer that 'expr'
> evaluates to nonzero. It can help optimize the code. Standalone use of
> __assume is not recommended because compiler does not check if the
> assumption is correct, and if it isn't the program is ill formed. But it
> fits perfectly in ASSERT macro. This way putting many ASSERTs will not only
> help to verify the code - it will also help the optimizer. Wow!


> To be sure I did a small test with MSVC:
> int f(int n)
> {
> // __assume(n == 0);
> if (n == 1) return 1; else return 0;
> }

I recall now how I came to that idea. I was reading
section 2.20 "Alternating among Two or More Values" when I realized
that this kind of optimization is possible without bits magic.

Marcin, could you try an example from the book

void f(int& n)
   // __assume(n == 0 || n == 1);
   if (n == 0) n = 1; else n = 0;

with and without __assume?

Alexander Nasonov

Boost list run by bdawes at, gregod at, cpdaniel at, john at