crc-turbo 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 042e9756847215414abcb1a12fbf37b81d7fe16f
4
- data.tar.gz: aac8893c3843eaf1055db3ace8a2623b35be3f3c
3
+ metadata.gz: a9c55e6cae87bb11776ca21753515a6c5f9d5d93
4
+ data.tar.gz: a57614ca5264bae1ad92edd823ece6a4e530885d
5
5
  SHA512:
6
- metadata.gz: a62d436e87a9d0fae480d3e69247017a55e1abe300f828aa3b9b3c38702cdc01c9afa4d22d1f72d56ebc0030f7cf47c9bbd2d81dbdeb1570d16c194d06d17bd9
7
- data.tar.gz: 07b0ad0a958e8c243d514c677b24a8a1c54fd1b9548900b507a06d9565cd35a14380eb6aa02382e9d4ee67f7598abe9ac776d0e0b8892e82ce135128b9d8dfa8
6
+ metadata.gz: 39813537987919956767a2ecef0ae83e055c9a2adb7c41b540030f45fa9b5d309bf244fcb95d5f82c74dce71241fbd7b9cdd7ae76340d4322abc28e7fe5b9421
7
+ data.tar.gz: 180d142c3f0df7a350e1493421f0b11af33b8d79be1f2b8cdac035226e3ed1e8c2f443400a3e195c4d9548b481e86f8605b37707845fa65dddc675205163ff02
@@ -0,0 +1,14 @@
1
+ This document is written in Japanese.
2
+
3
+ # crc-turbo for ruby の更新履歴
4
+
5
+ ## crc-0.2 (平成28年5月15日 (日))
6
+
7
+ * \[FIX\] CRC::Generator#polynomial メソッドが間違った値を返していた問題を修正
8
+ * CRC::Utils.bitreflect128 メソッドの実装
9
+ * CRC::BasicCRC#update メソッドの実装を slice-by-eight から slice-by-16 へと変更
10
+ * CRC ルックアップテーブル領域確保の時機を初回計算時の直前とするように改善
11
+
12
+ ## crc-0.1 (平成28年5月8日 (日))
13
+
14
+ 初版
data/README.md CHANGED
@@ -3,24 +3,24 @@
3
3
 
4
4
  This is a C extention for "crc" gem library.
5
5
 
6
- Just install this, and "require 'crc'". Additional other work is not required.
6
+ Just install this, and to do "require 'crc'" only. Additional other work is not required.
7
7
 
8
8
 
9
- ## SUMMARY
9
+ ## Summary
10
10
 
11
11
  * package name: crc-turbo
12
12
  * author: dearblue (mailto:dearblue@users.osdn.me)
13
13
  * report issue to: <https://osdn.jp/projects/rutsubo/ticket/>
14
14
  * how to install: ``gem install crc-turbo``
15
- * version: 0.1
15
+ * version: 0.2
16
16
  * release quality: thechnical preview
17
- * licensing: BSD-2-Clause
18
- * dependency gems: crc (<https://rubygems/gems/crc>)
17
+ * licensing: BSD-2-Clause<br>any parts are under Creative Commons License Zero (CC0 / Public Domain).
18
+ * dependency gems: crc-0.2 (<https://rubygems/gems/crc>)
19
19
  * dependency external c libraries: none
20
20
  * bundled external c libraries: none
21
21
 
22
22
 
23
- ## HOW TO USAGE
23
+ ## How to usage
24
24
 
25
25
  First, install on your system.
26
26
 
@@ -34,19 +34,21 @@ And, to do ``require "crc"``.
34
34
 
35
35
  ``` shell:shell
36
36
  $ ruby -r crc -e 'puts $".grep(/crc/)'
37
- /usr/local/lib/ruby/gems/2.3/gems/crc-turbo-0.1/lib/crc/_turbo.so
38
- /usr/local/lib/ruby/gems/2.3/gems/crc-0.1/lib/crc/_modules.rb
39
- /usr/local/lib/ruby/gems/2.3/gems/crc-0.1/lib/crc.rb
37
+ /usr/local/lib/ruby/gems/2.3/gems/crc-turbo-0.2/lib/crc/2.3/_turbo.so
38
+ /usr/local/lib/ruby/gems/2.3/gems/crc-0.2/lib/crc/_modules.rb
39
+ /usr/local/lib/ruby/gems/2.3/gems/crc-0.2/lib/crc/_combine.rb
40
+ /usr/local/lib/ruby/gems/2.3/gems/crc-0.2/lib/crc.rb
40
41
  $
41
42
  ```
42
43
 
43
- If you want not use crc-turbo, set ``RUBY_CRC_NOFAST`` enviroment variable.
44
+ If you want to not use crc-turbo, set ``RUBY_CRC_NOFAST`` enviroment variable.
44
45
 
45
46
  ``` shell:shell
46
- $ RUBY_CRC_NOFAST=1
47
+ $ export RUBY_CRC_NOFAST=1
47
48
  $ ruby -r crc -e 'puts $".grep(/crc/)'
48
- /usr/local/lib/ruby/gems/2.3/gems/crc-0.1/lib/crc/_byruby.rb
49
- /usr/local/lib/ruby/gems/2.3/gems/crc-0.1/lib/crc/_modules.rb
50
- /usr/local/lib/ruby/gems/2.3/gems/crc-0.1/lib/crc.rb
49
+ /usr/local/lib/ruby/gems/2.3/gems/crc-0.2/lib/crc/_byruby.rb
50
+ /usr/local/lib/ruby/gems/2.3/gems/crc-0.2/lib/crc/_modules.rb
51
+ /usr/local/lib/ruby/gems/2.3/gems/crc-0.2/lib/crc/_combine.rb
52
+ /usr/local/lib/ruby/gems/2.3/gems/crc-0.2/lib/crc.rb
51
53
  $
52
54
  ```
@@ -0,0 +1,264 @@
1
+ /*
2
+ * author:: dearblue <dearblue@users.osdn.me>
3
+ * license:: Creative Commons License Zero (CC0 / Public Domain)
4
+ *
5
+ * This is a general CRC generator.
6
+ *
7
+ * It's used slice-by-16 algorithm with byte-order free and byte-alignment free.
8
+ * This is based on the Intel's slice-by-eight algorithm.
9
+ *
10
+ * Worst point is need more memory!
11
+ *
12
+ * reference:
13
+ * * https://sourceforge.net/projects/slicing-by-8/
14
+ * * xz-utils
15
+ * * http://tukaani.org/xz/
16
+ * * xz-5.2.2/src/liblzma/check/crc32_fast.c
17
+ * * xz-5.2.2/src/liblzma/check/crc32_tablegen.c
18
+ */
19
+
20
+ #if !defined(CRC_TYPE) || \
21
+ !defined(CRC_BITREFLECT) || \
22
+ !defined(CRC_BUILD_TABLES) || \
23
+ !defined(CRC_UPDATE) || \
24
+ !defined(CRC_BUILD_REFLECT_TABLES) || \
25
+ !defined(CRC_REFLECT_UPDATE)
26
+ # error not defined CRC_TYPE, CRC_BITREFLECT, CRC_BUILD_TABLES, CRC_UPDATE, CRC_BUILD_REFLECT_TABLES or CRC_REFLECT_UPDATE before include this file
27
+ #endif
28
+
29
+ #ifndef CRC_INLINE
30
+ # define CRC_INLINE
31
+ #endif
32
+
33
+ /*
34
+ * [bitsize]
35
+ * CRC bit-size. 32 for CRC-32, 7 for CRC-7, and so on.
36
+ *
37
+ * [table]
38
+ * lookup table. total byte size is sizeof(CRC_TYPE[slicesize][256]).
39
+ *
40
+ * 16384 for CRC-32:slicesize=16.
41
+ *
42
+ * [poly]
43
+ * polynomial. not reflected.
44
+ *
45
+ * [slicesize]
46
+ * table slice size.
47
+ *
48
+ * 1 for standard lookup-table, 4 for slice-by-four, 16 for slice-by-16, and so on.
49
+ */
50
+ static CRC_INLINE void
51
+ CRC_BUILD_TABLES(int bitsize, CRC_TYPE table[][256], CRC_TYPE poly, int slicesize)
52
+ {
53
+ static const int typebits = sizeof(CRC_TYPE) * CHAR_BIT;
54
+ CRC_TYPE (*t)[256] = table;
55
+ int s, b, i;
56
+ poly <<= (typebits - bitsize);
57
+ for (s = 0; s < slicesize; s ++) {
58
+ CRC_TYPE *p = *t;
59
+ for (b = 0; b < 256; b ++) {
60
+ CRC_TYPE r = (s == 0) ? ((CRC_TYPE)b << (typebits - 8)) : t[-1][b];
61
+ for (i = 0; i < 8; i ++) {
62
+ r = (r << 1) ^ (poly & -(r >> (typebits - 1)));
63
+ }
64
+ *p ++ = r;
65
+ }
66
+ t ++;
67
+ }
68
+ }
69
+
70
+ /*
71
+ * [bitsize]
72
+ * CRC bit-size. 32 for CRC-32, 7 for CRC-7, and so on.
73
+ *
74
+ * [table]
75
+ * lookup table. total byte size is sizeof(CRC_TYPE[slicesize][256]).
76
+ *
77
+ * 16384 for CRC-32:slicesize=16.
78
+ *
79
+ * [poly]
80
+ * polynomial. not reflected.
81
+ *
82
+ * [slicesize]
83
+ * table slice size.
84
+ *
85
+ * 1 for standard lookup-table, 4 for slice-by-four, 16 for slice-by-16, and so on.
86
+ */
87
+ static CRC_INLINE void
88
+ CRC_BUILD_REFLECT_TABLES(int bitsize, CRC_TYPE table[][256], CRC_TYPE poly, int slicesize)
89
+ {
90
+ static const int typebits = sizeof(CRC_TYPE) * CHAR_BIT;
91
+ CRC_TYPE (*t)[256] = table;
92
+ poly = CRC_BITREFLECT(poly << (typebits - bitsize));
93
+ int s, b;
94
+ for (s = 0; s < slicesize; s ++) {
95
+ CRC_TYPE *p = *t;
96
+ for (b = 0; b < 256; b ++) {
97
+ int i;
98
+ CRC_TYPE r = (s == 0) ? b : t[-1][b];
99
+ for (i = 0; i < 8; i ++) {
100
+ r = (r >> 1) ^ (poly & -(r & 1));
101
+ }
102
+ *p ++ = r;
103
+ }
104
+ t ++;
105
+ }
106
+ }
107
+
108
+ static CRC_TYPE
109
+ CRC_UPDATE(int bitsize, const CRC_TYPE table[16][256],
110
+ const char *p, const char *pp, CRC_TYPE state)
111
+ {
112
+ static const int typebits = sizeof(CRC_TYPE) * CHAR_BIT;
113
+ const char *pp16 = ((pp - p) & ~0x0f) + p;
114
+ state <<= typebits - bitsize;
115
+ for (; p < pp16; p += 16) {
116
+ state = table[15][(uint8_t)p[ 0] ^ (uint8_t)(state >> (typebits - 8)) ] ^
117
+ table[14][(uint8_t)p[ 1] ^ (typebits > 8 ? (uint8_t)(state >> (typebits - 16)) : 0)] ^
118
+ table[13][(uint8_t)p[ 2] ^ (typebits > 16 ? (uint8_t)(state >> (typebits - 24)) : 0)] ^
119
+ table[12][(uint8_t)p[ 3] ^ (typebits > 24 ? (uint8_t)(state >> (typebits - 32)) : 0)] ^
120
+ table[11][(uint8_t)p[ 4] ^ (typebits > 32 ? (uint8_t)(state >> (typebits - 40)) : 0)] ^
121
+ table[10][(uint8_t)p[ 5] ^ (typebits > 40 ? (uint8_t)(state >> (typebits - 48)) : 0)] ^
122
+ table[ 9][(uint8_t)p[ 6] ^ (typebits > 48 ? (uint8_t)(state >> (typebits - 56)) : 0)] ^
123
+ table[ 8][(uint8_t)p[ 7] ^ (typebits > 56 ? (uint8_t)(state >> (typebits - 64)) : 0)] ^
124
+ table[ 7][(uint8_t)p[ 8] ^ (typebits > 64 ? (uint8_t)(state >> (typebits - 72)) : 0)] ^
125
+ table[ 6][(uint8_t)p[ 9] ^ (typebits > 72 ? (uint8_t)(state >> (typebits - 80)) : 0)] ^
126
+ table[ 5][(uint8_t)p[10] ^ (typebits > 80 ? (uint8_t)(state >> (typebits - 88)) : 0)] ^
127
+ table[ 4][(uint8_t)p[11] ^ (typebits > 88 ? (uint8_t)(state >> (typebits - 96)) : 0)] ^
128
+ table[ 3][(uint8_t)p[12] ^ (typebits > 96 ? (uint8_t)(state >> (typebits - 104)) : 0)] ^
129
+ table[ 2][(uint8_t)p[13] ^ (typebits > 104 ? (uint8_t)(state >> (typebits - 112)) : 0)] ^
130
+ table[ 1][(uint8_t)p[14] ^ (typebits > 112 ? (uint8_t)(state >> (typebits - 120)) : 0)] ^
131
+ table[ 0][(uint8_t)p[15] ^ (typebits > 120 ? (uint8_t)(state >> (typebits - 128)) : 0)];
132
+ }
133
+
134
+ static const int sh = typebits - 8;
135
+ for (; p < pp; p ++) {
136
+ state = table[0][(uint8_t)*p ^ (uint8_t)(state >> sh)] ^ (state << 8);
137
+ }
138
+
139
+ return state >> (typebits - bitsize);
140
+ }
141
+
142
+ /*
143
+ * [bitsize]
144
+ * CRC bit-size. 32 for CRC-32, 7 for CRC-7, and so on.
145
+ *
146
+ * [table]
147
+ * lookup table.
148
+ *
149
+ * [p]
150
+ * input sequence.
151
+ *
152
+ * [pp]
153
+ * end of input sequence.
154
+ *
155
+ * example, p + len(p).
156
+ *
157
+ * [state]
158
+ * internal state.
159
+ *
160
+ * example, state ^ xor_output.
161
+ */
162
+ static CRC_INLINE CRC_TYPE
163
+ CRC_REFLECT_UPDATE(int bitsize, const CRC_TYPE table[16][256],
164
+ const char *p, const char *pp, CRC_TYPE state)
165
+ {
166
+ static const int typebits = sizeof(CRC_TYPE) * CHAR_BIT;
167
+ const char *pp16 = ((pp - p) & ~0x0f) + p;
168
+ for (; p < pp16; p += 16) {
169
+ state = table[15][(uint8_t)p[ 0] ^ (uint8_t)(state >> 0) ] ^
170
+ table[14][(uint8_t)p[ 1] ^ (typebits > 8 ? (uint8_t)(state >> 8) : 0)] ^
171
+ table[13][(uint8_t)p[ 2] ^ (typebits > 16 ? (uint8_t)(state >> 16) : 0)] ^
172
+ table[12][(uint8_t)p[ 3] ^ (typebits > 24 ? (uint8_t)(state >> 24) : 0)] ^
173
+ table[11][(uint8_t)p[ 4] ^ (typebits > 32 ? (uint8_t)(state >> 32) : 0)] ^
174
+ table[10][(uint8_t)p[ 5] ^ (typebits > 40 ? (uint8_t)(state >> 40) : 0)] ^
175
+ table[ 9][(uint8_t)p[ 6] ^ (typebits > 48 ? (uint8_t)(state >> 48) : 0)] ^
176
+ table[ 8][(uint8_t)p[ 7] ^ (typebits > 56 ? (uint8_t)(state >> 56) : 0)] ^
177
+ table[ 7][(uint8_t)p[ 8] ^ (typebits > 64 ? (uint8_t)(state >> 64) : 0)] ^
178
+ table[ 6][(uint8_t)p[ 9] ^ (typebits > 72 ? (uint8_t)(state >> 72) : 0)] ^
179
+ table[ 5][(uint8_t)p[10] ^ (typebits > 80 ? (uint8_t)(state >> 80) : 0)] ^
180
+ table[ 4][(uint8_t)p[11] ^ (typebits > 88 ? (uint8_t)(state >> 88) : 0)] ^
181
+ table[ 3][(uint8_t)p[12] ^ (typebits > 96 ? (uint8_t)(state >> 96) : 0)] ^
182
+ table[ 2][(uint8_t)p[13] ^ (typebits > 104 ? (uint8_t)(state >> 104) : 0)] ^
183
+ table[ 1][(uint8_t)p[14] ^ (typebits > 112 ? (uint8_t)(state >> 112) : 0)] ^
184
+ table[ 0][(uint8_t)p[15] ^ (typebits > 120 ? (uint8_t)(state >> 120) : 0)];
185
+ }
186
+
187
+ for (; p < pp; p ++) {
188
+ state = table[0][(uint8_t)*p ^ (uint8_t)state] ^ (state >> 8);
189
+ }
190
+
191
+ return state;
192
+ }
193
+
194
+ #ifdef CRC_REFLECT_UPDATE_WITH_BITWISE
195
+
196
+ /*
197
+ * no lookup tables with bitwise.
198
+ *
199
+ * reference::
200
+ * http://www.hackersdelight.org/hdcodetxt/crc.c.txt#crc32b
201
+ */
202
+ static CRC_INLINE CRC_TYPE
203
+ CRC_REFLECT_UPDATE_WITH_BITWISE(const int bitsize, CRC_TYPE poly,
204
+ const char *p, const char *pp, CRC_TYPE state)
205
+ {
206
+ poly = CRC_BITREFLECT(poly) >> (32 - bitsize);
207
+
208
+ for (; p < pp; p ++) {
209
+ state ^= (uint8_t)*p;
210
+ int i;
211
+ for (i = 0; i < 8; i ++) {
212
+ state = (state >> 1) ^ (poly & -(state & 1));
213
+ }
214
+ }
215
+
216
+ return state;
217
+ }
218
+
219
+ #undef CRC_REFLECT_UPDATE_WITH_BITWISE
220
+ #endif
221
+
222
+ #ifdef CRC_REFLECT_UPDATE_WITH_BITPACK
223
+
224
+ /*
225
+ * loopless for each bits.
226
+ *
227
+ * reference::
228
+ * http://www.hackersdelight.org/hdcodetxt/crc.c.txt#crc32h
229
+ */
230
+ static CRC_INLINE CRC_TYPE
231
+ CRC_REFLECT_UPDATE_WITH_BITPACK(const int bitsize, const CRC_TYPE poly,
232
+ const char *p, const char *pp, CRC_TYPE state)
233
+ {
234
+ const CRC_TYPE g0 = CRC_BITREFLECT(poly) >> (32 - bitsize),
235
+ g1 = (g0 >> 1) ^ (g0 & -(g0 & 1)),
236
+ g2 = (g1 >> 1) ^ (g0 & -(g1 & 1)),
237
+ g3 = (g2 >> 1) ^ (g0 & -(g2 & 1)),
238
+ g4 = (g3 >> 1) ^ (g0 & -(g3 & 1)),
239
+ g5 = (g4 >> 1) ^ (g0 & -(g4 & 1)),
240
+ g6 = (g5 >> 1) ^ (g0 & -(g5 & 1)),
241
+ g7 = (g6 >> 1) ^ (g0 & -(g6 & 1));
242
+
243
+ for (; p < pp; p ++) {
244
+ const uint8_t s1 = state ^ *p;
245
+ state = (g7 & -(CRC_TYPE)((s1 >> 0) & 1)) ^ (g6 & -(CRC_TYPE)((s1 >> 1) & 1)) ^
246
+ (g5 & -(CRC_TYPE)((s1 >> 2) & 1)) ^ (g4 & -(CRC_TYPE)((s1 >> 3) & 1)) ^
247
+ (g3 & -(CRC_TYPE)((s1 >> 4) & 1)) ^ (g2 & -(CRC_TYPE)((s1 >> 5) & 1)) ^
248
+ (g1 & -(CRC_TYPE)((s1 >> 6) & 1)) ^ (g0 & -(CRC_TYPE)((s1 >> 7) & 1)) ^
249
+ (state >> 8);
250
+ }
251
+
252
+ return state;
253
+ }
254
+
255
+ #undef CRC_REFLECT_UPDATE_WITH_BITPACK
256
+ #endif
257
+
258
+ #undef CRC_TYPE
259
+ #undef CRC_BITREFLECT
260
+ #undef CRC_BUILD_TABLES
261
+ #undef CRC_UPDATE
262
+ #undef CRC_BUILD_REFLECT_TABLES
263
+ #undef CRC_REFLECT_UPDATE
264
+ #undef CRC_INLINE
@@ -228,6 +228,19 @@ bitreflect128(uint128_t n)
228
228
  }
229
229
  #endif
230
230
 
231
+ static inline unsigned long
232
+ bitreflect_ulong(unsigned long n)
233
+ {
234
+ if (sizeof(unsigned long) == sizeof(uint64_t)) {
235
+ return bitreflect64(n);
236
+ } else if (sizeof(unsigned long) == sizeof(uint32_t)) {
237
+ return bitreflect32(n);
238
+ } else {
239
+ // FIXME
240
+ return ~0;
241
+ }
242
+ }
243
+
231
244
  static inline int
232
245
  bitsize_to_type(int bitsize)
233
246
  {
@@ -246,131 +259,48 @@ bitsize_to_type(int bitsize)
246
259
  }
247
260
  }
248
261
 
262
+ #define CRC_TYPE uint8_t
263
+ #define CRC_BITREFLECT bitreflect8
264
+ #define CRC_BUILD_TABLES crc_build_tables_u8
265
+ #define CRC_UPDATE crc_update_u8
266
+ #define CRC_BUILD_REFLECT_TABLES crc_build_reflect_tables_u8
267
+ #define CRC_REFLECT_UPDATE crc_reflect_update_u8
268
+ #include "crc_imps.h"
269
+
270
+ #define CRC_TYPE uint16_t
271
+ #define CRC_BITREFLECT bitreflect16
272
+ #define CRC_BUILD_TABLES crc_build_tables_u16
273
+ #define CRC_UPDATE crc_update_u16
274
+ #define CRC_BUILD_REFLECT_TABLES crc_build_reflect_tables_u16
275
+ #define CRC_REFLECT_UPDATE crc_reflect_update_u16
276
+ #include "crc_imps.h"
277
+
278
+ #define CRC_TYPE uint32_t
279
+ #define CRC_BITREFLECT bitreflect32
280
+ #define CRC_BUILD_TABLES crc_build_tables_u32
281
+ #define CRC_UPDATE crc_update_u32
282
+ #define CRC_BUILD_REFLECT_TABLES crc_build_reflect_tables_u32
283
+ #define CRC_REFLECT_UPDATE crc_reflect_update_u32
284
+ #include "crc_imps.h"
285
+
286
+ #define CRC_TYPE uint64_t
287
+ #define CRC_BITREFLECT bitreflect64
288
+ #define CRC_BUILD_TABLES crc_build_tables_u64
289
+ #define CRC_UPDATE crc_update_u64
290
+ #define CRC_BUILD_REFLECT_TABLES crc_build_reflect_tables_u64
291
+ #define CRC_REFLECT_UPDATE crc_reflect_update_u64
292
+ #include "crc_imps.h"
249
293
 
250
- #define IMP_BUILD_TABLE(NAME, TYPE) \
251
- static void \
252
- NAME(TYPE table[8][256], int bitsize, TYPE poly) \
253
- { \
254
- static const int typesize = sizeof(TYPE) * CHAR_BIT; \
255
- poly <<= (typesize - bitsize); \
256
- TYPE (*t)[256] = table; \
257
- int s, b, i; \
258
- for (s = 0; s < 8; s ++) { \
259
- TYPE *p = *t; \
260
- for (b = 0; b < 256; b ++) { \
261
- TYPE r = (s == 0) ? ((TYPE)b << (typesize - 8)) : t[-1][b]; \
262
- for (i = 0; i < 8; i ++) { \
263
- r = (r << 1) ^ (poly & -(r >> (typesize - 1))); \
264
- } \
265
- *p ++ = r; \
266
- } \
267
- t ++; \
268
- } \
269
- }
270
-
271
- #define IMP_CRC_UPDATE(NAME, TYPE) \
272
- static TYPE \
273
- NAME(int bitsize, const TYPE table[8][256], \
274
- const char *p, const char *pp, TYPE state) \
275
- { \
276
- static const int typesize = sizeof(TYPE) * CHAR_BIT; \
277
- const char *pp8 = ((pp - p) & ~0x07) + p; \
278
- state <<= typesize - bitsize; \
279
- for (; p < pp8; p += 8) { \
280
- state = table[7][(uint8_t)p[0] ^ (uint8_t)(state >> (typesize - 8)) ] ^ \
281
- table[6][(uint8_t)p[1] ^ (typesize > 8 ? (uint8_t)(state >> (typesize - 16)) : 0)] ^ \
282
- table[5][(uint8_t)p[2] ^ (typesize > 16 ? (uint8_t)(state >> (typesize - 24)) : 0)] ^ \
283
- table[4][(uint8_t)p[3] ^ (typesize > 24 ? (uint8_t)(state >> (typesize - 32)) : 0)] ^ \
284
- table[3][(uint8_t)p[4] ^ (typesize > 32 ? (uint8_t)(state >> (typesize - 40)) : 0)] ^ \
285
- table[2][(uint8_t)p[5] ^ (typesize > 40 ? (uint8_t)(state >> (typesize - 48)) : 0)] ^ \
286
- table[1][(uint8_t)p[6] ^ (typesize > 48 ? (uint8_t)(state >> (typesize - 56)) : 0)] ^ \
287
- table[0][(uint8_t)p[7] ^ (typesize > 56 ? (uint8_t)(state >> (typesize - 64)) : 0)]; \
288
- } \
289
- \
290
- int sh = typesize - 8; \
291
- for (; p < pp; p ++) { \
292
- state = table[0][(uint8_t)*p ^ (uint8_t)(state >> sh)] ^ (state << 8); \
293
- } \
294
- \
295
- return state >> (typesize - bitsize); \
296
- } \
297
-
298
- IMP_BUILD_TABLE(build_table8_u8, uint8_t);
299
- IMP_CRC_UPDATE(crc_update_u8, uint8_t);
300
- IMP_BUILD_TABLE(build_table8_u16, uint16_t);
301
- IMP_CRC_UPDATE(crc_update_u16, uint16_t);
302
- IMP_BUILD_TABLE(build_table8_u32, uint32_t);
303
- IMP_CRC_UPDATE(crc_update_u32, uint32_t);
304
- IMP_BUILD_TABLE(build_table8_u64, uint64_t);
305
- IMP_CRC_UPDATE(crc_update_u64, uint64_t);
306
- #ifdef HAVE_TYPE_UINT128_T
307
- IMP_BUILD_TABLE(build_table8_u128, uint128_t);
308
- IMP_CRC_UPDATE(crc_update_u128, uint128_t);
309
- #endif
310
-
311
- #undef IMP_BUILD_TABLE
312
-
313
- #define IMP_BUILD_REFLECT_TABLE(NAME, TYPE, BITREFLECT) \
314
- static void \
315
- NAME(TYPE table[8][256], int bitsize, TYPE poly) \
316
- { \
317
- static const int typesize = sizeof(TYPE) * CHAR_BIT; \
318
- poly = BITREFLECT(poly << (typesize - bitsize)); \
319
- TYPE (*t)[256] = table; \
320
- int s, b, i; \
321
- for (s = 0; s < 8; s ++) { \
322
- TYPE *p = *t; \
323
- for (b = 0; b < 256; b ++) { \
324
- TYPE r = (s == 0) ? (TYPE)b : t[-1][b]; \
325
- for (i = 0; i < 8; i ++) { \
326
- r = (r >> 1) ^ (poly & -(r & 1)); \
327
- } \
328
- *p ++ = r; \
329
- } \
330
- t ++; \
331
- } \
332
- } \
333
-
334
- #define IMP_CRC_REFLECT_UPDATE(NAME, TYPE) \
335
- static TYPE \
336
- NAME(int bitsize, const TYPE table[8][256], \
337
- const char *p, const char *pp, TYPE state) \
338
- { \
339
- static const int typesize = sizeof(TYPE) * CHAR_BIT; \
340
- const char *pp8 = ((pp - p) & ~0x07) + p; \
341
- for (; p < pp8; p += 8) { \
342
- state = table[7][(uint8_t)p[0] ^ (uint8_t)(state >> 0) ] ^ \
343
- table[6][(uint8_t)p[1] ^ (typesize > 8 ? (uint8_t)(state >> 8) : 0)] ^ \
344
- table[5][(uint8_t)p[2] ^ (typesize > 16 ? (uint8_t)(state >> 16) : 0)] ^ \
345
- table[4][(uint8_t)p[3] ^ (typesize > 24 ? (uint8_t)(state >> 24) : 0)] ^ \
346
- table[3][(uint8_t)p[4] ^ (typesize > 32 ? (uint8_t)(state >> 32) : 0)] ^ \
347
- table[2][(uint8_t)p[5] ^ (typesize > 40 ? (uint8_t)(state >> 40) : 0)] ^ \
348
- table[1][(uint8_t)p[6] ^ (typesize > 48 ? (uint8_t)(state >> 48) : 0)] ^ \
349
- table[0][(uint8_t)p[7] ^ (typesize > 56 ? (uint8_t)(state >> 56) : 0)]; \
350
- } \
351
- \
352
- for (; p < pp; p ++) { \
353
- state = table[0][(uint8_t)*p ^ (uint8_t)state] ^ (state >> 8); \
354
- } \
355
- \
356
- return state; \
357
- } \
358
-
359
- IMP_BUILD_REFLECT_TABLE(build_reflect_table8_u8, uint8_t, bitreflect8);
360
- IMP_CRC_REFLECT_UPDATE(crc_reflect_update_u8, uint8_t);
361
- IMP_BUILD_REFLECT_TABLE(build_reflect_table8_u16, uint16_t, bitreflect16);
362
- IMP_CRC_REFLECT_UPDATE(crc_reflect_update_u16, uint16_t);
363
- IMP_BUILD_REFLECT_TABLE(build_reflect_table8_u32, uint32_t, bitreflect32);
364
- IMP_CRC_REFLECT_UPDATE(crc_reflect_update_u32, uint32_t);
365
- IMP_BUILD_REFLECT_TABLE(build_reflect_table8_u64, uint64_t, bitreflect64);
366
- IMP_CRC_REFLECT_UPDATE(crc_reflect_update_u64, uint64_t);
367
294
  #ifdef HAVE_TYPE_UINT128_T
368
- IMP_BUILD_REFLECT_TABLE(build_reflect_table8_u128, uint128_t, bitreflect128);
369
- IMP_CRC_REFLECT_UPDATE(crc_reflect_update_u128, uint128_t);
295
+ # define CRC_TYPE uint128_t
296
+ # define CRC_BITREFLECT bitreflect128
297
+ # define CRC_BUILD_TABLES crc_build_tables_u128
298
+ # define CRC_UPDATE crc_update_u128
299
+ # define CRC_BUILD_REFLECT_TABLES crc_build_reflect_tables_u128
300
+ # define CRC_REFLECT_UPDATE crc_reflect_update_u128
301
+ # include "crc_imps.h"
370
302
  #endif
371
303
 
372
- #undef IMP_BUILD_REFLECT_TABLE
373
- #undef IMP_CRC_REFLECT_UPDATE
374
304
 
375
305
  /*
376
306
  *
@@ -396,21 +326,14 @@ struct generator
396
326
  int bitsize;
397
327
  int flags; /* int type, refin, refout */
398
328
  anyuint_t bitmask, polynomial, initial, xorout;
399
- union {
400
- uint8_t as8[8][256];
401
- uint16_t as16[8][256];
402
- uint32_t as32[8][256];
403
- uint64_t as64[8][256];
404
- #ifdef HAVE_TYPE_UINT128_T
405
- uint128_t as128[8][256];
406
- #endif
407
- } table[0];
329
+ const void *table; /* entity is String buffer as instance variable */
408
330
  };
409
331
 
410
332
  static VALUE mCRC; /* module CRC */
411
333
  static VALUE mUtils; /* module CRC::Utils */
412
334
  static VALUE cGenerator; /* class CRC::Generator */
413
335
  static ID generator_iv_name;
336
+ static ID generator_iv_table_buffer;
414
337
 
415
338
  static const rb_data_type_t generator_type = {
416
339
  .wrap_struct_name = "crc-turbo.CRC::Generator",
@@ -480,10 +403,11 @@ generator_init(int argc, VALUE argv[], VALUE obj)
480
403
  generator_init_args(argc, argv, &flags, &bitsize, &poly, &init, &xorout, &name);
481
404
 
482
405
  struct generator *p;
483
- size_t allocsize = sizeof(struct generator) + (flags & TYPE_MASK) * 256 * 8;
406
+ size_t allocsize = sizeof(struct generator);
484
407
  RTYPEDDATA_DATA(obj) = p = (struct generator *)ALLOC_N(char, allocsize);
485
408
  p->bitsize = bitsize;
486
- p->flags = flags | TABLE_NOTREADY;
409
+ p->flags = flags;
410
+ p->table = NULL;
487
411
 
488
412
  /*
489
413
  * bitmask の代入でわざわざ1ビット分を後から行う理由は、
@@ -533,11 +457,11 @@ generator_polynomial(VALUE t)
533
457
  {
534
458
  struct generator *p = get_generator(t);
535
459
  SWITCH_BY_TYPE(p->flags,
536
- return conv_uint8(p->bitmask.as8),
537
- return conv_uint16(p->bitmask.as16),
538
- return conv_uint32(p->bitmask.as32),
539
- return conv_uint64(p->bitmask.as64),
540
- return conv_uint128(p->bitmask.as128));
460
+ return conv_uint8(p->polynomial.as8),
461
+ return conv_uint16(p->polynomial.as16),
462
+ return conv_uint32(p->polynomial.as32),
463
+ return conv_uint64(p->polynomial.as64),
464
+ return conv_uint128(p->polynomial.as128));
541
465
  }
542
466
 
543
467
  static VALUE
@@ -553,7 +477,7 @@ generator_initial_state(VALUE t)
553
477
  }
554
478
 
555
479
  static VALUE
556
- generator_table8(VALUE t)
480
+ generator_table(VALUE t)
557
481
  {
558
482
  struct generator *p = get_generator(t);
559
483
  rb_raise(rb_eNotImpError, "");
@@ -608,43 +532,49 @@ static VALUE
608
532
  generator_update(VALUE t, VALUE seq, VALUE state)
609
533
  {
610
534
  struct generator *p = get_generator(t);
535
+ rb_check_type(seq, RUBY_T_STRING);
536
+ const char *q = RSTRING_PTR(seq);
537
+ const char *qq = q + RSTRING_LEN(seq);
611
538
 
612
- if (p->flags & TABLE_NOTREADY) {
539
+ if (!p->table) {
540
+ size_t tablebytes = (p->flags & TYPE_MASK) * 16 * 256;
541
+ VALUE tablebuf = rb_str_buf_new(tablebytes);
542
+ rb_str_set_len(tablebuf, tablebytes);
543
+ void *table = RSTRING_PTR(tablebuf);
613
544
  if (p->flags & REFLECT_INPUT) {
614
545
  SWITCH_BY_TYPE(p->flags,
615
- build_reflect_table8_u8(p->table[0].as8, p->bitsize, p->polynomial.as8),
616
- build_reflect_table8_u16(p->table[0].as16, p->bitsize, p->polynomial.as16),
617
- build_reflect_table8_u32(p->table[0].as32, p->bitsize, p->polynomial.as32),
618
- build_reflect_table8_u64(p->table[0].as64, p->bitsize, p->polynomial.as64),
619
- build_reflect_table8_u128(p->table[0].as128, p->bitsize, p->polynomial.as128));
546
+ crc_build_reflect_tables_u8(p->bitsize, table, p->polynomial.as8, 16),
547
+ crc_build_reflect_tables_u16(p->bitsize, table, p->polynomial.as16, 16),
548
+ crc_build_reflect_tables_u32(p->bitsize, table, p->polynomial.as32, 16),
549
+ crc_build_reflect_tables_u64(p->bitsize, table, p->polynomial.as64, 16),
550
+ crc_build_reflect_tables_u128(p->bitsize, table, p->polynomial.as128, 16));
620
551
  } else {
621
552
  SWITCH_BY_TYPE(p->flags,
622
- build_table8_u8(p->table[0].as8, p->bitsize, p->polynomial.as8),
623
- build_table8_u16(p->table[0].as16, p->bitsize, p->polynomial.as16),
624
- build_table8_u32(p->table[0].as32, p->bitsize, p->polynomial.as32),
625
- build_table8_u64(p->table[0].as64, p->bitsize, p->polynomial.as64),
626
- build_table8_u128(p->table[0].as128, p->bitsize, p->polynomial.as128));
553
+ crc_build_tables_u8(p->bitsize, table, p->polynomial.as8, 16),
554
+ crc_build_tables_u16(p->bitsize, table, p->polynomial.as16, 16),
555
+ crc_build_tables_u32(p->bitsize, table, p->polynomial.as32, 16),
556
+ crc_build_tables_u64(p->bitsize, table, p->polynomial.as64, 16),
557
+ crc_build_tables_u128(p->bitsize, table, p->polynomial.as128, 16));
627
558
  }
628
- p->flags ^= TABLE_NOTREADY;
559
+ rb_ivar_set(t, generator_iv_table_buffer, tablebuf);
560
+ rb_obj_freeze(tablebuf);
561
+ p->table = table;
629
562
  }
630
563
 
631
- rb_check_type(seq, RUBY_T_STRING);
632
- const char *q = RSTRING_PTR(seq);
633
- const char *qq = q + RSTRING_LEN(seq);
634
564
  if (p->flags & REFLECT_INPUT) {
635
565
  SWITCH_BY_TYPE(p->flags,
636
- return conv_uint8(crc_reflect_update_u8(p->bitsize, p->table[0].as8, q, qq, to_uint8(state))),
637
- return conv_uint16(crc_reflect_update_u16(p->bitsize, p->table[0].as16, q, qq, to_uint16(state))),
638
- return conv_uint32(crc_reflect_update_u32(p->bitsize, p->table[0].as32, q, qq, to_uint32(state))),
639
- return conv_uint64(crc_reflect_update_u64(p->bitsize, p->table[0].as64, q, qq, to_uint64(state))),
640
- return conv_uint128(crc_reflect_update_u128(p->bitsize, p->table[0].as128, q, qq, to_uint128(state))));
566
+ return conv_uint8(crc_reflect_update_u8(p->bitsize, p->table, q, qq, to_uint8(state))),
567
+ return conv_uint16(crc_reflect_update_u16(p->bitsize, p->table, q, qq, to_uint16(state))),
568
+ return conv_uint32(crc_reflect_update_u32(p->bitsize, p->table, q, qq, to_uint32(state))),
569
+ return conv_uint64(crc_reflect_update_u64(p->bitsize, p->table, q, qq, to_uint64(state))),
570
+ return conv_uint128(crc_reflect_update_u128(p->bitsize, p->table, q, qq, to_uint128(state))));
641
571
  } else {
642
572
  SWITCH_BY_TYPE(p->flags,
643
- return conv_uint8(crc_update_u8(p->bitsize, p->table[0].as8, q, qq, to_uint8(state))),
644
- return conv_uint16(crc_update_u16(p->bitsize, p->table[0].as16, q, qq, to_uint16(state))),
645
- return conv_uint32(crc_update_u32(p->bitsize, p->table[0].as32, q, qq, to_uint32(state))),
646
- return conv_uint64(crc_update_u64(p->bitsize, p->table[0].as64, q, qq, to_uint64(state))),
647
- return conv_uint128(crc_update_u128(p->bitsize, p->table[0].as128, q, qq, to_uint128(state))));
573
+ return conv_uint8(crc_update_u8(p->bitsize, p->table, q, qq, to_uint8(state))),
574
+ return conv_uint16(crc_update_u16(p->bitsize, p->table, q, qq, to_uint16(state))),
575
+ return conv_uint32(crc_update_u32(p->bitsize, p->table, q, qq, to_uint32(state))),
576
+ return conv_uint64(crc_update_u64(p->bitsize, p->table, q, qq, to_uint64(state))),
577
+ return conv_uint128(crc_update_u128(p->bitsize, p->table, q, qq, to_uint128(state))));
648
578
  }
649
579
  }
650
580
 
@@ -679,10 +609,27 @@ utils_s_bitref64(VALUE mod, VALUE num)
679
609
  static VALUE
680
610
  utils_s_bitref128(VALUE mod, VALUE num)
681
611
  {
682
- #ifdef HAVE_TYPE_UINT128_T
683
- return conv_uint128(bitreflect128(to_uint128(num)));
684
- #endif
685
- rb_raise(rb_eNotImpError, "");
612
+ if (sizeof(unsigned long) < 16) {
613
+ static const int len = sizeof(char[16]) / sizeof(unsigned long);
614
+ unsigned long tmp[len + 1];
615
+ rb_big_pack(num, tmp, len);
616
+ unsigned long *p = tmp;
617
+ unsigned long *q = p + len;
618
+ while (p < q) {
619
+ unsigned long tmp1 = bitreflect_ulong(*p);
620
+ *p ++ = bitreflect_ulong(*-- q);
621
+ *q = tmp1;
622
+ }
623
+ tmp[len] = 0;
624
+ return rb_big_unpack(tmp, len + 1);
625
+ } else {
626
+ unsigned long n[2];
627
+ rb_big_pack(num, n, 1);
628
+ n[0] = (unsigned long)bitreflect64(n[0] >> 64) |
629
+ ((unsigned long)bitreflect64(n[0] ) << 64);
630
+ n[1] = 0;
631
+ return rb_big_unpack(n, 2);
632
+ }
686
633
  }
687
634
 
688
635
  /*
@@ -693,6 +640,7 @@ void
693
640
  Init__turbo(void)
694
641
  {
695
642
  generator_iv_name = rb_intern("crc-turbo.CRC::Generator.name");
643
+ generator_iv_table_buffer = rb_intern("crc-turbo.CRC::Generator.table-buffer");
696
644
 
697
645
  mCRC = rb_define_module("CRC");
698
646
 
@@ -710,7 +658,7 @@ Init__turbo(void)
710
658
  rb_define_method(cGenerator, "bitmask", generator_bitmask, 0);
711
659
  rb_define_method(cGenerator, "polynomial", generator_polynomial, 0);
712
660
  rb_define_method(cGenerator, "initial_state", generator_initial_state, 0);
713
- rb_define_method(cGenerator, "table8", generator_table8, 0);
661
+ rb_define_method(cGenerator, "table", generator_table, 0);
714
662
  rb_define_method(cGenerator, "reflect_input", generator_reflect_input, 0);
715
663
  rb_define_method(cGenerator, "reflect_output", generator_reflect_output, 0);
716
664
  rb_define_method(cGenerator, "xor_output", generator_xor_output, 0);
data/gemstub.rb CHANGED
@@ -1,19 +1,19 @@
1
1
  GEMSTUB = Gem::Specification.new do |s|
2
2
  s.name = "crc-turbo"
3
- s.version = "0.1"
3
+ s.version = "0.2"
4
4
  s.summary = "general CRC generator"
5
5
  s.description = <<EOS
6
6
  This is a C extention for "crc" gem library.
7
- Just install this, and "require 'crc'". Additional other work is not required.
7
+ Just install this, and to do "require 'crc'" only. Additional other work is not required.
8
8
  EOS
9
9
  s.homepage = "https://osdn.jp/projects/rutsubo/"
10
- s.license = "BSD-2-Clause"
10
+ s.licenses = ["BSD-2-Clause", "CC0-1.0"]
11
11
  s.author = "dearblue"
12
12
  s.email = "dearblue@users.osdn.me"
13
13
 
14
14
  s.required_ruby_version = ">= 2.0"
15
15
  s.add_development_dependency "rake", "~> 11.0"
16
- s.add_runtime_dependency "crc", "~> 0.1"
16
+ s.add_runtime_dependency "crc", "~> 0.2"
17
17
  end
18
18
 
19
19
  EXTMAP["crc"] = "crc/_turbo"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crc-turbo
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - dearblue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-08 00:00:00.000000000 Z
11
+ date: 2016-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -30,35 +30,40 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.1'
33
+ version: '0.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.1'
40
+ version: '0.2'
41
41
  description: |
42
42
  This is a C extention for "crc" gem library.
43
- Just install this, and "require 'crc'". Additional other work is not required.
43
+ Just install this, and to do "require 'crc'" only. Additional other work is not required.
44
44
  email: dearblue@users.osdn.me
45
45
  executables: []
46
46
  extensions:
47
47
  - ext/crc/extconf.rb
48
48
  extra_rdoc_files:
49
+ - HISTORY.ja.md
49
50
  - LICENSE
50
51
  - README.md
52
+ - ext/crc/crc_imps.h
51
53
  - ext/crc/crcturbo.c
52
54
  files:
55
+ - HISTORY.ja.md
53
56
  - LICENSE
54
57
  - README.md
55
58
  - Rakefile
59
+ - ext/crc/crc_imps.h
56
60
  - ext/crc/crcturbo.c
57
61
  - ext/crc/extconf.rb
58
62
  - gemstub.rb
59
63
  homepage: https://osdn.jp/projects/rutsubo/
60
64
  licenses:
61
65
  - BSD-2-Clause
66
+ - CC0-1.0
62
67
  metadata: {}
63
68
  post_install_message:
64
69
  rdoc_options:
@@ -80,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
85
  version: '0'
81
86
  requirements: []
82
87
  rubyforge_project:
83
- rubygems_version: 2.6.2
88
+ rubygems_version: 2.6.4
84
89
  signing_key:
85
90
  specification_version: 4
86
91
  summary: general CRC generator