In
computer software
Software consists of computer programs that instruct the Execution (computing), execution of a computer. Software also includes design documents and specifications.
The history of software is closely tied to the development of digital comput ...
, 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
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
whose implementation is handled specially by the
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
. Typically, it may substitute a sequence of automatically generated
instructions for the original function call, similar to an
inline function
In the C (programming language), C and C++ programming languages, an inline function is one qualified with the Keyword (computer programming), keyword inline; this serves two purposes:
# It serves as a compiler directive that suggests (but doe ...
.
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
Mathematical optimization (alternatively spelled ''optimisation'') or mathematical programming is the selection of a best element, with regard to some criteria, from some set of available alternatives. It is generally divided into two subfiel ...
, 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 interface
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software Interface (computing), interface, offering a service to other pieces of software. A document or standard that des ...
s (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.
By programming language
C and C++
Compilers for
C and
C++, of Microsoft,
Intel,
and the
GNU Compiler Collection
The GNU Compiler Collection (GCC) is a collection of compilers from the GNU Project that support various programming languages, Computer architecture, hardware architectures, and operating systems. The Free Software Foundation (FSF) distributes ...
(GCC)
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, ...). Intrinsics allow mapping 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. It is common for C++ libraries, such as ''glm'' or
Sony
is a Japanese multinational conglomerate (company), conglomerate headquartered at Sony City in Minato, Tokyo, Japan. The Sony Group encompasses various businesses, including Sony Corporation (electronics), Sony Semiconductor Solutions (i ...
's
vector maths libraries, 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
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
Java
The
HotSpot Java virtual machine
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally descr ...
's (JVM)
just-in-time compiler also has intrinsics for specific Java APIs.
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.
These are conventionally grouped as follows:
* 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
External links
Intel Intrinsics Guide IBM AIX 6.1 documentation
{{DEFAULTSORT:Intrinsic Function
Compiler construction
Computer programming