comparison src/core/ngx_crc32.c @ 793:8d39da951bbd

split ngx_crc32() to short and long version
author Igor Sysoev <igor@sysoev.ru>
date Thu, 19 Oct 2006 09:57:49 +0000
parents 50f1741a330a
children 45df22906c12
comparison
equal deleted inserted replaced
792:99858705b03f 793:8d39da951bbd
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 9
10 10
11 /* 11 /*
12 * The code and lookup table are based on the algorithm 12 * The code and lookup tables are based on the algorithm
13 * described at http://www.w3.org/TR/PNG/ 13 * described at http://www.w3.org/TR/PNG/
14 *
15 * The 256 element lookup table takes 1024 bytes, and it may be completely
16 * cached after processing about 30-60 bytes. So for short messages
17 * we use the 16 element lookup table that takes only 64 bytes and align it
18 * to CPU cache line size. Of course, the small table adds code inside
19 * CRC32 cycle, but cache misses overhead is bigger than overhead of
20 * the additional code. For example, ngx_crc32_short() of 16 byte message
21 * takes half as much CPU clocks than ngx_crc32_long().
14 */ 22 */
15 23
16 24
17 uint32_t ngx_crc32_table[] = { 25 static uint32_t ngx_crc32_table16[] = {
26 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
27 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
28 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
29 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
30 };
18 31
32
33 uint32_t ngx_crc32_table256[] = {
19 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 34 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
20 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 35 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
21 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 36 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
22 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 37 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
23 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 38 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
79 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 94 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
80 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 95 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
81 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 96 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
82 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 97 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
83 }; 98 };
99
100
101 uint32_t *ngx_crc32_table_short = ngx_crc32_table16;
102
103
104 ngx_int_t
105 ngx_crc32_init(ngx_pool_t *pool)
106 {
107 void *p;
108
109 if (((uintptr_t) ngx_crc32_table_short
110 & ~((uintptr_t) ngx_cacheline_size - 1))
111 == (uintptr_t) ngx_crc32_table_short)
112 {
113 return NGX_OK;
114 }
115
116 p = ngx_palloc(pool, 16 * sizeof(uint32_t) + ngx_cacheline_size);
117 if (p == NULL) {
118 return NGX_ERROR;
119 }
120
121 p = ngx_align_ptr(p, ngx_cacheline_size);
122
123 ngx_memcpy(p, ngx_crc32_table16, 16 * sizeof(uint32_t));
124
125 ngx_crc32_table_short = p;
126
127 return NGX_OK;
128 }