yencode 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/binding.gyp CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
+ "variables": {
3
+ "enable_native_tuning%": 1,
4
+ "disable_avx256%": 0
5
+ },
2
6
  "target_defaults": {
3
- "variables": {
4
- "enable_native_tuning%": 1,
5
- "disable_avx256%": 0
6
- },
7
7
  "conditions": [
8
8
  ['target_arch=="ia32"', {
9
9
  "msvs_settings": {"VCCLCompilerTool": {"EnableEnhancedInstructionSet": "2"}}
@@ -41,7 +41,7 @@
41
41
  ['disable_avx256!=0', {
42
42
  "defines": ["YENC_DISABLE_AVX256=1"]
43
43
  }],
44
- ['enable_native_tuning!=0', {
44
+ ['OS!="win" and enable_native_tuning!=0', {
45
45
  "defines": ["YENC_BUILD_NATIVE=1"]
46
46
  }]
47
47
  ],
@@ -128,7 +128,7 @@
128
128
  "target_name": "yencode_clmul",
129
129
  "type": "static_library",
130
130
  "sources": [
131
- "src/crc_folding.c"
131
+ "src/crc_folding.cc"
132
132
  ],
133
133
  "cflags!": ["-fno-omit-frame-pointer", "-fno-tree-vrp", "-fno-strict-aliasing"],
134
134
  "cxxflags!": ["-fno-omit-frame-pointer", "-fno-tree-vrp", "-fno-strict-aliasing"],
@@ -16,7 +16,7 @@
16
16
 
17
17
  #include "generic_crc.h"
18
18
 
19
- #if CRCUTIL_USE_ASM && HAVE_I386 && HAVE_MMX
19
+ #if CRCUTIL_USE_ASM && HAVE_I386 && HAVE_MMX && !defined(_MSC_VER) && !(defined(__GNUC__) && !defined(__clang__))
20
20
 
21
21
  namespace crcutil {
22
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yencode",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "SIMD accelerated yEnc encoder/decoder and CRC32 calculator",
5
5
  "keywords": [
6
6
  "yenc",
package/src/common.h CHANGED
@@ -8,7 +8,7 @@
8
8
  defined(__LP64 ) || \
9
9
  defined(_M_X64 ) || \
10
10
  defined(_M_AMD64 ) || \
11
- defined(_WIN64 )
11
+ (defined(_WIN64) && !defined(_M_ARM64))
12
12
  #define PLATFORM_AMD64 1
13
13
  #endif
14
14
  #if defined(PLATFORM_AMD64) || \
@@ -18,7 +18,7 @@
18
18
  defined(__i686__ ) || \
19
19
  defined(_M_I86 ) || \
20
20
  defined(_M_IX86 ) || \
21
- defined(_WIN32 )
21
+ (defined(_WIN32) && !defined(_M_ARM) && !defined(_M_ARM64))
22
22
  #define PLATFORM_X86 1
23
23
  #endif
24
24
  #if defined(__aarch64__) || \
@@ -35,23 +35,25 @@
35
35
  #endif
36
36
 
37
37
 
38
- #if defined(__cplusplus) && __cplusplus > 201100 && !(defined(_MSC_VER) && defined(__clang__)) && !defined(__APPLE__)
38
+ #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
39
+ #include <stdlib.h> // MSVC ARM64 seems to need this
40
+ #define ALIGN_ALLOC(buf, len, align) *(void**)&(buf) = _aligned_malloc((len), align)
41
+ #define ALIGN_FREE _aligned_free
42
+ #elif defined(__cplusplus) && __cplusplus >= 201100 && !(defined(_MSC_VER) && (defined(__clang__) || defined(_M_ARM64) || defined(_M_ARM))) && !defined(__APPLE__)
39
43
  // C++11 method
40
44
  // len needs to be a multiple of alignment, although it sometimes works if it isn't...
41
45
  #include <cstdlib>
42
46
  #define ALIGN_ALLOC(buf, len, align) *(void**)&(buf) = aligned_alloc(align, ((len) + (align)-1) & ~((align)-1))
43
47
  #define ALIGN_FREE free
44
- #elif defined(_MSC_VER)
45
- #define ALIGN_ALLOC(buf, len, align) *(void**)&(buf) = _aligned_malloc((len), align)
46
- #define ALIGN_FREE _aligned_free
47
48
  #else
49
+ #include <stdlib.h>
48
50
  #define ALIGN_ALLOC(buf, len, align) if(posix_memalign((void**)&(buf), align, (len))) (buf) = NULL
49
51
  #define ALIGN_FREE free
50
52
  #endif
51
53
 
52
54
 
53
55
  // MSVC compatibility
54
- #if (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(_M_X64)
56
+ #if ((defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(_M_X64)) && !defined(__clang__)
55
57
  #define __SSE2__ 1
56
58
  #define __SSSE3__ 1
57
59
  #define __SSE4_1__ 1
@@ -85,12 +87,16 @@
85
87
  #define __ARM_NEON 1
86
88
  #endif
87
89
  #if defined(_M_ARM)
88
- /*#define __ARM_NEON 1*/
90
+ #define __ARM_NEON 1
89
91
  #endif
90
92
  #ifdef _MSC_VER
91
- #define __BYTE_ORDER__ 1234
92
- #define __ORDER_BIG_ENDIAN__ 4321
93
- #include <intrin.h>
93
+ # ifndef __BYTE_ORDER__
94
+ # define __BYTE_ORDER__ 1234
95
+ # endif
96
+ # ifndef __ORDER_BIG_ENDIAN__
97
+ # define __ORDER_BIG_ENDIAN__ 4321
98
+ # endif
99
+ # include <intrin.h>
94
100
  #endif
95
101
 
96
102
 
@@ -137,6 +143,59 @@
137
143
 
138
144
  #ifdef __ARM_NEON
139
145
  # include <arm_neon.h>
146
+
147
+ // ARM provides no standard way to inline define a vector :(
148
+ static HEDLEY_ALWAYS_INLINE uint8x8_t vmake_u8(
149
+ uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f, uint8_t g, uint8_t h
150
+ ) {
151
+ # if defined(_MSC_VER)
152
+ uint8_t t[] = {a,b,c,d,e,f,g,h};
153
+ return vld1_u8(t);
154
+ # else
155
+ return (uint8x8_t){a,b,c,d,e,f,g,h};
156
+ # endif
157
+ }
158
+ static HEDLEY_ALWAYS_INLINE uint8x16_t vmakeq_u8(
159
+ uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f, uint8_t g, uint8_t h,
160
+ uint8_t i, uint8_t j, uint8_t k, uint8_t l, uint8_t m, uint8_t n, uint8_t o, uint8_t p
161
+ ) {
162
+ # if defined(_MSC_VER)
163
+ uint8_t t[] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p};
164
+ return vld1q_u8(t);
165
+ # else
166
+ return (uint8x16_t){a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p};
167
+ # endif
168
+ }
169
+ static HEDLEY_ALWAYS_INLINE int8x16_t vmakeq_s8(
170
+ int8_t a, int8_t b, int8_t c, int8_t d, int8_t e, int8_t f, int8_t g, int8_t h,
171
+ int8_t i, int8_t j, int8_t k, int8_t l, int8_t m, int8_t n, int8_t o, int8_t p
172
+ ) {
173
+ # if defined(_MSC_VER)
174
+ int8_t t[] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p};
175
+ return vld1q_s8(t);
176
+ # else
177
+ return (int8x16_t){a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p};
178
+ # endif
179
+ }
180
+
181
+ # ifdef _MSC_VER
182
+ # define _CREATE_TUPLE(type, ...) type{{ __VA_ARGS__ }}
183
+ # else
184
+ # define _CREATE_TUPLE(type, ...) (type){{ __VA_ARGS__ }}
185
+ # endif
186
+ static HEDLEY_ALWAYS_INLINE uint8x16x2_t vcreate2_u8(uint8x16_t a, uint8x16_t b) {
187
+ return _CREATE_TUPLE(uint8x16x2_t, a, b);
188
+ }
189
+ static HEDLEY_ALWAYS_INLINE int8x16x2_t vcreate2_s8(int8x16_t a, int8x16_t b) {
190
+ return _CREATE_TUPLE(int8x16x2_t, a, b);
191
+ }
192
+ static HEDLEY_ALWAYS_INLINE uint8x16x3_t vcreate3_u8(uint8x16_t a, uint8x16_t b, uint8x16_t c) {
193
+ return _CREATE_TUPLE(uint8x16x3_t, a, b, c);
194
+ }
195
+ static HEDLEY_ALWAYS_INLINE uint8x16x4_t vcreate4_u8(uint8x16_t a, uint8x16_t b, uint8x16_t c, uint8x16_t d) {
196
+ return _CREATE_TUPLE(uint8x16x4_t, a, b, c, d);
197
+ }
198
+ # undef _CREATE_TUPLE
140
199
  #endif
141
200
  #ifdef PLATFORM_ARM
142
201
  bool cpu_supports_neon();
@@ -220,10 +279,13 @@ int cpu_supports_isa();
220
279
  #endif
221
280
 
222
281
  // weird thing with Apple's Clang; doesn't seem to always occur, so assume that Clang >= 9 is fine: https://github.com/animetosho/node-yencode/issues/8#issuecomment-583385864
223
- #if defined(__clang__) && defined(__APPLE__) && __clang_major__ < 9
282
+ // seems that Clang < 3.6 also uses the old name
283
+ #if defined(__clang__) && ((defined(__APPLE__) && __clang_major__ < 9) || __clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 6))
224
284
  # define _lzcnt_u32 __lzcnt32
225
285
  #endif
226
286
 
287
+
288
+
227
289
  #ifdef __GNUC__
228
290
  # if __GNUC__ >= 9
229
291
  # define LIKELIHOOD(p, c) (HEDLEY_PREDICT(!!(c), 1, p))
package/src/crc.cc CHANGED
@@ -1,42 +1,57 @@
1
- #include "common.h"
2
1
  #include "crc_common.h"
3
2
 
4
-
5
3
  #include "interface.h"
6
4
  crcutil_interface::CRC* crc = NULL;
7
5
 
8
-
9
- static void do_crc32_generic(const void* data, size_t length, unsigned char out[4]) {
10
- crcutil_interface::UINT64 tmp = 0;
11
- crc->Compute(data, length, &tmp);
12
- UNPACK_4(out, tmp);
13
- }
14
- crc_func _do_crc32 = &do_crc32_generic;
15
-
16
- static void do_crc32_incremental_generic(const void* data, size_t length, unsigned char init[4]) {
17
- crcutil_interface::UINT64 tmp = PACK_4(init);
6
+ static uint32_t do_crc32_incremental_generic(const void* data, size_t length, uint32_t init) {
7
+ crcutil_interface::UINT64 tmp = init;
18
8
  crc->Compute(data, length, &tmp);
19
- UNPACK_4(init, tmp);
9
+ return (uint32_t)tmp;
20
10
  }
21
11
  crc_func _do_crc32_incremental = &do_crc32_incremental_generic;
22
12
 
23
13
 
24
14
 
25
- void do_crc32_combine(unsigned char crc1[4], const unsigned char crc2[4], size_t len2) {
26
- crcutil_interface::UINT64 crc1_ = PACK_4(crc1), crc2_ = PACK_4(crc2);
15
+ uint32_t do_crc32_combine(uint32_t crc1, uint32_t crc2, size_t len2) {
16
+ crcutil_interface::UINT64 crc1_ = crc1, crc2_ = crc2;
27
17
  crc->Concatenate(crc2_, 0, len2, &crc1_);
28
- UNPACK_4(crc1, crc1_);
18
+ return (uint32_t)crc1_;
29
19
  }
30
20
 
31
- void do_crc32_zeros(unsigned char crc1[4], size_t len) {
32
- crcutil_interface::UINT64 crc_ = PACK_4(crc1);
21
+ uint32_t do_crc32_zeros(uint32_t crc1, size_t len) {
22
+ crcutil_interface::UINT64 crc_ = crc1;
33
23
  crc->CrcOfZeroes(len, &crc_);
34
- UNPACK_4(crc1, crc_);
24
+ return (uint32_t)crc_;
35
25
  }
36
26
 
37
- extern "C" void crc_clmul_set_funcs(crc_func*, crc_func*);
38
- void crc_arm_set_funcs(crc_func*, crc_func*);
27
+ void crc_clmul_set_funcs(crc_func*);
28
+ void crc_arm_set_funcs(crc_func*);
39
29
 
30
+ #if defined(PLATFORM_ARM) && defined(_WIN32)
31
+ # define WIN32_LEAN_AND_MEAN
32
+ # include <Windows.h>
33
+ #endif
34
+ #ifdef PLATFORM_ARM
35
+ # ifdef __ANDROID__
36
+ # include <cpu-features.h>
37
+ # elif defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 12)
38
+ # include <sys/auxv.h>
39
+ # include <asm/hwcap.h>
40
+ # elif (defined(__FreeBSD__) && __FreeBSD__ < 12)
41
+ # include <sys/sysctl.h>
42
+ # include <asm/hwcap.h>
43
+ # elif defined(__APPLE__)
44
+ # include <sys/types.h>
45
+ # include <sys/sysctl.h>
46
+ # endif
47
+ # ifdef __FreeBSD__
48
+ static unsigned long getauxval(unsigned long cap) {
49
+ unsigned long ret;
50
+ elf_aux_info(cap, &ret, sizeof(ret));
51
+ return ret;
52
+ }
53
+ # endif
54
+ #endif
40
55
  void crc_init() {
41
56
  crc = crcutil_interface::CRC::Create(
42
57
  0xEDB88320, 0, 32, true, 0, 0, 0, 0, NULL);
@@ -46,9 +61,15 @@ void crc_init() {
46
61
  int flags[4];
47
62
  _cpuid1(flags);
48
63
  if((flags[2] & 0x80202) == 0x80202) // SSE4.1 + SSSE3 + CLMUL
49
- crc_clmul_set_funcs(&_do_crc32, &_do_crc32_incremental);
64
+ crc_clmul_set_funcs(&_do_crc32_incremental);
50
65
  #endif
51
66
  #ifdef PLATFORM_ARM
67
+ # ifdef __APPLE__
68
+ int supported = 0;
69
+ size_t len = sizeof(supported);
70
+ if(sysctlbyname("hw.optional.armv8_crc32", &supported, &len, NULL, 0))
71
+ supported = 0;
72
+ # endif
52
73
  if(
53
74
  # if defined(AT_HWCAP2) && defined(HWCAP2_CRC32)
54
75
  getauxval(AT_HWCAP2) & HWCAP2_CRC32
@@ -56,14 +77,19 @@ void crc_init() {
56
77
  getauxval(AT_HWCAP) & HWCAP_CRC32
57
78
  # elif defined(ANDROID_CPU_FAMILY_ARM) && defined(__aarch64__)
58
79
  android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_CRC32
59
- /* no 32-bit flag - presumably CRC not allowed on 32-bit CPUs on Android */
80
+ # elif defined(ANDROID_CPU_FAMILY_ARM) /* aarch32 */
81
+ android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_CRC32
82
+ # elif defined(_WIN32)
83
+ IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE)
84
+ # elif defined(__APPLE__)
85
+ supported
60
86
  # elif defined(__ARM_FEATURE_CRC32)
61
87
  true /* assume available if compiled as such */
62
88
  # else
63
89
  false
64
90
  # endif
65
91
  ) {
66
- crc_arm_set_funcs(&_do_crc32, &_do_crc32_incremental);
92
+ crc_arm_set_funcs(&_do_crc32_incremental);
67
93
  }
68
94
  #endif
69
95
  }
package/src/crc.h CHANGED
@@ -1,9 +1,23 @@
1
+ #ifndef __YENC_CRC_H
2
+ #define __YENC_CRC_H
1
3
 
2
- extern void (*_do_crc32)(const void* data, size_t length, unsigned char out[4]);
3
- #define do_crc32 (*_do_crc32)
4
- extern void (*_do_crc32_incremental)(const void* data, size_t length, unsigned char init[4]);
5
- #define do_crc32_incremental (*_do_crc32_incremental)
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
6
7
 
7
- void do_crc32_combine(unsigned char crc1[4], const unsigned char crc2[4], size_t len2);
8
- void do_crc32_zeros(unsigned char crc1[4], size_t len);
8
+
9
+
10
+ typedef uint32_t (*crc_func)(const void*, size_t, uint32_t);
11
+ extern crc_func _do_crc32_incremental;
12
+ #define do_crc32 (*_do_crc32_incremental)
13
+
14
+ uint32_t do_crc32_combine(uint32_t crc1, const uint32_t crc2, size_t len2);
15
+ uint32_t do_crc32_zeros(uint32_t crc1, size_t len);
9
16
  void crc_init();
17
+
18
+
19
+
20
+ #ifdef __cplusplus
21
+ }
22
+ #endif
23
+ #endif
package/src/crc_arm.cc CHANGED
@@ -1,15 +1,69 @@
1
- #include "common.h"
2
1
  #include "crc_common.h"
3
2
 
4
- #if defined(__ARM_FEATURE_CRC32) || defined(_M_ARM64) /* TODO: AArch32 for MSVC? */
3
+ #if defined(PLATFORM_ARM) && defined(_MSC_VER) && defined(__clang__) && !defined(__ARM_FEATURE_CRC32)
4
+ // I don't think GYP provides a nice way to detect whether MSVC or clang-cl is being used, but it doesn't use clang-cl by default, so a warning here is probably sufficient
5
+ HEDLEY_WARNING("CRC32 acceleration is not been enabled under ARM clang-cl by default; add `-march=armv8-a+crc` to additional compiler arguments to enable");
6
+ #endif
7
+
8
+ #if defined(__ARM_FEATURE_CRC32) || (defined(_M_ARM64) && !defined(__clang__)) // MSVC doesn't support CRC for ARM32
5
9
 
6
10
  /* ARMv8 accelerated CRC */
7
- #ifdef _MSC_VER
11
+ #if defined(_MSC_VER) && !defined(__clang__)
8
12
  #include <intrin.h>
9
13
  #else
10
14
  #include <arm_acle.h>
11
15
  #endif
12
16
 
17
+ #ifdef __aarch64__
18
+ # define WORD_T uint64_t
19
+ # define WORDSIZE_LOG 3 // sizeof(WORD_T) == 1<<WORDSIZE_LOG
20
+ # define CRC_WORD __crc32d
21
+ #else
22
+ # define WORD_T uint32_t
23
+ # define WORDSIZE_LOG 2 // sizeof(WORD_T) == 1<<WORDSIZE_LOG
24
+ # define CRC_WORD __crc32w
25
+ #endif
26
+
27
+
28
+ // exploit CPU pipelining during CRC computation; unfortunately I haven't been able to measure any benefit
29
+ // - Neoverse N1: no noticeable difference
30
+ // - Cortex A53: actually runs a bit slower
31
+ //#define ENABLE_PIPELINE_OPT 1
32
+
33
+ #ifdef ENABLE_PIPELINE_OPT
34
+ // workaround MSVC complaining "unary minus operator applied to unsigned type, result still unsigned"
35
+ #define NEGATE(n) (uint32_t)(-((int32_t)(n)))
36
+
37
+ static HEDLEY_ALWAYS_INLINE uint32_t crc_multiply(uint32_t a, uint32_t b) {
38
+ uint32_t res = 0;
39
+ for(int i=0; i<31; i++) {
40
+ res ^= NEGATE(b>>31) & a;
41
+ a = ((a >> 1) ^ (0xEDB88320 & NEGATE(a&1)));
42
+ b <<= 1;
43
+ }
44
+ res ^= NEGATE(b>>31) & a;
45
+ return res;
46
+ }
47
+
48
+ static const uint32_t crc_power[] = { // pre-computed 2^n, with first 3 entries removed (saves a shift)
49
+ 0x00800000, 0x00008000, 0xedb88320, 0xb1e6b092, 0xa06a2517, 0xed627dae, 0x88d14467, 0xd7bbfe6a,
50
+ 0xec447f11, 0x8e7ea170, 0x6427800e, 0x4d47bae0, 0x09fe548f, 0x83852d0f, 0x30362f1a, 0x7b5a9cc3,
51
+ 0x31fec169, 0x9fec022a, 0x6c8dedc4, 0x15d6874d, 0x5fde7a4e, 0xbad90e37, 0x2e4e5eef, 0x4eaba214,
52
+ 0xa8a472c0, 0x429a969e, 0x148d302a, 0xc40ba6d0, 0xc4e22c3c, 0x40000000, 0x20000000, 0x08000000
53
+ };
54
+ /* above table can be computed with
55
+ int main(void) {
56
+ uint32_t k = 0x80000000 >> 1;
57
+ for (size_t i = 0; i < 32+3; ++i) {
58
+ if(i>2) printf("0x%08x, ", k);
59
+ k = crc_multiply(k, k);
60
+ }
61
+ return 0;
62
+ }
63
+ */
64
+ #endif
65
+
66
+
13
67
  // inspired/stolen off https://github.com/jocover/crc32_armv8/blob/master/crc32_armv8.c
14
68
  static uint32_t arm_crc_calc(uint32_t crc, const unsigned char *src, long len) {
15
69
 
@@ -25,25 +79,74 @@ static uint32_t arm_crc_calc(uint32_t crc, const unsigned char *src, long len) {
25
79
  src += sizeof(uint16_t);
26
80
  len -= sizeof(uint16_t);
27
81
  }
28
-
29
82
  #ifdef __aarch64__
30
83
  if ((uintptr_t)src & sizeof(uint32_t)) {
31
84
  crc = __crc32w(crc, *((uint32_t *)src));
32
85
  src += sizeof(uint32_t);
33
86
  len -= sizeof(uint32_t);
34
87
  }
88
+ #endif
35
89
  }
36
- while ((len -= sizeof(uint64_t)) >= 0) {
37
- crc = __crc32d(crc, *((uint64_t *)src));
38
- src += sizeof(uint64_t);
90
+
91
+ const WORD_T* srcW = (const WORD_T*)src;
92
+
93
+ #ifdef ENABLE_PIPELINE_OPT
94
+ // uses ideas from https://github.com/komrad36/crc#option-13-golden
95
+ // (this is a slightly less efficient, but much simpler implementation of the idea)
96
+ const unsigned SPLIT_WORDS_LOG = 10; // make sure it's at least 2
97
+ const unsigned SPLIT_WORDS = 1<<SPLIT_WORDS_LOG;
98
+ while(len >= (long)(sizeof(WORD_T)*SPLIT_WORDS*2)) {
99
+ // compute 2x CRCs concurrently to leverage piplining
100
+ uint32_t crc2 = 0;
101
+ for(unsigned i=0; i<SPLIT_WORDS; i+=4) {
102
+ crc = CRC_WORD(crc, *srcW);
103
+ crc2 = CRC_WORD(crc2, *(srcW + SPLIT_WORDS));
104
+ srcW++;
105
+ crc = CRC_WORD(crc, *srcW);
106
+ crc2 = CRC_WORD(crc2, *(srcW + SPLIT_WORDS));
107
+ srcW++;
108
+ crc = CRC_WORD(crc, *srcW);
109
+ crc2 = CRC_WORD(crc2, *(srcW + SPLIT_WORDS));
110
+ srcW++;
111
+ crc = CRC_WORD(crc, *srcW);
112
+ crc2 = CRC_WORD(crc2, *(srcW + SPLIT_WORDS));
113
+ srcW++;
114
+ }
115
+ // merge the CRCs
116
+ // since we're multiplying by a fixed number, it could be sped up with some lookup tables
117
+ crc = crc_multiply(crc, crc_power[SPLIT_WORDS_LOG + WORDSIZE_LOG]) ^ crc2;
118
+ srcW += SPLIT_WORDS;
119
+ len -= sizeof(WORD_T)*SPLIT_WORDS*2;
39
120
  }
40
- if (len & sizeof(uint32_t)) {
41
- crc = __crc32w(crc, *((uint32_t *)src));
42
- src += sizeof(uint32_t);
121
+ #endif
122
+
123
+ while ((len -= sizeof(WORD_T)*8) >= 0) {
124
+ crc = CRC_WORD(crc, *(srcW++));
125
+ crc = CRC_WORD(crc, *(srcW++));
126
+ crc = CRC_WORD(crc, *(srcW++));
127
+ crc = CRC_WORD(crc, *(srcW++));
128
+ crc = CRC_WORD(crc, *(srcW++));
129
+ crc = CRC_WORD(crc, *(srcW++));
130
+ crc = CRC_WORD(crc, *(srcW++));
131
+ crc = CRC_WORD(crc, *(srcW++));
43
132
  }
44
- #else
133
+ if (len & sizeof(WORD_T)*4) {
134
+ crc = CRC_WORD(crc, *(srcW++));
135
+ crc = CRC_WORD(crc, *(srcW++));
136
+ crc = CRC_WORD(crc, *(srcW++));
137
+ crc = CRC_WORD(crc, *(srcW++));
138
+ }
139
+ if (len & sizeof(WORD_T)*2) {
140
+ crc = CRC_WORD(crc, *(srcW++));
141
+ crc = CRC_WORD(crc, *(srcW++));
45
142
  }
46
- while ((len -= sizeof(uint32_t)) >= 0) {
143
+ if (len & sizeof(WORD_T)) {
144
+ crc = CRC_WORD(crc, *(srcW++));
145
+ }
146
+ src = (const unsigned char*)srcW;
147
+
148
+ #ifdef __aarch64__
149
+ if (len & sizeof(uint32_t)) {
47
150
  crc = __crc32w(crc, *((uint32_t *)src));
48
151
  src += sizeof(uint32_t);
49
152
  }
@@ -58,20 +161,15 @@ static uint32_t arm_crc_calc(uint32_t crc, const unsigned char *src, long len) {
58
161
  return crc;
59
162
  }
60
163
 
61
- static void do_crc32_arm(const void* data, size_t length, unsigned char out[4]) {
62
- uint32_t crc = arm_crc_calc(~0, (const unsigned char*)data, (long)length);
63
- UNPACK_4(out, ~crc);
64
- }
65
- static void do_crc32_incremental_arm(const void* data, size_t length, unsigned char init[4]) {
66
- uint32_t crc = PACK_4(init);
67
- crc = arm_crc_calc(~crc, (const unsigned char*)data, (long)length);
68
- UNPACK_4(init, ~crc);
164
+ static uint32_t do_crc32_incremental_arm(const void* data, size_t length, uint32_t init) {
165
+ return ~arm_crc_calc(~init, (const unsigned char*)data, (long)length);
69
166
  }
70
167
 
71
- void crc_arm_set_funcs(crc_func* _do_crc32, crc_func* _do_crc32_incremental) {
72
- *_do_crc32 = &do_crc32_arm;
168
+ void crc_arm_set_funcs(crc_func* _do_crc32_incremental) {
73
169
  *_do_crc32_incremental = &do_crc32_incremental_arm;
74
170
  }
75
171
  #else
76
- void crc_arm_set_funcs(crc_func* _do_crc32, crc_func* _do_crc32_incremental) {}
172
+ void crc_arm_set_funcs(crc_func* _do_crc32_incremental) {
173
+ (void)_do_crc32_incremental;
174
+ }
77
175
  #endif
package/src/crc_common.h CHANGED
@@ -1,11 +1,4 @@
1
-
2
- #define PACK_4(arr) (((uint_fast32_t)arr[0] << 24) | ((uint_fast32_t)arr[1] << 16) | ((uint_fast32_t)arr[2] << 8) | (uint_fast32_t)arr[3])
3
- #define UNPACK_4(arr, val) { \
4
- arr[0] = (unsigned char)(val >> 24) & 0xFF; \
5
- arr[1] = (unsigned char)(val >> 16) & 0xFF; \
6
- arr[2] = (unsigned char)(val >> 8) & 0xFF; \
7
- arr[3] = (unsigned char)val & 0xFF; \
8
- }
9
-
1
+ #include "common.h"
10
2
  #include <stddef.h> // for size_t
11
- typedef void (*crc_func)(const void*, size_t, unsigned char[4]);
3
+ #include "crc.h"
4
+