Skip to content

Commit

Permalink
Fixing cpuid function on some compilers/architectures.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahknight committed Feb 1, 2015
1 parent 6eeded0 commit 3831f5b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
27 changes: 21 additions & 6 deletions crc32c/crc32c.c
Expand Up @@ -18,6 +18,21 @@ static uint32_t crc32c_CPUDetection(uint32_t crc, const void* data, size_t lengt

CRC32CFunctionPtr crc32c = crc32c_CPUDetection;

// Use the compiler's cpuid, if it exists.
#if (defined __GNUC__) || (defined __clang__)
#include <cpuid.h>

static uint32_t cpuid(uint32_t functionInput) {
uint32_t eax;
uint32_t ebx;
uint32_t ecx;
uint32_t edx;
__cpuid(functionInput, eax, ebx, ecx, edx);
return ecx;
}

#else
// Basic implementation. Seems to only cover x86, not x64.
static uint32_t cpuid(uint32_t functionInput) {
uint32_t eax;
uint32_t ebx;
Expand All @@ -26,16 +41,16 @@ static uint32_t cpuid(uint32_t functionInput) {
#ifdef __PIC__
// PIC: Need to save and restore ebx See:
// http://sam.zoy.org/blog/2007-04-13-shlib-with-non-pic-code-have-inline-assembly-and-pic-mix-well
asm("pushl %%ebx\n\t" /* save %ebx */
"cpuid\n\t"
"movl %%ebx, %[ebx]\n\t" /* save what cpuid just put in %ebx */
"popl %%ebx" : "=a"(eax), [ebx] "=r"(ebx), "=c"(ecx), "=d"(edx) : "a" (functionInput)
: "cc");
__asm__("pushl %%ebx\n\t" /* save %ebx */
"cpuid\n\t"
"movl %%ebx, %[ebx]\n\t" /* save what cpuid just put in %ebx */
"popl %%ebx" : "=a"(eax), [ebx] "=r"(ebx), "=c"(ecx), "=d"(edx) : "a" (functionInput) : "cc");
#else
asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (functionInput));
__asm__("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (functionInput));
#endif
return ecx;
}
#endif

CRC32CFunctionPtr detectBestCRC32C() {
static const int SSE42_BIT = 20;
Expand Down
26 changes: 13 additions & 13 deletions crc32c/crc32c_tables.c
Expand Up @@ -14,20 +14,20 @@
#define CRCINIT 0xFFFFFFFF
void init() {
for (uint32_t i = 0; i <= 0xFF; i++) {
uint32_t x = i;
for (uint32_t j = 0; j < 8; j++)
x = (x>>1) ^ (CRCPOLY & (-(int32_t)(x & 1)));
g_crc_slicing[0][i] = x;
}
for (uint32_t i = 0; i <= 0xFF; i++) {
uint32_t x = i;
for (uint32_t j = 0; j < 8; j++)
x = (x>>1) ^ (CRCPOLY & (-(int32_t)(x & 1)));
g_crc_slicing[0][i] = x;
}
for (uint32_t i = 0; i <= 0xFF; i++) {
uint32_t c = g_crc_slicing[0][i];
for (uint32_t j = 1; j < 8; j++) {
c = g_crc_slicing[0][c & 0xFF] ^ (c >> 8);
g_crc_slicing[j][i] = c;
}
}
for (uint32_t i = 0; i <= 0xFF; i++) {
uint32_t c = g_crc_slicing[0][i];
for (uint32_t j = 1; j < 8; j++) {
c = g_crc_slicing[0][c & 0xFF] ^ (c >> 8);
g_crc_slicing[j][i] = c;
}
}
}
*/

Expand Down

0 comments on commit 3831f5b

Please sign in to comment.