lemon-project-template-glpk

annotate deps/glpk/src/zlib/crc32.c @ 11:4fc6ad2fb8a6

Test GLPK in src/main.cc
author Alpar Juttner <alpar@cs.elte.hu>
date Sun, 06 Nov 2011 21:43:29 +0100
parents
children
rev   line source
alpar@9 1 /* crc32.c -- compute the CRC-32 of a data stream
alpar@9 2 * Copyright (C) 1995-2006, 2010 Mark Adler
alpar@9 3 * For conditions of distribution and use, see copyright notice in zlib.h
alpar@9 4 *
alpar@9 5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
alpar@9 6 * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
alpar@9 7 * tables for updating the shift register in one step with three exclusive-ors
alpar@9 8 * instead of four steps with four exclusive-ors. This results in about a
alpar@9 9 * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
alpar@9 10 */
alpar@9 11
alpar@9 12 /* @(#) $Id$ */
alpar@9 13
alpar@9 14 /*
alpar@9 15 Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
alpar@9 16 protection on the static variables used to control the first-use generation
alpar@9 17 of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
alpar@9 18 first call get_crc_table() to initialize the tables before allowing more than
alpar@9 19 one thread to use crc32().
alpar@9 20 */
alpar@9 21
alpar@9 22 #ifdef MAKECRCH
alpar@9 23 # include <stdio.h>
alpar@9 24 # ifndef DYNAMIC_CRC_TABLE
alpar@9 25 # define DYNAMIC_CRC_TABLE
alpar@9 26 # endif /* !DYNAMIC_CRC_TABLE */
alpar@9 27 #endif /* MAKECRCH */
alpar@9 28
alpar@9 29 #include "zutil.h" /* for STDC and FAR definitions */
alpar@9 30
alpar@9 31 #define local static
alpar@9 32
alpar@9 33 /* Find a four-byte integer type for crc32_little() and crc32_big(). */
alpar@9 34 #ifndef NOBYFOUR
alpar@9 35 # ifdef STDC /* need ANSI C limits.h to determine sizes */
alpar@9 36 # include <limits.h>
alpar@9 37 # define BYFOUR
alpar@9 38 # if (UINT_MAX == 0xffffffffUL)
alpar@9 39 typedef unsigned int u4;
alpar@9 40 # else
alpar@9 41 # if (ULONG_MAX == 0xffffffffUL)
alpar@9 42 typedef unsigned long u4;
alpar@9 43 # else
alpar@9 44 # if (USHRT_MAX == 0xffffffffUL)
alpar@9 45 typedef unsigned short u4;
alpar@9 46 # else
alpar@9 47 # undef BYFOUR /* can't find a four-byte integer type! */
alpar@9 48 # endif
alpar@9 49 # endif
alpar@9 50 # endif
alpar@9 51 # endif /* STDC */
alpar@9 52 #endif /* !NOBYFOUR */
alpar@9 53
alpar@9 54 /* Definitions for doing the crc four data bytes at a time. */
alpar@9 55 #ifdef BYFOUR
alpar@9 56 # define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
alpar@9 57 (((w)&0xff00)<<8)+(((w)&0xff)<<24))
alpar@9 58 local unsigned long crc32_little OF((unsigned long,
alpar@9 59 const unsigned char FAR *, unsigned));
alpar@9 60 local unsigned long crc32_big OF((unsigned long,
alpar@9 61 const unsigned char FAR *, unsigned));
alpar@9 62 # define TBLS 8
alpar@9 63 #else
alpar@9 64 # define TBLS 1
alpar@9 65 #endif /* BYFOUR */
alpar@9 66
alpar@9 67 /* Local functions for crc concatenation */
alpar@9 68 local unsigned long gf2_matrix_times OF((unsigned long *mat,
alpar@9 69 unsigned long vec));
alpar@9 70 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
alpar@9 71 local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2);
alpar@9 72
alpar@9 73
alpar@9 74 #ifdef DYNAMIC_CRC_TABLE
alpar@9 75
alpar@9 76 local volatile int crc_table_empty = 1;
alpar@9 77 local unsigned long FAR crc_table[TBLS][256];
alpar@9 78 local void make_crc_table OF((void));
alpar@9 79 #ifdef MAKECRCH
alpar@9 80 local void write_table OF((FILE *, const unsigned long FAR *));
alpar@9 81 #endif /* MAKECRCH */
alpar@9 82 /*
alpar@9 83 Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
alpar@9 84 x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
alpar@9 85
alpar@9 86 Polynomials over GF(2) are represented in binary, one bit per coefficient,
alpar@9 87 with the lowest powers in the most significant bit. Then adding polynomials
alpar@9 88 is just exclusive-or, and multiplying a polynomial by x is a right shift by
alpar@9 89 one. If we call the above polynomial p, and represent a byte as the
alpar@9 90 polynomial q, also with the lowest power in the most significant bit (so the
alpar@9 91 byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
alpar@9 92 where a mod b means the remainder after dividing a by b.
alpar@9 93
alpar@9 94 This calculation is done using the shift-register method of multiplying and
alpar@9 95 taking the remainder. The register is initialized to zero, and for each
alpar@9 96 incoming bit, x^32 is added mod p to the register if the bit is a one (where
alpar@9 97 x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
alpar@9 98 x (which is shifting right by one and adding x^32 mod p if the bit shifted
alpar@9 99 out is a one). We start with the highest power (least significant bit) of
alpar@9 100 q and repeat for all eight bits of q.
alpar@9 101
alpar@9 102 The first table is simply the CRC of all possible eight bit values. This is
alpar@9 103 all the information needed to generate CRCs on data a byte at a time for all
alpar@9 104 combinations of CRC register values and incoming bytes. The remaining tables
alpar@9 105 allow for word-at-a-time CRC calculation for both big-endian and little-
alpar@9 106 endian machines, where a word is four bytes.
alpar@9 107 */
alpar@9 108 local void make_crc_table()
alpar@9 109 {
alpar@9 110 unsigned long c;
alpar@9 111 int n, k;
alpar@9 112 unsigned long poly; /* polynomial exclusive-or pattern */
alpar@9 113 /* terms of polynomial defining this crc (except x^32): */
alpar@9 114 static volatile int first = 1; /* flag to limit concurrent making */
alpar@9 115 static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
alpar@9 116
alpar@9 117 /* See if another task is already doing this (not thread-safe, but better
alpar@9 118 than nothing -- significantly reduces duration of vulnerability in
alpar@9 119 case the advice about DYNAMIC_CRC_TABLE is ignored) */
alpar@9 120 if (first) {
alpar@9 121 first = 0;
alpar@9 122
alpar@9 123 /* make exclusive-or pattern from polynomial (0xedb88320UL) */
alpar@9 124 poly = 0UL;
alpar@9 125 for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
alpar@9 126 poly |= 1UL << (31 - p[n]);
alpar@9 127
alpar@9 128 /* generate a crc for every 8-bit value */
alpar@9 129 for (n = 0; n < 256; n++) {
alpar@9 130 c = (unsigned long)n;
alpar@9 131 for (k = 0; k < 8; k++)
alpar@9 132 c = c & 1 ? poly ^ (c >> 1) : c >> 1;
alpar@9 133 crc_table[0][n] = c;
alpar@9 134 }
alpar@9 135
alpar@9 136 #ifdef BYFOUR
alpar@9 137 /* generate crc for each value followed by one, two, and three zeros,
alpar@9 138 and then the byte reversal of those as well as the first table */
alpar@9 139 for (n = 0; n < 256; n++) {
alpar@9 140 c = crc_table[0][n];
alpar@9 141 crc_table[4][n] = REV(c);
alpar@9 142 for (k = 1; k < 4; k++) {
alpar@9 143 c = crc_table[0][c & 0xff] ^ (c >> 8);
alpar@9 144 crc_table[k][n] = c;
alpar@9 145 crc_table[k + 4][n] = REV(c);
alpar@9 146 }
alpar@9 147 }
alpar@9 148 #endif /* BYFOUR */
alpar@9 149
alpar@9 150 crc_table_empty = 0;
alpar@9 151 }
alpar@9 152 else { /* not first */
alpar@9 153 /* wait for the other guy to finish (not efficient, but rare) */
alpar@9 154 while (crc_table_empty)
alpar@9 155 ;
alpar@9 156 }
alpar@9 157
alpar@9 158 #ifdef MAKECRCH
alpar@9 159 /* write out CRC tables to crc32.h */
alpar@9 160 {
alpar@9 161 FILE *out;
alpar@9 162
alpar@9 163 out = fopen("crc32.h", "w");
alpar@9 164 if (out == NULL) return;
alpar@9 165 fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
alpar@9 166 fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
alpar@9 167 fprintf(out, "local const unsigned long FAR ");
alpar@9 168 fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
alpar@9 169 write_table(out, crc_table[0]);
alpar@9 170 # ifdef BYFOUR
alpar@9 171 fprintf(out, "#ifdef BYFOUR\n");
alpar@9 172 for (k = 1; k < 8; k++) {
alpar@9 173 fprintf(out, " },\n {\n");
alpar@9 174 write_table(out, crc_table[k]);
alpar@9 175 }
alpar@9 176 fprintf(out, "#endif\n");
alpar@9 177 # endif /* BYFOUR */
alpar@9 178 fprintf(out, " }\n};\n");
alpar@9 179 fclose(out);
alpar@9 180 }
alpar@9 181 #endif /* MAKECRCH */
alpar@9 182 }
alpar@9 183
alpar@9 184 #ifdef MAKECRCH
alpar@9 185 local void write_table(out, table)
alpar@9 186 FILE *out;
alpar@9 187 const unsigned long FAR *table;
alpar@9 188 {
alpar@9 189 int n;
alpar@9 190
alpar@9 191 for (n = 0; n < 256; n++)
alpar@9 192 fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n],
alpar@9 193 n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
alpar@9 194 }
alpar@9 195 #endif /* MAKECRCH */
alpar@9 196
alpar@9 197 #else /* !DYNAMIC_CRC_TABLE */
alpar@9 198 /* ========================================================================
alpar@9 199 * Tables of CRC-32s of all single-byte values, made by make_crc_table().
alpar@9 200 */
alpar@9 201 #include "crc32.h"
alpar@9 202 #endif /* DYNAMIC_CRC_TABLE */
alpar@9 203
alpar@9 204 /* =========================================================================
alpar@9 205 * This function can be used by asm versions of crc32()
alpar@9 206 */
alpar@9 207 const unsigned long FAR * ZEXPORT get_crc_table()
alpar@9 208 {
alpar@9 209 #ifdef DYNAMIC_CRC_TABLE
alpar@9 210 if (crc_table_empty)
alpar@9 211 make_crc_table();
alpar@9 212 #endif /* DYNAMIC_CRC_TABLE */
alpar@9 213 return (const unsigned long FAR *)crc_table;
alpar@9 214 }
alpar@9 215
alpar@9 216 /* ========================================================================= */
alpar@9 217 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
alpar@9 218 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
alpar@9 219
alpar@9 220 /* ========================================================================= */
alpar@9 221 unsigned long ZEXPORT crc32(crc, buf, len)
alpar@9 222 unsigned long crc;
alpar@9 223 const unsigned char FAR *buf;
alpar@9 224 uInt len;
alpar@9 225 {
alpar@9 226 if (buf == Z_NULL) return 0UL;
alpar@9 227
alpar@9 228 #ifdef DYNAMIC_CRC_TABLE
alpar@9 229 if (crc_table_empty)
alpar@9 230 make_crc_table();
alpar@9 231 #endif /* DYNAMIC_CRC_TABLE */
alpar@9 232
alpar@9 233 #ifdef BYFOUR
alpar@9 234 if (sizeof(void *) == sizeof(ptrdiff_t)) {
alpar@9 235 u4 endian;
alpar@9 236
alpar@9 237 endian = 1;
alpar@9 238 if (*((unsigned char *)(&endian)))
alpar@9 239 return crc32_little(crc, buf, len);
alpar@9 240 else
alpar@9 241 return crc32_big(crc, buf, len);
alpar@9 242 }
alpar@9 243 #endif /* BYFOUR */
alpar@9 244 crc = crc ^ 0xffffffffUL;
alpar@9 245 while (len >= 8) {
alpar@9 246 DO8;
alpar@9 247 len -= 8;
alpar@9 248 }
alpar@9 249 if (len) do {
alpar@9 250 DO1;
alpar@9 251 } while (--len);
alpar@9 252 return crc ^ 0xffffffffUL;
alpar@9 253 }
alpar@9 254
alpar@9 255 #ifdef BYFOUR
alpar@9 256
alpar@9 257 /* ========================================================================= */
alpar@9 258 #define DOLIT4 c ^= *buf4++; \
alpar@9 259 c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
alpar@9 260 crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
alpar@9 261 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
alpar@9 262
alpar@9 263 /* ========================================================================= */
alpar@9 264 local unsigned long crc32_little(crc, buf, len)
alpar@9 265 unsigned long crc;
alpar@9 266 const unsigned char FAR *buf;
alpar@9 267 unsigned len;
alpar@9 268 {
alpar@9 269 register u4 c;
alpar@9 270 register const u4 FAR *buf4;
alpar@9 271
alpar@9 272 c = (u4)crc;
alpar@9 273 c = ~c;
alpar@9 274 while (len && ((ptrdiff_t)buf & 3)) {
alpar@9 275 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
alpar@9 276 len--;
alpar@9 277 }
alpar@9 278
alpar@9 279 buf4 = (const u4 FAR *)(const void FAR *)buf;
alpar@9 280 while (len >= 32) {
alpar@9 281 DOLIT32;
alpar@9 282 len -= 32;
alpar@9 283 }
alpar@9 284 while (len >= 4) {
alpar@9 285 DOLIT4;
alpar@9 286 len -= 4;
alpar@9 287 }
alpar@9 288 buf = (const unsigned char FAR *)buf4;
alpar@9 289
alpar@9 290 if (len) do {
alpar@9 291 c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
alpar@9 292 } while (--len);
alpar@9 293 c = ~c;
alpar@9 294 return (unsigned long)c;
alpar@9 295 }
alpar@9 296
alpar@9 297 /* ========================================================================= */
alpar@9 298 #define DOBIG4 c ^= *++buf4; \
alpar@9 299 c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
alpar@9 300 crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
alpar@9 301 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
alpar@9 302
alpar@9 303 /* ========================================================================= */
alpar@9 304 local unsigned long crc32_big(crc, buf, len)
alpar@9 305 unsigned long crc;
alpar@9 306 const unsigned char FAR *buf;
alpar@9 307 unsigned len;
alpar@9 308 {
alpar@9 309 register u4 c;
alpar@9 310 register const u4 FAR *buf4;
alpar@9 311
alpar@9 312 c = REV((u4)crc);
alpar@9 313 c = ~c;
alpar@9 314 while (len && ((ptrdiff_t)buf & 3)) {
alpar@9 315 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
alpar@9 316 len--;
alpar@9 317 }
alpar@9 318
alpar@9 319 buf4 = (const u4 FAR *)(const void FAR *)buf;
alpar@9 320 buf4--;
alpar@9 321 while (len >= 32) {
alpar@9 322 DOBIG32;
alpar@9 323 len -= 32;
alpar@9 324 }
alpar@9 325 while (len >= 4) {
alpar@9 326 DOBIG4;
alpar@9 327 len -= 4;
alpar@9 328 }
alpar@9 329 buf4++;
alpar@9 330 buf = (const unsigned char FAR *)buf4;
alpar@9 331
alpar@9 332 if (len) do {
alpar@9 333 c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
alpar@9 334 } while (--len);
alpar@9 335 c = ~c;
alpar@9 336 return (unsigned long)(REV(c));
alpar@9 337 }
alpar@9 338
alpar@9 339 #endif /* BYFOUR */
alpar@9 340
alpar@9 341 #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
alpar@9 342
alpar@9 343 /* ========================================================================= */
alpar@9 344 local unsigned long gf2_matrix_times(mat, vec)
alpar@9 345 unsigned long *mat;
alpar@9 346 unsigned long vec;
alpar@9 347 {
alpar@9 348 unsigned long sum;
alpar@9 349
alpar@9 350 sum = 0;
alpar@9 351 while (vec) {
alpar@9 352 if (vec & 1)
alpar@9 353 sum ^= *mat;
alpar@9 354 vec >>= 1;
alpar@9 355 mat++;
alpar@9 356 }
alpar@9 357 return sum;
alpar@9 358 }
alpar@9 359
alpar@9 360 /* ========================================================================= */
alpar@9 361 local void gf2_matrix_square(square, mat)
alpar@9 362 unsigned long *square;
alpar@9 363 unsigned long *mat;
alpar@9 364 {
alpar@9 365 int n;
alpar@9 366
alpar@9 367 for (n = 0; n < GF2_DIM; n++)
alpar@9 368 square[n] = gf2_matrix_times(mat, mat[n]);
alpar@9 369 }
alpar@9 370
alpar@9 371 /* ========================================================================= */
alpar@9 372 local uLong crc32_combine_(crc1, crc2, len2)
alpar@9 373 uLong crc1;
alpar@9 374 uLong crc2;
alpar@9 375 z_off64_t len2;
alpar@9 376 {
alpar@9 377 int n;
alpar@9 378 unsigned long row;
alpar@9 379 unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
alpar@9 380 unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
alpar@9 381
alpar@9 382 /* degenerate case (also disallow negative lengths) */
alpar@9 383 if (len2 <= 0)
alpar@9 384 return crc1;
alpar@9 385
alpar@9 386 /* put operator for one zero bit in odd */
alpar@9 387 odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
alpar@9 388 row = 1;
alpar@9 389 for (n = 1; n < GF2_DIM; n++) {
alpar@9 390 odd[n] = row;
alpar@9 391 row <<= 1;
alpar@9 392 }
alpar@9 393
alpar@9 394 /* put operator for two zero bits in even */
alpar@9 395 gf2_matrix_square(even, odd);
alpar@9 396
alpar@9 397 /* put operator for four zero bits in odd */
alpar@9 398 gf2_matrix_square(odd, even);
alpar@9 399
alpar@9 400 /* apply len2 zeros to crc1 (first square will put the operator for one
alpar@9 401 zero byte, eight zero bits, in even) */
alpar@9 402 do {
alpar@9 403 /* apply zeros operator for this bit of len2 */
alpar@9 404 gf2_matrix_square(even, odd);
alpar@9 405 if (len2 & 1)
alpar@9 406 crc1 = gf2_matrix_times(even, crc1);
alpar@9 407 len2 >>= 1;
alpar@9 408
alpar@9 409 /* if no more bits set, then done */
alpar@9 410 if (len2 == 0)
alpar@9 411 break;
alpar@9 412
alpar@9 413 /* another iteration of the loop with odd and even swapped */
alpar@9 414 gf2_matrix_square(odd, even);
alpar@9 415 if (len2 & 1)
alpar@9 416 crc1 = gf2_matrix_times(odd, crc1);
alpar@9 417 len2 >>= 1;
alpar@9 418
alpar@9 419 /* if no more bits set, then done */
alpar@9 420 } while (len2 != 0);
alpar@9 421
alpar@9 422 /* return combined crc */
alpar@9 423 crc1 ^= crc2;
alpar@9 424 return crc1;
alpar@9 425 }
alpar@9 426
alpar@9 427 /* ========================================================================= */
alpar@9 428 uLong ZEXPORT crc32_combine(crc1, crc2, len2)
alpar@9 429 uLong crc1;
alpar@9 430 uLong crc2;
alpar@9 431 z_off_t len2;
alpar@9 432 {
alpar@9 433 return crc32_combine_(crc1, crc2, len2);
alpar@9 434 }
alpar@9 435
alpar@9 436 uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
alpar@9 437 uLong crc1;
alpar@9 438 uLong crc2;
alpar@9 439 z_off64_t len2;
alpar@9 440 {
alpar@9 441 return crc32_combine_(crc1, crc2, len2);
alpar@9 442 }