#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
.
|