Boost logo

Boost Users :

Subject: Re: [Boost-users] About naming of integer log in base 2
From: Paul A. Bristow (pbristow_at_[hidden])
Date: 2010-07-16 09:25:23


From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Murilo Adriano
Vasconcelos
Sent: Friday, July 16, 2010 1:34 PM
To: boost-users_at_[hidden]
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_at_[hidden]
 
 
 



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net