C++ 什么是内在因素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2268562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are intrinsics?
提问by Scott J
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?
谁能解释一下它们是什么以及为什么我需要它们?如果我需要使用内在函数,我要构建什么样的应用程序?
采纳答案by Jerry Coffin
Normally, "intrinsics" refers to functions that are built-in -- i.e. 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:
通常,“内部函数”指的是内置函数——即编译器可以/将生成内联的大多数标准库函数,而不是调用库中的实际函数。例如,像这样的调用memset(array1, 10, 0)
可以为 x86 编译如下:
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 Intrinsics”,让你可以使用“函数”,这些“函数”实际上只是 MMX 指令的直接表示。
回答by Heath Hunnicutt
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. Worse, the effect on CPU pre-fetch buffers will stall the CPU execution for several clock cycles.
对于短字符串,对 进行函数调用strncpy()
(包括设置带有返回地址的“堆栈帧”)将比实际复制字节消耗更多时间。更糟糕的是,对 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. 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.
与内联函数相比,内部函数由编译器提供。在 C 程序的源代码中没有编写内部函数的地方,也没有必须链接到的库实现。内联函数的不同之处在于编译器读取内联函数的源代码,但相似之处在于它稍后可以将内联函数的编译翻译直接发送到目标代码中,省略函数调用的开销。
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. For an inline function, the header file with the function declaration must be #include
'd (or otherwise declared) first.
简而言之,内在函数和内联函数之间的实际区别在于,即使您没有#include
包含函数声明的所需头文件,内在函数也“存在” 。对于内联函数,带有函数声明的头文件必须首先被#include
'd(或以其他方式声明)。
回答by greyfade
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 intrinsicswhich 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 指令的代码中使用它们,并且您不一定需要完整的内联汇编部分。
回答by wallyk
''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. PHP, Javascript, Ruby, etc. have hundreds of intrinsic functions such as to create and search arrays, perform regular expression matches, etc., etc.
其他示例包括 Fortran 之类的语言,其中隐式支持复杂类型,并且不需要也不能声明超越(正弦、正切等)函数。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.
没有依赖于内在函数的特定应用程序模式;这只是编译器编写者和程序员的重要问题。