Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-14 Thread via Digitalmars-d-learn
On Saturday, 14 February 2015 at 08:01:27 UTC, Ola Fosheim Grøstad wrote: I think the naming depends on use context, so it is reasonable to land on different names in different domains. Maybe the standard notation should be ffs(x): Oops, I meant fls(x)... I just woke up :-P. It is a bad name a

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-14 Thread via Digitalmars-d-learn
On Friday, 13 February 2015 at 22:39:10 UTC, bearophile wrote: H. S. Teoh: So it could be called ilog2? Perhaps floorIlog2? Isn't ilog2 a different function? Bye, bearophile I think the naming depends on use context, so it is reasonable to land on different names in different domains. Mayb

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread bearophile via Digitalmars-d-learn
H. S. Teoh: So it could be called ilog2? Perhaps floorIlog2? Isn't ilog2 a different function? Bye, bearophile

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Feb 13, 2015 at 07:54:32PM +, via Digitalmars-d-learn wrote: > On Friday, 13 February 2015 at 15:14:44 UTC, H. S. Teoh wrote: > >Isn't it essentially floor(log_2(a)), mathematically speaking? Maybe > >that could be the basis of a better name? > > integer log2: > https://graphics.stanfo

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread via Digitalmars-d-learn
On Friday, 13 February 2015 at 15:14:44 UTC, H. S. Teoh wrote: Isn't it essentially floor(log_2(a)), mathematically speaking? Maybe that could be the basis of a better name? integer log2: https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogFloat

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread bearophile via Digitalmars-d-learn
H. S. Teoh: Maybe that could be the basis of a better name? Right. Bye, bearophile

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Feb 13, 2015 at 12:28:23PM +, bearophile via Digitalmars-d-learn wrote: > Dominikus Dittes Scherkl: > > >I would recommend to use something like this: > > > >/// returns the number of the highest set bit +1 in the given value > >/// or 0 if no bit is set > >size_t bitlen(T)(const(T) a

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread bearophile via Digitalmars-d-learn
Dominikus Dittes Scherkl: I would recommend to use something like this: /// returns the number of the highest set bit +1 in the given value or 0 if no bit is set size_t bitlen(T)(const(T) a) pure @safe @nogc nothrow if(isUnsigned!T) { static if(T.sizeof <= size_t.sizeof) // doesn't work f

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-02-13 Thread via Digitalmars-d-learn
On Monday, 19 January 2015 at 20:54:50 UTC, Steven Schveighoffer wrote: Cool. I would point out that the commented code suggests you should be handling the 0 case, but you are not (when T.min == T.max) Should I do a Phobos PR for this? If so where should I put it?

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-20 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn
On Monday, 19 January 2015 at 21:23:47 UTC, Nordlöw wrote: On Monday, 19 January 2015 at 20:54:50 UTC, Steven Schveighoffer wrote: Cool. I would point out that the commented code suggests you should be handling the 0 case, but you are not (when T.min == T.max) I believe that should trigger a

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread Nordlöw
On Monday, 19 January 2015 at 20:54:50 UTC, Steven Schveighoffer wrote: Cool. I would point out that the commented code suggests you should be handling the 0 case, but you are not (when T.min == T.max) I believe that should trigger a failing static assert with a good error message as it doesn

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/19/15 3:46 PM, "Nordlöw" wrote: On Monday, 19 January 2015 at 13:30:47 UTC, Steven Schveighoffer wrote: http://dlang.org/phobos/core_bitop.html#.bsr It's actually an intrinsic, reduces to an instruction. Mind the requirements for 0. This works for me https://github.com/nordlow/justd/blo

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread Nordlöw
On Monday, 19 January 2015 at 13:30:47 UTC, Steven Schveighoffer wrote: http://dlang.org/phobos/core_bitop.html#.bsr It's actually an intrinsic, reduces to an instruction. Mind the requirements for 0. This works for me https://github.com/nordlow/justd/blob/master/traits_ex.d#L406 :)

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, January 19, 2015 13:37:12 Steven Schveighoffer via Digitalmars-d-learn wrote: > On 1/19/15 12:35 PM, Jonathan M Davis via Digitalmars-d-learn wrote: > > On Monday, January 19, 2015 08:30:47 Steven Schveighoffer via > > Digitalmars-d-learn wrote: > >> http://dlang.org/phobos/core_bitop.

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/19/15 10:49 AM, "Per =?UTF-8?B?Tm9yZGzDtnci?= " wrote: On Monday, 19 January 2015 at 13:30:47 UTC, Steven Schveighoffer wrote: http://dlang.org/phobos/core_bitop.html#.bsr It's actually an intrinsic, reduces to an instruction. Mind the requirements for 0. Nice. Is this intrinsic support

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/19/15 12:35 PM, Jonathan M Davis via Digitalmars-d-learn wrote: On Monday, January 19, 2015 08:30:47 Steven Schveighoffer via Digitalmars-d-learn wrote: http://dlang.org/phobos/core_bitop.html#.bsr It's actually an intrinsic, reduces to an instruction. Mind the requirements for 0. Sadly

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, January 19, 2015 08:30:47 Steven Schveighoffer via Digitalmars-d-learn wrote: > http://dlang.org/phobos/core_bitop.html#.bsr > > It's actually an intrinsic, reduces to an instruction. Mind the > requirements for 0. Sadly, I don't think that it have occurred to me from just reading the

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread via Digitalmars-d-learn
On Monday, 19 January 2015 at 13:30:47 UTC, Steven Schveighoffer wrote: http://dlang.org/phobos/core_bitop.html#.bsr It's actually an intrinsic, reduces to an instruction. Mind the requirements for 0. -Steve Nice. Is this intrinsic supported for all DMD/GCD/LDC supported platforms or do we

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/19/15 7:04 AM, "Per =?UTF-8?B?Tm9yZGzDtnci?= " wrote: As a follow-up to http://forum.dlang.org/thread/fdfwrdtjcawprvvko...@forum.dlang.org#post-qxudiyoygnvvbovhjfgt:40forum.dlang.org I'm looking for a function that figures out the number of bits that are needed to represent a zero-based i

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread ketmar via Digitalmars-d-learn
On Mon, 19 Jan 2015 12:04:38 + via Digitalmars-d-learn wrote: > As a follow-up to > > http://forum.dlang.org/thread/fdfwrdtjcawprvvko...@forum.dlang.org#post-qxudiyoygnvvbovhjfgt:40forum.dlang.org > > I'm looking for a function that figures out the number of bits > that are needed to repre

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread bearophile via Digitalmars-d-learn
Per Nordlöw: I'm looking for a function that figures out the number of bits that are needed to represent a zero-based integer: // http://stackoverflow.com/questions/3272424/compute-fast-log-base-2-ceiling uint ceilLog2(ulong x) pure nothrow @safe @nogc in { assert(x > 0); } body {

Re: Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, January 19, 2015 12:04:38 via Digitalmars-d-learn wrote: > As a follow-up to > > http://forum.dlang.org/thread/fdfwrdtjcawprvvko...@forum.dlang.org#post-qxudiyoygnvvbovhjfgt:40forum.dlang.org > > I'm looking for a function that figures out the number of bits > that are needed to represen

Number of Bits Needed to Represent a Zero-Offset Integer

2015-01-19 Thread via Digitalmars-d-learn
As a follow-up to http://forum.dlang.org/thread/fdfwrdtjcawprvvko...@forum.dlang.org#post-qxudiyoygnvvbovhjfgt:40forum.dlang.org I'm looking for a function that figures out the number of bits that are needed to represent a zero-based integer: value-set => bits = 0,1 => 1 (*) 0