函数使用
GCC/Clang 内置函数速查汇总表
一、位操作内置函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__builtin_clz(x) |
unsigned int |
int |
返回前导零个数(x=0时未定义) |
__builtin_clzl(x) |
unsigned long |
int |
返回前导零个数(x=0时未定义) |
__builtin_clzll(x) |
unsigned long long |
int |
返回前导零个数(x=0时未定义) |
__builtin_ctz(x) |
unsigned int |
int |
返回末尾零个数(x=0时未定义) |
__builtin_ctzl(x) |
unsigned long |
int |
返回末尾零个数(x=0时未定义) |
__builtin_ctzll(x) |
unsigned long long |
int |
返回末尾零个数(x=0时未定义) |
__builtin_popcount(x) |
unsigned int |
int |
返回二进制中1的个数 |
__builtin_popcountl(x) |
unsigned long |
int |
返回二进制中1的个数 |
__builtin_popcountll(x) |
unsigned long long |
int |
返回二进制中1的个数 |
__builtin_parity(x) |
unsigned int |
int |
返回1的个数的奇偶性(0偶1奇) |
__builtin_parityl(x) |
unsigned long |
int |
返回1的个数的奇偶性 |
__builtin_parityll(x) |
unsigned long long |
int |
返回1的个数的奇偶性 |
__builtin_ffs(x) |
unsigned int |
int |
返回最后一个1的位置(从1开始),0返回0 |
__builtin_ffsl(x) |
unsigned long |
int |
同上 |
__builtin_ffsll(x) |
unsigned long long |
int |
同上 |
__builtin_bswap16(x) |
unsigned short |
unsigned short |
16位字节交换 |
__builtin_bswap32(x) |
unsigned int |
unsigned int |
32位字节交换 |
__builtin_bswap64(x) |
unsigned long long |
unsigned long long |
64位字节交换 |
二、算术与数学内置函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__builtin_add_overflow(a,b,&res) |
T, T, T* |
bool |
a+b是否溢出,结果存入res |
__builtin_sub_overflow(a,b,&res) |
T, T, T* |
bool |
a-b是否溢出 |
__builtin_mul_overflow(a,b,&res) |
T, T, T* |
bool |
a*b是否溢出 |
__builtin_sadd_overflow(a,b,&res) |
int,int,int* |
bool |
有符号加法溢出检测 |
__builtin_saddl_overflow(a,b,&res) |
long,long,long* |
bool |
有符号long加法溢出检测 |
__builtin_saddll_overflow(a,b,&res) |
long long,long long,long long* |
bool |
有符号long long加法溢出检测 |
__builtin_uadd_overflow(a,b,&res) |
unsigned,unsigned,unsigned* |
bool |
无符号加法溢出检测 |
__builtin_uaddl_overflow(a,b,&res) |
unsigned long,unsigned long,unsigned long* |
bool |
无符号long加法溢出检测 |
__builtin_uaddll_overflow(a,b,&res) |
unsigned long long,unsigned long long,unsigned long long* |
bool |
无符号long long加法溢出检测 |
__builtin_ssub_overflow(a,b,&res) |
int,int,int* |
bool |
有符号减法溢出检测 |
__builtin_smul_overflow(a,b,&res) |
int,int,int* |
bool |
有符号乘法溢出检测 |
__builtin_sqrt(x) |
double |
double |
平方根 |
__builtin_sqrtf(x) |
float |
float |
平方根(单精度) |
__builtin_sqrtl(x) |
long double |
long double |
平方根(长双精度) |
__builtin_pow(x,y) |
double,double |
double |
幂运算 |
__builtin_powf(x,y) |
float,float |
float |
幂运算(单精度) |
__builtin_powl(x,y) |
long double,long double |
long double |
幂运算(长双精度) |
__builtin_sin(x) |
double |
double |
正弦 |
__builtin_cos(x) |
double |
double |
余弦 |
__builtin_tan(x) |
double |
double |
正切 |
__builtin_log(x) |
double |
double |
自然对数 |
__builtin_log10(x) |
double |
double |
以10为底的对数 |
__builtin_exp(x) |
double |
double |
指数函数e^x |
__builtin_fabs(x) |
double |
double |
绝对值 |
__builtin_fabsf(x) |
float |
float |
绝对值(单精度) |
__builtin_fabsl(x) |
long double |
long double |
绝对值(长双精度) |
三、内存操作内置函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__builtin_memset(dest,val,len) |
void*, int, size_t |
void* |
设置内存,返回dest |
__builtin_memcpy(dest,src,len) |
void*, const void*, size_t |
void* |
内存拷贝 |
__builtin_memmove(dest,src,len) |
void*, const void*, size_t |
void* |
内存移动(处理重叠) |
__builtin_memcmp(ptr1,ptr2,len) |
const void*, const void*, size_t |
int |
内存比较 |
四、控制流内置函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__builtin_expect(exp,c) |
long, long |
long |
提示编译器exp很可能等于c |
__builtin_expect_with_probability(exp,c,p) |
long, long, double |
long |
带概率的期望提示 |
__builtin_prefetch(addr,rw,locality) |
const void*, int, int |
void |
预取数据到缓存 |
__builtin_unreachable() |
无 | void |
标记代码不可达 |
__builtin_assume(exp) |
bool |
void |
告诉编译器exp始终为真 |
__builtin_trap() |
无 | void |
触发陷阱,程序中止 |
__builtin_abort() |
无 | void |
调用abort()函数 |
五、类型操作内置函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__builtin_types_compatible_p(T1,T2) |
type, type |
int |
判断两个类型是否兼容 |
__builtin_choose_expr(exp,e1,e2) |
int, expression, expression |
expression |
编译期选择表达式 |
__builtin_isnan(x) |
double |
int |
判断是否为NaN |
__builtin_isinf(x) |
double |
int |
判断是否为无穷大 |
__builtin_isfinite(x) |
double |
int |
判断是否为有限值 |
__builtin_isnormal(x) |
double |
int |
判断是否为正规数 |
__builtin_signbit(x) |
double |
int |
判断符号位 |
__builtin_constant_p(exp) |
expression |
int |
判断表达式是否为编译期常量 |
六、原子操作内置函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__sync_fetch_and_add(ptr,val) |
T*, T |
T |
先获取后加,返回旧值 |
__sync_fetch_and_sub(ptr,val) |
T*, T |
T |
先获取后减 |
__sync_fetch_and_or(ptr,val) |
T*, T |
T |
先获取后或 |
__sync_fetch_and_and(ptr,val) |
T*, T |
T |
先获取后与 |
__sync_fetch_and_xor(ptr,val) |
T*, T |
T |
先获取后异或 |
__sync_fetch_and_nand(ptr,val) |
T*, T |
T |
先获取后与非 |
__sync_add_and_fetch(ptr,val) |
T*, T |
T |
先加后获取,返回新值 |
__sync_sub_and_fetch(ptr,val) |
T*, T |
T |
先减后获取 |
__sync_or_and_fetch(ptr,val) |
T*, T |
T |
先或后获取 |
__sync_and_and_fetch(ptr,val) |
T*, T |
T |
先与后获取 |
__sync_xor_and_fetch(ptr,val) |
T*, T |
T |
先异或后获取 |
__sync_nand_and_fetch(ptr,val) |
T*, T |
T |
先与非后获取 |
__sync_val_compare_and_swap(ptr,old,new) |
T*, T, T |
T |
比较交换,返回旧值 |
__sync_bool_compare_and_swap(ptr,old,new) |
T*, T, T |
bool |
比较交换,返回是否成功 |
__sync_lock_test_and_set(ptr,val) |
T*, T |
T |
测试并设置锁 |
__sync_lock_release(ptr) |
T* |
void |
释放锁 |
__sync_synchronize() |
无 | void |
完整内存屏障 |
七、对象生命周期内置函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__builtin_malloc(size) |
size_t |
void* |
分配内存 |
__builtin_free(ptr) |
void* |
void |
释放内存 |
__builtin_alloca(size) |
size_t |
void* |
栈上分配内存 |
__builtin_offsetof(type,member) |
type, member |
size_t |
成员偏移量 |
__builtin_addressof(ref) |
T& |
T* |
取地址(与&类似) |
八、调试与性能分析内置函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__builtin_return_address(level) |
unsigned int |
void* |
获取返回地址 |
__builtin_frame_address(level) |
unsigned int |
void* |
获取栈帧地址 |
__builtin_extract_return_addr(addr) |
void* |
void* |
提取返回地址 |
__builtin_frob_return_addr(addr) |
void* |
void* |
调整返回地址 |
__builtin___clear_cache(start,end) |
char*, char* |
void |
清除指令缓存 |
九、平台特定内置函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__builtin_cpu_supports(feature) |
const char* |
int |
检测CPU是否支持某特性 |
__builtin_cpu_init() |
无 | void |
初始化CPU信息 |
__builtin_cpu_is(type) |
const char* |
int |
检测CPU类型 |
十、编译期常量函数
| 函数 | 参数类型 | 返回类型 | 功能说明 |
|---|---|---|---|
__builtin_constant_p(exp) |
expression |
int |
编译期常量判断 |
__builtin_strlen(str) |
const char* |
size_t |
编译期字符串长度 |
__builtin_strcmp(s1,s2) |
const char*, const char* |
int |
编译期字符串比较 |
功能分类速查索引
| 使用场景 | 推荐函数 |
|---|---|
| 位运算优化 | __builtin_popcount, __builtin_clz, __builtin_ctz, __builtin_ffs |
| 安全算术运算 | __builtin_add_overflow, __builtin_mul_overflow |
| 热点代码分支优化 | __builtin_expect, __builtin_prefetch |
| 无锁多线程编程 | __sync_fetch_and_add, __sync_bool_compare_and_swap |
| 内存操作优化 | __builtin_memcpy, __builtin_memset, __builtin_alloca |
| 数学计算 | __builtin_sqrt, __builtin_pow, __builtin_sin, __builtin_cos |
| 堆栈跟踪调试 | __builtin_return_address, __builtin_frame_address |
| CPU特性检测 | __builtin_cpu_supports, __builtin_cpu_is |
| 编译期元编程 | __builtin_constant_p, __builtin_types_compatible_p |
| 浮点数检测 | __builtin_isnan, __builtin_isinf, __builtin_signbit |
GCC/Clang 与 C++20 标准对照
| GCC内置函数 | C++20标准替代 | 头文件 |
|---|---|---|
__builtin_popcount |
std::popcount |
<bit> |
__builtin_clz |
std::countl_zero |
<bit> |
__builtin_ctz |
std::countr_zero |
<bit> |
__builtin_ffs |
std::countr_zero(x) + 1 |
<bit> |
__builtin_parity |
std::popcount(x) & 1 |
<bit> |
__builtin_bswap16/32/64 |
std::byteswap |
<bit> (C++23) |
__sync_fetch_and_add |
std::atomic::fetch_add |
<atomic> |
__sync_bool_compare_and_swap |
std::atomic::compare_exchange_weak |
<atomic> |
__builtin_add_overflow |
std::add_sat |
<numeric> (C++26) |
__builtin_memcpy |
std::memcpy |
<cstring> |
__builtin_unreachable |
std::unreachable |
<utility> (C++23) |
重要注意事项
| 类别 | 说明 |
|---|---|
| 编译器依赖 | 这些函数是 GCC/Clang 特有,MSVC 和 ICC 不完全支持 |
| 未定义行为 | __builtin_clz(0)、__builtin_ctz(0) 是未定义的,需要避免 |
| 可移植性 | 跨平台代码应优先使用 C++20 标准库函数 |
| 优化级别 | 某些内置函数在 -O0 下可能被替换为运行时实现 |
| 类型大小 | 返回 int 的函数结果依赖平台类型大小(32位/64位) |