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.
返回右起第一个‘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 #include <stdio.h>
 2 #include <iostream>
 3 #include <bitset>
 4 #include <string>
 5 #include <limits.h>
 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_parity
27 //};
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         // ffs
45         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         // clz
54         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         // ctz
63         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         // popcount
72         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         // parity
81         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): 1
10 8(00000000000000000000000000001000): 4
11 9(00000000000000000000000000001001): 1
12 4294967294(11111111111111111111111111111110): 2
13 4294967295(11111111111111111111111111111111): 1
14  
15 __builtin_clz:
16 0(00000000000000000000000000000000): 31
17 1(00000000000000000000000000000001): 31
18 2(00000000000000000000000000000010): 30
19 3(00000000000000000000000000000011): 30
20 4(00000000000000000000000000000100): 29
21 5(00000000000000000000000000000101): 29
22 6(00000000000000000000000000000110): 29
23 7(00000000000000000000000000000111): 29
24 8(00000000000000000000000000001000): 28
25 9(00000000000000000000000000001001): 28
26 4294967294(11111111111111111111111111111110): 0
27 4294967295(11111111111111111111111111111111): 0
28  
29 __builtin_ctz:
30 0(00000000000000000000000000000000): 0
31 1(00000000000000000000000000000001): 0
32 2(00000000000000000000000000000010): 1
33 3(00000000000000000000000000000011): 0
34 4(00000000000000000000000000000100): 2
35 5(00000000000000000000000000000101): 0
36 6(00000000000000000000000000000110): 1
37 7(00000000000000000000000000000111): 0
38 8(00000000000000000000000000001000): 3
39 9(00000000000000000000000000001001): 0
40 4294967294(11111111111111111111111111111110): 1
41 4294967295(11111111111111111111111111111111): 0
42  
43 __builtin_popcount:
44 0(00000000000000000000000000000000): 0
45 1(00000000000000000000000000000001): 1
46 2(00000000000000000000000000000010): 1
47 3(00000000000000000000000000000011): 2
48 4(00000000000000000000000000000100): 1
49 5(00000000000000000000000000000101): 2
50 6(00000000000000000000000000000110): 2
51 7(00000000000000000000000000000111): 3
52 8(00000000000000000000000000001000): 1
53 9(00000000000000000000000000001001): 2
54 4294967294(11111111111111111111111111111110): 31
55 4294967295(11111111111111111111111111111111): 32
56  
57 __builtin_parity:
58 0(00000000000000000000000000000000): 0
59 1(00000000000000000000000000000001): 1
60 2(00000000000000000000000000000010): 1
61 3(00000000000000000000000000000011): 0
62 4(00000000000000000000000000000100): 1
63 5(00000000000000000000000000000101): 0
64 6(00000000000000000000000000000110): 0
65 7(00000000000000000000000000000111): 1
66 8(00000000000000000000000000001000): 1
67 9(00000000000000000000000000001001): 0
68 4294967294(11111111111111111111111111111110): 1
69 4294967295(11111111111111111111111111111111): 0
70  
71  
72 Process returned 0 (0x0)   execution time : 2.405 s
73 Press any key to continue.

这里存在一个为题,希望知道的朋友能够解释,就是为什么不能用函数指针指向这些内置函数。

答:为了性能,这些函数都是内联的。
比如很多平台都有特定的指令,所以这些“函数”都只要一条指令就完成了,做成函数得不偿失。

 

转自:https://www.cnblogs.com/nysanier/archive/2011/04/19/2020778.html

posted @ 2018-01-04 12:15  鸭子船长  阅读(941)  评论(0编辑  收藏  举报