Skip to content

Commit

Permalink
Speeding up C implementation
Browse files Browse the repository at this point in the history
Switch from Sarwate's algorithm to byte-order free Slicing-by-16.
Each table data file was generated by `ext/digest/gentable.rb`.

In addition, the inspection string for rspec, which was previously 10 bytes, is extended to 19 bytes.
This is to check the Slicing-by-16 loop, which processes in 16-byte units.
  • Loading branch information
dearblue committed Aug 15, 2023
1 parent 91ee75f commit f93262b
Show file tree
Hide file tree
Showing 79 changed files with 20,922 additions and 1,097 deletions.
61 changes: 24 additions & 37 deletions ext/digest/crc12_3gpp/crc12_3gpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,39 @@
*/

#include "crc12_3gpp.h"

/**
* Static table used for the table_driven implementation.
*/
static const crc12_t crc12_table[256] = {
0x000, 0x80f, 0x811, 0x01e, 0x82d, 0x022, 0x03c, 0x833, 0x855, 0x05a, 0x044, 0x84b, 0x078, 0x877, 0x869, 0x066,
0x8a5, 0x0aa, 0x0b4, 0x8bb, 0x088, 0x887, 0x899, 0x096, 0x0f0, 0x8ff, 0x8e1, 0x0ee, 0x8dd, 0x0d2, 0x0cc, 0x8c3,
0x945, 0x14a, 0x154, 0x95b, 0x168, 0x967, 0x979, 0x176, 0x110, 0x91f, 0x901, 0x10e, 0x93d, 0x132, 0x12c, 0x923,
0x1e0, 0x9ef, 0x9f1, 0x1fe, 0x9cd, 0x1c2, 0x1dc, 0x9d3, 0x9b5, 0x1ba, 0x1a4, 0x9ab, 0x198, 0x997, 0x989, 0x186,
0xa85, 0x28a, 0x294, 0xa9b, 0x2a8, 0xaa7, 0xab9, 0x2b6, 0x2d0, 0xadf, 0xac1, 0x2ce, 0xafd, 0x2f2, 0x2ec, 0xae3,
0x220, 0xa2f, 0xa31, 0x23e, 0xa0d, 0x202, 0x21c, 0xa13, 0xa75, 0x27a, 0x264, 0xa6b, 0x258, 0xa57, 0xa49, 0x246,
0x3c0, 0xbcf, 0xbd1, 0x3de, 0xbed, 0x3e2, 0x3fc, 0xbf3, 0xb95, 0x39a, 0x384, 0xb8b, 0x3b8, 0xbb7, 0xba9, 0x3a6,
0xb65, 0x36a, 0x374, 0xb7b, 0x348, 0xb47, 0xb59, 0x356, 0x330, 0xb3f, 0xb21, 0x32e, 0xb1d, 0x312, 0x30c, 0xb03,
0xd05, 0x50a, 0x514, 0xd1b, 0x528, 0xd27, 0xd39, 0x536, 0x550, 0xd5f, 0xd41, 0x54e, 0xd7d, 0x572, 0x56c, 0xd63,
0x5a0, 0xdaf, 0xdb1, 0x5be, 0xd8d, 0x582, 0x59c, 0xd93, 0xdf5, 0x5fa, 0x5e4, 0xdeb, 0x5d8, 0xdd7, 0xdc9, 0x5c6,
0x440, 0xc4f, 0xc51, 0x45e, 0xc6d, 0x462, 0x47c, 0xc73, 0xc15, 0x41a, 0x404, 0xc0b, 0x438, 0xc37, 0xc29, 0x426,
0xce5, 0x4ea, 0x4f4, 0xcfb, 0x4c8, 0xcc7, 0xcd9, 0x4d6, 0x4b0, 0xcbf, 0xca1, 0x4ae, 0xc9d, 0x492, 0x48c, 0xc83,
0x780, 0xf8f, 0xf91, 0x79e, 0xfad, 0x7a2, 0x7bc, 0xfb3, 0xfd5, 0x7da, 0x7c4, 0xfcb, 0x7f8, 0xff7, 0xfe9, 0x7e6,
0xf25, 0x72a, 0x734, 0xf3b, 0x708, 0xf07, 0xf19, 0x716, 0x770, 0xf7f, 0xf61, 0x76e, 0xf5d, 0x752, 0x74c, 0xf43,
0xec5, 0x6ca, 0x6d4, 0xedb, 0x6e8, 0xee7, 0xef9, 0x6f6, 0x690, 0xe9f, 0xe81, 0x68e, 0xebd, 0x6b2, 0x6ac, 0xea3,
0x660, 0xe6f, 0xe71, 0x67e, 0xe4d, 0x642, 0x65c, 0xe53, 0xe35, 0x63a, 0x624, 0xe2b, 0x618, 0xe17, 0xe09, 0x606
};

crc12_t crc_reflect(crc12_t data, size_t data_len)
{
unsigned int i;
crc12_t ret = data & 0x01;

for (i = 1; i < data_len; i++)
{
data >>= 1;
ret = (ret << 1) | (data & 0x01);
}

return ret;
}
#include "crc12_3gpp_table.h"

crc12_t crc12_3gpp_update(crc12_t crc, const void *data, size_t data_len)
{
const unsigned char *d = (const unsigned char *)data;
unsigned int tbl_idx;

for (; data_len >= SLICE_SIZE; data_len -= SLICE_SIZE)
{
crc = crc12_3gpp_table[15][d[ 0] ^ ((crc >> 4) & 0xff)] ^
crc12_3gpp_table[14][d[ 1] ^ ((crc << 4) & 0xff)] ^
crc12_3gpp_table[13][d[ 2] ] ^
crc12_3gpp_table[12][d[ 3] ] ^
crc12_3gpp_table[11][d[ 4] ] ^
crc12_3gpp_table[10][d[ 5] ] ^
crc12_3gpp_table[ 9][d[ 6] ] ^
crc12_3gpp_table[ 8][d[ 7] ] ^
crc12_3gpp_table[ 7][d[ 8] ] ^
crc12_3gpp_table[ 6][d[ 9] ] ^
crc12_3gpp_table[ 5][d[10] ] ^
crc12_3gpp_table[ 4][d[11] ] ^
crc12_3gpp_table[ 3][d[12] ] ^
crc12_3gpp_table[ 2][d[13] ] ^
crc12_3gpp_table[ 1][d[14] ] ^
crc12_3gpp_table[ 0][d[15] ];
crc &= 0xfff;
d += SLICE_SIZE;
}

while (data_len--)
{
tbl_idx = ((crc >> 4) ^ *d) & 0xff;
crc = (crc12_table[tbl_idx] ^ (crc << 8)) & 0xfff;
crc = (crc12_3gpp_table[0][tbl_idx] ^ (crc << 8)) & 0xfff;
d++;
}

Expand Down
558 changes: 558 additions & 0 deletions ext/digest/crc12_3gpp/crc12_3gpp_table.h

Large diffs are not rendered by default.

47 changes: 24 additions & 23 deletions ext/digest/crc15/crc15.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,39 @@
*/

#include "crc15.h"

/**
* Static table used for the table_driven implementation.
*/
static const crc15_t crc15_table[256] = {
0x0000, 0x4599, 0x4eab, 0x0b32, 0x58cf, 0x1d56, 0x1664, 0x53fd, 0x7407, 0x319e, 0x3aac, 0x7f35, 0x2cc8, 0x6951, 0x6263, 0x27fa,
0x2d97, 0x680e, 0x633c, 0x26a5, 0x7558, 0x30c1, 0x3bf3, 0x7e6a, 0x5990, 0x1c09, 0x173b, 0x52a2, 0x015f, 0x44c6, 0x4ff4, 0x0a6d,
0x5b2e, 0x1eb7, 0x1585, 0x501c, 0x03e1, 0x4678, 0x4d4a, 0x08d3, 0x2f29, 0x6ab0, 0x6182, 0x241b, 0x77e6, 0x327f, 0x394d, 0x7cd4,
0x76b9, 0x3320, 0x3812, 0x7d8b, 0x2e76, 0x6bef, 0x60dd, 0x2544, 0x02be, 0x4727, 0x4c15, 0x098c, 0x5a71, 0x1fe8, 0x14da, 0x5143,
0x73c5, 0x365c, 0x3d6e, 0x78f7, 0x2b0a, 0x6e93, 0x65a1, 0x2038, 0x07c2, 0x425b, 0x4969, 0x0cf0, 0x5f0d, 0x1a94, 0x11a6, 0x543f,
0x5e52, 0x1bcb, 0x10f9, 0x5560, 0x069d, 0x4304, 0x4836, 0x0daf, 0x2a55, 0x6fcc, 0x64fe, 0x2167, 0x729a, 0x3703, 0x3c31, 0x79a8,
0x28eb, 0x6d72, 0x6640, 0x23d9, 0x7024, 0x35bd, 0x3e8f, 0x7b16, 0x5cec, 0x1975, 0x1247, 0x57de, 0x0423, 0x41ba, 0x4a88, 0x0f11,
0x057c, 0x40e5, 0x4bd7, 0x0e4e, 0x5db3, 0x182a, 0x1318, 0x5681, 0x717b, 0x34e2, 0x3fd0, 0x7a49, 0x29b4, 0x6c2d, 0x671f, 0x2286,
0x2213, 0x678a, 0x6cb8, 0x2921, 0x7adc, 0x3f45, 0x3477, 0x71ee, 0x5614, 0x138d, 0x18bf, 0x5d26, 0x0edb, 0x4b42, 0x4070, 0x05e9,
0x0f84, 0x4a1d, 0x412f, 0x04b6, 0x574b, 0x12d2, 0x19e0, 0x5c79, 0x7b83, 0x3e1a, 0x3528, 0x70b1, 0x234c, 0x66d5, 0x6de7, 0x287e,
0x793d, 0x3ca4, 0x3796, 0x720f, 0x21f2, 0x646b, 0x6f59, 0x2ac0, 0x0d3a, 0x48a3, 0x4391, 0x0608, 0x55f5, 0x106c, 0x1b5e, 0x5ec7,
0x54aa, 0x1133, 0x1a01, 0x5f98, 0x0c65, 0x49fc, 0x42ce, 0x0757, 0x20ad, 0x6534, 0x6e06, 0x2b9f, 0x7862, 0x3dfb, 0x36c9, 0x7350,
0x51d6, 0x144f, 0x1f7d, 0x5ae4, 0x0919, 0x4c80, 0x47b2, 0x022b, 0x25d1, 0x6048, 0x6b7a, 0x2ee3, 0x7d1e, 0x3887, 0x33b5, 0x762c,
0x7c41, 0x39d8, 0x32ea, 0x7773, 0x248e, 0x6117, 0x6a25, 0x2fbc, 0x0846, 0x4ddf, 0x46ed, 0x0374, 0x5089, 0x1510, 0x1e22, 0x5bbb,
0x0af8, 0x4f61, 0x4453, 0x01ca, 0x5237, 0x17ae, 0x1c9c, 0x5905, 0x7eff, 0x3b66, 0x3054, 0x75cd, 0x2630, 0x63a9, 0x689b, 0x2d02,
0x276f, 0x62f6, 0x69c4, 0x2c5d, 0x7fa0, 0x3a39, 0x310b, 0x7492, 0x5368, 0x16f1, 0x1dc3, 0x585a, 0x0ba7, 0x4e3e, 0x450c, 0x0095
};
#include "crc15_table.h"

crc15_t crc15_update(crc15_t crc, const void *data, size_t data_len)
{
const unsigned char *d = (const unsigned char *)data;
unsigned int tbl_idx;

for (; data_len >= SLICE_SIZE; data_len -= SLICE_SIZE)
{
crc = crc15_table[15][d[ 0] ^ ((crc >> 7) & 0xff)] ^
crc15_table[14][d[ 1] ^ ((crc << 1) & 0xff)] ^
crc15_table[13][d[ 2] ] ^
crc15_table[12][d[ 3] ] ^
crc15_table[11][d[ 4] ] ^
crc15_table[10][d[ 5] ] ^
crc15_table[ 9][d[ 6] ] ^
crc15_table[ 8][d[ 7] ] ^
crc15_table[ 7][d[ 8] ] ^
crc15_table[ 6][d[ 9] ] ^
crc15_table[ 5][d[10] ] ^
crc15_table[ 4][d[11] ] ^
crc15_table[ 3][d[12] ] ^
crc15_table[ 2][d[13] ] ^
crc15_table[ 1][d[14] ] ^
crc15_table[ 0][d[15] ];
crc &= 0x7fff;
d += SLICE_SIZE;
}

while (data_len--)
{
tbl_idx = ((crc >> 7) ^ *d) & 0xff;
crc = (crc15_table[tbl_idx] ^ (crc << 8)) & 0x7fff;
crc = (crc15_table[0][tbl_idx] ^ (crc << 8)) & 0x7fff;
d++;
}

Expand Down
Loading

0 comments on commit f93262b

Please sign in to comment.