blob: 8a6e6dcefb749b41ed5ecddb7035ae6a8c247adf [file] [log] [blame]
Jens Axboe38455912008-08-04 15:35:26 +02001#include <inttypes.h>
Jens Axboe5d7c5d32010-06-21 15:08:17 +02002#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 Carroll419484b2008-09-12 10:57:13 +02008#include "crc32c.h"
Jens Axboe38455912008-08-04 15:35:26 +02009
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 Axboee3aaafc2012-02-22 20:28:17 +010021int crc32c_intel_available = 0;
22
Jens Axboe2f681242010-10-21 08:15:59 +020023#ifdef ARCH_HAVE_SSE4_2
Aaron Carroll419484b2008-09-12 10:57:13 +020024
Jens Axboe38455912008-08-04 15:35:26 +020025#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 Axboee3aaafc2012-02-22 20:28:17 +010033static int crc32c_probed;
34
Jens Axboecc62ea72012-02-09 21:17:06 +010035static uint32_t crc32c_intel_le_hw_byte(uint32_t crc, unsigned char const *data,
36 unsigned long length)
Jens Axboe38455912008-08-04 15:35:26 +020037{
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 */
54uint32_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 Carroll419484b2008-09-12 10:57:13 +020080
Jens Axboee0ab5f92010-06-29 10:07:13 +020081static void do_cpuid(unsigned int *eax, unsigned int *ebx, unsigned int *ecx,
82 unsigned int *edx)
Jens Axboe5d7c5d32010-06-21 15:08:17 +020083{
Jens Axboee0ab5f92010-06-29 10:07:13 +020084 int id = *eax;
Aaron Carroll419484b2008-09-12 10:57:13 +020085
Jens Axboee0ab5f92010-06-29 10:07:13 +020086 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 Axboe5d7c5d32010-06-21 15:08:17 +020095}
96
Jens Axboee3aaafc2012-02-22 20:28:17 +010097void crc32c_intel_probe(void)
Jens Axboe5d7c5d32010-06-21 15:08:17 +020098{
Jens Axboee3aaafc2012-02-22 20:28:17 +010099 if (!crc32c_probed) {
100 unsigned int eax, ebx, ecx, edx;
Jens Axboe5d7c5d32010-06-21 15:08:17 +0200101
Jens Axboee3aaafc2012-02-22 20:28:17 +0100102 eax = 1;
Jens Axboee0ab5f92010-06-29 10:07:13 +0200103
Jens Axboee3aaafc2012-02-22 20:28:17 +0100104 do_cpuid(&eax, &ebx, &ecx, &edx);
105 crc32c_intel_available = (ecx & (1 << 20)) != 0;
106 crc32c_probed = 1;
107 }
Jens Axboe5d7c5d32010-06-21 15:08:17 +0200108}
109
110#endif /* ARCH_HAVE_SSE */