On Fri, Jul 23, 2010 at 2:48 PM,
address@hidden <address@hidden> wrote:
On Fri, Jul 23, 2010 at 12:34 PM, Christian Franke
<address@hidden> wrote:
richardvoigt wrote:
might I suggest:
unsigned long patternl = pattern8;
patternl |= patternl << 8;
patternl |= patternl << 16;
patternl |= patternl << 32;
patternl |= patternl << 64;
O(lg N) instead of O(N), no loop, no branches, and the compiler should be smart enough to optimize away the last two lines on systems with narrower long.
The latter is unfortunately not the case. At least gcc 4.5.0 prints a warning but still produces code.
Ok then, a compile-time conditional should fix that.
Thanks to Seth for pointing out my obvious bug.
unsigned long patternl = pattern8;
if (1 < sizeof patternl) patternl |= patternl << 8;
if (2 < sizeof patternl) patternl |= patternl << 16;
if (4 < sizeof patternl) patternl |= patternl << 32;
if (8 < sizeof patternl) patternl |= patternl << 64;
I'm pretty confident in gcc's ability to optimize this version. I hope.