This file is indexed.

/usr/include/crcutil/generic_crc.h is in libcrcutil-dev 1.0-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// Copyright 2010 Google Inc.  All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Defines GenericCrc class which implements arbitrary CRCs.
//
// Please read crc.pdf to understand how it all works.

#ifndef CRCUTIL_GENERIC_CRC_H_
#define CRCUTIL_GENERIC_CRC_H_

#include "base_types.h"     // uint8
#include "crc_casts.h"      // TO_BYTE(), Downcast<>.
#include "gf_util.h"        // GfUtil<Crc> class.
#include "platform.h"       // GCC_ALIGN_ATTRIBUTE(16)
#include "uint128_sse2.h"   // uint128_sse2 type (if necessary)

namespace crcutil {

#pragma pack(push, 16)

// Extends CRC by one byte.
// Technically, if degree of a polynomial does not exceed 8,
// right shift by 8 bits is not required, but who cares about CRC-8?
#define CRC_BYTE(table, crc, byte) do { \
  crc = ((sizeof(crc) > 1) ? SHIFT_RIGHT_SAFE(crc, 8) : 0) ^ \
        table->crc_word_[sizeof(Word) - 1][TO_BYTE(crc) ^ (byte)]; \
} while (0)

#define TABLE_ENTRY(table, byte, buf) \
  table[byte][Downcast<Word, uint8>(buf)]

#define TABLE_ENTRY_LAST(table, buf) \
  table[sizeof(Word) - 1][buf]

// Extends CRC by one word.
#define CRC_WORD(table, crc, buf) do { \
  buf ^= Downcast<Crc, Word>(crc); \
  if (sizeof(crc) > sizeof(buf)) { \
    crc = SHIFT_RIGHT_SAFE(crc, sizeof(buf) * 8); \
    crc ^= TABLE_ENTRY(table->crc_word_, 0, buf); \
  } else { \
    crc = TABLE_ENTRY(table->crc_word_, 0, buf); \
  } \
  buf >>= 8; \
  for (size_t byte = 1; byte < sizeof(buf) - 1; ++byte) { \
    crc ^= TABLE_ENTRY(table->crc_word_, byte, buf); \
    buf >>= 8; \
  } \
  crc ^= TABLE_ENTRY_LAST(table->crc_word_, buf); \
} while (0)

// Process beginning of data block byte by byte until source pointer
// becomes perfectly aligned on Word boundary.
#define ALIGN_ON_WORD_BOUNDARY(table, src, end, crc, Word) do { \
  while ((reinterpret_cast<size_t>(src) & (sizeof(Word) - 1)) != 0) { \
    if (src >= end) { \
      return (crc ^ table->Base().Canonize()); \
    } \
    CRC_BYTE(table, crc, *src); \
    src += 1; \
  } \
} while (0)


// On amd64, enforcing alignment is 2-4% slower on small (<= 64 bytes) blocks
// but 6-10% faster on larger blocks (>= 2KB).
// Break-even point (+-1%) is around 1KB (Q9650, E6600).
//
#define ALIGN_ON_WORD_BOUNDARY_IF_NEEDED(bytes, table, src, end, crc, Word) \
do { \
  if (sizeof(Word) > 8 || (bytes) > CRCUTIL_MIN_ALIGN_SIZE) { \
    ALIGN_ON_WORD_BOUNDARY(table, src, end, crc, Word); \
  } \
} while (0)

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4127)  // conditional expression is constant
#endif  // defined(_MSC_VER)

// Forward declarations.
template<typename CrcImplementation> class RollingCrc;

// Crc        is the type used internally and to return values of N-bit CRC.
//            It should be at least as large as "TableEntry" and "Word" but
//            may be larger (e.g. for 16-bit CRC, TableEntry and Word may be
//            set to uint16 but Crc may be set to uint32).
//
// TableEntry is the type of values stored in the tables.
//            To implement N-bit CRC, TableEntry should be large enough
//            to store N bits.
//
// Word       is the type used to read data sizeof(Word) at a time.
//            Ideally, it shoulde be "most suitable for given architecture"
//            integer type -- typically "size_t".
//
// kStride    is the number of words processed in interleaved manner by
//            CrcMultiword() and CrcWordblock(). Shall be either 3 or 4.
//            Optimal value depends on hardware architecture (AMD64, ARM, etc).
//
template<typename _Crc, typename _TableEntry, typename _Word, int kStride>
    class GenericCrc {
 public:
  // Make Crc, TableEntry, and Word types visible (used by RollingCrc etc.)
  typedef _Crc Crc;
  typedef _TableEntry TableEntry;
  typedef _Word Word;

  GenericCrc() {}

  // Initializes the tables given generating polynomial of degree.
  // If "canonical" is true, crc value will be XOR'ed with (-1) before and
  // after actual CRC computation.
  GenericCrc(const Crc &generating_polynomial, size_t degree, bool canonical) {
    Init(generating_polynomial, degree, canonical);
  }
  void Init(const Crc &generating_polynomial, size_t degree, bool canonical) {
    base_.Init(generating_polynomial, degree, canonical);

    // Instead of computing
    //    table[j][i] = MultiplyUnnormalized(i, 8, k),
    // for all i = 0...255, we may notice that
    // if i = 2**n then for all m = 1...(i-1)
    // MultiplyUnnormalized(i + m, 8, k) =
    //    MultiplyUnnormalized(i ^ m, 8, k) =
    //    MultiplyUnnormalized(i, 8, k) ^ MultiplyUnnormalized(m, 8, k) =
    //    MultiplyUnnormalized(i, 8, k) ^ crc_word_interleaved[j][m] =
    //    table[i] ^ table[m].
#if 0
    for (size_t j = 0; j < sizeof(Word); ++j) {
      Crc k = Base().XpowN((sizeof(Word) * kStride - 1 - j) * 8 + degree);
      for (size_t i = 0; i < 256; ++i) {
        Crc temp = Base().MultiplyUnnormalized(static_cast<Crc>(i), 8, k);
        this->crc_word_interleaved_[j][i] = Downcast<Crc, TableEntry>(temp);
      }
    }
#else
    for (size_t j = 0; j < sizeof(Word); ++j) {
      Crc k = Base().XpowN((sizeof(Word) * kStride - 1 - j) * 8 + degree);
      TableEntry *table = this->crc_word_interleaved_[j];
      table[0] = 0;  // Init 0s entry -- multiply 0 by anything yields 0.
      for (size_t i = 1; i < 256; i <<= 1) {
        TableEntry value = Downcast<Crc, TableEntry>(
            Base().MultiplyUnnormalized(static_cast<Crc>(i), 8, k));
        table[i] = value;
        for (size_t m = 1; m < i; ++m) {
          table[i + m] = value ^ table[m];
        }
      }
    }
#endif

#if 0
    for (size_t j = 0; j < sizeof(Word); ++j) {
      Crc k = Base().XpowN((sizeof(Word) - 1 - j) * 8 + degree);
      for (size_t i = 0; i < 256; ++i) {
        Crc temp = Base().MultiplyUnnormalized(static_cast<Crc>(i), 8, k);
        this->crc_word_[j][i] = Downcast<Crc, TableEntry>(temp);
      }
    }
#else
    for (size_t j = 0; j < sizeof(Word); ++j) {
      Crc k = Base().XpowN((sizeof(Word) - 1 - j) * 8 + degree);
      TableEntry *table = this->crc_word_[j];
      table[0] = 0;  // Init 0s entry -- multiply 0 by anything yields 0.
      for (size_t i = 1; i < 256; i <<= 1) {
        TableEntry value = Downcast<Crc, TableEntry>(
            Base().MultiplyUnnormalized(static_cast<Crc>(i), 8, k));
        table[i] = value;
        for (size_t m = 1; m < i; ++m) {
          table[i + m] = value ^ table[m];
        }
      }
    }
#endif
  }

  // Default CRC implementation
  Crc CrcDefault(const void *data, size_t bytes, const Crc &start) const {
#if HAVE_AMD64 || HAVE_I386
    return CrcMultiword(data, bytes, start);
#else
    // Very few CPUs have multiple ALUs and speculative execution
    // (Itanium is an exception) so sophisticated algorithms will
    // not perform better than good old Sarwate algorithm.
    return CrcByteUnrolled(data, bytes, start);
#endif  // HAVE_AMD64 || HAVE_I386
  }

  // Returns base class.
  const GfUtil<Crc> &Base() const { return base_; }

 protected:
  // Canonical, byte-by-byte CRC computation.
  Crc CrcByte(const void *data, size_t bytes, const Crc &start) const {
    const uint8 *src = static_cast<const uint8 *>(data);
    Crc crc = start ^ Base().Canonize();
    for (const uint8 *end = src + bytes; src < end; ++src) {
      CRC_BYTE(this, crc, *src);
    }
    return (crc ^ Base().Canonize());
  }

  // Byte-by-byte CRC with main loop unrolled.
  Crc CrcByteUnrolled(const void *data, size_t bytes, const Crc &start) const {
    if (bytes == 0) {
      return start;
    }

    const uint8 *src = static_cast<const uint8 *>(data);
    const uint8 *end = src + bytes;
    Crc crc = start ^ Base().Canonize();

    // Unroll loop 4 times.
    end -= 3;
    for (; src < end; src += 4) {
      PREFETCH(src);
      CRC_BYTE(this, crc, src[0]);
      CRC_BYTE(this, crc, src[1]);
      CRC_BYTE(this, crc, src[2]);
      CRC_BYTE(this, crc, src[3]);
    }
    end += 3;

    // Compute CRC of remaining bytes.
    for (; src < end; ++src) {
      CRC_BYTE(this, crc, *src);
    }

    return (crc ^ Base().Canonize());
  }

  // Canonical, byte-by-byte CRC computation.
  Crc CrcByteWord(const void *data, size_t bytes, const Crc &start) const {
    const uint8 *src = static_cast<const uint8 *>(data);
    const uint8 *end = src + bytes;
    Crc crc0 = start ^ Base().Canonize();

    ALIGN_ON_WORD_BOUNDARY_IF_NEEDED(bytes, this, src, end, crc0, Crc);
    if (src >= end) {
      return (crc0 ^ Base().Canonize());
    }

    // Process 4*sizeof(Crc) bytes at a time.
    end -= 4 * sizeof(Crc) - 1;
    for (; src < end; src += 4 * sizeof(Crc)) {
      for (size_t i = 0; i < 4; ++i) {
        crc0 ^= reinterpret_cast<const Crc *>(src)[i];
        if (i == 0) {
          PREFETCH(src);
        }
        for (size_t byte = 0; byte < sizeof(crc0); ++byte) {
          CRC_BYTE(this, crc0, 0);
        }
      }
    }
    end += 4 * sizeof(Crc) - 1;

    // Process sizeof(Crc) bytes at a time.
    end -= sizeof(Crc) - 1;
    for (; src < end; src += sizeof(Crc)) {
      crc0 ^= reinterpret_cast<const Crc *>(src)[0];
      for (size_t byte = 0; byte < sizeof(crc0); ++byte) {
        CRC_BYTE(this, crc0, 0);
      }
    }
    end += sizeof(Crc) - 1;

    // Compute CRC of remaining bytes.
    for (;src < end; ++src) {
      CRC_BYTE(this, crc0, *src);
    }

    return (crc0 ^ Base().Canonize());
  }

  // Faster, word-by-word CRC.
  Crc CrcWord(const void *data, size_t bytes, const Crc &start) const {
    const uint8 *src = static_cast<const uint8 *>(data);
    const uint8 *end = src + bytes;
    Crc crc0 = start ^ Base().Canonize();

    ALIGN_ON_WORD_BOUNDARY_IF_NEEDED(bytes, this, src, end, crc0, Word);
    if (src >= end) {
      return (crc0 ^ Base().Canonize());
    }

    // Process 4 sizeof(Word) bytes at once.
    end -= 4 * sizeof(Word) - 1;
    for (; src < end; src += 4 * sizeof(Word)) {
      Word buf0 = reinterpret_cast<const Word *>(src)[0];
      PREFETCH(src);
      CRC_WORD(this, crc0, buf0);
      buf0 = reinterpret_cast<const Word *>(src)[1];
      CRC_WORD(this, crc0, buf0);
      buf0 = reinterpret_cast<const Word *>(src)[2];
      CRC_WORD(this, crc0, buf0);
      buf0 = reinterpret_cast<const Word *>(src)[3];
      CRC_WORD(this, crc0, buf0);
    }
    end += 4 * sizeof(Word) - 1;

    // Process sizeof(Word) bytes at a time.
    end -= sizeof(Word) - 1;
    for (; src < end; src += sizeof(Word)) {
      Word buf0 = reinterpret_cast<const Word *>(src)[0];
      CRC_WORD(this, crc0, buf0);
    }
    end += sizeof(Word) - 1;

    // Compute CRC of remaining bytes.
    for (;src < end; ++src) {
      CRC_BYTE(this, crc0, *src);
    }

    return (crc0 ^ Base().Canonize());
  }

#define REPEAT_FROM_1(macro) \
  macro(1); \
  macro(2); \
  macro(3); \
  macro(4); \
  macro(5); \
  macro(6); \
  macro(7);

#define REPEAT_FROM_0(macro) \
  macro(0); \
  REPEAT_FROM_1(macro)

  // Faster, process adjusent blocks in parallel and concatenate CRCs.
  Crc CrcBlockword(const void *data, size_t bytes, const Crc &start) const {
    if (kStride < 2 || kStride > 8) {
      // Unsupported configuration;
      // fall back to something sensible.
      return CrcWord(data, bytes, start);
    }

    const uint8 *src = static_cast<const uint8 *>(data);
    const uint8 *end = src + bytes;
    Crc crc0 = start ^ Base().Canonize();
    enum {
      // Add 16 to avoid false L1 cache collisions.
      kStripe = (15*1024 + 16) & ~(sizeof(Word) - 1),
    };

    ALIGN_ON_WORD_BOUNDARY_IF_NEEDED(bytes, this, src, end, crc0, Word);
    if (src >= end) {
      return (crc0 ^ Base().Canonize());
    }

    end -= kStride * kStripe - 1;
    if (src < end) {
      Crc x_pow_8kStripe = Base().Xpow8N(kStripe);
      do {
        const uint8 *stripe_end = src + kStripe;

#define INIT_CRC(reg) \
        Crc crc##reg; \
        if (kStride >= reg) { \
          crc##reg = 0; \
        }
        REPEAT_FROM_1(INIT_CRC);
#undef INIT_CRC

        do {
#define FIRST(reg) \
          Word buf##reg; \
          if (kStride > reg) { \
            buf##reg = reinterpret_cast<const Word *>(src + reg * kStripe)[0]; \
            buf##reg ^= Downcast<Crc, Word>(crc##reg); \
            if (sizeof(crc##reg) > sizeof(buf##reg)) { \
              crc##reg = SHIFT_RIGHT_SAFE(crc##reg, sizeof(buf##reg) * 8); \
              crc##reg ^= TABLE_ENTRY(this->crc_word_, 0, buf##reg); \
            } else { \
              crc##reg = TABLE_ENTRY(this->crc_word_, 0, buf##reg); \
            } \
            buf##reg >>= 8; \
          }
          REPEAT_FROM_0(FIRST);
#undef FIRST

          for (size_t byte = 1; byte < sizeof(buf0) - 1; ++byte) {
#define NEXT(reg) do { \
            if (kStride > reg) { \
              crc##reg ^= TABLE_ENTRY(this->crc_word_, byte, buf##reg); \
              buf##reg >>= 8; \
            } \
} while (0)
            REPEAT_FROM_0(NEXT);
#undef NEXT
          }

#define LAST(reg) do { \
          if (kStride > reg) { \
            crc##reg ^= TABLE_ENTRY_LAST(this->crc_word_, buf##reg); \
          } \
} while (0)
          REPEAT_FROM_0(LAST);
#undef LAST

          src += sizeof(Word);
        } while (src < stripe_end);

#if 0
// The code is left for illustrational purposes only.
#define COMBINE(reg) do { \
        if (reg > 0 && kStride > reg) { \
          crc0 = Base().ChangeStartValue(crc##reg, kStripe, 0, crc0); \
        } \
} while (0)
#else
#define COMBINE(reg) do { \
        if (reg > 0 && kStride > reg) { \
          crc0 = crc##reg ^ Base().Multiply(crc0, x_pow_8kStripe); \
        } \
} while (0)
#endif
        REPEAT_FROM_0(COMBINE);
#undef COMBINE

        src += (kStride - 1) * kStripe;
      }
      while (src < end);
    }
    end += kStride * kStripe - 1;

    // Process sizeof(Word) bytes at a time.
    end -= sizeof(Word) - 1;
    for (; src < end; src += sizeof(Word)) {
      Word buf0 = reinterpret_cast<const Word *>(src)[0];
      CRC_WORD(this, crc0, buf0);
    }
    end += sizeof(Word) - 1;

    // Compute CRC of remaining bytes.
    for (;src < end; ++src) {
      CRC_BYTE(this, crc0, *src);
    }

    return (crc0 ^ Base().Canonize());
  }

  // Fastest, interleaved multi-byte CRC.
  Crc CrcMultiword(const void *data, size_t bytes, const Crc &start) const {
    if (kStride < 2 || kStride > 8) {
      // Unsupported configuration;
      // fall back to something sensible.
      return CrcWord(data, bytes, start);
    }

    const uint8 *src = static_cast<const uint8 *>(data);
    const uint8 *end = src + bytes;
    Crc crc0 = start ^ Base().Canonize();

    ALIGN_ON_WORD_BOUNDARY_IF_NEEDED(bytes, this, src, end, crc0, Word);
    if (src >= end) {
      return (crc0 ^ Base().Canonize());
    }

    // Process kStride Word registers at once;
    // should have have at least 2*kInterleaveBytes of data to start.
    end -= 2*kInterleaveBytes - 1;
    if (src < end) {
      Crc crc_carryover;
      if (sizeof(Crc) > sizeof(Word)) {
        // crc_carryover is used if and only if Crc is wider than Word.
        crc_carryover = 0;
      }
#define INIT_CRC(reg) \
      Crc crc##reg; \
      if (reg > 0 && kStride > reg) { \
        crc##reg = 0; \
      }
      REPEAT_FROM_1(INIT_CRC);
#undef INIT_CRC

#define INIT_BUF(reg) \
      Word buf##reg; \
      if (kStride > reg) { \
        buf##reg = reinterpret_cast<const Word *>(src)[reg]; \
      }
      REPEAT_FROM_0(INIT_BUF);
#undef INIT_BUF

      do {
        PREFETCH(src);
        src += kInterleaveBytes;

        if (sizeof(Crc) > sizeof(Word)) {
          crc0 ^= crc_carryover;
        }

#define FIRST(reg, next_reg) do { \
        if (kStride > reg) { \
          buf##reg ^= Downcast<Crc, Word>(crc##reg); \
          if (sizeof(Crc) > sizeof(Word)) { \
            if (reg < kStride - 1) { \
              crc##next_reg ^= SHIFT_RIGHT_SAFE(crc##reg, 8 * sizeof(buf0)); \
            } else { \
              crc_carryover = SHIFT_RIGHT_SAFE(crc##reg, 8 * sizeof(buf0)); \
            } \
          } \
          crc##reg = TABLE_ENTRY(this->crc_word_interleaved_, 0, buf##reg); \
          buf##reg >>= 8; \
        } \
} while (0)
        FIRST(0, 1);
        FIRST(1, 2);
        FIRST(2, 3);
        FIRST(3, 4);
        FIRST(4, 5);
        FIRST(5, 6);
        FIRST(6, 7);
        FIRST(7, 0);
#undef FIRST

        for (size_t byte = 1; byte < sizeof(Word) - 1; ++byte) {
#define NEXT(reg) do { \
          if (kStride > reg) { \
            crc##reg ^= \
                TABLE_ENTRY(this->crc_word_interleaved_, byte, buf##reg); \
            buf##reg >>= 8; \
          } \
} while(0)
          REPEAT_FROM_0(NEXT);
#undef NEXT
        }

#define LAST(reg) do { \
        if (kStride > reg) { \
          crc##reg ^= TABLE_ENTRY_LAST(this->crc_word_interleaved_, buf##reg); \
          buf##reg = reinterpret_cast<const Word *>(src)[reg]; \
        } \
} while(0)
        REPEAT_FROM_0(LAST);
#undef LAST
      }
      while (src < end);

      if (sizeof(Crc) > sizeof(Word)) {
        crc0 ^= crc_carryover;
      }

#define COMBINE(reg) do { \
      if (kStride > reg) { \
        if (reg != 0) { \
          crc0 ^= crc##reg; \
        } \
        CRC_WORD(this, crc0, buf##reg); \
      } \
} while (0)
      REPEAT_FROM_0(COMBINE);
#undef COMBINE

      src += kInterleaveBytes;
    }
    end += 2*kInterleaveBytes - 1;

    // Process sizeof(Word) bytes at once.
    end -= sizeof(Word) - 1;
    for (; src < end; src += sizeof(Word)) {
      Word buf0 = reinterpret_cast<const Word *>(src)[0];
      CRC_WORD(this, crc0, buf0);
    }
    end += sizeof(Word) - 1;

    // Compute CRC of remaining bytes.
    for (;src < end; ++src) {
      CRC_BYTE(this, crc0, *src);
    }

    return (crc0 ^ Base().Canonize());
  }

 protected:
  enum {
    kInterleaveBytes = sizeof(Word) * kStride,
  };

  // Multiplication tables used by CRCs.
  TableEntry crc_word_interleaved_[sizeof(Word)][256];
  TableEntry crc_word_[sizeof(Word)][256];

  // Base class stored after CRC tables so that the most frequently
  // used table is at offset 0 and may be accessed faster.
  GfUtil<Crc> base_;

  friend class RollingCrc< GenericCrc<Crc, TableEntry, Word, kStride> >;

 private:
  // CrcMultiword on amd64 may run at 1.2 CPU cycles per byte which is
  // noticeably faster than CrcWord (2.2-2.6 cycles/byte depending on
  // hardware and compiler). However, there are problems with compilers.
  //
  // Test system: P45 chipset, Intel Q9650 CPU, 800MHz 4-4-4-12 memory.
  //
  // 64-bit compiler, <= 64-bit CRC, 64-bit tables, 64-bit reads:
  // CL 15.00.307291.1  C++   >1.2< CPU cycles/byte
  // ICL 11.1.051 -O3   C++    1.5  CPU cycles/byte
  // GCC 4.5 -O3        C++    2.0  CPU cycles/byte
  // GCC 4.x -O3        ASM   >1.2< CPU cycles/byte
  //
  // 32-bit compiler, MMX used, <= 64-bit CRC, 64-bit tables, 64-bit reads
  // CL 15.00.307291.1  C++   2.0  CPU cycles/byte
  // GCC 4.5 -O3        C++   1.9  CPU cycles/byte
  // ICL 11.1.051 -S    C++   1.6  CPU cycles/byte
  // GCC 4.x -O3        ASM  >1.3< CPU cycles/byte
  //
  // So, use inline ASM code for GCC for both i386 and amd64.

  Crc CrcMultiwordI386Mmx(
          const void *data, size_t bytes, const Crc &start) const;
  Crc CrcMultiwordGccAmd64(
          const void *data, size_t bytes, const Crc &start) const;
  Crc CrcMultiwordGccAmd64Sse2(
          const uint8 *src, const uint8 *end, const Crc &start) const;
} GCC_ALIGN_ATTRIBUTE(16);

#undef REPEAT_FROM_0
#undef REPEAT_FROM_1


// Specialized variants.
#if CRCUTIL_USE_ASM

#if (defined(__GNUC__) && (HAVE_AMD64 || (HAVE_I386 && HAVE_MMX)))

// Declare specialized functions.
template<> uint64 GenericCrc<uint64, uint64, uint64, 4>::CrcMultiword(
    const void *data, size_t bytes, const uint64 &start) const;

#if HAVE_AMD64 && HAVE_SSE2
template<>
uint128_sse2
GenericCrc<uint128_sse2, uint128_sse2, uint64, 4>::CrcMultiword(
    const void *data, size_t bytes, const uint128_sse2 &start) const;
#endif  // HAVE_AMD64 && HAVE_SSE2

#elif defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 150030729 && \
      (HAVE_I386 && HAVE_MMX)

// Work around bug in MSC (present at least in v. 15.00.30729.1)
template<> uint64 GenericCrc<uint64, uint64, uint64, 4>::CrcMultiwordI386Mmx(
    const void *data,
    size_t bytes,
    const uint64 &start) const;
template<> __forceinline
uint64 GenericCrc<uint64, uint64, uint64, 4>::CrcMultiword(
    const void *data,
    size_t bytes,
    const uint64 &start) const {
  typedef uint64 Word;
  typedef uint64 Crc;
  if (bytes <= 12) {
    const uint8 *src = static_cast<const uint8 *>(data);
    uint64 crc = start ^ Base().Canonize();
    for (const uint8 *end = src + bytes; src < end; ++src) {
      CRC_BYTE(this, crc, *src);
    }
    return (crc ^ Base().Canonize());
  }
  return CrcMultiwordI386Mmx(data, bytes, start);
}

#endif  // (defined(__GNUC__) && (HAVE_AMD64 || (HAVE_I386 && HAVE_MMX)))

#endif  // CRCUTIL_USE_ASM


#pragma pack(pop)

}  // namespace crcutil

#endif  // CRCUTIL_GENERIC_CRC_H_