CRC basic library

Dependents:   D7A_1x_demo_CodeUpgradeProtocol D7A_1x_demo_big_file D7A_1x_demo_sensors_v3 D7A_1x_demo_send_file_data

Committer:
Jeej
Date:
Thu Feb 01 11:41:24 2018 +0000
Revision:
2:88116ae677af
Added fast CRC32 function for CRC calculation by chunks.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jeej 2:88116ae677af 1 // //////////////////////////////////////////////////////////
Jeej 2:88116ae677af 2 // Crc32.h
Jeej 2:88116ae677af 3 // Copyright (c) 2011-2016 Stephan Brumme. All rights reserved.
Jeej 2:88116ae677af 4 // Slicing-by-16 contributed by Bulat Ziganshin
Jeej 2:88116ae677af 5 // Tableless bytewise CRC contributed by Hagai Gold
Jeej 2:88116ae677af 6 // see http://create.stephan-brumme.com/disclaimer.html
Jeej 2:88116ae677af 7 //
Jeej 2:88116ae677af 8
Jeej 2:88116ae677af 9 // if running on an embedded system, you might consider shrinking the
Jeej 2:88116ae677af 10 // big Crc32Lookup table by undefining these lines:
Jeej 2:88116ae677af 11 #define CRC32_USE_LOOKUP_TABLE_BYTE
Jeej 2:88116ae677af 12 //#define CRC32_USE_LOOKUP_TABLE_SLICING_BY_4
Jeej 2:88116ae677af 13 //#define CRC32_USE_LOOKUP_TABLE_SLICING_BY_8
Jeej 2:88116ae677af 14 //#define CRC32_USE_LOOKUP_TABLE_SLICING_BY_16
Jeej 2:88116ae677af 15 // - crc32_bitwise doesn't need it at all
Jeej 2:88116ae677af 16 // - crc32_halfbyte has its own small lookup table
Jeej 2:88116ae677af 17 // - crc32_1byte_tableless and crc32_1byte_tableless2 don't need it at all
Jeej 2:88116ae677af 18 // - crc32_1byte needs only Crc32Lookup[0]
Jeej 2:88116ae677af 19 // - crc32_4bytes needs only Crc32Lookup[0..3]
Jeej 2:88116ae677af 20 // - crc32_8bytes needs only Crc32Lookup[0..7]
Jeej 2:88116ae677af 21 // - crc32_4x8bytes needs only Crc32Lookup[0..7]
Jeej 2:88116ae677af 22 // - crc32_16bytes needs all of Crc32Lookup
Jeej 2:88116ae677af 23 // using the aforementioned #defines the the table is automatically fitted to your needs
Jeej 2:88116ae677af 24
Jeej 2:88116ae677af 25 // uint8_t, uint32_t, int32_t
Jeej 2:88116ae677af 26 #include <stdint.h>
Jeej 2:88116ae677af 27 // size_t
Jeej 2:88116ae677af 28 #include <stddef.h>
Jeej 2:88116ae677af 29
Jeej 2:88116ae677af 30 // crc32_fast selects the fastest algorithm depending on flags (CRC32_USE_LOOKUP_...)
Jeej 2:88116ae677af 31 /// compute CRC32 using the fastest algorithm for large datasets on modern CPUs
Jeej 2:88116ae677af 32 uint32_t crc32_fast(const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 33
Jeej 2:88116ae677af 34 /// compute CRC32 (bitwise algorithm)
Jeej 2:88116ae677af 35 uint32_t crc32_bitwise (const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 36 /// compute CRC32 (half-byte algoritm)
Jeej 2:88116ae677af 37 uint32_t crc32_halfbyte(const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 38
Jeej 2:88116ae677af 39 #ifdef CRC32_USE_LOOKUP_TABLE_BYTE
Jeej 2:88116ae677af 40 /// compute CRC32 (standard algorithm)
Jeej 2:88116ae677af 41 uint32_t crc32_1byte (const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 42 #endif
Jeej 2:88116ae677af 43
Jeej 2:88116ae677af 44 /// compute CRC32 (byte algorithm) without lookup tables
Jeej 2:88116ae677af 45 uint32_t crc32_1byte_tableless (const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 46 /// compute CRC32 (byte algorithm) without lookup tables
Jeej 2:88116ae677af 47 uint32_t crc32_1byte_tableless2(const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 48
Jeej 2:88116ae677af 49 #ifdef CRC32_USE_LOOKUP_TABLE_SLICING_BY_4
Jeej 2:88116ae677af 50 /// compute CRC32 (Slicing-by-4 algorithm)
Jeej 2:88116ae677af 51 uint32_t crc32_4bytes (const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 52 #endif
Jeej 2:88116ae677af 53
Jeej 2:88116ae677af 54 #ifdef CRC32_USE_LOOKUP_TABLE_SLICING_BY_8
Jeej 2:88116ae677af 55 /// compute CRC32 (Slicing-by-8 algorithm)
Jeej 2:88116ae677af 56 uint32_t crc32_8bytes (const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 57 /// compute CRC32 (Slicing-by-8 algorithm), unroll inner loop 4 times
Jeej 2:88116ae677af 58 uint32_t crc32_4x8bytes(const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 59 #endif
Jeej 2:88116ae677af 60
Jeej 2:88116ae677af 61 #ifdef CRC32_USE_LOOKUP_TABLE_SLICING_BY_16
Jeej 2:88116ae677af 62 /// compute CRC32 (Slicing-by-16 algorithm)
Jeej 2:88116ae677af 63 uint32_t crc32_16bytes (const void* data, size_t length, uint32_t previousCrc32 = 0);
Jeej 2:88116ae677af 64 /// compute CRC32 (Slicing-by-16 algorithm, prefetch upcoming data blocks)
Jeej 2:88116ae677af 65 uint32_t crc32_16bytes_prefetch(const void* data, size_t length, uint32_t previousCrc32 = 0, size_t prefetchAhead = 256);
Jeej 2:88116ae677af 66 #endif