Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Everybody mentions this, and what's great is that it is a pretty natural solution to a lot of problems. I remember coming up with a version of it while writing an optimized prime-sieve[1], and was surprised when I later learned that it was some named technique.

In addition to just the basic loop-unrolling (which I'm pretty sure you usually don't need to do by hand with modern compilers), it works really well when you need to jump into the middle of a pattern. Like if you're sieving primes in a wheel-factorized array.

[1] https://github.com/patricksjackson/primes-cpp/blob/master/pr...

  // Here we're only checking possible primes after wheel-factorization
  switch ((p.second/num)%30) do {
    case  1: target->set(n); n+=num*6;
    case  7: target->set(n); n+=num*4;
    case 11: target->set(n); n+=num*2;
    case 13: target->set(n); n+=num*4;
    case 17: target->set(n); n+=num*2;
    case 19: target->set(n); n+=num*4;
    case 23: target->set(n); n+=num*6;
    case 29: target->set(n); n+=num*2;
  } while(n <= high - range.first);


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: