Skip to content

Instantly share code, notes, and snippets.

@gizmo98
Last active September 5, 2016 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gizmo98/8a83aef820f1e6d15da4a4baf3b7e0dc to your computer and use it in GitHub Desktop.
Save gizmo98/8a83aef820f1e6d15da4a4baf3b7e0dc to your computer and use it in GitHub Desktop.
// ok --> "gcc crctest.c -o testapp -std=c11 -march=armv8-a+crc -mfpu=crypto-neon-fp-armv8"
// fails --> "gcc crctest.c -o testapp -std=c11 -mcpu=cortex-a53 -mfpu=crypto-neon-fp-armv8"
#include <stdio.h>
#include <math.h>
#include <arm_acle.h>
#include <time.h>
unsigned int CRCTable[ 256 ];
typedef unsigned long long u64;
typedef signed long long s64;
typedef unsigned int u32;
typedef unsigned int uint32_t;
typedef unsigned short u16;
typedef unsigned short uint16_t;
typedef unsigned char u8;
typedef unsigned char uint8_t;
#define CRC32_POLYNOMIAL 0x04C11DB7
unsigned int CRCTable[ 256 ];
u32 Reflect( u32 ref, char ch )
{
u32 value = 0;
// Swap bit 0 for bit 7
// bit 1 for bit 6, etc.
for (int i = 1; i < (ch + 1); ++i) {
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
return value;
}
void CRC_BuildTable()
{
u32 crc;
for (int i = 0; i < 256; ++i) {
crc = Reflect( i, 8 ) << 24;
for (int j = 0; j < 8; ++j)
crc = (crc << 1) ^ (crc & (1 << 31) ? CRC32_POLYNOMIAL : 0);
CRCTable[i] = Reflect( crc, 32 );
}
}
u32 CRC_Calculate( u32 crc, const void * buffer, u32 count )
{
u8 *p;
u32 orig = crc;
p = (u8*) buffer;
while (count--)
crc = (crc >> 8) ^ CRCTable[(crc & 0xFF) ^ *p++];
return crc ^ orig;
}
u32 CRC_CalculateARM( u32 crc, const void * buffer, u32 count )
{
u8 *p;
u32 orig = crc;
p = (u8*) buffer;
while (count >= 8) {
crc = __crc32d(crc, *((u64*)p));//(uint64_t)(p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24) << 32 | (p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24) );
p += 8;
count -=8;
}
if (count >= 4) {
crc = __crc32w(crc, *((u32*)p));//(p[0] | p[1] << 8) );
p += 4;
count -= 4;
}
if (count >= 2) {
crc = __crc32h(crc, *((u16*)p));//(p[0] | p[1] << 8) );
p += 2;
count -=2;
}
if (count == 1)
crc = __crc32b(crc, *p);
return crc ^ orig;
}
u32 CRC_CalculatePaletteARM(u32 crc, const void * buffer, u32 count )
{
u8 *p;
u32 orig = crc;
p = (u8*) buffer;
while (count--) {
crc = __crc32h(crc, *((u32*)p));
p += 8;
}
return crc ^ orig;
}
u32 CRC_CalculatePalette(u32 crc, const void * buffer, u32 count )
{
u8 *p;
u32 orig = crc;
p = (u8*) buffer;
while (count--) {
crc = (crc >> 8) ^ CRCTable[(crc & 0xFF) ^ *p++];
crc = (crc >> 8) ^ CRCTable[(crc & 0xFF) ^ *p++];
p += 6;
}
return crc ^ orig;
}
int main(void)
{
const u32 size = 1024;
u8 bytes[size];
int i = 0;
u32 old_crc = 0xFFFFFFFF;
u32 new_crc = 0;
CRC_BuildTable();
for(int j = 0;j<100;j++)
{
for(int i = 0 ;i<size;i++)
{
bytes[i] = (((i *i)-2*i+8)*j);
//printf ("%x ",bytes[i]);
}
//CRC_BuildTable();
new_crc = CRC_Calculate(0xFFFFFFFF,&bytes,1024);
if (old_crc == new_crc)
printf("match\r\n");
// printf("%u %u\r\n",new_crc, CRC_Calculate_ARM(0xFFFFFFFF,&bytes,1024));
old_crc=new_crc;
//printf("%u\r\n",crc32(0xFFFFFFFF,&bytes,size));
}
clock_t begin = clock();
for(int k=0; k<50000;k++){
new_crc = CRC_Calculate(0xFFFFFFFF,&bytes,1017);
}
clock_t end = clock();
double time_spent = (double)(end - begin);
printf("%f %x\r\n",time_spent,new_crc);
new_crc=0;
begin = clock();
for(int k=0; k<50000;k++){
new_crc = CRC_CalculateARM(0xFFFFFFFF,&bytes,1017);
}
end = clock();
time_spent = (double)(end - begin);
printf("%f %x\r\n",time_spent, new_crc);
printf("test\r\n");
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment