Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Borland Delphi > Re: Wanna do a ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 28 Topic 3723 of 3900
Post > Topic >>

Re: Wanna do a WriteLongwordBits contest ?

by Terje Mathisen <terje.mathisen@[EMAIL PROTECTED] > May 4, 2008 at 05:30 PM

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"
 




 28 Posts in Topic:
Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-02 10:47:32 
Re: Wanna do a WriteLongwordBits contest ?
MitchAlsup <MitchAlsup  2008-05-03 14:05:49 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-04 14:05:56 
Re: Wanna do a WriteLongwordBits contest ?
Terje Mathisen <terje.  2008-05-04 17:30:07 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-04 21:25:04 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-05 15:11:42 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-05 15:21:01 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-05 15:39:46 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-05 15:46:18 
Re: Wanna do a WriteLongwordBits contest ?
Terje Mathisen <terje.  2008-05-06 08:31:55 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-06 10:41:53 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-06 14:41:26 
Re: Wanna do a WriteLongwordBits contest ?
Terje Mathisen <terje.  2008-05-06 17:19:16 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-06 20:04:26 
Re: Wanna do a WriteLongwordBits contest ?
Terje Mathisen <terje.  2008-05-07 09:35:30 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-07 13:31:05 
Re: Wanna do a WriteLongwordBits contest ?
"Skybuck Flying"  2008-05-07 14:34:37 
Re: Wanna do a WriteLongwordBits contest ? (Skybuck's SimInt64 C
"Skybuck Flying"  2008-05-05 21:34:20 
Re: Wanna do a WriteLongwordBits contest ? (Skybuck's SimInt64 C
"Skybuck Flying"  2008-05-05 22:02:51 
Re: Wanna do a WriteLongwordBits contest ? (Skybuck's Second Ent
"Skybuck Flying"  2008-05-05 19:38:42 
Re: Wanna do a WriteLongwordBits contest ? (Skybuck's Third Entr
"Skybuck Flying"  2008-05-05 20:02:10 
Re: Wanna do a WriteLongwordBits contest ? (Skybuck's Third Entr
"Skybuck Flying"  2008-05-05 20:35:17 
Re: Wanna do a WriteLongwordBits contest ? (Skybuck's Fourth Ent
"Skybuck Flying"  2008-05-06 21:50:17 
Re: Wanna do a WriteLongwordBits contest ? (Skybuck's Fourth Ent
"Skybuck Flying"  2008-05-06 21:53:40 
Re: Wanna do a WriteLongwordBits contest ? (Skybuck's Fiveth Ent
"Skybuck Flying"  2008-05-06 22:54:22 
Re: Wanna do a WriteLongwordBits contest ? (Skybuck's Fiveth Ent
"Skybuck Flying"  2008-05-06 23:10:52 
Re: Wanna do a WriteLongwordBits contest ? (Benchmark, Verificat
"Skybuck Flying"  2008-05-07 19:09:04 
Re: Wanna do a WriteLongwordBits contest ? (Benchmark, Verificat
"Skybuck Flying"  2008-05-08 02:07:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Sep 6 22:40:06 CDT 2008.