From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Murilo Adriano Vasconcelos
Sent: Friday, July 16, 2010 1:34 PM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] About naming of integer log in base 2

Glancing at your code, I note you don’t explicitly check that type T is integer (but perhaps is_signed does that?)

 But I feel that

 *     If `value` is equal to 0, the value returned is undefined

 may be evil in some peoples opinion.

 Of course it is always a vexed question what to do for the corner cases like this.  I am sure that this sort of issue will raise its ugly head for other functions.

That is because I made count_leading_zeros [1] function to be conditionally compiled: if the compiler is GCC, the functions makes the use of GCC's builtin functions [2] for performance. The function used

in count_leading_zeros is __builtin_clz(l and ll) wich is undefined for the value 0.

 

So if the policy requires throwing an exception, then it would mean checking argument is != 0 (but not if the fastest ‘ignore errors’ policy is chosen).  Or return -1 might be another policy.  All are potentially reasonable choices.

 

The initial intent was log2(0) = -1, you can check if your compiler isn't GCC that count_leading_zeros(T(0)) is always sizeof(T) * 8 (all bits are leading zeros). 

But one of the intents of this implementation is performance so, using compiler optimized functions is desired.

 

So the function to be analyzed in this case is count_leading_zeros.

 

I can see the ‘need for speed’, but that doesn’t change the argument for using policies to allow users to control at compile time how exceptional situations are handled.  Policies should have no effect on run time, apart from any checks implied by the chosen policy. 

 

Paul

 

---

Paul A. Bristow

Prizet Farmhouse

Kendal, UK   LA8 8AB

+44 1539 561830, mobile +44 7714330204

pbristow@hetp.u-net.com