LWN.net Logo

Knock me over with a feather

Knock me over with a feather

Posted Feb 6, 2012 12:22 UTC (Mon) by etienne (subscriber, #25256)
In reply to: Knock me over with a feather by cavok
Parent article: Betrayed by a bitfield

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...


(Log in to post comments)

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