简体   繁体   English

什么是内在函数?

[英]What are intrinsics?

Can anyone explain what they are and why I would need them? 任何人都可以解释它们是什么以及为什么我需要它们? What kind of applications am I building if I need to use intrinsics? 如果我需要使用内在函数,我将构建什么样的应用程序?

An intrinsic function is a function which the compiler implements directly when possible, rather than linking to a library-provided implementation of the function. 内部函数是编译器在可能的情况下直接实现的函数,而不是链接到库提供的函数实现。

A common example is strncpy() . 一个常见的例子是strncpy()

For short strings, making a function call to strncpy() , which involves setting up a 'stack frame' with a return address, will consume more time than the actual copying of bytes does. 对于短字符串,对strncpy()进行函数调用strncpy()包括设置带有返回地址的“堆栈帧”)将比实际的字节复制消耗更多的时间。 Worse, the effect on CPU pre-fetch buffers will stall the CPU execution for several clock cycles. 更糟糕的是,对CPU预取缓冲区的影响将使CPU执行停顿几个时钟周期。

Instead, the intrinsic function is implemented by the compiler in lieu of a function call. 相反,内部函数由编译器实现,而不是函数调用。 In the example of strncpy() , the byte-copying code is emitted directly at the place where strncpy() is invoked. strncpy()的示例中,字节复制代码直接在调用strncpy()的位置发出。

Similar to this strncpy() example, every intrinsic function is implemented directly as in-line code if required constraints are met. 与此strncpy()示例类似,如果满足所需的约束,则每个内部函数都直接作为内联代码实现。

A non-intrinsic copy of the intrinsic function usually still exists in the standard library, in case the address of the function is needed. 内部函数的非内在副本通常仍然存在于标准库中,以防需要函数的地址。

As compared to inline functions, the intrinsic function is provided by the compiler. 与内联函数相比,内部函数由编译器提供。 There isn't a place in the source code of a C program where the intrinsic function is written, nor is there a library implementation that must be linked to. 在C程序的源代码中没有写入内部函数的地方,也没有必须链接到的库实现。 An inline function is different in that the compiler reads the source code for the inline function, but is similar in that later it may emit a compiled translation of the inline function directly into the object code, omitting the overhead of a function call. 内联函数的不同之处在于编译器读取内联函数的源代码,但类似之后,它可以将内联函数的编译转换直接发送到目标代码中,省略函数调用的开销。

In short, the practical difference between an intrinsic funciton and an inline function is that intrinsic functions are "present" even if you have not #include the needed header file which contains the function declaration. 简而言之,内部函数和内联函数之间的实际区别在于,即使您没有#include包含函数声明的必需头文件,内部函数也“存在”。 For an inline function, the header file with the function declaration must be #include 'd (or otherwise declared) first. 对于内联函数,带有函数声明的头文件必须首先是#include (或以其他方式声明)。

Normally, "intrinsics" refers to functions that are built-in -- ie most standard library functions that the compiler can/will generate inline instead of calling an actual function in the library. 通常,“内在函数”指的是内置函数 - 即大多数标准库函数,编译器可以/将生成内联而不是调用库中的实际函数。 For example, a call like: memset(array1, 10, 0) could be compiled for an x86 as something like: 例如,可以为x86编译类似于: memset(array1, 10, 0)的调用memset(array1, 10, 0)如:

 mov ecx, 10
 xor eax, eax
 mov edi, offset FLAT:array1
 rep stosb

Intrinsics like this are purely an optimization. 像这样的内在函数纯粹是一种优化。 "Needing" intrinsics would most likely be a situation where the compiler supports intrinsics that let you generate code that the compiler can't (or usually won't) generate directly. “需要”内在函数很可能是编译器支持内在函数的情况,这些内在函数允许您生成编译器无法(或通常不会)直接生成的代码。 For an obvious example, quite a few compilers for x86 have "MMX Intrinsics" that let you use "functions" that are really just direct representations of MMX instructions. 对于一个明显的例子,x86的很多编译器都有“MMX内在函数”,它允许你使用“函数”,这些函数实际上只是MMX指令的直接表示。

Intrinsics are exposed by the compiler as functions that are not part of any library, per se . 编译器将内在函数公开为不属于任何库的函数本身

The ones you'd probably use the most are assembly intrinsics which are treated by the compiler as precisely the machine instruction they represent. 您可能最常使用的是汇编内在函数 ,它们被编译器视为它们所代表的机器指令。 You'd use them, for example, in code where you need to take advantage of a specific CPU instruction that the compiler doesn't automatically generate, and where you don't necessarily require a full inline assembly section. 例如,您可以在需要利用编译器不自动生成的特定CPU指令的代码中使用它们,并且您不一定需要完整的内联汇编部分。

''Intrinsics'' are those features of a language that a compiler recognizes and implements without any need for the program to declare them. “内在函数”是编译器识别和实现的语言的那些特性,而不需要程序来声明它们。 The compiler may—or may not—link to a runtime library to perform the operation. 编译器可能或可能不链接到运行时库以执行操作。 In C++ for example, the structure copy operation is implicit: 例如,在C ++中,结构复制操作是隐式的:

struct {
    int  a;
    char b [100];
    long c [27];
} s, t;

...
s = t;   // this statement copies hundreds of bytes, likely with a rtl call

Other examples include languages like Fortran where there is implicit support for the complex type, and the transcendental (sine, tangent, etc.) functions need not—and can't—be declared. 其他示例包括像Fortran这样的语言,其中隐含支持复杂类型,并且超越(正弦,切线等)函数不需要 - 也不能声明。 PHP, Javascript, Ruby, etc. have hundreds of intrinsic functions such as to create and search arrays, perform regular expression matches, etc., etc. PHP,Javascript,Ruby等具有数百种内在函数,例如创建和搜索数组,执行正则表达式匹配等等。

As for your other questions, the only difference is whether they need to be declared. 至于你的其他问题,唯一的区别是它们是否需要申报。 For example, a C++ program using transcendental functions must include math library declarations: 例如,使用超越函数的C ++程序必须包含数学库声明:

#include <math.h>

There is no particular pattern of applications which depend on intrinsics; 没有特定的应用程序模式依赖于内在函数; that's only a matter of significance to compiler writers and programmers. 这对编译器编写者和程序员来说只是一个重要的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM