— 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.
返回右起第一个‘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.
返回左起第一个‘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.
返回右起第一个‘1’之后的0的个数。 — 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.
返回‘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.
【实验程序】
1 #include2 #include 3 #include 4 #include 5 #include 6 7 using namespace std; 8 9 uint32_t g_arr[12] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, UINT_MAX-1, UINT_MAX};10 11 string g_str_func[] = {12 "__builtin_ffs",13 "__builtin_clz",14 "__builtin_ctz",15 "__builtin_popcount",16 "__builtin_parity"17 };18 19 //typedef int (*fp_builtin_t)(unsigned int);20 21 //fp_builtin_t g_func[] = {22 //__builtin_ffs,23 //__builtin_clz,24 //__builtin_ctz,25 //__builtin_popcount,26 //__builtin_parity27 //};28 29 int main()30 {31 /* for (int k = 0; k < 5; k ++) {32 printf("%s:\n", g_str_func[k].c_str());33 for (int i = 0; i < 12; i ++) {34 int t = g_arr[i];35 bitset<32> b(t);36 fp_builtin_t fp_func = g_func[k];37 printf("%u(%s): %d\n", t, b.to_string().c_str(), fp_func(t));38 }39 40 puts("");41 }42 */43 44 // ffs45 printf("%s:\n", g_str_func[0].c_str());46 for (int i = 0; i < 12; i ++) {47 int t = g_arr[i];48 bitset<32> b(t);49 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_ffs(t));50 }51 puts("");52 53 // clz54 printf("%s:\n", g_str_func[1].c_str());55 for (int i = 0; i < 12; i ++) {56 int t = g_arr[i];57 bitset<32> b(t);58 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_clz(t));59 }60 puts("");61 62 // ctz63 printf("%s:\n", g_str_func[2].c_str());64 for (int i = 0; i < 12; i ++) {65 int t = g_arr[i];66 bitset<32> b(t);67 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_ctz(t));68 }69 puts("");70 71 // popcount72 printf("%s:\n", g_str_func[3].c_str());73 for (int i = 0; i < 12; i ++) {74 int t = g_arr[i];75 bitset<32> b(t);76 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_popcount(t));77 }78 puts("");79 80 // parity81 printf("%s:\n", g_str_func[4].c_str());82 for (int i = 0; i < 12; i ++) {83 int t = g_arr[i];84 bitset<32> b(t);85 printf("%u(%s): %d\n", t, b.to_string().c_str(), __builtin_parity(t));86 }87 puts("");88 return 0;89 }
【测试结果】
1 __builtin_ffs: 2 0(00000000000000000000000000000000): 0 3 1(00000000000000000000000000000001): 1 4 2(00000000000000000000000000000010): 2 5 3(00000000000000000000000000000011): 1 6 4(00000000000000000000000000000100): 3 7 5(00000000000000000000000000000101): 1 8 6(00000000000000000000000000000110): 2 9 7(00000000000000000000000000000111): 110 8(00000000000000000000000000001000): 411 9(00000000000000000000000000001001): 112 4294967294(11111111111111111111111111111110): 213 4294967295(11111111111111111111111111111111): 114 15 __builtin_clz:16 0(00000000000000000000000000000000): 3117 1(00000000000000000000000000000001): 3118 2(00000000000000000000000000000010): 3019 3(00000000000000000000000000000011): 3020 4(00000000000000000000000000000100): 2921 5(00000000000000000000000000000101): 2922 6(00000000000000000000000000000110): 2923 7(00000000000000000000000000000111): 2924 8(00000000000000000000000000001000): 2825 9(00000000000000000000000000001001): 2826 4294967294(11111111111111111111111111111110): 027 4294967295(11111111111111111111111111111111): 028 29 __builtin_ctz:30 0(00000000000000000000000000000000): 031 1(00000000000000000000000000000001): 032 2(00000000000000000000000000000010): 133 3(00000000000000000000000000000011): 034 4(00000000000000000000000000000100): 235 5(00000000000000000000000000000101): 036 6(00000000000000000000000000000110): 137 7(00000000000000000000000000000111): 038 8(00000000000000000000000000001000): 339 9(00000000000000000000000000001001): 040 4294967294(11111111111111111111111111111110): 141 4294967295(11111111111111111111111111111111): 042 43 __builtin_popcount:44 0(00000000000000000000000000000000): 045 1(00000000000000000000000000000001): 146 2(00000000000000000000000000000010): 147 3(00000000000000000000000000000011): 248 4(00000000000000000000000000000100): 149 5(00000000000000000000000000000101): 250 6(00000000000000000000000000000110): 251 7(00000000000000000000000000000111): 352 8(00000000000000000000000000001000): 153 9(00000000000000000000000000001001): 254 4294967294(11111111111111111111111111111110): 3155 4294967295(11111111111111111111111111111111): 3256 57 __builtin_parity:58 0(00000000000000000000000000000000): 059 1(00000000000000000000000000000001): 160 2(00000000000000000000000000000010): 161 3(00000000000000000000000000000011): 062 4(00000000000000000000000000000100): 163 5(00000000000000000000000000000101): 064 6(00000000000000000000000000000110): 065 7(00000000000000000000000000000111): 166 8(00000000000000000000000000001000): 167 9(00000000000000000000000000001001): 068 4294967294(11111111111111111111111111111110): 169 4294967295(11111111111111111111111111111111): 070 71 72 Process returned 0 (0x0) execution time : 2.405 s73 Press any key to continue.
这里存在一个为题,希望知道的朋友能够解释,就是为什么不能用函数指针指向这些内置函数。
答:为了性能,这些函数都是内联的。
比如很多平台都有特定的指令,所以这些“函数”都只要一条指令就完成了,做成函数得不偿失。
转自: