Re: Checking if an Integer is an Exact Binary Power

2016-04-26 Thread Timon Gehr via Digitalmars-d
On 26.04.2016 17:27, Xinok wrote: On Monday, 25 April 2016 at 15:35:14 UTC, Dominikus Dittes Scherkl wrote: On Monday, 25 April 2016 at 15:27:02 UTC, Xinok wrote: Brute force. http://dpaste.dzfl.pl/882d7cdc5f74 Yeah. And your test spares the failed case int.min (0x8000), because in this c

Re: Checking if an Integer is an Exact Binary Power

2016-04-26 Thread Era Scarecrow via Digitalmars-d
On Tuesday, 26 April 2016 at 18:04:53 UTC, deadalnix wrote: You got to explain me how you end up with a negative number by multiplying 2 by 2 a bunch of time. You gotta scrawl your numbers down badly so one of the 2's looks like a minus sign. Or go so large that the universe caves in on itsel

Re: Checking if an Integer is an Exact Binary Power

2016-04-26 Thread deadalnix via Digitalmars-d
On Tuesday, 26 April 2016 at 08:12:02 UTC, Dominikus Dittes Scherkl wrote: On Monday, 25 April 2016 at 22:42:38 UTC, deadalnix wrote: x & -x is the smallest power of 2 that divides x. Basically, if x = 1000 , x & -x = 1000 . This is easy to proves considering -x = ~x + 1 . Now, x >> 1

Re: Checking if an Integer is an Exact Binary Power

2016-04-26 Thread Temtaime via Digitalmars-d
On Tuesday, 26 April 2016 at 16:53:00 UTC, tsbockman wrote: On Tuesday, 26 April 2016 at 14:37:46 UTC, Marco Leise wrote: Can we just use __traits(hasTargetFeature, "popcnt") already and get rid of _popcnt? No, because DMD does not currently support setting SSE4 as the minimum target. Thus, `

Re: Checking if an Integer is an Exact Binary Power

2016-04-26 Thread tsbockman via Digitalmars-d
On Tuesday, 26 April 2016 at 14:37:46 UTC, Marco Leise wrote: Can we just use __traits(hasTargetFeature, "popcnt") already and get rid of _popcnt? No, because DMD does not currently support setting SSE4 as the minimum target. Thus, `__traits(hasTargetFeature, "popcnt")` must always return `fa

Re: Checking if an Integer is an Exact Binary Power

2016-04-26 Thread Xinok via Digitalmars-d
On Monday, 25 April 2016 at 15:35:14 UTC, Dominikus Dittes Scherkl wrote: On Monday, 25 April 2016 at 15:27:02 UTC, Xinok wrote: Brute force. http://dpaste.dzfl.pl/882d7cdc5f74 Yeah. And your test spares the failed case int.min (0x8000), because in this case x & -x is negative, but of caus

Re: Checking if an Integer is an Exact Binary Power

2016-04-26 Thread Marco Leise via Digitalmars-d
Am Mon, 25 Apr 2016 17:04:43 + schrieb tsbockman : > On Monday, 25 April 2016 at 16:48:14 UTC, Lass Safin wrote: > > Example; > > > > import core.bitop; > > import core.cpuid; > > > > int count; > > if(hasPopcnt) > > count = _popcnt; // Uses x86-instruction "popcnt". > > else > > count

Re: Checking if an Integer is an Exact Binary Power

2016-04-26 Thread Matthias Bentrup via Digitalmars-d
On Tuesday, 26 April 2016 at 08:12:02 UTC, Dominikus Dittes Scherkl wrote: On Monday, 25 April 2016 at 22:42:38 UTC, deadalnix wrote: x & -x is the smallest power of 2 that divides x. Basically, if x = 1000 , x & -x = 1000 . This is easy to proves considering -x = ~x + 1 . Now, x >> 1

Re: Checking if an Integer is an Exact Binary Power

2016-04-26 Thread Dominikus Dittes Scherkl via Digitalmars-d
On Monday, 25 April 2016 at 22:42:38 UTC, deadalnix wrote: x & -x is the smallest power of 2 that divides x. Basically, if x = 1000 , x & -x = 1000 . This is easy to proves considering -x = ~x + 1 . Now, x >> 1 will be of the form 0100 . If one of these digit is one, then (x >> 1)

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Solomon E via Digitalmars-d
On Monday, 25 April 2016 at 22:15:31 UTC, Solomon E wrote: ... I was hoping to suggest that if isPow2F isn't wrong, it might be used, because it has been proved and checked to be correct in this thread. ... On second thought, no one's going to understand why the code (x & -x) > (x >>> 1)

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread deadalnix via Digitalmars-d
On Monday, 25 April 2016 at 12:56:27 UTC, Andrei Alexandrescu wrote: On 4/25/16 6:42 AM, Solomon E wrote: On Monday, 25 April 2016 at 05:35:12 UTC, Andrei Alexandrescu wrote: With gdc https://godbolt.org/g/jcU4np isPow2B is the winner (shortest code, simplest instructions). -- Andrei I gen

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Solomon E via Digitalmars-d
On Monday, 25 April 2016 at 19:37:45 UTC, Andrei Alexandrescu wrote: On 04/25/2016 03:21 PM, Solomon E wrote: I'm more concerned with getting the application logic right when I program than optimizing for speed or bytes, until something is noticeably pausing or taking extra megabytes. This i

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Andrei Alexandrescu via Digitalmars-d
On 04/25/2016 03:21 PM, Solomon E wrote: I'm more concerned with getting the application logic right when I program than optimizing for speed or bytes, until something is noticeably pausing or taking extra megabytes. This is a luxury not available to library writers. bool isPow2D(T)(T x) {

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Andrei Alexandrescu via Digitalmars-d
On 04/25/2016 03:21 PM, Solomon E wrote: There are other parts in isPow2F where I'm not sure exactly what the bits are doing, such as how the compiler makes the result negative I suggest you simply consider x unsigned. There is no value to the discussion brought by negative numbers, and implem

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Solomon E via Digitalmars-d
On Monday, 25 April 2016 at 16:53:20 UTC, Solomon E wrote: ... (x >>> 1) is equal to half of x or one less than half of x, or a positive number if x is negative. ... Obviously wrong explanation, sorry. (x >>> 1) is equal to half of x or half of one less than x, or a positive number if x is

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread tsbockman via Digitalmars-d
On Monday, 25 April 2016 at 16:48:14 UTC, Lass Safin wrote: Example; import core.bitop; import core.cpuid; int count; if(hasPopcnt) count = _popcnt; // Uses x86-instruction "popcnt". else count = popcnt; // Phobos's software implementation. // Do stuff with count That is not right (s

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Solomon E via Digitalmars-d
On Monday, 25 April 2016 at 15:27:02 UTC, Xinok wrote: On Monday, 25 April 2016 at 12:56:27 UTC, Andrei Alexandrescu wrote: On 4/25/16 6:42 AM, Solomon E wrote: ... I generalized function isPow2B to work for negative numbers in case of a signed type, while keeping the assembly the same or bett

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Iain Buclaw via Digitalmars-d
On 25 April 2016 at 18:48, Lass Safin via Digitalmars-d wrote: > On Saturday, 23 April 2016 at 21:04:52 UTC, Nordlöw wrote: >> >> On Saturday, 23 April 2016 at 20:42:25 UTC, Lass Safin wrote: >>> >>> CPUID: https://en.wikipedia.org/wiki/CPUID. >>> You can check for the presence of a lot of instruc

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Solomon E via Digitalmars-d
On Monday, 25 April 2016 at 12:56:27 UTC, Andrei Alexandrescu wrote: ... That's smaller and faster, thanks. But how do you show it is still correct? -- Andrei First I wrote the following variants of isPow2A. It's more obvious that they're correct, but they didn't compile to fewer instructi

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Lass Safin via Digitalmars-d
On Saturday, 23 April 2016 at 21:04:52 UTC, Nordlöw wrote: On Saturday, 23 April 2016 at 20:42:25 UTC, Lass Safin wrote: CPUID: https://en.wikipedia.org/wiki/CPUID. You can check for the presence of a lot of instructions with this instruction. However this will only work on x86 and only run-ti

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Dominikus Dittes Scherkl via Digitalmars-d
On Monday, 25 April 2016 at 15:27:02 UTC, Xinok wrote: On Monday, 25 April 2016 at 12:56:27 UTC, Andrei Alexandrescu wrote: On 4/25/16 6:42 AM, Solomon E wrote: On Monday, 25 April 2016 at 05:35:12 UTC, Andrei Alexandrescu wrote: With gdc https://godbolt.org/g/jcU4np isPow2B is the winner (s

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Shachar Shemesh via Digitalmars-d
On 23/04/16 16:04, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. I guess https://dlang.org/phobos/std_math.html#.truncPow2 or https://dlang.org/phobos/std_math.html#.nextPow2 could be reused, but I bet there

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Xinok via Digitalmars-d
On Monday, 25 April 2016 at 12:56:27 UTC, Andrei Alexandrescu wrote: On 4/25/16 6:42 AM, Solomon E wrote: On Monday, 25 April 2016 at 05:35:12 UTC, Andrei Alexandrescu wrote: With gdc https://godbolt.org/g/jcU4np isPow2B is the winner (shortest code, simplest instructions). -- Andrei I gen

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Andrei Alexandrescu via Digitalmars-d
On 4/25/16 6:42 AM, Solomon E wrote: On Monday, 25 April 2016 at 05:35:12 UTC, Andrei Alexandrescu wrote: With gdc https://godbolt.org/g/jcU4np isPow2B is the winner (shortest code, simplest instructions). -- Andrei I generalized function isPow2B to work for negative numbers in case of a sign

Re: Checking if an Integer is an Exact Binary Power

2016-04-25 Thread Solomon E via Digitalmars-d
On Monday, 25 April 2016 at 05:35:12 UTC, Andrei Alexandrescu wrote: With gdc https://godbolt.org/g/jcU4np isPow2B is the winner (shortest code, simplest instructions). -- Andrei I generalized function isPow2B to work for negative numbers in case of a signed type, while keeping the assembly

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread Andrei Alexandrescu via Digitalmars-d
On 4/24/16 7:00 PM, Temtaime wrote: On Sunday, 24 April 2016 at 21:45:32 UTC, Walter Bright wrote: On 4/24/2016 8:33 AM, Andrei Alexandrescu wrote: On 04/24/2016 02:57 AM, deadalnix wrote: On Saturday, 23 April 2016 at 15:29:08 UTC, Andrei Alexandrescu wrote: Yah, that's the canonical. I forg

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread Andrei Alexandrescu via Digitalmars-d
On 4/24/16 9:17 PM, Xinok wrote: I modified David's solution a bit to (hopefully) eliminate the branch: bool powerOf2(uint x){ return !x ^ !(x & (x - 1)); } <_D4test8powerOf2FkZb>: 0: 50 push %rax 1: 53 push

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread Xinok via Digitalmars-d
On Sunday, 24 April 2016 at 23:17:53 UTC, David Nadlinger wrote: On Sunday, 24 April 2016 at 23:00:56 UTC, Temtaime wrote: Please no cmp. Just bool isPowerOf2(uint x) { return x && !(x & (x - 1)); } You do realise that this will (typically) emit a branch? — David I compiled using dmd -O an

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread Xinok via Digitalmars-d
On Monday, 25 April 2016 at 01:17:48 UTC, Xinok wrote: ... Sorry, didn't mean to say David's solution. Too many edits. >_<

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread David Nadlinger via Digitalmars-d
On Sunday, 24 April 2016 at 23:00:56 UTC, Temtaime wrote: Please no cmp. Just bool isPowerOf2(uint x) { return x && !(x & (x - 1)); } You do realise that this will (typically) emit a branch? — David

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread Temtaime via Digitalmars-d
On Sunday, 24 April 2016 at 21:45:32 UTC, Walter Bright wrote: On 4/24/2016 8:33 AM, Andrei Alexandrescu wrote: On 04/24/2016 02:57 AM, deadalnix wrote: On Saturday, 23 April 2016 at 15:29:08 UTC, Andrei Alexandrescu wrote: Yah, that's the canonical. I forgot why I chose (x & -x) > (x - 1) ov

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread Walter Bright via Digitalmars-d
On 4/24/2016 8:33 AM, Andrei Alexandrescu wrote: On 04/24/2016 02:57 AM, deadalnix wrote: On Saturday, 23 April 2016 at 15:29:08 UTC, Andrei Alexandrescu wrote: Yah, that's the canonical. I forgot why I chose (x & -x) > (x - 1) over it. I'm not sure why do you test against x - 1 when you coul

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread Andrei Alexandrescu via Digitalmars-d
On 04/24/2016 02:57 AM, deadalnix wrote: On Saturday, 23 April 2016 at 15:29:08 UTC, Andrei Alexandrescu wrote: Yah, that's the canonical. I forgot why I chose (x & -x) > (x - 1) over it. I'm not sure why do you test against x - 1 when you could test for equality. Not only it looks like it is

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread Iain Buclaw via Digitalmars-d
On 23 April 2016 at 15:56, Andrei Alexandrescu via Digitalmars-d wrote: > On 4/23/16 9:54 AM, Andrei Alexandrescu wrote: >> >> On 4/23/16 9:06 AM, Vladimir Panteleev wrote: >>> >>> On Saturday, 23 April 2016 at 13:04:00 UTC, Nordlöw wrote: Wanted: CT-trait and run-time predicate for chec

Re: Checking if an Integer is an Exact Binary Power

2016-04-24 Thread deadalnix via Digitalmars-d
On Saturday, 23 April 2016 at 15:29:08 UTC, Andrei Alexandrescu wrote: Yah, that's the canonical. I forgot why I chose (x & -x) > (x - 1) over it. I'm not sure why do you test against x - 1 when you could test for equality. Not only it looks like it is going to require an extra computation (x

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread deadalnix via Digitalmars-d
On Saturday, 23 April 2016 at 13:04:00 UTC, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. I guess https://dlang.org/phobos/std_math.html#.truncPow2 or https://dlang.org/phobos/std_math.html#.nextPow2 could

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Marco Leise via Digitalmars-d
Am Sat, 23 Apr 2016 20:34:52 + schrieb Nordlöw : > On Saturday, 23 April 2016 at 17:28:21 UTC, Andrei Alexandrescu > wrote: > >> Yah, that's the canonical. I forgot why I chose (x & -x) > (x > >> - 1) over > >> it. -- Andrei > > So is there a way to check if popcnt builtin is available on

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread tsbockman via Digitalmars-d
On Saturday, 23 April 2016 at 21:04:52 UTC, Nordlöw wrote: On Saturday, 23 April 2016 at 20:42:25 UTC, Lass Safin wrote: CPUID: https://en.wikipedia.org/wiki/CPUID. You can check for the presence of a lot of instructions with this instruction. However this will only work on x86 and only run-ti

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread David Nadlinger via Digitalmars-d
On Saturday, 23 April 2016 at 20:34:52 UTC, Nordlöw wrote: So is there a way to check if popcnt builtin is available on current platform? As Andrei pointed out, it's not only popcnt being available, it's also it being fast. A function implementing the classic hand-written version compiles dow

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread safety0ff via Digitalmars-d
On Saturday, 23 April 2016 at 21:04:52 UTC, Nordlöw wrote: On Saturday, 23 April 2016 at 20:42:25 UTC, Lass Safin wrote: CPUID: https://en.wikipedia.org/wiki/CPUID. You can check for the presence of a lot of instructions with this instruction. However this will only work on x86 and only run-ti

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Nordlöw via Digitalmars-d
On Saturday, 23 April 2016 at 20:42:25 UTC, Lass Safin wrote: CPUID: https://en.wikipedia.org/wiki/CPUID. You can check for the presence of a lot of instructions with this instruction. However this will only work on x86 and only run-time. Code you give a complete code example in D, please or

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Lass Safin via Digitalmars-d
On Saturday, 23 April 2016 at 20:34:52 UTC, Nordlöw wrote: On Saturday, 23 April 2016 at 17:28:21 UTC, Andrei Alexandrescu wrote: Yah, that's the canonical. I forgot why I chose (x & -x) > (x - 1) over it. -- Andrei So is there a way to check if popcnt builtin is available on current platfor

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Nordlöw via Digitalmars-d
On Saturday, 23 April 2016 at 17:28:21 UTC, Andrei Alexandrescu wrote: Yah, that's the canonical. I forgot why I chose (x & -x) > (x - 1) over it. -- Andrei So is there a way to check if popcnt builtin is available on current platform?

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Andrei Alexandrescu via Digitalmars-d
On 04/23/2016 11:29 AM, Andrei Alexandrescu wrote: On 4/23/16 10:41 AM, Dmitry Olshansky wrote: On 23-Apr-2016 16:04, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. I guess https://dlang.org/phobos/std_math.ht

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Andrei Alexandrescu via Digitalmars-d
On 4/23/16 10:41 AM, Dmitry Olshansky wrote: On 23-Apr-2016 16:04, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. I guess https://dlang.org/phobos/std_math.html#.truncPow2 or https://dlang.org/phobos/std_math

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Dmitry Olshansky via Digitalmars-d
On 23-Apr-2016 16:04, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. I guess https://dlang.org/phobos/std_math.html#.truncPow2 or https://dlang.org/phobos/std_math.html#.nextPow2 could be reused, but I bet th

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Andrei Alexandrescu via Digitalmars-d
On 4/23/16 9:54 AM, Andrei Alexandrescu wrote: On 4/23/16 9:06 AM, Vladimir Panteleev wrote: On Saturday, 23 April 2016 at 13:04:00 UTC, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. popcnt(x) <= 1 ? Would

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Andrei Alexandrescu via Digitalmars-d
On 4/23/16 9:06 AM, Vladimir Panteleev wrote: On Saturday, 23 April 2016 at 13:04:00 UTC, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. popcnt(x) <= 1 ? Would be interesting to see how it compares to the han

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Andrei Alexandrescu via Digitalmars-d
On 4/23/16 9:04 AM, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. I guess https://dlang.org/phobos/std_math.html#.truncPow2 or https://dlang.org/phobos/std_math.html#.nextPow2 could be reused, but I bet ther

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Nordlöw via Digitalmars-d
On Saturday, 23 April 2016 at 13:06:58 UTC, Vladimir Panteleev wrote: On Saturday, 23 April 2016 at 13:04:00 UTC, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. popcnt(x) <= 1 ? Great. My solution: /** Chec

Re: Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Vladimir Panteleev via Digitalmars-d
On Saturday, 23 April 2016 at 13:04:00 UTC, Nordlöw wrote: Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. popcnt(x) <= 1 ?

Checking if an Integer is an Exact Binary Power

2016-04-23 Thread Nordlöw via Digitalmars-d
Wanted: CT-trait and run-time predicate for checking whether its single integer parameter is an exact power of two. I guess https://dlang.org/phobos/std_math.html#.truncPow2 or https://dlang.org/phobos/std_math.html#.nextPow2 could be reused, but I bet there's a more efficient way of checki