add slice by 8 algorithm to crc32.c
PATCH 1/1 add slice by 8 algorithm to crc32.c
Sorry I didn't see this when first posted.
The "slice by 8" terminology is pretty confusing. How about "Extended Joakim Tjernlund's optimization from commit 836e2af92503f1642dbc3c3281ec68ec1dd39d2e to 8-way parallelism."
Which is essentally what you're doing. The renaming of tab[0] to t0_le and t0_be, and removal of the DO_CRC4 macro just increases the diff size.
If you're looking at speeding up the CRC through larger tables, have you tried using 10+11+11-bit tables? That would require 20K of tables rather than 8K, but would reduce the number of table lookups per byte.
One more stunt you could try to increase parallelism: rather than maintain the CRC in one register, maintain it in several, and only XOR and collapse them at the end.
Start with your 64-bit code, but imagine that the second code block's "q = *p32++" always loads 0, and therefore the whole block can be skipped. (Since tab[0] = 0 for all CRC tables.)
This computes the CRC of the even words. Then do a second one in parallel for the odd words into a separate CRC register. Then combine them at the end. (Shift one up by 32 bits and XOR into the other.)
This would let you get away with 5K of tables: t4 through t7, and t0. t1 through t3 could be skipped.
Ideally, I'd write all this code myself, but I'm a bit crunched at work right now so wouldn't be able to get to it for a few days.
Another possible simplification to the startup code. There's no need to compute init_bytes explicitly; just loop until the pointer is aligned:
while ((unsigned)buf & 3) {
if (!len--)
goto done;
#ifdef __LITTLE_ENDIAN
i0 = *buf++ ^ crc;
crc = t0_le[i0] ^ (crc >> 8);
#else
i0 = *buf++ ^ (crc >> 24);
crc = t0_le[i0] ^ (crc << 8);
#endif
}
p32 = (u32 const *)buf;
words = len >> 2;
end_bytes = len & 3;... although I'd prefer to keep the DO_CRC() and DO_CRC4 macros, and extend them to the 64-bit case, to avoid the nested #ifdefs. That would make:
while ((unsigned)buf & 3) {
if (!len--)
goto done;
DO_CRC(*buf++);
}
p32 = (u32 const *)buf;
words = len >> 2;
end_bytes = len & 3;Hi George, just a quick comment or two.
"George Spelvin" <[email protected]> wrote on 2011/08/08 11:28:26:
Quoted 9 lines
Sorry I didn't see this when first posted.
The "slice by 8" terminology is pretty confusing. How about "Extended Joakim Tjernlund's optimization from commit
836e2af92503f1642dbc3c3281ec68ec1dd39d2eto 8-way parallelism."Which is essentally what you're doing. The renaming of tab[0] to
t0_leandt0_be, and removal of theDO_CRC4macro just increases the diff size.
Yes, and also increases ppc size. The matrix needs to stay. I have proposed to add this intead:
@@ -51,20 +51,21 @@ static inline u32
crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256]) {
# ifdef __LITTLE_ENDIAN -# define DO_CRC(x) crc = tab[0][(crc ^ (x)) & 255] ^ (crc >> 8) -# define DO_CRC4 crc = tab[3][(crc) & 255] ^ \ - tab[2][(crc >> 8) & 255] ^ \ - tab[1][(crc >> 16) & 255] ^ \ - tab[0][(crc >> 24) & 255] +# define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8) +# define DO_CRC4 crc = t3[(crc) & 255] ^ \ + t2[(crc >> 8) & 255] ^ \ + t1[(crc >> 16) & 255] ^ \ + t0[(crc >> 24) & 255] # else -# define DO_CRC(x) crc = tab[0][((crc >> 24) ^ (x)) & 255] ^ (crc << 8) -# define DO_CRC4 crc = tab[0][(crc) & 255] ^ \ - tab[1][(crc >> 8) & 255] ^ \ - tab[2][(crc >> 16) & 255] ^ \ - tab[3][(crc >> 24) & 255] +# define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8) +# define DO_CRC4 crc = t0[(crc) & 255] ^ \ + t1[(crc >> 8) & 255] ^ \ + t2[(crc >> 16) & 255] ^ \ + t3[(crc >> 24) & 255] # endif const u32 *b; size_t rem_len; + const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
This saves 3 insns for ppc in the hot loop, hopefully x86 is happy with this too.
Quoted 50 lines
If you're looking at speeding up the CRC through larger tables, have you tried using 10+11+11-bit tables? That would require 20K of tables rather than 8K, but would reduce the number of table lookups per byte.
One more stunt you could try to increase parallelism: rather than maintain the CRC in one register, maintain it in several, and only XOR and collapse them at the end.
Start with your 64-bit code, but imagine that the second code block's "q = *p32++" always loads 0, and therefore the whole block can be skipped. (Since tab[0] = 0 for all CRC tables.)
This computes the CRC of the even words. Then do a second one in parallel for the odd words into a separate CRC register. Then combine them at the end. (Shift one up by 32 bits and XOR into the other.)
This would let you get away with 5K of tables: t4 through t7, and t0. t1 through t3 could be skipped.
Ideally, I'd write all this code myself, but I'm a bit crunched at work right now so wouldn't be able to get to it for a few days.
Another possible simplification to the startup code. There's no need to compute
init_bytesexplicitly; just loop until the pointer is aligned:while ((unsigned)buf & 3) { if (!len--) goto done; #ifdef __LITTLE_ENDIAN i0 = *buf++ ^ crc; crc = t0_le[i0] ^ (crc >> 8); #else i0 = *buf++ ^ (crc >> 24); crc = t0_le[i0] ^ (crc << 8); #endif } p32 = (u32 const *)buf; words = len >> 2; end_bytes = len & 3;... although I'd prefer to keep the
DO_CRC()andDO_CRC4macros, and extend them to the 64-bit case, to avoid the nested #ifdefs. That would make:
Quoted 8 lines
while ((unsigned)buf & 3) {
if (!len--)
goto done;
DO_CRC(*buf++);
}
p32 = (u32 const *)buf;
words = len >> 2;
end_bytes = len & 3;I prefer to keep the current code which (at the time) generated good code for at least ppc:
/* Align it */
if (unlikely((long)buf & 3 && len)) {
do {
DO_CRC(*buf++);
} while ((--len) && ((long)buf)&3);
}Quoted 8 lines
I prefer to keep the current code which (at the time) generated good code for at least ppc:
/* Align it */ if (unlikely((long)buf & 3 && len)) { do { DO_CRC(*buf++); } while ((--len) && ((long)buf)&3); }
Ah, I was looking at fzago's initial patch; I hadn't realized you'd tweaked it. That's pretty much what I was talking about.
Would
if (unlikely((long)buf & 3) && len) {
give the compiler better hints? len != 0 is awfully likely, actually.
"George Spelvin" <[email protected]> wrote on 2011/08/08 12:52:01:
Quoted 10 lines
Quoted 9 lines
I prefer to keep the current code which (at the time) generated good code for at least ppc:
/* Align it */ if (unlikely((long)buf & 3 && len)) { do { DO_CRC(*buf++); } while ((--len) && ((long)buf)&3); }
Quoted 8 lines
Ah, I was looking at fzago's initial patch; I hadn't realized you'd tweaked it. That's pretty much what I was talking about.
Would
if (unlikely((long)buf & 3) && len) {give the compiler better hints? len != 0 is awfully likely, actually.
Doesn't matter on ppc(gcc 4.4.4). The whole while loop is moved out of line in both cases and the generated asm is identical.
Joakim Tjernlund/Transmode wrote on 2011/08/08 13:11:14:
Quoted 15 lines
"George Spelvin" <[email protected]> wrote on 2011/08/08 12:52:01:
Quoted 13 lines
Quoted 9 lines
I prefer to keep the current code which (at the time) generated good code for at least ppc:
/* Align it */ if (unlikely((long)buf & 3 && len)) { do { DO_CRC(*buf++); } while ((--len) && ((long)buf)&3); }Ah, I was looking at fzago's initial patch; I hadn't realized you'd tweaked it. That's pretty much what I was talking about.
Quoted 8 lines
Quoted 5 lines
Would
if (unlikely((long)buf & 3) && len) {give the compiler better hints? len != 0 is awfully likely, actually.
Doesn't matter on ppc(gcc 4.4.4). The whole while loop is moved out of line in both cases and the generated asm is identical.
Actually, it does make a difference(must remember to save file before gcc :) Hard to tell if its any better though. Strangle this impacted the inner loop as well, it gained an extra addi.
Jocke
"George Spelvin" <[email protected]> wrote on 2011/08/08 12:52:01:
Quoted 10 lines
Quoted 9 lines
I prefer to keep the current code which (at the time) generated good code for at least ppc:
/* Align it */ if (unlikely((long)buf & 3 && len)) { do { DO_CRC(*buf++); } while ((--len) && ((long)buf)&3); }
Quoted 8 lines
Ah, I was looking at fzago's initial patch; I hadn't realized you'd tweaked it. That's pretty much what I was talking about.
Would
if (unlikely((long)buf & 3) && len) {give the compiler better hints? len != 0 is awfully likely, actually.
This is my current hack. Not ideal yet, just to give you a hint:
lib/crc32.c +44 -27
@@ -28,13 +28,13 @@#include <linux/init.h> #include <asm/atomic.h> #include "crc32defs.h" -#if CRC_LE_BITS ==8 +#if CRC_LE_BITS ==32 ||CRC_LE_BITS ==64 # define tole (x) __constant_cpu_to_le32(x)#else # define tole (x) (x)#endif -#if CRC_BE_BITS ==8 +#if CRC_BE_BITS ==32 ||CRC_BE_BITS ==64 # define tobe (x) __constant_cpu_to_be32(x)#else # define tobe (x) (x)
@@ -45,27 +45,32 @@ MODULE_AUTHOR("Matt Domsch <[email protected]>");MODULE_DESCRIPTION ("Ethernet CRC32 calculations" );MODULE_LICENSE ("GPL" ); -#if CRC_LE_BITS ==8 ||CRC_BE_BITS ==8 +#if CRC_LE_BITS ==32 ||CRC_BE_BITS ==64 static inline u32 crc32_body (u32 crc,unsigned char const *buf,size_t len,const u32 (*tab)[256 ]) {# ifdef __LITTLE_ENDIAN# define DO_CRC (x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8) -# define DO_CRC4 crc = t3[(crc) & 255] ^ \ - t2[(crc >> 8) & 255] ^ \ - t1[(crc >> 16) & 255] ^ \ - t0[(crc >> 24) & 255] +# define DO_CRC4 (crc, x0, x1, x2, x3) \ + x3[(crc) & 255] ^ \ + x2[(crc >> 8) & 255] ^ \ + x1[(crc >> 16) & 255] ^ \ + x0[(crc >> 24) & 255]# else # define DO_CRC (x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8) -# define DO_CRC4 crc = t0[(crc) & 255] ^ \ - t1[(crc >> 8) & 255] ^ \ - t2[(crc >> 16) & 255] ^ \ - t3[(crc >> 24) & 255] +# define DO_CRC4 (crc, x0, x1, x2, x3) \ + x0[(crc) & 255] ^ \ + x1[(crc >> 8) & 255] ^ \ + x2[(crc >> 16) & 255] ^ \ + x3[(crc >> 24) & 255]# endif const u32 *b;size_t rem_len;const u32 *t0=tab[0 ], *t1=tab[1 ], *t2=tab[2 ], *t3=tab[3 ]; +#if CRC_LE_BITS ==64 ||CRC_BE_BITS ==64 +const u32 *t4=tab[4 ], *t5=tab[5 ], *t6=tab[6 ], *t7=tab[7 ]; +#endif /* Align it */ if (unlikely ((long )buf &3 && len)) {
@@ -73,13 +78,25 @@ crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])DO_CRC (*buf++); }while ((--len) && ((long )buf)&3 ); } +#if CRC_LE_BITS ==64 ||CRC_BE_BITS ==64 + rem_len = len &7 ; + len = len >>3 ; +#else rem_len = len &3 ; -/* load data 32 bits wide, xor data 32 bits wide. */ len = len >>2 ; +#endif +/* load data 32 bits wide, xor data 32 bits wide. */ b = (const u32 *)buf;for (--b; len; --len) { crc ^= *++b;/* use pre increment for speed */ -DO_CRC4 ; +#if CRC_LE_BITS ==64 ||CRC_BE_BITS ==64 + crc =DO_CRC4 (crc, t4, t5, t6, t7); + ++b; + crc ^=DO_CRC4 (*b, t0, t1, t2, t3); +#else + crc =DO_CRC4 (crc, t0, t1, t2, t3); +#endif + } len = rem_len;/* And the last few bytes */
@@ -123,7 +140,7 @@ u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)u32 __pure crc32_le (u32 crc,unsigned char const *p,size_t len) { -# if CRC_LE_BITS ==8 +# if CRC_LE_BITS ==32 ||CRC_LE_BITS ==64 const u32 (*tab)[] = crc32table_le; crc =__cpu_to_le32 (crc);
@@ -132,17 +149,17 @@ u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)# elif CRC_LE_BITS ==4 while (len--) { crc ^= *p++; - crc = (crc >>4 ) ^ crc32table_le[crc &15 ]; - crc = (crc >>4 ) ^ crc32table_le[crc &15 ]; + crc = (crc >>4 ) ^ crc32table_le[0 ][crc &15 ]; + crc = (crc >>4 ) ^ crc32table_le[0 ][crc &15 ]; }return crc;# elif CRC_LE_BITS ==2 while (len--) { crc ^= *p++; - crc = (crc >>2 ) ^ crc32table_le[crc &3 ]; - crc = (crc >>2 ) ^ crc32table_le[crc &3 ]; - crc = (crc >>2 ) ^ crc32table_le[crc &3 ]; - crc = (crc >>2 ) ^ crc32table_le[crc &3 ]; + crc = (crc >>2 ) ^ crc32table_le[0 ][crc &3 ]; + crc = (crc >>2 ) ^ crc32table_le[0 ][crc &3 ]; + crc = (crc >>2 ) ^ crc32table_le[0 ][crc &3 ]; + crc = (crc >>2 ) ^ crc32table_le[0 ][crc &3 ]; }return crc;# endif
@@ -180,7 +197,7 @@ u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)#else /* Table-based approach */ u32 __pure crc32_be (u32 crc,unsigned char const *p,size_t len) { -# if CRC_BE_BITS ==8 +# if CRC_BE_BITS ==32 ||CRC_BE_BITS ==64 const u32 (*tab)[] = crc32table_be; crc =__cpu_to_be32 (crc);
@@ -189,17 +206,17 @@ u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)# elif CRC_BE_BITS ==4 while (len--) { crc ^= *p++ <<24 ; - crc = (crc <<4 ) ^ crc32table_be[crc >>28 ]; - crc = (crc <<4 ) ^ crc32table_be[crc >>28 ]; + crc = (crc <<4 ) ^ crc32table_be[0 ][crc >>28 ]; + crc = (crc <<4 ) ^ crc32table_be[0 ][crc >>28 ]; }return crc;# elif CRC_BE_BITS ==2 while (len--) { crc ^= *p++ <<24 ; - crc = (crc <<2 ) ^ crc32table_be[crc >>30 ]; - crc = (crc <<2 ) ^ crc32table_be[crc >>30 ]; - crc = (crc <<2 ) ^ crc32table_be[crc >>30 ]; - crc = (crc <<2 ) ^ crc32table_be[crc >>30 ]; + crc = (crc <<2 ) ^ crc32table_be[0 ][crc >>30 ]; + crc = (crc <<2 ) ^ crc32table_be[0 ][crc >>30 ]; + crc = (crc <<2 ) ^ crc32table_be[0 ][crc >>30 ]; + crc = (crc <<2 ) ^ crc32table_be[0 ][crc >>30 ]; }return crc;# endif
lib/crc32defs.h +6 -6
@@ -8,25 +8,25 @@/* How many bits at a time to use. Requires a table of 4<<CRC_xx_BITS bytes. */ /* For less performance-sensitive, use 4 */ -#ifndef CRC_LE_BITS -# define CRC_LE_BITS 8 +#ifndef CRC_LE_BITS +# define CRC_LE_BITS 32#endif #ifndef CRC_BE_BITS -# define CRC_BE_BITS 8 +# define CRC_BE_BITS 32#endif /* * Little-endian CRC computation. Used with serial bit streams sent * lsbit-first. Be sure to use cpu_to_le32() to append the computed CRC. */ -#if CRC_LE_BITS >8 ||CRC_LE_BITS <1 ||CRC_LE_BITS &CRC_LE_BITS -1 -# error CRC_LE_BITS must be a power of 2 between 1 and 8 +#if CRC_LE_BITS >64 ||CRC_LE_BITS <1 ||CRC_LE_BITS &CRC_LE_BITS -1 +# error CRC_LE_BITS must be a power of 2 between 1 and 64#endif /* * Big-endian CRC computation. Used with serial bit streams sent * msbit-first. Be sure to use cpu_to_be32() to append the computed CRC. */ -#if CRC_BE_BITS >8 ||CRC_BE_BITS <1 ||CRC_BE_BITS &CRC_BE_BITS -1 +#if CRC_BE_BITS >64 ||CRC_BE_BITS <1 ||CRC_BE_BITS &CRC_BE_BITS -1 # error CRC_BE_BITS must be a power of 2 between 1 and 8#endif
lib/gen_crc32table.c +37 -22
105 lines not on this page · raw message
Quoted 10 lines
lib/gen_crc32table.c +8
-2-#define LE_TABLE_SIZE (1 << CRC_LE_BITS) -#define BE_TABLE_SIZE (1 << CRC_BE_BITS) +#if CRC_LE_BITS >8 +# define LE_TABLE_SIZE 256 +#else +# define LE_TABLE_SIZE (1 << CRC_LE_BITS) +#endif +#if CRC_BE_BITS >8 +# define BE_TABLE_SIZE 256 +#else
lib/gen_crc32table.c line 22
Quoted 10 lines
lib/gen_crc32table.c +7
-2+# define BE_TABLE_SIZE (1 << CRC_BE_BITS) +#endif -static uint32_t crc32table_le[4 ][LE_TABLE_SIZE ]; -static uint32_t crc32table_be[4 ][BE_TABLE_SIZE ]; +#define LE_TABLE_ROWS ((CRC_LE_BITS - 1)/8 + 1) +#define BE_TABLE_ROWS ((CRC_BE_BITS - 1)/8 + 1) + +static uint32_t crc32table_le[LE_TABLE_ROWS ][LE_TABLE_SIZE ]; +static uint32_t crc32table_be[BE_TABLE_ROWS ][BE_TABLE_SIZE ];
Minor cleanup suggestion: The two different ways of computing xE_TABLE_SIZE and xE_TABLE_ROWS is a bit confusing.
May I recommend choosing one of the following:
#if CRC_LE_BITS > 8
# define LE_TABLE_ROWS (CRC_LE_BITS/8) # define LE_TABLE_SIZE 256 #else # define LE_TABLE_ROWS 1 # define LE_TABLE_SIZE (1 << CRC_LE_BITS) #endif
or
#define LE_TABLE_ROWS ((CRC_LE_BITS - 1)/8 + 1) #define LE_TABLE_SIZE (1 << ((CRC_LE_BITS - 1)%8 + 1))
Either one makes the relationship between the two clearer.
Quoted 10 lines
-----Original Message-----
From: George Spelvin [mailto:[email protected]]
Sent: Monday, August 08, 2011 4:28 AM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH] add slice by 8 algorithm tocrc32.c
Quoted 5 lines
Sorry I didn't see this when first posted.
The "slice by 8" terminology is pretty confusing. How about "Extended Joakim Tjernlund's optimization from commit
836e2af92503f1642dbc3c3281ec68ec1dd39d2eto 8-way parallelism."
Here is a link to the article I first read about this algorithm. It mentions both the 4 and 8 byte version.
I do not know about priority between Joakim and the folks at Intel but Intel is usually credited with the idea in other articles I have seen. Clearly the algorithm that is currently in crc32.c is the same as the one described in the article. As you can see I mis-copied the name from slicing-by-8 to slice by 8.
http://www.intel.com/technology/comms/perfnet/download/CRC_generators.pdf
Quoted 54 lines
Which is essentally what you're doing. The renaming of tab[0] to
t0_leandt0_be, and removal of theDO_CRC4macro just increases the diff size.If you're looking at speeding up the CRC through larger tables, have you tried using 10+11+11-bit tables? That would require 20K of tables rather than 8K, but would reduce the number of table lookups per byte.
One more stunt you could try to increase parallelism: rather than maintain the CRC in one register, maintain it in several, and only XOR and collapse them at the end.
Start with your 64-bit code, but imagine that the second code block's "q = *p32++" always loads 0, and therefore the whole block can be skipped. (Since tab[0] = 0 for all CRC tables.)
This computes the CRC of the even words. Then do a second one in parallel for the odd words into a separate CRC register. Then combine them at the end.
(Shift one up by 32 bits and XOR into the other.)This would let you get away with 5K of tables: t4 through t7, and t0. t1 through t3 could be skipped.
Ideally, I'd write all this code myself, but I'm a bit crunched at work right now so wouldn't be able to get to it for a few days.
Another possible simplification to the startup code. There's no need to compute
init_bytesexplicitly; just loop until the pointer is aligned:while ((unsigned)buf & 3) { if (!len--) goto done; #ifdef __LITTLE_ENDIAN i0 = *buf++ ^ crc; crc = t0_le[i0] ^ (crc >> 8); #else i0 = *buf++ ^ (crc >> 24); crc = t0_le[i0] ^ (crc << 8); #endif } p32 = (u32 const *)buf; words = len >> 2; end_bytes = len & 3;... although I'd prefer to keep the
DO_CRC()andDO_CRC4macros, and extend them to the 64-bit case, to avoid the nested #ifdefs. That would make:
Quoted 8 lines
while ((unsigned)buf & 3) {
if (!len--)
goto done;
DO_CRC(*buf++);
}
p32 = (u32 const *)buf;
words = len >> 2;
end_bytes = len & 3;Personally I don't like macros unless they are very frequently used as you can probably tell. The ifdefs were somewhat rediced in the second version of the patch.
Quoted 11 lines
-----Original Message-----
From: George Spelvin [mailto:[email protected]]
Sent: Monday, August 08, 2011 5:52 AM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] add slice by 8 algorithm tocrc32.cQuoted 1 line
I prefer to keep the current code which (at the time) generated good
code
Quoted 10 lines
Quoted 7 lines
for at least ppc:
/* Align it */ if (unlikely((long)buf & 3 && len)) { do { DO_CRC(*buf++); } while ((--len) && ((long)buf)&3); }Ah, I was looking at fzago's initial patch; I hadn't realized you'd tweaked it. That's pretty much what I was talking about.
Frank actually deserves none of the 'blame' for this effort. He is much more experienced in sending in patches to linux-kernel than I am and helped me to prepare and send the first version of the patch.
Quoted 6 lines
Would
if (unlikely((long)buf & 3)&& len) {give the compiler better hints? len != 0 is awfully likely, actually.
Happy to consider this. I have been asking the list for comments about the idea of dropping the BITS=2 and BITS=4 algorithms altogether which would make the table size just 256. So far no one has claimed that they actually care about those algorithms except as 'examples'. The 'Sarwate' algorithm (which I added as an 8 bit version) is faster and only adds 2x4KB of table.
If no one really uses the smaller but much slower versions I don't see a reason to keep them.
Quoted 48 lines
-----Original Message-----
From: George Spelvin [mailto:[email protected]]
Sent: Monday, August 08, 2011 7:55 AM
To: [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] add slice by 8 algorithm tocrc32.cQuoted 10 lines
lib/gen_crc32table.c +8
-2-#define LE_TABLE_SIZE (1 << CRC_LE_BITS) -#define BE_TABLE_SIZE (1 << CRC_BE_BITS) +#if CRC_LE_BITS >8 +# define LE_TABLE_SIZE 256 +#else +# define LE_TABLE_SIZE (1 << CRC_LE_BITS) +#endif +#if CRC_BE_BITS >8 +# define BE_TABLE_SIZE 256 +#else lib/gen_crc32table.c line 22
Quoted 10 lines
lib/gen_crc32table.c +7
-2+# define BE_TABLE_SIZE (1 << CRC_BE_BITS) +#endif -static uint32_t crc32table_le[4 ][LE_TABLE_SIZE ]; -static uint32_t crc32table_be[4 ][BE_TABLE_SIZE ]; +#define LE_TABLE_ROWS ((CRC_LE_BITS - 1)/8 + 1) +#define BE_TABLE_ROWS ((CRC_BE_BITS - 1)/8 + 1) + +static uint32_t crc32table_le[LE_TABLE_ROWS ][LE_TABLE_SIZE ]; +static uint32_t crc32table_be[BE_TABLE_ROWS ][BE_TABLE_SIZE ];Minor cleanup suggestion: The two different ways of computing
xE_TABLE_SIZEandxE_TABLE_ROWSis a bit confusing.May I recommend choosing one of the following:
#if
CRC_LE_BITS> 8# define LE_TABLE_ROWS (CRC_LE_BITS/8) # define LE_TABLE_SIZE 256 #else # define LE_TABLE_ROWS 1 # define LE_TABLE_SIZE (1 << CRC_LE_BITS) #endifor
#define
LE_TABLE_ROWS((CRC_LE_BITS- 1)/8 + 1) #defineLE_TABLE_SIZE(1 << ((CRC_LE_BITS- 1)%8 + 1))Either one makes the relationship between the two clearer.
Same for x86. The code for the pre and post cleanup loops are the same there.
Quoted 23 lines
-----Original Message-----
From: Joakim Tjernlund [mailto:[email protected]]
Sent: Monday, August 08, 2011 6:11 AM
To: George Spelvin
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] add slice by 8 algorithm tocrc32.c"George Spelvin" <[email protected]> wrote on 2011/08/08 12:52:01:
Quoted 2 lines
Quoted 2 lines
I prefer to keep the current code which (at the time) generated good
code
Quoted 11 lines
Quoted 7 lines
for at least ppc:
/* Align it */ if (unlikely((long)buf & 3 && len)) { do { DO_CRC(*buf++); } while ((--len) && ((long)buf)&3); }Ah, I was looking at fzago's initial patch; I hadn't realized you'd tweaked it. That's pretty much what I was talking about.
Quoted 7 lines
Quoted 5 lines
Would
if (unlikely((long)buf & 3) && len) {give the compiler better hints? len != 0 is awfully likely, actually.
Doesn't matter on ppc(gcc 4.4.4). The whole while loop is moved out of
line
Quoted 1 line
in both cases and the generated asm is identical.
Quoted 5 lines
Happy to consider this. I have been asking the list for comments about the idea of dropping the BITS=2 and BITS=4 algorithms altogether which would make the table size just 256. So far no one has claimed that they actually care about those algorithms except as 'examples'. The 'Sarwate' algorithm (which I added as an 8 bit version) is faster and only adds 2x4KB of table.
Um, I thought the Sarwate agorithm was what was already there as BITS=8. (The original paper stores the CRC in two 8-bit variables and two 8-bit tables rather than using 16-bit words, but other than that, it's identical.)
And that requires just 1KB of table per endianness.
Anyway, I thought the complaint about laarge tables was not so much the memory as the L1 cache pollution.
Quoted 13 lines
-----Original Message-----
From: [email protected] [mailto:linux-kernel- [email protected]] On Behalf Of George Spelvin
Sent: Monday, August 08, 2011 3:45 PM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]
Subject: RE: [PATCH] add slice by 8 algorithm tocrc32.cQuoted 1 line
Happy to consider this. I have been asking the list for comments about
the
Quoted 2 lines
Quoted 2 lines
idea of dropping the BITS=2 and BITS=4 algorithms altogether which would make the table size just 256. So far no one has claimed that they
actually
Quoted 1 line
Quoted 1 line
care about those algorithms except as 'examples'. The 'Sarwate'
algorithm
Quoted 1 line
Quoted 1 line
(which I added as an 8 bit version) is faster and only adds 2x4KB of
table.
Quoted 5 lines
Um, I thought the Sarwate agorithm was what was already there as BITS=8. (The original paper stores the CRC in two 8-bit variables and two 8-bit tables rather than using 16-bit words, but other than that, it's identical.)
In the current i.e. original version of crc32.c BITS=8 is the 32 bit algorithm. The Sarwate version is not used. In the patched version that was moved to BITS=32 and another one slipped in as BITS=8 which is Sarwate.
Quoted 2 lines
And that requires just 1KB of table per endianness.
You're right! Its 2X1KB not 2X4KB. There are 2 tables (le and be).
Quoted 3 lines
Anyway, I thought the complaint about laarge tables was not so much the memory as the L1 cache pollution.
As far as performance goes cache pollution is the more important effect. That is why you can't just extend the 2, 4, 8 sequence which has the tables growing exponentially with BITS. The Slicing-by-N algorithm though has table size growing linearly with N. But, if you are trying to run Linux on a small embedded CPU even linear table size might be important. Back in the good old days you could strip Linux down to a very small kernel.
Quoted 5 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/