Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/ext2fs: port fast crc32c routines for Intel CPUs from kernel. #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/ext2fs/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ OBJS= $(DEBUGFS_LIB_OBJS) $(RESIZE_LIB_OBJS) $(E2IMAGE_LIB_OBJS) \
closefs.o \
crc16.o \
crc32c.o \
crc32c_intel.o \
crc32c_intel_pcl.o \
csum.o \
dblist.o \
dblist_dir.o \
Expand Down Expand Up @@ -154,6 +156,8 @@ SRCS= ext2_err.c \
$(srcdir)/closefs.c \
$(srcdir)/crc16.c \
$(srcdir)/crc32c.c \
$(srcdir)/crc32c_intel.c \
$(srcdir)/crc32c_intel_pcl.S \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that the makefile rules have a .S -> .o rule, and that compilers will accept the .S as a assembly file as input. That's not very portable. There are people who build e2fsprogs on NetBSD, FreeBSD, MacOS, Windows, Android, OpenSolaris, AIX, etc. There seems to be an unfortunate assumption that "all the world's x86" and "all the world's Linux", or "the only compiler is gcc" or "the only compiler is clang".

$(srcdir)/gen_crc32ctable.c \
$(srcdir)/csum.c \
$(srcdir)/dblist.c \
Expand Down Expand Up @@ -520,8 +524,12 @@ tst_csum: csum.c $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR) $(STATIC_LIBE2P) \
$(ALL_CFLAGS) $(ALL_LDFLAGS) $(STATIC_LIBEXT2FS) \
$(STATIC_LIBCOM_ERR) $(STATIC_LIBE2P) $(SYSLIBS)

tst_crc32c: $(srcdir)/crc32c.c $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR)
$(Q) $(CC) $(ALL_LDFLAGS) $(ALL_CFLAGS) -o tst_crc32c $(srcdir)/crc32c.c \
tst_crc32c: $(srcdir)/crc32c.c $(srcdir)/crc32c_intel.c \
$(srcdir)/crc32c_intel_pcl.S \
$(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR)
$(Q) $(CC) $(ALL_LDFLAGS) $(ALL_CFLAGS) -o tst_crc32c \
$(srcdir)/crc32c.c $(srcdir)/crc32c_intel.c \
$(srcdir)/crc32c_intel_pcl.S \
-DUNITTEST $(STATIC_LIBEXT2FS) $(STATIC_LIBCOM_ERR) \
$(SYSLIBS)

Expand Down Expand Up @@ -766,6 +774,8 @@ crc32c.o: $(srcdir)/crc32c.c $(top_builddir)/lib/config.h \
$(srcdir)/ext3_extents.h $(top_srcdir)/lib/et/com_err.h $(srcdir)/ext2_io.h \
$(top_builddir)/lib/ext2fs/ext2_err.h $(srcdir)/ext2_ext_attr.h \
$(srcdir)/hashmap.h $(srcdir)/bitops.h crc32c_table.h
crc32c_intel.o: $(srcdir)/crc32c_intel.c
crc32c_intel_pcl.o: $(srcdir)/crc32c_intel_pcl.S
gen_crc32ctable.o: $(srcdir)/gen_crc32ctable.c $(srcdir)/crc32c_defs.h
csum.o: $(srcdir)/csum.c $(top_builddir)/lib/config.h \
$(top_builddir)/lib/dirpaths.h $(srcdir)/ext2_fs.h \
Expand Down
8 changes: 8 additions & 0 deletions lib/ext2fs/crc32c.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@

#include "crc32c_table.h"

#if defined(__x86_64__) || defined(__i386__)
int crc32c_intel_le(uint32_t *crc, unsigned char const *data, size_t length);
#endif

#if CRC_LE_BITS > 8 || CRC_BE_BITS > 8

#if CRC_LE_BITS < 64 && CRC_BE_BITS < 64
Expand Down Expand Up @@ -191,6 +195,10 @@ static inline uint32_t crc32_le_generic(uint32_t crc, unsigned char const *p,

uint32_t ext2fs_crc32c_le(uint32_t crc, unsigned char const *p, size_t len)
{
#if defined(__x86_64__) || defined(__i386__)
if (crc32c_intel_le(&crc, p, len))
return crc;
#endif
return crc32_le_generic(crc, p, len, crc32ctable_le, CRC32C_POLY_LE);
}

Expand Down
109 changes: 109 additions & 0 deletions lib/ext2fs/crc32c_intel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Using hardware provided CRC32 instruction to accelerate the CRC32 disposal.
* CRC32C polynomial:0x1EDC6F41(BE)/0x82F63B78(LE)
* CRC32 is a new instruction in Intel SSE4.2, the reference can be found at:
* http://www.intel.com/products/processor/manuals/
* Intel(R) 64 and IA-32 Architectures Software Developer's Manual
* Volume 2A: Instruction Set Reference, A-M
*
* Copyright (C) 2008 Intel Corporation
* Authors: Austin Zhang <[email protected]>
* Kent Liu <[email protected]>
* Dinglan Peng <[email protected]>
*/
#if defined(__x86_64__) || defined(__i386__)
#include <stdint.h>
#include <stddef.h>
#include <cpuid.h>

#define CHKSUM_BLOCK_SIZE 1
#define CHKSUM_DIGEST_SIZE 4

#define SCALE_F sizeof(unsigned long)

#ifdef __x86_64__
#define CRC32_INST "crc32q %1, %q0"
#else
#define CRC32_INST "crc32l %1, %0"
#endif

#ifdef __x86_64__
unsigned int crc_pcl(const uint8_t *buffer, int len,
unsigned int crc_init);
#endif

#define HAVE_SSE42 1
#define NO_SSE42 2
#define HAVE_PCL 4
#define NO_PCL 8

static int support_features;

static void detect_features(void) {
unsigned int eax, ebx, ecx, edx;
int features = 0;

if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
features = NO_SSE42 | NO_PCL;
if (ecx & bit_SSE4_2)
features |= HAVE_SSE42;
else
features |= NO_SSE42;
#ifdef __x86_64__
if (ecx & bit_PCLMUL)
features |= HAVE_PCL;
else
features |= NO_PCL;
#endif

support_features = features;
}

static uint32_t crc32c_intel_le_hw_byte(uint32_t crc, unsigned char const *data, size_t length)
{
while (length--) {
asm("crc32b %1, %0"
: "+r" (crc) : "rm" (*data));
data++;
}

return crc;
}

static uint32_t crc32c_intel_le_hw(uint32_t crc, unsigned char const *p, size_t len)
{
unsigned int iquotient = len / SCALE_F;
unsigned int iremainder = len % SCALE_F;
unsigned long *ptmp = (unsigned long *)p;

while (iquotient--) {
asm(CRC32_INST
: "+r" (crc) : "rm" (*ptmp));
ptmp++;
}

if (iremainder)
crc = crc32c_intel_le_hw_byte(crc, (unsigned char *)ptmp,
iremainder);

return crc;
}

int crc32c_intel_le(uint32_t *crc, unsigned char const *data, size_t length)
{
if (!support_features)
detect_features();
#ifdef __x86_64__
if (support_features & HAVE_PCL) {
*crc = crc_pcl(data, length, *crc);
return 1;
} else
#endif
if (support_features & HAVE_SSE42) {
*crc = crc32c_intel_le_hw(*crc, data, length);
return 1;
}
return 0;
}
#endif
Loading