News for hackers. Stuff that matters.
Re:GodBolt: Enter C, get Assembly by OskarS on Thursday December 15, 2016 @02:58AM (#13183175)
Dude, modern compilers are smart.
Re:GodBolt: Enter C, get Assembly by user51442 on Thursday December 15, 2016 @05:03AM (#13183557)
A simple recursive factorial function:

https://godbolt.org/g/97dUFg

Not sure if that's smart or stupid.

Re:GodBolt: Enter C, get Assembly by GlitchMr on Thursday December 15, 2016 @05:32AM (#13183646)
It does try to vectorize it (doing multiple math operations at once is faster).

(1 * 5 * 9 * 13 * 17 * ...) * (2 * 6 * 10 * 14 * 18 * ...) * (3 * 7 * 11 * 15 * 19 * ...) * (4 * 8 * 12 * 16 * 20 * ...)

(it's not precisely that, but close enough)

It isn't optimal however, optimal code would be pre-computed results (signed integer overflow is undefined, so n <= 12 is defined)

(if signed integer overflow would be defined to be 2's complement overflow, you can still use a table, as n > 33 gives 0)

Re:GodBolt: Enter C, get Assembly by Tuna-Fish on Thursday December 15, 2016 @05:34AM (#13183653)
Dude, your code got:

1. Turned from recursive form into iterative form 2. Unrolled heavily 3. Autovectorized (!)

The throughput will be substantially more than the simple version.

Re:GodBolt: Enter C, get Assembly by user51442 on Thursday December 15, 2016 @06:19AM (#13183833)
Well, the optimizations are pretty smart to be sure, the stupid thing is that anything over 12! overflows (as GlitchMr points out).

I'd like to know how the recursion to iteration step was done.

Re:GodBolt: Enter C, get Assembly by eutectic on Thursday December 15, 2016 @08:05AM (#13184287)
Presumably there is a pass to turn f(recur(x), y) into recur(f(y, acc), x) and then tail-call optimization can be applied. This works for any associative f.
Re:GodBolt: Enter C, get Assembly by jdcarter on Thursday December 15, 2016 @12:36PM (#13186379)
I took that and check out what Clang does with 32-bit ints:

https://godbolt.org/g/ORsX5h

vs. what it does with 64-bit ints:

https://godbolt.org/g/2wBzn3

Can anybody explain the 32 bit version?

Re:GodBolt: Enter C, get Assembly by CorvusCrypto on Thursday December 15, 2016 @05:59PM (#13189478)
It's just vectorizing calculations so that it's faster than pure iterative calculation. 64 bit version doesn't get this probably because that optimizer isn't SSE2 aware yet (just a guess I actually don't know) and can't do SIMD arithmetic with 2 64 bit floats
Re:GodBolt: Enter C, get Assembly by user2994cb on Thursday December 15, 2016 @05:35PM (#13189263)
Harder to vectorize 64-bit arithmetic?
Re:GodBolt: Enter C, get Assembly by CorvusCrypto on Thursday December 15, 2016 @05:52PM (#13189406)
was going to say this. It probably only does SSE and not SSE2. Therefore vectorization only happens for 32 bit ints.
Re:GodBolt: Enter C, get Assembly by user2994cb on Friday December 16, 2016 @03:00AM (#13191789)
Seems to need -mavx2 to really go to town with 64 bit: https://godbolt.org/g/6EFYeY
Re:GodBolt: Enter C, get Assembly by jdcarter on Friday December 16, 2016 @03:31PM (#13196497)
Thank you both, I appreciate the insight!
Re:GodBolt: Enter C, get Assembly by yellowapple on Friday December 16, 2016 @06:49PM (#13197967)
Interestingly, all the RISC architectures except for 64-bit ARM seem to compile down to something a lot simpler.
Re:GodBolt: Enter C, get Assembly by lisivka on Thursday December 15, 2016 @01:40PM (#13186952)
Use -Os to check. Compiler turned recursive function to loop.
Re:GodBolt: Enter C, get Assembly by topkekz on Thursday December 15, 2016 @08:03AM (#13184279)
Check this one (clang)

https://godbolt.org/g/Brn57Z

Re:GodBolt: Enter C, get Assembly by Veedrac on Thursday December 15, 2016 @08:09AM (#13184314)
That's not smart - that's pattern matching.