- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I found some unexpected behavior in ICC relating to bitfields, although it works in GCC and Clang. Initially, I thought this was caused by constexpr lambda, but it appears that some bitfield masking is getting incorrectly dropped/misapplied.
----
Output w/ icpc (ICC) 19.0.0.117 20180804
bitfield sizes:
nFourBits: 16
rest: 16
nFourBits: 4
rest: 12
nFourBits: 16
rest: 16
Output w/ g++ (GCC) 7.2.1 20170829 (Red Hat 7.2.1-1)
bitfield sizes:
nFourBits: 4
rest: 12
nFourBits: 4
rest: 12
nFourBits: 4
rest: 12
Output w/ clang version 5.0.1 (tags/RELEASE_501/final)
bitfield sizes:
nFourBits: 4
rest: 12
nFourBits: 4
rest: 12
nFourBits: 4
rest: 12
-----
#include
#include
#include
#include
template
constexpr size_t countBits(T t) {
size_t bits = 0;
while (t)
{
t &= (t - 1);
++bits;
}
return bits;
}
#define GET_BITFIELD_SIZE_CONSTEXPR(Bitfield, element) \
[]() constexpr -> size_t { \
Bitfield t = {}; \
using int_type = decltype(t.element); \
int_type inverted = static_cast(~0ull); \
t.element |= inverted; \
return countBits(t.element); \
} ()
#define GET_BITFIELD_SIZE_NON_CONSTEXPR(Bitfield, element) \
[]() -> size_t { \
Bitfield t; \
using int_type = decltype(t.element); \
int_type inverted = static_cast(~0ull); \
t.element = inverted; \
return countBits(t.element); \
} ()
#define GET_BITFIELD_SIZE_NON_CONSTEXPR2(Bitfield, element) \
[]() -> size_t { \
Bitfield t = {}; \
using int_type = decltype(t.element); \
int_type inverted = static_cast(~0ull); \
t.element |= inverted; \
return countBits(t.element); \
} ()
struct SomeStruct
{
uint16_t nFourBits:4;
uint16_t rest:12;
};
static_assert(sizeof(SomeStruct) == 2, "Unexpected size");
int main(int argc, char* argv[])
{
printf("bitfield sizes:\n");
printf("nFourBits: %lu\n", GET_BITFIELD_SIZE_CONSTEXPR(SomeStruct, nFourBits));
printf("rest: %lu\n", GET_BITFIELD_SIZE_CONSTEXPR(SomeStruct, rest));
printf("\n");
printf("nFourBits: %lu\n", GET_BITFIELD_SIZE_NON_CONSTEXPR(SomeStruct, nFourBits));
printf("rest: %lu\n", GET_BITFIELD_SIZE_NON_CONSTEXPR(SomeStruct, rest));
printf("\n");
printf("nFourBits: %lu\n", GET_BITFIELD_SIZE_NON_CONSTEXPR2(SomeStruct, nFourBits));
printf("rest: %lu\n", GET_BITFIELD_SIZE_NON_CONSTEXPR2(SomeStruct, rest));
return 0;
}
- Tags:
- CC++
- Development Tools
- Intel® C++ Compiler
- Intel® Parallel Studio XE
- Intel® System Studio
- Optimization
- Parallel Computing
- Vectorization
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Somehow copy-paste erased the headers:
#include <cstdio>
#include <cstddef>
#include <cstdint>
#include <cstring>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank for the test case. I have reported this issue to our Compiler Developer. Case number is: CMPLRIL0-30797

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