Intrinsic function
![]() | This article needs additional citations for verification. (December 2009) |
In compiler theory, an intrinsic function is a function available for use in a given programming language whose implementation is handled specially by the compiler. Typically, it substitutes a sequence of automatically generated instructions for the original function call, similar to an inline function. Unlike an inline function though, the compiler has an intimate knowledge of the intrinsic function and can therefore better integrate it and optimize it for the situation. This is also called builtin function in many languages.
Compilers that implement intrinsic functions generally enable them only when the user has requested optimization, falling back to a default implementation provided by the language runtime environment otherwise.
Intrinsic functions are often used to explicitly implement vectorization and parallelization in languages which do not address such constructs. Altivec and OpenMP are examples of APIs which use intrinsic functions to declare, respectively, vectorizable and multiprocessor-aware operations during compilation. The compiler parses the intrinsic functions and converts them into vector math or multiprocessing object code appropriate for the target platform.
Microsoft[1] and Intel's C/C++ compilers as well as GCC[2] implement intrinsics that map directly to the x86 SIMD instructions (MMX, SSE, SSE2, SSE3, SSSE3, SSE4). In the latest version of the Microsoft Visual Studio (VS2010), the Visual C++ compiler does not support inline assembler for X86-64[3] (neither VS2008[4] nor VS2005[5]). To compensate for the lack of inline assembly, new intrinsics have been added that map to standard assembly instructions that are not normally accessible through C/C++ (e.g.: bit scan).
[edit] References
- ^ MSDN (VS2010). "Compiler Intrinsics". Microsoft. http://msdn.Microsoft.com/en-us/library/26td21ds. Retrieved 2012-06-20.
- ^ GCC documentation. "Built-in Functions Specific to Particular Target Machines". Free Software Foundation. http://gcc.gnu.org/onlinedocs/gcc/Target-Builtins.html. Retrieved 2012-06-20.
- ^ MSDN (VS2010). "Intrinsics and Inline Assembly". Microsoft. http://msdn.microsoft.com/en-us/library/wbk4z78b.aspx?ppud=4. Retrieved 2010-04-16.
- ^ MSDN (VS2008). "Intrinsics and Inline Assembly". Microsoft. http://msdn.microsoft.com/en-us/library/wbk4z78b(v=VS.90).aspx. Retrieved 2011-09-28.
- ^ MSDN (VS2005). "Intrinsics and Inline Assembly". Microsoft. http://msdn.microsoft.com/en-us/library/wbk4z78b(v=VS.80).aspx. Retrieved 2011-09-28.
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.
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.
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. The intrinsics are required on 64-bit architectures where inline assembly is not supported.
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. 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. The second is to use the /Oi (Generate Intrinsic Functions) compiler option, which makes all intrinsics on a given platform available. Under /Oi, use #pragma function(intrinsic-function-name-list) to force a function call to be used instead of an 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. 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.
Some standard C/C++ library functions are available in intrinsic implementations on some architectures. When calling a CRT function, the intrinsic implementation is used if /Oi is specified on the command line.
A header file, Intrin.h, is available that declares prototypes for the intrinsic functions. 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.