__builtin_clz(); __builtin_ctz(); __builtin_popcount();
16lz
2021-01-24
GCC provides quite a lot of builtin functions. These functions are part of standard C offered by the compiler and may come in various variants as per the gcc. These are also termed as hardware specific functions which are internally implemented in assembly or we can say machine instructions, with wide usage in low level programming and are generally target optimized.
These functions are prefixed with “__builtin”. There are a huge lot of builtin functions by GCC, however here in this article, we shall be learning about two such builtin functions.
Code:
This builtin method is provided by GCC to count the number of leading zero’s in variable.
The Syntax:
Code:
Taking for example, lets take a number 16. An int takes 4 bytes in gcc. Its binary representation is
Code:
This builtin method by GCC determines the count of trailing zero in the binary representation of a number.
The Syntax:
Code:
Taking for example, lets take the same number 16. Again, an int takes 4 bytes in gcc. Its binary representation is
Code:
This builtin method by GCC determines the number of one’s in the binary representation of a number.
The Syntax:
Code:
Taking for example, lets take the same number 16. Again, an int takes 4 bytes in gcc. Its binary representation is
Code:
Here is a complete example program which demonstrates the use of these builtin methods.
Code:
Code:
These functions are prefixed with “__builtin”. There are a huge lot of builtin functions by GCC, however here in this article, we shall be learning about two such builtin functions.
Code:
__builtin_clz();__builtin_ctz();__builtin_popcount();Lets learn more about these methods.
Function __builtin_clz
This builtin method is provided by GCC to count the number of leading zero’s in variable.
The Syntax:
Code:
int __builtin_clz (unsigned int x)It takes the input parameter as a number for which the the count of leading zero’s is to be determined. It returns the count of leading zero’s as expected.
Taking for example, lets take a number 16. An int takes 4 bytes in gcc. Its binary representation is
Code:
00000000 00000000 00000000 00010000Counting the number of leading zero’s is 27 which should be our result for this case..
Function __builtin_ctz
This builtin method by GCC determines the count of trailing zero in the binary representation of a number.
The Syntax:
Code:
int __builtin_ctz (unsigned int x)The input parameter is the number for which the the count of trailing zero’s is to be determined. It returns the count of trailing zero’s as expected.
Taking for example, lets take the same number 16. Again, an int takes 4 bytes in gcc. Its binary representation is
Code:
00000000 00000000 00000000 00010000Counting the number of trailing zero’s is 4 which should be our result for this case..
Function __builtin_popcount
This builtin method by GCC determines the number of one’s in the binary representation of a number.
The Syntax:
Code:
int __builtin_popcount (unsigned int x)The input parameter is the number for which the the number of 1’s is to be determined. It returns the count of set bits as expected..
Taking for example, lets take the same number 16. Again, an int takes 4 bytes in gcc. Its binary representation is
Code:
00000000 00000000 00000000 00010000Counting the number of one’s is just 1, which should be our result for this case..
Complete Example
Here is a complete example program which demonstrates the use of these builtin methods.
Code:
#includeOn running the above example program, here is our output:#include int main(){ int num = 16; int clz = 0; int ctz = 0; int ones = 0; clz = __builtin_clz(num); printf("Number of leading zero's in %d is %d\n", num, clz); clz = __builtin_clz(-num); printf("Number of leading zero's in %d is %d\n", -num, clz); ctz = __builtin_ctz(num); printf("Number of trailing zero's in %d is %d\n", num, ctz); ones = __builtin_popcount(num); printf("Number of one's in %d is %d\n", num, ones); return 0;}
Code:
Number of leading zero's in 16 is 27Number of leading zero's in -16 is 0Number of trailing zero's in 16 is 4Number of one's in 16 is 1
Great! it works fine as per our understanding. Note, it even worked fine for negative numbers which are stored as two’s complement.
vc test code(32位)
// ConsoleApplication1.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include using namespace std;__inline int __builtin_clz(int v){ if (v == 0) return 31; __asm { bsr ecx, dword ptr[v]; mov eax, 1Fh; sub eax, ecx; }}__inline int __builtin_ctz(int v){ int pos; if (v == 0) return 0; __asm { bsf eax, dword ptr[v]; }}int main(){ // clz printf("__builtin_clz:\n"); for (int i = 0; i < 32; i++) { int v = 1 << i; bitset<32> b(v); printf("%12u(%s): %2d %s \n", v, b.to_string().c_str(), __builtin_clz(v), __builtin_clz(v) == 31 - i ? "OK" : "Err"); } printf("\n"); // ctz printf("__builtin_ctz:\n"); for (int i = 0; i < 32; i++) { int v = 1 << i; bitset<32> b(v); printf("%12u(%s): %2d %s \n", v, b.to_string().c_str(), __builtin_ctz(v), __builtin_ctz(v) == i ? "OK" : "Err"); } return 0;}
更多相关文章
- android一键分享功能不使用任何第三方sdk
- [Android]-图片JNI(C++\Java)高斯模糊的实现与比较
- Android快速批量多渠道包的“蛋生”
- Android(安卓)out of memory 彻底解决Android因加载多个大图引起
- android handler线程原理详详解
- 安卓手机卡慢的原因,你真的想知道么?
- 微软手握的 Android(安卓)专利一年可捞 4.44 亿美刀
- Android(安卓)EditText样式
- Android中的GalleryView实例演示-周末福利有美女图