Skybuck Flying wrote:
> "MitchAlsup" <MitchAlsup@[EMAIL PROTECTED]
> wrote in message
>
news:cb20dc12-415b-4a0c-8bd9-4985a7c04946@[EMAIL PROTECTED]
>> Lets do this in an instruction set that it makes it easy, and use C;
>>
>> void WriteLongwordBits( unsigned long Value, unsignedint BitCount,
>> unsigned long int *DestAddress, unsigned int DestBitIndex )
>> {
>> ASM{ // get stuff from the parameter passing area
>> mov a1,12[sp]
>> mov d2,16[sp]
>> mov a3,20[sp]
>> mov d4,24[sp]
>>
>> BFEXTU d5,d2,[a1] // get the source field
>> BFINS d5,d4,[a3] // put it where it belongs
>> }
[snip]
> Maybe somebody can re-write all of this assembler to be more efficient ?
;)
Mitch already did, see above.
>
> Or simply come up with an alternative assembler implementation.
Your example code is simply horrible, besides the fact that it can trap
on a 64-bit platform since you don't seem to care about doing aligned
64-bit accesses.
You also imply a 32-bit little-endian architecture since you use a array
of 32-bit values as your target address, which together with a random
bit offset means that writes will probably straddle between two 32-bit
words.
However, none of this really matters at all, because the function can
_never_ be time critical: If it is, you simply have no idea at all how
to write fast code.
So, with that out of the way, lets look at your function:
void WriteLongwordBits( unsigned long Value, unsigned int BitCount,
unsigned long int *DestAddress, unsigned int DestBitIndex )
{
/* Assumptions for this function:
* 1. unsigned long is 32-bit
* 2. little endian: bits are numbered from the first/least
significant (0) up to the msb (31)
* 3. Bit 32 == bit 0 in the next 32-bit word
*/
/* Normalize bit index */
DestAddress += DestBitIndex >> 5;
DestBitIndex &= 31;
/* Where is the last bit going to end up? */
unsigned int lastbit = BitCount + DestBitIndex - 1;
/* This mask will be zero starting at the first inserted bit */
unsigned int bottom_mask = ((unsigned int) -1) >> DestBitIndex;
/* Zero mask for bits after the last bit inserted */
/* The masked ****ft count is implicit on x86! */
unsigned int top_mask = ((unsigned int) -1) >> (lastbit & 31);
/* One or two affected words? */
if (lastbit >= 32) {
DestAddress[0] = (DestAddress[0] & bottom_mask) |
(Value << DestBitIndex);
DestAddress[1] = (DestAddress[1] & ~top_mask) |
(Value >> (32-DestBitIndex));
}
else { /* It all fits in a single word! */
DestAddress[0] = (DestAddress[0] & (bottom_mask | ~top_mask)) |
(Value << DestBitIndex);
}
}
Terje
--
- <Terje.Mathisen@[EMAIL PROTECTED]
>
"almost all programming can be viewed as an exercise in caching"


|