xref: /isa-l/crc/crc64_ref.h (revision d3cfb2fb772e375cf2007e484e0a6ec0c6a7c993)
1 /**********************************************************************
2   Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #ifndef _CRC64_REF_H
31 #define _CRC64_REF_H
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #include "crc64.h"
38 
39 #ifdef _MSC_VER
40 # define inline __inline
41 #endif
42 
43 #define MAX_ITER	8
44 
45 // crc64_ecma reference function, slow crc64 from the definition.
46 static inline uint64_t crc64_ecma_refl_ref(uint64_t seed, const uint8_t * buf, uint64_t len)
47 {
48 	uint64_t rem = ~seed;
49 	unsigned int i, j;
50 
51 	uint64_t poly = 0xC96C5795D7870F42ULL;	// ECMA-182 standard reflected
52 
53 	for (i = 0; i < len; i++) {
54 		rem = rem ^ (uint64_t) buf[i];
55 		for (j = 0; j < MAX_ITER; j++) {
56 			rem = (rem & 0x1ULL ? poly : 0) ^ (rem >> 1);
57 		}
58 	}
59 	return ~rem;
60 }
61 
62 static inline uint64_t crc64_ecma_norm_ref(uint64_t seed, const uint8_t * buf, uint64_t len)
63 {
64 	uint64_t rem = ~seed;
65 	unsigned int i, j;
66 
67 	uint64_t poly = 0x42F0E1EBA9EA3693ULL;	// ECMA-182 standard
68 
69 	for (i = 0; i < len; i++) {
70 		rem = rem ^ ((uint64_t) buf[i] << 56);
71 		for (j = 0; j < MAX_ITER; j++) {
72 			rem = (rem & 0x8000000000000000ULL ? poly : 0) ^ (rem << 1);
73 		}
74 	}
75 	return ~rem;
76 }
77 
78 // crc64_iso reference function, slow crc64 from the definition.
79 static inline uint64_t crc64_iso_refl_ref(uint64_t seed, const uint8_t * buf, uint64_t len)
80 {
81 	uint64_t rem = ~seed;
82 	unsigned int i, j;
83 
84 	uint64_t poly = 0xD800000000000000ULL;	// ISO standard reflected
85 
86 	for (i = 0; i < len; i++) {
87 		rem = rem ^ (uint64_t) buf[i];
88 		for (j = 0; j < MAX_ITER; j++) {
89 			rem = (rem & 0x1ULL ? poly : 0) ^ (rem >> 1);
90 		}
91 	}
92 	return ~rem;
93 }
94 
95 static inline uint64_t crc64_iso_norm_ref(uint64_t seed, const uint8_t * buf, uint64_t len)
96 {
97 	uint64_t rem = ~seed;
98 	unsigned int i, j;
99 
100 	uint64_t poly = 0x000000000000001BULL;	// ISO standard
101 
102 	for (i = 0; i < len; i++) {
103 		rem = rem ^ ((uint64_t) buf[i] << 56);
104 		for (j = 0; j < MAX_ITER; j++) {
105 			rem = (rem & 0x8000000000000000ULL ? poly : 0) ^ (rem << 1);
106 		}
107 	}
108 	return ~rem;
109 }
110 
111 // crc64_jones reference function, slow crc64 from the definition.
112 static inline uint64_t crc64_jones_refl_ref(uint64_t seed, const uint8_t * buf, uint64_t len)
113 {
114 	uint64_t rem = ~seed;
115 	unsigned int i, j;
116 
117 	uint64_t poly = 0x95ac9329ac4bc9b5ULL;	// Jones coefficients reflected
118 
119 	for (i = 0; i < len; i++) {
120 		rem = rem ^ (uint64_t) buf[i];
121 		for (j = 0; j < MAX_ITER; j++) {
122 			rem = (rem & 0x1ULL ? poly : 0) ^ (rem >> 1);
123 		}
124 	}
125 	return ~rem;
126 }
127 
128 static inline uint64_t crc64_jones_norm_ref(uint64_t seed, const uint8_t * buf, uint64_t len)
129 {
130 	uint64_t rem = ~seed;
131 	unsigned int i, j;
132 
133 	uint64_t poly = 0xad93d23594c935a9ULL;	// Jones coefficients
134 
135 	for (i = 0; i < len; i++) {
136 		rem = rem ^ ((uint64_t) buf[i] << 56);
137 		for (j = 0; j < MAX_ITER; j++) {
138 			rem = (rem & 0x8000000000000000ULL ? poly : 0) ^ (rem << 1);
139 		}
140 	}
141 	return ~rem;
142 }
143 
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif
149