-
Notifications
You must be signed in to change notification settings - Fork 224
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
pdlan
wants to merge
1
commit into
tytso:master
Choose a base branch
from
pdlan:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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".