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 | e3aaafc | 2012-02-22 20:28:17 +0100 | [diff] [blame^] | 21 | int crc32c_intel_available = 0; |
| 22 | |
Jens Axboe | 2f68124 | 2010-10-21 08:15:59 +0200 | [diff] [blame] | 23 | #ifdef ARCH_HAVE_SSE4_2 |
Aaron Carroll | 419484b | 2008-09-12 10:57:13 +0200 | [diff] [blame] | 24 | |
Jens Axboe | 3845591 | 2008-08-04 15:35:26 +0200 | [diff] [blame] | 25 | #if BITS_PER_LONG == 64 |
| 26 | #define REX_PRE "0x48, " |
| 27 | #define SCALE_F 8 |
| 28 | #else |
| 29 | #define REX_PRE |
| 30 | #define SCALE_F 4 |
| 31 | #endif |
| 32 | |
Jens Axboe | e3aaafc | 2012-02-22 20:28:17 +0100 | [diff] [blame^] | 33 | static int crc32c_probed; |
| 34 | |
Jens Axboe | cc62ea7 | 2012-02-09 21:17:06 +0100 | [diff] [blame] | 35 | static uint32_t crc32c_intel_le_hw_byte(uint32_t crc, unsigned char const *data, |
| 36 | unsigned long length) |
Jens Axboe | 3845591 | 2008-08-04 15:35:26 +0200 | [diff] [blame] | 37 | { |
| 38 | while (length--) { |
| 39 | __asm__ __volatile__( |
| 40 | ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1" |
| 41 | :"=S"(crc) |
| 42 | :"0"(crc), "c"(*data) |
| 43 | ); |
| 44 | data++; |
| 45 | } |
| 46 | |
| 47 | return crc; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Steps through buffer one byte at at time, calculates reflected |
| 52 | * crc using table. |
| 53 | */ |
| 54 | uint32_t crc32c_intel(unsigned char const *data, unsigned long length) |
| 55 | { |
| 56 | unsigned int iquotient = length / SCALE_F; |
| 57 | unsigned int iremainder = length % SCALE_F; |
| 58 | #if BITS_PER_LONG == 64 |
| 59 | uint64_t *ptmp = (uint64_t *) data; |
| 60 | #else |
| 61 | uint32_t *ptmp = (uint32_t *) data; |
| 62 | #endif |
| 63 | uint32_t crc = ~0; |
| 64 | |
| 65 | while (iquotient--) { |
| 66 | __asm__ __volatile__( |
| 67 | ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;" |
| 68 | :"=S"(crc) |
| 69 | :"0"(crc), "c"(*ptmp) |
| 70 | ); |
| 71 | ptmp++; |
| 72 | } |
| 73 | |
| 74 | if (iremainder) |
| 75 | crc = crc32c_intel_le_hw_byte(crc, (unsigned char *)ptmp, |
| 76 | iremainder); |
| 77 | |
| 78 | return crc; |
| 79 | } |
Aaron Carroll | 419484b | 2008-09-12 10:57:13 +0200 | [diff] [blame] | 80 | |
Jens Axboe | e0ab5f9 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 81 | static void do_cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx, |
| 82 | unsigned int *edx) |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 83 | { |
Jens Axboe | e0ab5f9 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 84 | int id = *eax; |
Aaron Carroll | 419484b | 2008-09-12 10:57:13 +0200 | [diff] [blame] | 85 | |
Jens Axboe | e0ab5f9 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 86 | asm("movl %4, %%eax;" |
| 87 | "cpuid;" |
| 88 | "movl %%eax, %0;" |
| 89 | "movl %%ebx, %1;" |
| 90 | "movl %%ecx, %2;" |
| 91 | "movl %%edx, %3;" |
| 92 | : "=r" (*eax), "=r" (*ebx), "=r" (*ecx), "=r" (*edx) |
| 93 | : "r" (id) |
| 94 | : "eax", "ebx", "ecx", "edx"); |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Jens Axboe | e3aaafc | 2012-02-22 20:28:17 +0100 | [diff] [blame^] | 97 | void crc32c_intel_probe(void) |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 98 | { |
Jens Axboe | e3aaafc | 2012-02-22 20:28:17 +0100 | [diff] [blame^] | 99 | if (!crc32c_probed) { |
| 100 | unsigned int eax, ebx, ecx, edx; |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 101 | |
Jens Axboe | e3aaafc | 2012-02-22 20:28:17 +0100 | [diff] [blame^] | 102 | eax = 1; |
Jens Axboe | e0ab5f9 | 2010-06-29 10:07:13 +0200 | [diff] [blame] | 103 | |
Jens Axboe | e3aaafc | 2012-02-22 20:28:17 +0100 | [diff] [blame^] | 104 | do_cpuid(&eax, &ebx, &ecx, &edx); |
| 105 | crc32c_intel_available = (ecx & (1 << 20)) != 0; |
| 106 | crc32c_probed = 1; |
| 107 | } |
Jens Axboe | 5d7c5d3 | 2010-06-21 15:08:17 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | #endif /* ARCH_HAVE_SSE */ |