473,657 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bitfields in a heterogenous environment

I am looking for opinions on a possible approach to coping with
bitfields in a heterogenous environment.

We've got a bunch of legacy code which maps to hardware using
bitfields. The code was developed for a PowerPC/VxWorks/Tornado
enviromant, with the bits packed in a big-endian manner: msb first. We
now have to develop an Intel/Windows/MSVC++ version (lsb assigned
first) which must maintain compatibility with the hardware interface
and use the same source for manipulating the related bitfields. So
discussion of better approaches than using bitfields is moot.

I am considering the following approach to minimize impact to the
legacy code. It uses nested macro calls to define the bitfields in
forward or reverse order according to the environment:

#ifdef BITFIELDS_LITTL E_ENDIAN
#define DEFINE_BITFIELD S_MSB_FIRST(A,B ) B A
#else
#define DEFINE_BITFIELD S_MSB_FIRST(A,B ) A B
#endif

struct bitfield_struct
{
DEFINE_BITFIELD S_MSB_FIRST( int bit31 : 1; ,
DEFINE_BITFIELD S_MSB_FIRST( int bit20_30 : 11; ,
DEFINE_BITFIELD S_MSB_FIRST( int bit10_19 : 10; ,
DEFINE_BITFIELD S_MSB_FIRST( int bit1_9 : 9; ,
int bit0 : 1;
))))
};

The preprocessor generates the following if BITFIELDS_LITTL E_ENDIAN is
defined:

struct bitfield_struct
{

int bit0 : 1; int bit1_9 : 9; int bit10_19 : 10; int bit20_30 : 11; int
bit31 : 1;
};

or otherwise,

struct bitfield_struct
{

int bit31 : 1; int bit20_30 : 11; int bit10_19 : 10; int bit1_9 : 9;
int bit0 : 1;
};

Of course, we will have to be careful to fill every bit position. We
will nest the calls to define exactly 32 bits worth at a time, so the
maximum nesting of the macro calls would be 32. We will also have to
put up with the collapse of multiple lines of code into a single line
for compiler error reporting and during debug.

If we are to avoid touching bitfield-processing code, the only other
option we have identified is to define the structures twice:

#ifdef BITFIELDS_LITTL E_ENDIAN
struct bitfield_struct
{
int bit0 : 1;
int bit1_9 : 9;
int bit10_19 : 10;
int bit20_30 : 11;
int bit31 : 1;
};
#else
struct bitfield_struct
{
int bit31 : 1;
int bit20_30 : 11;
int bit10_19 : 10;
int bit1_9 : 9;
int bit0 : 1;
};
#endif

I like the nested-macro approach because we only code the idea of the
hardware bit map once and inserting "DEFINE_BITFIEL DS_MSB_FIRST(" in
front of the existing structures seems less error prone than trying to
manually invert the order of definition. I was surprised to not find
anything similar in previous discussions here and am wondering if it
has any drawbacks I have not considered. If it helps to focus the
discussion, we have no plans to go beyond the current 32-bit PowerPC
and Intel architectures, though we might migrate the Intel platform
from Windows/MSVC++ to Linux/gcc some day. We have 2000+ bitfield
definitions to cope with.

I look forward to your (constructive!) comments.

-Galen

Nov 14 '05 #1
6 2678
"GalenTX" <gb******@kanna rr.tuffmail.com > wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I am looking for opinions on a possible approach to coping with
bitfields in a heterogenous environment.

We've got a bunch of legacy code which maps to hardware using
bitfields. The code was developed for a PowerPC/VxWorks/Tornado
enviromant, with the bits packed in a big-endian manner: msb first. We
now have to develop an Intel/Windows/MSVC++ version (lsb assigned
first) which must maintain compatibility with the hardware interface
and use the same source for manipulating the related bitfields. So
discussion of better approaches than using bitfields is moot.
Well then, you're stuck with poorly implemented legacy code.
You should've used bit masking at the byte level to have a chance
at portability to other hardware (at least where CHAR_BIT is
same).
I am considering the following approach to minimize impact to the
legacy code. It uses nested macro calls to define the bitfields in
forward or reverse order according to the environment:
Since you're planning on editing every header file to
insert the macros, maybe you should take the next step
and just convert the code to use masking. Masking is
easy to understand, easy to maintain.
#ifdef BITFIELDS_LITTL E_ENDIAN
#define DEFINE_BITFIELD S_MSB_FIRST(A,B ) B A
#else
#define DEFINE_BITFIELD S_MSB_FIRST(A,B ) A B
#endif

struct bitfield_struct
{
DEFINE_BITFIELD S_MSB_FIRST( int bit31 : 1; ,
DEFINE_BITFIELD S_MSB_FIRST( int bit20_30 : 11; ,
DEFINE_BITFIELD S_MSB_FIRST( int bit10_19 : 10; ,
DEFINE_BITFIELD S_MSB_FIRST( int bit1_9 : 9; ,
int bit0 : 1;
))))
};

The preprocessor generates the following if BITFIELDS_LITTL E_ENDIAN is
defined:

struct bitfield_struct
{

int bit0 : 1; int bit1_9 : 9; int bit10_19 : 10; int bit20_30 : 11; int
bit31 : 1;
};

or otherwise,

struct bitfield_struct
{

int bit31 : 1; int bit20_30 : 11; int bit10_19 : 10; int bit1_9 : 9;
int bit0 : 1;
};

Of course, we will have to be careful to fill every bit position. We
will nest the calls to define exactly 32 bits worth at a time, so the
maximum nesting of the macro calls would be 32. We will also have to
put up with the collapse of multiple lines of code into a single line
for compiler error reporting and during debug.
Yup, you're asking for even more headaches, than if you
had done it right the first time.
If we are to avoid touching bitfield-processing code, the only other
option we have identified is to define the structures twice:
Bite the bullet NOW. Fix the broken code.
#ifdef BITFIELDS_LITTL E_ENDIAN
struct bitfield_struct
{
int bit0 : 1;
int bit1_9 : 9;
int bit10_19 : 10;
int bit20_30 : 11;
int bit31 : 1;
};
#else
struct bitfield_struct
{
int bit31 : 1;
int bit20_30 : 11;
int bit10_19 : 10;
int bit1_9 : 9;
int bit0 : 1;
};
#endif
Hugely worse; a double-maintenance headache. Your junior
programmers will certainly screw this up.
I like the nested-macro approach because we only code the idea of the
hardware bit map once and inserting "DEFINE_BITFIEL DS_MSB_FIRST(" in
front of the existing structures seems less error prone than trying to
manually invert the order of definition. I was surprised to not find
anything similar in previous discussions here and am wondering if it
has any drawbacks I have not considered. If it helps to focus the
discussion, we have no plans to go beyond the current 32-bit PowerPC
and Intel architectures, though we might migrate the Intel platform
from Windows/MSVC++ to Linux/gcc some day. We have 2000+ bitfield
definitions to cope with.

I look forward to your (constructive!) comments.


Good luck, you'll need it!
Nov 14 '05 #2


GalenTX wrote:
I am looking for opinions on a possible approach to coping with
bitfields in a heterogenous environment.

We've got a bunch of legacy code which maps to hardware using
bitfields. The code was developed for a PowerPC/VxWorks/Tornado
enviromant, with the bits packed in a big-endian manner: msb first. We
now have to develop an Intel/Windows/MSVC++ version (lsb assigned
first) which must maintain compatibility with the hardware interface
and use the same source for manipulating the related bitfields. So
discussion of better approaches than using bitfields is moot.

I am considering the following approach to minimize impact to the
legacy code. It uses nested macro calls to define the bitfields in
forward or reverse order according to the environment:

#ifdef BITFIELDS_LITTL E_ENDIAN
#define DEFINE_BITFIELD S_MSB_FIRST(A,B ) B A
#else
#define DEFINE_BITFIELD S_MSB_FIRST(A,B ) A B
#endif

struct bitfield_struct
{
DEFINE_BITFIELD S_MSB_FIRST( int bit31 : 1; ,
DEFINE_BITFIELD S_MSB_FIRST( int bit20_30 : 11; ,
DEFINE_BITFIELD S_MSB_FIRST( int bit10_19 : 10; ,
DEFINE_BITFIELD S_MSB_FIRST( int bit1_9 : 9; ,
int bit0 : 1;
))))
};
[...]


You seem to have taken care of arranging the bit fields
in the desired order, but what about the individual bits in
multi-bit fields like bit1_9? I'm using "order" rather
loosely here because the bits probably aren't addressable.
What I'm getting at is that if you do `foo.bit1_9 = 5', how
do you know which two bits of the 32-bit (probably) struct
are set?

There's no portable way to be sure of getting the layout
you want, so whatever solution you adopt will be compiler-
(or compilers-) dependent. If you're stuck with bit fields,
all I can suggest is to hack away and hope for the best; the
C language as such is of little help in your predicament.

It occurs to me that there might be a way out of the mess,
depending on how the code is structured. If (*if*) the bulk
of the code deals with bits already read from the hardware or
prepares batches of bits to be written to the hardware, but
the actual reading and writing takes place in relatively few
places, you could just let the structs line up any old way and
provide get() and put() functions that swizzled the bits as
needed, on the fly. That, in fact, is a pretty good model for
all such format-matching tasks: Put the format knowledge in a
few isolated functions and use a "CPU-friendly" representation
elsewhere. Whether your existing code base is organized so as
to make this approach attractive is something you'll need to
judge for yourself.

--
Er*********@sun .com

Nov 14 '05 #3
I don't think the bit-order within individually named bitfields is an
issue. Once accessed by name, they should be converted to an int with
the proper bit order. The issue just occurs with the way the names are
allocated within the comprising int.

We contemplated some bit-swizzling, as you say, but decided that was
going to be much more work and leaves places in your program where the
data in memory is not organized according to the structures you've
defined--always an opportunity for errors. The VxWorks system is an
embedded one, so the hardware interfaces permeate the code.

In defense of the original programmers, some of the code is 10 years
old and it was never envisioned that it would be used in any other
environment. Given the constraint of a static and well-understood
environment, bit-fields make for the clearest code at the point of use.
Bit masking and shifting is absolutely more portable, but even with
macros or functions to hide the complexity, it takes up more visual
space in the application code, which makes it harder to see the real
task at hand.

Nov 14 '05 #4


GalenTX wrote:
I don't think the bit-order within individually named bitfields is an
issue. Once accessed by name, they should be converted to an int with
the proper bit order. The issue just occurs with the way the names are
allocated within the comprising int.


You said the purpose was to match the fields to a
hardware device, which usually means that the order of
all the non-ignored bits is important. If you've got
a bit-order mismatch between the CPU and the device,
you might store 19 = 10011 in a five-bit field and find
that the device understood it as 11001 = 25 instead.
That's a good way to turn a READ SECTOR command into
LOW-LEVEL FORMAT ...

But then, perhaps you're not facing such issues.
If not, fine -- but you're still forced to rely on the
behaviors of the particular compilers you're using, and
not on anything guaranteed by the C language. Good luck!

--
Er*********@sun .com

Nov 14 '05 #5
You're absolutely right, the C language guarantees me nothing here.
But it would seem odd to assign an int to a bitfield and have the
compiler place the bits into the int structure containing the bitfield
in the opposite order that they appear in the int. That would require
bit reversal, which would seem expensive.

I'm not talking about the way the hardware is mapped on the processor
bus. But even there, the hardware guy who wires more significant bits
on the processor to less significant ones on the device is going to be
in trouble. I'm also not talking about byte order, either.
Fortunately, everything we are working with is a uniform byte width and
is read and written in one place in the code, so we have only one place
to verify that byte order is correct.

Again, I think it is a pretty safe bet that bit significance will be
preserved within a bitfield, regardless of the order in which multiple
bitfields are allocated space in an int. At least for the limited set
of compilers I am contemplating, I think I am okay.

Nov 14 '05 #6
Trying to pull the discussion back to the point of my original post, I
need to mention that we am constrained to edit header files only. So,
for better or worse, we're going to have bitfields, the only question
is how we go about defining the structure.

Does anybody see any problem with my preferred approach, the nested
macro calls? The alternative, to define the structures twice while
inverting the order manually, seems more error prone in initial
implementation seems to pose a higher maintenance cost.

By the way, can anyone suggest a better name for the macro? I'm not
entirely happy with DEFINE_BITFIELD S_MSB_FIRST.

-Galen

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3178
by: Grumfish | last post by:
In order to familiarize my self with Flash files and their bytecode I've started to make an assembler. My first problem is writing the bitfields the format uses often. It is a series of fields, each can be a different number of bits, combined into the least amount of bytes possible. Extra bits in the last byte are padded with zeros. I would like to make a function that takes a size and value for each field needed, calculate the amount of...
3
3397
by: Jon Slaughter | last post by:
I'm using bit fields to compactly represent some data I need to manage and I've read that they are not portable. I don't understand why that is the case? As long as I don't "indirectly" mess with the fields and I go through the compiler and allow it to choose the appropriate way to handle them then why shouldn't it be ok? The whole point of me using bit fields was so that I could be somewhat portable instead of managing the bits...
0
400
by: tmartsum | last post by:
I have a discussion in comp.std.c++ After a (bit stupid) suggestion on representing a fixed 'big' length int I moderated it to "Bitfields-ints should be allowed to have any fixed length" I got no replys on my moderated suggestion. (I am asking you your opinion of this - I do not really have a C++-problem)
8
1902
by: Régis Troadec | last post by:
Hi all, I follow c.l.c. for only a short time and I would like to know why there isn't anything concerning bitfields among the FAQs. Is it because ... 1. of portability issues? 2. bitfields aren't enough useful to be discussed? 3. of the low frequency of questions concerning this topic? 4. of anything else?...
23
2816
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find relevance.Though both of these(bitfields and unions) are used where space is a constraint(so I can assume always in embedded systems,where memory is particularly less)and we want to save space/memory. As far as I have read, there is no...
19
14801
by: Mehta Shailendrakumar | last post by:
Hi, I would like to know why array of bitfields is not possible. Is there any relation with processor architecture for this? Thank you for your time. Regards, Shailendra
18
4713
by: richard_l | last post by:
Hello All, I am writing an application which receives a word which is a bitmap. I have created a word typedef which contains a bitfield defining each bit. however, I was wondering however if it would be better to write a macro to access each bit instead of the bitfield. I have read the C-FAQ on bit fields, but was wondering if there were any advantages/disadvantages to using bit shifting. To my mind I think using bitfields are more...
9
19253
by: cman | last post by:
Who can explain to me what bitfields are and how to use them in practice? I would need a fairly detailed explaination, I would be a newbie to advanced C programming features. What are the advantages of using bitfields? cman
10
8484
by: lithiumcat | last post by:
Hi, This question seems to come up quite often, however I haven't managed to find an answer relevant to my case. I often use binary flags, and I have alaways used a "bitmask" technique, that is using #defined or const int powers of two, and using the following primitives : /* declaration and initizalisation of flags */ int flags = 0; /* setting flag */ flags |= FLAG_1;
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.