__builtin_clz(); __builtin_ctz(); __builtin_popcount();
来源:互联网 发布:win7开启端口 编辑:程序博客网 时间:2024/05/21 18:37
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.
Lets learn more about these methods.
This builtin method is provided by GCC to count the number of leading zero’s in variable.
The Syntax:
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
Counting the number of leading zero’s is 27 which should be our result for this case..
This builtin method by GCC determines the count of trailing zero in the binary representation of a number.
The Syntax:
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
Counting the number of trailing zero’s is 4 which should be our result for this case..
This builtin method by GCC determines the number of one’s in the binary representation of a number.
The Syntax:
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
Counting the number of one’s is just 1, which should be our result for this case..
Here is a complete example program which demonstrates the use of these builtin methods.
On running the above example program, here is our output:
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();
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)
Taking for example, lets take a number 16. An int takes 4 bytes in gcc. Its binary representation is
Code:
00000000 00000000 00000000 00010000
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)
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 00010000
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)
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 00010000
Complete Example
Here is a complete example program which demonstrates the use of these builtin methods.
Code:
#include <stdio.h>#include <stdlib.h>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 <bitset>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;}
1 0
- __builtin_clz(); __builtin_ctz(); __builtin_popcount();
- __builtin_popcount()
- Android 事件分发机制总结
- 程序员的那些事儿 -- 皆大欢喜的加薪
- PhoneGap 启动慢 未显示App页面前 黑屏处理解决方案
- Android 去掉导航栏的HOME和RECENT_APP,只保留BACK
- 华信5
- __builtin_clz(); __builtin_ctz(); __builtin_popcount();
- Java面试宝典系列之基础排序算法
- C#序列化
- 《VR入门系列教程》之9---谷歌纸盒
- Hibernate 一对多级联删除
- Ionic项目代码混淆
- 淘宝UWP--自定义图片缓存
- struts.xml使用通配符
- MySQL存储过程中使用动态行转列