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
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
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
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, `
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
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
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
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
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)
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)
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
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
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)
{
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
On Monday, 25 April 2016 at 01:17:48 UTC, Xinok wrote:
...
Sorry, didn't mean to say David's solution. Too many edits. >_<
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
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
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
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
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
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
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
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
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
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
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
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
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
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?
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
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
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
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
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
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
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
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 ?
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
52 matches
Mail list logo