
422 views
Builtin functions of GCC compiler in C
The GCC (GNU Compiler Collection) is a widely used compiler for the C programming language. It provides a range of built-in functions to perform various tasks efficiently and is not specific to C; many of these functions are available in C++ and other languages supported by GCC. Below is a list of some common GCC built-in functions:
- Built-in Math Functions:
__builtin_sqrt(x): Compute the square root ofx.__builtin_powi(x, y): Computexraised to the power ofy(integer).__builtin_fabs(x): Compute the absolute value ofx.__builtin_fmod(x, y): Compute the remainder ofxdivided byy.__builtin_sin(x): Compute the sine ofx.__builtin_cos(x): Compute the cosine ofx.__builtin_tan(x): Compute the tangent ofx.
- Bit Manipulation Functions:
__builtin_popcount(x): Count the number of set bits (1s) inx.__builtin_parity(x): Check the parity (even or odd) of set bits inx.__builtin_clz(x): Count leading zeros inx.__builtin_ctz(x): Count trailing zeros inx.
- Miscellaneous Functions:
__builtin_expect(exp, c): Provide a hint to the compiler about the expected value ofexp, improving branch prediction.__builtin_unreachable(): Indicate that a certain point in the code should never be reached, helping the compiler optimize.__builtin_prefetch(addr): Prefetch data fromaddrinto the cache to reduce memory access latency.__builtin_memcpy(dest, src, n): Efficient memory copy function, optimized by the compiler.
- Type Conversion Functions:
__builtin_types_compatible_p(type1, type2): Check iftype1andtype2are compatible types.__builtin_convertvector(expr, type): Convert a vector expression to another vector type.
- Atomic Built-in Functions (for atomic operations):
__atomic_load(ptr, memorder): Load the value pointed to byptratomically.__atomic_store(ptr, value, memorder): Store the valuevalueatomically toptr.__atomic_add_fetch(ptr, value, memorder): Atomically addvalueto the value atptrand return the result.__atomic_sub_fetch(ptr, value, memorder): Atomically subtractvaluefrom the value atptrand return the result.
- Built-in Vector Functions (SSE/AVX instructions):
- Various built-in functions for vector operations, such as
_mm_add_ps,_mm_mul_pd, etc., for SIMD (Single Instruction, Multiple Data) operations.
- Built-in Goto Label:
__builtin_apply_args(): Used in conjunction with__builtin_apply, a GCC-specific way of implementing computed gotos.
It’s important to note that these functions are compiler-specific and might not be available in other C compilers. They are typically used for performance optimizations or for tasks that require low-level hardware access. The use of these functions should be done with care and consideration for portability, as code using GCC built-ins may not compile with other compilers.