Explain, please, what is intrinsiki?

  • one
    By the way, look here: msdn.microsoft.com/ru-ru/library/tzkfha43.aspx And yet - for example, in Open Watcom you could specify whether to use a real call for some functions, or use intrinsic - in fact, ready-made inline assembly code. - Harry
  • can you look somewhere at an example of the use of this miracle? - Arkady
  • @VladfromMoscow with the first comment Grundy gave a link to a scientific article with this term in Russian transcription. There the author introduces the term and explains it just in case. - Arkady
  • one
    @ 0andriy, my question was idle. I just agree with the theses from the VladFromMoscow comment on the one hand, and on the other I realized that if I ran into such a task, I would have wondered how to translate it correctly and whether it was worth it. And the "inherent", IMHO, is still not obvious. So far it seems to me the most reasonable thing is not to translate the term, but also not to use translit. - Arkady

4 answers 4

Here is a specific example from VC ++ .

#pragma intrinsic(strlen) int main(int argc, const char * argv[]) { cout << strlen(argv[0]); } 

Compile with disabled optimization (/ Od). In the presence of #pragma intrinsic(strlen) we get

  mov eax, 4 imul ecx, eax, 0 mov edx, DWORD PTR _argv$[ebp] mov eax, DWORD PTR [edx+ecx] mov DWORD PTR tv74[ebp], eax mov ecx, DWORD PTR tv74[ebp] add ecx, 1 mov DWORD PTR tv77[ebp], ecx $LL3@main: mov edx, DWORD PTR tv74[ebp] mov al, BYTE PTR [edx] mov BYTE PTR tv80[ebp], al add DWORD PTR tv74[ebp], 1 cmp BYTE PTR tv80[ebp], 0 jne SHORT $LL3@main mov ecx, DWORD PTR tv74[ebp] sub ecx, DWORD PTR tv77[ebp] mov DWORD PTR tv70[ebp], ecx mov edx, DWORD PTR tv70[ebp] push edx mov ecx, OFFSET ?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout 

And in the absence of -

 mov eax, 4 imul ecx, eax, 0 mov edx, DWORD PTR _argv$[ebp] mov eax, DWORD PTR [edx+ecx] push eax call _strlen add esp, 4 push eax mov ecx, OFFSET ?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout call ??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@I@Z ; std::basic_ostream<char,std::char_traits<char> >::operator<< 

(For information - with optimization turned on in the same way in both cases -

  mov eax, DWORD PTR _argv$[esp-4] mov eax, DWORD PTR [eax] lea edx, DWORD PTR [eax+1] npad 7 $LL3@main: mov cl, BYTE PTR [eax] inc eax test cl, cl jne SHORT $LL3@main sub eax, edx mov ecx, OFFSET ?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout push eax call ??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@I@Z ; std::basic_ostream<char,std::char_traits<char> >::operator<< 

Those. in a certain sense, these are inline functions, only with pre-built assembly code ...

  • 7
    in a certain sense, these are inline functions, only with pre-assembled assembly code - +1 was completely accessible - Denis Bubnov

In C / C ++, any entity declared but not defined within the compiled file is considered external. This applies to functions as well as to variables. References to external functions remain in the compiled object file and will be replaced with references to real entities only at the linking stage, if all intermodule dependencies are satisfied. There is no difference between PrivetVasya() and printf() - from the point of view of the compiler, both are absolutely equivalent and one can say about both “yes, these are just some external functions”. When idiotic textbooks or under-educated teachers begin to say “the built-in function of the printf () language” (and this is a very popular nonsense) - you need to understand that this is just nonsense, that nothing is built into the language, that the compiler handles the call to printf () on the same conditions as the call to any other function, but at least in the next file implemented. As for the same printf (), this is not a built-in language function, but a function of the standard language library. The language standard describes this function, declares its presence in the standard library, but the compiler itself has no relation to the standard library - it may appear at the linking stage, or it may not appear at all.

However, there are truly built-in functions for which special processing is actually implemented in the compiler - they are called intrinsics. Different compilers have a different intrinsic set. Intrinsic may also be a function that should normally live in a standard library. When you call an intrinsic-function, the compiler generates a special code specific to this function: no call is generated, there will be no real call and return, but there will be several instructions that perform the desired task. For example, a very common intrinsic memcpy() not compiled into a call to a function, but into the repnz movs instruction (an example for x86).

It is clear that in the standard library C (libc) for AVR there are some functions that provide a delay. Naturally, these are full-fledged functions that spin a cycle inside. If a 1-2 clock delay is needed, then naturally such heavy functions are not suitable. You cannot make a delay of 1 clock full (and ordinary) function: even if it is a completely empty function, the call instruction is executed in 4 cycles, and the ret instruction is another 4 cycles, totaling 8 cycles to call an empty function.

Without the slightest superfluous thought, it is clear that delays in units of cycles (less than 8) can only be realized by intrinsic ones. And now, with fingers crossed, we ask: is there any delay-function (delay function) in avr-gcc , performed as intrinsic-i? Indeed there is such an intrinsic - the function is called __builtin_avr_delay_cycles() .

Source: Feil gcc (or called intrinsic'om - get into the optimizer) .

  • And I will not draw out a quote. Moreover, in one of the paragraphs I changed a few words. - Qwertiy
  • And do the general - too? ) - AK
  • @AK, I rewrote bb codes on markdown and changed one sentence :) - Qwertiy

Intrinsics are functions whose calls are replaced by the compiler with some kind of intracompiler magic. That is, you think that you are calling a function, but in fact it does not exist, and instead of it, a certain machine code is directly substituted, regardless of the optimization flags.

    Intrinsic is a wrapper for functions that is built into the compiler, in the same way as regular functions. In other words, we can say that this is a mechanism for including machine instructions in the code.

    Useful links for familiarization:

    1. Intrinsic function (WiKi)
    2. Built-in compiler objects
    3. What are intrinsics? (en SO)