From: "Darrick J. Wong" <djwong@us.ibm.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Theodore Tso <tytso@mit.edu>,
Joakim Tjernlund <joakim.tjernlund@transmode.se>,
Bob Pearson <rpearson@systemfabricworks.com>,
linux-kernel <linux-kernel@vger.kernel.org>,
Andreas Dilger <adilger.kernel@dilger.ca>,
linux-crypto <linux-crypto@vger.kernel.org>,
linux-fsdevel <linux-fsdevel@vger.kernel.org>,
Mingming Cao <cmm@us.ibm.com>,
linux-ext4@vger.kernel.org,
Herbert Xu <herbert@gondor.apana.org.au>
Subject: [PATCH 07/14] crc32: Make CRC_*_BITS definition correspond to actual bit counts
Date: Thu, 01 Dec 2011 12:14:30 -0800 [thread overview]
Message-ID: <20111201201430.5876.93627.stgit@elm3c44.beaverton.ibm.com> (raw)
In-Reply-To: <20111201201341.5876.83743.stgit@elm3c44.beaverton.ibm.com>
crc32.c provides a choice of one of several algorithms for
computing the LSB and LSB versions of the CRC32 checksum
based on the parameters CRC_LE_BITS and CRC_BE_BITS. In the
original version the values 1, 2, 4 and 8 respectively selected
versions of the alrogithm that computed the crc 1, 2, 4 and 32
bits as a time. This patch series adds a new version that computes
the CRC 64 bits at a time. To make things easier to understand
the parameter has been reinterpreted to actually stand for the
number of bits processed in each step of the algorithm so that
the old value 8 has been replaced with the value 32. This also
allows us to add in a widely used crc algorithm that
computes the crc 8 bits at a time called the Sarwate algorithm.
From: Bob Pearson <rpearson@systemfabricworks.com>
Signed-off-by: Bob Pearson <rpearson@systemfabricworks.com>
[djwong@us.ibm.com: Minor changelog tweaks]
Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>
---
lib/crc32.c | 17 ++++++++++++++---
lib/crc32defs.h | 18 ++++++++++--------
lib/gen_crc32table.c | 11 ++++++++++-
3 files changed, 34 insertions(+), 12 deletions(-)
diff --git a/lib/crc32.c b/lib/crc32.c
index ff6bb9a..157b35f 100644
--- a/lib/crc32.c
+++ b/lib/crc32.c
@@ -27,13 +27,13 @@
#include <linux/types.h>
#include "crc32defs.h"
-#if CRC_LE_BITS == 8
+#if CRC_LE_BITS > 8
# define tole(x) (__force u32) __constant_cpu_to_le32(x)
#else
# define tole(x) (x)
#endif
-#if CRC_BE_BITS == 8
+#if CRC_BE_BITS > 8
# define tobe(x) (__force u32) __constant_cpu_to_be32(x)
#else
# define tobe(x) (x)
@@ -45,7 +45,7 @@ MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
MODULE_DESCRIPTION("Ethernet CRC32 calculations");
MODULE_LICENSE("GPL");
-#if CRC_LE_BITS == 8 || CRC_BE_BITS == 8
+#if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
static inline u32
crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
@@ -126,6 +126,12 @@ u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
crc = (crc >> 4) ^ crc32table_le[0][crc & 15];
}
# elif CRC_LE_BITS == 8
+ /* aka Sarwate algorithm */
+ while (len--) {
+ crc ^= *p++;
+ crc = (crc >> 8) ^ crc32table_le[0][crc & 255];
+ }
+# else
const u32 (*tab)[] = crc32table_le;
crc = (__force u32) __cpu_to_le32(crc);
@@ -169,6 +175,11 @@ u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
crc = (crc << 4) ^ crc32table_be[0][crc >> 28];
}
# elif CRC_BE_BITS == 8
+ while (len--) {
+ crc ^= *p++ << 24;
+ crc = (crc << 8) ^ crc32table_be[0][crc >> 24];
+ }
+# else
const u32 (*tab)[] = crc32table_be;
crc = (__force u32) __cpu_to_be32(crc);
diff --git a/lib/crc32defs.h b/lib/crc32defs.h
index f5a5401..daa3a5e 100644
--- a/lib/crc32defs.h
+++ b/lib/crc32defs.h
@@ -6,27 +6,29 @@
#define CRCPOLY_LE 0xedb88320
#define CRCPOLY_BE 0x04c11db7
-/* How many bits at a time to use. Requires a table of 4<<CRC_xx_BITS bytes. */
-/* For less performance-sensitive, use 4 */
+/* How many bits at a time to use. Valid values are 1, 2, 4, 8, and 32. */
+/* For less performance-sensitive, use 4 or 8 */
#ifndef CRC_LE_BITS
-# define CRC_LE_BITS 8
+# define CRC_LE_BITS 32
#endif
#ifndef CRC_BE_BITS
-# define CRC_BE_BITS 8
+# define CRC_BE_BITS 32
#endif
/*
* Little-endian CRC computation. Used with serial bit streams sent
* lsbit-first. Be sure to use cpu_to_le32() to append the computed CRC.
*/
-#if CRC_LE_BITS > 8 || CRC_LE_BITS < 1 || CRC_LE_BITS & CRC_LE_BITS-1
-# error CRC_LE_BITS must be a power of 2 between 1 and 8
+#if CRC_LE_BITS > 32 || CRC_LE_BITS < 1 || CRC_LE_BITS == 16 || \
+ CRC_LE_BITS & CRC_LE_BITS-1
+# error "CRC_LE_BITS must be one of {1, 2, 4, 8, 32}"
#endif
/*
* Big-endian CRC computation. Used with serial bit streams sent
* msbit-first. Be sure to use cpu_to_be32() to append the computed CRC.
*/
-#if CRC_BE_BITS > 8 || CRC_BE_BITS < 1 || CRC_BE_BITS & CRC_BE_BITS-1
-# error CRC_BE_BITS must be a power of 2 between 1 and 8
+#if CRC_BE_BITS > 32 || CRC_BE_BITS < 1 || CRC_BE_BITS == 16 || \
+ CRC_BE_BITS & CRC_BE_BITS-1
+# error "CRC_BE_BITS must be one of {1, 2, 4, 8, 32}"
#endif
diff --git a/lib/gen_crc32table.c b/lib/gen_crc32table.c
index eced769..99ac744 100644
--- a/lib/gen_crc32table.c
+++ b/lib/gen_crc32table.c
@@ -4,8 +4,17 @@
#define ENTRIES_PER_LINE 4
+#if CRC_LE_BITS <= 8
#define LE_TABLE_SIZE (1 << CRC_LE_BITS)
+#else
+#define LE_TABLE_SIZE 256
+#endif
+
+#if CRC_BE_BITS <= 8
#define BE_TABLE_SIZE (1 << CRC_BE_BITS)
+#else
+#define BE_TABLE_SIZE 256
+#endif
static uint32_t crc32table_le[4][256];
static uint32_t crc32table_be[4][256];
@@ -24,7 +33,7 @@ static void crc32init_le(void)
crc32table_le[0][0] = 0;
- for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
+ for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
crc32table_le[0][i + j] = crc ^ crc32table_le[0][j];
next prev parent reply other threads:[~2011-12-01 20:14 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-01 20:13 [PATCH v5.2 00/14] crc32c: Add faster algorithm and self-test code Darrick J. Wong
2011-12-01 20:13 ` [PATCH 01/14] crc32: removed two instances of trailing whitespaces Darrick J. Wong
2011-12-01 20:13 ` [PATCH 02/14] crc32: Move long comment about crc32 fundamentals to Documentation/ Darrick J. Wong
2011-12-01 20:14 ` [PATCH 03/14] crc32: Simplify unit test code Darrick J. Wong
2011-12-01 20:14 ` [PATCH 04/14] crc32: Speed up memory table access on powerpc Darrick J. Wong
2011-12-01 20:14 ` [PATCH 05/14] crc32: Miscellaneous cleanups Darrick J. Wong
2011-12-01 20:14 ` [PATCH 06/14] crc32: Fix mixing of endian-specific types Darrick J. Wong
2011-12-01 20:14 ` Darrick J. Wong [this message]
2011-12-01 20:14 ` [PATCH 08/14] crc32: Add slice-by-8 algorithm to existing code Darrick J. Wong
2011-12-01 20:14 ` [PATCH 09/14] crc32: Optimize loop counter for x86 Darrick J. Wong
2011-12-01 20:14 ` [PATCH 10/14] crc32: Add note about this patchset to crc32.c Darrick J. Wong
2011-12-01 20:14 ` [PATCH 11/14] crc32: Bolt on crc32c Darrick J. Wong
2011-12-01 20:15 ` [PATCH 12/14] crypto: crc32c should use library implementation Darrick J. Wong
2011-12-01 20:15 ` [PATCH 13/14] crc32: Add self-test code for crc32c Darrick J. Wong
2011-12-01 20:15 ` [PATCH 14/14] crc32: Select an algorithm via kconfig Darrick J. Wong
2011-12-02 0:25 ` Herbert Xu
2011-12-03 2:36 ` Darrick J. Wong
2011-12-12 22:58 ` Darrick J. Wong
2011-12-12 23:10 ` Bob Pearson
2011-12-13 6:32 ` Darrick J. Wong
2011-12-13 8:27 ` Joakim Tjernlund
2011-12-13 18:36 ` Darrick J. Wong
2011-12-01 20:20 ` [PATCH v5.2 00/14] crc32c: Add faster algorithm and self-test code Joel Becker
2011-12-01 20:31 ` Darrick J. Wong
2011-12-02 0:23 ` Herbert Xu
2011-12-03 2:30 ` Darrick J. Wong
2011-12-03 11:00 ` Herbert Xu
2012-01-07 5:50 [PATCH v5.3 " Darrick J. Wong
2012-01-07 5:51 ` [PATCH 07/14] crc32: Make CRC_*_BITS definition correspond to actual bit counts Darrick J. Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20111201201430.5876.93627.stgit@elm3c44.beaverton.ibm.com \
--to=djwong@us.ibm.com \
--cc=adilger.kernel@dilger.ca \
--cc=akpm@linux-foundation.org \
--cc=cmm@us.ibm.com \
--cc=herbert@gondor.apana.org.au \
--cc=joakim.tjernlund@transmode.se \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rpearson@systemfabricworks.com \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).