Gcc intrinsic vs inline assembly: which is better?

If I want to show the programmer a separate instruction for a specific computer, I can do this in two ways:

  • Define a new inline / inline
  • Expose the same as inline assembly asm () [As its one arithmetic type instruction, I believe that there is no need for asm volatile ()]

I read that the built-in functions allow the compiler to monitor the type of validation, the allocation of registers and "other optimizations", etc. But the compiler will have to do this even in the case of asm (), right? So, what is the performance advantage when using the built-in asm () for a single command?

How does the equation change when there are several machine instructions?

The argument of "portability" in favor of the internal is understandable, but I am curious to understand the performance benefits, if any, of one over the other.

+6
source share
1 answer

I think it depends a lot on what you are doing. Modifying GCC and requiring a GCC change to build your program, unless your GCC patch makes it upstream is more of a headache than just using the built-in asm.

If the command you want to use has an abstract value that is not tied to a specific instruction set architecture, adding inline / inline so that the same code can automatically work with all goals with its help (with a return to a more complex implementation with several instructions for goals that don't have instructions) is probably the β€œright” choice, but may not be entirely practical.

If the instruction is something very specific to ISA, obscure, not performance critical, etc. (I'm going to download a special equipment recorder, processor mode register, get information about the model, etc., but I'm sure you can come up with other examples), then just using inline asm is definitely the right solution.

Even if you think that the built-in is the β€œright” solution for your problem, but for practical reasons you need to use the asm built-in approach, you can still abstract it with a macro or a static built-in function so that it can easily replace all use with built-in later (or with a backup implementation for goals without instructions).

+6
source

Source: https://habr.com/ru/post/974857/


All Articles