Jens Axboe | 3845591 | 2008-08-04 15:35:26 +0200 | [diff] [blame] | 1 | #include <inttypes.h> |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <signal.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/wait.h> |
Aaron Carroll | 419484b | 2008-09-12 10:57:13 +0200 | [diff] [blame] | 8 | #include "crc32c.h" |
Jens Axboe | 3845591 | 2008-08-04 15:35:26 +0200 | [diff] [blame] | 9 | |
| 10 | /* |
| 11 | * Based on a posting to lkml by Austin Zhang <austin.zhang@intel.com> |
| 12 | * |
| 13 | * Using hardware provided CRC32 instruction to accelerate the CRC32 disposal. |
| 14 | * CRC32C polynomial:0x1EDC6F41(BE)/0x82F63B78(LE) |
| 15 | * CRC32 is a new instruction in Intel SSE4.2, the reference can be found at: |
| 16 | * http://www.intel.com/products/processor/manuals/ |
| 17 | * Intel(R) 64 and IA-32 Architectures Software Developer's Manual |
| 18 | * Volume 2A: Instruction Set Reference, A-M |
| 19 | */ |
| 20 | |
Jens Axboe | 2f68124 | 2010-10-21 08:15:59 +0200 | [diff] [blame] | 21 | #ifdef ARCH_HAVE_SSE4_2 |
Aaron Carroll | 419484b | 2008-09-12 10:57:13 +0200 | [diff] [blame] | 22 | |
Jens Axboe | 3845591 | 2008-08-04 15:35:26 +0200 | [diff] [blame] | 23 | #if BITS_PER_LONG == 64 |
| 24 | #define REX_PRE "0x48, " |
| 25 | #define SCALE_F 8 |
| 26 | #else |
| 27 | #define REX_PRE |
| 28 | #define SCALE_F 4 |
| 29 | #endif |
| 30 | |
Jens Axboe | cc62ea7 | 2012-02-09 21:17:06 +0100 | [diff] [blame] | 31 | static uint32_t crc32c_intel_le_hw_byte(uint32_t crc, unsigned char const *data, |
| 32 | unsigned long length) |
Jens Axboe | 3845591 | 2008-08-04 15:35:26 +0200 | [diff] [blame] | 33 | { |
| 34 | while (length--) { |
| 35 | __asm__ __volatile__( |
| 36 | ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1" |
| 37 | :"=S"(crc) |
| 38 | :"0"(crc), "c"(*data) |
| 39 | ); |
| 40 | data++; |
| 41 | } |
| 42 | |
| 43 | return crc; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Steps through buffer one byte at at time, calculates reflected |
| 48 | * crc using table. |
| 49 | */ |
| 50 | uint32_t crc32c_intel(unsigned char const *data, unsigned long length) |
| 51 | { |
| 52 | unsigned int iquotient = length / SCALE_F; |
| 53 | unsigned int iremainder = length % SCALE_F; |
| 54 | #if BITS_PER_LONG == 64 |
| 55 | uint64_t *ptmp = (uint64_t *) data; |
| 56 | #else |
| 57 | uint32_t *ptmp = (uint32_t *) data; |
| 58 | #endif |
| 59 | uint32_t crc = ~0; |
| 60 | |
| 61 | while (iquotient--) { |
| 62 | __asm__ __volatile__( |
| 63 | ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;" |
| 64 | :"=S"(crc) |
| 65 | :"0"(crc), "c"(*ptmp) |
| 66 | ); |
| 67 | ptmp++; |
| 68 | } |
| 69 | |
| 70 | if (iremainder) |
| 71 | crc = crc32c_intel_le_hw_byte(crc, (unsigned char *)ptmp, |
| 72 | iremainder); |
| 73 | |
| 74 | return crc; |
| 75 | } |
Aaron Carroll | 419484b | 2008-09-12 10:57:13 +0200 | [diff] [blame] | 76 | |
Jens Axboe | e0ab5f9 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 77 | static void do_cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx, |
| 78 | unsigned int *edx) |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 79 | { |
Jens Axboe | e0ab5f9 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 80 | int id = *eax; |
Aaron Carroll | 419484b | 2008-09-12 10:57:13 +0200 | [diff] [blame] | 81 | |
Jens Axboe | e0ab5f9 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 82 | asm("movl %4, %%eax;" |
| 83 | "cpuid;" |
| 84 | "movl %%eax, %0;" |
| 85 | "movl %%ebx, %1;" |
| 86 | "movl %%ecx, %2;" |
| 87 | "movl %%edx, %3;" |
| 88 | : "=r" (*eax), "=r" (*ebx), "=r" (*ecx), "=r" (*edx) |
| 89 | : "r" (id) |
| 90 | : "eax", "ebx", "ecx", "edx"); |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | int crc32c_intel_works(void) |
| 94 | { |
Jens Axboe | e0ab5f9 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 95 | unsigned int eax, ebx, ecx, edx; |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 96 | |
Jens Axboe | e0ab5f9 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 97 | eax = 1; |
| 98 | |
| 99 | do_cpuid(&eax, &ebx, &ecx, &edx); |
| 100 | return (ecx & (1 << 20)) != 0; |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | #endif /* ARCH_HAVE_SSE */ |