Code: Select all
s.i = s1.i + s2.i;
A good optimizer should recognize if your bit-field is an addressible unit, like (x>>8 & 0xFF) is just the second-lowest byte, and could be fetched through a MOVB or MOVSX instruction. so the point is not to make it more efficient, but to make it more portable. And perhaps even optimally efficient without such a high optimization level. When you define 4 byte field within the integer, even the most stupid compiler would use MOVB when you access one of those, and interpret the sign bit properly.UncombedCoconut wrote:I once tried replacing Stockfish's Score type with a struct that used two 16-bit fields, and got identical benchmark results. The result surprised me at the time. (I had a pre-conceived notion that the compiler would generate inefficient code for loading and storing bitfields.)