Jump to content

Intrinsic function

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Bender235 (talk | contribs) at 15:03, 25 July 2016 (C and C++: clean up; http->https (see this RfC) using AWB). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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.

C and C++

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). The Visual C++ compiler of Microsoft Visual Studio does not support inline assembler for X86-64.[3][4][5][6] 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).

Some C/C++ compilers provide non-portable platform-specific intrinsics. Other intrinsics (such as GNU built-ins) are slightly more abstracted, approximating the capabilities of several contemporary platforms, with portable "fall back" implementations on platforms without the appropriate instructions. It is common for C++ libraries, such as glm or Sony's vector maths libraries,[7] 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 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 ABI).

Java

The HotSpot JVM's just-in-time compiler also has intrinsics for specific Java APIs.[8] Hotspot intrinsics are standard Java APIs which may have an optimized implementations on some platforms.

References

  1. ^ MSDN (VS2010). "Compiler Intrinsics". Microsoft. Retrieved 2012-06-20.{{cite web}}: CS1 maint: numeric names: authors list (link)
  2. ^ GCC documentation. "Built-in Functions Specific to Particular Target Machines". Free Software Foundation. Retrieved 2012-06-20.
  3. ^ MSDN (VS2012). "Intrinsics and Inline Assembly". Microsoft. Retrieved 2010-04-16.{{cite web}}: CS1 maint: numeric names: authors list (link)
  4. ^ MSDN (VS2010). "Intrinsics and Inline Assembly". Microsoft. Retrieved 2011-09-28.{{cite web}}: CS1 maint: numeric names: authors list (link)
  5. ^ MSDN (VS2008). "Intrinsics and Inline Assembly". Microsoft. Retrieved 2011-09-28.{{cite web}}: CS1 maint: numeric names: authors list (link)
  6. ^ MSDN (VS2005). "Intrinsics and Inline Assembly". Microsoft. Retrieved 2011-09-28.{{cite web}}: CS1 maint: numeric names: authors list (link)
  7. ^ "sony open sources vector math simd libraries".
  8. ^ Mok, Kris (25 February 2013). "Intrinsic Methods in HotSpot VM". Slideshare. Retrieved 20 December 2014.