What the fuck is this shit

What the fuck is this shit

Attached: duffs-device.png (525x475, 36K)

Other urls found in this thread:

en.wikipedia.org/wiki/Duff's_device
twitter.com/SFWRedditImages

Something a pajeet like you will never understand

It's cool but I'm not sure if it would improve performance with newer compilers and CPUs. Has anyone here checked?

It's manual loop unrolling. Try and go through the steps to understand how it works.
Mostly useless to do nowadays, just trust the compiler.

The hack C-tard had to use in the 80s. Nowadays it would be slower than naive implementation thanks to CPU and compiler being smart.

is every other case statement in that do-while? it looks like only case 0: would actually work.

Division and module are extreme expensive operations over 500 times increment or sum operation.

is it good style though?

Something that used to set good programmers apart from pajeets. Thankfully for you, compilers these days are optimized to do this shit on their own, and CPUs are so good you don't even need to worry anyway. Wasn't always like that, back then your ass wouldn't get a job.

do-while can be unrolled into a do-while

Absolutely no.

They are not much more expensive, and modulo 8 is just a binary and.

would use templates to unroll this in compile time

Wat

Antiquated optimizations, more likely to make it run slower now.

don't you see the loop inside case

Not easy, I know, but it can be done. I'm speaking from experience.

loop unrolling, you pajeet

Duff's device.
It (ab)uses the fact that switch "falls through" when there is no break statement.

Clever but this kind of stuff is best left to compilers nowadays.

>What the fuck is this shit
premature optimization

there is no break. the cases will fall into eachother

carmacks fast inverse square is optimisation, not this code copy garbade, imagine making a typo in this shit

It was not premature. This was done in the 80s by a guy working for LucasFilm
en.wikipedia.org/wiki/Duff's_device

Computers were slow and compilers were dumb.

oh yeah the case points not having to be in same scope is where I'm reminded that I don't know C edge-cases so deeply
>register keyword
either archaic or embedded
>duffs device
archaic

good style is when it's possible to understand what it does

It's not Carmack's, and loop unrolling is an (obsolete) optimization technique.

>Here we take a simple do-while loop and unroll it into a far less readable do-while loop with a ton of extra shit around it.

isn't loop unrolling done by the compiler anyway (at least with O3)?

*to = *(from+count-1). wala!

Usually not, since it increases code size (which can end up slowing down the program) and modern processors tend to be good enough at branch prediction that there's minimal slowdown in the first place. Most modern compilers will auto-vectorize loops at higher optimization levels though (if possible).

Have you looked at the output of modern compilers?

Bullshit.

the amount of auties in this thread pretending like they know how a compiler works :^)

What exactly?

You don't do manual loop unrolling because you want the code to look pretty

"hold up, this can be unrolled into unreadable garbage, so we should definitely do it"

Jaguar
>throughput of 32b ADD is 0.5
>throughput of 32b IDIV is 12-27 -> 24-54x slower, 500 > 54
Ryzen
>throughput of 32b ADD is 0.25
>throughput of 32b IDIV is 14-30 -> 56-150x slower, 500 > 150
Core 2
>throughput of 32b ADD is 0.33
>throughput of 32b IDIV is 14-23 -> 42-70x slower, 500 > 70
Haswell
>throughput of 32b ADD is 0.25
>throughput of 32b IDIV is 8-11 -> 32-44x slower, 500 > 44
On average, you're an order of magnitude off with the slowdown. Not to mention that, as already said, modulo 8 is just AND reg, 7, which has the same throughput as ADD.

are you from the 90s? nowadays any compiler will unroll loops for you

Thanks for the link, senpai. Makes sense now