Performance analysis of CRC computing

Time:2020-10-21

Introduction to test environment

CPU model: mpc8548mghatgb, working frequency is 1GHz

Camera data: 280mbps

Take 10 W CRC calculation as an example

#define COUNT 100000
#define SIZE 1024

Original calculation method

__u16 crc_itu_t_byte(__u16 crc, const __u8 data)
{
   return (crc << 8) ^ crc_itu_t_table[((crc >> 8) ^ data) & 0xff];
}

__u16 gCrc16(__u16 crc, const __u8 *buffer, __u32 len)
{
   while((len > 0) && (buffer != NULL))
   {
      crc = crc_itu_t_byte(crc, *buffer);
      buffer = buffer + 1;

      len = len - 1;
   }

   return crc;
}

Test time

old_tv tv_sec 4404 tv_usec 74838
tv tv_sec 4409 tv_usec 856335
tv_usec 5781497

Optimization method 1

Compiler optimization options

CFLAGS := -O2

Test time

old_tv tv_sec 4450 tv_usec 352077
tv tv_sec 4451 tv_usec 489980
tv_usec 1137903

Optimization mode 2

Remove pointer judgment

__u16 gCrc16(__u16 crc, const __u8 *buffer, __u32 len)
{
   while(len > 0)
   {
      crc = crc_itu_t_byte(crc, *buffer);
      buffer = buffer + 1;

      len = len - 1;
   }

   return crc;
}

Test time

old_tv tv_sec 4487 tv_usec 717217
tv tv_sec 4488 tv_usec 647959
tv_usec 930742

Optimization mode 3

Use the inline keyword

inline __u16 gCrc16(__u16 crc, const __u8 *buffer, __u32 len)
{
   while(len > 0)
   {
      crc = crc_itu_t_byte(crc, *buffer);
      buffer = buffer + 1;

      len = len - 1;
   }

   return crc;
}

Test time

old_tv tv_sec 4603 tv_usec 798503
tv tv_sec 4604 tv_usec 6208
tv_usec 207705

CRC used 64K look-up table method

WORD LibCrc16_2(const BYTE *pbyData, DWORD dwSize)
{
   WORD wCRC = 0x0000;
   DWORD dwIndex;
   WORD wValue;
   DWORD dwLen = (dwSize >> 1) << 1;

   for(dwIndex = 0; dwIndex < dwLen; dwIndex += 2)
   {
      wValue = pbyData[dwIndex] << 8 | pbyData[dwIndex + 1];
      wCRC = g_wLibCrc16_2_Table[wCRC ^ wValue];
   }

   dwLen = dwSize % 2;

   if(dwLen == 1)
   {
      wCRC = (wCRC << 8) ^ g_wLibCrc16Table[((wCRC >> 8) & 0xff) ^ pbyData[dwSize - 1]];
   }

   return wCRC;
}

Test time

old_tv tv_sec 4731 tv_usec 352502
tv tv_sec 4731 tv_usec 807167
tv_usec 454665