8 ms·
ARM and Intel have different performance characteristics: a case study
- palango 8y agoCan someone explain this in a bit more detail? Why does "the computation of the most significant bits of a 64-bit product on an ARM processor requires a separate and expensive instruction"?
- ridiculous_fish 8y agoARM64 has separate instructions for computing the low and high halves of a product, in keeping with its single destination register approach. x86-64 has a single instruction that computes both halves simultaneously, writing to two registers.
- petermcneeley 8y agoIt would have been just simpler for the author to show us the difference in the multiplication performance directly. Here the benchmarks are comparing the completely different hash functions and we have to simply take his word that it is this instruction that is causing the performance discrepancy.
- sligor 8y agoOn a modern out of order uarch that kind of "complex" instruction would by spited in two micro operation, one for each register result. And Agner's tables [1] confirms that the mul 64*64 => 128 is split in two micro-ops on Skylake. So it doesn't give any strong advantage. [1] https://www.agner.org/optimize/instruction_tables.pdf https://www.agner.org/optimize/instruction_tables.pdf
- BeeOnRope 8y agoYes, but the second uop is not expensive like the first in this case. That is, it seems like the the full multiplication is done by the latency-3 op on p1 and the other uop is just needed to move the high half of the result to the destination (indeed, instructions with 2 outputs always need 2 uops due to the way the renamer works). The whole 64x64->128 multiplication still has a latency of only 3, and a throughput of 1 per cycle. So the 64x64->128 multiplication is still quite efficient compared to ARM where two "full strength" multiplications are needed. It is odd though that there is nearly a 20x difference in relative speeds though, I wouldn't expect multiply upper to be that slow.
- dragontamer 8y agoNote: The test seems to have been done on Skylark (aka: Ampere), which is a non-standard ARM core. I can't find any documentation on Skylark's latency / throughput specifications.
- sligor 8y agoI strongly suspect the ARM compiler is not optimized for 128b mults and just calls a generic software function to do the computation like this one: https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/multi3.c https://github.com/llvm-mirror/compiler-rt/blob/master/lib/b...
- mmozeiko 8y agoFor 64-bit ARM there are no function calls when using clang: https://godbolt.org/z/IR2DIj https://godbolt.org/z/IR2DIj
- sligor 8y agoyou're right, and even on old versions of gcc and clang it seems that it is correctly generated as mul + umulh pair
- mmrezaie 8y agoI just wanted to thank you for this website. It is putting end to so many discussions we have about the quality of the code. Extra, it has a support for FORTRAN. Previously we were doing debug and disassembly ourselves.
- deleted 8y ago[deleted]
- dragontamer 8y agoIntel does 64x64 bit multiplies and returns a 128-bit result. ARM does 64x64 bit multiplies and returns just the lower (or upper) 64-bits. Wyhash is built around the 128-bit result. This is fast on Intel, but slower on ARM.
- dmitrygr 8y agoArticle is very light on details, and contains zero citations, and only a single result of a single benchmark the guy ran, with no details of how it was run. It follows up by stating his theory as to why this happens as a fact (again with no citations). Author does not even offer us a clue as to what ARM core is used. The claim is: > The difference is that the computation > of the most significant bits of a > 64-bit product on an ARM processor > requires a separate and expensive > instruction. I see no proof of this anywhere in the ARMv8 spec. You get the lower 64 bits of result using MUL and higher 64 bits using UMULH. Neither of those is that expensive. Looking at [1] we can see that MUL has throughput of 1 and latency of 3, UMULH has 1/4 and 6, but as long as you do not issue another multiply just after your UMULH, this 1/4 throughput is easily hidden, since only the multiplier is busy, the rest of the CPU can go on. So unless your entire loop is under 6 cycles, or you simply have no instructions to schedule that do not need a multiply within the next 3 of UMULH, it shouldn't matter. Given those large constants that need to be loaded, they will each need 4 instrs (mov+movk+movk+movk), there are plenty of instrs to schedule after UMULH. Either OP's compiler messed up, or something entirely different is going on. If, the author was using a weaker in-order core, say Cortex-A55, still more performance is expected than appears demonstrated. There [2] the low part is calculated in 2 or 3 cycles, the high in 4. But comparing an ARM in-order little core to a modern OoO x86 is just not fair. EDIT: Indeed, looking [3] at what gcc produces for this code is sad. For example, why it is bothering synthesizing 0x1b03738712fad5c9 before issuing the first UMULH is unclear, but it IS stupid. EDIT2: on skylake [4] MUL has a latency of 3, so faster than on ARM but not by that much. I'd guess the constant loading on arm using 4 instructions per constant hurts more than UMULH EDIT3: in comments on original site, author said the ARM chip being used is a "Skylark" by "Ampere Computing" [5]. Given that I cannot find any info on that microarchitecture, I cannot say more about why it might be slow. [1] Cortex®-A72 Software Optimization Guide: https://static.docs.arm.com/uan0016/a/cortex_a72_software_optimization_guide_external.pdf https://static.docs.arm.com/uan0016/a/cortex_a72_software_op... [2] Cortex®-A55 Software Optimization Guide: https://static.docs.arm.com/epm128372/20/arm_cortex_a55_software_optimization_guide_v2.pdf https://static.docs.arm.com/epm128372/20/arm_cortex_a55_soft... [3] Godbolt for this code: https://godbolt.org/z/UeOo6C https://godbolt.org/z/UeOo6C [4] Lists of instruction latencies, throughputs and micro operation breakdowns for Intel, AMD and VIA CPUs: https://www.agner.org/optimize/instruction_tables.pdf https://www.agner.org/optimize/instruction_tables.pdf [5] Skylark - Microarchitectures - AppliedMicro: https://en.wikichip.org/wiki/apm/microarchitectures/skylark https://en.wikichip.org/wiki/apm/microarchitectures/skylark
- TheMagicHorsey 8y agoNovice here ... but how do these results give random results? Are the uninitialized memory considered random? Or is there some other source of randomness. It seems like a deterministic function to me if the variables are initialized to zero.
- tntn 8y agohttps://en.m.wikipedia.org/wiki/Pseudorandom_number_generator https://en.m.wikipedia.org/wiki/Pseudorandom_number_generato...
- aidenn0 8y agoThey are deterministic.
- TheMagicHorsey 8y agoWhy wouldn't one use some sort of pseudorandom seed instead of just uninitialized memory? Couldn't one sample a clock, image sensor, thermometer or some other sensor that would have a random value to use as a seed? Seems like a part of memory allocated by the compiler might always be zero.
- wahern 8y agoThere's no uninitialized memory. File-scoped variables are initialized to 0 in C.
- olliej 8y agoI am very confused isn’t mul a 64bit multiply on arm64? Or is this a comparison of a 64 bit processor to a 32bit one?
- glangdale 8y agoCSA: Don't fixate on the code generated by Godbolt in isolation; it's not going to reflect what happens in the benchmark loop. (see my other reply to dimitrgy for more details)
- deepsun 8y agoUhm, isn't the article title obvious? It would be rather surprising if they had the same characteristics.
- UnpossibleJim 8y agoyeah, I feel like this is stating, "A span bridge and an arch bridge perform differently under load and in a strong wind." It seems obvious.
- microcolonel 8y agoSure, but it may not be intuitive to people that one function could be exactly as fast on two very different processors, but another very similar function would be orders of magnitude different.
- tyingq 8y agoThis was pretty common in the RISC heyday. The competition meant lots of jockeying to lead benchmarks from Sparc, Alpha, PA-RISC, Power, etc. This seems to be on the rise again with AMD and ARM being closer to Intel in servers than they were in the recent past.
- klingonopera 8y agoI'm guessing the 128-bit multiplication implementation on the ARM architecture isn't as well done as is on the Intel platform? You might be able to reclaim the performance if you manually implement the multiplication using 64-bit variables instead.
- klodolph 8y agoNo, the compiler is generating good code. If you use a smaller word size you just end up doing more multiplications (e.g. cut your word size in half, do 4x as many multiplications).
- monocasa 8y agoARMv8 NEON has a 64x64->128 multiply.
- profquail 8y agoAye, it’s PMULL, and it’s available as part of the ARM Crypto Extensions. This patch shows that using it can result in a large performance improvement: https://github.com/randombit/botan/issues/842 https://github.com/randombit/botan/issues/842
- floatboth 8y agoHm, why aren't compilers generating that instruction? upd: apparently reasons like: > So I guess for most of the case loading or storing i128, the data will be used by some library functions running on cores instead of NEON, so storing i128 to two GPR64 is more general. https://reviews.llvm.org/D2344 https://reviews.llvm.org/D2344
- dragontamer 8y ago> Hm, why aren't compilers generating that instruction? Thats polynomial multiply. Its (almost) a multiplication in GF2 for elliptical curves. Thats not a "normal" multiply. "PMULL" is basically a bitshift and XOR. Your traditional "MUL" is bitshift and ADD. Its called "polynomial multiply" because bitshift-and-xor has very similar properties to bitshift-and-add (it distributes over XOR, associative, communative, etc. etc). Bitshift-and-xor has a few features that are better for cryptography. But its NOT the multiplication you are taught in grade school. -------- EDIT: With that being said... those "better features" for cryptography would make PMULL probably a better function for random-number generation. PMULL will return a different result than the real multiplication, but you'll have an easier time making a field (aka: reversable 1-to-1 bijections) out of PMULL than MUL...
- dragontamer 8y agoI don't see any generalized 64x64->128-bit multiply on ARM's intrinsic documentation: https://developer.arm.com/technologies/neon/intrinsics https://developer.arm.com/technologies/neon/intrinsics I see "pmull" (vmull_p64), but that's polynomial multiply, which is used for Galois Field multiplication. Not "normal" multiplication. You can search for "uint64", to look for all NEON intrinsics that take a 64-bit integer. (ex: uint64x2_t). I personally didn't see any 64x64->128 bit standard multiply in NEON.
- rsp1984 8y agoShould we be surprised? The __(u)int128_t types are an optional extension of the C standard, hence there can be no expectation that operations with these are implemented well or at all, let alone implemented in silicon.
- ajross 8y agoI don't see what specification has to do with this. I mean, a 32 bit 2's complement integer is also a technically optional part of the C standard, and indeed there is hardware that doesn't support multiplications on them with a single instruction. What's happening here isn't related to word size, really. It's that multiplication, as an operation, is lossy. It produces 2 words worth of output, not one. Traditionally, most RISC architectures have just skipped the complexity and returned the product modulo the word space. But x86 didn't, it put the product into two registers (specific registers: DX and AX, and their subsequent extensions). Most of the time this just a quirk of the instruction set and an annoyance to compiler writers. But sometimes (and this trick has been exploited on x86 for decades for applications like hashing) it turns out to be really, really useful.
- rsp1984 8y agoInteger multiplication always carries the risk of Integer overflow. Integer Overflow is undefined behavior in C, so it's the programmer's responsibility to make sure it doesn't happen. To that end in the example a __uint128_t was used, which is nonstandard, and apparently not implemented all that well with the given combination of compiler and ARM CPU. Given that we're talking about a 64-bit CPU, my argument is that this is not very surprising.
- ajross 8y agoAgain, I think you're looking at this backwards. C's undefined behavior rules exist because targetted hardware didn't have common behavior, where you seem to be arguing the reverse. I mean, I can't sit here and tell you what to be surprised about, but to me, as someone interested in how machines behave, it's absolutely interesting and "surprising"[1] that one machine with an otherwise very similar architecture can be 8x slower than another on straightforward code. And trying to wave that away with "but the spec never promised!" seems like it's only going to get you in this kind of trouble (but "unsurprising trouble", I guess) more, and not less. [1] Not here, precisely, since I already understand what's going on, but in general.
- m0zg 8y agoMoreover, different ARM chips have different performance characteristics as well. Apple's implementation, for instance, seems to be easily twice as fast on most problems at the same clock speed as its leading competitors.
- amelius 8y agoAre there differences in speculative execution?
- userbinator 8y agoI suspect if you were to compare code that uses both the remainder and quotient of a division, you would find a similar trend: the x86 division instructions produce both, but ARM has only a quotient-generating division instruction, and you have to do another multiplication(!) and subtraction to get the remainder. That, along with the fact that earlier ARMs didn't even have a divide instruction, is one of the reasons why I'm not at all fan of the "RISC approach" --- division may not have been easy to implement, but microcoding would've been at least as fast as doing the algorithm in software while taking up far less space, and with hardware advances that move operations from microcode into direct execution, existing software that already has those instructions would automatically become faster on newer hardware with no other action needed. Division hardware almost always will generate the remainder along with the quotient, but ARM would either need to add another instruction (no benefit to existing software, only new software that knows how to use it) or attempt to detect patterns of simpler instructions that are doing the same thing in order to replace them with one "fused uop" (much more difficult to do.)
- acdha 8y agoI wonder how much that has been changing as an increasingly large percentage of software is compiled far more frequently than in the past (JIT, formats like WASM and Apple’s bitcode, toolchains like LLVM becoming widespread, etc.). It seems like at least Apple could ship a new ARM instruction and be relatively confident that most of the important code would be updated to use it on a time-scale which used to seem impossible. Java might be a counter-example but I’m not sure you can separate that from all of the other dubious decisions which happened over the years there.
- pcwalton 8y agoThere is a good reason why nobody implements multiply and divide like x86 did: it's terrible for register allocation. Especially on 32-bit, where register pressure is very high, having division input and output registers hardwired is obnoxious, and it kills a register for the upper half of the product or remainder or quotient that you almost never need. ARM has the correct design. Even Intel admits this: look at the MULX instruction that retrofits the right design onto x86. Earlier versions of ARM didn't have a divide instruction because of die space and the fact that it's rarely needed. Integer division is usually by a constant, so a shift and/or a multiplication suffices. There's no need to microcode a divide instruction either, because the kernel can just trap the #UD and perform the division itself, just like soft float works. (Yeah, it's slow, but software division is always slow.) Modern designs like RISC-V do this properly: expose division as an optional extension so as to scale down to microcontrollers.