Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Intel Community
- Software Development Tools (Compilers, Debuggers, Profilers & Analyzers)
- Intel® C++ Compiler
- Bit count: preprocessing magic compared to modern C ++

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Highlighted
##

Jacqueline

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-29-2020
05:15 AM

107 Views

Bit count: preprocessing magic compared to modern C ++

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?

3 Replies

Highlighted
##

AbhishekD_Intel

Moderator

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-30-2020
04:30 AM

83 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

Highlighted
##

AbhishekD_Intel

Moderator

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-10-2020
03:32 AM

52 Views

Hi,

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

Warm Regards,

Abhishek

Highlighted
##

AbhishekD_Intel

Moderator

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-23-2020
12:57 AM

14 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

For more complete information about compiler optimizations, see our Optimization Notice.