[llvm-commits] [llvm] r107199 - /llvm/trunk/include/llvm/ADT/SmallPtrSet.h

Duncan Sands baldrick at free.fr
Wed Jun 30 07:49:12 PDT 2010


Hi Jakob,

>> NextPowerOfTwo was already used to ensure that SmallSizePowTwo is a power
>> of two, no need to do it a second time (NextPowerOfTwo is idempotent).
>
> No it isn't.
>
> Ignoring overflow, NextPowerOfTwo(x) is always strictly greater than x.

are you sure?  C++ templates make my head hurt, but as far as I can see it
works as follows.  Suppose N is a power of two, then the definition

   template<unsigned N>
   struct NextPowerOfTwo {
     enum { Val = NextPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
   };

gives [since the condition (N&(N-1)) == 0 evaluates to true]

   struct NextPowerOfTwo {
   enum { Val = NextPowerOfTwoH<N, true>::Val };
   };

Since NextPowerOfTwoH<N, true> is defined by

   template<unsigned N, bool isPowerTwo>
   struct NextPowerOfTwoH {
     enum { Val = N };
   };

it looks to me like you get Val set to N, and thus the result is N.

Ciao,

Duncan.



More information about the llvm-commits mailing list