Boost logo

Boost Users :

Subject: Re: [Boost-users] About naming of integer log in base 2
From: Murilo Adriano Vasconcelos (muriloufg_at_[hidden])
Date: 2010-07-16 08:34:14


Hi, sorry by the delay and thanks for the reply,

2010/7/15 Paul A. Bristow <pbristow_at_[hidden]>

>
>
>
>
> *From:* boost-users-bounces_at_[hidden] [mailto:
> boost-users-bounces_at_[hidden]] *On Behalf Of *Murilo Adriano
> Vasconcelos
> *Sent:* Wednesday, July 14, 2010 9:04 PM
> *To:* boost-users_at_[hidden]
> *Subject:* [Boost-users] About naming of integer log in base 2
>
>
>
> Hi all,
>
>
>
> I implemented a function [0] wich computes the logarithm in base two of
> integers and want to know what naming is most appropriated:
>
>
>
> ilog2
>
> or simply
>
> log2
>
>
>
> [0] -
> http://svn.boost.org/svn/boost/sandbox/SOC/2010/bits_and_ints/boost/integer/ilog2.hpp
>
>
>
> My instinct is for log2 so it might be specialised for floating point too.
>
>
>
> 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.

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.

[1] -
http://svn.boost.org/svn/boost/sandbox/SOC/2010/bits_and_ints/boost/integer/count_leading_zeros.hpp
[2] - http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Other-Builtins.html

>
>
> You might like to consider making problem cases ‘policy controlled’.
>
>
>
> In the Boost.Math package John Maddock went to the trouble of providing a
> way of letting users controlling what happens for cases like this.
>
>
>
> (He retrofitted policies, so you don’t have to do it immediately – but
> doing it will focus your mind on the possible corner cases).
>
>
>
> If you read the math docs, perhaps the pdf at
>
>
>
> http://boost.cowic.de/rc/pdf/math.pdf
>
>
>
> or the download from source,
>
>
>
> and see the section on Policies you will see what it being suggested, and
> some examples.
>
>
>
> You might return zero, or ? or throw an exception?
>
>
>
> You can consult and see if there is a consensus on the default policy.
>
Thanks for the link.
I have to check this so I can discuss about.

>
>
> At least it may reduce the discussion (Boosters are really good at that ;-)
> on what **should** happen, if you can say “You **can** make it do what you
> want.”
>
>
>
> Paul
>
>
>
>
>
> ---
>
> Paul A. Bristow
>
> Prizet Farmhouse
>
> Kendal, UK LA8 8AB
>
> +44 1539 561830, mobile +44 7714330204
>
> pbristow_at_[hidden]
>

Best,

-- 
Murilo Adriano Vasconcelos
http://murilo.wordpress.com


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