Cover Image for Builtin functions of GCC compiler in C
118 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:

  1. Built-in Math Functions:
  • __builtin_sqrt(x): Compute the square root of x.
  • __builtin_powi(x, y): Compute x raised to the power of y (integer).
  • __builtin_fabs(x): Compute the absolute value of x.
  • __builtin_fmod(x, y): Compute the remainder of x divided by y.
  • __builtin_sin(x): Compute the sine of x.
  • __builtin_cos(x): Compute the cosine of x.
  • __builtin_tan(x): Compute the tangent of x.
  1. Bit Manipulation Functions:
  • __builtin_popcount(x): Count the number of set bits (1s) in x.
  • __builtin_parity(x): Check the parity (even or odd) of set bits in x.
  • __builtin_clz(x): Count leading zeros in x.
  • __builtin_ctz(x): Count trailing zeros in x.
  1. Miscellaneous Functions:
  • __builtin_expect(exp, c): Provide a hint to the compiler about the expected value of exp, 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 from addr into the cache to reduce memory access latency.
  • __builtin_memcpy(dest, src, n): Efficient memory copy function, optimized by the compiler.
  1. Type Conversion Functions:
  • __builtin_types_compatible_p(type1, type2): Check if type1 and type2 are compatible types.
  • __builtin_convertvector(expr, type): Convert a vector expression to another vector type.
  1. Atomic Built-in Functions (for atomic operations):
  • __atomic_load(ptr, memorder): Load the value pointed to by ptr atomically.
  • __atomic_store(ptr, value, memorder): Store the value value atomically to ptr.
  • __atomic_add_fetch(ptr, value, memorder): Atomically add value to the value at ptr and return the result.
  • __atomic_sub_fetch(ptr, value, memorder): Atomically subtract value from the value at ptr and return the result.
  1. 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.
  1. 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.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS