CartwrightLab / dawg

Simulating Sequence Evolution
GNU General Public License v2.0
11 stars 3 forks source link

Different power of two algorithm #54

Closed zmertens closed 6 years ago

zmertens commented 6 years ago

https://github.com/reedacartwright/dawg/blob/9c44bdc8cb1ebbfca4cddf4976841d734cf09288/src/include/dawg/utils/aliastable.h#L125-L131

I came across this algorithm here, and it is non-iterative and appears to work very well for 32-bit numbers.

inline int nextPowerOfTwo(int x)
{
    --x;
    x |= x >> 1; // handle 2 bit numbers
    x |= x >> 2; // handle 4 bit numbers
    x |= x >> 4; // handle 8 bit numbers
    x |= x >> 8; // handle 16 bit numbers
    x |= x >> 16; // handle 32 bit numbers
    x++;
    return x;
}

I was wondering what you might think Reed and if it would be worth profiling it?

reedacartwright commented 6 years ago

I am aware of the non-branching way to calculate the next power of two; however, it is unclear if this section of code needs to be optimized.

The template in alias_table also does more than rounding up.

zmertens commented 6 years ago

OK, I didn't think it'd be that much of a change anyway. I'm just trying to think of ways to improve the code.