Use unsafe blocks in project

Memory pointer access is needed to improve performance
with basic input report copying as well as CRC32
validation
This commit is contained in:
Travis Nickles 2018-03-05 08:57:30 -06:00
parent f5f6002cdc
commit 8be2f9f271
3 changed files with 92 additions and 77 deletions

View File

@ -276,7 +276,7 @@ namespace DS4Windows
return crc; return crc;
} }
public static uint CalculateFasterBTHash(ref uint seed, ref byte[] buffer, ref int start, ref int size) public static unsafe uint CalculateFasterBTHash(ref uint seed, ref byte[] buffer, ref int start, ref int size)
{ {
/*uint crc = seed; /*uint crc = seed;
for (int i = start; i < size + start; i++) for (int i = start; i < size + start; i++)
@ -288,24 +288,26 @@ namespace DS4Windows
int i = start; int i = start;
int bufsize = size; int bufsize = size;
//while (bufsize >= 16) //while (bufsize >= 16)
fixed (byte* byteP = buffer)
{
for (int j = 0; j < 4; j++) for (int j = 0; j < 4; j++)
{ {
uint one = (buffer[i++] | uint one = (byteP[i++] |
(uint)(buffer[i++] << 8) | (uint)(byteP[i++] << 8) |
(uint)(buffer[i++] << 16) | (uint)(byteP[i++] << 16) |
(uint)(buffer[i++] << 24)) ^ crc; (uint)(byteP[i++] << 24)) ^ crc;
uint two = buffer[i++] | uint two = byteP[i++] |
(uint)(buffer[i++] << 8) | (uint)(byteP[i++] << 8) |
(uint)(buffer[i++] << 16) | (uint)(byteP[i++] << 16) |
(uint)(buffer[i++] << 24); (uint)(byteP[i++] << 24);
uint three = (buffer[i++] | uint three = (byteP[i++] |
(uint)(buffer[i++] << 8) | (uint)(byteP[i++] << 8) |
(uint)(buffer[i++] << 16) | (uint)(byteP[i++] << 16) |
(uint)(buffer[i++] << 24)); (uint)(byteP[i++] << 24));
uint four = buffer[i++] | uint four = byteP[i++] |
(uint)(buffer[i++] << 8) | (uint)(byteP[i++] << 8) |
(uint)(buffer[i++] << 16) | (uint)(byteP[i++] << 16) |
(uint)(buffer[i++] << 24); (uint)(byteP[i++] << 24);
crc = secondLook[15, one & 0xFF] ^ crc = secondLook[15, one & 0xFF] ^
secondLook[14, (one >> 8) & 0xFF] ^ secondLook[14, (one >> 8) & 0xFF] ^
@ -330,14 +332,14 @@ namespace DS4Windows
//while (bufsize >= 8) //while (bufsize >= 8)
//if (bufsize >= 8) //if (bufsize >= 8)
uint one8 = (buffer[i++] | uint one8 = (byteP[i++] |
(uint)(buffer[i++] << 8) | (uint)(byteP[i++] << 8) |
(uint)(buffer[i++] << 16) | (uint)(byteP[i++] << 16) |
(uint)(buffer[i++] << 24)) ^ crc; (uint)(byteP[i++] << 24)) ^ crc;
uint two8 = buffer[i++] | uint two8 = byteP[i++] |
(uint)(buffer[i++] << 8) | (uint)(byteP[i++] << 8) |
(uint)(buffer[i++] << 16) | (uint)(byteP[i++] << 16) |
(uint)(buffer[i++] << 24); (uint)(byteP[i++] << 24);
crc = secondLook[7, one8 & 0xFF] ^ crc = secondLook[7, one8 & 0xFF] ^
secondLook[6, (one8 >> 8) & 0xFF] ^ secondLook[6, (one8 >> 8) & 0xFF] ^
secondLook[5, (one8 >> 16) & 0xFF] ^ secondLook[5, (one8 >> 16) & 0xFF] ^
@ -363,9 +365,10 @@ namespace DS4Windows
//while (--bufsize >= 0) //while (--bufsize >= 0)
//{ //{
crc = (crc >> 8) ^ defaultTable[(crc & 0xFF) ^ buffer[i++]];// i++; crc = (crc >> 8) ^ defaultTable[(crc & 0xFF) ^ byteP[i++]];// i++;
crc = (crc >> 8) ^ defaultTable[(crc & 0xFF) ^ buffer[i++]];// i++; crc = (crc >> 8) ^ defaultTable[(crc & 0xFF) ^ byteP[i++]];// i++;
//} //}
}
return crc; return crc;
} }

View File

@ -697,7 +697,7 @@ namespace DS4Windows
const uint DefaultPolynomial = 0xedb88320u; const uint DefaultPolynomial = 0xedb88320u;
uint HamSeed = 2351727372; uint HamSeed = 2351727372;
private void performDs4Input() private unsafe void performDs4Input()
{ {
firstActive = DateTime.UtcNow; firstActive = DateTime.UtcNow;
NativeMethods.HidD_SetNumInputBuffers(hDevice.safeReadHandle.DangerousGetHandle(), 2); NativeMethods.HidD_SetNumInputBuffers(hDevice.safeReadHandle.DangerousGetHandle(), 2);
@ -749,7 +749,18 @@ namespace DS4Windows
timeoutEvent = false; timeoutEvent = false;
if (res == HidDevice.ReadStatus.Success) if (res == HidDevice.ReadStatus.Success)
{ {
Array.Copy(btInputReport, 2, inputReport, 0, inputReport.Length); //Array.Copy(btInputReport, 2, inputReport, 0, inputReport.Length);
fixed (byte* byteP = &btInputReport[2], imp = inputReport)
{
byte* btImp = byteP;
byte* finImp = imp;
for (int j = 0; j < BT_INPUT_REPORT_LENGTH-2;j++)
{
finImp[j] = btImp[j];
//*finImp = *btImp;
//btImp++; finImp++;
}
}
//uint recvCrc32 = BitConverter.ToUInt32(btInputReport, BT_INPUT_REPORT_CRC32_POS); //uint recvCrc32 = BitConverter.ToUInt32(btInputReport, BT_INPUT_REPORT_CRC32_POS);
uint recvCrc32 = btInputReport[BT_INPUT_REPORT_CRC32_POS] | uint recvCrc32 = btInputReport[BT_INPUT_REPORT_CRC32_POS] |

View File

@ -92,6 +92,7 @@
<GenerateSerializationAssemblies>On</GenerateSerializationAssemblies> <GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>
<LangVersion>6</LangVersion> <LangVersion>6</LangVersion>
<DebugSymbols>false</DebugSymbols> <DebugSymbols>false</DebugSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>