linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Li YanBo" <dreamfly281@gmail.com>
To: Ranjan Sinha <rnjn.sinha@gmail.com>
Cc: Arjan van de Ven <arjan@infradead.org>,
	linux-c-programming@vger.kernel.org, kernelnewbies@nl.linux.org
Subject: Re: Confuse with big endian bitwise field
Date: Tue, 10 Jul 2007 11:58:30 +0800	[thread overview]
Message-ID: <1197ff4c0707092058y47efa3d7i3801f99c0b9ebd53@mail.gmail.com> (raw)
In-Reply-To: <1197ff4c0707022112hf42ba45y6fcb88a5d877a62c@mail.gmail.com>

On 7/3/07, Li YanBo <dreamfly281@gmail.com> wrote:
> On 6/28/07, Ranjan Sinha <rnjn.sinha@gmail.com> wrote:
> > Thanks for this discussion, since this really gets confusing at times.
> > > >
> > > > struct xxx {
> > > >         __be32 pdu_cnt:6;
> > > >         __be32 y:3;
> > > >         __be32 wep_key:2;
> > > >         __be32 uses_wep_key:1;
> > > >         __be32 keep_alive:1;
> > > >         __be32 buff_tail_addr:19;
> > > >
> > > >         __be32 cts_11g:1;
> > > >         __be32 rts_11g:1;
> > > >         __be32 x:2;situation
> > > >         __be32 frag_size:12;
> > > >         __be32 payload_len:12;
> > > >         __be32 frag_num:4;
> > > > }
> > >
> > > this isn't safe if you want to mimic hardware layout; the order of the
> > > bits in the struct is different for little endian and big endian
> > > machines...
> > >
> > What is the best way in such scenario? Should we then use an unsigned
> > char array and then keep separate mappings of each bit for little and
> > big endian machines ?
> >
>
> I am sorry I still can't figure out how to use bits shift and make it
> can be portability to any Be or Le endian machine. what I can tell you
> is if the bits fields was define for Be, and if the host is Le, so you
> just need reverse the order of bits field and it will work well. For
> example:
>
> That a specs define for Be:
> Descriptor(big endian)  size   offset
> RTS                            1        0
> multicast                     1        1
> x                                15       2
> y                                15       17
>
> If you want define it in Le host, we can define it like this
> struct desc {
>          y:15;
>          x:15;
>          muticast:1;
>          rts:1;
> } __attribute__ ((packed));
>
> That only my personal opinion, CMIIW. if anyone know better method,
> please share with us. thanks!
>

Define the bits field as follow can be portable to either  Le or Be host.
For the above sample, we can define it like that:

#define RTS         0x80000000
#define multicast  0x40000000
#define x              0x3FFF8000
#define y              0x00007FFF
I think that can make sure it can be portable in Be or Le host.

But there is another  situation confuse me, if the data are come from
net, ie the wireless 802.11 header, even we define the structure like
that, we also need to do cpu_to_le32 or le32_to_cpu() manual. I don't
know is it different for data that transport from CPU to IO device or
data that transport from CPU to net?

BR
lyb

      reply	other threads:[~2007-07-10  3:58 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-26 10:20 Confuse with big endian bitwise field Li YanBo
2007-06-26 18:09 ` Kristof Provost
2007-06-28 14:39 ` Arjan van de Ven
2007-06-28 14:59   ` Li YanBo
2007-06-28 15:25   ` Ranjan Sinha
2007-07-03  4:12     ` Li YanBo
2007-07-10  3:58       ` Li YanBo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1197ff4c0707092058y47efa3d7i3801f99c0b9ebd53@mail.gmail.com \
    --to=dreamfly281@gmail.com \
    --cc=arjan@infradead.org \
    --cc=kernelnewbies@nl.linux.org \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=rnjn.sinha@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).