GCC 内建函数

来源:互联网 发布:企业排名优化 编辑:程序博客网 时间:2024/06/04 18:23

__builtin_*形式的gcc内建函数一般是基于不同硬件平台采用专门的硬件指令实现的,因此性能较高。如

int __builtin_popcount (unsigned int x)
Returns the number of 1-bits in x.

int __builtin_popcountl (unsigned long)
Similar to __builtin_popcount, except the argument type is unsigned long.

int __builtin_popcountll (unsigned long long)
Similar to __builtin_popcount, except the argument type is unsigned long long.

在Intel SIMD的sse4_2 中就有 POPCNT and LZCNT 硬件指令,__builtin_popcount就是用POPCNT硬件指令实现的。


以X86平台为例,我们可以通过include <x86intrin.h>使用或者直接使用__builtin_*,下面是x86 intrinsics中提供的popcnt接口代码:
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include/popcntintrin.h
/* Calculate a number of bits set to 1.  */
extern __inline int __attribute__((__gnu_inline__, __always_inline__, __artificial__))
_mm_popcnt_u32 (unsigned int __X)
{
  return __builtin_popcount (__X);
}

调用示例:
#include <stdio.h>
#include <stdlib.h>
#include <x86intrin.h>

int main(int argc, char *argv) {
    //int res = __builtin_popcount(123456);
    int res = _mm_popcnt_u32(123456);


    printf("%d\n", res);


    return 0;
}
gcc -o ctest -msse4.2 ctest.c (直接调用__builtin_popcount不需要指定-msse4.2

0 0
原创粉丝点击