News for hackers. Stuff that matters.
Re:Why is this C++ code faster than my hand-written assembly... by mmphosis on Tuesday August 22, 2017 @01:08PM (#15074358)
As someone who has used assembly in production code, I am much more than merely curious. I would like to assemble / generate microcode directly. I would like to know exactly what the CPUs are doing, and not rely on voodoo.
Re:Why is this C++ code faster than my hand-written assembly... by mikebenfield on Tuesday August 22, 2017 @02:07PM (#15074938)
I have the same instinct, but I think in a few years we'd be in the same position, where what we write doesn't directly correspond to the architecture of the CPU. The CPU people are always going to work to make existing code faster, and since it's impossible for many generations of CPUs to "be just like the last generation, only faster", that's inevitably going to involve some kind of translation layer between your code and the CPU's "real architecture."
Re:Why is this C++ code faster than my hand-written assembly... by vram22 on Tuesday August 22, 2017 @04:58PM (#15076333)
Won't the translation layer reduce some of that extra speed? And if so, why is it done? so that people can keep programming to the same instruction set, even though the underlying (micro)code and hardware architecture has changed?
Re:Why is this C++ code faster than my hand-written assembly... by mikebenfield on Tuesday August 22, 2017 @06:31PM (#15076936)
Here's my informal and possibly screwy understanding of why translating to microcode doesn't really slow things down: When you've got a bunch of tasks to do, breaking the task-execution process into a pipeline of steps may potentially increase the latency (how long does it take to do a single task), because, well, you've now got to go through several steps do do that task. But (assuming your pipeline is designed well), it will also increase the throughput (how many tasks can you do in a fixed amount of time).

In software, it's likely that latency is not going to be a problem as long as you can get more throughput (you probably don't care if the time between when the program hits your opcode and the CPU actually executes your instruction is 1 nanosecond later than it conceivably could be as long as the number of instructions executed per millisecond is greater).

In any case, yes, you need to expose the same machine code interface, not just so people can write new software using the same code, but so that old code continues to work. It would be a nightmare if every few years there were incompatible changes to the instruction set.

Re:Why is this C++ code faster than my hand-written assembly... by vram22 on Wednesday August 23, 2017 @07:25AM (#15080498)
Yes, could be, interesting reason.

And your last paragraph makes sense for why the same interface.

Re:Why is this C++ code faster than my hand-written assembly... by pcordes on Wednesday August 23, 2017 @01:34PM (#15083429)
You're basically asking why Intel doesn't expose their internal uops as an ISA that you can program in. See my answer on a SO question asking exactly that. https://stackoverflow.com/a/32866797/224132

The other answers on that question make some good points, too.

Most instructions decode to a single internal uop already, so x86 machine code is just a compact way of programming in uops.

Things like variable-count shifts are multi-uop because of x86's legacy CISC flag-handling, but BMI2 fixes that by giving you no-flags shifts that do decode to a single uop.

You're usually not missing out on much.

Also, there's plenty of voodoo besides just translation to uops. Programming in uops directly wouldn't help you figure out why there seems to be some kind of limit on register reads per clock (http://agner.org/optimize/blog/read.php?i=415#852) for example, since the uops you'd program with would still have register renaming done to them as they're issued into the out-of-order core.

Re:Why is this C++ code faster than my hand-written assembly... by laythea on Tuesday August 22, 2017 @03:06PM (#15075439)
In that case, all I can advise, is to never get involved with GPU programming :)