glibc的几个有用的处理二进制位的内置函数
— 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.
Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
返回右起第一个‘1’的位置。
— 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_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.
返回左起第一个‘1’之前0的个数。
— 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_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.
返回右起第一个‘1’之后的0的个数。
— Built-in Function: int __builtin_popcount (unsigned int x)
Returns the number of 1-bits in x.
— Built-in Function: int __builtin_popcount (unsigned int x)
Returns the number of 1-bits in x.
返回‘1’的个数。
— 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.
— 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’的个数的奇偶性。
— Built-in Function: int __builtin_ffsl (unsigned long)
Similar to __builtin_ffs, except the argument type is unsigned long.
— Built-in Function: int __builtin_clzl (unsigned long)
Similar to __builtin_clz, except the argument type is unsigned long.
— Built-in Function: int __builtin_ctzl (unsigned long)
Similar to __builtin_ctz, except the argument type is unsigned long.
— Built-in Function: int __builtin_popcountl (unsigned long)
Similar to __builtin_popcount, except the argument type is unsigned long.
— Built-in Function: int __builtin_parityl (unsigned long)
Similar to __builtin_parity, except the argument type is unsigned long.
— Built-in Function: int __builtin_ffsll (unsigned long long)
Similar to __builtin_ffs, except the argument type is unsigned long long.
— Built-in Function: int __builtin_clzll (unsigned long long)
Similar to __builtin_clz, except the argument type is unsigned long long.
— Built-in Function: int __builtin_ctzll (unsigned long long)
Similar to __builtin_ctz, except the argument type is unsigned long long.
— Built-in Function: int __builtin_popcountll (unsigned long long)
Similar to __builtin_popcount, except the argument type is unsigned long long.
— Built-in Function: int __builtin_parityll (unsigned long long)
Similar to __builtin_parity, except the argument type is unsigned long long.
— Built-in Function: int __builtin_ffsl (unsigned long)
Similar to __builtin_ffs, except the argument type is unsigned long.
— Built-in Function: int __builtin_clzl (unsigned long)
Similar to __builtin_clz, except the argument type is unsigned long.
— Built-in Function: int __builtin_ctzl (unsigned long)
Similar to __builtin_ctz, except the argument type is unsigned long.
— Built-in Function: int __builtin_popcountl (unsigned long)
Similar to __builtin_popcount, except the argument type is unsigned long.
— Built-in Function: int __builtin_parityl (unsigned long)
Similar to __builtin_parity, except the argument type is unsigned long.
— Built-in Function: int __builtin_ffsll (unsigned long long)
Similar to __builtin_ffs, except the argument type is unsigned long long.
— Built-in Function: int __builtin_clzll (unsigned long long)
Similar to __builtin_clz, except the argument type is unsigned long long.
— Built-in Function: int __builtin_ctzll (unsigned long long)
Similar to __builtin_ctz, except the argument type is unsigned long long.
— Built-in Function: int __builtin_popcountll (unsigned long long)
Similar to __builtin_popcount, except the argument type is unsigned long long.
— Built-in Function: int __builtin_parityll (unsigned long long)
Similar to __builtin_parity, except the argument type is unsigned long long.
【实验程序】
#include <stdio.h>
#include <iostream>
#include <bitset>
#include <string>
#include <limits.h>
using namespace std;
uint32_t g_arr[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, UINT_MAX-1, UINT_MAX};
string g_str_func[] = {
"__builtin_ffs",
"__builtin_clz",
"__builtin_ctz",
"__builtin_popcount",
"__builtin_parity"
};
//typedef int (*fp_builtin_t)(unsigned int);
//fp_builtin_t g_func[] = {
//__builtin_ffs,
//__builtin_clz,
//__builtin_ctz,
//__builtin_popcount,
//__builtin_parity
//};
int main()
{
/* for (int k = 0; k < 5; k ++) {
printf("%s:\n", g_str_func[k].c_str());
for (int i = 0; i < 12; i ++) {
int t = g_arr[i];
bitset<32> b(t);
fp_builtin_t fp_func = g_func[k];
printf("%u(%s): %d\n", t, b.to_string().c_str(), fp_func(t));
}
puts("");
}
*/
// ffs
printf("%s:\n", g_str_func[0].c_str());
for (int i = 0; i < 12; i ++) {
int t = g_arr[i];
bitset<32> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_ffs(t));
}
puts("");
// clz
printf("%s:\n", g_str_func[1].c_str());
for (int i = 0; i < 12; i ++) {
int t = g_arr[i];
bitset<32> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_clz(t));
}
puts("");
// ctz
printf("%s:\n", g_str_func[2].c_str());
for (int i = 0; i < 12; i ++) {
int t = g_arr[i];
bitset<32> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_ctz(t));
}
puts("");
// popcount
printf("%s:\n", g_str_func[3].c_str());
for (int i = 0; i < 12; i ++) {
int t = g_arr[i];
bitset<32> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_popcount(t));
}
puts("");
// parity
printf("%s:\n", g_str_func[4].c_str());
for (int i = 0; i < 12; i ++) {
int t = g_arr[i];
bitset<32> b(t);
printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_parity(t));
}
puts("");
return 0;
}
这里存在一个为题,希望知道的朋友能够解释,就是为什么不能用函数指针指向这些内置函数。
【测试结果】
__builtin_ffs: 0(00000000000000000000000000000000): 0 1(00000000000000000000000000000001): 1 2(00000000000000000000000000000010): 2 3(00000000000000000000000000000011): 1 4(00000000000000000000000000000100): 3 5(00000000000000000000000000000101): 1 6(00000000000000000000000000000110): 2 7(00000000000000000000000000000111): 1 8(00000000000000000000000000001000): 4 9(00000000000000000000000000001001): 1 4294967294(11111111111111111111111111111110): 2 4294967295(11111111111111111111111111111111): 1 __builtin_clz: 0(00000000000000000000000000000000): 31 1(00000000000000000000000000000001): 31 2(00000000000000000000000000000010): 30 3(00000000000000000000000000000011): 30 4(00000000000000000000000000000100): 29 5(00000000000000000000000000000101): 29 6(00000000000000000000000000000110): 29 7(00000000000000000000000000000111): 29 8(00000000000000000000000000001000): 28 9(00000000000000000000000000001001): 28 4294967294(11111111111111111111111111111110): 0 4294967295(11111111111111111111111111111111): 0 __builtin_ctz: 0(00000000000000000000000000000000): 0 1(00000000000000000000000000000001): 0 2(00000000000000000000000000000010): 1 3(00000000000000000000000000000011): 0 4(00000000000000000000000000000100): 2 5(00000000000000000000000000000101): 0 6(00000000000000000000000000000110): 1 7(00000000000000000000000000000111): 0 8(00000000000000000000000000001000): 3 9(00000000000000000000000000001001): 0 4294967294(11111111111111111111111111111110): 1 4294967295(11111111111111111111111111111111): 0 __builtin_popcount: 0(00000000000000000000000000000000): 0 1(00000000000000000000000000000001): 1 2(00000000000000000000000000000010): 1 3(00000000000000000000000000000011): 2 4(00000000000000000000000000000100): 1 5(00000000000000000000000000000101): 2 6(00000000000000000000000000000110): 2 7(00000000000000000000000000000111): 3 8(00000000000000000000000000001000): 1 9(00000000000000000000000000001001): 2 4294967294(11111111111111111111111111111110): 31 4294967295(11111111111111111111111111111111): 32 __builtin_parity: 0(00000000000000000000000000000000): 0 1(00000000000000000000000000000001): 1 2(00000000000000000000000000000010): 1 3(00000000000000000000000000000011): 0 4(00000000000000000000000000000100): 1 5(00000000000000000000000000000101): 0 6(00000000000000000000000000000110): 0 7(00000000000000000000000000000111): 1 8(00000000000000000000000000001000): 1 9(00000000000000000000000000001001): 0 4294967294(11111111111111111111111111111110): 1 4294967295(11111111111111111111111111111111): 0 Process returned 0 (0x0) execution time : 2.405 s Press any key to continue.
浙公网安备 33010602011771号