gcc的__builtin_函数(注意前面是两个下划线)

说明:

GCC provides a large number of built-in functions other than the ones mentioned above. Some of these are for internal use in the processing of exceptions or variable-length argument lists and will not be documented here because they may change from time to time; we do not recommend general use of these functions.

GCC includes built-in versions of many of the functions in the standard C library. The versions prefixed with __builtin_ will always be treated as having the same meaning as the C library function even if you specify the-fno-builtinoption. (see C Dialect Options) Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function will be emitted.

— Built-in Function: int __builtin_ffs (unsigned int x)

Returns one plus the index of the least significant 1-bit of x, or
if x is zero, returns zero.

— Built-in Function: int __builtin_clz (unsigned int x)

Returns the number of leading 0-bits in x, starting at the most
significant bit position. If x is 0, the result is undefined.

— Built-in Function: int __builtin_ctz (unsigned int x)

Returns the number of trailing 0-bits in x, starting at the least
significant bit position. If x is 0, the result is undefined.

— Built-in Function: int __builtin_popcount (unsigned int x)

Returns the number of 1-bits in x.

— Built-in Function: int __builtin_parity (unsigned int x)

Returns the parity of x, i.e. the number of 1-bits in x
modulo 2.

(以上内容来源见参考哦)

1.__builtin_parity(unsigned int x)

统计一个数二进制表示中1的个数是偶数还是奇数

2.__builtin_popcount(unsigned int x)

统计一个数的二进制表示中1的个数

3.__builtin_ffs(unsigned int x)

找出一个数的二进制表示中从末尾开始到遇见第一个1的的位置

4.__builtin_clz(unsigned int x)

返回一个数二进制表示中前导零的数目

5.__builtin_ctz(unsigned int x)

返回一个数二进制表示中尾零的数目

试验代码如下:

 1 #include <iostream>
 2 #include <map>
 3
 4 #define max_n 200005
 5 using namespace std;
 6 map<int,int> mp;
 7 long long a[max_n];
 8 int n;
 9 int main()
10 {
11     cout << __builtin_popcount(3) << endl; //3:11 output:2
12     cout << __builtin_popcount(7) << endl; //7:111 output:3
13
14     cout << __builtin_parity(3) << endl; //output:0
15     cout << __builtin_parity(7) << endl; //output:1
16
17     cout << __builtin_ffs(3) << endl; //output:1
18     cout << __builtin_ffs(10) << endl;//10:1010 output:2
19
20     cout << __builtin_ctz(3) << endl; //output:0
21     cout << __builtin_ctz(10) << endl;//output:1
22
23     cout << __builtin_clz(3) << endl;//output:30
24     cout << __builtin_clz(10) << endl;//output:28
25     return 0;
26 }

更多内置函数参见:http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Other-Builtins.html

原文地址:https://www.cnblogs.com/zhanhonhao/p/11221354.html

时间: 2024-08-30 15:16:33

gcc的__builtin_函数(注意前面是两个下划线)的相关文章

【转】gcc的__builtin_函数介绍

转自:http://blog.csdn.net/jasonchen_gbd/article/details/44948523 GCC提供了一系列的builtin函数,可以实现一些简单快捷的功能来方便程序编写,另外,很多builtin函数可用来优化编译结果.这些函数以"__builtin_"作为函数名前缀.很多C标准库函数都有与之对应的GCC builtin函数,例如strcpy()有对应的__builtin_strcpy()内建函数.下面就介绍一些builtin函数及其作用: __bu

python中有两个下划线__的是内置方法,一个下划线_或者没有下划线的可能是属性,也可能是方法,也可能是类名

>>> dir(__builtins__)['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarn

关于gcc内置函数和c隐式函数声明的认识以及一些推测

最近在看APUE,不愧是经典,看一点就收获一点.但是感觉有些东西还是没说清楚,需要自己动手验证一下,结果发现需要用gcc,就了解一下. 有时候,你在代码里面引用了一个函数但是没有包含相关的头文件,这个时候gcc报的错误比较诡异,一般是这样:[math.c:6:25: 警告:隐式声明与内建函数‘sin’不兼容 [默认启用]].这个错误网上大量博客都在说需要包含XXX.h文件,但是没有人解释这个错误信息为什么这样表达.什么是隐式声明,什么是内建函数,我就纠结了. 隐式声明函数的概念网上有相关的资料,

VC和gcc在保证函数static变量线程安全性上的区别

VC和gcc不同,不能保证静态变量的线程安全性.这就给我们的程序带来了很大的安全隐患和诸多不便.这一点应该引起我们的重视!尤其是在构造函数耗时比较长的时候,很可能给程序带来意想不到的结果.本文从测试代码开始,逐步分析原理,最后给出解决方案. 多线程状态下,VC不能保证在使用函数的静态变量的时候,它的构造函数已经被执行完毕,下面是一段测试代码: class TestStatic { public: TestStatic() { Sleep(1000*10); m_num = 999; } publ

GCC中初始化函数是如何被处理的?

本文译至: http://gcc.gnu.org/onlinedocs/gccint/Initialization.html 如我们所知,在GCC通过给代码追加__attribute__((constructor))和__attribute__((destructor))的方式可以追加初始函数和终止函数, 这篇文章介绍了GCC内部是如何实现上述处理的. 简单的说,就是在最经常的情况下,初始函数会被追加到.ctor section中,.init会调用对应的函数处理这些初始函数.终止情况类似. --

编程题:指针变量作函数参数,将两个整数按由大到小的顺序输出。

分析:通过指针变量作函数参数,无需返回值和全局变量,主调函数就可以使用被调用函数改变的值. #include<stdio.h> void swap(int *p1,int *p2) { int p; p=*p1; *p1=*p2; *p2=p; } void main() { int a=3,b=4; int *ptr1,*ptr2; ptr1=&a;ptr2=&b; if(a<b) swap(ptr1,ptr2); printf("%d,%d\n",

学习python的第十五天(函数的装饰器,两层装饰器和三层装饰器)

06.01自我总结 一.装饰器 1.函数装饰圈的定义 函数装饰器:一种装饰函数的函数 2.个人理解两层函数装饰器 两层函数装饰器个人觉得他其实就是把需要装饰的函数名丢入形参,然后用一个嵌套的函数对其头尾进行添加程序,但是不能减少他的程序内容,他的原来程序不变只能增不能减少,然后返回装饰好的子函数,再全局定义一个变量名与要装饰的函数名相同名字,并且将装饰后的函数调用赋予改变量. 1.简单的例子(无参函数) 如 #有个函数f1 def f1(): print('nick machachong') #

【转】关于python中带下划线的变量和函数 的意义

http://www.blogjava.net/lincode/archive/2011/02/02/343859.html 总结: 变量: 1.  前带_的变量:  标明是一个私有变量, 只用于标明, 外部类还是可以访问到这个变量 2.  前带两个_ ,后带两个_ 的变量:  标明是内置变量, 3.  大写加下划线的变量:  标明是 不会发生改变的全局变量 函数: 1. 前带_的变量: 标明是一个私有函数, 只用于标明, 2.  前带两个_ ,后带两个_ 的函数:  标明是特殊函数 Pytho

excel两个下拉框相互关联

我有两列数据,录入了所有人的信息,一个是姓名,一个是编号 现在我想再做两个下拉框,一个是选择姓名的,一个是选择编号的,我希望我选择姓名后,编号自动对应到这个人的,如果我选择编号,那么姓名也自动对应过来.请教各位excel的达人们. 这个用VLOOKUP函数做更方便 假设是在A列写入了姓名,B列写入了编号 在C2单元格设置了下拉菜单可以选择姓名, 则在D2单元格写入公式 =VLOOKUP(C2,A:B,2,) 原文 http://zhidao.baidu.com/question/14945914