×

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows:

Function_ built in_popcount (x)

The number of 1s in data of the integer type can be counted using this built-in function. Let's look at a builtin _popcount() function sample.

Example

if x = 4
100 is the binary value of 4


Output:

The number of ones in binary value 4 is 1.

Program

#include<iostream>
using namespace std;
int main() {
   int n = 13; // 1101 is binary
   cout << "Count of 1s in binary of "<< n <<" is " << __builtin_popcount(n);
   return 0;
}

Output

Builtin functions of GCC compiler

Function_builtin_parity (x)

The parity of an integer can be checked using this built-in function. If the number shows even parity, it results false, otherwise it results true. Let's look at a _builtin parity() function sample.

Example:

if x = 7
As binary number of 7 is 111 it contains odd number of 1’s.

Output:

1 is 7’s parity

Program:

#include<iostream>
using namespace std;
int main () {
   int n = 13; //1101 is binary
   cout << "Parity of "<< n <<" is " << __builtin_parity (n);
   return 0;
}

Output:

Builtin functions of GCC compiler

Function_builtin_clz (x)

The leading zeros of an integer are counted using this built-in function. Let's look at a _builtin clz() function sample.

Example:

b = 16
00000000 00000000 00000000 00010000is the binary form of the number 16

Output:  

The number obtained is 27

Program:

#include<iostream>
using namespace std;
int main () {
   int n = 13; //1101 is binary
   //0000 0000 0000 0000 0000 0000 0000 1101 (32bit integer)
   cout << "Leading zero count of "<< n <<" is " << __builtin_clz(n);
   return 0;
}

Output:

Builtin functions of GCC compiler

Function_builtin_ctz (x)

The trailing zeros of an integer are counted using this built-in function. Count Trailing Zeros is what the ctz stands for. Let's look at a _builtin ctz() function sample.

Example

b = 16
 00000000 00000000 00000000 00010000 is the binary form of number 16

Output:

the result obtained is ctz which equals to 4

Program:

#include <stdio.h>
intmain () 
{ 
    intn = 16; 
    printf ("Count of zeros from last to first "Occurrence of one is %d", 
           __builtin_ctz(n)); 
    return0; 
}

Output:

Builtin functions of GCC compiler
// Code to find the GCC compiler's built-in functions
#include <stdio.h>
#include <stdlib.h>
  
intmain () 
{ 
    intnum = 4; 
    intclz = 0; 
    intctz = 0; 
    intpop = 0; 
    intparity = 0; 
  
    pop = __builtin_popcount(num); 
    printf ("Number of one's in %d is %d
", num, pop); 
  
    parity = __builtin_parity(num); 
    printf ("Parity of %d is %d
", num, parity); 
  
    clz = __builtin_clz(num); 
    printf("Number of leading zero's in %d is %d
", num, clz); 
  
    // It does not work for signed values
    clz = __builtin_clz(-num); 
    printf ("Number of leading zero's in %d is %d
", -num, clz); 
  
    ctz = __builtin_ctz(num); 
    printf ("Number of trailing zero's in %d is %d
", num, ctz); 
  
    return0; 
}

Output:

Builtin functions of GCC compiler

GCC also provides other built_in_functions

In addition to the above-mentioned built-in features, GCC offers a sizable number of others. We do not advise using these routines for general use; some of them are intended for internal usage in the processing of exceptions or variable-length argument lists and are not described here since they may change at any time.

The remaining functions are offered to aid with optimization.

Except for built-ins that expand to library calls or that have equivalents in the standard C library (described below), all built-in functions in GCC are always expanded inline, negating the need for corresponding entry points, and making it impossible to determine their address. A compile-time error occurs if you try to utilise them in an expression other than a function call.

Many of the functions in the common C library have built-in counterparts in GCC. There are two versions of these functions: one with names that begin with the __builtin_ prefix and one without. Even if you set the -fno-builtin option, both forms have the same type (including prototype), address (when their address is taken), and meaning as the C library functions. The library function is called when one of these functions cannot be optimised because many of them are only optimised in specific circumstances.


Related Topics

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.