# Intrinsic function

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Intrinsic_function
> Markdown URL: https://mediated.wiki/source/Intrinsic_function.md
> Source: https://en.wikipedia.org/wiki/Intrinsic_function
> Source revision: 1352695254
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Function whose implementation is handled specially by the compiler

This article is about compiler intrinsic functions. For X toolkit, see [X Toolkit Intrinsics](/source/X_Toolkit_Intrinsics).

This article needs more citations. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Intrinsic function" – news · newspapers · books · scholar · JSTOR (December 2009) (Learn how and when to remove this message)

In [computer software](/source/Software), in [compiler theory](/source/Compiler), an **intrinsic function**, also called **built-in function** or **builtin function**, is a function ([subroutine](/source/Subroutine)) available for use in a given [programming language](/source/Programming_language) whose implementation is handled specially by the [compiler](/source/Compiler). Typically, it may substitute a sequence of automatically generated [instructions](/source/Instruction_set_architecture) for the original function call, similar to an [inline function](/source/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](/source/Optimizing_compiler), otherwise falling back to a default implementation provided by the language [runtime system](/source/Runtime_system) (environment).

## Vectorization and parallelization

Intrinsic functions are often used to explicitly implement [vectorization](/source/Automatic_vectorization) and [parallelization](/source/Automatic_parallelization) in languages which do not address such constructs. Some [application programming interfaces](/source/Application_programming_interface) (API), for example, [AltiVec](/source/AltiVec) and [OpenMP](/source/OpenMP), use intrinsic functions to declare, respectively, vectorizable and [multiprocessing](/source/Multiprocessing)-aware operations during compiling. The compiler parses the intrinsic functions and converts them into vector math or multiprocessing [object code](/source/Object_code) appropriate for the target [platform](/source/Platform_(computing)). 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](/source/C_(programming_language)) and [C++](/source/C%2B%2B), of Microsoft,[3] Intel,[1] and the [GCC](/source/GNU_Compiler_Collection)[4] implement intrinsics that map directly to the [x86](/source/X86) *single instruction, multiple data* ([SIMD](/source/SIMD)) instructions ([MMX](/source/MMX_(instruction_set)), *[Streaming SIMD Extensions](/source/Streaming_SIMD_Extensions)* (SSE), [SSE2](/source/SSE2), [SSE3](/source/SSE3), [SSSE3](/source/SSSE3), [SSE4](/source/SSE4), [AVX](/source/Advanced_Vector_Extensions), [AVX2](/source/AVX2), [AVX512](/source/AVX512), [FMA](/source/FMA_instruction_set), ...). Intrinsics allow mapping to standard assembly instructions that are not normally accessible through C/C++; e.g. bit scan and parallel operations on [128 bits](/source/Streaming_SIMD_Extensions) and [256 bits](/source/Advanced_Vector_Extensions).

Some C and C++ compilers provide non-portable platform-specific intrinsics. Other intrinsics (such as [GNU](/source/GNU_Compiler_Collection) built-ins) are slightly more abstracted, approximating the abilities of several contemporary platforms, with portable *fall back* implementations on platforms with no appropriate instructions.[5] It's common for C++ libraries, such as *glm* or [Sony](/source/Sony)'s [vector maths libraries](/source/Glossary_of_computer_graphics#V),[6] to achieve portability via [conditional compilation](/source/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](/source/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 [ABI](/source/Application_binary_interface).

#### Examples

The following are examples of signatures of intrinsic functions from Intel's set of intrinsic functions.

 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

[7]

### COBOL

*Intrinsic function Module* for [COBOL](/source/COBOL) was introduced in ANSI INCITS X3.23a-1989 and ISO 1989:1985/Amd 1:1992.

### Java

The [HotSpot](/source/HotSpot) [Java virtual machine](/source/Java_virtual_machine)'s (JVM) [just-in-time compiler](/source/Just-in-time_compilation) also has intrinsics for specific Java APIs.[8] Hotspot intrinsics are standard Java APIs which may have one or more optimized implementation on some platforms.

### PL/I

ANSI/ISO [PL/I](/source/PL%2FI) defines nearly 90 builtin functions.[9] These are conventionally grouped as follows:[10]: 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. ^ [***a***](#cite_ref-IntelCompilerIntrinsics_1-0) [***b***](#cite_ref-IntelCompilerIntrinsics_1-1) ["Intel® C++ Compiler 19.1 Developer Guide and Reference"](https://software.intel.com/en-us/cpp-compiler-developer-guide-and-reference-intrinsics). *Intel® C++ Compiler Documentation*. 16 December 2019. Retrieved 2020-01-17.

1. **[^](#cite_ref-2)** The Clang Team (2020). ["Clang Language Extensions"](https://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume). *Clang 11 documentation*. Retrieved 2020-01-17. Builtin Functions

1. **[^](#cite_ref-3)** [MSDN](/source/Microsoft_Developer_Network). ["Compiler Intrinsics"](http://msdn.Microsoft.com/en-us/library/26td21ds). [Microsoft](/source/Microsoft). Retrieved 2012-06-20.

1. **[^](#cite_ref-4)** GCC documentation. ["Built-in Functions Specific to Particular Target Machines"](https://gcc.gnu.org/onlinedocs/gcc/Target-Builtins.html). [Free Software Foundation](/source/Free_Software_Foundation). Retrieved 2012-06-20.

1. **[^](#cite_ref-5)** ["Vector Extensions"](https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html). *Using the GNU Compiler Collection (GCC)*. Retrieved 2020-01-16.

1. **[^](#cite_ref-6)** ["Sony open sources Vector Math and SIMD math libraries (Cell PPU/SPU/other platforms)"](https://web.archive.org/web/20160624183718/https://forum.beyond3d.com/threads/sony-open-sources-vector-math-and-simd-math-libraries-cell-ppu-spu-other-platforms.38677/). *Beyond3D Forum*. Archived from [the original](https://forum.beyond3d.com/threads/sony-open-sources-vector-math-and-simd-math-libraries-cell-ppu-spu-other-platforms.38677/) on 2016-06-24. Retrieved 2020-01-17.

1. **[^](#cite_ref-7)** [Intel Intrinsics](https://software.intel.com/sites/landingpage/IntrinsicsGuide/)

1. **[^](#cite_ref-8)** Mok, Kris (25 February 2013). ["Intrinsic Methods in HotSpot VM"](https://www.slideshare.net/RednaxelaFX/green-teajug-hotspotintrinsics02232013). Slideshare. Retrieved 2014-12-20.

1. **[^](#cite_ref-ANSI_9-0)** ANSI X3 Committee (1976). *American National Standard programming language PL/I*.{{[cite book](https://en.wikipedia.org/wiki/Template:Cite_book)}}: CS1 maint: numeric names: authors list ([link](https://en.wikipedia.org/wiki/Category:CS1_maint:_numeric_names:_authors_list))

1. **[^](#cite_ref-PLI1.1_10-0)** IBM Corporation (1995). *IBM PL/I for MVS & VM Language Reference*.

## External links

- [Intel Intrinsics Guide](https://software.intel.com/sites/landingpage/IntrinsicsGuide/)

- [Using milicode routines](https://www-01.ibm.com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.alangref/idalangref_milicode_routines.htm), IBM AIX 6.1 documentation

---
Adapted from the Wikipedia article [Intrinsic function](https://en.wikipedia.org/wiki/Intrinsic_function) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Intrinsic_function?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
