User: Password:
|
|
Subscribe / Log in / New account

Knock me over with a feather

Knock me over with a feather

Posted Feb 3, 2012 22:58 UTC (Fri) by cavok (subscriber, #33216)
In reply to: Knock me over with a feather by ncm
Parent article: Betrayed by a bitfield

bitfields in a mostly 32/64 bit aligned world...
how many millions of those structures do you need to see some gain in using bitfields?


(Log in to post comments)

Knock me over with a feather

Posted Feb 3, 2012 23:02 UTC (Fri) by dlang (subscriber, #313) [Link]

not very many.

if you can even replace two int variables with one bitfield, you may shrink your datastructure enough to have it fit within a CPU cache line. The memory access that can then be avoided can be enough to make a noticeable difference.

Knock me over with a feather

Posted Feb 6, 2012 13:32 UTC (Mon) by cavok (subscriber, #33216) [Link]

got it. thank you.

Knock me over with a feather

Posted Feb 6, 2012 12:22 UTC (Mon) by etienne (guest, #25256) [Link]

You can gain a lot of code space using related bits declared in a bitfield structure, because the compiler can do a single mask (to hide irrelevant bits) and a single test of a 32 bits value to decide something.
For instance, a bitfield structure describing the debugging/logging of messages where bits are enable_global, enable_subsystem1, enable_onserial, telnet_connected, and the test is "if (debug.enable_global && (debug.enable_onserial || debug.telnet_connected) && debug.enable_subsystem5)".
If you do not do that, on processors with one single "flag" register, the only way for the compiler is to do multiple test and jump and storing intermediate bit into 32 or 64 bits registers (compilers do not do well sub-register allocation by themself).
You could also use the preprocessor with "#define enable_global 0x80000", but I prefer writing in C/C++ than in CPP...


Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds