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;
var dataOff = 0;
switch(len)
{
/* in case user likes doing a byte at a time, keep it fast */
if(len == 1)
case 1:
{
sum1 += data[dataOff];
@@ -175,9 +177,8 @@ public sealed class Adler32Context : IChecksum
return;
}
/* in case short lengths are provided, keep it somewhat fast */
if(len < 16)
case < 16:
{
while(len-- > 0)
{
@@ -194,6 +195,7 @@ public sealed class Adler32Context : IChecksum
return;
}
}
/* do length NMAX blocks -- requires just one modulo operation */
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],
// 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}",
// channel[0x00C], channel[0x00D], channel[0x00E]);
@@ -215,27 +218,20 @@ public static class CdChecksums
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}",
// channel[0x00C], channel[0x00D], channel[0x00E]);
if(channel[0x814] != 0x00 || // reserved (8 bytes)
channel[0x815] != 0x00 ||
channel[0x816] != 0x00 ||
channel[0x817] != 0x00 ||
channel[0x818] != 0x00 ||
channel[0x819] != 0x00 ||
channel[0x81A] != 0x00 ||
channel[0x81B] != 0x00)
{
case 0x01 when channel[0x814] != 0x00 || // reserved (8 bytes)
channel[0x815] != 0x00 || channel[0x816] != 0x00 || channel[0x817] != 0x00 ||
channel[0x818] != 0x00 || channel[0x819] != 0x00 || channel[0x81A] != 0x00 ||
channel[0x81B] != 0x00:
AaruConsole.DebugWriteLine("CD checksums",
"Mode 1 sector with data in reserved bytes at address: {0:X2}:{1:X2}:{2:X2}",
channel[0x00C], channel[0x00D], channel[0x00E]);
return false;
}
case 0x01:
{
var address = new byte[4];
var data = new byte[2060];
var data2 = new byte[2232];
@@ -279,7 +275,8 @@ public static class CdChecksums
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}",
// channel[0x00C], channel[0x00D], channel[0x00E]);
@@ -311,7 +308,8 @@ public static class CdChecksums
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}",
channel[0x00C], channel[0x00D], channel[0x00E], calculatedEdc, storedEdc);
channel[0x00C], channel[0x00D], channel[0x00E], calculatedEdc,
storedEdc);
return false;
}
@@ -358,17 +356,19 @@ public static class CdChecksums
AaruConsole.DebugWriteLine("CD checksums",
"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;
}
}
default:
AaruConsole.DebugWriteLine("CD checksums", "Unknown mode {0} sector at address: {1:X2}:{2:X2}:{3:X2}",
channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]);
return null;
}
}
static uint ComputeEdc(uint edc, IReadOnlyList<byte> src, int size)
{

View File

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

View File

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