Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

Bit count: preprocessing magic compared to modern C ++

Jacqueline
Beginner
679 Views

Assume that I want to create a compile-time built-in bit count lookup table for 64-bit integers in 16-bit chunks. The only way I know to do this is the following code:

#define B4(n) n, n + 1, n + 1, n + 2
#define B6(n)   B4(n),   B4(n + 1),   B4(n + 1),  B4(n + 2)  
#define B8(n)   B6(n),   B6(n + 1),   B6(n + 1),  B6(n + 2)
#define B10(n)  B8(n),   B8(n + 1),   B8(n + 1),  B8(n + 2)
#define B12(n)  B10(n),  B10(n + 1),  B10(n + 1), B10(n + 2)
#define B14(n)  B12(n),  B12(n + 1),  B12(n + 1), B12(n + 2)
#define B16(n)  B14(n),  B14(n + 1),  B14(n + 1), B14(n + 2)
#define COUNT_BITS B16(0), B16(1), B16(1), B16(2)

unsigned int lookup[65536] = { COUNT_BITS };

Is there a modern way (C ++ 11/14) to get the same result?

0 Kudos
3 Replies
AbhishekD_Intel
Moderator
655 Views

Hi,

 

Thanks for reaching out to us.

There is a feature in C++11 called constexpr which is mainly used to do computations at compile time. So you can use constexpr to build your lookup table.

 

There is also one feature called template metaprogramming, it is like creating a template for doing computation at compile-time, but I am not much sure that you can use it in your use-case. But if you want to explore you can try that.

 

 

Warm Regards,

Abhishek

 

0 Kudos
AbhishekD_Intel
Moderator
623 Views

Hi,


Please update us on the provided details. Let us know if this solved your problem.



Warm Regards,

Abhishek



0 Kudos
AbhishekD_Intel
Moderator
586 Views

Hi,


We haven't heard back from a long time so we are assuming that the provided details helped you.

We will no longer monitor this thread. Please post a new thread if you have any other issues.



Warm Regards,

Abhishek


0 Kudos
Reply