Alignment puzzle

J

JFG

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

JFG

Can no one help with this? I'd have thought it's bread and butter for
the experts!

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

Lew Pitcher

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top