Fastest way to turn on/off a bit in memory ?

  • Follow


Hello,

I need two fast procedures which can turn a bit on and off as fast as 
possible with the following prototypes:

procedure TurnBitOn( BaseAddress : pointer; BitIndex : byte );
begin

end;

procedure TurnBitOff( BaseAddress : pointer; BitIndex : byte );
begin

end;

BaseAddress points to a memory block.

BitIndex ranges from 0 to 255, being the bit offset from the base address.

What is the fastest code in Delphi (win32) ?

What is the fastest code in Assembler (win32) ?

All entries will be benchmarked and results posted.

Code should work on 286, 386, 486, pentium, pentium pro, pentium iii, amd x2 
3800, etc.

Specific code for specific cpu's is also interesting. Especially if cpu 
detection code is provided.

For specific code please specify for which cpu's it works.

I look forward to your entries in this modest contest. I shall later also 
provide my own slow implementation but for now I do not want to influence 
the entries.

Bye,
  Skybuck. 

0
Reply Skybuck 1/4/2007 3:14:50 PM

"Skybuck Flying"  <spamtrap@crayne.org> wrote:
>
>I need two fast procedures which can turn a bit on and off as fast as 
>possible with the following prototypes:
>
>procedure TurnBitOn( BaseAddress : pointer; BitIndex : byte );
>begin
>
>end;
>
>procedure TurnBitOff( BaseAddress : pointer; BitIndex : byte );
>begin
>
>end;

The right way to do this in Delphi is to define BaseAddress as a "set of
byte".  Then, TurnBitOn becomes
  BaseAddress := BaseAddress + BitIndex;
and TurnBitOff becomes
  BaseAddress := BaseAddress - BitIndex;

IsBitSet becomes
  IsBitSet := BitIndex in BaseAddress;
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

0
Reply Tim 1/5/2007 5:35:11 AM


"Tim Roberts" <spamtrap@crayne.org> wrote in message 
news:ngorp2ldm999c22m4gj528b7ibolh12bqj@4ax.com...
> "Skybuck Flying"  <spamtrap@crayne.org> wrote:
>>
>>I need two fast procedures which can turn a bit on and off as fast as
>>possible with the following prototypes:
>>
>>procedure TurnBitOn( BaseAddress : pointer; BitIndex : byte );
>>begin
>>
>>end;
>>
>>procedure TurnBitOff( BaseAddress : pointer; BitIndex : byte );
>>begin
>>
>>end;
>
> The right way to do this in Delphi is to define BaseAddress as a "set of
> byte".  Then, TurnBitOn becomes
>  BaseAddress := BaseAddress + BitIndex;
> and TurnBitOff becomes
>  BaseAddress := BaseAddress - BitIndex;
>
> IsBitSet becomes
>  IsBitSet := BitIndex in BaseAddress;
> -- 
> Tim Roberts, timr@probo.com
> Providenza & Boekelheide, Inc.
>

Ahhhhh such a pleasure =D

The first contester (besides myself ;)) has entered the contest =D

I have turned your idea and code into an implementation, if you are afraid I 
did not implement it optimal you must add your implementation otherwise my 
implementation of your concept code/idea will have to suffice ;) :) I have 
done my best to make it as short as possible though ;) :)

Bye,
  Skybuck. 

0
Reply Skybuck 1/5/2007 7:43:37 AM

"Skybuck Flying"  <spamtrap@crayne.org> wrote:
>
>I have turned your idea and code into an implementation, if you are afraid I 
>did not implement it optimal you must add your implementation otherwise my 
>implementation of your concept code/idea will have to suffice ;) :) I have 
>done my best to make it as short as possible though ;) :)

I used to be a studly Delphi guru, but since I got turned on to Python, I
don't even have Delphi installed any more.
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

0
Reply Tim 1/7/2007 5:37:47 AM

"Skybuck Flying" <spamtrap@crayne.org> wrote in message
news:enj5kv$6ll$1@news6.zwoll1.ov.home.nl...
> Hello,
>
> I need two fast procedures which can turn a bit on and off as fast as
> possible with the following prototypes:
>
> procedure TurnBitOn( BaseAddress : pointer; BitIndex : byte );
> begin
>
> end;
>
> procedure TurnBitOff( BaseAddress : pointer; BitIndex : byte );
> begin
>
> end;
>
> BaseAddress points to a memory block.
>
> BitIndex ranges from 0 to 255, being the bit offset from the base address.
>
> What is the fastest code in Delphi (win32) ?

The obvious way to do it with Delphi is to use a set:

type
  PByteSet = ^TByteSet;
  TByteSet = set of [0..255];

procedure TurnBitOn(BaseAddress: Pointer; BitIndex: Byte);
var
  P: PByteSet absolute BaseAddress;
begin
  Include(P^, BitIndex);
end;

procedure TurnBitOff(BaseAddress: Pointer; BitIndex: Byte);
var
  P: PByteSet absolute BaseAddress;
begin
  Exclude(P^, BitIndex);
end;

function GetBitState(BaseAddress: Pointer; BitIndex: Byte): Boolean;
var
  P: PByteSet absolute BaseAddress;
begin
  GetBitState := BitIndex in P^;
end;

Untested.

-- 
Jay

Jason Burgon - author of Graphic Vision
http://homepage.ntlworld.com/gvision

0
Reply Jason 7/7/2007 12:36:57 AM

"Tim Roberts" wrote:
: I used to be a studly Delphi guru, but since I got turned on to Python,
: I don't even have Delphi installed any more.

I've avoided Python for awhile. You're using it in a Windows OS?
Don't know anything about it, except for the fact that I ran across
an application or two in the last year that requires it to compile the
apps.

-- 
Jim Carlock

0
Reply Jim 7/7/2007 2:20:27 AM

The fastest way is to execute in-line code, not call a function.
First:
Break address into word or byte number and bit number remainder (could
be already available as register usage, else a long shift).

Second:
Use bit number to index a table of n words/bytes (in form 00010000
etc) where n is the word/byte width of memory you are working with,
(looks like 16 bits since OP mentions 256), and pick up the word (or
byte) from the table.

Third:
Use operation XOR to flip, OR to set, AND inverse to turn off
(alternative but table or one more operation).

"Operation" this word/byte to memory indexed by the shift result of
operation 1).



0
Reply Terence 7/7/2007 11:36:35 PM

"Jim Carlock"  <spamtrap@crayne.org> wrote:
>
>"Tim Roberts" wrote:
>: I used to be a studly Delphi guru, but since I got turned on to Python,
>: I don't even have Delphi installed any more.
>
>I've avoided Python for awhile. You're using it in a Windows OS?

I use it on both Windows and Linux.  It is clearly my language of choice
for web applications, and I reach for it on Windows any time I need the
kind of shell tools that one used to do in sh and awk (or perl).

I write drivers for a living, and those drivers often need test harnesses
and demo applications.  I do those almost exclusively in Python, partly
because it's a quick prototype language, and partly because I can actually
read what I've written.

>Don't know anything about it, except for the fact that I ran across
>an application or two in the last year that requires it to compile the
>apps.

Yes.  The RealPlayer components (now called Helix) use a rather complicated
Python-based build system.
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

0
Reply Tim 7/8/2007 1:36:02 AM

Thank you for your entry :)

1. Your entry has been verified OK and thus is entered into the contest.

2. The fastest entry is 70% faster than your method.

Your method uses:

2 moves.
1 BTS.
3 pops.

Also a little typo/bug has been corrected for you:

"Jason Burgon" <spamtrap@crayne.org> wrote in message 
news:JeBji.3345$XR.3105@newsfe4-win.ntli.net...
> "Skybuck Flying" <spamtrap@crayne.org> wrote in message
> news:enj5kv$6ll$1@news6.zwoll1.ov.home.nl...
>> Hello,
>>
>> I need two fast procedures which can turn a bit on and off as fast as
>> possible with the following prototypes:
>>
>> procedure TurnBitOn( BaseAddress : pointer; BitIndex : byte );
>> begin
>>
>> end;
>>
>> procedure TurnBitOff( BaseAddress : pointer; BitIndex : byte );
>> begin
>>
>> end;
>>
>> BaseAddress points to a memory block.
>>
>> BitIndex ranges from 0 to 255, being the bit offset from the base 
>> address.
>>
>> What is the fastest code in Delphi (win32) ?
>
> The obvious way to do it with Delphi is to use a set:
>
> type
>  PByteSet = ^TByteSet;
>  TByteSet = set of [0..255];

^^^ typo/bug ^^^

Should be:

TByteSet = set of 0..255;

>
> procedure TurnBitOn(BaseAddress: Pointer; BitIndex: Byte);
> var
>  P: PByteSet absolute BaseAddress;
> begin
>  Include(P^, BitIndex);
> end;

^^^ only this method tested ^^^

> procedure TurnBitOff(BaseAddress: Pointer; BitIndex: Byte);
> var
>  P: PByteSet absolute BaseAddress;
> begin
>  Exclude(P^, BitIndex);
> end;
>
> function GetBitState(BaseAddress: Pointer; BitIndex: Byte): Boolean;
> var
>  P: PByteSet absolute BaseAddress;
> begin
>  GetBitState := BitIndex in P^;
> end;

Bye,
  Skybuck. 

0
Reply Skybuck 7/8/2007 4:47:15 AM

Skybuck Flying wrote:
> "Jason Burgon" <spamtrap@crayne.org> wrote in message 
> news:JeBji.3345$XR.3105@newsfe4-win.ntli.net...
>> type
>>  PByteSet = ^TByteSet;
>>  TByteSet = set of [0..255];
> 
> ^^^ typo/bug ^^^
> 
> Should be:
> 
> TByteSet = set of 0..255;

Even better, TByteSet = set of byte;

Cheers,
Nicholas Sherlock

0
Reply Nicholas 7/8/2007 9:23:28 AM

"Nicholas Sherlock" <spamtrap@crayne.org> wrote in message
news:f6qaa9$1tl$1@lust.ihug.co.nz...
> Skybuck Flying wrote:
> > "Jason Burgon" <spamtrap@crayne.org> wrote in message
> > news:JeBji.3345$XR.3105@newsfe4-win.ntli.net...
> >> type
> >>  PByteSet = ^TByteSet;
> >>  TByteSet = set of [0..255];
> >
> > ^^^ typo/bug ^^^
> >
> > Should be:
> >
> > TByteSet = set of 0..255;

Yep, I'm always doing that, but then again I have only being programming in
Pascal for about 15 years. ;-)

> Even better, TByteSet = set of byte;

Yes. That's the best definition.

Jay

--

Jason Burgon - author of Graphic Vision
http://homepage.ntlworld.com/gvision

0
Reply Jason 7/9/2007 12:15:39 AM

I see everybody ignored by 3(4?) asm instruction method.
Since we supply market research software since 1972, we use the method
described for:-
1) setting a logical bit reply in a logical array of bits, to 0 or 1
(no,yes)
2) noting a presence to be counted once only (OR=setting bit in
presence array to 1)
3) testing for a logical condition (TEST bit)
4) covering conditional cases only once en a selected set (.NOT. bit)
5) Bit flipping (XOR).
6) testing for multiple conditions progressively (AND bits or words).

And since we can access bits in parallel as words, we can do most
required opeerations in parallel too, which why we devised (not
invented) the method when we started.

Note: I have several patents in image processing and machine vision;
the development of techniques used hand-coded programs before making
hardwired (fast) cpus for the jobs.

0
Reply Terence 7/13/2007 12:01:37 AM

11 Replies
135 Views

(page loaded in 0.124 seconds)

Similiar Articles:


















7/25/2012 4:23:08 PM


Reply: