Quantcast
Channel: Intel® Software - Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1175

bitfield errors

$
0
0

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;
}


Viewing all articles
Browse latest Browse all 1175

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>