3 ms·
And this is why inline Assembly done by GCC and clang sucks, and the inline Assembly done by most PC compilers, with first level support for opcodes, or instris
by pjmlp 2y ago
And this is why inline Assembly done by GCC and clang sucks, and the inline Assembly done by most PC compilers, with first level support for opcodes, or instrisics is a great experience.
- SassyBird 2y agoIt seems that MSVC supports inline assembly only on 32-bit x86. For amd64 and ARM they moved entirely to intrinsics.
- vnorilo 2y agoThe more I've written code close to metal (mostly SIMD for signal processing), the more I've grown to prefer either intrinsics or separate translation unit for assembly. If you want your code to intertwine with what the C compiler does, intrinsics are great. If you don't, .s is great.
- pjmlp 2y agoYes, Assembly programming concatenanting strings, is horrible. Intrisics give the best of both worlds, without dealing with string based content. Actually ESPOL/NEWP from 1961 was one of the very first system programming languages with two key inovations, intrisics and unsafe code blocks.
- SassyBird 2y agoI’m not a fan of concatenating strings either. I was thinking about the MSVC/Pascal-style inline assembly here, which isn’t based on strings. Correct me if I’m wrong, but I don’t think that you can guarantee a routine is constant-time by using intrinsics. You need asm for that. Asm that won’t be changed by the compiler. So you need to write an external asm file for that now, which is fair enough, but I just wouldn’t present intrinsics as all-around superior.
- pjmlp 2y agoYes, if you want full control, only external Assemblers will do the trick.
- ack_complete 2y agoYes, and the set of intrinsics supported by MSVC is really lacking. On x64, this left no way to force a conditional move to bypass a poorly predictable branch, nor was there a way to access carry-extended operations until recently. On ARM64, it doesn't have complete coverage of scalar intrinsics for operations like RBIT (reverse bit order).
- clausecker 2y agoIt's great experience, but it's also slow as the compier must save the entire function state before your assembly block and cannot make any assumptions about what you do inside. This is acceptable if you see the point of inline assembly in writing long code sections, but that's not what inine assembly is meant to solve. If you want to write long sections of assembly code, just put the whole thing into an assembly source file and link it into your binary. gcc-style inline assembly on the other hand is designed to solve the problem of augmenting code generation with individual instructions the compiler doesn't know about. Correctly used, gcc-style asm statements typically only hold one or two instructions that are then integrated into a complex function with little to no overhead. And for this usage, the syntax and model provided by gcc is perfectly fine. It only fails if you misuse it for writing long chunks of assembly code, something for which you should have used an assembly file in the first place.
- pjmlp 2y ago> It's great experience, but it's also slow as the compier must save the entire function state before your assembly block and cannot make any assumptions about what you do inside. That is why there are function attributes and #pragmas to control what the compiler is supposed to do. Compilers that don't follow the brainded execution model of UNIX, with hard separation between compiler and Assembler phases, as separate processes comunicating via pipes, are a bit more knowledgeable of what those Assembly instructions are actually doing. Not only that, they are able to provide proper error messages when the opcodes are badly used.
- cesarb 2y ago> [...] are a bit more knowledgeable of what those Assembly instructions are actually doing. Not only that, they are able to provide proper error messages when the opcodes are badly used. That's only possible if the compiler knows all assembly instructions being used. But the whole point of GCC-style inline assembly is to be able to use assembly instructions the compiler doesn't know about (for instance, because they're new instructions which didn't exist when the compiler was released), with the same performance as the ones the compiler does know about. In many cases, even the assembler doesn't know about the new instruction, and you have to specify it using .byte directives or similar (though the Unix compiler model, with proper separation between the compiler and the assembler, also allows you to use a newer assembler without having to change your compiler).
- Retr0id 2y agoWhat is a PC compiler in this context? MSVC?
- pjmlp 2y agoAlmost all MS-DOS, OS/2, Windows 16 bit, Windows 32 bit and Windows 64 bit compilers you can think of, that aren't GCC or clang forks with their UNIX architecture, and support inline Assembly. For C, C++, BASIC, Pascal, Delphi, Modula-2, Oberon-2, Component Pascal, D,... Traditionally PC refers to IBM/Microsoft's linage of computers and operating systems.