Home > Archive > C > September 2007 > Alignment puzzle
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi -
I have this program to maintain and I'm puzzling over the following
function -
void
lance_copytobuf_gap2(sc, fromv, boff, len)
struct lance_softc *sc;
void *fromv;
int boff;
register int len;
{
volatile caddr_t buf = sc->sc_mem;
register caddr_t from = fromv;
register volatile u_int16_t *bptr;
if (boff & 0x1) {
/* handle unaligned first byte */
bptr = ((volatile u_int16_t *)buf) + (boff - 1);
*bptr = (*from++ << 8) | (*bptr & 0xff);
bptr += 2;
len--;
} else
bptr = ((volatile u_int16_t *)buf) + boff;
while (len > 1) {
*bptr = (from[1] << 8) | (from[0] & 0xff);
bptr += 2;
from += 2;
len -= 2;
}
if (len == 1)
*bptr = (u_int16_t)*from;
}
What exactly is happening in the "handle unaligned first byte" part? It
seems quite opaque to me.
--
We all live in a yellow submarine.
| |
|
| Can no one help with this? I'd have thought it's bread and butter for
the experts!
On 20 Sep 2007 at 0:16, JFG wrote:
> Hi -
>
> I have this program to maintain and I'm puzzling over the following
> function -
>
> void
> lance_copytobuf_gap2(sc, fromv, boff, len)
> struct lance_softc *sc;
> void *fromv;
> int boff;
> register int len;
> {
> volatile caddr_t buf = sc->sc_mem;
> register caddr_t from = fromv;
> register volatile u_int16_t *bptr;
> if (boff & 0x1) {
> /* handle unaligned first byte */
> bptr = ((volatile u_int16_t *)buf) + (boff - 1);
> *bptr = (*from++ << 8) | (*bptr & 0xff);
> bptr += 2;
> len--;
> } else
> bptr = ((volatile u_int16_t *)buf) + boff;
> while (len > 1) {
> *bptr = (from[1] << 8) | (from[0] & 0xff);
> bptr += 2;
> from += 2;
> len -= 2;
> }
> if (len == 1)
> *bptr = (u_int16_t)*from;
> }
>
> What exactly is happening in the "handle unaligned first byte" part? It
> seems quite opaque to me.
>
> --
> We all live in a yellow submarine.
--
We all live in a yellow submarine.
| |
| Lew Pitcher 2007-09-20, 7:57 am |
| On Sep 19, 8:16 pm, JFG <j...@nospam.invalid> wrote:
> Hi -
>
> I have this program to maintain and I'm puzzling over the following
> function -
>
> void
> lance_copytobuf_gap2(sc, fromv, boff, len)
> struct lance_softc *sc;
> void *fromv;
> int boff;
> register int len;
> {
> volatile caddr_t buf = sc->sc_mem;
> register caddr_t from = fromv;
> register volatile u_int16_t *bptr;
> if (boff & 0x1) {
> /* handle unaligned first byte */
> bptr = ((volatile u_int16_t *)buf) + (boff - 1);
> *bptr = (*from++ << 8) | (*bptr & 0xff);
> bptr += 2;
> len--;
> } else
> bptr = ((volatile u_int16_t *)buf) + boff;
> while (len > 1) {
> *bptr = (from[1] << 8) | (from[0] & 0xff);
> bptr += 2;
> from += 2;
> len -= 2;
> }
> if (len == 1)
> *bptr = (u_int16_t)*from;
>
> }
>
> What exactly is happening in the "handle unaligned first byte" part? It
> seems quite opaque to me.
While you haven't supplied enough information to be certain (how are
lance_softc and caddr_t defined?) I can certainly supply you with an
educated guess.
The K&R C function you show will copy 16bit quantities into a buffer,
at a specified offset from the beginning of the buffer. The code seems
to assume that the buffer is otherwise "aligned" to some boundary, and
the offset may cause the start of the copy to move off of that
alignment.
So, the code in question checks to see if the offset is a whole
multiple of 2, and if it is not, it "merges" the first byte of the
data to be copied into the high order 8 bits of the 16bit quantity
that the offset breaks across.
After that, the code then copies the remaining data byte by byte,
building pairs of bytes into 16bit quantities and populating the
remaining space in the buffer with these 16bit quantities.
|
|
|
|
|