×

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

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 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.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.