编译器内部函数Compiler intrinsics

大多数函数都包含在库中,但也有一些函数是在编译器中生成的(即内部函数)。Most functions are contained in libraries, but some functions are built in (that is, intrinsic) to the compiler. 这些被称为内联函数或内部函数。These are referred to as intrinsic functions or intrinsics.

备注Remarks

如果一个函数是内部函数,在通常会采用内联方式插入该函数的代码,从而避免函数调用的开销并可发出该函数的高效率计算机指令。If a function is an intrinsic, the code for that function is usually inserted inline, avoiding the overhead of a function call and allowing highly efficient machine instructions to be emitted for that function. 内部函数通常比等效的内联程序集速度更快,因为优化程序拥有众多内部函数行为方式的内置知识,因此可以优化使用内联程序集无法优化的内容。An intrinsic is often faster than the equivalent inline assembly, because the optimizer has a built-in knowledge of how many intrinsics behave, so some optimizations can be available that are not available when inline assembly is used. 此外,优化程序还可以采用不同的方式扩展内部函数、对齐缓冲区或根据上下文和调用自变量进行其他方面的调整。Also, the optimizer can expand the intrinsic differently, align buffers differently, or make other adjustments depending on the context and arguments of the call.

使用内部函数会影响到代码的可移植性,因为在 Visual C++ 中可用的内部函数如果用其他编译器编译代码则可能不可用,并且对于某些目标体系结构可用的部分内部函数并非对所有体系结构都可用。The use of intrinsics affects the portability of code, because intrinsics that are available in Visual C++ might not be available if the code is compiled with other compilers and some intrinsics that might be available for some target architectures are not available for all architectures. 但是,内部函数通常比内联程序集可移植性更大。However, intrinsics are usually more portable than inline assembly. 64 位体系结构要求内部函数,但不支持内联程序集。The intrinsics are required on 64-bit architectures where inline assembly is not supported.

某些内部函数(例如 __assume__ReadWriteBarrier)向编译器提供信息,但这会影响到优化程序的行为。Some intrinsics, such as __assume and __ReadWriteBarrier, provide information to the compiler, which affects the behavior of the optimizer.

某些内部函数只能用作内部函数,某些内部函数可以同时用于函数和内部函数实现。Some intrinsics are available only as intrinsics, and some are available both in function and intrinsic implementations. 你可以指示编译器使用这两种方式中的一种来使用内部函数实现,具体取决于你是想仅启用特定函数还是想启用所有内部函数。You can instruct the compiler to use the intrinsic implementation in one of two ways, depending on whether you want to enable only specific functions or you want to enable all intrinsics. 第一种方法是使用 #pragma intrinsic(内部函数名称列表)The first way is to use #pragma intrinsic(intrinsic-function-name-list). 杂注可用于指定单个内部函数或用逗号分隔的多个内部函数。The pragma can be used to specify a single intrinsic or multiple intrinsics separated by commas. 第二种方法是使用/Oi (生成内部函数)编译器选项,该选项使给定平台上的所有内部函数都可用。The second is to use the /Oi (Generate intrinsic functions) compiler option, which makes all intrinsics on a given platform available. /Oi下,使用 #pragma function(内部函数名称列表) 强制使用函数调用而不是内部函数。Under /Oi, use #pragma function(intrinsic-function-name-list) to force a function call to be used instead of an intrinsic. 如果特定内部函数的文档说明例程仅作为内部函数提供,则无论是否指定了 /Oi#pragma intrinsic,都将使用内部实现。If the documentation for a specific intrinsic notes that the routine is only available as an intrinsic, then the intrinsic implementation is used regardless of whether /Oi or #pragma intrinsic is specified. 在所有情况下, /Oi#pragma intrinsic 都允许但不强制优化器使用内部函数。In all cases, /Oi or #pragma intrinsic allows, but does not force, the optimizer to use the intrinsic. 优化程序仍然可以调用函数。The optimizer can still call the function.

一些标准的 C/C++ 库函数在某些体系结构上可用于内部函数实现。Some standard C/C++ library functions are available in intrinsic implementations on some architectures. 调用 CRT 函数时,如果在命令行上指定 /Oi ,则使用内部实现。When calling a CRT function, the intrinsic implementation is used if /Oi is specified on the command line.

标头文件(<intrin.h >)可用于声明常用内部函数的原型。A header file, <intrin.h>, is available that declares prototypes for the common intrinsic functions. 制造商特定的内部函数在 <immintrin.h> > 中提供,<ammintrin.h > 头文件中。Manufacturer-specific intrinsics are available in the <immintrin.h> and <ammintrin.h> header files. 此外,某些 Windows 头文件还可声明在编译器内部函数上映射的函数。Additionally, certain Windows headers declare functions that map onto a compiler intrinsic.

以下部分列出了可用于各种体系结构的所有内部函数。The following sections list all intrinsics that are available on various architectures. 有关内部函数在特定目标处理器上的工作方式的详细信息,请参阅制造商参考文档。For more information on how the intrinsics work on your particular target processor, refer to the manufacturer's reference documentation.

请参阅See also

ARM 汇编程序参考ARM assembler reference
Microsoft 宏汇编程序参考Microsoft Macro Assembler reference
关键字Keywords
C 运行时库参考C run-time library reference