Intrinsic function
In computer software, in compiler theory, an intrinsic function, also called built-in function or builtin function, is a function (subroutine) available for use in a given programming language whose implementation is handled specially by the compiler. Typically, it may substitute a sequence of automatically generated instructions for the original function call, similar to an inline function.[1] Unlike an inline function, the compiler has an intimate knowledge of an intrinsic function and can thus better integrate and optimize it for a given situation.
Compilers that implement intrinsic functions may enable them only when a program requests optimization, otherwise falling back to a default implementation provided by the language runtime system (environment).
Vectorization and parallelization
Intrinsic functions are often used to explicitly implement vectorization and parallelization in languages which do not address such constructs. Some application programming interfaces (API), for example, AltiVec and OpenMP, use intrinsic functions to declare, respectively, vectorizable and multiprocessing-aware operations during compiling. The compiler parses the intrinsic functions and converts them into vector math or multiprocessing object code appropriate for the target platform. Some intrinsics are used to provide additional constraints to the optimizer, such as values a variable cannot assume.[2]
By programming language
C and C++
Compilers for C and C++, of Microsoft,[3] Intel,[1] and the GNU Compiler Collection (GCC)[4] implement intrinsics that map directly to the x86 single instruction, multiple data (SIMD) instructions (MMX, Streaming SIMD Extensions (SSE), SSE2, SSE3, SSSE3, SSE4, AVX, AVX2, AVX512, FMA, ...). The Microsoft Visual C++ compiler of Microsoft Visual Studio does not support inline assembly for x86-64.[5][6][7][8] To compensate for this, new intrinsics have been added that map to standard assembly instructions that are not normally accessible through C/C++, e.g., bit scan.
Some C and C++ compilers provide non-portable platform-specific intrinsics. Other intrinsics (such as GNU built-ins) are slightly more abstracted, approximating the abilities of several contemporary platforms, with portable fall back implementations on platforms with no appropriate instructions.[9] It is common for C++ libraries, such as glm or Sony's vector maths libraries,[10] to achieve portability via conditional compilation (based on platform specific compiler flags), providing fully portable high-level primitives (e.g., a four-element floating-point vector type) mapped onto the appropriate low level programming language implementations, while still benefiting from the C++ type system and inlining; hence the advantage over linking to hand-written assembly object files, using the C application binary interface (ABI).
Examples
uint64_t __rdtsc (); // return internal CPU clock counter uint64_t __popcnt64 (uint64_t n); // count of bits set in n uint64_t _umul128 (uint64_t Factor1, uint64_t Factor2, uint64_t* HighProduct); // 64 bit * 64 bit => 128 bit multiplication __m512 _mm512_add_ps (__m512 a, __m512 b); // calculates a + b for two vectors of 16 floats __m512 _mm512_fmadd_ps(__m512 a, __m512 b, __m512 c); // calculates a*b + c for three vectors of 16 floats
Links
Java
The HotSpot Java virtual machine's (JVM) just-in-time compiler also has intrinsics for specific Java APIs.[11] Hotspot intrinsics are standard Java APIs which may have one or more optimized implementation on some platforms.
PL/I
ANSI/ISO PL/I defines nearly 90 builtin functions.[12] These are conventionally grouped as follows:[13]:337–338
- String-handling builtin functions such as INDEX, LENGTH
- Arithmetic builtin functions such as ABS, CEIL, ROUND
- Mathematical builtin functions like SIN, COS, LOG, ERF
- Array-handling builtin functions, for example ANY, ALL, PROD
- Condition-handling builtin functions like ONCODE, ONFILE
- Storage Control builtin functions, for example ADDR, POINTER
- Input-Output builtins: LINENO
- Miscellaneous builtin functions like DATE and TIME
Individual compilers have added additional builtins specific to a machine architecture or operating system.
A builtin function is identified by leaving its name undeclared and allowing it to default, or by declaring it BUILTIN
. A user-supplied function of the same name can be substituted by declaring it as ENTRY
.
References
- ↑ 1.0 1.1 "Intel® C++ Compiler 19.1 Developer Guide and Reference". 16 December 2019. https://software.intel.com/en-us/cpp-compiler-developer-guide-and-reference-intrinsics.
- ↑ The Clang Team (2020). "Clang Language Extensions". https://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume. "Builtin Functions"
- ↑ MSDN. "Compiler Intrinsics". Microsoft. http://msdn.Microsoft.com/en-us/library/26td21ds.
- ↑ GCC documentation. "Built-in Functions Specific to Particular Target Machines". Free Software Foundation. https://gcc.gnu.org/onlinedocs/gcc/Target-Builtins.html.
- ↑ MSDN. "Intrinsics and Inline Assembly". Microsoft. http://msdn.microsoft.com/en-us/library/wbk4z78b.aspx?ppud=4.
- ↑ MSDN. "Intrinsics and Inline Assembly". Microsoft. http://msdn.microsoft.com/en-us/library/wbk4z78b(v=vs.100).aspx.
- ↑ MSDN. "Intrinsics and Inline Assembly". Microsoft. http://msdn.microsoft.com/en-us/library/wbk4z78b(v=VS.90).aspx.
- ↑ MSDN. "Intrinsics and Inline Assembly". Microsoft. http://msdn.microsoft.com/en-us/library/wbk4z78b(v=VS.80).aspx.
- ↑ "Vector Extensions". https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html.
- ↑ "Sony open sources Vector Math and SIMD math libraries (Cell PPU/SPU/other platforms)" (in en-US). Beyond3D Forum. https://forum.beyond3d.com/threads/sony-open-sources-vector-math-and-simd-math-libraries-cell-ppu-spu-other-platforms.38677/.
- ↑ Mok, Kris (25 February 2013). "Intrinsic Methods in HotSpot VM". Slideshare. http://www.slideshare.net/RednaxelaFX/green-teajug-hotspotintrinsics02232013.
- ↑ ANSI X3 Committee (1976). American National Standard programming language PL/I.
- ↑ IBM Corporation (1995). IBM PL/I for MVS & VM Language Reference.
External links
- Intel Intrinsics Guide
- Using milicode routines, IBM AIX 6.1 documentation
Original source: https://en.wikipedia.org/wiki/Intrinsic function.
Read more |