Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
15 Views

bitfield errors

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; }
0 Kudos
3 Replies
Highlighted
15 Views

Somehow copy-paste erased the headers:

#include <cstdio>

#include <cstddef>

#include <cstdint>

#include <cstring>

0 Kudos
Highlighted
15 Views

Attached is the source

0 Kudos
Highlighted
Moderator
15 Views

Thank for the test case. I have reported this issue to our Compiler Developer. Case number is: CMPLRIL0-30797

 

0 Kudos