News for hackers. Stuff that matters.
Re:Don't use assembly unless you're an expert by sillysaurus3 on Friday April 04, 2014 @04:08AM (#7529958)
I recently wrote some ARM (Thumb-2, for the Cortex-M4) code. It wasn't very difficult, and the code I wrote was much better than what gcc could produce.

Better in terms of code size, speed, or both? (Hm, does code size even matter anymore? Maybe for the CPU cache.)

Re:Don't use assembly unless you're an expert by jwr on Friday April 04, 2014 @05:29AM (#7530210)
Better in terms of both, and code size matters on all architectures. On microcontrollers it's because you really have limited memory, and on larger machines it's because your code cache imprint matters for performance.
Re:Don't use assembly unless you're an expert by panic on Friday April 04, 2014 @04:40AM (#7530053)
Since memory latency improves more slowly than CPU instruction throughput, code size actually matters more and more over time.
Re:Don't use assembly unless you're an expert by userbinator on Friday April 04, 2014 @07:25AM (#7530554)
That's what I thought too when I saw the numbers; OK, he managed to get it 18% faster, but how much bigger was the result? I'm guessing far more than 18% bigger. I've seen cases where replacing a function with a smaller one, but one which microbenchmarked slower, actually lead to the application as a whole performing much faster due to more code fitting in the cache. Ditto for individual instructions; (unfortunately - but Intel seems to be slowly changing this) many of the shortest instructions aren't the fastest when considered in isolation, so compilers tend to avoid them, but the 2-3x difference there is far less than the cost of a single cache miss.

64K of L1 might seem like a huge amount for a function to fit into, but that's 64K shared among every other often-used piece of code. It's for this reason that loop unrolling is no longer recommended too.

The first one is alignment of branches: the assembly code contains no instructions to align basic blocks on particular branches, whereas gcc happily emits these for some basic blocks. I mention this first as it is mere conjecture; I never made an attempt to measure the effects for myself.

And if you were, you would likely not see much on a modern x86; in fact it would probably make things slower, as it may confuse the branch predictor --- instructions are variable-length, so their addresses naturally are not aligned, and that enables e.g. lower-order bits to be used as cache tags.

Re:Don't use assembly unless you're an expert by ithkuil on Friday April 04, 2014 @06:11AM (#7530335)
yes, actually a better word would be code density. A weird encoding like x86 behaves like a compressed code, that gets decoded in the cpu into another instruction set that actually gets executed. This property, along with the backwards compatibility is what kept the x86 instruction set alive.

With the move to 64-bit, code had to be recompiled anyways; hence there was a good opportunity to reshape the x86 instruction set significantly. Instead, the number of changes were relatively small; the instruction set maintained his flavour (variable length encoding, a non orthogonal operands, ...). I believe there are many reasons for that choice (made by AMD), but code density was an key factor.

Re:Don't use assembly unless you're an expert by raverbashing on Friday April 04, 2014 @07:56AM (#7530672)
Yes, but that's why you have an L1 cache

And code is much smaller than data usually, it's a small code snippet operating on a lot of data, not the other way usually

Re:Don't use assembly unless you're an expert by zokier on Friday April 04, 2014 @04:24AM (#7530003)
> Does code size even matter anymore? Maybe for the CPU cache

Cortex-M4 implies microcontroller. Quickly looking, the cheapest M4 chips have 32KB of flash for program memory.

Re:Don't use assembly unless you're an expert by joezydeco on Friday April 04, 2014 @09:25PM (#7535590)
And little to no instruction caching.