Convert if to switch statement.

This commit is contained in:
2022-11-13 19:38:02 +00:00
parent a9af82e47f
commit 55fb8416e8
4 changed files with 228 additions and 220 deletions

View File

@@ -157,8 +157,10 @@ public sealed class Adler32Context : IChecksum
uint sum2 = preSum2; uint sum2 = preSum2;
var dataOff = 0; var dataOff = 0;
switch(len)
{
/* in case user likes doing a byte at a time, keep it fast */ /* in case user likes doing a byte at a time, keep it fast */
if(len == 1) case 1:
{ {
sum1 += data[dataOff]; sum1 += data[dataOff];
@@ -175,9 +177,8 @@ public sealed class Adler32Context : IChecksum
return; return;
} }
/* in case short lengths are provided, keep it somewhat fast */ /* in case short lengths are provided, keep it somewhat fast */
if(len < 16) case < 16:
{ {
while(len-- > 0) while(len-- > 0)
{ {
@@ -194,6 +195,7 @@ public sealed class Adler32Context : IChecksum
return; return;
} }
}
/* do length NMAX blocks -- requires just one modulo operation */ /* do length NMAX blocks -- requires just one modulo operation */
while(len >= NMAX) while(len >= NMAX)

View File

@@ -198,7 +198,10 @@ public static class CdChecksums
//AaruConsole.DebugWriteLine("CD checksums", "Data sector, address {0:X2}:{1:X2}:{2:X2}", channel[0x00C], //AaruConsole.DebugWriteLine("CD checksums", "Data sector, address {0:X2}:{1:X2}:{2:X2}", channel[0x00C],
// channel[0x00D], channel[0x00E]); // channel[0x00D], channel[0x00E]);
if((channel[0x00F] & 0x03) == 0x00) // mode (1 byte) switch(channel[0x00F] & 0x03)
{
// mode (1 byte)
case 0x00:
{ {
//AaruConsole.DebugWriteLine("CD checksums", "Mode 0 sector at address {0:X2}:{1:X2}:{2:X2}", //AaruConsole.DebugWriteLine("CD checksums", "Mode 0 sector at address {0:X2}:{1:X2}:{2:X2}",
// channel[0x00C], channel[0x00D], channel[0x00E]); // channel[0x00C], channel[0x00D], channel[0x00E]);
@@ -215,27 +218,20 @@ public static class CdChecksums
return true; return true;
} }
if((channel[0x00F] & 0x03) == 0x01) // mode (1 byte) // mode (1 byte)
{
//AaruConsole.DebugWriteLine("CD checksums", "Mode 1 sector at address {0:X2}:{1:X2}:{2:X2}", //AaruConsole.DebugWriteLine("CD checksums", "Mode 1 sector at address {0:X2}:{1:X2}:{2:X2}",
// channel[0x00C], channel[0x00D], channel[0x00E]); // channel[0x00C], channel[0x00D], channel[0x00E]);
case 0x01 when channel[0x814] != 0x00 || // reserved (8 bytes)
if(channel[0x814] != 0x00 || // reserved (8 bytes) channel[0x815] != 0x00 || channel[0x816] != 0x00 || channel[0x817] != 0x00 ||
channel[0x815] != 0x00 || channel[0x818] != 0x00 || channel[0x819] != 0x00 || channel[0x81A] != 0x00 ||
channel[0x816] != 0x00 || channel[0x81B] != 0x00:
channel[0x817] != 0x00 ||
channel[0x818] != 0x00 ||
channel[0x819] != 0x00 ||
channel[0x81A] != 0x00 ||
channel[0x81B] != 0x00)
{
AaruConsole.DebugWriteLine("CD checksums", AaruConsole.DebugWriteLine("CD checksums",
"Mode 1 sector with data in reserved bytes at address: {0:X2}:{1:X2}:{2:X2}", "Mode 1 sector with data in reserved bytes at address: {0:X2}:{1:X2}:{2:X2}",
channel[0x00C], channel[0x00D], channel[0x00E]); channel[0x00C], channel[0x00D], channel[0x00E]);
return false; return false;
} case 0x01:
{
var address = new byte[4]; var address = new byte[4];
var data = new byte[2060]; var data = new byte[2060];
var data2 = new byte[2232]; var data2 = new byte[2232];
@@ -279,7 +275,8 @@ public static class CdChecksums
return false; return false;
} }
if((channel[0x00F] & 0x03) == 0x02) // mode (1 byte) // mode (1 byte)
case 0x02:
{ {
//AaruConsole.DebugWriteLine("CD checksums", "Mode 2 sector at address {0:X2}:{1:X2}:{2:X2}", //AaruConsole.DebugWriteLine("CD checksums", "Mode 2 sector at address {0:X2}:{1:X2}:{2:X2}",
// channel[0x00C], channel[0x00D], channel[0x00E]); // channel[0x00C], channel[0x00D], channel[0x00E]);
@@ -311,7 +308,8 @@ public static class CdChecksums
AaruConsole.DebugWriteLine("CD checksums", AaruConsole.DebugWriteLine("CD checksums",
"Mode 2 form 2 sector at address: {0:X2}:{1:X2}:{2:X2}, got CRC 0x{3:X8} expected 0x{4:X8}", "Mode 2 form 2 sector at address: {0:X2}:{1:X2}:{2:X2}, got CRC 0x{3:X8} expected 0x{4:X8}",
channel[0x00C], channel[0x00D], channel[0x00E], calculatedEdc, storedEdc); channel[0x00C], channel[0x00D], channel[0x00E], calculatedEdc,
storedEdc);
return false; return false;
} }
@@ -358,17 +356,19 @@ public static class CdChecksums
AaruConsole.DebugWriteLine("CD checksums", AaruConsole.DebugWriteLine("CD checksums",
"Mode 2 sector at address: {0:X2}:{1:X2}:{2:X2}, got CRC 0x{3:X8} expected 0x{4:X8}", "Mode 2 sector at address: {0:X2}:{1:X2}:{2:X2}, got CRC 0x{3:X8} expected 0x{4:X8}",
channel[0x00C], channel[0x00D], channel[0x00E], calculatedEdc, storedEdc); channel[0x00C], channel[0x00D], channel[0x00E], calculatedEdc,
storedEdc);
return false; return false;
} }
} }
default:
AaruConsole.DebugWriteLine("CD checksums", "Unknown mode {0} sector at address: {1:X2}:{2:X2}:{3:X2}", AaruConsole.DebugWriteLine("CD checksums", "Unknown mode {0} sector at address: {1:X2}:{2:X2}:{3:X2}",
channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]); channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]);
return null; return null;
} }
}
static uint ComputeEdc(uint edc, IReadOnlyList<byte> src, int size) static uint ComputeEdc(uint edc, IReadOnlyList<byte> src, int size)
{ {

View File

@@ -136,8 +136,10 @@ public sealed class Fletcher32Context : IChecksum
uint sum2 = previousSum2; uint sum2 = previousSum2;
var dataOff = 0; var dataOff = 0;
switch(len)
{
/* in case user likes doing a byte at a time, keep it fast */ /* in case user likes doing a byte at a time, keep it fast */
if(len == 1) case 1:
{ {
sum1 += data[dataOff]; sum1 += data[dataOff];
@@ -154,9 +156,8 @@ public sealed class Fletcher32Context : IChecksum
return; return;
} }
/* in case short lengths are provided, keep it somewhat fast */ /* in case short lengths are provided, keep it somewhat fast */
if(len < 16) case < 16:
{ {
while(len-- > 0) while(len-- > 0)
{ {
@@ -173,6 +174,7 @@ public sealed class Fletcher32Context : IChecksum
return; return;
} }
}
/* do length NMAX blocks -- requires just one modulo operation */ /* do length NMAX blocks -- requires just one modulo operation */
while(len >= NMAX) while(len >= NMAX)
@@ -482,8 +484,10 @@ public sealed class Fletcher16Context : IChecksum
uint sum2 = previousSum2; uint sum2 = previousSum2;
var dataOff = 0; var dataOff = 0;
switch(len)
{
/* in case user likes doing a byte at a time, keep it fast */ /* in case user likes doing a byte at a time, keep it fast */
if(len == 1) case 1:
{ {
sum1 += data[dataOff]; sum1 += data[dataOff];
@@ -500,9 +504,8 @@ public sealed class Fletcher16Context : IChecksum
return; return;
} }
/* in case short lengths are provided, keep it somewhat fast */ /* in case short lengths are provided, keep it somewhat fast */
if(len < 11) case < 11:
{ {
while(len-- > 0) while(len-- > 0)
{ {
@@ -519,6 +522,7 @@ public sealed class Fletcher16Context : IChecksum
return; return;
} }
}
/* do length NMAX blocks -- requires just one modulo operation */ /* do length NMAX blocks -- requires just one modulo operation */
while(len >= NMAX) while(len >= NMAX)

View File

@@ -169,11 +169,13 @@ public sealed class SpamSumContext : IChecksum
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
void fuzzy_try_fork_blockhash() void fuzzy_try_fork_blockhash()
{ {
if(_self.Bhend >= NUM_BLOCKHASHES) switch(_self.Bhend)
return; {
case >= NUM_BLOCKHASHES: return;
if(_self.Bhend == 0) // assert // assert
throw new Exception("Assertion failed"); case 0: throw new Exception("Assertion failed");
}
uint obh = _self.Bhend - 1; uint obh = _self.Bhend - 1;
uint nbh = _self.Bhend; uint nbh = _self.Bhend;