File Coverage

crc32csb8.c
Criterion Covered Total %
statement 27 28 96.4
branch 6 8 75.0
condition n/a
subroutine n/a
pod n/a
total 33 36 91.6


line stmt bran cond sub pod time code
1             // Copyright 2024 Marc A. Lehmann
2             // Copyright 2008,2009,2010 Massachusetts Institute of Technology.
3             // All rights reserved. Use of this source code is governed by a
4             // BSD-style license that can be found in the LICENSE file.
5              
6             #include "crc32ctables.c"
7              
8             // Implementations adapted from Intel's Slicing By 8 Sourceforge Project
9             // http://sourceforge.net/projects/slicing-by-8/
10             /*++
11             *
12             * Copyright (c) 2004-2006 Intel Corporation - All Rights Reserved
13             *
14             * This software program is licensed subject to the BSD License,
15             * available at http://www.opensource.org/licenses/bsd-license.html
16             *
17             * Abstract: The main routine
18             *
19             --*/
20              
21 8           static uint32_t crc32cSlicingBy8(uint32_t crc, const void* data, size_t length) {
22             size_t li;
23 8           const char* p_buf = (const char*) data;
24              
25             // Handle leading misaligned bytes
26 8           size_t initial_bytes = (sizeof(int32_t) - (intptr_t)p_buf) & (sizeof(int32_t) - 1);
27 8 50         if (length < initial_bytes) initial_bytes = length;
28 8 50         for (li = 0; li < initial_bytes; li++) {
29 0           crc = crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^ (crc >> 8);
30             }
31              
32 8           length -= initial_bytes;
33 8           size_t running_length = length & ~(sizeof(uint64_t) - 1);
34 8           size_t end_bytes = length - running_length;
35              
36 23 100         for (li = 0; li < running_length/8; li++) {
37 15           crc ^= *(uint32_t*) p_buf;
38 15           p_buf += 4;
39 15           uint32_t term1 = crc_tableil8_o88[crc & 0x000000FF] ^
40 15           crc_tableil8_o80[(crc >> 8) & 0x000000FF];
41 15           uint32_t term2 = crc >> 16;
42 15           crc = term1 ^
43 15           crc_tableil8_o72[term2 & 0x000000FF] ^
44 15           crc_tableil8_o64[(term2 >> 8) & 0x000000FF];
45 15           term1 = crc_tableil8_o56[(*(uint32_t *)p_buf) & 0x000000FF] ^
46 15           crc_tableil8_o48[((*(uint32_t *)p_buf) >> 8) & 0x000000FF];
47              
48 15           term2 = (*(uint32_t *)p_buf) >> 16;
49 15           crc = crc ^ term1 ^
50 15           crc_tableil8_o40[term2 & 0x000000FF] ^
51 15           crc_tableil8_o32[(term2 >> 8) & 0x000000FF];
52 15           p_buf += 4;
53             }
54              
55 22 100         for (li=0; li < end_bytes; li++) {
56 14           crc = crc_tableil8_o32[(crc ^ *p_buf++) & 0x000000FF] ^ (crc >> 8);
57             }
58              
59 8           return crc;
60             }
61