Connecting Tech Pros Worldwide Forums | Help | Site Map

bitwise rotation

Shawn B.
Guest
 
Posts: n/a
#1: Nov 15 '05
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
....
public uint Value;
....
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I
presume I'd have to have knowledge of the intended size of the value and do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?


Thanks,
Shawn



Robert Jeppesen
Guest
 
Posts: n/a
#2: Nov 15 '05

re: bitwise rotation


How about:

public uint RotateRight16(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (16-Count)))&0xffff;
}
public uint RotateRight8(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (8-Count)))&0xff;
}

public uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (16-Count)))&0xffff;
}
public uint RotateLeft8(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (8-Count)))&0xff;
}

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:O9%23ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...[color=blue]
> Greetings,
>
> I am simulating an assembly language bit rotation in C# and it works
> wonderfully
>
> ---------
> ...
> public uint Value;
> ...
> public uint RotateRight(byte count) {
> Value = ((Value >> count) | (Value << (32-count)));
> return Value;
> }
> ---------
>
> as long is the value is 32-bit or 64-bit (because of a limitation of the
> shift operator). I need it to work on an 8-bit or 16-bit value, as well. I
> presume I'd have to have knowledge of the intended size of the value and do
> the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
> the extra bits if there are any. That seems like a lot of work.
>
> Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> value?
>
>
> Thanks,
> Shawn
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#3: Nov 15 '05

re: bitwise rotation


Oops, forgot to make sure that value is within limits:

public static uint RotateRight16(uint Value, byte Count)
{
return (((Value&0xffff) >> Count) | (Value << (16-Count)))&0xffff;
}
public static uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | ((Value&0xffff) >> (16-Count)))&0xffff;
}

etc for 8-bit.

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message news:eEHCD$qjDHA.1408@TK2MSFTNGP11.phx.gbl...[color=blue]
> How about:
>
> public uint RotateRight16(uint Value, byte Count)
> {
> return ((Value >> Count) | (Value << (16-Count)))&0xffff;
> }
> public uint RotateRight8(uint Value, byte Count)
> {
> return ((Value >> Count) | (Value << (8-Count)))&0xff;
> }
>
> public uint RotateLeft16(uint Value, byte Count)
> {
> return ((Value << Count) | (Value >> (16-Count)))&0xffff;
> }
> public uint RotateLeft8(uint Value, byte Count)
> {
> return ((Value << Count) | (Value >> (8-Count)))&0xff;
> }
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message news:O9%23ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...[color=green]
> > Greetings,
> >
> > I am simulating an assembly language bit rotation in C# and it works
> > wonderfully
> >
> > ---------
> > ...
> > public uint Value;
> > ...
> > public uint RotateRight(byte count) {
> > Value = ((Value >> count) | (Value << (32-count)));
> > return Value;
> > }
> > ---------
> >
> > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > shift operator). I need it to work on an 8-bit or 16-bit value, as well. I
> > presume I'd have to have knowledge of the intended size of the value and do
> > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
> > the extra bits if there are any. That seems like a lot of work.
> >
> > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> > value?
> >
> >
> > Thanks,
> > Shawn
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#4: Nov 15 '05

re: bitwise rotation


Hard to believe it was so simple, I tried that... except where you're
16-count is, I left it at 64 bits with the 8-bit mask, that explains why it
was wrong.

Thanks very much,
Shawn


"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:eEHCD$qjDHA.1408@TK2MSFTNGP11.phx.gbl...[color=blue]
> How about:
>
> public uint RotateRight16(uint Value, byte Count)
> {
> return ((Value >> Count) | (Value << (16-Count)))&0xffff;
> }
> public uint RotateRight8(uint Value, byte Count)
> {
> return ((Value >> Count) | (Value << (8-Count)))&0xff;
> }
>
> public uint RotateLeft16(uint Value, byte Count)
> {
> return ((Value << Count) | (Value >> (16-Count)))&0xffff;
> }
> public uint RotateLeft8(uint Value, byte Count)
> {
> return ((Value << Count) | (Value >> (8-Count)))&0xff;
> }
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:O9%23ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...[color=blue][color=green]
> > Greetings,
> >
> > I am simulating an assembly language bit rotation in C# and it works
> > wonderfully
> >
> > ---------
> > ...
> > public uint Value;
> > ...
> > public uint RotateRight(byte count) {
> > Value = ((Value >> count) | (Value << (32-count)));
> > return Value;
> > }
> > ---------
> >
> > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > shift operator). I need it to work on an 8-bit or 16-bit value, as[/color][/color]
well. I[color=blue][color=green]
> > presume I'd have to have knowledge of the intended size of the value and[/color][/color]
do[color=blue][color=green]
> > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and[/color][/color]
clear[color=blue][color=green]
> > the extra bits if there are any. That seems like a lot of work.
> >
> > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> > value?
> >
> >
> > Thanks,
> > Shawn
> >
> >[/color]
>
>[/color]


Bob Powell [MVP]
Guest
 
Posts: n/a
#5: Nov 15 '05

re: bitwise rotation


So, you're writing an emulator for which processor??

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm



"Shawn B." <leabre@html.com> wrote in message
news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...[color=blue]
> Greetings,
>
> I am simulating an assembly language bit rotation in C# and it works
> wonderfully
>
> ---------
> ...
> public uint Value;
> ...
> public uint RotateRight(byte count) {
> Value = ((Value >> count) | (Value << (32-count)));
> return Value;
> }
> ---------
>
> as long is the value is 32-bit or 64-bit (because of a limitation of the
> shift operator). I need it to work on an 8-bit or 16-bit value, as well.[/color]
I[color=blue]
> presume I'd have to have knowledge of the intended size of the value and[/color]
do[color=blue]
> the shift and move the bit and the 0 or 7, or 0 or 15 bit position and[/color]
clear[color=blue]
> the extra bits if there are any. That seems like a lot of work.
>
> Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> value?
>
>
> Thanks,
> Shawn
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#6: Nov 15 '05

re: bitwise rotation


Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
16-bit 65816. In particular, I'm doing this so I can debug (I have the
ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware
(such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
plugins can treat an area of memory as a dedicated "something" such as text
display or graphics display, etc. For now, I'm concerned not even with the
text display. My end result, is actually more akin to being able to debug
my NES games I've been into writing lately. Also I want to be ready with my
own debugger for Andre LaMothe's upcoming XGameStation console (don't know
whether he'll have a debugger or not) but I want a debugger that allows me
to change the code while it's being debugged and my simulator allows for it
(as long as the source code file is available -- else if it was loading as a
..bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable)
assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast.
I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm
primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
but also wanted it to target the .NET framework (as much as is reasonable
and I'll find out what that really means when its mature enough to start
using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
16-bit processor as I haven't deciphered the specs for the CPU yet (the
8-bit CPU is very easy compared to the 16-bit version of the processor). I
ultimately will make this Interfacable so I can write a Z80 core, as well.

These bit manipulations are the utmost inner-loop so I have to make them
tight. Looking at the JIT'd code for these methods seems to be decent but I
have to make too many function calls (read, jumps) for my tastes but doesn't
seem to be hurting me at the moment). Since I can't inline in .NET, I'll
have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate
8-bit and 16-bit shifts and rotates. I'm also trying to work out a better
way for simulating memory manipulation. Now I'm creating a string of (64kb
in length) but manipulating each byte by substrings has got to be expensive.
I'm looking into whether a byte array would be better.


Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string
of 1's and 0's). I know now have to read the binary back to a numeric, and
allow a way to get/set/toggle individual bits.


Thanks,
Shawn


"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...[color=blue]
> So, you're writing an emulator for which processor??
>
> --
> Bob Powell [MVP]
> C#, System.Drawing
>
> September's edition of Well Formed is now available.
> http://www.bobpowell.net/currentissue.htm
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/gdiplus_faq.htm
>
>
>
> "Shawn B." <leabre@html.com> wrote in message
> news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...[color=green]
> > Greetings,
> >
> > I am simulating an assembly language bit rotation in C# and it works
> > wonderfully
> >
> > ---------
> > ...
> > public uint Value;
> > ...
> > public uint RotateRight(byte count) {
> > Value = ((Value >> count) | (Value << (32-count)));
> > return Value;
> > }
> > ---------
> >
> > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > shift operator). I need it to work on an 8-bit or 16-bit value, as[/color][/color]
well.[color=blue]
> I[color=green]
> > presume I'd have to have knowledge of the intended size of the value and[/color]
> do[color=green]
> > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and[/color]
> clear[color=green]
> > the extra bits if there are any. That seems like a lot of work.
> >
> > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> > value?
> >
> >
> > Thanks,
> > Shawn
> >
> >[/color]
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#7: Nov 15 '05

re: bitwise rotation


Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=blue]
> Don't miss a thing, d'ya? =))
>
> I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
> 16-bit 65816. In particular, I'm doing this so I can debug (I have the
> ability to modify the assembly language while the debugger is running
> (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> instructions. I'm not designing it to be a particular piece of hardware
> (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> plugins can treat an area of memory as a dedicated "something" such as text
> display or graphics display, etc. For now, I'm concerned not even with the
> text display. My end result, is actually more akin to being able to debug
> my NES games I've been into writing lately. Also I want to be ready with my
> own debugger for Andre LaMothe's upcoming XGameStation console (don't know
> whether he'll have a debugger or not) but I want a debugger that allows me
> to change the code while it's being debugged and my simulator allows for it
> (as long as the source code file is available -- else if it was loading as a
> .bin file then it won't be editable during a debug session).
>
> So it's really more than a simulator. It's an integrated (non-seperable)
> assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast.
> I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm
> primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
> but also wanted it to target the .NET framework (as much as is reasonable
> and I'll find out what that really means when its mature enough to start
> using reliably).
>
> I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
> GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> 16-bit processor as I haven't deciphered the specs for the CPU yet (the
> 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> ultimately will make this Interfacable so I can write a Z80 core, as well.
>
> These bit manipulations are the utmost inner-loop so I have to make them
> tight. Looking at the JIT'd code for these methods seems to be decent but I
> have to make too many function calls (read, jumps) for my tastes but doesn't
> seem to be hurting me at the moment). Since I can't inline in .NET, I'll
> have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> generate the best code for the rotates and shifts because I have to emulate
> 8-bit and 16-bit shifts and rotates. I'm also trying to work out a better
> way for simulating memory manipulation. Now I'm creating a string of (64kb
> in length) but manipulating each byte by substrings has got to be expensive.
> I'm looking into whether a byte array would be better.
>
>
> Thanks for the help,
> if interested, I'll post my binary class here for all to see.
>
> I've completed the Rotates, Shift, converting numerics into binary (string
> of 1's and 0's). I know now have to read the binary back to a numeric, and
> allow a way to get/set/toggle individual bits.
>
>
> Thanks,
> Shawn
>
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...[color=green]
> > So, you're writing an emulator for which processor??
> >
> > --
> > Bob Powell [MVP]
> > C#, System.Drawing
> >
> > September's edition of Well Formed is now available.
> > http://www.bobpowell.net/currentissue.htm
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdiplus_faq.htm
> >
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message
> > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > Greetings,
> > >
> > > I am simulating an assembly language bit rotation in C# and it works
> > > wonderfully
> > >
> > > ---------
> > > ...
> > > public uint Value;
> > > ...
> > > public uint RotateRight(byte count) {
> > > Value = ((Value >> count) | (Value << (32-count)));
> > > return Value;
> > > }
> > > ---------
> > >
> > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > shift operator). I need it to work on an 8-bit or 16-bit value, as[/color][/color]
> well.[color=green]
> > I[color=darkred]
> > > presume I'd have to have knowledge of the intended size of the value and[/color]
> > do[color=darkred]
> > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and[/color]
> > clear[color=darkred]
> > > the extra bits if there are any. That seems like a lot of work.
> > >
> > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> > > value?
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#8: Nov 15 '05

re: bitwise rotation


Robert,

Youre suggestions have transformed my operations into 1-liners from 5
liners... you'll be the first to be notified (on these groups, of course).

I've opted to create a series of classes, Binary8, Binary16, Binary32,
Binary64 (all are unsigned).

I need to know, when you say "int x = 10;" is that a compiler trick or is
there a way to create my own type that can do that, for example, "Binary8 x
= 0xFF;"???


Thanks,
Shawn



"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=blue]
> Do post it here!
> I'd be interested to see, as I'm sure many others would.
> You'll probably get a ton of optimization hints from it. :)
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=blue][color=green]
> > Don't miss a thing, d'ya? =))
> >
> > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and[/color][/color]
the[color=blue][color=green]
> > 16-bit 65816. In particular, I'm doing this so I can debug (I have the
> > ability to modify the assembly language while the debugger is running
> > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > instructions. I'm not designing it to be a particular piece of hardware
> > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so[/color][/color]
my[color=blue][color=green]
> > plugins can treat an area of memory as a dedicated "something" such as[/color][/color]
text[color=blue][color=green]
> > display or graphics display, etc. For now, I'm concerned not even with[/color][/color]
the[color=blue][color=green]
> > text display. My end result, is actually more akin to being able to[/color][/color]
debug[color=blue][color=green]
> > my NES games I've been into writing lately. Also I want to be ready[/color][/color]
with my[color=blue][color=green]
> > own debugger for Andre LaMothe's upcoming XGameStation console (don't[/color][/color]
know[color=blue][color=green]
> > whether he'll have a debugger or not) but I want a debugger that allows[/color][/color]
me[color=blue][color=green]
> > to change the code while it's being debugged and my simulator allows for[/color][/color]
it[color=blue][color=green]
> > (as long as the source code file is available -- else if it was loading[/color][/color]
as a[color=blue][color=green]
> > .bin file then it won't be editable during a debug session).
> >
> > So it's really more than a simulator. It's an integrated[/color][/color]
(non-seperable)[color=blue][color=green]
> > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color][/color]
beast.[color=blue][color=green]
> > I chose C# because it's not the kind of thing I'd want to do in VB.NET[/color][/color]
(I'm[color=blue][color=green]
> > primarly a VB.NET programmer) but didn't feel like doing it in Managed[/color][/color]
C++[color=blue][color=green]
> > but also wanted it to target the .NET framework (as much as is[/color][/color]
reasonable[color=blue][color=green]
> > and I'll find out what that really means when its mature enough to start
> > using reliably).
> >
> > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
> > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating[/color][/color]
the[color=blue][color=green]
> > 16-bit processor as I haven't deciphered the specs for the CPU yet (the
> > 8-bit CPU is very easy compared to the 16-bit version of the processor).[/color][/color]
I[color=blue][color=green]
> > ultimately will make this Interfacable so I can write a Z80 core, as[/color][/color]
well.[color=blue][color=green]
> >
> > These bit manipulations are the utmost inner-loop so I have to make them
> > tight. Looking at the JIT'd code for these methods seems to be decent[/color][/color]
but I[color=blue][color=green]
> > have to make too many function calls (read, jumps) for my tastes but[/color][/color]
doesn't[color=blue][color=green]
> > seem to be hurting me at the moment). Since I can't inline in .NET,[/color][/color]
I'll[color=blue][color=green]
> > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > generate the best code for the rotates and shifts because I have to[/color][/color]
emulate[color=blue][color=green]
> > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color][/color]
better[color=blue][color=green]
> > way for simulating memory manipulation. Now I'm creating a string of[/color][/color]
(64kb[color=blue][color=green]
> > in length) but manipulating each byte by substrings has got to be[/color][/color]
expensive.[color=blue][color=green]
> > I'm looking into whether a byte array would be better.
> >
> >
> > Thanks for the help,
> > if interested, I'll post my binary class here for all to see.
> >
> > I've completed the Rotates, Shift, converting numerics into binary[/color][/color]
(string[color=blue][color=green]
> > of 1's and 0's). I know now have to read the binary back to a numeric,[/color][/color]
and[color=blue][color=green]
> > allow a way to get/set/toggle individual bits.
> >
> >
> > Thanks,
> > Shawn
> >
> >
> > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...[color=darkred]
> > > So, you're writing an emulator for which processor??
> > >
> > > --
> > > Bob Powell [MVP]
> > > C#, System.Drawing
> > >
> > > September's edition of Well Formed is now available.
> > > http://www.bobpowell.net/currentissue.htm
> > >
> > > Answer those GDI+ questions with the GDI+ FAQ
> > > http://www.bobpowell.net/gdiplus_faq.htm
> > >
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message
> > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > Greetings,
> > > >
> > > > I am simulating an assembly language bit rotation in C# and it works
> > > > wonderfully
> > > >
> > > > ---------
> > > > ...
> > > > public uint Value;
> > > > ...
> > > > public uint RotateRight(byte count) {
> > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > return Value;
> > > > }
> > > > ---------
> > > >
> > > > as long is the value is 32-bit or 64-bit (because of a limitation of[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > shift operator). I need it to work on an 8-bit or 16-bit value, as[/color]
> > well.[color=darkred]
> > > I
> > > > presume I'd have to have knowledge of the intended size of the value[/color][/color][/color]
and[color=blue][color=green][color=darkred]
> > > do
> > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position[/color][/color][/color]
and[color=blue][color=green][color=darkred]
> > > clear
> > > > the extra bits if there are any. That seems like a lot of work.
> > > >
> > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or[/color][/color][/color]
64-bit[color=blue][color=green][color=darkred]
> > > > value?
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#9: Nov 15 '05

re: bitwise rotation


Shawn B. <leabre@html.com> wrote:[color=blue]
> I need to know, when you say "int x = 10;" is that a compiler trick or is
> there a way to create my own type that can do that, for example, "Binary8 x
> = 0xFF;"???[/color]

It's not really a compiler trick, it's just that the type of 10 is int,
so the compiler can just assign that value to the variable.

I believe if you give the Binary8 class an implicit conversion from
int, you could get away with it.

I'm not sure whether or not you could get away with giving an implicit
conversion from byte, letting the compiler figure out that it can
convert a constant expression of type int to byte, and then from byte
to Binary8 - if that works though, it would remove the need for range
checking in your conversion.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Robert Jeppesen
Guest
 
Posts: n/a
#10: Nov 15 '05

re: bitwise rotation


In addition to Jon's response,
No, you can't overload the = operator.
Found a nice summary of what can be overloaded:
http://www.csharphelp.com/archives/archive135.html

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:ez5W57ujDHA.1004@tk2msftngp13.phx.gbl...[color=blue]
> Robert,
>
> Youre suggestions have transformed my operations into 1-liners from 5
> liners... you'll be the first to be notified (on these groups, of course).
>
> I've opted to create a series of classes, Binary8, Binary16, Binary32,
> Binary64 (all are unsigned).
>
> I need to know, when you say "int x = 10;" is that a compiler trick or is
> there a way to create my own type that can do that, for example, "Binary8 x
> = 0xFF;"???
>
>
> Thanks,
> Shawn
>
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=green]
> > Do post it here!
> > I'd be interested to see, as I'm sure many others would.
> > You'll probably get a ton of optimization hints from it. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=green][color=darkred]
> > > Don't miss a thing, d'ya? =))
> > >
> > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and[/color][/color]
> the[color=green][color=darkred]
> > > 16-bit 65816. In particular, I'm doing this so I can debug (I have the
> > > ability to modify the assembly language while the debugger is running
> > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > instructions. I'm not designing it to be a particular piece of hardware
> > > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so[/color][/color]
> my[color=green][color=darkred]
> > > plugins can treat an area of memory as a dedicated "something" such as[/color][/color]
> text[color=green][color=darkred]
> > > display or graphics display, etc. For now, I'm concerned not even with[/color][/color]
> the[color=green][color=darkred]
> > > text display. My end result, is actually more akin to being able to[/color][/color]
> debug[color=green][color=darkred]
> > > my NES games I've been into writing lately. Also I want to be ready[/color][/color]
> with my[color=green][color=darkred]
> > > own debugger for Andre LaMothe's upcoming XGameStation console (don't[/color][/color]
> know[color=green][color=darkred]
> > > whether he'll have a debugger or not) but I want a debugger that allows[/color][/color]
> me[color=green][color=darkred]
> > > to change the code while it's being debugged and my simulator allows for[/color][/color]
> it[color=green][color=darkred]
> > > (as long as the source code file is available -- else if it was loading[/color][/color]
> as a[color=green][color=darkred]
> > > .bin file then it won't be editable during a debug session).
> > >
> > > So it's really more than a simulator. It's an integrated[/color][/color]
> (non-seperable)[color=green][color=darkred]
> > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color][/color]
> beast.[color=green][color=darkred]
> > > I chose C# because it's not the kind of thing I'd want to do in VB.NET[/color][/color]
> (I'm[color=green][color=darkred]
> > > primarly a VB.NET programmer) but didn't feel like doing it in Managed[/color][/color]
> C++[color=green][color=darkred]
> > > but also wanted it to target the .NET framework (as much as is[/color][/color]
> reasonable[color=green][color=darkred]
> > > and I'll find out what that really means when its mature enough to start
> > > using reliably).
> > >
> > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
> > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating[/color][/color]
> the[color=green][color=darkred]
> > > 16-bit processor as I haven't deciphered the specs for the CPU yet (the
> > > 8-bit CPU is very easy compared to the 16-bit version of the processor).[/color][/color]
> I[color=green][color=darkred]
> > > ultimately will make this Interfacable so I can write a Z80 core, as[/color][/color]
> well.[color=green][color=darkred]
> > >
> > > These bit manipulations are the utmost inner-loop so I have to make them
> > > tight. Looking at the JIT'd code for these methods seems to be decent[/color][/color]
> but I[color=green][color=darkred]
> > > have to make too many function calls (read, jumps) for my tastes but[/color][/color]
> doesn't[color=green][color=darkred]
> > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color][/color]
> I'll[color=green][color=darkred]
> > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > generate the best code for the rotates and shifts because I have to[/color][/color]
> emulate[color=green][color=darkred]
> > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color][/color]
> better[color=green][color=darkred]
> > > way for simulating memory manipulation. Now I'm creating a string of[/color][/color]
> (64kb[color=green][color=darkred]
> > > in length) but manipulating each byte by substrings has got to be[/color][/color]
> expensive.[color=green][color=darkred]
> > > I'm looking into whether a byte array would be better.
> > >
> > >
> > > Thanks for the help,
> > > if interested, I'll post my binary class here for all to see.
> > >
> > > I've completed the Rotates, Shift, converting numerics into binary[/color][/color]
> (string[color=green][color=darkred]
> > > of 1's and 0's). I know now have to read the binary back to a numeric,[/color][/color]
> and[color=green][color=darkred]
> > > allow a way to get/set/toggle individual bits.
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > So, you're writing an emulator for which processor??
> > > >
> > > > --
> > > > Bob Powell [MVP]
> > > > C#, System.Drawing
> > > >
> > > > September's edition of Well Formed is now available.
> > > > http://www.bobpowell.net/currentissue.htm
> > > >
> > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > >
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > Greetings,
> > > > >
> > > > > I am simulating an assembly language bit rotation in C# and it works
> > > > > wonderfully
> > > > >
> > > > > ---------
> > > > > ...
> > > > > public uint Value;
> > > > > ...
> > > > > public uint RotateRight(byte count) {
> > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > return Value;
> > > > > }
> > > > > ---------
> > > > >
> > > > > as long is the value is 32-bit or 64-bit (because of a limitation of[/color][/color]
> the[color=green][color=darkred]
> > > > > shift operator). I need it to work on an 8-bit or 16-bit value, as
> > > well.
> > > > I
> > > > > presume I'd have to have knowledge of the intended size of the value[/color][/color]
> and[color=green][color=darkred]
> > > > do
> > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position[/color][/color]
> and[color=green][color=darkred]
> > > > clear
> > > > > the extra bits if there are any. That seems like a lot of work.
> > > > >
> > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or[/color][/color]
> 64-bit[color=green][color=darkred]
> > > > > value?
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Jason Smith
Guest
 
Posts: n/a
#11: Nov 15 '05

re: bitwise rotation


Instead of creating classes, use structs. Or, if you want the best
performance, just use the unsigned integers supported by .NET and C#.
Manipulate the integers using static methods.

Your BinaryX classes are really just unsigned integers with some added
functionality.

"Shawn B." <leabre@html.com> wrote in message
news:ez5W57ujDHA.1004@tk2msftngp13.phx.gbl...[color=blue]
> Robert,
>
> Youre suggestions have transformed my operations into 1-liners from 5
> liners... you'll be the first to be notified (on these groups, of course).
>
> I've opted to create a series of classes, Binary8, Binary16, Binary32,
> Binary64 (all are unsigned).
>
> I need to know, when you say "int x = 10;" is that a compiler trick or is
> there a way to create my own type that can do that, for example, "Binary8[/color]
x[color=blue]
> = 0xFF;"???
>
>
> Thanks,
> Shawn
>
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=green]
> > Do post it here!
> > I'd be interested to see, as I'm sure many others would.
> > You'll probably get a ton of optimization hints from it. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=green][color=darkred]
> > > Don't miss a thing, d'ya? =))
> > >
> > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and[/color][/color]
> the[color=green][color=darkred]
> > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > ability to modify the assembly language while the debugger is running
> > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > instructions. I'm not designing it to be a particular piece of[/color][/color][/color]
hardware[color=blue][color=green][color=darkred]
> > > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins[/color][/color][/color]
so[color=blue]
> my[color=green][color=darkred]
> > > plugins can treat an area of memory as a dedicated "something" such as[/color][/color]
> text[color=green][color=darkred]
> > > display or graphics display, etc. For now, I'm concerned not even[/color][/color][/color]
with[color=blue]
> the[color=green][color=darkred]
> > > text display. My end result, is actually more akin to being able to[/color][/color]
> debug[color=green][color=darkred]
> > > my NES games I've been into writing lately. Also I want to be ready[/color][/color]
> with my[color=green][color=darkred]
> > > own debugger for Andre LaMothe's upcoming XGameStation console (don't[/color][/color]
> know[color=green][color=darkred]
> > > whether he'll have a debugger or not) but I want a debugger that[/color][/color][/color]
allows[color=blue]
> me[color=green][color=darkred]
> > > to change the code while it's being debugged and my simulator allows[/color][/color][/color]
for[color=blue]
> it[color=green][color=darkred]
> > > (as long as the source code file is available -- else if it was[/color][/color][/color]
loading[color=blue]
> as a[color=green][color=darkred]
> > > .bin file then it won't be editable during a debug session).
> > >
> > > So it's really more than a simulator. It's an integrated[/color][/color]
> (non-seperable)[color=green][color=darkred]
> > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color][/color]
> beast.[color=green][color=darkred]
> > > I chose C# because it's not the kind of thing I'd want to do in VB.NET[/color][/color]
> (I'm[color=green][color=darkred]
> > > primarly a VB.NET programmer) but didn't feel like doing it in Managed[/color][/color]
> C++[color=green][color=darkred]
> > > but also wanted it to target the .NET framework (as much as is[/color][/color]
> reasonable[color=green][color=darkred]
> > > and I'll find out what that really means when its mature enough to[/color][/color][/color]
start[color=blue][color=green][color=darkred]
> > > using reliably).
> > >
> > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color][/color]
with[color=blue][color=green][color=darkred]
> > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color][/color]
emulating[color=blue]
> the[color=green][color=darkred]
> > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color][/color]
(the[color=blue][color=green][color=darkred]
> > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color][/color]
processor).[color=blue]
> I[color=green][color=darkred]
> > > ultimately will make this Interfacable so I can write a Z80 core, as[/color][/color]
> well.[color=green][color=darkred]
> > >
> > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color][/color]
them[color=blue][color=green][color=darkred]
> > > tight. Looking at the JIT'd code for these methods seems to be decent[/color][/color]
> but I[color=green][color=darkred]
> > > have to make too many function calls (read, jumps) for my tastes but[/color][/color]
> doesn't[color=green][color=darkred]
> > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color][/color]
> I'll[color=green][color=darkred]
> > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > generate the best code for the rotates and shifts because I have to[/color][/color]
> emulate[color=green][color=darkred]
> > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color][/color]
> better[color=green][color=darkred]
> > > way for simulating memory manipulation. Now I'm creating a string of[/color][/color]
> (64kb[color=green][color=darkred]
> > > in length) but manipulating each byte by substrings has got to be[/color][/color]
> expensive.[color=green][color=darkred]
> > > I'm looking into whether a byte array would be better.
> > >
> > >
> > > Thanks for the help,
> > > if interested, I'll post my binary class here for all to see.
> > >
> > > I've completed the Rotates, Shift, converting numerics into binary[/color][/color]
> (string[color=green][color=darkred]
> > > of 1's and 0's). I know now have to read the binary back to a[/color][/color][/color]
numeric,[color=blue]
> and[color=green][color=darkred]
> > > allow a way to get/set/toggle individual bits.
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > So, you're writing an emulator for which processor??
> > > >
> > > > --
> > > > Bob Powell [MVP]
> > > > C#, System.Drawing
> > > >
> > > > September's edition of Well Formed is now available.
> > > > http://www.bobpowell.net/currentissue.htm
> > > >
> > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > >
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > Greetings,
> > > > >
> > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color][/color]
works[color=blue][color=green][color=darkred]
> > > > > wonderfully
> > > > >
> > > > > ---------
> > > > > ...
> > > > > public uint Value;
> > > > > ...
> > > > > public uint RotateRight(byte count) {
> > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > return Value;
> > > > > }
> > > > > ---------
> > > > >
> > > > > as long is the value is 32-bit or 64-bit (because of a limitation[/color][/color][/color]
of[color=blue]
> the[color=green][color=darkred]
> > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color][/color]
as[color=blue][color=green][color=darkred]
> > > well.
> > > > I
> > > > > presume I'd have to have knowledge of the intended size of the[/color][/color][/color]
value[color=blue]
> and[color=green][color=darkred]
> > > > do
> > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position[/color][/color]
> and[color=green][color=darkred]
> > > > clear
> > > > > the extra bits if there are any. That seems like a lot of work.
> > > > >
> > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or[/color][/color]
> 64-bit[color=green][color=darkred]
> > > > > value?
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Bob Powell [MVP]
Guest
 
Posts: n/a
#12: Nov 15 '05

re: bitwise rotation


Cool. I started on one for the Z80 a while back and I wanted to write a
Sinclair / Timex Spectrum emulator.

Some years ago I wrote a complete disassembler for the z80 for the Grundy
corporation (newbrain) before they went belly up so I got to know the
instruction set inside-out.

Great project. I'd like to keep informed of it if you don't mind.

Regards,

Bob.


--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm



"Shawn B." <leabre@html.com> wrote in message
news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=blue]
> Don't miss a thing, d'ya? =))
>
> I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
> 16-bit 65816. In particular, I'm doing this so I can debug (I have the
> ability to modify the assembly language while the debugger is running
> (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> instructions. I'm not designing it to be a particular piece of hardware
> (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so[/color]
my[color=blue]
> plugins can treat an area of memory as a dedicated "something" such as[/color]
text[color=blue]
> display or graphics display, etc. For now, I'm concerned not even with[/color]
the[color=blue]
> text display. My end result, is actually more akin to being able to debug
> my NES games I've been into writing lately. Also I want to be ready with[/color]
my[color=blue]
> own debugger for Andre LaMothe's upcoming XGameStation console (don't know
> whether he'll have a debugger or not) but I want a debugger that allows me
> to change the code while it's being debugged and my simulator allows for[/color]
it[color=blue]
> (as long as the source code file is available -- else if it was loading as[/color]
a[color=blue]
> .bin file then it won't be editable during a debug session).
>
> So it's really more than a simulator. It's an integrated (non-seperable)
> assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast[/color]
..[color=blue]
> I chose C# because it's not the kind of thing I'd want to do in VB.NET[/color]
(I'm[color=blue]
> primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
> but also wanted it to target the .NET framework (as much as is reasonable
> and I'll find out what that really means when its mature enough to start
> using reliably).
>
> I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
> GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating[/color]
the[color=blue]
> 16-bit processor as I haven't deciphered the specs for the CPU yet (the
> 8-bit CPU is very easy compared to the 16-bit version of the processor).[/color]
I[color=blue]
> ultimately will make this Interfacable so I can write a Z80 core, as well.
>
> These bit manipulations are the utmost inner-loop so I have to make them
> tight. Looking at the JIT'd code for these methods seems to be decent but[/color]
I[color=blue]
> have to make too many function calls (read, jumps) for my tastes but[/color]
doesn't[color=blue]
> seem to be hurting me at the moment). Since I can't inline in .NET, I'll
> have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> generate the best code for the rotates and shifts because I have to[/color]
emulate[color=blue]
> 8-bit and 16-bit shifts and rotates. I'm also trying to work out a better
> way for simulating memory manipulation. Now I'm creating a string of[/color]
(64kb[color=blue]
> in length) but manipulating each byte by substrings has got to be[/color]
expensive.[color=blue]
> I'm looking into whether a byte array would be better.
>
>
> Thanks for the help,
> if interested, I'll post my binary class here for all to see.
>
> I've completed the Rotates, Shift, converting numerics into binary (string
> of 1's and 0's). I know now have to read the binary back to a numeric,[/color]
and[color=blue]
> allow a way to get/set/toggle individual bits.
>
>
> Thanks,
> Shawn
>
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...[color=green]
> > So, you're writing an emulator for which processor??
> >
> > --
> > Bob Powell [MVP]
> > C#, System.Drawing
> >
> > September's edition of Well Formed is now available.
> > http://www.bobpowell.net/currentissue.htm
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdiplus_faq.htm
> >
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message
> > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > Greetings,
> > >
> > > I am simulating an assembly language bit rotation in C# and it works
> > > wonderfully
> > >
> > > ---------
> > > ...
> > > public uint Value;
> > > ...
> > > public uint RotateRight(byte count) {
> > > Value = ((Value >> count) | (Value << (32-count)));
> > > return Value;
> > > }
> > > ---------
> > >
> > > as long is the value is 32-bit or 64-bit (because of a limitation of[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > shift operator). I need it to work on an 8-bit or 16-bit value, as[/color][/color]
> well.[color=green]
> > I[color=darkred]
> > > presume I'd have to have knowledge of the intended size of the value[/color][/color][/color]
and[color=blue][color=green]
> > do[color=darkred]
> > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and[/color]
> > clear[color=darkred]
> > > the extra bits if there are any. That seems like a lot of work.
> > >
> > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or[/color][/color][/color]
64-bit[color=blue][color=green][color=darkred]
> > > value?
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#13: Nov 15 '05

re: bitwise rotation


Yeah, I learned that (you can overload it in MC++)... instead I used the
"implicit" gig.

I am embarking now to overload all the operators for this struct and
implementing IConvertable as well as some other Interfaces and things. I
want to make it as complete as I possibly can since I intend to use it very
intensely.


Thanks,
Shawn


"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:OD8KvSwjDHA.1284@TK2MSFTNGP09.phx.gbl...[color=blue]
> In addition to Jon's response,
> No, you can't overload the = operator.
> Found a nice summary of what can be overloaded:
> http://www.csharphelp.com/archives/archive135.html
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:ez5W57ujDHA.1004@tk2msftngp13.phx.gbl...[color=blue][color=green]
> > Robert,
> >
> > Youre suggestions have transformed my operations into 1-liners from 5
> > liners... you'll be the first to be notified (on these groups, of[/color][/color]
course).[color=blue][color=green]
> >
> > I've opted to create a series of classes, Binary8, Binary16, Binary32,
> > Binary64 (all are unsigned).
> >
> > I need to know, when you say "int x = 10;" is that a compiler trick or[/color][/color]
is[color=blue][color=green]
> > there a way to create my own type that can do that, for example,[/color][/color]
"Binary8 x[color=blue][color=green]
> > = 0xFF;"???
> >
> >
> > Thanks,
> > Shawn
> >
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > Do post it here!
> > > I'd be interested to see, as I'm sure many others would.
> > > You'll probably get a ton of optimization hints from it. :)
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Don't miss a thing, d'ya? =))
> > > >
> > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color][/color]
and[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > ability to modify the assembly language while the debugger is[/color][/color][/color]
running[color=blue][color=green][color=darkred]
> > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > instructions. I'm not designing it to be a particular piece of[/color][/color][/color]
hardware[color=blue][color=green][color=darkred]
> > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color][/color]
plugins so[color=blue][color=green]
> > my[color=darkred]
> > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color][/color]
as[color=blue][color=green]
> > text[color=darkred]
> > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color][/color]
with[color=blue][color=green]
> > the[color=darkred]
> > > > text display. My end result, is actually more akin to being able to[/color]
> > debug[color=darkred]
> > > > my NES games I've been into writing lately. Also I want to be ready[/color]
> > with my[color=darkred]
> > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color][/color]
(don't[color=blue][color=green]
> > know[color=darkred]
> > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color][/color]
allows[color=blue][color=green]
> > me[color=darkred]
> > > > to change the code while it's being debugged and my simulator allows[/color][/color][/color]
for[color=blue][color=green]
> > it[color=darkred]
> > > > (as long as the source code file is available -- else if it was[/color][/color][/color]
loading[color=blue][color=green]
> > as a[color=darkred]
> > > > .bin file then it won't be editable during a debug session).
> > > >
> > > > So it's really more than a simulator. It's an integrated[/color]
> > (non-seperable)[color=darkred]
> > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color]
> > beast.[color=darkred]
> > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color][/color]
VB.NET[color=blue][color=green]
> > (I'm[color=darkred]
> > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color][/color]
Managed[color=blue][color=green]
> > C++[color=darkred]
> > > > but also wanted it to target the .NET framework (as much as is[/color]
> > reasonable[color=darkred]
> > > > and I'll find out what that really means when its mature enough to[/color][/color][/color]
start[color=blue][color=green][color=darkred]
> > > > using reliably).
> > > >
> > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color][/color]
with[color=blue][color=green][color=darkred]
> > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color][/color]
emulating[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color][/color]
(the[color=blue][color=green][color=darkred]
> > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color][/color]
processor).[color=blue][color=green]
> > I[color=darkred]
> > > > ultimately will make this Interfacable so I can write a Z80 core, as[/color]
> > well.[color=darkred]
> > > >
> > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color][/color]
them[color=blue][color=green][color=darkred]
> > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color][/color]
decent[color=blue][color=green]
> > but I[color=darkred]
> > > > have to make too many function calls (read, jumps) for my tastes but[/color]
> > doesn't[color=darkred]
> > > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color]
> > I'll[color=darkred]
> > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > generate the best code for the rotates and shifts because I have to[/color]
> > emulate[color=darkred]
> > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color]
> > better[color=darkred]
> > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color][/color]
of[color=blue][color=green]
> > (64kb[color=darkred]
> > > > in length) but manipulating each byte by substrings has got to be[/color]
> > expensive.[color=darkred]
> > > > I'm looking into whether a byte array would be better.
> > > >
> > > >
> > > > Thanks for the help,
> > > > if interested, I'll post my binary class here for all to see.
> > > >
> > > > I've completed the Rotates, Shift, converting numerics into binary[/color]
> > (string[color=darkred]
> > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color][/color]
numeric,[color=blue][color=green]
> > and[color=darkred]
> > > > allow a way to get/set/toggle individual bits.
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > So, you're writing an emulator for which processor??
> > > > >
> > > > > --
> > > > > Bob Powell [MVP]
> > > > > C#, System.Drawing
> > > > >
> > > > > September's edition of Well Formed is now available.
> > > > > http://www.bobpowell.net/currentissue.htm
> > > > >
> > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > >
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > Greetings,
> > > > > >
> > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color][/color]
works[color=blue][color=green][color=darkred]
> > > > > > wonderfully
> > > > > >
> > > > > > ---------
> > > > > > ...
> > > > > > public uint Value;
> > > > > > ...
> > > > > > public uint RotateRight(byte count) {
> > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > return Value;
> > > > > > }
> > > > > > ---------
> > > > > >
> > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color][/color]
limitation of[color=blue][color=green]
> > the[color=darkred]
> > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color][/color]
as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > I
> > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color][/color]
value[color=blue][color=green]
> > and[color=darkred]
> > > > > do
> > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color][/color]
position[color=blue][color=green]
> > and[color=darkred]
> > > > > clear
> > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > >
> > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color][/color]
or[color=blue][color=green]
> > 64-bit[color=darkred]
> > > > > > value?
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#14: Nov 15 '05

re: bitwise rotation


Yes, I had already (in the 3rd major revision of this thing) changed it to
use structs. Each of the Binary8, Binary16, Binary32, and Binary64 are
Byte, ushort, uint, and ulong respectively. If I don't use unsigned then
the rotates crash if rotating a set high or low bit.

Yes, they are just instrinsic valuetypes with added functionality, for that
matter.


Thanks,
Shawn



"Jason Smith" <jason@nospam.com> wrote in message
news:ertSBUwjDHA.3612@TK2MSFTNGP11.phx.gbl...[color=blue]
> Instead of creating classes, use structs. Or, if you want the best
> performance, just use the unsigned integers supported by .NET and C#.
> Manipulate the integers using static methods.
>
> Your BinaryX classes are really just unsigned integers with some added
> functionality.
>
> "Shawn B." <leabre@html.com> wrote in message
> news:ez5W57ujDHA.1004@tk2msftngp13.phx.gbl...[color=green]
> > Robert,
> >
> > Youre suggestions have transformed my operations into 1-liners from 5
> > liners... you'll be the first to be notified (on these groups, of[/color][/color]
course).[color=blue][color=green]
> >
> > I've opted to create a series of classes, Binary8, Binary16, Binary32,
> > Binary64 (all are unsigned).
> >
> > I need to know, when you say "int x = 10;" is that a compiler trick or[/color][/color]
is[color=blue][color=green]
> > there a way to create my own type that can do that, for example,[/color][/color]
"Binary8[color=blue]
> x[color=green]
> > = 0xFF;"???
> >
> >
> > Thanks,
> > Shawn
> >
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > Do post it here!
> > > I'd be interested to see, as I'm sure many others would.
> > > You'll probably get a ton of optimization hints from it. :)
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Don't miss a thing, d'ya? =))
> > > >
> > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color][/color]
and[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color]
> the[color=green][color=darkred]
> > > > ability to modify the assembly language while the debugger is[/color][/color][/color]
running[color=blue][color=green][color=darkred]
> > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > instructions. I'm not designing it to be a particular piece of[/color][/color]
> hardware[color=green][color=darkred]
> > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color][/color]
plugins[color=blue]
> so[color=green]
> > my[color=darkred]
> > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color][/color]
as[color=blue][color=green]
> > text[color=darkred]
> > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color]
> with[color=green]
> > the[color=darkred]
> > > > text display. My end result, is actually more akin to being able to[/color]
> > debug[color=darkred]
> > > > my NES games I've been into writing lately. Also I want to be ready[/color]
> > with my[color=darkred]
> > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color][/color]
(don't[color=blue][color=green]
> > know[color=darkred]
> > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color]
> allows[color=green]
> > me[color=darkred]
> > > > to change the code while it's being debugged and my simulator allows[/color][/color]
> for[color=green]
> > it[color=darkred]
> > > > (as long as the source code file is available -- else if it was[/color][/color]
> loading[color=green]
> > as a[color=darkred]
> > > > .bin file then it won't be editable during a debug session).
> > > >
> > > > So it's really more than a simulator. It's an integrated[/color]
> > (non-seperable)[color=darkred]
> > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color]
> > beast.[color=darkred]
> > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color][/color]
VB.NET[color=blue][color=green]
> > (I'm[color=darkred]
> > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color][/color]
Managed[color=blue][color=green]
> > C++[color=darkred]
> > > > but also wanted it to target the .NET framework (as much as is[/color]
> > reasonable[color=darkred]
> > > > and I'll find out what that really means when its mature enough to[/color][/color]
> start[color=green][color=darkred]
> > > > using reliably).
> > > >
> > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color]
> with[color=green][color=darkred]
> > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color]
> emulating[color=green]
> > the[color=darkred]
> > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color]
> (the[color=green][color=darkred]
> > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color]
> processor).[color=green]
> > I[color=darkred]
> > > > ultimately will make this Interfacable so I can write a Z80 core, as[/color]
> > well.[color=darkred]
> > > >
> > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color]
> them[color=green][color=darkred]
> > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color][/color]
decent[color=blue][color=green]
> > but I[color=darkred]
> > > > have to make too many function calls (read, jumps) for my tastes but[/color]
> > doesn't[color=darkred]
> > > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color]
> > I'll[color=darkred]
> > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > generate the best code for the rotates and shifts because I have to[/color]
> > emulate[color=darkred]
> > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color]
> > better[color=darkred]
> > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color][/color]
of[color=blue][color=green]
> > (64kb[color=darkred]
> > > > in length) but manipulating each byte by substrings has got to be[/color]
> > expensive.[color=darkred]
> > > > I'm looking into whether a byte array would be better.
> > > >
> > > >
> > > > Thanks for the help,
> > > > if interested, I'll post my binary class here for all to see.
> > > >
> > > > I've completed the Rotates, Shift, converting numerics into binary[/color]
> > (string[color=darkred]
> > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color]
> numeric,[color=green]
> > and[color=darkred]
> > > > allow a way to get/set/toggle individual bits.
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > So, you're writing an emulator for which processor??
> > > > >
> > > > > --
> > > > > Bob Powell [MVP]
> > > > > C#, System.Drawing
> > > > >
> > > > > September's edition of Well Formed is now available.
> > > > > http://www.bobpowell.net/currentissue.htm
> > > > >
> > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > >
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > Greetings,
> > > > > >
> > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color]
> works[color=green][color=darkred]
> > > > > > wonderfully
> > > > > >
> > > > > > ---------
> > > > > > ...
> > > > > > public uint Value;
> > > > > > ...
> > > > > > public uint RotateRight(byte count) {
> > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > return Value;
> > > > > > }
> > > > > > ---------
> > > > > >
> > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color][/color]
limitation[color=blue]
> of[color=green]
> > the[color=darkred]
> > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color]
> as[color=green][color=darkred]
> > > > well.
> > > > > I
> > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color]
> value[color=green]
> > and[color=darkred]
> > > > > do
> > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color][/color]
position[color=blue][color=green]
> > and[color=darkred]
> > > > > clear
> > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > >
> > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color][/color]
or[color=blue][color=green]
> > 64-bit[color=darkred]
> > > > > > value?
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#15: Nov 15 '05

re: bitwise rotation


BTW:

I am at the point now where I can say:

Binary8 bin = "10101110";
Binary8 bin2 = bin - 0x03;
Binary16 bin3 = bin << 4;
....
Binary32 bin4 = bin3.RotateLeft(7);
Bin4 >>= 1;

bin4.Bit(7) = False; // I'm Thinking about being able to say Bit(x) = ON;
Bit(x) = OFF; Not sure yet, perhaps even
// Bit(x) = 1; Bit(x) = 0;

MessageBox.Show(bin4.ToString());

The ToString() method returns a string of 1's and 0's representing the
binary form of the value.
Binary8.ToString() returns similar "00011111";
Binary8.ToString(False) returns similar "11111"; // no trailing padding of
0's.

I still have to work out the "&" and "|" operators to the the same effect as
"bin.And(0x8A);" or something like that.

Still interested?


Thanks,
Shawn


"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:OD8KvSwjDHA.1284@TK2MSFTNGP09.phx.gbl...[color=blue]
> In addition to Jon's response,
> No, you can't overload the = operator.
> Found a nice summary of what can be overloaded:
> http://www.csharphelp.com/archives/archive135.html
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:ez5W57ujDHA.1004@tk2msftngp13.phx.gbl...[color=blue][color=green]
> > Robert,
> >
> > Youre suggestions have transformed my operations into 1-liners from 5
> > liners... you'll be the first to be notified (on these groups, of[/color][/color]
course).[color=blue][color=green]
> >
> > I've opted to create a series of classes, Binary8, Binary16, Binary32,
> > Binary64 (all are unsigned).
> >
> > I need to know, when you say "int x = 10;" is that a compiler trick or[/color][/color]
is[color=blue][color=green]
> > there a way to create my own type that can do that, for example,[/color][/color]
"Binary8 x[color=blue][color=green]
> > = 0xFF;"???
> >
> >
> > Thanks,
> > Shawn
> >
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > Do post it here!
> > > I'd be interested to see, as I'm sure many others would.
> > > You'll probably get a ton of optimization hints from it. :)
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Don't miss a thing, d'ya? =))
> > > >
> > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color][/color]
and[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > ability to modify the assembly language while the debugger is[/color][/color][/color]
running[color=blue][color=green][color=darkred]
> > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > instructions. I'm not designing it to be a particular piece of[/color][/color][/color]
hardware[color=blue][color=green][color=darkred]
> > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color][/color]
plugins so[color=blue][color=green]
> > my[color=darkred]
> > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color][/color]
as[color=blue][color=green]
> > text[color=darkred]
> > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color][/color]
with[color=blue][color=green]
> > the[color=darkred]
> > > > text display. My end result, is actually more akin to being able to[/color]
> > debug[color=darkred]
> > > > my NES games I've been into writing lately. Also I want to be ready[/color]
> > with my[color=darkred]
> > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color][/color]
(don't[color=blue][color=green]
> > know[color=darkred]
> > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color][/color]
allows[color=blue][color=green]
> > me[color=darkred]
> > > > to change the code while it's being debugged and my simulator allows[/color][/color][/color]
for[color=blue][color=green]
> > it[color=darkred]
> > > > (as long as the source code file is available -- else if it was[/color][/color][/color]
loading[color=blue][color=green]
> > as a[color=darkred]
> > > > .bin file then it won't be editable during a debug session).
> > > >
> > > > So it's really more than a simulator. It's an integrated[/color]
> > (non-seperable)[color=darkred]
> > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color]
> > beast.[color=darkred]
> > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color][/color]
VB.NET[color=blue][color=green]
> > (I'm[color=darkred]
> > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color][/color]
Managed[color=blue][color=green]
> > C++[color=darkred]
> > > > but also wanted it to target the .NET framework (as much as is[/color]
> > reasonable[color=darkred]
> > > > and I'll find out what that really means when its mature enough to[/color][/color][/color]
start[color=blue][color=green][color=darkred]
> > > > using reliably).
> > > >
> > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color][/color]
with[color=blue][color=green][color=darkred]
> > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color][/color]
emulating[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color][/color]
(the[color=blue][color=green][color=darkred]
> > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color][/color]
processor).[color=blue][color=green]
> > I[color=darkred]
> > > > ultimately will make this Interfacable so I can write a Z80 core, as[/color]
> > well.[color=darkred]
> > > >
> > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color][/color]
them[color=blue][color=green][color=darkred]
> > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color][/color]
decent[color=blue][color=green]
> > but I[color=darkred]
> > > > have to make too many function calls (read, jumps) for my tastes but[/color]
> > doesn't[color=darkred]
> > > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color]
> > I'll[color=darkred]
> > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > generate the best code for the rotates and shifts because I have to[/color]
> > emulate[color=darkred]
> > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color]
> > better[color=darkred]
> > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color][/color]
of[color=blue][color=green]
> > (64kb[color=darkred]
> > > > in length) but manipulating each byte by substrings has got to be[/color]
> > expensive.[color=darkred]
> > > > I'm looking into whether a byte array would be better.
> > > >
> > > >
> > > > Thanks for the help,
> > > > if interested, I'll post my binary class here for all to see.
> > > >
> > > > I've completed the Rotates, Shift, converting numerics into binary[/color]
> > (string[color=darkred]
> > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color][/color]
numeric,[color=blue][color=green]
> > and[color=darkred]
> > > > allow a way to get/set/toggle individual bits.
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > So, you're writing an emulator for which processor??
> > > > >
> > > > > --
> > > > > Bob Powell [MVP]
> > > > > C#, System.Drawing
> > > > >
> > > > > September's edition of Well Formed is now available.
> > > > > http://www.bobpowell.net/currentissue.htm
> > > > >
> > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > >
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > Greetings,
> > > > > >
> > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color][/color]
works[color=blue][color=green][color=darkred]
> > > > > > wonderfully
> > > > > >
> > > > > > ---------
> > > > > > ...
> > > > > > public uint Value;
> > > > > > ...
> > > > > > public uint RotateRight(byte count) {
> > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > return Value;
> > > > > > }
> > > > > > ---------
> > > > > >
> > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color][/color]
limitation of[color=blue][color=green]
> > the[color=darkred]
> > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color][/color]
as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > I
> > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color][/color]
value[color=blue][color=green]
> > and[color=darkred]
> > > > > do
> > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color][/color]
position[color=blue][color=green]
> > and[color=darkred]
> > > > > clear
> > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > >
> > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color][/color]
or[color=blue][color=green]
> > 64-bit[color=darkred]
> > > > > > value?
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#16: Nov 15 '05

re: bitwise rotation


Well, I've posted it twice and no one is jumpting to optimize it... perhaps
it's just so well working that there's no need? he he




Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=blue]
> Do post it here!
> I'd be interested to see, as I'm sure many others would.
> You'll probably get a ton of optimization hints from it. :)
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=blue][color=green]
> > Don't miss a thing, d'ya? =))
> >
> > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and[/color][/color]
the[color=blue][color=green]
> > 16-bit 65816. In particular, I'm doing this so I can debug (I have the
> > ability to modify the assembly language while the debugger is running
> > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > instructions. I'm not designing it to be a particular piece of hardware
> > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so[/color][/color]
my[color=blue][color=green]
> > plugins can treat an area of memory as a dedicated "something" such as[/color][/color]
text[color=blue][color=green]
> > display or graphics display, etc. For now, I'm concerned not even with[/color][/color]
the[color=blue][color=green]
> > text display. My end result, is actually more akin to being able to[/color][/color]
debug[color=blue][color=green]
> > my NES games I've been into writing lately. Also I want to be ready[/color][/color]
with my[color=blue][color=green]
> > own debugger for Andre LaMothe's upcoming XGameStation console (don't[/color][/color]
know[color=blue][color=green]
> > whether he'll have a debugger or not) but I want a debugger that allows[/color][/color]
me[color=blue][color=green]
> > to change the code while it's being debugged and my simulator allows for[/color][/color]
it[color=blue][color=green]
> > (as long as the source code file is available -- else if it was loading[/color][/color]
as a[color=blue][color=green]
> > .bin file then it won't be editable during a debug session).
> >
> > So it's really more than a simulator. It's an integrated[/color][/color]
(non-seperable)[color=blue][color=green]
> > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color][/color]
beast.[color=blue][color=green]
> > I chose C# because it's not the kind of thing I'd want to do in VB.NET[/color][/color]
(I'm[color=blue][color=green]
> > primarly a VB.NET programmer) but didn't feel like doing it in Managed[/color][/color]
C++[color=blue][color=green]
> > but also wanted it to target the .NET framework (as much as is[/color][/color]
reasonable[color=blue][color=green]
> > and I'll find out what that really means when its mature enough to start
> > using reliably).
> >
> > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
> > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating[/color][/color]
the[color=blue][color=green]
> > 16-bit processor as I haven't deciphered the specs for the CPU yet (the
> > 8-bit CPU is very easy compared to the 16-bit version of the processor).[/color][/color]
I[color=blue][color=green]
> > ultimately will make this Interfacable so I can write a Z80 core, as[/color][/color]
well.[color=blue][color=green]
> >
> > These bit manipulations are the utmost inner-loop so I have to make them
> > tight. Looking at the JIT'd code for these methods seems to be decent[/color][/color]
but I[color=blue][color=green]
> > have to make too many function calls (read, jumps) for my tastes but[/color][/color]
doesn't[color=blue][color=green]
> > seem to be hurting me at the moment). Since I can't inline in .NET,[/color][/color]
I'll[color=blue][color=green]
> > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > generate the best code for the rotates and shifts because I have to[/color][/color]
emulate[color=blue][color=green]
> > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color][/color]
better[color=blue][color=green]
> > way for simulating memory manipulation. Now I'm creating a string of[/color][/color]
(64kb[color=blue][color=green]
> > in length) but manipulating each byte by substrings has got to be[/color][/color]
expensive.[color=blue][color=green]
> > I'm looking into whether a byte array would be better.
> >
> >
> > Thanks for the help,
> > if interested, I'll post my binary class here for all to see.
> >
> > I've completed the Rotates, Shift, converting numerics into binary[/color][/color]
(string[color=blue][color=green]
> > of 1's and 0's). I know now have to read the binary back to a numeric,[/color][/color]
and[color=blue][color=green]
> > allow a way to get/set/toggle individual bits.
> >
> >
> > Thanks,
> > Shawn
> >
> >
> > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...[color=darkred]
> > > So, you're writing an emulator for which processor??
> > >
> > > --
> > > Bob Powell [MVP]
> > > C#, System.Drawing
> > >
> > > September's edition of Well Formed is now available.
> > > http://www.bobpowell.net/currentissue.htm
> > >
> > > Answer those GDI+ questions with the GDI+ FAQ
> > > http://www.bobpowell.net/gdiplus_faq.htm
> > >
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message
> > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > Greetings,
> > > >
> > > > I am simulating an assembly language bit rotation in C# and it works
> > > > wonderfully
> > > >
> > > > ---------
> > > > ...
> > > > public uint Value;
> > > > ...
> > > > public uint RotateRight(byte count) {
> > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > return Value;
> > > > }
> > > > ---------
> > > >
> > > > as long is the value is 32-bit or 64-bit (because of a limitation of[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > shift operator). I need it to work on an 8-bit or 16-bit value, as[/color]
> > well.[color=darkred]
> > > I
> > > > presume I'd have to have knowledge of the intended size of the value[/color][/color][/color]
and[color=blue][color=green][color=darkred]
> > > do
> > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position[/color][/color][/color]
and[color=blue][color=green][color=darkred]
> > > clear
> > > > the extra bits if there are any. That seems like a lot of work.
> > > >
> > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or[/color][/color][/color]
64-bit[color=blue][color=green][color=darkred]
> > > > value?
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#17: Nov 15 '05

re: bitwise rotation


Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=blue]
> Well, I've posted it twice and no one is jumpting to optimize it... perhaps
> it's just so well working that there's no need? he he
>
>
>
>
> Thanks,
> Shawn
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=green]
> > Do post it here!
> > I'd be interested to see, as I'm sure many others would.
> > You'll probably get a ton of optimization hints from it. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=green][color=darkred]
> > > Don't miss a thing, d'ya? =))
> > >
> > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and[/color][/color]
> the[color=green][color=darkred]
> > > 16-bit 65816. In particular, I'm doing this so I can debug (I have the
> > > ability to modify the assembly language while the debugger is running
> > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > instructions. I'm not designing it to be a particular piece of hardware
> > > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so[/color][/color]
> my[color=green][color=darkred]
> > > plugins can treat an area of memory as a dedicated "something" such as[/color][/color]
> text[color=green][color=darkred]
> > > display or graphics display, etc. For now, I'm concerned not even with[/color][/color]
> the[color=green][color=darkred]
> > > text display. My end result, is actually more akin to being able to[/color][/color]
> debug[color=green][color=darkred]
> > > my NES games I've been into writing lately. Also I want to be ready[/color][/color]
> with my[color=green][color=darkred]
> > > own debugger for Andre LaMothe's upcoming XGameStation console (don't[/color][/color]
> know[color=green][color=darkred]
> > > whether he'll have a debugger or not) but I want a debugger that allows[/color][/color]
> me[color=green][color=darkred]
> > > to change the code while it's being debugged and my simulator allows for[/color][/color]
> it[color=green][color=darkred]
> > > (as long as the source code file is available -- else if it was loading[/color][/color]
> as a[color=green][color=darkred]
> > > .bin file then it won't be editable during a debug session).
> > >
> > > So it's really more than a simulator. It's an integrated[/color][/color]
> (non-seperable)[color=green][color=darkred]
> > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color][/color]
> beast.[color=green][color=darkred]
> > > I chose C# because it's not the kind of thing I'd want to do in VB.NET[/color][/color]
> (I'm[color=green][color=darkred]
> > > primarly a VB.NET programmer) but didn't feel like doing it in Managed[/color][/color]
> C++[color=green][color=darkred]
> > > but also wanted it to target the .NET framework (as much as is[/color][/color]
> reasonable[color=green][color=darkred]
> > > and I'll find out what that really means when its mature enough to start
> > > using reliably).
> > >
> > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
> > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating[/color][/color]
> the[color=green][color=darkred]
> > > 16-bit processor as I haven't deciphered the specs for the CPU yet (the
> > > 8-bit CPU is very easy compared to the 16-bit version of the processor).[/color][/color]
> I[color=green][color=darkred]
> > > ultimately will make this Interfacable so I can write a Z80 core, as[/color][/color]
> well.[color=green][color=darkred]
> > >
> > > These bit manipulations are the utmost inner-loop so I have to make them
> > > tight. Looking at the JIT'd code for these methods seems to be decent[/color][/color]
> but I[color=green][color=darkred]
> > > have to make too many function calls (read, jumps) for my tastes but[/color][/color]
> doesn't[color=green][color=darkred]
> > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color][/color]
> I'll[color=green][color=darkred]
> > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > generate the best code for the rotates and shifts because I have to[/color][/color]
> emulate[color=green][color=darkred]
> > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color][/color]
> better[color=green][color=darkred]
> > > way for simulating memory manipulation. Now I'm creating a string of[/color][/color]
> (64kb[color=green][color=darkred]
> > > in length) but manipulating each byte by substrings has got to be[/color][/color]
> expensive.[color=green][color=darkred]
> > > I'm looking into whether a byte array would be better.
> > >
> > >
> > > Thanks for the help,
> > > if interested, I'll post my binary class here for all to see.
> > >
> > > I've completed the Rotates, Shift, converting numerics into binary[/color][/color]
> (string[color=green][color=darkred]
> > > of 1's and 0's). I know now have to read the binary back to a numeric,[/color][/color]
> and[color=green][color=darkred]
> > > allow a way to get/set/toggle individual bits.
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > So, you're writing an emulator for which processor??
> > > >
> > > > --
> > > > Bob Powell [MVP]
> > > > C#, System.Drawing
> > > >
> > > > September's edition of Well Formed is now available.
> > > > http://www.bobpowell.net/currentissue.htm
> > > >
> > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > >
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > Greetings,
> > > > >
> > > > > I am simulating an assembly language bit rotation in C# and it works
> > > > > wonderfully
> > > > >
> > > > > ---------
> > > > > ...
> > > > > public uint Value;
> > > > > ...
> > > > > public uint RotateRight(byte count) {
> > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > return Value;
> > > > > }
> > > > > ---------
> > > > >
> > > > > as long is the value is 32-bit or 64-bit (because of a limitation of[/color][/color]
> the[color=green][color=darkred]
> > > > > shift operator). I need it to work on an 8-bit or 16-bit value, as
> > > well.
> > > > I
> > > > > presume I'd have to have knowledge of the intended size of the value[/color][/color]
> and[color=green][color=darkred]
> > > > do
> > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position[/color][/color]
> and[color=green][color=darkred]
> > > > clear
> > > > > the extra bits if there are any. That seems like a lot of work.
> > > > >
> > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or[/color][/color]
> 64-bit[color=green][color=darkred]
> > > > > value?
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#18: Nov 15 '05

re: bitwise rotation


Oh, nice classes by the way. They seem pretty fast.
:)

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=blue]
> Well, I've posted it twice and no one is jumpting to optimize it... perhaps
> it's just so well working that there's no need? he he
>
>
>
>
> Thanks,
> Shawn
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=green]
> > Do post it here!
> > I'd be interested to see, as I'm sure many others would.
> > You'll probably get a ton of optimization hints from it. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=green][color=darkred]
> > > Don't miss a thing, d'ya? =))
> > >
> > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and[/color][/color]
> the[color=green][color=darkred]
> > > 16-bit 65816. In particular, I'm doing this so I can debug (I have the
> > > ability to modify the assembly language while the debugger is running
> > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > instructions. I'm not designing it to be a particular piece of hardware
> > > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so[/color][/color]
> my[color=green][color=darkred]
> > > plugins can treat an area of memory as a dedicated "something" such as[/color][/color]
> text[color=green][color=darkred]
> > > display or graphics display, etc. For now, I'm concerned not even with[/color][/color]
> the[color=green][color=darkred]
> > > text display. My end result, is actually more akin to being able to[/color][/color]
> debug[color=green][color=darkred]
> > > my NES games I've been into writing lately. Also I want to be ready[/color][/color]
> with my[color=green][color=darkred]
> > > own debugger for Andre LaMothe's upcoming XGameStation console (don't[/color][/color]
> know[color=green][color=darkred]
> > > whether he'll have a debugger or not) but I want a debugger that allows[/color][/color]
> me[color=green][color=darkred]
> > > to change the code while it's being debugged and my simulator allows for[/color][/color]
> it[color=green][color=darkred]
> > > (as long as the source code file is available -- else if it was loading[/color][/color]
> as a[color=green][color=darkred]
> > > .bin file then it won't be editable during a debug session).
> > >
> > > So it's really more than a simulator. It's an integrated[/color][/color]
> (non-seperable)[color=green][color=darkred]
> > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color][/color]
> beast.[color=green][color=darkred]
> > > I chose C# because it's not the kind of thing I'd want to do in VB.NET[/color][/color]
> (I'm[color=green][color=darkred]
> > > primarly a VB.NET programmer) but didn't feel like doing it in Managed[/color][/color]
> C++[color=green][color=darkred]
> > > but also wanted it to target the .NET framework (as much as is[/color][/color]
> reasonable[color=green][color=darkred]
> > > and I'll find out what that really means when its mature enough to start
> > > using reliably).
> > >
> > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
> > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating[/color][/color]
> the[color=green][color=darkred]
> > > 16-bit processor as I haven't deciphered the specs for the CPU yet (the
> > > 8-bit CPU is very easy compared to the 16-bit version of the processor).[/color][/color]
> I[color=green][color=darkred]
> > > ultimately will make this Interfacable so I can write a Z80 core, as[/color][/color]
> well.[color=green][color=darkred]
> > >
> > > These bit manipulations are the utmost inner-loop so I have to make them
> > > tight. Looking at the JIT'd code for these methods seems to be decent[/color][/color]
> but I[color=green][color=darkred]
> > > have to make too many function calls (read, jumps) for my tastes but[/color][/color]
> doesn't[color=green][color=darkred]
> > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color][/color]
> I'll[color=green][color=darkred]
> > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > generate the best code for the rotates and shifts because I have to[/color][/color]
> emulate[color=green][color=darkred]
> > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color][/color]
> better[color=green][color=darkred]
> > > way for simulating memory manipulation. Now I'm creating a string of[/color][/color]
> (64kb[color=green][color=darkred]
> > > in length) but manipulating each byte by substrings has got to be[/color][/color]
> expensive.[color=green][color=darkred]
> > > I'm looking into whether a byte array would be better.
> > >
> > >
> > > Thanks for the help,
> > > if interested, I'll post my binary class here for all to see.
> > >
> > > I've completed the Rotates, Shift, converting numerics into binary[/color][/color]
> (string[color=green][color=darkred]
> > > of 1's and 0's). I know now have to read the binary back to a numeric,[/color][/color]
> and[color=green][color=darkred]
> > > allow a way to get/set/toggle individual bits.
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > So, you're writing an emulator for which processor??
> > > >
> > > > --
> > > > Bob Powell [MVP]
> > > > C#, System.Drawing
> > > >
> > > > September's edition of Well Formed is now available.
> > > > http://www.bobpowell.net/currentissue.htm
> > > >
> > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > >
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > Greetings,
> > > > >
> > > > > I am simulating an assembly language bit rotation in C# and it works
> > > > > wonderfully
> > > > >
> > > > > ---------
> > > > > ...
> > > > > public uint Value;
> > > > > ...
> > > > > public uint RotateRight(byte count) {
> > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > return Value;
> > > > > }
> > > > > ---------
> > > > >
> > > > > as long is the value is 32-bit or 64-bit (because of a limitation of[/color][/color]
> the[color=green][color=darkred]
> > > > > shift operator). I need it to work on an 8-bit or 16-bit value, as
> > > well.
> > > > I
> > > > > presume I'd have to have knowledge of the intended size of the value[/color][/color]
> and[color=green][color=darkred]
> > > > do
> > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position[/color][/color]
> and[color=green][color=darkred]
> > > > clear
> > > > > the extra bits if there are any. That seems like a lot of work.
> > > > >
> > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or[/color][/color]
> 64-bit[color=green][color=darkred]
> > > > > value?
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#19: Nov 15 '05

re: bitwise rotation


Robert,

You're first suggestion crossed my mind, I have already started eliminating
the temp variables thus far (thus far only in the 8-bit operator overloads
region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...

I also plan to move the Shift function logic into the overload to avoid the
extra function call.

The GetBit optimization hint you provided is what I've been wanting to do
but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like
it's necessary. Perhaps I could just make the if based on some calculation
and then just throw an exception at the end of the function and avoid that
early check, perhaps that would be a better approach. Such as (if (Value ==
0 && Value < SIZE-1) {)

There is one thing I need help on.


I don't like the GetBit, SetBit approach. I would rather be able to say x =
BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x =
x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a
reference of the Binary class into it so it would be able to change the bit
if a bit was set. Not a good approach. Do you have any ideas?


Thanks for your help,
Shawn


"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=blue]
> Hi Shawn,
>
> I've looked at it, and no optimization suggestions other than
> some obvious simple ones:
>
> public int GetBit(byte index) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> uint val = (uint)Value;
> byte shift = (byte)(index - 1);
> if (index == 0)
> return (int)(val & 0x01);
> else
> return (int)(val >> shift) & 0x01;
> }
>
> This is very verbose, and you're creating lots of temp variables
> I'd rather write something like (untested):
>
> public int GetBit(byte index) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> return (int)((Value >> index-1) & 0x01;
> }
>
> The check to avoid a shift probably just hurts performance anyway.
> Also for SetBit, the two if else must result in unwanted branches:
>
> public void SetBit(byte index, int value) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> byte shift = index;
> if (index == 0) {
> if (value == 0)
> Value &= unchecked((ushort)~0x01);
> else
> Value |= 0x01;
> }
> else {
> if (value == 0)
> Value &= ((ushort)~(0x01 << shift));
> else
> Value |= (ushort)(0x01 << shift);
> }
> }
>
> How about this instead (also untested):
>
> public void SetBit(byte index, int value) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> if (value == 0)
> Value &= ((ushort)~(0x01 << index));
> else
> Value |= (ushort)(0x01 << index);
> }
>
> This is the only type of optimizations I came up with, so I thought it[/color]
wasn't worth posting. :)[color=blue]
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=blue][color=green]
> > Well, I've posted it twice and no one is jumpting to optimize it...[/color][/color]
perhaps[color=blue][color=green]
> > it's just so well working that there's no need? he he
> >
> >
> >
> >
> > Thanks,
> > Shawn
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > Do post it here!
> > > I'd be interested to see, as I'm sure many others would.
> > > You'll probably get a ton of optimization hints from it. :)
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Don't miss a thing, d'ya? =))
> > > >
> > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color][/color]
and[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > ability to modify the assembly language while the debugger is[/color][/color][/color]
running[color=blue][color=green][color=darkred]
> > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > instructions. I'm not designing it to be a particular piece of[/color][/color][/color]
hardware[color=blue][color=green][color=darkred]
> > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color][/color]
plugins so[color=blue][color=green]
> > my[color=darkred]
> > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color][/color]
as[color=blue][color=green]
> > text[color=darkred]
> > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color][/color]
with[color=blue][color=green]
> > the[color=darkred]
> > > > text display. My end result, is actually more akin to being able to[/color]
> > debug[color=darkred]
> > > > my NES games I've been into writing lately. Also I want to be ready[/color]
> > with my[color=darkred]
> > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color][/color]
(don't[color=blue][color=green]
> > know[color=darkred]
> > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color][/color]
allows[color=blue][color=green]
> > me[color=darkred]
> > > > to change the code while it's being debugged and my simulator allows[/color][/color][/color]
for[color=blue][color=green]
> > it[color=darkred]
> > > > (as long as the source code file is available -- else if it was[/color][/color][/color]
loading[color=blue][color=green]
> > as a[color=darkred]
> > > > .bin file then it won't be editable during a debug session).
> > > >
> > > > So it's really more than a simulator. It's an integrated[/color]
> > (non-seperable)[color=darkred]
> > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color]
> > beast.[color=darkred]
> > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color][/color]
VB.NET[color=blue][color=green]
> > (I'm[color=darkred]
> > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color][/color]
Managed[color=blue][color=green]
> > C++[color=darkred]
> > > > but also wanted it to target the .NET framework (as much as is[/color]
> > reasonable[color=darkred]
> > > > and I'll find out what that really means when its mature enough to[/color][/color][/color]
start[color=blue][color=green][color=darkred]
> > > > using reliably).
> > > >
> > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color][/color]
with[color=blue][color=green][color=darkred]
> > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color][/color]
emulating[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color][/color]
(the[color=blue][color=green][color=darkred]
> > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color][/color]
processor).[color=blue][color=green]
> > I[color=darkred]
> > > > ultimately will make this Interfacable so I can write a Z80 core, as[/color]
> > well.[color=darkred]
> > > >
> > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color][/color]
them[color=blue][color=green][color=darkred]
> > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color][/color]
decent[color=blue][color=green]
> > but I[color=darkred]
> > > > have to make too many function calls (read, jumps) for my tastes but[/color]
> > doesn't[color=darkred]
> > > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color]
> > I'll[color=darkred]
> > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > generate the best code for the rotates and shifts because I have to[/color]
> > emulate[color=darkred]
> > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color]
> > better[color=darkred]
> > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color][/color]
of[color=blue][color=green]
> > (64kb[color=darkred]
> > > > in length) but manipulating each byte by substrings has got to be[/color]
> > expensive.[color=darkred]
> > > > I'm looking into whether a byte array would be better.
> > > >
> > > >
> > > > Thanks for the help,
> > > > if interested, I'll post my binary class here for all to see.
> > > >
> > > > I've completed the Rotates, Shift, converting numerics into binary[/color]
> > (string[color=darkred]
> > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color][/color]
numeric,[color=blue][color=green]
> > and[color=darkred]
> > > > allow a way to get/set/toggle individual bits.
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > So, you're writing an emulator for which processor??
> > > > >
> > > > > --
> > > > > Bob Powell [MVP]
> > > > > C#, System.Drawing
> > > > >
> > > > > September's edition of Well Formed is now available.
> > > > > http://www.bobpowell.net/currentissue.htm
> > > > >
> > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > >
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > Greetings,
> > > > > >
> > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color][/color]
works[color=blue][color=green][color=darkred]
> > > > > > wonderfully
> > > > > >
> > > > > > ---------
> > > > > > ...
> > > > > > public uint Value;
> > > > > > ...
> > > > > > public uint RotateRight(byte count) {
> > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > return Value;
> > > > > > }
> > > > > > ---------
> > > > > >
> > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color][/color]
limitation of[color=blue][color=green]
> > the[color=darkred]
> > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color][/color]
as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > I
> > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color][/color]
value[color=blue][color=green]
> > and[color=darkred]
> > > > > do
> > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color][/color]
position[color=blue][color=green]
> > and[color=darkred]
> > > > > clear
> > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > >
> > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color][/color]
or[color=blue][color=green]
> > 64-bit[color=darkred]
> > > > > > value?
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#20: Nov 15 '05

re: bitwise rotation


Also,

I'm using byte in some of the parameters such as GetBit(byte index), I'm
wondering, if there incurrs a performance penalty because the processor
thinks in 32-bit and not 8-bit, perhaps I should just make that int? Do you
think it matters? I know in assembly language it takes longer to "think"
8-bit than 32-bit (I program windows in asm as a hobby).


Thanks,
Shawn


"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=blue]
> Hi Shawn,
>
> I've looked at it, and no optimization suggestions other than
> some obvious simple ones:
>
> public int GetBit(byte index) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> uint val = (uint)Value;
> byte shift = (byte)(index - 1);
> if (index == 0)
> return (int)(val & 0x01);
> else
> return (int)(val >> shift) & 0x01;
> }
>
> This is very verbose, and you're creating lots of temp variables
> I'd rather write something like (untested):
>
> public int GetBit(byte index) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> return (int)((Value >> index-1) & 0x01;
> }
>
> The check to avoid a shift probably just hurts performance anyway.
> Also for SetBit, the two if else must result in unwanted branches:
>
> public void SetBit(byte index, int value) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> byte shift = index;
> if (index == 0) {
> if (value == 0)
> Value &= unchecked((ushort)~0x01);
> else
> Value |= 0x01;
> }
> else {
> if (value == 0)
> Value &= ((ushort)~(0x01 << shift));
> else
> Value |= (ushort)(0x01 << shift);
> }
> }
>
> How about this instead (also untested):
>
> public void SetBit(byte index, int value) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> if (value == 0)
> Value &= ((ushort)~(0x01 << index));
> else
> Value |= (ushort)(0x01 << index);
> }
>
> This is the only type of optimizations I came up with, so I thought it[/color]
wasn't worth posting. :)[color=blue]
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=blue][color=green]
> > Well, I've posted it twice and no one is jumpting to optimize it...[/color][/color]
perhaps[color=blue][color=green]
> > it's just so well working that there's no need? he he
> >
> >
> >
> >
> > Thanks,
> > Shawn
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > Do post it here!
> > > I'd be interested to see, as I'm sure many others would.
> > > You'll probably get a ton of optimization hints from it. :)
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Don't miss a thing, d'ya? =))
> > > >
> > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color][/color]
and[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > ability to modify the assembly language while the debugger is[/color][/color][/color]
running[color=blue][color=green][color=darkred]
> > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > instructions. I'm not designing it to be a particular piece of[/color][/color][/color]
hardware[color=blue][color=green][color=darkred]
> > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color][/color]
plugins so[color=blue][color=green]
> > my[color=darkred]
> > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color][/color]
as[color=blue][color=green]
> > text[color=darkred]
> > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color][/color]
with[color=blue][color=green]
> > the[color=darkred]
> > > > text display. My end result, is actually more akin to being able to[/color]
> > debug[color=darkred]
> > > > my NES games I've been into writing lately. Also I want to be ready[/color]
> > with my[color=darkred]
> > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color][/color]
(don't[color=blue][color=green]
> > know[color=darkred]
> > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color][/color]
allows[color=blue][color=green]
> > me[color=darkred]
> > > > to change the code while it's being debugged and my simulator allows[/color][/color][/color]
for[color=blue][color=green]
> > it[color=darkred]
> > > > (as long as the source code file is available -- else if it was[/color][/color][/color]
loading[color=blue][color=green]
> > as a[color=darkred]
> > > > .bin file then it won't be editable during a debug session).
> > > >
> > > > So it's really more than a simulator. It's an integrated[/color]
> > (non-seperable)[color=darkred]
> > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color]
> > beast.[color=darkred]
> > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color][/color]
VB.NET[color=blue][color=green]
> > (I'm[color=darkred]
> > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color][/color]
Managed[color=blue][color=green]
> > C++[color=darkred]
> > > > but also wanted it to target the .NET framework (as much as is[/color]
> > reasonable[color=darkred]
> > > > and I'll find out what that really means when its mature enough to[/color][/color][/color]
start[color=blue][color=green][color=darkred]
> > > > using reliably).
> > > >
> > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color][/color]
with[color=blue][color=green][color=darkred]
> > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color][/color]
emulating[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color][/color]
(the[color=blue][color=green][color=darkred]
> > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color][/color]
processor).[color=blue][color=green]
> > I[color=darkred]
> > > > ultimately will make this Interfacable so I can write a Z80 core, as[/color]
> > well.[color=darkred]
> > > >
> > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color][/color]
them[color=blue][color=green][color=darkred]
> > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color][/color]
decent[color=blue][color=green]
> > but I[color=darkred]
> > > > have to make too many function calls (read, jumps) for my tastes but[/color]
> > doesn't[color=darkred]
> > > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color]
> > I'll[color=darkred]
> > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > generate the best code for the rotates and shifts because I have to[/color]
> > emulate[color=darkred]
> > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color]
> > better[color=darkred]
> > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color][/color]
of[color=blue][color=green]
> > (64kb[color=darkred]
> > > > in length) but manipulating each byte by substrings has got to be[/color]
> > expensive.[color=darkred]
> > > > I'm looking into whether a byte array would be better.
> > > >
> > > >
> > > > Thanks for the help,
> > > > if interested, I'll post my binary class here for all to see.
> > > >
> > > > I've completed the Rotates, Shift, converting numerics into binary[/color]
> > (string[color=darkred]
> > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color][/color]
numeric,[color=blue][color=green]
> > and[color=darkred]
> > > > allow a way to get/set/toggle individual bits.
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > So, you're writing an emulator for which processor??
> > > > >
> > > > > --
> > > > > Bob Powell [MVP]
> > > > > C#, System.Drawing
> > > > >
> > > > > September's edition of Well Formed is now available.
> > > > > http://www.bobpowell.net/currentissue.htm
> > > > >
> > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > >
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > Greetings,
> > > > > >
> > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color][/color]
works[color=blue][color=green][color=darkred]
> > > > > > wonderfully
> > > > > >
> > > > > > ---------
> > > > > > ...
> > > > > > public uint Value;
> > > > > > ...
> > > > > > public uint RotateRight(byte count) {
> > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > return Value;
> > > > > > }
> > > > > > ---------
> > > > > >
> > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color][/color]
limitation of[color=blue][color=green]
> > the[color=darkred]
> > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color][/color]
as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > I
> > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color][/color]
value[color=blue][color=green]
> > and[color=darkred]
> > > > > do
> > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color][/color]
position[color=blue][color=green]
> > and[color=darkred]
> > > > > clear
> > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > >
> > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color][/color]
or[color=blue][color=green]
> > 64-bit[color=darkred]
> > > > > > value?
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#21: Nov 15 '05

re: bitwise rotation


Robert,

I updated the objects with the opimizations you suggested and a few others I
thought were neccessary. I also added an "And" and "Or" method to the
classes. I changed the Rotate and Shift methods to not set Value and then
return Value. Now they only return it. The problem was if I said "bin2 =
bin.RotateLeft(2);" both bin2 and bin would end up with a new value. I
originally did that because before I was overloading the operators I was
actually just saying "bin.RotateLeft(2);" then later decided to make it
return the new value, as well. Now it is not necessary with all the
overloads. Not very consistant with the rest of the .NET framework so I
corrected it. I put a special thanks in at the end of the ReadMe.

Okay, now, let me know if you have any other ideas or optimization tips =)).


Thanks,
Shawn



"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=blue]
> Hi Shawn,
>
> I've looked at it, and no optimization suggestions other than
> some obvious simple ones:
>
> public int GetBit(byte index) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> uint val = (uint)Value;
> byte shift = (byte)(index - 1);
> if (index == 0)
> return (int)(val & 0x01);
> else
> return (int)(val >> shift) & 0x01;
> }
>
> This is very verbose, and you're creating lots of temp variables
> I'd rather write something like (untested):
>
> public int GetBit(byte index) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> return (int)((Value >> index-1) & 0x01;
> }
>
> The check to avoid a shift probably just hurts performance anyway.
> Also for SetBit, the two if else must result in unwanted branches:
>
> public void SetBit(byte index, int value) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> byte shift = index;
> if (index == 0) {
> if (value == 0)
> Value &= unchecked((ushort)~0x01);
> else
> Value |= 0x01;
> }
> else {
> if (value == 0)
> Value &= ((ushort)~(0x01 << shift));
> else
> Value |= (ushort)(0x01 << shift);
> }
> }
>
> How about this instead (also untested):
>
> public void SetBit(byte index, int value) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
does not exist.");[color=blue]
> }
> if (value == 0)
> Value &= ((ushort)~(0x01 << index));
> else
> Value |= (ushort)(0x01 << index);
> }
>
> This is the only type of optimizations I came up with, so I thought it[/color]
wasn't worth posting. :)[color=blue]
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=blue][color=green]
> > Well, I've posted it twice and no one is jumpting to optimize it...[/color][/color]
perhaps[color=blue][color=green]
> > it's just so well working that there's no need? he he
> >
> >
> >
> >
> > Thanks,
> > Shawn
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > Do post it here!
> > > I'd be interested to see, as I'm sure many others would.
> > > You'll probably get a ton of optimization hints from it. :)
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Don't miss a thing, d'ya? =))
> > > >
> > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color][/color]
and[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > ability to modify the assembly language while the debugger is[/color][/color][/color]
running[color=blue][color=green][color=darkred]
> > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > instructions. I'm not designing it to be a particular piece of[/color][/color][/color]
hardware[color=blue][color=green][color=darkred]
> > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color][/color]
plugins so[color=blue][color=green]
> > my[color=darkred]
> > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color][/color]
as[color=blue][color=green]
> > text[color=darkred]
> > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color][/color]
with[color=blue][color=green]
> > the[color=darkred]
> > > > text display. My end result, is actually more akin to being able to[/color]
> > debug[color=darkred]
> > > > my NES games I've been into writing lately. Also I want to be ready[/color]
> > with my[color=darkred]
> > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color][/color]
(don't[color=blue][color=green]
> > know[color=darkred]
> > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color][/color]
allows[color=blue][color=green]
> > me[color=darkred]
> > > > to change the code while it's being debugged and my simulator allows[/color][/color][/color]
for[color=blue][color=green]
> > it[color=darkred]
> > > > (as long as the source code file is available -- else if it was[/color][/color][/color]
loading[color=blue][color=green]
> > as a[color=darkred]
> > > > .bin file then it won't be editable during a debug session).
> > > >
> > > > So it's really more than a simulator. It's an integrated[/color]
> > (non-seperable)[color=darkred]
> > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a[/color]
> > beast.[color=darkred]
> > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color][/color]
VB.NET[color=blue][color=green]
> > (I'm[color=darkred]
> > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color][/color]
Managed[color=blue][color=green]
> > C++[color=darkred]
> > > > but also wanted it to target the .NET framework (as much as is[/color]
> > reasonable[color=darkred]
> > > > and I'll find out what that really means when its mature enough to[/color][/color][/color]
start[color=blue][color=green][color=darkred]
> > > > using reliably).
> > > >
> > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color][/color]
with[color=blue][color=green][color=darkred]
> > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color][/color]
emulating[color=blue][color=green]
> > the[color=darkred]
> > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color][/color]
(the[color=blue][color=green][color=darkred]
> > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color][/color]
processor).[color=blue][color=green]
> > I[color=darkred]
> > > > ultimately will make this Interfacable so I can write a Z80 core, as[/color]
> > well.[color=darkred]
> > > >
> > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color][/color]
them[color=blue][color=green][color=darkred]
> > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color][/color]
decent[color=blue][color=green]
> > but I[color=darkred]
> > > > have to make too many function calls (read, jumps) for my tastes but[/color]
> > doesn't[color=darkred]
> > > > seem to be hurting me at the moment). Since I can't inline in .NET,[/color]
> > I'll[color=darkred]
> > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > generate the best code for the rotates and shifts because I have to[/color]
> > emulate[color=darkred]
> > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a[/color]
> > better[color=darkred]
> > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color][/color]
of[color=blue][color=green]
> > (64kb[color=darkred]
> > > > in length) but manipulating each byte by substrings has got to be[/color]
> > expensive.[color=darkred]
> > > > I'm looking into whether a byte array would be better.
> > > >
> > > >
> > > > Thanks for the help,
> > > > if interested, I'll post my binary class here for all to see.
> > > >
> > > > I've completed the Rotates, Shift, converting numerics into binary[/color]
> > (string[color=darkred]
> > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color][/color]
numeric,[color=blue][color=green]
> > and[color=darkred]
> > > > allow a way to get/set/toggle individual bits.
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > So, you're writing an emulator for which processor??
> > > > >
> > > > > --
> > > > > Bob Powell [MVP]
> > > > > C#, System.Drawing
> > > > >
> > > > > September's edition of Well Formed is now available.
> > > > > http://www.bobpowell.net/currentissue.htm
> > > > >
> > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > >
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > Greetings,
> > > > > >
> > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color][/color]
works[color=blue][color=green][color=darkred]
> > > > > > wonderfully
> > > > > >
> > > > > > ---------
> > > > > > ...
> > > > > > public uint Value;
> > > > > > ...
> > > > > > public uint RotateRight(byte count) {
> > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > return Value;
> > > > > > }
> > > > > > ---------
> > > > > >
> > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color][/color]
limitation of[color=blue][color=green]
> > the[color=darkred]
> > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color][/color]
as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > I
> > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color][/color]
value[color=blue][color=green]
> > and[color=darkred]
> > > > > do
> > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color][/color]
position[color=blue][color=green]
> > and[color=darkred]
> > > > > clear
> > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > >
> > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color][/color]
or[color=blue][color=green]
> > 64-bit[color=darkred]
> > > > > > value?
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#22: Nov 15 '05

re: bitwise rotation


Hi Shawn,

In GetBit, SetBit or in shift operators, you don't need to check of are shifting 0 steps, just do it anyway. It will return the
correct value.
Couldn't you have an indexer on the BinaryXX class itself?
Then you'd say,
BinaryX[0] = 1;
BinaryX[2] = BinaryX[0];

For me, that would make more sense than a separate Bit class.

public int this [int index] // Indexer declaration
{
get
{
//GetBit here
}
set
{
//SetBit here
}
}


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=blue]
> Robert,
>
> You're first suggestion crossed my mind, I have already started eliminating
> the temp variables thus far (thus far only in the 8-bit operator overloads
> region area). Just be happy you didn't see the earlier version I first
> posted, it didn't cast because I kept getting compile errors, it ran
> everything through a Convert.ToUIntX() call but I've since removed those...
>
> I also plan to move the Shift function logic into the overload to avoid the
> extra function call.
>
> The GetBit optimization hint you provided is what I've been wanting to do
> but didn't quite put it they way you did... I'll try that and see how it
> works. I don't know what to do about that check in it, though. I feel like
> it's necessary. Perhaps I could just make the if based on some calculation
> and then just throw an exception at the end of the function and avoid that
> early check, perhaps that would be a better approach. Such as (if (Value ==
> 0 && Value < SIZE-1) {)
>
> There is one thing I need help on.
>
>
> I don't like the GetBit, SetBit approach. I would rather be able to say x =
> BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
> indexer but it had problems. Not the least of which was a circular
> reference that ended up being a memory leak, not what I wanted in the
> innermost loop of an emulator. The second being that I could easily say x =
> x.Bit[0] but not easily say x.Bit[0] = 1.
>
> Do you have any ideas how I might approach this? I was using a property
> get/set at first but that didn't work, instead I just created a public
> module variable "public Bit Bit;" and then it had a constructor to pass a
> reference of the Binary class into it so it would be able to change the bit
> if a bit was set. Not a good approach. Do you have any ideas?
>
>
> Thanks for your help,
> Shawn
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=green]
> > Hi Shawn,
> >
> > I've looked at it, and no optimization suggestions other than
> > some obvious simple ones:
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > uint val = (uint)Value;
> > byte shift = (byte)(index - 1);
> > if (index == 0)
> > return (int)(val & 0x01);
> > else
> > return (int)(val >> shift) & 0x01;
> > }
> >
> > This is very verbose, and you're creating lots of temp variables
> > I'd rather write something like (untested):
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > return (int)((Value >> index-1) & 0x01;
> > }
> >
> > The check to avoid a shift probably just hurts performance anyway.
> > Also for SetBit, the two if else must result in unwanted branches:
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > byte shift = index;
> > if (index == 0) {
> > if (value == 0)
> > Value &= unchecked((ushort)~0x01);
> > else
> > Value |= 0x01;
> > }
> > else {
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << shift));
> > else
> > Value |= (ushort)(0x01 << shift);
> > }
> > }
> >
> > How about this instead (also untested):
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << index));
> > else
> > Value |= (ushort)(0x01 << index);
> > }
> >
> > This is the only type of optimizations I came up with, so I thought it[/color]
> wasn't worth posting. :)[color=green]
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=green][color=darkred]
> > > Well, I've posted it twice and no one is jumpting to optimize it...[/color][/color]
> perhaps[color=green][color=darkred]
> > > it's just so well working that there's no need? he he
> > >
> > >
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > Do post it here!
> > > > I'd be interested to see, as I'm sure many others would.
> > > > You'll probably get a ton of optimization hints from it. :)
> > > >
> > > >
> > > > --
> > > > Robert Jeppesen
> > > > robert.jeppesen%at%durius-dot-se
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > Don't miss a thing, d'ya? =))
> > > > >
> > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color]
> and[color=green][color=darkred]
> > > the
> > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color]
> the[color=green][color=darkred]
> > > > > ability to modify the assembly language while the debugger is[/color][/color]
> running[color=green][color=darkred]
> > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > instructions. I'm not designing it to be a particular piece of[/color][/color]
> hardware[color=green][color=darkred]
> > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color]
> plugins so[color=green][color=darkred]
> > > my
> > > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color]
> as[color=green][color=darkred]
> > > text
> > > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color]
> with[color=green][color=darkred]
> > > the
> > > > > text display. My end result, is actually more akin to being able to
> > > debug
> > > > > my NES games I've been into writing lately. Also I want to be ready
> > > with my
> > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color]
> (don't[color=green][color=darkred]
> > > know
> > > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color]
> allows[color=green][color=darkred]
> > > me
> > > > > to change the code while it's being debugged and my simulator allows[/color][/color]
> for[color=green][color=darkred]
> > > it
> > > > > (as long as the source code file is available -- else if it was[/color][/color]
> loading[color=green][color=darkred]
> > > as a
> > > > > .bin file then it won't be editable during a debug session).
> > > > >
> > > > > So it's really more than a simulator. It's an integrated
> > > (non-seperable)
> > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
> > > beast.
> > > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color]
> VB.NET[color=green][color=darkred]
> > > (I'm
> > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color]
> Managed[color=green][color=darkred]
> > > C++
> > > > > but also wanted it to target the .NET framework (as much as is
> > > reasonable
> > > > > and I'll find out what that really means when its mature enough to[/color][/color]
> start[color=green][color=darkred]
> > > > > using reliably).
> > > > >
> > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color]
> with[color=green][color=darkred]
> > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color]
> emulating[color=green][color=darkred]
> > > the
> > > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color]
> (the[color=green][color=darkred]
> > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color]
> processor).[color=green][color=darkred]
> > > I
> > > > > ultimately will make this Interfacable so I can write a Z80 core, as
> > > well.
> > > > >
> > > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color]
> them[color=green][color=darkred]
> > > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color]
> decent[color=green][color=darkred]
> > > but I
> > > > > have to make too many function calls (read, jumps) for my tastes but
> > > doesn't
> > > > > seem to be hurting me at the moment). Since I can't inline in .NET,
> > > I'll
> > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > > generate the best code for the rotates and shifts because I have to
> > > emulate
> > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
> > > better
> > > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color]
> of[color=green][color=darkred]
> > > (64kb
> > > > > in length) but manipulating each byte by substrings has got to be
> > > expensive.
> > > > > I'm looking into whether a byte array would be better.
> > > > >
> > > > >
> > > > > Thanks for the help,
> > > > > if interested, I'll post my binary class here for all to see.
> > > > >
> > > > > I've completed the Rotates, Shift, converting numerics into binary
> > > (string
> > > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color]
> numeric,[color=green][color=darkred]
> > > and
> > > > > allow a way to get/set/toggle individual bits.
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > So, you're writing an emulator for which processor??
> > > > > >
> > > > > > --
> > > > > > Bob Powell [MVP]
> > > > > > C#, System.Drawing
> > > > > >
> > > > > > September's edition of Well Formed is now available.
> > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > >
> > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > >
> > > > > >
> > > > > >
> > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > Greetings,
> > > > > > >
> > > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color]
> works[color=green][color=darkred]
> > > > > > > wonderfully
> > > > > > >
> > > > > > > ---------
> > > > > > > ...
> > > > > > > public uint Value;
> > > > > > > ...
> > > > > > > public uint RotateRight(byte count) {
> > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > return Value;
> > > > > > > }
> > > > > > > ---------
> > > > > > >
> > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color]
> limitation of[color=green][color=darkred]
> > > the
> > > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color]
> as[color=green][color=darkred]
> > > > > well.
> > > > > > I
> > > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color]
> value[color=green][color=darkred]
> > > and
> > > > > > do
> > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color]
> position[color=green][color=darkred]
> > > and
> > > > > > clear
> > > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > > >
> > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color]
> or[color=green][color=darkred]
> > > 64-bit
> > > > > > > value?
> > > > > > >
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Shawn
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#23: Nov 15 '05

re: bitwise rotation


I think in index parameters and such, you'd probably be better off using an int,
as int is sort of the default. If you use byte, an implicit conversion occurs.

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:u5WUAiqkDHA.1084@tk2msftngp13.phx.gbl...[color=blue]
> Also,
>
> I'm using byte in some of the parameters such as GetBit(byte index), I'm
> wondering, if there incurrs a performance penalty because the processor
> thinks in 32-bit and not 8-bit, perhaps I should just make that int? Do you
> think it matters? I know in assembly language it takes longer to "think"
> 8-bit than 32-bit (I program windows in asm as a hobby).
>
>
> Thanks,
> Shawn
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=green]
> > Hi Shawn,
> >
> > I've looked at it, and no optimization suggestions other than
> > some obvious simple ones:
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > uint val = (uint)Value;
> > byte shift = (byte)(index - 1);
> > if (index == 0)
> > return (int)(val & 0x01);
> > else
> > return (int)(val >> shift) & 0x01;
> > }
> >
> > This is very verbose, and you're creating lots of temp variables
> > I'd rather write something like (untested):
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > return (int)((Value >> index-1) & 0x01;
> > }
> >
> > The check to avoid a shift probably just hurts performance anyway.
> > Also for SetBit, the two if else must result in unwanted branches:
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > byte shift = index;
> > if (index == 0) {
> > if (value == 0)
> > Value &= unchecked((ushort)~0x01);
> > else
> > Value |= 0x01;
> > }
> > else {
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << shift));
> > else
> > Value |= (ushort)(0x01 << shift);
> > }
> > }
> >
> > How about this instead (also untested):
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << index));
> > else
> > Value |= (ushort)(0x01 << index);
> > }
> >
> > This is the only type of optimizations I came up with, so I thought it[/color]
> wasn't worth posting. :)[color=green]
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=green][color=darkred]
> > > Well, I've posted it twice and no one is jumpting to optimize it...[/color][/color]
> perhaps[color=green][color=darkred]
> > > it's just so well working that there's no need? he he
> > >
> > >
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > Do post it here!
> > > > I'd be interested to see, as I'm sure many others would.
> > > > You'll probably get a ton of optimization hints from it. :)
> > > >
> > > >
> > > > --
> > > > Robert Jeppesen
> > > > robert.jeppesen%at%durius-dot-se
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > Don't miss a thing, d'ya? =))
> > > > >
> > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color]
> and[color=green][color=darkred]
> > > the
> > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color]
> the[color=green][color=darkred]
> > > > > ability to modify the assembly language while the debugger is[/color][/color]
> running[color=green][color=darkred]
> > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > instructions. I'm not designing it to be a particular piece of[/color][/color]
> hardware[color=green][color=darkred]
> > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color]
> plugins so[color=green][color=darkred]
> > > my
> > > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color]
> as[color=green][color=darkred]
> > > text
> > > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color]
> with[color=green][color=darkred]
> > > the
> > > > > text display. My end result, is actually more akin to being able to
> > > debug
> > > > > my NES games I've been into writing lately. Also I want to be ready
> > > with my
> > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color]
> (don't[color=green][color=darkred]
> > > know
> > > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color]
> allows[color=green][color=darkred]
> > > me
> > > > > to change the code while it's being debugged and my simulator allows[/color][/color]
> for[color=green][color=darkred]
> > > it
> > > > > (as long as the source code file is available -- else if it was[/color][/color]
> loading[color=green][color=darkred]
> > > as a
> > > > > .bin file then it won't be editable during a debug session).
> > > > >
> > > > > So it's really more than a simulator. It's an integrated
> > > (non-seperable)
> > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
> > > beast.
> > > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color]
> VB.NET[color=green][color=darkred]
> > > (I'm
> > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color]
> Managed[color=green][color=darkred]
> > > C++
> > > > > but also wanted it to target the .NET framework (as much as is
> > > reasonable
> > > > > and I'll find out what that really means when its mature enough to[/color][/color]
> start[color=green][color=darkred]
> > > > > using reliably).
> > > > >
> > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color]
> with[color=green][color=darkred]
> > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color]
> emulating[color=green][color=darkred]
> > > the
> > > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color]
> (the[color=green][color=darkred]
> > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color]
> processor).[color=green][color=darkred]
> > > I
> > > > > ultimately will make this Interfacable so I can write a Z80 core, as
> > > well.
> > > > >
> > > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color]
> them[color=green][color=darkred]
> > > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color]
> decent[color=green][color=darkred]
> > > but I
> > > > > have to make too many function calls (read, jumps) for my tastes but
> > > doesn't
> > > > > seem to be hurting me at the moment). Since I can't inline in .NET,
> > > I'll
> > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > > generate the best code for the rotates and shifts because I have to
> > > emulate
> > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
> > > better
> > > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color]
> of[color=green][color=darkred]
> > > (64kb
> > > > > in length) but manipulating each byte by substrings has got to be
> > > expensive.
> > > > > I'm looking into whether a byte array would be better.
> > > > >
> > > > >
> > > > > Thanks for the help,
> > > > > if interested, I'll post my binary class here for all to see.
> > > > >
> > > > > I've completed the Rotates, Shift, converting numerics into binary
> > > (string
> > > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color]
> numeric,[color=green][color=darkred]
> > > and
> > > > > allow a way to get/set/toggle individual bits.
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > So, you're writing an emulator for which processor??
> > > > > >
> > > > > > --
> > > > > > Bob Powell [MVP]
> > > > > > C#, System.Drawing
> > > > > >
> > > > > > September's edition of Well Formed is now available.
> > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > >
> > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > >
> > > > > >
> > > > > >
> > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > Greetings,
> > > > > > >
> > > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color]
> works[color=green][color=darkred]
> > > > > > > wonderfully
> > > > > > >
> > > > > > > ---------
> > > > > > > ...
> > > > > > > public uint Value;
> > > > > > > ...
> > > > > > > public uint RotateRight(byte count) {
> > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > return Value;
> > > > > > > }
> > > > > > > ---------
> > > > > > >
> > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color]
> limitation of[color=green][color=darkred]
> > > the
> > > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color]
> as[color=green][color=darkred]
> > > > > well.
> > > > > > I
> > > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color]
> value[color=green][color=darkred]
> > > and
> > > > > > do
> > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color]
> position[color=green][color=darkred]
> > > and
> > > > > > clear
> > > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > > >
> > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color]
> or[color=green][color=darkred]
> > > 64-bit
> > > > > > > value?
> > > > > > >
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Shawn
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#24: Nov 15 '05

re: bitwise rotation


Yes, it's better to just return the value in those cases.
I'm gonna go check the new code now. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:u9hmviukDHA.2772@TK2MSFTNGP12.phx.gbl...[color=blue]
> Robert,
>
> I updated the objects with the opimizations you suggested and a few others I
> thought were neccessary. I also added an "And" and "Or" method to the
> classes. I changed the Rotate and Shift methods to not set Value and then
> return Value. Now they only return it. The problem was if I said "bin2 =
> bin.RotateLeft(2);" both bin2 and bin would end up with a new value. I
> originally did that because before I was overloading the operators I was
> actually just saying "bin.RotateLeft(2);" then later decided to make it
> return the new value, as well. Now it is not necessary with all the
> overloads. Not very consistant with the rest of the .NET framework so I
> corrected it. I put a special thanks in at the end of the ReadMe.
>
> Okay, now, let me know if you have any other ideas or optimization tips =)).
>
>
> Thanks,
> Shawn
>
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=green]
> > Hi Shawn,
> >
> > I've looked at it, and no optimization suggestions other than
> > some obvious simple ones:
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > uint val = (uint)Value;
> > byte shift = (byte)(index - 1);
> > if (index == 0)
> > return (int)(val & 0x01);
> > else
> > return (int)(val >> shift) & 0x01;
> > }
> >
> > This is very verbose, and you're creating lots of temp variables
> > I'd rather write something like (untested):
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > return (int)((Value >> index-1) & 0x01;
> > }
> >
> > The check to avoid a shift probably just hurts performance anyway.
> > Also for SetBit, the two if else must result in unwanted branches:
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > byte shift = index;
> > if (index == 0) {
> > if (value == 0)
> > Value &= unchecked((ushort)~0x01);
> > else
> > Value |= 0x01;
> > }
> > else {
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << shift));
> > else
> > Value |= (ushort)(0x01 << shift);
> > }
> > }
> >
> > How about this instead (also untested):
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << index));
> > else
> > Value |= (ushort)(0x01 << index);
> > }
> >
> > This is the only type of optimizations I came up with, so I thought it[/color]
> wasn't worth posting. :)[color=green]
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=green][color=darkred]
> > > Well, I've posted it twice and no one is jumpting to optimize it...[/color][/color]
> perhaps[color=green][color=darkred]
> > > it's just so well working that there's no need? he he
> > >
> > >
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > Do post it here!
> > > > I'd be interested to see, as I'm sure many others would.
> > > > You'll probably get a ton of optimization hints from it. :)
> > > >
> > > >
> > > > --
> > > > Robert Jeppesen
> > > > robert.jeppesen%at%durius-dot-se
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > Don't miss a thing, d'ya? =))
> > > > >
> > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color]
> and[color=green][color=darkred]
> > > the
> > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color]
> the[color=green][color=darkred]
> > > > > ability to modify the assembly language while the debugger is[/color][/color]
> running[color=green][color=darkred]
> > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > instructions. I'm not designing it to be a particular piece of[/color][/color]
> hardware[color=green][color=darkred]
> > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color]
> plugins so[color=green][color=darkred]
> > > my
> > > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color]
> as[color=green][color=darkred]
> > > text
> > > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color]
> with[color=green][color=darkred]
> > > the
> > > > > text display. My end result, is actually more akin to being able to
> > > debug
> > > > > my NES games I've been into writing lately. Also I want to be ready
> > > with my
> > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color]
> (don't[color=green][color=darkred]
> > > know
> > > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color]
> allows[color=green][color=darkred]
> > > me
> > > > > to change the code while it's being debugged and my simulator allows[/color][/color]
> for[color=green][color=darkred]
> > > it
> > > > > (as long as the source code file is available -- else if it was[/color][/color]
> loading[color=green][color=darkred]
> > > as a
> > > > > .bin file then it won't be editable during a debug session).
> > > > >
> > > > > So it's really more than a simulator. It's an integrated
> > > (non-seperable)
> > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
> > > beast.
> > > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color]
> VB.NET[color=green][color=darkred]
> > > (I'm
> > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color]
> Managed[color=green][color=darkred]
> > > C++
> > > > > but also wanted it to target the .NET framework (as much as is
> > > reasonable
> > > > > and I'll find out what that really means when its mature enough to[/color][/color]
> start[color=green][color=darkred]
> > > > > using reliably).
> > > > >
> > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color]
> with[color=green][color=darkred]
> > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color]
> emulating[color=green][color=darkred]
> > > the
> > > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color]
> (the[color=green][color=darkred]
> > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color]
> processor).[color=green][color=darkred]
> > > I
> > > > > ultimately will make this Interfacable so I can write a Z80 core, as
> > > well.
> > > > >
> > > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color]
> them[color=green][color=darkred]
> > > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color]
> decent[color=green][color=darkred]
> > > but I
> > > > > have to make too many function calls (read, jumps) for my tastes but
> > > doesn't
> > > > > seem to be hurting me at the moment). Since I can't inline in .NET,
> > > I'll
> > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > > generate the best code for the rotates and shifts because I have to
> > > emulate
> > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
> > > better
> > > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color]
> of[color=green][color=darkred]
> > > (64kb
> > > > > in length) but manipulating each byte by substrings has got to be
> > > expensive.
> > > > > I'm looking into whether a byte array would be better.
> > > > >
> > > > >
> > > > > Thanks for the help,
> > > > > if interested, I'll post my binary class here for all to see.
> > > > >
> > > > > I've completed the Rotates, Shift, converting numerics into binary
> > > (string
> > > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color]
> numeric,[color=green][color=darkred]
> > > and
> > > > > allow a way to get/set/toggle individual bits.
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > So, you're writing an emulator for which processor??
> > > > > >
> > > > > > --
> > > > > > Bob Powell [MVP]
> > > > > > C#, System.Drawing
> > > > > >
> > > > > > September's edition of Well Formed is now available.
> > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > >
> > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > >
> > > > > >
> > > > > >
> > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > Greetings,
> > > > > > >
> > > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color]
> works[color=green][color=darkred]
> > > > > > > wonderfully
> > > > > > >
> > > > > > > ---------
> > > > > > > ...
> > > > > > > public uint Value;
> > > > > > > ...
> > > > > > > public uint RotateRight(byte count) {
> > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > return Value;
> > > > > > > }
> > > > > > > ---------
> > > > > > >
> > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color]
> limitation of[color=green][color=darkred]
> > > the
> > > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color]
> as[color=green][color=darkred]
> > > > > well.
> > > > > > I
> > > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color]
> value[color=green][color=darkred]
> > > and
> > > > > > do
> > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color]
> position[color=green][color=darkred]
> > > and
> > > > > > clear
> > > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > > >
> > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color]
> or[color=green][color=darkred]
> > > 64-bit
> > > > > > > value?
> > > > > > >
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Shawn
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#25: Nov 15 '05

re: bitwise rotation


Another suggestion..
You have a method called IsBinary to determine whether a string is valid and can be parsed.
In the framework, methods called IsXXX and return a bool usually won't throw an exception (which you method does).
The purpose of these methods (as far as I know) is to avoid catching errors by Exception handling, cause it incurrs such a
performance penalty. Remove the exception from this method and return false if the string is too long to be compliant.

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=blue]
> Robert,
>
> You're first suggestion crossed my mind, I have already started eliminating
> the temp variables thus far (thus far only in the 8-bit operator overloads
> region area). Just be happy you didn't see the earlier version I first
> posted, it didn't cast because I kept getting compile errors, it ran
> everything through a Convert.ToUIntX() call but I've since removed those...
>
> I also plan to move the Shift function logic into the overload to avoid the
> extra function call.
>
> The GetBit optimization hint you provided is what I've been wanting to do
> but didn't quite put it they way you did... I'll try that and see how it
> works. I don't know what to do about that check in it, though. I feel like
> it's necessary. Perhaps I could just make the if based on some calculation
> and then just throw an exception at the end of the function and avoid that
> early check, perhaps that would be a better approach. Such as (if (Value ==
> 0 && Value < SIZE-1) {)
>
> There is one thing I need help on.
>
>
> I don't like the GetBit, SetBit approach. I would rather be able to say x =
> BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
> indexer but it had problems. Not the least of which was a circular
> reference that ended up being a memory leak, not what I wanted in the
> innermost loop of an emulator. The second being that I could easily say x =
> x.Bit[0] but not easily say x.Bit[0] = 1.
>
> Do you have any ideas how I might approach this? I was using a property
> get/set at first but that didn't work, instead I just created a public
> module variable "public Bit Bit;" and then it had a constructor to pass a
> reference of the Binary class into it so it would be able to change the bit
> if a bit was set. Not a good approach. Do you have any ideas?
>
>
> Thanks for your help,
> Shawn
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=green]
> > Hi Shawn,
> >
> > I've looked at it, and no optimization suggestions other than
> > some obvious simple ones:
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > uint val = (uint)Value;
> > byte shift = (byte)(index - 1);
> > if (index == 0)
> > return (int)(val & 0x01);
> > else
> > return (int)(val >> shift) & 0x01;
> > }
> >
> > This is very verbose, and you're creating lots of temp variables
> > I'd rather write something like (untested):
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > return (int)((Value >> index-1) & 0x01;
> > }
> >
> > The check to avoid a shift probably just hurts performance anyway.
> > Also for SetBit, the two if else must result in unwanted branches:
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > byte shift = index;
> > if (index == 0) {
> > if (value == 0)
> > Value &= unchecked((ushort)~0x01);
> > else
> > Value |= 0x01;
> > }
> > else {
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << shift));
> > else
> > Value |= (ushort)(0x01 << shift);
> > }
> > }
> >
> > How about this instead (also untested):
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << index));
> > else
> > Value |= (ushort)(0x01 << index);
> > }
> >
> > This is the only type of optimizations I came up with, so I thought it[/color]
> wasn't worth posting. :)[color=green]
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=green][color=darkred]
> > > Well, I've posted it twice and no one is jumpting to optimize it...[/color][/color]
> perhaps[color=green][color=darkred]
> > > it's just so well working that there's no need? he he
> > >
> > >
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > Do post it here!
> > > > I'd be interested to see, as I'm sure many others would.
> > > > You'll probably get a ton of optimization hints from it. :)
> > > >
> > > >
> > > > --
> > > > Robert Jeppesen
> > > > robert.jeppesen%at%durius-dot-se
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > Don't miss a thing, d'ya? =))
> > > > >
> > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color]
> and[color=green][color=darkred]
> > > the
> > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color]
> the[color=green][color=darkred]
> > > > > ability to modify the assembly language while the debugger is[/color][/color]
> running[color=green][color=darkred]
> > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > instructions. I'm not designing it to be a particular piece of[/color][/color]
> hardware[color=green][color=darkred]
> > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color]
> plugins so[color=green][color=darkred]
> > > my
> > > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color]
> as[color=green][color=darkred]
> > > text
> > > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color]
> with[color=green][color=darkred]
> > > the
> > > > > text display. My end result, is actually more akin to being able to
> > > debug
> > > > > my NES games I've been into writing lately. Also I want to be ready
> > > with my
> > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color]
> (don't[color=green][color=darkred]
> > > know
> > > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color]
> allows[color=green][color=darkred]
> > > me
> > > > > to change the code while it's being debugged and my simulator allows[/color][/color]
> for[color=green][color=darkred]
> > > it
> > > > > (as long as the source code file is available -- else if it was[/color][/color]
> loading[color=green][color=darkred]
> > > as a
> > > > > .bin file then it won't be editable during a debug session).
> > > > >
> > > > > So it's really more than a simulator. It's an integrated
> > > (non-seperable)
> > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
> > > beast.
> > > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color]
> VB.NET[color=green][color=darkred]
> > > (I'm
> > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color]
> Managed[color=green][color=darkred]
> > > C++
> > > > > but also wanted it to target the .NET framework (as much as is
> > > reasonable
> > > > > and I'll find out what that really means when its mature enough to[/color][/color]
> start[color=green][color=darkred]
> > > > > using reliably).
> > > > >
> > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color]
> with[color=green][color=darkred]
> > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color]
> emulating[color=green][color=darkred]
> > > the
> > > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color]
> (the[color=green][color=darkred]
> > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color]
> processor).[color=green][color=darkred]
> > > I
> > > > > ultimately will make this Interfacable so I can write a Z80 core, as
> > > well.
> > > > >
> > > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color]
> them[color=green][color=darkred]
> > > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color]
> decent[color=green][color=darkred]
> > > but I
> > > > > have to make too many function calls (read, jumps) for my tastes but
> > > doesn't
> > > > > seem to be hurting me at the moment). Since I can't inline in .NET,
> > > I'll
> > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > > generate the best code for the rotates and shifts because I have to
> > > emulate
> > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
> > > better
> > > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color]
> of[color=green][color=darkred]
> > > (64kb
> > > > > in length) but manipulating each byte by substrings has got to be
> > > expensive.
> > > > > I'm looking into whether a byte array would be better.
> > > > >
> > > > >
> > > > > Thanks for the help,
> > > > > if interested, I'll post my binary class here for all to see.
> > > > >
> > > > > I've completed the Rotates, Shift, converting numerics into binary
> > > (string
> > > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color]
> numeric,[color=green][color=darkred]
> > > and
> > > > > allow a way to get/set/toggle individual bits.
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > So, you're writing an emulator for which processor??
> > > > > >
> > > > > > --
> > > > > > Bob Powell [MVP]
> > > > > > C#, System.Drawing
> > > > > >
> > > > > > September's edition of Well Formed is now available.
> > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > >
> > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > >
> > > > > >
> > > > > >
> > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > Greetings,
> > > > > > >
> > > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color]
> works[color=green][color=darkred]
> > > > > > > wonderfully
> > > > > > >
> > > > > > > ---------
> > > > > > > ...
> > > > > > > public uint Value;
> > > > > > > ...
> > > > > > > public uint RotateRight(byte count) {
> > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > return Value;
> > > > > > > }
> > > > > > > ---------
> > > > > > >
> > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color]
> limitation of[color=green][color=darkred]
> > > the
> > > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color]
> as[color=green][color=darkred]
> > > > > well.
> > > > > > I
> > > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color]
> value[color=green][color=darkred]
> > > and
> > > > > > do
> > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color]
> position[color=green][color=darkred]
> > > and
> > > > > > clear
> > > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > > >
> > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color]
> or[color=green][color=darkred]
> > > 64-bit
> > > > > > > value?
> > > > > > >
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Shawn
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#26: Nov 15 '05

re: bitwise rotation


Removing the if check in SetBit:
Value = (ushort)((~(0x01 << index))&Value|(0x01 << index));

As always, untested. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=blue]
> Robert,
>
> You're first suggestion crossed my mind, I have already started eliminating
> the temp variables thus far (thus far only in the 8-bit operator overloads
> region area). Just be happy you didn't see the earlier version I first
> posted, it didn't cast because I kept getting compile errors, it ran
> everything through a Convert.ToUIntX() call but I've since removed those...
>
> I also plan to move the Shift function logic into the overload to avoid the
> extra function call.
>
> The GetBit optimization hint you provided is what I've been wanting to do
> but didn't quite put it they way you did... I'll try that and see how it
> works. I don't know what to do about that check in it, though. I feel like
> it's necessary. Perhaps I could just make the if based on some calculation
> and then just throw an exception at the end of the function and avoid that
> early check, perhaps that would be a better approach. Such as (if (Value ==
> 0 && Value < SIZE-1) {)
>
> There is one thing I need help on.
>
>
> I don't like the GetBit, SetBit approach. I would rather be able to say x =
> BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
> indexer but it had problems. Not the least of which was a circular
> reference that ended up being a memory leak, not what I wanted in the
> innermost loop of an emulator. The second being that I could easily say x =
> x.Bit[0] but not easily say x.Bit[0] = 1.
>
> Do you have any ideas how I might approach this? I was using a property
> get/set at first but that didn't work, instead I just created a public
> module variable "public Bit Bit;" and then it had a constructor to pass a
> reference of the Binary class into it so it would be able to change the bit
> if a bit was set. Not a good approach. Do you have any ideas?
>
>
> Thanks for your help,
> Shawn
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=green]
> > Hi Shawn,
> >
> > I've looked at it, and no optimization suggestions other than
> > some obvious simple ones:
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > uint val = (uint)Value;
> > byte shift = (byte)(index - 1);
> > if (index == 0)
> > return (int)(val & 0x01);
> > else
> > return (int)(val >> shift) & 0x01;
> > }
> >
> > This is very verbose, and you're creating lots of temp variables
> > I'd rather write something like (untested):
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > return (int)((Value >> index-1) & 0x01;
> > }
> >
> > The check to avoid a shift probably just hurts performance anyway.
> > Also for SetBit, the two if else must result in unwanted branches:
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > byte shift = index;
> > if (index == 0) {
> > if (value == 0)
> > Value &= unchecked((ushort)~0x01);
> > else
> > Value |= 0x01;
> > }
> > else {
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << shift));
> > else
> > Value |= (ushort)(0x01 << shift);
> > }
> > }
> >
> > How about this instead (also untested):
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " + index.ToString() + "[/color]
> does not exist.");[color=green]
> > }
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << index));
> > else
> > Value |= (ushort)(0x01 << index);
> > }
> >
> > This is the only type of optimizations I came up with, so I thought it[/color]
> wasn't worth posting. :)[color=green]
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=green][color=darkred]
> > > Well, I've posted it twice and no one is jumpting to optimize it...[/color][/color]
> perhaps[color=green][color=darkred]
> > > it's just so well working that there's no need? he he
> > >
> > >
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > Do post it here!
> > > > I'd be interested to see, as I'm sure many others would.
> > > > You'll probably get a ton of optimization hints from it. :)
> > > >
> > > >
> > > > --
> > > > Robert Jeppesen
> > > > robert.jeppesen%at%durius-dot-se
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > Don't miss a thing, d'ya? =))
> > > > >
> > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02,[/color][/color]
> and[color=green][color=darkred]
> > > the
> > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have[/color][/color]
> the[color=green][color=darkred]
> > > > > ability to modify the assembly language while the debugger is[/color][/color]
> running[color=green][color=darkred]
> > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > instructions. I'm not designing it to be a particular piece of[/color][/color]
> hardware[color=green][color=darkred]
> > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color][/color]
> plugins so[color=green][color=darkred]
> > > my
> > > > > plugins can treat an area of memory as a dedicated "something" such[/color][/color]
> as[color=green][color=darkred]
> > > text
> > > > > display or graphics display, etc. For now, I'm concerned not even[/color][/color]
> with[color=green][color=darkred]
> > > the
> > > > > text display. My end result, is actually more akin to being able to
> > > debug
> > > > > my NES games I've been into writing lately. Also I want to be ready
> > > with my
> > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color][/color]
> (don't[color=green][color=darkred]
> > > know
> > > > > whether he'll have a debugger or not) but I want a debugger that[/color][/color]
> allows[color=green][color=darkred]
> > > me
> > > > > to change the code while it's being debugged and my simulator allows[/color][/color]
> for[color=green][color=darkred]
> > > it
> > > > > (as long as the source code file is available -- else if it was[/color][/color]
> loading[color=green][color=darkred]
> > > as a
> > > > > .bin file then it won't be editable during a debug session).
> > > > >
> > > > > So it's really more than a simulator. It's an integrated
> > > (non-seperable)
> > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
> > > beast.
> > > > > I chose C# because it's not the kind of thing I'd want to do in[/color][/color]
> VB.NET[color=green][color=darkred]
> > > (I'm
> > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color][/color]
> Managed[color=green][color=darkred]
> > > C++
> > > > > but also wanted it to target the .NET framework (as much as is
> > > reasonable
> > > > > and I'll find out what that really means when its mature enough to[/color][/color]
> start[color=green][color=darkred]
> > > > > using reliably).
> > > > >
> > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU[/color][/color]
> with[color=green][color=darkred]
> > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color][/color]
> emulating[color=green][color=darkred]
> > > the
> > > > > 16-bit processor as I haven't deciphered the specs for the CPU yet[/color][/color]
> (the[color=green][color=darkred]
> > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color][/color]
> processor).[color=green][color=darkred]
> > > I
> > > > > ultimately will make this Interfacable so I can write a Z80 core, as
> > > well.
> > > > >
> > > > > These bit manipulations are the utmost inner-loop so I have to make[/color][/color]
> them[color=green][color=darkred]
> > > > > tight. Looking at the JIT'd code for these methods seems to be[/color][/color]
> decent[color=green][color=darkred]
> > > but I
> > > > > have to make too many function calls (read, jumps) for my tastes but
> > > doesn't
> > > > > seem to be hurting me at the moment). Since I can't inline in .NET,
> > > I'll
> > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > > > > generate the best code for the rotates and shifts because I have to
> > > emulate
> > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
> > > better
> > > > > way for simulating memory manipulation. Now I'm creating a string[/color][/color]
> of[color=green][color=darkred]
> > > (64kb
> > > > > in length) but manipulating each byte by substrings has got to be
> > > expensive.
> > > > > I'm looking into whether a byte array would be better.
> > > > >
> > > > >
> > > > > Thanks for the help,
> > > > > if interested, I'll post my binary class here for all to see.
> > > > >
> > > > > I've completed the Rotates, Shift, converting numerics into binary
> > > (string
> > > > > of 1's and 0's). I know now have to read the binary back to a[/color][/color]
> numeric,[color=green][color=darkred]
> > > and
> > > > > allow a way to get/set/toggle individual bits.
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > So, you're writing an emulator for which processor??
> > > > > >
> > > > > > --
> > > > > > Bob Powell [MVP]
> > > > > > C#, System.Drawing
> > > > > >
> > > > > > September's edition of Well Formed is now available.
> > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > >
> > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > >
> > > > > >
> > > > > >
> > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > Greetings,
> > > > > > >
> > > > > > > I am simulating an assembly language bit rotation in C# and it[/color][/color]
> works[color=green][color=darkred]
> > > > > > > wonderfully
> > > > > > >
> > > > > > > ---------
> > > > > > > ...
> > > > > > > public uint Value;
> > > > > > > ...
> > > > > > > public uint RotateRight(byte count) {
> > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > return Value;
> > > > > > > }
> > > > > > > ---------
> > > > > > >
> > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color][/color]
> limitation of[color=green][color=darkred]
> > > the
> > > > > > > shift operator). I need it to work on an 8-bit or 16-bit value,[/color][/color]
> as[color=green][color=darkred]
> > > > > well.
> > > > > > I
> > > > > > > presume I'd have to have knowledge of the intended size of the[/color][/color]
> value[color=green][color=darkred]
> > > and
> > > > > > do
> > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color][/color]
> position[color=green][color=darkred]
> > > and
> > > > > > clear
> > > > > > > the extra bits if there are any. That seems like a lot of work.
> > > > > > >
> > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit[/color][/color]
> or[color=green][color=darkred]
> > > 64-bit
> > > > > > > value?
> > > > > > >
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Shawn
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#27: Nov 15 '05

re: bitwise rotation


Wow, great idea about the indexer, glad I thought of it... =))


Thanks,
Shawn



"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:usKNb2vkDHA.3612@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hi Shawn,
>
> In GetBit, SetBit or in shift operators, you don't need to check of are[/color]
shifting 0 steps, just do it anyway. It will return the[color=blue]
> correct value.
> Couldn't you have an indexer on the BinaryXX class itself?
> Then you'd say,
> BinaryX[0] = 1;
> BinaryX[2] = BinaryX[0];
>
> For me, that would make more sense than a separate Bit class.
>
> public int this [int index] // Indexer declaration
> {
> get
> {
> //GetBit here
> }
> set
> {
> //SetBit here
> }
> }
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=blue][color=green]
> > Robert,
> >
> > You're first suggestion crossed my mind, I have already started[/color][/color]
eliminating[color=blue][color=green]
> > the temp variables thus far (thus far only in the 8-bit operator[/color][/color]
overloads[color=blue][color=green]
> > region area). Just be happy you didn't see the earlier version I first
> > posted, it didn't cast because I kept getting compile errors, it ran
> > everything through a Convert.ToUIntX() call but I've since removed[/color][/color]
those...[color=blue][color=green]
> >
> > I also plan to move the Shift function logic into the overload to avoid[/color][/color]
the[color=blue][color=green]
> > extra function call.
> >
> > The GetBit optimization hint you provided is what I've been wanting to[/color][/color]
do[color=blue][color=green]
> > but didn't quite put it they way you did... I'll try that and see how it
> > works. I don't know what to do about that check in it, though. I feel[/color][/color]
like[color=blue][color=green]
> > it's necessary. Perhaps I could just make the if based on some[/color][/color]
calculation[color=blue][color=green]
> > and then just throw an exception at the end of the function and avoid[/color][/color]
that[color=blue][color=green]
> > early check, perhaps that would be a better approach. Such as (if[/color][/color]
(Value ==[color=blue][color=green]
> > 0 && Value < SIZE-1) {)
> >
> > There is one thing I need help on.
> >
> >
> > I don't like the GetBit, SetBit approach. I would rather be able to say[/color][/color]
x =[color=blue][color=green]
> > BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
> > indexer but it had problems. Not the least of which was a circular
> > reference that ended up being a memory leak, not what I wanted in the
> > innermost loop of an emulator. The second being that I could easily say[/color][/color]
x =[color=blue][color=green]
> > x.Bit[0] but not easily say x.Bit[0] = 1.
> >
> > Do you have any ideas how I might approach this? I was using a property
> > get/set at first but that didn't work, instead I just created a public
> > module variable "public Bit Bit;" and then it had a constructor to pass[/color][/color]
a[color=blue][color=green]
> > reference of the Binary class into it so it would be able to change the[/color][/color]
bit[color=blue][color=green]
> > if a bit was set. Not a good approach. Do you have any ideas?
> >
> >
> > Thanks for your help,
> > Shawn
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > Hi Shawn,
> > >
> > > I've looked at it, and no optimization suggestions other than
> > > some obvious simple ones:
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > uint val = (uint)Value;
> > > byte shift = (byte)(index - 1);
> > > if (index == 0)
> > > return (int)(val & 0x01);
> > > else
> > > return (int)(val >> shift) & 0x01;
> > > }
> > >
> > > This is very verbose, and you're creating lots of temp variables
> > > I'd rather write something like (untested):
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > return (int)((Value >> index-1) & 0x01;
> > > }
> > >
> > > The check to avoid a shift probably just hurts performance anyway.
> > > Also for SetBit, the two if else must result in unwanted branches:
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > byte shift = index;
> > > if (index == 0) {
> > > if (value == 0)
> > > Value &= unchecked((ushort)~0x01);
> > > else
> > > Value |= 0x01;
> > > }
> > > else {
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << shift));
> > > else
> > > Value |= (ushort)(0x01 << shift);
> > > }
> > > }
> > >
> > > How about this instead (also untested):
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << index));
> > > else
> > > Value |= (ushort)(0x01 << index);
> > > }
> > >
> > > This is the only type of optimizations I came up with, so I thought it[/color]
> > wasn't worth posting. :)[color=darkred]
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Well, I've posted it twice and no one is jumpting to optimize it...[/color]
> > perhaps[color=darkred]
> > > > it's just so well working that there's no need? he he
> > > >
> > > >
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > > Do post it here!
> > > > > I'd be interested to see, as I'm sure many others would.
> > > > > You'll probably get a ton of optimization hints from it. :)
> > > > >
> > > > >
> > > > > --
> > > > > Robert Jeppesen
> > > > > robert.jeppesen%at%durius-dot-se
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > > Don't miss a thing, d'ya? =))
> > > > > >
> > > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit[/color][/color][/color]
65c02,[color=blue][color=green]
> > and[color=darkred]
> > > > the
> > > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I[/color][/color][/color]
have[color=blue][color=green]
> > the[color=darkred]
> > > > > > ability to modify the assembly language while the debugger is[/color]
> > running[color=darkred]
> > > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > > instructions. I'm not designing it to be a particular piece of[/color]
> > hardware[color=darkred]
> > > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color]
> > plugins so[color=darkred]
> > > > my
> > > > > > plugins can treat an area of memory as a dedicated "something"[/color][/color][/color]
such[color=blue][color=green]
> > as[color=darkred]
> > > > text
> > > > > > display or graphics display, etc. For now, I'm concerned not[/color][/color][/color]
even[color=blue][color=green]
> > with[color=darkred]
> > > > the
> > > > > > text display. My end result, is actually more akin to being[/color][/color][/color]
able to[color=blue][color=green][color=darkred]
> > > > debug
> > > > > > my NES games I've been into writing lately. Also I want to be[/color][/color][/color]
ready[color=blue][color=green][color=darkred]
> > > > with my
> > > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color]
> > (don't[color=darkred]
> > > > know
> > > > > > whether he'll have a debugger or not) but I want a debugger that[/color]
> > allows[color=darkred]
> > > > me
> > > > > > to change the code while it's being debugged and my simulator[/color][/color][/color]
allows[color=blue][color=green]
> > for[color=darkred]
> > > > it
> > > > > > (as long as the source code file is available -- else if it was[/color]
> > loading[color=darkred]
> > > > as a
> > > > > > .bin file then it won't be editable during a debug session).
> > > > > >
> > > > > > So it's really more than a simulator. It's an integrated
> > > > (non-seperable)
> > > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind[/color][/color][/color]
of a[color=blue][color=green][color=darkred]
> > > > beast.
> > > > > > I chose C# because it's not the kind of thing I'd want to do in[/color]
> > VB.NET[color=darkred]
> > > > (I'm
> > > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color]
> > Managed[color=darkred]
> > > > C++
> > > > > > but also wanted it to target the .NET framework (as much as is
> > > > reasonable
> > > > > > and I'll find out what that really means when its mature enough[/color][/color][/color]
to[color=blue][color=green]
> > start[color=darkred]
> > > > > > using reliably).
> > > > > >
> > > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz[/color][/color][/color]
CPU[color=blue][color=green]
> > with[color=darkred]
> > > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color]
> > emulating[color=darkred]
> > > > the
> > > > > > 16-bit processor as I haven't deciphered the specs for the CPU[/color][/color][/color]
yet[color=blue][color=green]
> > (the[color=darkred]
> > > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color]
> > processor).[color=darkred]
> > > > I
> > > > > > ultimately will make this Interfacable so I can write a Z80[/color][/color][/color]
core, as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > >
> > > > > > These bit manipulations are the utmost inner-loop so I have to[/color][/color][/color]
make[color=blue][color=green]
> > them[color=darkred]
> > > > > > tight. Looking at the JIT'd code for these methods seems to be[/color]
> > decent[color=darkred]
> > > > but I
> > > > > > have to make too many function calls (read, jumps) for my tastes[/color][/color][/color]
but[color=blue][color=green][color=darkred]
> > > > doesn't
> > > > > > seem to be hurting me at the moment). Since I can't inline in[/color][/color][/color]
..NET,[color=blue][color=green][color=darkred]
> > > > I'll
> > > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it[/color][/color][/color]
can't[color=blue][color=green][color=darkred]
> > > > > > generate the best code for the rotates and shifts because I have[/color][/color][/color]
to[color=blue][color=green][color=darkred]
> > > > emulate
> > > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work[/color][/color][/color]
out a[color=blue][color=green][color=darkred]
> > > > better
> > > > > > way for simulating memory manipulation. Now I'm creating a[/color][/color][/color]
string[color=blue][color=green]
> > of[color=darkred]
> > > > (64kb
> > > > > > in length) but manipulating each byte by substrings has got to[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> > > > expensive.
> > > > > > I'm looking into whether a byte array would be better.
> > > > > >
> > > > > >
> > > > > > Thanks for the help,
> > > > > > if interested, I'll post my binary class here for all to see.
> > > > > >
> > > > > > I've completed the Rotates, Shift, converting numerics into[/color][/color][/color]
binary[color=blue][color=green][color=darkred]
> > > > (string
> > > > > > of 1's and 0's). I know now have to read the binary back to a[/color]
> > numeric,[color=darkred]
> > > > and
> > > > > > allow a way to get/set/toggle individual bits.
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in[/color][/color][/color]
message[color=blue][color=green][color=darkred]
> > > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > > So, you're writing an emulator for which processor??
> > > > > > >
> > > > > > > --
> > > > > > > Bob Powell [MVP]
> > > > > > > C#, System.Drawing
> > > > > > >
> > > > > > > September's edition of Well Formed is now available.
> > > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > > >
> > > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > > Greetings,
> > > > > > > >
> > > > > > > > I am simulating an assembly language bit rotation in C# and[/color][/color][/color]
it[color=blue][color=green]
> > works[color=darkred]
> > > > > > > > wonderfully
> > > > > > > >
> > > > > > > > ---------
> > > > > > > > ...
> > > > > > > > public uint Value;
> > > > > > > > ...
> > > > > > > > public uint RotateRight(byte count) {
> > > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > > return Value;
> > > > > > > > }
> > > > > > > > ---------
> > > > > > > >
> > > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color]
> > limitation of[color=darkred]
> > > > the
> > > > > > > > shift operator). I need it to work on an 8-bit or 16-bit[/color][/color][/color]
value,[color=blue][color=green]
> > as[color=darkred]
> > > > > > well.
> > > > > > > I
> > > > > > > > presume I'd have to have knowledge of the intended size of[/color][/color][/color]
the[color=blue][color=green]
> > value[color=darkred]
> > > > and
> > > > > > > do
> > > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color]
> > position[color=darkred]
> > > > and
> > > > > > > clear
> > > > > > > > the extra bits if there are any. That seems like a lot of[/color][/color][/color]
work.[color=blue][color=green][color=darkred]
> > > > > > > >
> > > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a[/color][/color][/color]
32-bit[color=blue][color=green]
> > or[color=darkred]
> > > > 64-bit
> > > > > > > > value?
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks,
> > > > > > > > Shawn
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#28: Nov 15 '05

re: bitwise rotation


Already done.


Thanks,
Shawn


"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:eBpEUPwkDHA.1656@tk2msftngp13.phx.gbl...[color=blue]
> Removing the if check in SetBit:
> Value = (ushort)((~(0x01 << index))&Value|(0x01 << index));
>
> As always, untested. :)
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=blue][color=green]
> > Robert,
> >
> > You're first suggestion crossed my mind, I have already started[/color][/color]
eliminating[color=blue][color=green]
> > the temp variables thus far (thus far only in the 8-bit operator[/color][/color]
overloads[color=blue][color=green]
> > region area). Just be happy you didn't see the earlier version I first
> > posted, it didn't cast because I kept getting compile errors, it ran
> > everything through a Convert.ToUIntX() call but I've since removed[/color][/color]
those...[color=blue][color=green]
> >
> > I also plan to move the Shift function logic into the overload to avoid[/color][/color]
the[color=blue][color=green]
> > extra function call.
> >
> > The GetBit optimization hint you provided is what I've been wanting to[/color][/color]
do[color=blue][color=green]
> > but didn't quite put it they way you did... I'll try that and see how it
> > works. I don't know what to do about that check in it, though. I feel[/color][/color]
like[color=blue][color=green]
> > it's necessary. Perhaps I could just make the if based on some[/color][/color]
calculation[color=blue][color=green]
> > and then just throw an exception at the end of the function and avoid[/color][/color]
that[color=blue][color=green]
> > early check, perhaps that would be a better approach. Such as (if[/color][/color]
(Value ==[color=blue][color=green]
> > 0 && Value < SIZE-1) {)
> >
> > There is one thing I need help on.
> >
> >
> > I don't like the GetBit, SetBit approach. I would rather be able to say[/color][/color]
x =[color=blue][color=green]
> > BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
> > indexer but it had problems. Not the least of which was a circular
> > reference that ended up being a memory leak, not what I wanted in the
> > innermost loop of an emulator. The second being that I could easily say[/color][/color]
x =[color=blue][color=green]
> > x.Bit[0] but not easily say x.Bit[0] = 1.
> >
> > Do you have any ideas how I might approach this? I was using a property
> > get/set at first but that didn't work, instead I just created a public
> > module variable "public Bit Bit;" and then it had a constructor to pass[/color][/color]
a[color=blue][color=green]
> > reference of the Binary class into it so it would be able to change the[/color][/color]
bit[color=blue][color=green]
> > if a bit was set. Not a good approach. Do you have any ideas?
> >
> >
> > Thanks for your help,
> > Shawn
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > Hi Shawn,
> > >
> > > I've looked at it, and no optimization suggestions other than
> > > some obvious simple ones:
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > uint val = (uint)Value;
> > > byte shift = (byte)(index - 1);
> > > if (index == 0)
> > > return (int)(val & 0x01);
> > > else
> > > return (int)(val >> shift) & 0x01;
> > > }
> > >
> > > This is very verbose, and you're creating lots of temp variables
> > > I'd rather write something like (untested):
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > return (int)((Value >> index-1) & 0x01;
> > > }
> > >
> > > The check to avoid a shift probably just hurts performance anyway.
> > > Also for SetBit, the two if else must result in unwanted branches:
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > byte shift = index;
> > > if (index == 0) {
> > > if (value == 0)
> > > Value &= unchecked((ushort)~0x01);
> > > else
> > > Value |= 0x01;
> > > }
> > > else {
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << shift));
> > > else
> > > Value |= (ushort)(0x01 << shift);
> > > }
> > > }
> > >
> > > How about this instead (also untested):
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << index));
> > > else
> > > Value |= (ushort)(0x01 << index);
> > > }
> > >
> > > This is the only type of optimizations I came up with, so I thought it[/color]
> > wasn't worth posting. :)[color=darkred]
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Well, I've posted it twice and no one is jumpting to optimize it...[/color]
> > perhaps[color=darkred]
> > > > it's just so well working that there's no need? he he
> > > >
> > > >
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > > Do post it here!
> > > > > I'd be interested to see, as I'm sure many others would.
> > > > > You'll probably get a ton of optimization hints from it. :)
> > > > >
> > > > >
> > > > > --
> > > > > Robert Jeppesen
> > > > > robert.jeppesen%at%durius-dot-se
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > > Don't miss a thing, d'ya? =))
> > > > > >
> > > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit[/color][/color][/color]
65c02,[color=blue][color=green]
> > and[color=darkred]
> > > > the
> > > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I[/color][/color][/color]
have[color=blue][color=green]
> > the[color=darkred]
> > > > > > ability to modify the assembly language while the debugger is[/color]
> > running[color=darkred]
> > > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > > instructions. I'm not designing it to be a particular piece of[/color]
> > hardware[color=darkred]
> > > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color]
> > plugins so[color=darkred]
> > > > my
> > > > > > plugins can treat an area of memory as a dedicated "something"[/color][/color][/color]
such[color=blue][color=green]
> > as[color=darkred]
> > > > text
> > > > > > display or graphics display, etc. For now, I'm concerned not[/color][/color][/color]
even[color=blue][color=green]
> > with[color=darkred]
> > > > the
> > > > > > text display. My end result, is actually more akin to being[/color][/color][/color]
able to[color=blue][color=green][color=darkred]
> > > > debug
> > > > > > my NES games I've been into writing lately. Also I want to be[/color][/color][/color]
ready[color=blue][color=green][color=darkred]
> > > > with my
> > > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color]
> > (don't[color=darkred]
> > > > know
> > > > > > whether he'll have a debugger or not) but I want a debugger that[/color]
> > allows[color=darkred]
> > > > me
> > > > > > to change the code while it's being debugged and my simulator[/color][/color][/color]
allows[color=blue][color=green]
> > for[color=darkred]
> > > > it
> > > > > > (as long as the source code file is available -- else if it was[/color]
> > loading[color=darkred]
> > > > as a
> > > > > > .bin file then it won't be editable during a debug session).
> > > > > >
> > > > > > So it's really more than a simulator. It's an integrated
> > > > (non-seperable)
> > > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind[/color][/color][/color]
of a[color=blue][color=green][color=darkred]
> > > > beast.
> > > > > > I chose C# because it's not the kind of thing I'd want to do in[/color]
> > VB.NET[color=darkred]
> > > > (I'm
> > > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color]
> > Managed[color=darkred]
> > > > C++
> > > > > > but also wanted it to target the .NET framework (as much as is
> > > > reasonable
> > > > > > and I'll find out what that really means when its mature enough[/color][/color][/color]
to[color=blue][color=green]
> > start[color=darkred]
> > > > > > using reliably).
> > > > > >
> > > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz[/color][/color][/color]
CPU[color=blue][color=green]
> > with[color=darkred]
> > > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color]
> > emulating[color=darkred]
> > > > the
> > > > > > 16-bit processor as I haven't deciphered the specs for the CPU[/color][/color][/color]
yet[color=blue][color=green]
> > (the[color=darkred]
> > > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color]
> > processor).[color=darkred]
> > > > I
> > > > > > ultimately will make this Interfacable so I can write a Z80[/color][/color][/color]
core, as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > >
> > > > > > These bit manipulations are the utmost inner-loop so I have to[/color][/color][/color]
make[color=blue][color=green]
> > them[color=darkred]
> > > > > > tight. Looking at the JIT'd code for these methods seems to be[/color]
> > decent[color=darkred]
> > > > but I
> > > > > > have to make too many function calls (read, jumps) for my tastes[/color][/color][/color]
but[color=blue][color=green][color=darkred]
> > > > doesn't
> > > > > > seem to be hurting me at the moment). Since I can't inline in[/color][/color][/color]
..NET,[color=blue][color=green][color=darkred]
> > > > I'll
> > > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it[/color][/color][/color]
can't[color=blue][color=green][color=darkred]
> > > > > > generate the best code for the rotates and shifts because I have[/color][/color][/color]
to[color=blue][color=green][color=darkred]
> > > > emulate
> > > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work[/color][/color][/color]
out a[color=blue][color=green][color=darkred]
> > > > better
> > > > > > way for simulating memory manipulation. Now I'm creating a[/color][/color][/color]
string[color=blue][color=green]
> > of[color=darkred]
> > > > (64kb
> > > > > > in length) but manipulating each byte by substrings has got to[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> > > > expensive.
> > > > > > I'm looking into whether a byte array would be better.
> > > > > >
> > > > > >
> > > > > > Thanks for the help,
> > > > > > if interested, I'll post my binary class here for all to see.
> > > > > >
> > > > > > I've completed the Rotates, Shift, converting numerics into[/color][/color][/color]
binary[color=blue][color=green][color=darkred]
> > > > (string
> > > > > > of 1's and 0's). I know now have to read the binary back to a[/color]
> > numeric,[color=darkred]
> > > > and
> > > > > > allow a way to get/set/toggle individual bits.
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in[/color][/color][/color]
message[color=blue][color=green][color=darkred]
> > > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > > So, you're writing an emulator for which processor??
> > > > > > >
> > > > > > > --
> > > > > > > Bob Powell [MVP]
> > > > > > > C#, System.Drawing
> > > > > > >
> > > > > > > September's edition of Well Formed is now available.
> > > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > > >
> > > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > > Greetings,
> > > > > > > >
> > > > > > > > I am simulating an assembly language bit rotation in C# and[/color][/color][/color]
it[color=blue][color=green]
> > works[color=darkred]
> > > > > > > > wonderfully
> > > > > > > >
> > > > > > > > ---------
> > > > > > > > ...
> > > > > > > > public uint Value;
> > > > > > > > ...
> > > > > > > > public uint RotateRight(byte count) {
> > > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > > return Value;
> > > > > > > > }
> > > > > > > > ---------
> > > > > > > >
> > > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color]
> > limitation of[color=darkred]
> > > > the
> > > > > > > > shift operator). I need it to work on an 8-bit or 16-bit[/color][/color][/color]
value,[color=blue][color=green]
> > as[color=darkred]
> > > > > > well.
> > > > > > > I
> > > > > > > > presume I'd have to have knowledge of the intended size of[/color][/color][/color]
the[color=blue][color=green]
> > value[color=darkred]
> > > > and
> > > > > > > do
> > > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color]
> > position[color=darkred]
> > > > and
> > > > > > > clear
> > > > > > > > the extra bits if there are any. That seems like a lot of[/color][/color][/color]
work.[color=blue][color=green][color=darkred]
> > > > > > > >
> > > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a[/color][/color][/color]
32-bit[color=blue][color=green]
> > or[color=darkred]
> > > > 64-bit
> > > > > > > > value?
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks,
> > > > > > > > Shawn
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#29: Nov 15 '05

re: bitwise rotation


I was thinking about that last night but forgot all about it. Now, I have
once again optimized that method and made it exactly what I always wanted it
to be. It will not check to see if the length of the string exceeds the
number of bits it can contain. Instead, it checks that all characters are
1's and 0' and if a 1 bit exceeds SIZE then it returns false. You can pass
a 100 "0"'s but if bit 9 is a 1, then it will return false because it can't
be an 8-bit value. This is in line with the rest of the object that will
allow me to assign a Binary64 to a Binary8 as long as the number is at most
an 8-bit value.

The execption handler is gone. Thanks for the suggestion.



Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:uU9hA%23vkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=blue]
> Another suggestion..
> You have a method called IsBinary to determine whether a string is valid[/color]
and can be parsed.[color=blue]
> In the framework, methods called IsXXX and return a bool usually won't[/color]
throw an exception (which you method does).[color=blue]
> The purpose of these methods (as far as I know) is to avoid catching[/color]
errors by Exception handling, cause it incurrs such a[color=blue]
> performance penalty. Remove the exception from this method and return[/color]
false if the string is too long to be compliant.[color=blue]
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=blue][color=green]
> > Robert,
> >
> > You're first suggestion crossed my mind, I have already started[/color][/color]
eliminating[color=blue][color=green]
> > the temp variables thus far (thus far only in the 8-bit operator[/color][/color]
overloads[color=blue][color=green]
> > region area). Just be happy you didn't see the earlier version I first
> > posted, it didn't cast because I kept getting compile errors, it ran
> > everything through a Convert.ToUIntX() call but I've since removed[/color][/color]
those...[color=blue][color=green]
> >
> > I also plan to move the Shift function logic into the overload to avoid[/color][/color]
the[color=blue][color=green]
> > extra function call.
> >
> > The GetBit optimization hint you provided is what I've been wanting to[/color][/color]
do[color=blue][color=green]
> > but didn't quite put it they way you did... I'll try that and see how it
> > works. I don't know what to do about that check in it, though. I feel[/color][/color]
like[color=blue][color=green]
> > it's necessary. Perhaps I could just make the if based on some[/color][/color]
calculation[color=blue][color=green]
> > and then just throw an exception at the end of the function and avoid[/color][/color]
that[color=blue][color=green]
> > early check, perhaps that would be a better approach. Such as (if[/color][/color]
(Value ==[color=blue][color=green]
> > 0 && Value < SIZE-1) {)
> >
> > There is one thing I need help on.
> >
> >
> > I don't like the GetBit, SetBit approach. I would rather be able to say[/color][/color]
x =[color=blue][color=green]
> > BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
> > indexer but it had problems. Not the least of which was a circular
> > reference that ended up being a memory leak, not what I wanted in the
> > innermost loop of an emulator. The second being that I could easily say[/color][/color]
x =[color=blue][color=green]
> > x.Bit[0] but not easily say x.Bit[0] = 1.
> >
> > Do you have any ideas how I might approach this? I was using a property
> > get/set at first but that didn't work, instead I just created a public
> > module variable "public Bit Bit;" and then it had a constructor to pass[/color][/color]
a[color=blue][color=green]
> > reference of the Binary class into it so it would be able to change the[/color][/color]
bit[color=blue][color=green]
> > if a bit was set. Not a good approach. Do you have any ideas?
> >
> >
> > Thanks for your help,
> > Shawn
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > Hi Shawn,
> > >
> > > I've looked at it, and no optimization suggestions other than
> > > some obvious simple ones:
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > uint val = (uint)Value;
> > > byte shift = (byte)(index - 1);
> > > if (index == 0)
> > > return (int)(val & 0x01);
> > > else
> > > return (int)(val >> shift) & 0x01;
> > > }
> > >
> > > This is very verbose, and you're creating lots of temp variables
> > > I'd rather write something like (untested):
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > return (int)((Value >> index-1) & 0x01;
> > > }
> > >
> > > The check to avoid a shift probably just hurts performance anyway.
> > > Also for SetBit, the two if else must result in unwanted branches:
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > byte shift = index;
> > > if (index == 0) {
> > > if (value == 0)
> > > Value &= unchecked((ushort)~0x01);
> > > else
> > > Value |= 0x01;
> > > }
> > > else {
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << shift));
> > > else
> > > Value |= (ushort)(0x01 << shift);
> > > }
> > > }
> > >
> > > How about this instead (also untested):
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << index));
> > > else
> > > Value |= (ushort)(0x01 << index);
> > > }
> > >
> > > This is the only type of optimizations I came up with, so I thought it[/color]
> > wasn't worth posting. :)[color=darkred]
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Well, I've posted it twice and no one is jumpting to optimize it...[/color]
> > perhaps[color=darkred]
> > > > it's just so well working that there's no need? he he
> > > >
> > > >
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > > Do post it here!
> > > > > I'd be interested to see, as I'm sure many others would.
> > > > > You'll probably get a ton of optimization hints from it. :)
> > > > >
> > > > >
> > > > > --
> > > > > Robert Jeppesen
> > > > > robert.jeppesen%at%durius-dot-se
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > > Don't miss a thing, d'ya? =))
> > > > > >
> > > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit[/color][/color][/color]
65c02,[color=blue][color=green]
> > and[color=darkred]
> > > > the
> > > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I[/color][/color][/color]
have[color=blue][color=green]
> > the[color=darkred]
> > > > > > ability to modify the assembly language while the debugger is[/color]
> > running[color=darkred]
> > > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > > instructions. I'm not designing it to be a particular piece of[/color]
> > hardware[color=darkred]
> > > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color]
> > plugins so[color=darkred]
> > > > my
> > > > > > plugins can treat an area of memory as a dedicated "something"[/color][/color][/color]
such[color=blue][color=green]
> > as[color=darkred]
> > > > text
> > > > > > display or graphics display, etc. For now, I'm concerned not[/color][/color][/color]
even[color=blue][color=green]
> > with[color=darkred]
> > > > the
> > > > > > text display. My end result, is actually more akin to being[/color][/color][/color]
able to[color=blue][color=green][color=darkred]
> > > > debug
> > > > > > my NES games I've been into writing lately. Also I want to be[/color][/color][/color]
ready[color=blue][color=green][color=darkred]
> > > > with my
> > > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color]
> > (don't[color=darkred]
> > > > know
> > > > > > whether he'll have a debugger or not) but I want a debugger that[/color]
> > allows[color=darkred]
> > > > me
> > > > > > to change the code while it's being debugged and my simulator[/color][/color][/color]
allows[color=blue][color=green]
> > for[color=darkred]
> > > > it
> > > > > > (as long as the source code file is available -- else if it was[/color]
> > loading[color=darkred]
> > > > as a
> > > > > > .bin file then it won't be editable during a debug session).
> > > > > >
> > > > > > So it's really more than a simulator. It's an integrated
> > > > (non-seperable)
> > > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind[/color][/color][/color]
of a[color=blue][color=green][color=darkred]
> > > > beast.
> > > > > > I chose C# because it's not the kind of thing I'd want to do in[/color]
> > VB.NET[color=darkred]
> > > > (I'm
> > > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color]
> > Managed[color=darkred]
> > > > C++
> > > > > > but also wanted it to target the .NET framework (as much as is
> > > > reasonable
> > > > > > and I'll find out what that really means when its mature enough[/color][/color][/color]
to[color=blue][color=green]
> > start[color=darkred]
> > > > > > using reliably).
> > > > > >
> > > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz[/color][/color][/color]
CPU[color=blue][color=green]
> > with[color=darkred]
> > > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color]
> > emulating[color=darkred]
> > > > the
> > > > > > 16-bit processor as I haven't deciphered the specs for the CPU[/color][/color][/color]
yet[color=blue][color=green]
> > (the[color=darkred]
> > > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color]
> > processor).[color=darkred]
> > > > I
> > > > > > ultimately will make this Interfacable so I can write a Z80[/color][/color][/color]
core, as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > >
> > > > > > These bit manipulations are the utmost inner-loop so I have to[/color][/color][/color]
make[color=blue][color=green]
> > them[color=darkred]
> > > > > > tight. Looking at the JIT'd code for these methods seems to be[/color]
> > decent[color=darkred]
> > > > but I
> > > > > > have to make too many function calls (read, jumps) for my tastes[/color][/color][/color]
but[color=blue][color=green][color=darkred]
> > > > doesn't
> > > > > > seem to be hurting me at the moment). Since I can't inline in[/color][/color][/color]
..NET,[color=blue][color=green][color=darkred]
> > > > I'll
> > > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it[/color][/color][/color]
can't[color=blue][color=green][color=darkred]
> > > > > > generate the best code for the rotates and shifts because I have[/color][/color][/color]
to[color=blue][color=green][color=darkred]
> > > > emulate
> > > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work[/color][/color][/color]
out a[color=blue][color=green][color=darkred]
> > > > better
> > > > > > way for simulating memory manipulation. Now I'm creating a[/color][/color][/color]
string[color=blue][color=green]
> > of[color=darkred]
> > > > (64kb
> > > > > > in length) but manipulating each byte by substrings has got to[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> > > > expensive.
> > > > > > I'm looking into whether a byte array would be better.
> > > > > >
> > > > > >
> > > > > > Thanks for the help,
> > > > > > if interested, I'll post my binary class here for all to see.
> > > > > >
> > > > > > I've completed the Rotates, Shift, converting numerics into[/color][/color][/color]
binary[color=blue][color=green][color=darkred]
> > > > (string
> > > > > > of 1's and 0's). I know now have to read the binary back to a[/color]
> > numeric,[color=darkred]
> > > > and
> > > > > > allow a way to get/set/toggle individual bits.
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in[/color][/color][/color]
message[color=blue][color=green][color=darkred]
> > > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > > So, you're writing an emulator for which processor??
> > > > > > >
> > > > > > > --
> > > > > > > Bob Powell [MVP]
> > > > > > > C#, System.Drawing
> > > > > > >
> > > > > > > September's edition of Well Formed is now available.
> > > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > > >
> > > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > > Greetings,
> > > > > > > >
> > > > > > > > I am simulating an assembly language bit rotation in C# and[/color][/color][/color]
it[color=blue][color=green]
> > works[color=darkred]
> > > > > > > > wonderfully
> > > > > > > >
> > > > > > > > ---------
> > > > > > > > ...
> > > > > > > > public uint Value;
> > > > > > > > ...
> > > > > > > > public uint RotateRight(byte count) {
> > > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > > return Value;
> > > > > > > > }
> > > > > > > > ---------
> > > > > > > >
> > > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color]
> > limitation of[color=darkred]
> > > > the
> > > > > > > > shift operator). I need it to work on an 8-bit or 16-bit[/color][/color][/color]
value,[color=blue][color=green]
> > as[color=darkred]
> > > > > > well.
> > > > > > > I
> > > > > > > > presume I'd have to have knowledge of the intended size of[/color][/color][/color]
the[color=blue][color=green]
> > value[color=darkred]
> > > > and
> > > > > > > do
> > > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color]
> > position[color=darkred]
> > > > and
> > > > > > > clear
> > > > > > > > the extra bits if there are any. That seems like a lot of[/color][/color][/color]
work.[color=blue][color=green][color=darkred]
> > > > > > > >
> > > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a[/color][/color][/color]
32-bit[color=blue][color=green]
> > or[color=darkred]
> > > > 64-bit
> > > > > > > > value?
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks,
> > > > > > > > Shawn
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#30: Nov 15 '05

re: bitwise rotation


There is now yet a newer version to check as of the time of this post.


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:uhD3H4vkDHA.3312@tk2msftngp13.phx.gbl...[color=blue]
> Yes, it's better to just return the value in those cases.
> I'm gonna go check the new code now. :)
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:u9hmviukDHA.2772@TK2MSFTNGP12.phx.gbl...[color=blue][color=green]
> > Robert,
> >
> > I updated the objects with the opimizations you suggested and a few[/color][/color]
others I[color=blue][color=green]
> > thought were neccessary. I also added an "And" and "Or" method to the
> > classes. I changed the Rotate and Shift methods to not set Value and[/color][/color]
then[color=blue][color=green]
> > return Value. Now they only return it. The problem was if I said "bin2[/color][/color]
=[color=blue][color=green]
> > bin.RotateLeft(2);" both bin2 and bin would end up with a new value. I
> > originally did that because before I was overloading the operators I was
> > actually just saying "bin.RotateLeft(2);" then later decided to make it
> > return the new value, as well. Now it is not necessary with all the
> > overloads. Not very consistant with the rest of the .NET framework so I
> > corrected it. I put a special thanks in at the end of the ReadMe.
> >
> > Okay, now, let me know if you have any other ideas or optimization tips[/color][/color]
=)).[color=blue][color=green]
> >
> >
> > Thanks,
> > Shawn
> >
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > Hi Shawn,
> > >
> > > I've looked at it, and no optimization suggestions other than
> > > some obvious simple ones:
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > uint val = (uint)Value;
> > > byte shift = (byte)(index - 1);
> > > if (index == 0)
> > > return (int)(val & 0x01);
> > > else
> > > return (int)(val >> shift) & 0x01;
> > > }
> > >
> > > This is very verbose, and you're creating lots of temp variables
> > > I'd rather write something like (untested):
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > return (int)((Value >> index-1) & 0x01;
> > > }
> > >
> > > The check to avoid a shift probably just hurts performance anyway.
> > > Also for SetBit, the two if else must result in unwanted branches:
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > byte shift = index;
> > > if (index == 0) {
> > > if (value == 0)
> > > Value &= unchecked((ushort)~0x01);
> > > else
> > > Value |= 0x01;
> > > }
> > > else {
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << shift));
> > > else
> > > Value |= (ushort)(0x01 << shift);
> > > }
> > > }
> > >
> > > How about this instead (also untested):
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << index));
> > > else
> > > Value |= (ushort)(0x01 << index);
> > > }
> > >
> > > This is the only type of optimizations I came up with, so I thought it[/color]
> > wasn't worth posting. :)[color=darkred]
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Well, I've posted it twice and no one is jumpting to optimize it...[/color]
> > perhaps[color=darkred]
> > > > it's just so well working that there's no need? he he
> > > >
> > > >
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > > Do post it here!
> > > > > I'd be interested to see, as I'm sure many others would.
> > > > > You'll probably get a ton of optimization hints from it. :)
> > > > >
> > > > >
> > > > > --
> > > > > Robert Jeppesen
> > > > > robert.jeppesen%at%durius-dot-se
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > > Don't miss a thing, d'ya? =))
> > > > > >
> > > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit[/color][/color][/color]
65c02,[color=blue][color=green]
> > and[color=darkred]
> > > > the
> > > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I[/color][/color][/color]
have[color=blue][color=green]
> > the[color=darkred]
> > > > > > ability to modify the assembly language while the debugger is[/color]
> > running[color=darkred]
> > > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > > instructions. I'm not designing it to be a particular piece of[/color]
> > hardware[color=darkred]
> > > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color]
> > plugins so[color=darkred]
> > > > my
> > > > > > plugins can treat an area of memory as a dedicated "something"[/color][/color][/color]
such[color=blue][color=green]
> > as[color=darkred]
> > > > text
> > > > > > display or graphics display, etc. For now, I'm concerned not[/color][/color][/color]
even[color=blue][color=green]
> > with[color=darkred]
> > > > the
> > > > > > text display. My end result, is actually more akin to being[/color][/color][/color]
able to[color=blue][color=green][color=darkred]
> > > > debug
> > > > > > my NES games I've been into writing lately. Also I want to be[/color][/color][/color]
ready[color=blue][color=green][color=darkred]
> > > > with my
> > > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color]
> > (don't[color=darkred]
> > > > know
> > > > > > whether he'll have a debugger or not) but I want a debugger that[/color]
> > allows[color=darkred]
> > > > me
> > > > > > to change the code while it's being debugged and my simulator[/color][/color][/color]
allows[color=blue][color=green]
> > for[color=darkred]
> > > > it
> > > > > > (as long as the source code file is available -- else if it was[/color]
> > loading[color=darkred]
> > > > as a
> > > > > > .bin file then it won't be editable during a debug session).
> > > > > >
> > > > > > So it's really more than a simulator. It's an integrated
> > > > (non-seperable)
> > > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind[/color][/color][/color]
of a[color=blue][color=green][color=darkred]
> > > > beast.
> > > > > > I chose C# because it's not the kind of thing I'd want to do in[/color]
> > VB.NET[color=darkred]
> > > > (I'm
> > > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color]
> > Managed[color=darkred]
> > > > C++
> > > > > > but also wanted it to target the .NET framework (as much as is
> > > > reasonable
> > > > > > and I'll find out what that really means when its mature enough[/color][/color][/color]
to[color=blue][color=green]
> > start[color=darkred]
> > > > > > using reliably).
> > > > > >
> > > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz[/color][/color][/color]
CPU[color=blue][color=green]
> > with[color=darkred]
> > > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color]
> > emulating[color=darkred]
> > > > the
> > > > > > 16-bit processor as I haven't deciphered the specs for the CPU[/color][/color][/color]
yet[color=blue][color=green]
> > (the[color=darkred]
> > > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color]
> > processor).[color=darkred]
> > > > I
> > > > > > ultimately will make this Interfacable so I can write a Z80[/color][/color][/color]
core, as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > >
> > > > > > These bit manipulations are the utmost inner-loop so I have to[/color][/color][/color]
make[color=blue][color=green]
> > them[color=darkred]
> > > > > > tight. Looking at the JIT'd code for these methods seems to be[/color]
> > decent[color=darkred]
> > > > but I
> > > > > > have to make too many function calls (read, jumps) for my tastes[/color][/color][/color]
but[color=blue][color=green][color=darkred]
> > > > doesn't
> > > > > > seem to be hurting me at the moment). Since I can't inline in[/color][/color][/color]
..NET,[color=blue][color=green][color=darkred]
> > > > I'll
> > > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it[/color][/color][/color]
can't[color=blue][color=green][color=darkred]
> > > > > > generate the best code for the rotates and shifts because I have[/color][/color][/color]
to[color=blue][color=green][color=darkred]
> > > > emulate
> > > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work[/color][/color][/color]
out a[color=blue][color=green][color=darkred]
> > > > better
> > > > > > way for simulating memory manipulation. Now I'm creating a[/color][/color][/color]
string[color=blue][color=green]
> > of[color=darkred]
> > > > (64kb
> > > > > > in length) but manipulating each byte by substrings has got to[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> > > > expensive.
> > > > > > I'm looking into whether a byte array would be better.
> > > > > >
> > > > > >
> > > > > > Thanks for the help,
> > > > > > if interested, I'll post my binary class here for all to see.
> > > > > >
> > > > > > I've completed the Rotates, Shift, converting numerics into[/color][/color][/color]
binary[color=blue][color=green][color=darkred]
> > > > (string
> > > > > > of 1's and 0's). I know now have to read the binary back to a[/color]
> > numeric,[color=darkred]
> > > > and
> > > > > > allow a way to get/set/toggle individual bits.
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in[/color][/color][/color]
message[color=blue][color=green][color=darkred]
> > > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > > So, you're writing an emulator for which processor??
> > > > > > >
> > > > > > > --
> > > > > > > Bob Powell [MVP]
> > > > > > > C#, System.Drawing
> > > > > > >
> > > > > > > September's edition of Well Formed is now available.
> > > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > > >
> > > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > > Greetings,
> > > > > > > >
> > > > > > > > I am simulating an assembly language bit rotation in C# and[/color][/color][/color]
it[color=blue][color=green]
> > works[color=darkred]
> > > > > > > > wonderfully
> > > > > > > >
> > > > > > > > ---------
> > > > > > > > ...
> > > > > > > > public uint Value;
> > > > > > > > ...
> > > > > > > > public uint RotateRight(byte count) {
> > > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > > return Value;
> > > > > > > > }
> > > > > > > > ---------
> > > > > > > >
> > > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color]
> > limitation of[color=darkred]
> > > > the
> > > > > > > > shift operator). I need it to work on an 8-bit or 16-bit[/color][/color][/color]
value,[color=blue][color=green]
> > as[color=darkred]
> > > > > > well.
> > > > > > > I
> > > > > > > > presume I'd have to have knowledge of the intended size of[/color][/color][/color]
the[color=blue][color=green]
> > value[color=darkred]
> > > > and
> > > > > > > do
> > > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color]
> > position[color=darkred]
> > > > and
> > > > > > > clear
> > > > > > > > the extra bits if there are any. That seems like a lot of[/color][/color][/color]
work.[color=blue][color=green][color=darkred]
> > > > > > > >
> > > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a[/color][/color][/color]
32-bit[color=blue][color=green]
> > or[color=darkred]
> > > > 64-bit
> > > > > > > > value?
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks,
> > > > > > > > Shawn
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#31: Nov 15 '05

re: bitwise rotation


I this has been completed. It works great. I would have never thought of
it in this manner and yet it is so simple, I was hoping more for a Bit type
of syntax but this works great. I didn't get rid of GetBit/SetBit (just
made them private) because the code that they perform doesn't "feel" right
in the indexer code, just looks weird. I'll make it look "right" soon but
for now...


Thanks,
Shawn


"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:usKNb2vkDHA.3612@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hi Shawn,
>
> In GetBit, SetBit or in shift operators, you don't need to check of are[/color]
shifting 0 steps, just do it anyway. It will return the[color=blue]
> correct value.
> Couldn't you have an indexer on the BinaryXX class itself?
> Then you'd say,
> BinaryX[0] = 1;
> BinaryX[2] = BinaryX[0];
>
> For me, that would make more sense than a separate Bit class.
>
> public int this [int index] // Indexer declaration
> {
> get
> {
> //GetBit here
> }
> set
> {
> //SetBit here
> }
> }
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=blue][color=green]
> > Robert,
> >
> > You're first suggestion crossed my mind, I have already started[/color][/color]
eliminating[color=blue][color=green]
> > the temp variables thus far (thus far only in the 8-bit operator[/color][/color]
overloads[color=blue][color=green]
> > region area). Just be happy you didn't see the earlier version I first
> > posted, it didn't cast because I kept getting compile errors, it ran
> > everything through a Convert.ToUIntX() call but I've since removed[/color][/color]
those...[color=blue][color=green]
> >
> > I also plan to move the Shift function logic into the overload to avoid[/color][/color]
the[color=blue][color=green]
> > extra function call.
> >
> > The GetBit optimization hint you provided is what I've been wanting to[/color][/color]
do[color=blue][color=green]
> > but didn't quite put it they way you did... I'll try that and see how it
> > works. I don't know what to do about that check in it, though. I feel[/color][/color]
like[color=blue][color=green]
> > it's necessary. Perhaps I could just make the if based on some[/color][/color]
calculation[color=blue][color=green]
> > and then just throw an exception at the end of the function and avoid[/color][/color]
that[color=blue][color=green]
> > early check, perhaps that would be a better approach. Such as (if[/color][/color]
(Value ==[color=blue][color=green]
> > 0 && Value < SIZE-1) {)
> >
> > There is one thing I need help on.
> >
> >
> > I don't like the GetBit, SetBit approach. I would rather be able to say[/color][/color]
x =[color=blue][color=green]
> > BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
> > indexer but it had problems. Not the least of which was a circular
> > reference that ended up being a memory leak, not what I wanted in the
> > innermost loop of an emulator. The second being that I could easily say[/color][/color]
x =[color=blue][color=green]
> > x.Bit[0] but not easily say x.Bit[0] = 1.
> >
> > Do you have any ideas how I might approach this? I was using a property
> > get/set at first but that didn't work, instead I just created a public
> > module variable "public Bit Bit;" and then it had a constructor to pass[/color][/color]
a[color=blue][color=green]
> > reference of the Binary class into it so it would be able to change the[/color][/color]
bit[color=blue][color=green]
> > if a bit was set. Not a good approach. Do you have any ideas?
> >
> >
> > Thanks for your help,
> > Shawn
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > Hi Shawn,
> > >
> > > I've looked at it, and no optimization suggestions other than
> > > some obvious simple ones:
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > uint val = (uint)Value;
> > > byte shift = (byte)(index - 1);
> > > if (index == 0)
> > > return (int)(val & 0x01);
> > > else
> > > return (int)(val >> shift) & 0x01;
> > > }
> > >
> > > This is very verbose, and you're creating lots of temp variables
> > > I'd rather write something like (untested):
> > >
> > > public int GetBit(byte index) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > return (int)((Value >> index-1) & 0x01;
> > > }
> > >
> > > The check to avoid a shift probably just hurts performance anyway.
> > > Also for SetBit, the two if else must result in unwanted branches:
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > byte shift = index;
> > > if (index == 0) {
> > > if (value == 0)
> > > Value &= unchecked((ushort)~0x01);
> > > else
> > > Value |= 0x01;
> > > }
> > > else {
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << shift));
> > > else
> > > Value |= (ushort)(0x01 << shift);
> > > }
> > > }
> > >
> > > How about this instead (also untested):
> > >
> > > public void SetBit(byte index, int value) {
> > > if (index > (byte)SIZE-1) {
> > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color][/color]
"[color=blue][color=green]
> > does not exist.");[color=darkred]
> > > }
> > > if (value == 0)
> > > Value &= ((ushort)~(0x01 << index));
> > > else
> > > Value |= (ushort)(0x01 << index);
> > > }
> > >
> > > This is the only type of optimizations I came up with, so I thought it[/color]
> > wasn't worth posting. :)[color=darkred]
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...[color=darkred]
> > > > Well, I've posted it twice and no one is jumpting to optimize it...[/color]
> > perhaps[color=darkred]
> > > > it's just so well working that there's no need? he he
> > > >
> > > >
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > > Do post it here!
> > > > > I'd be interested to see, as I'm sure many others would.
> > > > > You'll probably get a ton of optimization hints from it. :)
> > > > >
> > > > >
> > > > > --
> > > > > Robert Jeppesen
> > > > > robert.jeppesen%at%durius-dot-se
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > > Don't miss a thing, d'ya? =))
> > > > > >
> > > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit[/color][/color][/color]
65c02,[color=blue][color=green]
> > and[color=darkred]
> > > > the
> > > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I[/color][/color][/color]
have[color=blue][color=green]
> > the[color=darkred]
> > > > > > ability to modify the assembly language while the debugger is[/color]
> > running[color=darkred]
> > > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > > instructions. I'm not designing it to be a particular piece of[/color]
> > hardware[color=darkred]
> > > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it[/color]
> > plugins so[color=darkred]
> > > > my
> > > > > > plugins can treat an area of memory as a dedicated "something"[/color][/color][/color]
such[color=blue][color=green]
> > as[color=darkred]
> > > > text
> > > > > > display or graphics display, etc. For now, I'm concerned not[/color][/color][/color]
even[color=blue][color=green]
> > with[color=darkred]
> > > > the
> > > > > > text display. My end result, is actually more akin to being[/color][/color][/color]
able to[color=blue][color=green][color=darkred]
> > > > debug
> > > > > > my NES games I've been into writing lately. Also I want to be[/color][/color][/color]
ready[color=blue][color=green][color=darkred]
> > > > with my
> > > > > > own debugger for Andre LaMothe's upcoming XGameStation console[/color]
> > (don't[color=darkred]
> > > > know
> > > > > > whether he'll have a debugger or not) but I want a debugger that[/color]
> > allows[color=darkred]
> > > > me
> > > > > > to change the code while it's being debugged and my simulator[/color][/color][/color]
allows[color=blue][color=green]
> > for[color=darkred]
> > > > it
> > > > > > (as long as the source code file is available -- else if it was[/color]
> > loading[color=darkred]
> > > > as a
> > > > > > .bin file then it won't be editable during a debug session).
> > > > > >
> > > > > > So it's really more than a simulator. It's an integrated
> > > > (non-seperable)
> > > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind[/color][/color][/color]
of a[color=blue][color=green][color=darkred]
> > > > beast.
> > > > > > I chose C# because it's not the kind of thing I'd want to do in[/color]
> > VB.NET[color=darkred]
> > > > (I'm
> > > > > > primarly a VB.NET programmer) but didn't feel like doing it in[/color]
> > Managed[color=darkred]
> > > > C++
> > > > > > but also wanted it to target the .NET framework (as much as is
> > > > reasonable
> > > > > > and I'll find out what that really means when its mature enough[/color][/color][/color]
to[color=blue][color=green]
> > start[color=darkred]
> > > > > > using reliably).
> > > > > >
> > > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz[/color][/color][/color]
CPU[color=blue][color=green]
> > with[color=darkred]
> > > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do[/color]
> > emulating[color=darkred]
> > > > the
> > > > > > 16-bit processor as I haven't deciphered the specs for the CPU[/color][/color][/color]
yet[color=blue][color=green]
> > (the[color=darkred]
> > > > > > 8-bit CPU is very easy compared to the 16-bit version of the[/color]
> > processor).[color=darkred]
> > > > I
> > > > > > ultimately will make this Interfacable so I can write a Z80[/color][/color][/color]
core, as[color=blue][color=green][color=darkred]
> > > > well.
> > > > > >
> > > > > > These bit manipulations are the utmost inner-loop so I have to[/color][/color][/color]
make[color=blue][color=green]
> > them[color=darkred]
> > > > > > tight. Looking at the JIT'd code for these methods seems to be[/color]
> > decent[color=darkred]
> > > > but I
> > > > > > have to make too many function calls (read, jumps) for my tastes[/color][/color][/color]
but[color=blue][color=green][color=darkred]
> > > > doesn't
> > > > > > seem to be hurting me at the moment). Since I can't inline in[/color][/color][/color]
..NET,[color=blue][color=green][color=darkred]
> > > > I'll
> > > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it[/color][/color][/color]
can't[color=blue][color=green][color=darkred]
> > > > > > generate the best code for the rotates and shifts because I have[/color][/color][/color]
to[color=blue][color=green][color=darkred]
> > > > emulate
> > > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work[/color][/color][/color]
out a[color=blue][color=green][color=darkred]
> > > > better
> > > > > > way for simulating memory manipulation. Now I'm creating a[/color][/color][/color]
string[color=blue][color=green]
> > of[color=darkred]
> > > > (64kb
> > > > > > in length) but manipulating each byte by substrings has got to[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> > > > expensive.
> > > > > > I'm looking into whether a byte array would be better.
> > > > > >
> > > > > >
> > > > > > Thanks for the help,
> > > > > > if interested, I'll post my binary class here for all to see.
> > > > > >
> > > > > > I've completed the Rotates, Shift, converting numerics into[/color][/color][/color]
binary[color=blue][color=green][color=darkred]
> > > > (string
> > > > > > of 1's and 0's). I know now have to read the binary back to a[/color]
> > numeric,[color=darkred]
> > > > and
> > > > > > allow a way to get/set/toggle individual bits.
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in[/color][/color][/color]
message[color=blue][color=green][color=darkred]
> > > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > > So, you're writing an emulator for which processor??
> > > > > > >
> > > > > > > --
> > > > > > > Bob Powell [MVP]
> > > > > > > C#, System.Drawing
> > > > > > >
> > > > > > > September's edition of Well Formed is now available.
> > > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > > >
> > > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > > Greetings,
> > > > > > > >
> > > > > > > > I am simulating an assembly language bit rotation in C# and[/color][/color][/color]
it[color=blue][color=green]
> > works[color=darkred]
> > > > > > > > wonderfully
> > > > > > > >
> > > > > > > > ---------
> > > > > > > > ...
> > > > > > > > public uint Value;
> > > > > > > > ...
> > > > > > > > public uint RotateRight(byte count) {
> > > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > > return Value;
> > > > > > > > }
> > > > > > > > ---------
> > > > > > > >
> > > > > > > > as long is the value is 32-bit or 64-bit (because of a[/color]
> > limitation of[color=darkred]
> > > > the
> > > > > > > > shift operator). I need it to work on an 8-bit or 16-bit[/color][/color][/color]
value,[color=blue][color=green]
> > as[color=darkred]
> > > > > > well.
> > > > > > > I
> > > > > > > > presume I'd have to have knowledge of the intended size of[/color][/color][/color]
the[color=blue][color=green]
> > value[color=darkred]
> > > > and
> > > > > > > do
> > > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit[/color]
> > position[color=darkred]
> > > > and
> > > > > > > clear
> > > > > > > > the extra bits if there are any. That seems like a lot of[/color][/color][/color]
work.[color=blue][color=green][color=darkred]
> > > > > > > >
> > > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a[/color][/color][/color]
32-bit[color=blue][color=green]
> > or[color=darkred]
> > > > 64-bit
> > > > > > > > value?
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks,
> > > > > > > > Shawn
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Robert Jeppesen
Guest
 
Posts: n/a
#32: Nov 15 '05

re: bitwise rotation


Hey Shawn,

So now getting or setting bits costs an extra method call? Get used to it and remove the extra method call. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se


"Shawn B." <leabre@html.com> wrote in message news:e28C0mzkDHA.360@TK2MSFTNGP12.phx.gbl...[color=blue]
> I this has been completed. It works great. I would have never thought of
> it in this manner and yet it is so simple, I was hoping more for a Bit type
> of syntax but this works great. I didn't get rid of GetBit/SetBit (just
> made them private) because the code that they perform doesn't "feel" right
> in the indexer code, just looks weird. I'll make it look "right" soon but
> for now...
>
>
> Thanks,
> Shawn
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:usKNb2vkDHA.3612@TK2MSFTNGP11.phx.gbl...[color=green]
> > Hi Shawn,
> >
> > In GetBit, SetBit or in shift operators, you don't need to check of are[/color]
> shifting 0 steps, just do it anyway. It will return the[color=green]
> > correct value.
> > Couldn't you have an indexer on the BinaryXX class itself?
> > Then you'd say,
> > BinaryX[0] = 1;
> > BinaryX[2] = BinaryX[0];
> >
> > For me, that would make more sense than a separate Bit class.
> >
> > public int this [int index] // Indexer declaration
> > {
> > get
> > {
> > //GetBit here
> > }
> > set
> > {
> > //SetBit here
> > }
> > }
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <leabre@html.com> wrote in message[/color]
> news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=green][color=darkred]
> > > Robert,
> > >
> > > You're first suggestion crossed my mind, I have already started[/color][/color]
> eliminating[color=green][color=darkred]
> > > the temp variables thus far (thus far only in the 8-bit operator[/color][/color]
> overloads[color=green][color=darkred]
> > > region area). Just be happy you didn't see the earlier version I first
> > > posted, it didn't cast because I kept getting compile errors, it ran
> > > everything through a Convert.ToUIntX() call but I've since removed[/color][/color]
> those...[color=green][color=darkred]
> > >
> > > I also plan to move the Shift function logic into the overload to avoid[/color][/color]
> the[color=green][color=darkred]
> > > extra function call.
> > >
> > > The GetBit optimization hint you provided is what I've been wanting to[/color][/color]
> do[color=green][color=darkred]
> > > but didn't quite put it they way you did... I'll try that and see how it
> > > works. I don't know what to do about that check in it, though. I feel[/color][/color]
> like[color=green][color=darkred]
> > > it's necessary. Perhaps I could just make the if based on some[/color][/color]
> calculation[color=green][color=darkred]
> > > and then just throw an exception at the end of the function and avoid[/color][/color]
> that[color=green][color=darkred]
> > > early check, perhaps that would be a better approach. Such as (if[/color][/color]
> (Value ==[color=green][color=darkred]
> > > 0 && Value < SIZE-1) {)
> > >
> > > There is one thing I need help on.
> > >
> > >
> > > I don't like the GetBit, SetBit approach. I would rather be able to say[/color][/color]
> x =[color=green][color=darkred]
> > > BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
> > > indexer but it had problems. Not the least of which was a circular
> > > reference that ended up being a memory leak, not what I wanted in the
> > > innermost loop of an emulator. The second being that I could easily say[/color][/color]
> x =[color=green][color=darkred]
> > > x.Bit[0] but not easily say x.Bit[0] = 1.
> > >
> > > Do you have any ideas how I might approach this? I was using a property
> > > get/set at first but that didn't work, instead I just created a public
> > > module variable "public Bit Bit;" and then it had a constructor to pass[/color][/color]
> a[color=green][color=darkred]
> > > reference of the Binary class into it so it would be able to change the[/color][/color]
> bit[color=green][color=darkred]
> > > if a bit was set. Not a good approach. Do you have any ideas?
> > >
> > >
> > > Thanks for your help,
> > > Shawn
> > >
> > >
> > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...
> > > > Hi Shawn,
> > > >
> > > > I've looked at it, and no optimization suggestions other than
> > > > some obvious simple ones:
> > > >
> > > > public int GetBit(byte index) {
> > > > if (index > (byte)SIZE-1) {
> > > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color]
> "[color=green][color=darkred]
> > > does not exist.");
> > > > }
> > > > uint val = (uint)Value;
> > > > byte shift = (byte)(index - 1);
> > > > if (index == 0)
> > > > return (int)(val & 0x01);
> > > > else
> > > > return (int)(val >> shift) & 0x01;
> > > > }
> > > >
> > > > This is very verbose, and you're creating lots of temp variables
> > > > I'd rather write something like (untested):
> > > >
> > > > public int GetBit(byte index) {
> > > > if (index > (byte)SIZE-1) {
> > > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color]
> "[color=green][color=darkred]
> > > does not exist.");
> > > > }
> > > > return (int)((Value >> index-1) & 0x01;
> > > > }
> > > >
> > > > The check to avoid a shift probably just hurts performance anyway.
> > > > Also for SetBit, the two if else must result in unwanted branches:
> > > >
> > > > public void SetBit(byte index, int value) {
> > > > if (index > (byte)SIZE-1) {
> > > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color]
> "[color=green][color=darkred]
> > > does not exist.");
> > > > }
> > > > byte shift = index;
> > > > if (index == 0) {
> > > > if (value == 0)
> > > > Value &= unchecked((ushort)~0x01);
> > > > else
> > > > Value |= 0x01;
> > > > }
> > > > else {
> > > > if (value == 0)
> > > > Value &= ((ushort)~(0x01 << shift));
> > > > else
> > > > Value |= (ushort)(0x01 << shift);
> > > > }
> > > > }
> > > >
> > > > How about this instead (also untested):
> > > >
> > > > public void SetBit(byte index, int value) {
> > > > if (index > (byte)SIZE-1) {
> > > > throw new IndexOutOfRangeException("Bit " + index.ToString() +[/color][/color]
> "[color=green][color=darkred]
> > > does not exist.");
> > > > }
> > > > if (value == 0)
> > > > Value &= ((ushort)~(0x01 << index));
> > > > else
> > > > Value |= (ushort)(0x01 << index);
> > > > }
> > > >
> > > > This is the only type of optimizations I came up with, so I thought it
> > > wasn't worth posting. :)
> > > >
> > > >
> > > > --
> > > > Robert Jeppesen
> > > > robert.jeppesen%at%durius-dot-se
> > > >
> > > >
> > > > "Shawn B." <leabre@html.com> wrote in message
> > > news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...
> > > > > Well, I've posted it twice and no one is jumpting to optimize it...
> > > perhaps
> > > > > it's just so well working that there's no need? he he
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > > > Do post it here!
> > > > > > I'd be interested to see, as I'm sure many others would.
> > > > > > You'll probably get a ton of optimization hints from it. :)
> > > > > >
> > > > > >
> > > > > > --
> > > > > > Robert Jeppesen
> > > > > > robert.jeppesen%at%durius-dot-se
> > > > > >
> > > > > >
> > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > > > Don't miss a thing, d'ya? =))
> > > > > > >
> > > > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit[/color][/color]
> 65c02,[color=green][color=darkred]
> > > and
> > > > > the
> > > > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I[/color][/color]
> have[color=green][color=darkred]
> > > the
> > > > > > > ability to modify the assembly language while the debugger is
> > > running
> > > > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > > > > instructions. I'm not designing it to be a particular piece of
> > > hardware
> > > > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it
> > > plugins so
> > > > > my
> > > > > > > plugins can treat an area of memory as a dedicated "something"[/color][/color]
> such[color=green][color=darkred]
> > > as
> > > > > text
> > > > > > > display or graphics display, etc. For now, I'm concerned not[/color][/color]
> even[color=green][color=darkred]
> > > with
> > > > > the
> > > > > > > text display. My end result, is actually more akin to being[/color][/color]
> able to[color=green][color=darkred]
> > > > > debug
> > > > > > > my NES games I've been into writing lately. Also I want to be[/color][/color]
> ready[color=green][color=darkred]
> > > > > with my
> > > > > > > own debugger for Andre LaMothe's upcoming XGameStation console
> > > (don't
> > > > > know
> > > > > > > whether he'll have a debugger or not) but I want a debugger that
> > > allows
> > > > > me
> > > > > > > to change the code while it's being debugged and my simulator[/color][/color]
> allows[color=green][color=darkred]
> > > for
> > > > > it
> > > > > > > (as long as the source code file is available -- else if it was
> > > loading
> > > > > as a
> > > > > > > .bin file then it won't be editable during a debug session).
> > > > > > >
> > > > > > > So it's really more than a simulator. It's an integrated
> > > > > (non-seperable)
> > > > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind[/color][/color]
> of a[color=green][color=darkred]
> > > > > beast.
> > > > > > > I chose C# because it's not the kind of thing I'd want to do in
> > > VB.NET
> > > > > (I'm
> > > > > > > primarly a VB.NET programmer) but didn't feel like doing it in
> > > Managed
> > > > > C++
> > > > > > > but also wanted it to target the .NET framework (as much as is
> > > > > reasonable
> > > > > > > and I'll find out what that really means when its mature enough[/color][/color]
> to[color=green][color=darkred]
> > > start
> > > > > > > using reliably).
> > > > > > >
> > > > > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz[/color][/color]
> CPU[color=green][color=darkred]
> > > with
> > > > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do
> > > emulating
> > > > > the
> > > > > > > 16-bit processor as I haven't deciphered the specs for the CPU[/color][/color]
> yet[color=green][color=darkred]
> > > (the
> > > > > > > 8-bit CPU is very easy compared to the 16-bit version of the
> > > processor).
> > > > > I
> > > > > > > ultimately will make this Interfacable so I can write a Z80[/color][/color]
> core, as[color=green][color=darkred]
> > > > > well.
> > > > > > >
> > > > > > > These bit manipulations are the utmost inner-loop so I have to[/color][/color]
> make[color=green][color=darkred]
> > > them
> > > > > > > tight. Looking at the JIT'd code for these methods seems to be
> > > decent
> > > > > but I
> > > > > > > have to make too many function calls (read, jumps) for my tastes[/color][/color]
> but[color=green][color=darkred]
> > > > > doesn't
> > > > > > > seem to be hurting me at the moment). Since I can't inline in[/color][/color]
> .NET,[color=green][color=darkred]
> > > > > I'll
> > > > > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it[/color][/color]
> can't[color=green][color=darkred]
> > > > > > > generate the best code for the rotates and shifts because I have[/color][/color]
> to[color=green][color=darkred]
> > > > > emulate
> > > > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work[/color][/color]
> out a[color=green][color=darkred]
> > > > > better
> > > > > > > way for simulating memory manipulation. Now I'm creating a[/color][/color]
> string[color=green][color=darkred]
> > > of
> > > > > (64kb
> > > > > > > in length) but manipulating each byte by substrings has got to[/color][/color]
> be[color=green][color=darkred]
> > > > > expensive.
> > > > > > > I'm looking into whether a byte array would be better.
> > > > > > >
> > > > > > >
> > > > > > > Thanks for the help,
> > > > > > > if interested, I'll post my binary class here for all to see.
> > > > > > >
> > > > > > > I've completed the Rotates, Shift, converting numerics into[/color][/color]
> binary[color=green][color=darkred]
> > > > > (string
> > > > > > > of 1's and 0's). I know now have to read the binary back to a
> > > numeric,
> > > > > and
> > > > > > > allow a way to get/set/toggle individual bits.
> > > > > > >
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Shawn
> > > > > > >
> > > > > > >
> > > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in[/color][/color]
> message[color=green][color=darkred]
> > > > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > > > So, you're writing an emulator for which processor??
> > > > > > > >
> > > > > > > > --
> > > > > > > > Bob Powell [MVP]
> > > > > > > > C#, System.Drawing
> > > > > > > >
> > > > > > > > September's edition of Well Formed is now available.
> > > > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > > > >
> > > > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > > > Greetings,
> > > > > > > > >
> > > > > > > > > I am simulating an assembly language bit rotation in C# and[/color][/color]
> it[color=green][color=darkred]
> > > works
> > > > > > > > > wonderfully
> > > > > > > > >
> > > > > > > > > ---------
> > > > > > > > > ...
> > > > > > > > > public uint Value;
> > > > > > > > > ...
> > > > > > > > > public uint RotateRight(byte count) {
> > > > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > > > return Value;
> > > > > > > > > }
> > > > > > > > > ---------
> > > > > > > > >
> > > > > > > > > as long is the value is 32-bit or 64-bit (because of a
> > > limitation of
> > > > > the
> > > > > > > > > shift operator). I need it to work on an 8-bit or 16-bit[/color][/color]
> value,[color=green][color=darkred]
> > > as
> > > > > > > well.
> > > > > > > > I
> > > > > > > > > presume I'd have to have knowledge of the intended size of[/color][/color]
> the[color=green][color=darkred]
> > > value
> > > > > and
> > > > > > > > do
> > > > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit
> > > position
> > > > > and
> > > > > > > > clear
> > > > > > > > > the extra bits if there are any. That seems like a lot of[/color][/color]
> work.[color=green][color=darkred]
> > > > > > > > >
> > > > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a[/color][/color]
> 32-bit[color=green][color=darkred]
> > > or
> > > > > 64-bit
> > > > > > > > > value?
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > Thanks,
> > > > > > > > > Shawn
> > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#33: Nov 15 '05

re: bitwise rotation


This extra method call isn't the performance bottleneck, the real kicker
that I'm trying to solve is the fact that this value is immutable. It has
to return a new instance of the class everytime you do something with it.
MS solved that with the string object by providing us a stringbuilder class.
I'd like to be able to make this class mutable, as well.

And no, my intentions are not to keep that extra method call for the set/get
bit, and for the shifts either.


Thanks,
Shawn



"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:ebTXTtzkDHA.1408@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hey Shawn,
>
> So now getting or setting bits costs an extra method call? Get used to it[/color]
and remove the extra method call. :)[color=blue]
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:e28C0mzkDHA.360@TK2MSFTNGP12.phx.gbl...[color=blue][color=green]
> > I this has been completed. It works great. I would have never thought[/color][/color]
of[color=blue][color=green]
> > it in this manner and yet it is so simple, I was hoping more for a Bit[/color][/color]
type[color=blue][color=green]
> > of syntax but this works great. I didn't get rid of GetBit/SetBit (just
> > made them private) because the code that they perform doesn't "feel"[/color][/color]
right[color=blue][color=green]
> > in the indexer code, just looks weird. I'll make it look "right" soon[/color][/color]
but[color=blue][color=green]
> > for now...
> >
> >
> > Thanks,
> > Shawn
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:usKNb2vkDHA.3612@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > Hi Shawn,
> > >
> > > In GetBit, SetBit or in shift operators, you don't need to check of[/color][/color][/color]
are[color=blue][color=green]
> > shifting 0 steps, just do it anyway. It will return the[color=darkred]
> > > correct value.
> > > Couldn't you have an indexer on the BinaryXX class itself?
> > > Then you'd say,
> > > BinaryX[0] = 1;
> > > BinaryX[2] = BinaryX[0];
> > >
> > > For me, that would make more sense than a separate Bit class.
> > >
> > > public int this [int index] // Indexer declaration
> > > {
> > > get
> > > {
> > > //GetBit here
> > > }
> > > set
> > > {
> > > //SetBit here
> > > }
> > > }
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > > Robert,
> > > >
> > > > You're first suggestion crossed my mind, I have already started[/color]
> > eliminating[color=darkred]
> > > > the temp variables thus far (thus far only in the 8-bit operator[/color]
> > overloads[color=darkred]
> > > > region area). Just be happy you didn't see the earlier version I[/color][/color][/color]
first[color=blue][color=green][color=darkred]
> > > > posted, it didn't cast because I kept getting compile errors, it ran
> > > > everything through a Convert.ToUIntX() call but I've since removed[/color]
> > those...[color=darkred]
> > > >
> > > > I also plan to move the Shift function logic into the overload to[/color][/color][/color]
avoid[color=blue][color=green]
> > the[color=darkred]
> > > > extra function call.
> > > >
> > > > The GetBit optimization hint you provided is what I've been wanting[/color][/color][/color]
to[color=blue][color=green]
> > do[color=darkred]
> > > > but didn't quite put it they way you did... I'll try that and see[/color][/color][/color]
how it[color=blue][color=green][color=darkred]
> > > > works. I don't know what to do about that check in it, though. I[/color][/color][/color]
feel[color=blue][color=green]
> > like[color=darkred]
> > > > it's necessary. Perhaps I could just make the if based on some[/color]
> > calculation[color=darkred]
> > > > and then just throw an exception at the end of the function and[/color][/color][/color]
avoid[color=blue][color=green]
> > that[color=darkred]
> > > > early check, perhaps that would be a better approach. Such as (if[/color]
> > (Value ==[color=darkred]
> > > > 0 && Value < SIZE-1) {)
> > > >
> > > > There is one thing I need help on.
> > > >
> > > >
> > > > I don't like the GetBit, SetBit approach. I would rather be able to[/color][/color][/color]
say[color=blue][color=green]
> > x =[color=darkred]
> > > > BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had[/color][/color][/color]
an[color=blue][color=green][color=darkred]
> > > > indexer but it had problems. Not the least of which was a circular
> > > > reference that ended up being a memory leak, not what I wanted in[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > innermost loop of an emulator. The second being that I could easily[/color][/color][/color]
say[color=blue][color=green]
> > x =[color=darkred]
> > > > x.Bit[0] but not easily say x.Bit[0] = 1.
> > > >
> > > > Do you have any ideas how I might approach this? I was using a[/color][/color][/color]
property[color=blue][color=green][color=darkred]
> > > > get/set at first but that didn't work, instead I just created a[/color][/color][/color]
public[color=blue][color=green][color=darkred]
> > > > module variable "public Bit Bit;" and then it had a constructor to[/color][/color][/color]
pass[color=blue][color=green]
> > a[color=darkred]
> > > > reference of the Binary class into it so it would be able to change[/color][/color][/color]
the[color=blue][color=green]
> > bit[color=darkred]
> > > > if a bit was set. Not a good approach. Do you have any ideas?
> > > >
> > > >
> > > > Thanks for your help,
> > > > Shawn
> > > >
> > > >
> > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...
> > > > > Hi Shawn,
> > > > >
> > > > > I've looked at it, and no optimization suggestions other than
> > > > > some obvious simple ones:
> > > > >
> > > > > public int GetBit(byte index) {
> > > > > if (index > (byte)SIZE-1) {
> > > > > throw new IndexOutOfRangeException("Bit " +[/color][/color][/color]
index.ToString() +[color=blue][color=green]
> > "[color=darkred]
> > > > does not exist.");
> > > > > }
> > > > > uint val = (uint)Value;
> > > > > byte shift = (byte)(index - 1);
> > > > > if (index == 0)
> > > > > return (int)(val & 0x01);
> > > > > else
> > > > > return (int)(val >> shift) & 0x01;
> > > > > }
> > > > >
> > > > > This is very verbose, and you're creating lots of temp variables
> > > > > I'd rather write something like (untested):
> > > > >
> > > > > public int GetBit(byte index) {
> > > > > if (index > (byte)SIZE-1) {
> > > > > throw new IndexOutOfRangeException("Bit " +[/color][/color][/color]
index.ToString() +[color=blue][color=green]
> > "[color=darkred]
> > > > does not exist.");
> > > > > }
> > > > > return (int)((Value >> index-1) & 0x01;
> > > > > }
> > > > >
> > > > > The check to avoid a shift probably just hurts performance anyway.
> > > > > Also for SetBit, the two if else must result in unwanted branches:
> > > > >
> > > > > public void SetBit(byte index, int value) {
> > > > > if (index > (byte)SIZE-1) {
> > > > > throw new IndexOutOfRangeException("Bit " +[/color][/color][/color]
index.ToString() +[color=blue][color=green]
> > "[color=darkred]
> > > > does not exist.");
> > > > > }
> > > > > byte shift = index;
> > > > > if (index == 0) {
> > > > > if (value == 0)
> > > > > Value &= unchecked((ushort)~0x01);
> > > > > else
> > > > > Value |= 0x01;
> > > > > }
> > > > > else {
> > > > > if (value == 0)
> > > > > Value &= ((ushort)~(0x01 << shift));
> > > > > else
> > > > > Value |= (ushort)(0x01 << shift);
> > > > > }
> > > > > }
> > > > >
> > > > > How about this instead (also untested):
> > > > >
> > > > > public void SetBit(byte index, int value) {
> > > > > if (index > (byte)SIZE-1) {
> > > > > throw new IndexOutOfRangeException("Bit " +[/color][/color][/color]
index.ToString() +[color=blue][color=green]
> > "[color=darkred]
> > > > does not exist.");
> > > > > }
> > > > > if (value == 0)
> > > > > Value &= ((ushort)~(0x01 << index));
> > > > > else
> > > > > Value |= (ushort)(0x01 << index);
> > > > > }
> > > > >
> > > > > This is the only type of optimizations I came up with, so I[/color][/color][/color]
thought it[color=blue][color=green][color=darkred]
> > > > wasn't worth posting. :)
> > > > >
> > > > >
> > > > > --
> > > > > Robert Jeppesen
> > > > > robert.jeppesen%at%durius-dot-se
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...
> > > > > > Well, I've posted it twice and no one is jumpting to optimize[/color][/color][/color]
it...[color=blue][color=green][color=darkred]
> > > > perhaps
> > > > > > it's just so well working that there's no need? he he
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > > > > Do post it here!
> > > > > > > I'd be interested to see, as I'm sure many others would.
> > > > > > > You'll probably get a ton of optimization hints from it. :)
> > > > > > >
> > > > > > >
> > > > > > > --
> > > > > > > Robert Jeppesen
> > > > > > > robert.jeppesen%at%durius-dot-se
> > > > > > >
> > > > > > >
> > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > > > > Don't miss a thing, d'ya? =))
> > > > > > > >
> > > > > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit[/color]
> > 65c02,[color=darkred]
> > > > and
> > > > > > the
> > > > > > > > 16-bit 65816. In particular, I'm doing this so I can debug[/color][/color][/color]
(I[color=blue][color=green]
> > have[color=darkred]
> > > > the
> > > > > > > > ability to modify the assembly language while the debugger[/color][/color][/color]
is[color=blue][color=green][color=darkred]
> > > > running
> > > > > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see[/color][/color][/color]
raw[color=blue][color=green][color=darkred]
> > > > > > > > instructions. I'm not designing it to be a particular piece[/color][/color][/color]
of[color=blue][color=green][color=darkred]
> > > > hardware
> > > > > > > > (such as the Apple, Atari, Commadore, etc.). I can supply[/color][/color][/color]
it[color=blue][color=green][color=darkred]
> > > > plugins so
> > > > > > my
> > > > > > > > plugins can treat an area of memory as a dedicated[/color][/color][/color]
"something"[color=blue][color=green]
> > such[color=darkred]
> > > > as
> > > > > > text
> > > > > > > > display or graphics display, etc. For now, I'm concerned[/color][/color][/color]
not[color=blue][color=green]
> > even[color=darkred]
> > > > with
> > > > > > the
> > > > > > > > text display. My end result, is actually more akin to being[/color]
> > able to[color=darkred]
> > > > > > debug
> > > > > > > > my NES games I've been into writing lately. Also I want to[/color][/color][/color]
be[color=blue][color=green]
> > ready[color=darkred]
> > > > > > with my
> > > > > > > > own debugger for Andre LaMothe's upcoming XGameStation[/color][/color][/color]
console[color=blue][color=green][color=darkred]
> > > > (don't
> > > > > > know
> > > > > > > > whether he'll have a debugger or not) but I want a debugger[/color][/color][/color]
that[color=blue][color=green][color=darkred]
> > > > allows
> > > > > > me
> > > > > > > > to change the code while it's being debugged and my[/color][/color][/color]
simulator[color=blue][color=green]
> > allows[color=darkred]
> > > > for
> > > > > > it
> > > > > > > > (as long as the source code file is available -- else if it[/color][/color][/color]
was[color=blue][color=green][color=darkred]
> > > > loading
> > > > > > as a
> > > > > > > > .bin file then it won't be editable during a debug session).
> > > > > > > >
> > > > > > > > So it's really more than a simulator. It's an integrated
> > > > > > (non-seperable)
> > > > > > > > assembly, linker, dissassembler, debugger, simulator, etc.[/color][/color][/color]
kind[color=blue][color=green]
> > of a[color=darkred]
> > > > > > beast.
> > > > > > > > I chose C# because it's not the kind of thing I'd want to do[/color][/color][/color]
in[color=blue][color=green][color=darkred]
> > > > VB.NET
> > > > > > (I'm
> > > > > > > > primarly a VB.NET programmer) but didn't feel like doing it[/color][/color][/color]
in[color=blue][color=green][color=darkred]
> > > > Managed
> > > > > > C++
> > > > > > > > but also wanted it to target the .NET framework (as much as[/color][/color][/color]
is[color=blue][color=green][color=darkred]
> > > > > > reasonable
> > > > > > > > and I'll find out what that really means when its mature[/color][/color][/color]
enough[color=blue][color=green]
> > to[color=darkred]
> > > > start
> > > > > > > > using reliably).
> > > > > > > >
> > > > > > > > I can do a decent job emulating 1-5MHz in C# on my PIII[/color][/color][/color]
1000MHz[color=blue][color=green]
> > CPU[color=darkred]
> > > > with
> > > > > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll[/color][/color][/color]
do[color=blue][color=green][color=darkred]
> > > > emulating
> > > > > > the
> > > > > > > > 16-bit processor as I haven't deciphered the specs for the[/color][/color][/color]
CPU[color=blue][color=green]
> > yet[color=darkred]
> > > > (the
> > > > > > > > 8-bit CPU is very easy compared to the 16-bit version of the
> > > > processor).
> > > > > > I
> > > > > > > > ultimately will make this Interfacable so I can write a Z80[/color]
> > core, as[color=darkred]
> > > > > > well.
> > > > > > > >
> > > > > > > > These bit manipulations are the utmost inner-loop so I have[/color][/color][/color]
to[color=blue][color=green]
> > make[color=darkred]
> > > > them
> > > > > > > > tight. Looking at the JIT'd code for these methods seems to[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> > > > decent
> > > > > > but I
> > > > > > > > have to make too many function calls (read, jumps) for my[/color][/color][/color]
tastes[color=blue][color=green]
> > but[color=darkred]
> > > > > > doesn't
> > > > > > > > seem to be hurting me at the moment). Since I can't inline[/color][/color][/color]
in[color=blue][color=green]
> > .NET,[color=darkred]
> > > > > > I'll
> > > > > > > > have to live with it. Also, being an 8-bit and 16-bit CPU,[/color][/color][/color]
it[color=blue][color=green]
> > can't[color=darkred]
> > > > > > > > generate the best code for the rotates and shifts because I[/color][/color][/color]
have[color=blue][color=green]
> > to[color=darkred]
> > > > > > emulate
> > > > > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to[/color][/color][/color]
work[color=blue][color=green]
> > out a[color=darkred]
> > > > > > better
> > > > > > > > way for simulating memory manipulation. Now I'm creating a[/color]
> > string[color=darkred]
> > > > of
> > > > > > (64kb
> > > > > > > > in length) but manipulating each byte by substrings has got[/color][/color][/color]
to[color=blue][color=green]
> > be[color=darkred]
> > > > > > expensive.
> > > > > > > > I'm looking into whether a byte array would be better.
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks for the help,
> > > > > > > > if interested, I'll post my binary class here for all to[/color][/color][/color]
see.[color=blue][color=green][color=darkred]
> > > > > > > >
> > > > > > > > I've completed the Rotates, Shift, converting numerics into[/color]
> > binary[color=darkred]
> > > > > > (string
> > > > > > > > of 1's and 0's). I know now have to read the binary back to[/color][/color][/color]
a[color=blue][color=green][color=darkred]
> > > > numeric,
> > > > > > and
> > > > > > > > allow a way to get/set/toggle individual bits.
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks,
> > > > > > > > Shawn
> > > > > > > >
> > > > > > > >
> > > > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in[/color]
> > message[color=darkred]
> > > > > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > > > > So, you're writing an emulator for which processor??
> > > > > > > > >
> > > > > > > > > --
> > > > > > > > > Bob Powell [MVP]
> > > > > > > > > C#, System.Drawing
> > > > > > > > >
> > > > > > > > > September's edition of Well Formed is now available.
> > > > > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > > > > >
> > > > > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > > > > Greetings,
> > > > > > > > > >
> > > > > > > > > > I am simulating an assembly language bit rotation in C#[/color][/color][/color]
and[color=blue][color=green]
> > it[color=darkred]
> > > > works
> > > > > > > > > > wonderfully
> > > > > > > > > >
> > > > > > > > > > ---------
> > > > > > > > > > ...
> > > > > > > > > > public uint Value;
> > > > > > > > > > ...
> > > > > > > > > > public uint RotateRight(byte count) {
> > > > > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > > > > return Value;
> > > > > > > > > > }
> > > > > > > > > > ---------
> > > > > > > > > >
> > > > > > > > > > as long is the value is 32-bit or 64-bit (because of a
> > > > limitation of
> > > > > > the
> > > > > > > > > > shift operator). I need it to work on an 8-bit or[/color][/color][/color]
16-bit[color=blue][color=green]
> > value,[color=darkred]
> > > > as
> > > > > > > > well.
> > > > > > > > > I
> > > > > > > > > > presume I'd have to have knowledge of the intended size[/color][/color][/color]
of[color=blue][color=green]
> > the[color=darkred]
> > > > value
> > > > > > and
> > > > > > > > > do
> > > > > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15[/color][/color][/color]
bit[color=blue][color=green][color=darkred]
> > > > position
> > > > > > and
> > > > > > > > > clear
> > > > > > > > > > the extra bits if there are any. That seems like a lot[/color][/color][/color]
of[color=blue][color=green]
> > work.[color=darkred]
> > > > > > > > > >
> > > > > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a[/color]
> > 32-bit[color=darkred]
> > > > or
> > > > > > 64-bit
> > > > > > > > > > value?
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > > Thanks,
> > > > > > > > > > Shawn
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Shawn B.
Guest
 
Posts: n/a
#34: Nov 15 '05

re: bitwise rotation


Okay, check out the new files. I removed the shift and the get/set bit
methods and they now take place without the overhead of a jump.


Thanks,
Shawn


"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:ebTXTtzkDHA.1408@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hey Shawn,
>
> So now getting or setting bits costs an extra method call? Get used to it[/color]
and remove the extra method call. :)[color=blue]
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <leabre@html.com> wrote in message[/color]
news:e28C0mzkDHA.360@TK2MSFTNGP12.phx.gbl...[color=blue][color=green]
> > I this has been completed. It works great. I would have never thought[/color][/color]
of[color=blue][color=green]
> > it in this manner and yet it is so simple, I was hoping more for a Bit[/color][/color]
type[color=blue][color=green]
> > of syntax but this works great. I didn't get rid of GetBit/SetBit (just
> > made them private) because the code that they perform doesn't "feel"[/color][/color]
right[color=blue][color=green]
> > in the indexer code, just looks weird. I'll make it look "right" soon[/color][/color]
but[color=blue][color=green]
> > for now...
> >
> >
> > Thanks,
> > Shawn
> >
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:usKNb2vkDHA.3612@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > Hi Shawn,
> > >
> > > In GetBit, SetBit or in shift operators, you don't need to check of[/color][/color][/color]
are[color=blue][color=green]
> > shifting 0 steps, just do it anyway. It will return the[color=darkred]
> > > correct value.
> > > Couldn't you have an indexer on the BinaryXX class itself?
> > > Then you'd say,
> > > BinaryX[0] = 1;
> > > BinaryX[2] = BinaryX[0];
> > >
> > > For me, that would make more sense than a separate Bit class.
> > >
> > > public int this [int index] // Indexer declaration
> > > {
> > > get
> > > {
> > > //GetBit here
> > > }
> > > set
> > > {
> > > //SetBit here
> > > }
> > > }
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <leabre@html.com> wrote in message[/color]
> > news:%23pIMvgqkDHA.3700@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > > Robert,
> > > >
> > > > You're first suggestion crossed my mind, I have already started[/color]
> > eliminating[color=darkred]
> > > > the temp variables thus far (thus far only in the 8-bit operator[/color]
> > overloads[color=darkred]
> > > > region area). Just be happy you didn't see the earlier version I[/color][/color][/color]
first[color=blue][color=green][color=darkred]
> > > > posted, it didn't cast because I kept getting compile errors, it ran
> > > > everything through a Convert.ToUIntX() call but I've since removed[/color]
> > those...[color=darkred]
> > > >
> > > > I also plan to move the Shift function logic into the overload to[/color][/color][/color]
avoid[color=blue][color=green]
> > the[color=darkred]
> > > > extra function call.
> > > >
> > > > The GetBit optimization hint you provided is what I've been wanting[/color][/color][/color]
to[color=blue][color=green]
> > do[color=darkred]
> > > > but didn't quite put it they way you did... I'll try that and see[/color][/color][/color]
how it[color=blue][color=green][color=darkred]
> > > > works. I don't know what to do about that check in it, though. I[/color][/color][/color]
feel[color=blue][color=green]
> > like[color=darkred]
> > > > it's necessary. Perhaps I could just make the if based on some[/color]
> > calculation[color=darkred]
> > > > and then just throw an exception at the end of the function and[/color][/color][/color]
avoid[color=blue][color=green]
> > that[color=darkred]
> > > > early check, perhaps that would be a better approach. Such as (if[/color]
> > (Value ==[color=darkred]
> > > > 0 && Value < SIZE-1) {)
> > > >
> > > > There is one thing I need help on.
> > > >
> > > >
> > > > I don't like the GetBit, SetBit approach. I would rather be able to[/color][/color][/color]
say[color=blue][color=green]
> > x =[color=darkred]
> > > > BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had[/color][/color][/color]
an[color=blue][color=green][color=darkred]
> > > > indexer but it had problems. Not the least of which was a circular
> > > > reference that ended up being a memory leak, not what I wanted in[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > > innermost loop of an emulator. The second being that I could easily[/color][/color][/color]
say[color=blue][color=green]
> > x =[color=darkred]
> > > > x.Bit[0] but not easily say x.Bit[0] = 1.
> > > >
> > > > Do you have any ideas how I might approach this? I was using a[/color][/color][/color]
property[color=blue][color=green][color=darkred]
> > > > get/set at first but that didn't work, instead I just created a[/color][/color][/color]
public[color=blue][color=green][color=darkred]
> > > > module variable "public Bit Bit;" and then it had a constructor to[/color][/color][/color]
pass[color=blue][color=green]
> > a[color=darkred]
> > > > reference of the Binary class into it so it would be able to change[/color][/color][/color]
the[color=blue][color=green]
> > bit[color=darkred]
> > > > if a bit was set. Not a good approach. Do you have any ideas?
> > > >
> > > >
> > > > Thanks for your help,
> > > > Shawn
> > > >
> > > >
> > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > news:O9uOiRqkDHA.964@TK2MSFTNGP10.phx.gbl...
> > > > > Hi Shawn,
> > > > >
> > > > > I've looked at it, and no optimization suggestions other than
> > > > > some obvious simple ones:
> > > > >
> > > > > public int GetBit(byte index) {
> > > > > if (index > (byte)SIZE-1) {
> > > > > throw new IndexOutOfRangeException("Bit " +[/color][/color][/color]
index.ToString() +[color=blue][color=green]
> > "[color=darkred]
> > > > does not exist.");
> > > > > }
> > > > > uint val = (uint)Value;
> > > > > byte shift = (byte)(index - 1);
> > > > > if (index == 0)
> > > > > return (int)(val & 0x01);
> > > > > else
> > > > > return (int)(val >> shift) & 0x01;
> > > > > }
> > > > >
> > > > > This is very verbose, and you're creating lots of temp variables
> > > > > I'd rather write something like (untested):
> > > > >
> > > > > public int GetBit(byte index) {
> > > > > if (index > (byte)SIZE-1) {
> > > > > throw new IndexOutOfRangeException("Bit " +[/color][/color][/color]
index.ToString() +[color=blue][color=green]
> > "[color=darkred]
> > > > does not exist.");
> > > > > }
> > > > > return (int)((Value >> index-1) & 0x01;
> > > > > }
> > > > >
> > > > > The check to avoid a shift probably just hurts performance anyway.
> > > > > Also for SetBit, the two if else must result in unwanted branches:
> > > > >
> > > > > public void SetBit(byte index, int value) {
> > > > > if (index > (byte)SIZE-1) {
> > > > > throw new IndexOutOfRangeException("Bit " +[/color][/color][/color]
index.ToString() +[color=blue][color=green]
> > "[color=darkred]
> > > > does not exist.");
> > > > > }
> > > > > byte shift = index;
> > > > > if (index == 0) {
> > > > > if (value == 0)
> > > > > Value &= unchecked((ushort)~0x01);
> > > > > else
> > > > > Value |= 0x01;
> > > > > }
> > > > > else {
> > > > > if (value == 0)
> > > > > Value &= ((ushort)~(0x01 << shift));
> > > > > else
> > > > > Value |= (ushort)(0x01 << shift);
> > > > > }
> > > > > }
> > > > >
> > > > > How about this instead (also untested):
> > > > >
> > > > > public void SetBit(byte index, int value) {
> > > > > if (index > (byte)SIZE-1) {
> > > > > throw new IndexOutOfRangeException("Bit " +[/color][/color][/color]
index.ToString() +[color=blue][color=green]
> > "[color=darkred]
> > > > does not exist.");
> > > > > }
> > > > > if (value == 0)
> > > > > Value &= ((ushort)~(0x01 << index));
> > > > > else
> > > > > Value |= (ushort)(0x01 << index);
> > > > > }
> > > > >
> > > > > This is the only type of optimizations I came up with, so I[/color][/color][/color]
thought it[color=blue][color=green][color=darkred]
> > > > wasn't worth posting. :)
> > > > >
> > > > >
> > > > > --
> > > > > Robert Jeppesen
> > > > > robert.jeppesen%at%durius-dot-se
> > > > >
> > > > >
> > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > news:%23ZcR5IqkDHA.976@tk2msftngp13.phx.gbl...
> > > > > > Well, I've posted it twice and no one is jumpting to optimize[/color][/color][/color]
it...[color=blue][color=green][color=darkred]
> > > > perhaps
> > > > > > it's just so well working that there's no need? he he
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > > > > news:Oi3WUuujDHA.1884@TK2MSFTNGP09.phx.gbl...
> > > > > > > Do post it here!
> > > > > > > I'd be interested to see, as I'm sure many others would.
> > > > > > > You'll probably get a ton of optimization hints from it. :)
> > > > > > >
> > > > > > >
> > > > > > > --
> > > > > > > Robert Jeppesen
> > > > > > > robert.jeppesen%at%durius-dot-se
> > > > > > >
> > > > > > >
> > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > news:OWE$XlsjDHA.3256@tk2msftngp13.phx.gbl...
> > > > > > > > Don't miss a thing, d'ya? =))
> > > > > > > >
> > > > > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit[/color]
> > 65c02,[color=darkred]
> > > > and
> > > > > > the
> > > > > > > > 16-bit 65816. In particular, I'm doing this so I can debug[/color][/color][/color]
(I[color=blue][color=green]
> > have[color=darkred]
> > > > the
> > > > > > > > ability to modify the assembly language while the debugger[/color][/color][/color]
is[color=blue][color=green][color=darkred]
> > > > running
> > > > > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see[/color][/color][/color]
raw[color=blue][color=green][color=darkred]
> > > > > > > > instructions. I'm not designing it to be a particular piece[/color][/color][/color]
of[color=blue][color=green][color=darkred]
> > > > hardware
> > > > > > > > (such as the Apple, Atari, Commadore, etc.). I can supply[/color][/color][/color]
it[color=blue][color=green][color=darkred]
> > > > plugins so
> > > > > > my
> > > > > > > > plugins can treat an area of memory as a dedicated[/color][/color][/color]
"something"[color=blue][color=green]
> > such[color=darkred]
> > > > as
> > > > > > text
> > > > > > > > display or graphics display, etc. For now, I'm concerned[/color][/color][/color]
not[color=blue][color=green]
> > even[color=darkred]
> > > > with
> > > > > > the
> > > > > > > > text display. My end result, is actually more akin to being[/color]
> > able to[color=darkred]
> > > > > > debug
> > > > > > > > my NES games I've been into writing lately. Also I want to[/color][/color][/color]
be[color=blue][color=green]
> > ready[color=darkred]
> > > > > > with my
> > > > > > > > own debugger for Andre LaMothe's upcoming XGameStation[/color][/color][/color]
console[color=blue][color=green][color=darkred]
> > > > (don't
> > > > > > know
> > > > > > > > whether he'll have a debugger or not) but I want a debugger[/color][/color][/color]
that[color=blue][color=green][color=darkred]
> > > > allows
> > > > > > me
> > > > > > > > to change the code while it's being debugged and my[/color][/color][/color]
simulator[color=blue][color=green]
> > allows[color=darkred]
> > > > for
> > > > > > it
> > > > > > > > (as long as the source code file is available -- else if it[/color][/color][/color]
was[color=blue][color=green][color=darkred]
> > > > loading
> > > > > > as a
> > > > > > > > .bin file then it won't be editable during a debug session).
> > > > > > > >
> > > > > > > > So it's really more than a simulator. It's an integrated
> > > > > > (non-seperable)
> > > > > > > > assembly, linker, dissassembler, debugger, simulator, etc.[/color][/color][/color]
kind[color=blue][color=green]
> > of a[color=darkred]
> > > > > > beast.
> > > > > > > > I chose C# because it's not the kind of thing I'd want to do[/color][/color][/color]
in[color=blue][color=green][color=darkred]
> > > > VB.NET
> > > > > > (I'm
> > > > > > > > primarly a VB.NET programmer) but didn't feel like doing it[/color][/color][/color]
in[color=blue][color=green][color=darkred]
> > > > Managed
> > > > > > C++
> > > > > > > > but also wanted it to target the .NET framework (as much as[/color][/color][/color]
is[color=blue][color=green][color=darkred]
> > > > > > reasonable
> > > > > > > > and I'll find out what that really means when its mature[/color][/color][/color]
enough[color=blue][color=green]
> > to[color=darkred]
> > > > start
> > > > > > > > using reliably).
> > > > > > > >
> > > > > > > > I can do a decent job emulating 1-5MHz in C# on my PIII[/color][/color][/color]
1000MHz[color=blue][color=green]
> > CPU[color=darkred]
> > > > with
> > > > > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll[/color][/color][/color]
do[color=blue][color=green][color=darkred]
> > > > emulating
> > > > > > the
> > > > > > > > 16-bit processor as I haven't deciphered the specs for the[/color][/color][/color]
CPU[color=blue][color=green]
> > yet[color=darkred]
> > > > (the
> > > > > > > > 8-bit CPU is very easy compared to the 16-bit version of the
> > > > processor).
> > > > > > I
> > > > > > > > ultimately will make this Interfacable so I can write a Z80[/color]
> > core, as[color=darkred]
> > > > > > well.
> > > > > > > >
> > > > > > > > These bit manipulations are the utmost inner-loop so I have[/color][/color][/color]
to[color=blue][color=green]
> > make[color=darkred]
> > > > them
> > > > > > > > tight. Looking at the JIT'd code for these methods seems to[/color][/color][/color]
be[color=blue][color=green][color=darkred]
> > > > decent
> > > > > > but I
> > > > > > > > have to make too many function calls (read, jumps) for my[/color][/color][/color]
tastes[color=blue][color=green]
> > but[color=darkred]
> > > > > > doesn't
> > > > > > > > seem to be hurting me at the moment). Since I can't inline[/color][/color][/color]
in[color=blue][color=green]
> > .NET,[color=darkred]
> > > > > > I'll
> > > > > > > > have to live with it. Also, being an 8-bit and 16-bit CPU,[/color][/color][/color]
it[color=blue][color=green]
> > can't[color=darkred]
> > > > > > > > generate the best code for the rotates and shifts because I[/color][/color][/color]
have[color=blue][color=green]
> > to[color=darkred]
> > > > > > emulate
> > > > > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to[/color][/color][/color]
work[color=blue][color=green]
> > out a[color=darkred]
> > > > > > better
> > > > > > > > way for simulating memory manipulation. Now I'm creating a[/color]
> > string[color=darkred]
> > > > of
> > > > > > (64kb
> > > > > > > > in length) but manipulating each byte by substrings has got[/color][/color][/color]
to[color=blue][color=green]
> > be[color=darkred]
> > > > > > expensive.
> > > > > > > > I'm looking into whether a byte array would be better.
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks for the help,
> > > > > > > > if interested, I'll post my binary class here for all to[/color][/color][/color]
see.[color=blue][color=green][color=darkred]
> > > > > > > >
> > > > > > > > I've completed the Rotates, Shift, converting numerics into[/color]
> > binary[color=darkred]
> > > > > > (string
> > > > > > > > of 1's and 0's). I know now have to read the binary back to[/color][/color][/color]
a[color=blue][color=green][color=darkred]
> > > > numeric,
> > > > > > and
> > > > > > > > allow a way to get/set/toggle individual bits.
> > > > > > > >
> > > > > > > >
> > > > > > > > Thanks,
> > > > > > > > Shawn
> > > > > > > >
> > > > > > > >
> > > > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in[/color]
> > message[color=darkred]
> > > > > > > > news:%233JwKwrjDHA.3024@tk2msftngp13.phx.gbl...
> > > > > > > > > So, you're writing an emulator for which processor??
> > > > > > > > >
> > > > > > > > > --
> > > > > > > > > Bob Powell [MVP]
> > > > > > > > > C#, System.Drawing
> > > > > > > > >
> > > > > > > > > September's edition of Well Formed is now available.
> > > > > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > > > > >
> > > > > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > "Shawn B." <leabre@html.com> wrote in message
> > > > > > > > > news:O9#ORbqjDHA.392@TK2MSFTNGP11.phx.gbl...
> > > > > > > > > > Greetings,
> > > > > > > > > >
> > > > > > > > > > I am simulating an assembly language bit rotation in C#[/color][/color][/color]
and[color=blue][color=green]
> > it[color=darkred]
> > > > works
> > > > > > > > > > wonderfully
> > > > > > > > > >
> > > > > > > > > > ---------
> > > > > > > > > > ...
> > > > > > > > > > public uint Value;
> > > > > > > > > > ...
> > > > > > > > > > public uint RotateRight(byte count) {
> > > > > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > > > > return Value;
> > > > > > > > > > }
> > > > > > > > > > ---------
> > > > > > > > > >
> > > > > > > > > > as long is the value is 32-bit or 64-bit (because of a
> > > > limitation of
> > > > > > the
> > > > > > > > > > shift operator). I need it to work on an 8-bit or[/color][/color][/color]
16-bit[color=blue][color=green]
> > value,[color=darkred]
> > > > as
> > > > > > > > well.
> > > > > > > > > I
> > > > > > > > > > presume I'd have to have knowledge of the intended size[/color][/color][/color]
of[color=blue][color=green]
> > the[color=darkred]
> > > > value
> > > > > > and
> > > > > > > > > do
> > > > > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15[/color][/color][/color]
bit[color=blue][color=green][color=darkred]
> > > > position
> > > > > > and
> > > > > > > > > clear
> > > > > > > > > > the extra bits if there are any. That seems like a lot[/color][/color][/color]
of[color=blue][color=green]
> > work.[color=darkred]
> > > > > > > > > >
> > > > > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a[/color]
> > 32-bit[color=darkred]
> > > > or
> > > > > > 64-bit
> > > > > > > > > > value?
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > > Thanks,
> > > > > > > > > > Shawn
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Closed Thread


Similar C# / C Sharp bytes