ornerylawn/channel

Cant use 20 slots?

Closed this issue · 5 comments

Hey. I am trying to use Channel.
I did replicate the demo shown on the README file, but when I try to set c(20), I get an assertion error - channel.h:21: assert((n & (n + 1)) == 0);.
What is the value range I can enter?

I just did a short test with PHP's live console, and I saw that a very few amount of numbers match to result int(0). How can I determine the numbers I can use, is there a trick?

php > for($i=0; $i<=100; $i++) var_dump( ($i & ($i + 1)) );
int(0)
int(0)
int(2)
int(0)
int(4)
int(4)
int(6)
int(0)
int(8)
int(8)
int(10)
int(8)
int(12)
int(12)
int(14)
int(0)
int(16)
int(16)
int(18)
int(16)
int(20)
int(20)
int(22)
int(16)
int(24)
int(24)
int(26)
int(24)
int(28)
int(28)
int(30)
int(0)
int(32)
int(32)
int(34)
int(32)
int(36)
int(36)
int(38)
int(32)
int(40)
int(40)
int(42)
int(40)
int(44)
int(44)
int(46)
int(32)
int(48)
int(48)
int(50)
int(48)
int(52)
int(52)
int(54)
int(48)
int(56)
int(56)
int(58)
int(56)
int(60)
int(60)
int(62)
int(0)
int(64)
int(64)
int(66)
int(64)
int(68)
int(68)
int(70)
int(64)
int(72)
int(72)
int(74)
int(72)
int(76)
int(76)
int(78)
int(64)
int(80)
int(80)
int(82)
int(80)
int(84)
int(84)
int(86)
int(80)
int(88)
int(88)
int(90)
int(88)
int(92)
int(92)
int(94)
int(64)
int(96)
int(96)
int(98)
int(96)
int(100)

The size has to be one less than a power of 2. I did this because the underlying array is one more than the number provided, and I wanted to use a fast wrapping technique: index & sizemask. It probably should just round up to the next highest power of 2 but limit the distance between the read and write pointers.

Ohhh, that makes sense…however, I am not math-smart enough to figure out, which numbers are possible. I tried to litterally enter 2^15 and 15^2, but couldn’t really find a way to securely find out any possible number.
Can you give me a simple advice on how to calculate possible numbers - or can you push the suggestion you made into the code? It would actually make things a lot easier x)

Kind regards, Ingwie
Am 13.03.2014 um 22:03 schrieb Ryan L Brown notifications@github.com:

The size has to be one less than a power of 2. I did this because the underlying array is one more than the number provided, and I wanted to use a fast wrapping technique: index & sizemask. It probably should just round up to the next highest power of 2 but limit the distance between the read and write pointers.


Reply to this email directly or view it on GitHub.

This function that I found at SO seems to work for now:

inline int pow2roundup(int x) {
    if (x < 0) return 0;
    --x;
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    return x;
}

The original answer returned x+2 at the end, I removed that.

Am 13.03.2014 um 22:03 schrieb Ryan L Brown notifications@github.com:

The size has to be one less than a power of 2. I did this because the underlying array is one more than the number provided, and I wanted to use a fast wrapping technique: index & sizemask. It probably should just round up to the next highest power of 2 but limit the distance between the read and write pointers.


Reply to this email directly or view it on GitHub.

Okay, it allows any size channel now and I added an example of how to use it. fd83deb