Skip to content

Commit

Permalink
crc64: add jones and iso format, crc64 code clean
Browse files Browse the repository at this point in the history
1. Add normal and reflected bits order functions for ISO format and
   Jones coefficients format.
2. Add a multi-binary macro for crc64 functions.
3. In order to decrease number of repeated test.c and perf.c files,
   using crc64_funcs_test.c and cr crc64_funcs_perf.c.
4. Add crc64_example.c to take the demonstration role.

Change-Id: Icb8c14f1a84cd98f58eb12206ca605dea8a2cefb
Signed-off-by: Xiaodong Liu <[email protected]>
  • Loading branch information
dong-liuliu authored and gbtucker committed Dec 6, 2016
1 parent 90f0ea9 commit 3d66317
Show file tree
Hide file tree
Showing 13 changed files with 2,737 additions and 345 deletions.
10 changes: 7 additions & 3 deletions crc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ lsrc += \
crc/crc64_multibinary.asm \
crc/crc64_ecma_refl_by8.asm \
crc/crc64_ecma_norm_by8.asm \
crc/crc64_iso_refl_by8.asm \
crc/crc64_iso_norm_by8.asm \
crc/crc64_jones_refl_by8.asm \
crc/crc64_jones_norm_by8.asm \
crc/crc64_base.c \
crc/crc_multibinary.asm \
crc/crc_base.c
Expand All @@ -47,9 +51,9 @@ extern_hdrs += include/crc.h include/crc64.h
other_src += include/reg_sizes.asm include/types.h include/test.h

check_tests += crc/crc16_t10dif_test crc/crc32_ieee_test crc/crc32_iscsi_test \
crc/crc64_ecma_refl_test crc/crc64_ecma_norm_test
crc/crc64_funcs_test

perf_tests += crc/crc16_t10dif_perf crc/crc32_ieee_perf crc/crc32_iscsi_perf \
crc/crc64_ecma_refl_perf crc/crc64_ecma_norm_perf
crc/crc64_funcs_perf

examples += crc/crc_simple_test
examples += crc/crc_simple_test crc/crc64_example
84 changes: 82 additions & 2 deletions crc/crc64_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

// crc64_ecma baseline function
// Slow crc64 from the definition. Can be sped up with a lookup table.
uint64_t crc64_ecma_refl_base(uint64_t seed, uint8_t * buf, uint64_t len)
uint64_t crc64_ecma_refl_base(uint64_t seed, const uint8_t * buf, uint64_t len)
{
uint64_t rem = ~seed;
unsigned int i, j;
Expand All @@ -50,7 +50,7 @@ uint64_t crc64_ecma_refl_base(uint64_t seed, uint8_t * buf, uint64_t len)
return ~rem;
}

uint64_t crc64_ecma_norm_base(uint64_t seed, uint8_t * buf, uint64_t len)
uint64_t crc64_ecma_norm_base(uint64_t seed, const uint8_t * buf, uint64_t len)
{
uint64_t rem = ~seed;
unsigned int i, j;
Expand All @@ -66,6 +66,74 @@ uint64_t crc64_ecma_norm_base(uint64_t seed, uint8_t * buf, uint64_t len)
return ~rem;
}

// crc64_iso baseline function
// Slow crc64 from the definition. Can be sped up with a lookup table.
uint64_t crc64_iso_refl_base(uint64_t seed, const uint8_t * buf, uint64_t len)
{
uint64_t rem = ~seed;
unsigned int i, j;

uint64_t poly = 0xD800000000000000ULL; // ISO standard reflected

for (i = 0; i < len; i++) {
rem = rem ^ (uint64_t) buf[i];
for (j = 0; j < MAX_ITER; j++) {
rem = (rem & 0x1ULL ? poly : 0) ^ (rem >> 1);
}
}
return ~rem;
}

uint64_t crc64_iso_norm_base(uint64_t seed, const uint8_t * buf, uint64_t len)
{
uint64_t rem = ~seed;
unsigned int i, j;

uint64_t poly = 0x000000000000001BULL; // ISO standard

for (i = 0; i < len; i++) {
rem = rem ^ ((uint64_t) buf[i] << 56);
for (j = 0; j < MAX_ITER; j++) {
rem = (rem & 0x8000000000000000ULL ? poly : 0) ^ (rem << 1);
}
}
return ~rem;
}

// crc64_jones baseline function
// Slow crc64 from the definition. Can be sped up with a lookup table.
uint64_t crc64_jones_refl_base(uint64_t seed, const uint8_t * buf, uint64_t len)
{
uint64_t rem = ~seed;
unsigned int i, j;

uint64_t poly = 0x95ac9329ac4bc9b5ULL; // Jones coefficients reflected

for (i = 0; i < len; i++) {
rem = rem ^ (uint64_t) buf[i];
for (j = 0; j < MAX_ITER; j++) {
rem = (rem & 0x1ULL ? poly : 0) ^ (rem >> 1);
}
}
return ~rem;
}

uint64_t crc64_jones_norm_base(uint64_t seed, const uint8_t * buf, uint64_t len)
{
uint64_t rem = ~seed;
unsigned int i, j;

uint64_t poly = 0xad93d23594c935a9ULL; // Jones coefficients

for (i = 0; i < len; i++) {
rem = rem ^ ((uint64_t) buf[i] << 56);
for (j = 0; j < MAX_ITER; j++) {
rem = (rem & 0x8000000000000000ULL ? poly : 0) ^ (rem << 1);
}
}
return ~rem;
}

struct slver {
unsigned short snum;
unsigned char ver;
Expand All @@ -77,3 +145,15 @@ struct slver crc64_ecma_refl_base_slver = { 0x001c, 0x00, 0x00 };

struct slver crc64_ecma_norm_base_slver_00000019;
struct slver crc64_ecma_norm_base_slver = { 0x0019, 0x00, 0x00 };

struct slver crc64_iso_refl_base_slver_00000022;
struct slver crc64_iso_refl_base_slver = { 0x0022, 0x00, 0x00 };

struct slver crc64_iso_norm_base_slver_0000001f;
struct slver crc64_iso_norm_base_slver = { 0x001f, 0x00, 0x00 };

struct slver crc64_jones_refl_base_slver_00000028;
struct slver crc64_jones_refl_base_slver = { 0x0028, 0x00, 0x00 };

struct slver crc64_jones_norm_base_slver_00000025;
struct slver crc64_jones_norm_base_slver = { 0x0025, 0x00, 0x00 };
174 changes: 0 additions & 174 deletions crc/crc64_ecma_refl_test.c

This file was deleted.

66 changes: 23 additions & 43 deletions crc/crc64_ecma_refl_perf.c → crc/crc64_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,63 +26,43 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/time.h>
#include <assert.h>
#include "crc64.h"
#include "test.h"

//#define CACHED_TEST
#ifdef CACHED_TEST
// Cached test, loop many times over small dataset
# define TEST_LEN 8*1024
# define TEST_LOOPS 400000
# define TEST_TYPE_STR "_warm"
#else
// Uncached test. Pull from large mem base.
# define GT_L3_CACHE 32*1024*1024 /* some number > last level cache */
# define TEST_LEN (2 * GT_L3_CACHE)
# define TEST_LOOPS 100
# define TEST_TYPE_STR "_cold"
#endif

#ifndef TEST_SEED
# define TEST_SEED 0x1234
#endif

#define TEST_MEM TEST_LEN
#define BUF_SIZE 8192
#define INIT_SEED 0x12345678

int main(int argc, char *argv[])
{
int i;
void *buf;
uint64_t crc;
struct perf start, stop;
uint8_t inbuf[BUF_SIZE];
uint64_t avail_in, total_in = 0;
uint64_t crc64_checksum;
FILE *in;

printf("crc64_ecma_refl_perf:\n");

if (posix_memalign(&buf, 1024, TEST_LEN)) {
printf("alloc error: Fail");
return -1;
if (argc != 2) {
fprintf(stderr, "Usage: crc64_example infile\n");
exit(0);
}
in = fopen(argv[1], "rb");
if (!in) {
fprintf(stderr, "Can't open %s for reading\n", argv[1]);
exit(0);
}
memset(buf, (char)TEST_SEED, TEST_LEN);

printf("Start timed tests\n");
printf("crc64_example -- crc64_ecma_refl:\n");
fflush(0);

crc = crc64_ecma_refl(TEST_SEED, buf, TEST_LEN);
perf_start(&start);
for (i = 0; i < TEST_LOOPS; i++) {
crc = crc64_ecma_refl(TEST_SEED, buf, TEST_LEN);
crc64_checksum = INIT_SEED;
while ((avail_in = fread(inbuf, 1, BUF_SIZE, in))) {
// crc update mode
crc64_checksum = crc64_ecma_refl(crc64_checksum, inbuf, avail_in);
total_in += avail_in;
}
perf_stop(&stop);
printf("crc64_ecma_refl" TEST_TYPE_STR ": ");
perf_print(stop, start, (long long)TEST_LEN * i);

printf("finish 0x%lx\n", crc);
fclose(in);
printf("total length is %ld, checksum is 0x%lx\n", total_in, crc64_checksum);

return 0;
}
Loading

0 comments on commit 3d66317

Please sign in to comment.