stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Sebor <se...@roguewave.com>
Subject Re: [PATCH] bitset.cc (STDCXX-297)
Date Tue, 10 Oct 2006 18:29:39 GMT
Farid Zaripov wrote:
>>-----Original Message-----
>>From: Martin Sebor [mailto:sebor@roguewave.com] 
>>Sent: Monday, October 09, 2006 10:55 PM
>>To: stdcxx-dev@incubator.apache.org
>>Subject: Re: [PATCH] bitset.cc (STDCXX-297)
>>
>>
>>>the bitset). Also, I think the code could be simplified as
>>>follows:
>>>
>>>  nbytes =   (((maxbits | (sizeof *bits * CHAR_BIT - 1)) >> 5)
>>>           + (0 != (maxbits & (sizeof *bits * CHAR_BIT - 1)))) << 2;
>>
>>With the magic 5 and 2 replaced by expressions parametrized 
>>on sizeof *bits, of course.
> 
>   How log2 can be calculated at compile time?
> 
>   5 is log2(sizeof (*bits) * CHAR_BIT)
>   2 is log2(sizeof (*bits))

I suppose you could use some metaprogramming tricks but I wouldn't
recommend it for portability ;-)

   template <unsigned N>
   struct log2 {
       static const unsigned long value =
           1 < N ? 1 + log2<(N >> 1)>::value : 0;
   };

   template <>
   struct log2<0> { static const unsigned long value = 0; };

I would just hardcode it based on the size of the type. Something
simple like this (the same code is at the top of bitset.cpp) will
work:

   enum {
   #if 4 == _RWSTD_ULONG_SIZE
       log2_long_size = 2,
       log2_long_bits = 5
   #lif 8 == _RWSTD_ULONG_SIZE
       log2_long_size = 3,
       log2_long_bits = 6
   #else
       log2_long_size = 4,
       log2_long_bits = 7
   #endif
   };

Martin

Mime
View raw message