recursive template at ctfe
bioinfornatics
bioinfornatics at fedoraproject.org
Tue Nov 26 13:31:55 PST 2013
On Tuesday, 26 November 2013 at 21:18:29 UTC, Shammah Chancellor
wrote:
> On 2013-11-26 21:00:56 +0000, bioinfornatics said:
>
>> On Tuesday, 26 November 2013 at 20:50:13 UTC, bioinfornatics
>> wrote:
>>> On Tuesday, 26 November 2013 at 20:29:00 UTC, bearophile
>>> wrote:
>>>> bioinfornatics:
>>>>
>>>>> I wrote some template to compute at compile time how many
>>>>> bits is need for a number x.
>>>>> http://www.dpaste.dzfl.pl/99a842fd
>>>>>
>>>>> That works for small number but after i got an error about
>>>>> limit recursion
>>>>
>>>> Instead of template recursion, have you tried regular code
>>>> run at compile time?
>>>>
>>>> http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
>>>>
>>>> Bye,
>>>> bearophile
>>>
>>> Thanks i will take a look
>>
>> this one seem to be interesting
>> http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
>>
>> is like i do but use a table than me i used a recursive way. I
>> go to try it :-)
>
> I think you just have a bug in your template code not
> terminating. At most you should execute 4 times. I think
> there's a problem with casting down from byte to bool.
>
> However, not user if LesserType is what you're after, but this
> works:
>
> template log2(ulong x)
> {
> static if( x >> 8 )
> {
> enum log2 = log2!(x >> 8) + 8;
> }
> else static if( x >> 1)
> {
> enum log2 = log2!(x>>1) + 1;
> } else
> {
> enum log2 = 1;
> }
> }
Thanks your way is much easier to write, works fine :-)
More information about the Digitalmars-d-learn
mailing list