15 ms·
Could you explain how one would get a SIMD monster for a factorial function?
by tom_mellior 6y ago
Could you explain how one would get a SIMD monster for a factorial function?
- nwallin 6y agohttps://godbolt.org/z/61xGW7 https://godbolt.org/z/61xGW7 Basically, the compiler isn't smart enough to realize that the loop in the factorial function couldn't possibly execute more than a few iterations before returning. Basically, it tries to do this: int a = 1; int b = 2; int c = 3; int d = 4; for (int i = 5; i < x; i += 4) { a *= i; b *= i + 1; c *= i + 2; d *= i + 3; } // handwave a bunch of crap about edge cases return a * b * c * d;
- mhh__ 6y agonwallin has provided an example. I think I've lost the results now, but when benchmarked it's basically neck and neck between size and speed but similar speed for less code is almost always a good thing in this case.