博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ECC校验优化之路
阅读量:5091 次
发布时间:2019-06-13

本文共 25572 字,大约阅读时间需要 85 分钟。

    引子: 今天上嵌入式课程时,老师讲到Linux的文件系统,讲的重点是Linux中对于nand flash的ECC校验和纠错。上课很认真地听完,确实叹服代码作者的水平。

    晚上特地下载了Linux最新的内核,找到了作者自己写的那篇文章(路径为"linux-3.13.5\Documentation\mtd\nand_ecc.txt"),现摘录如下:

Introduction============Having looked at the linux mtd/nand driver and more specific at nand_ecc.cI felt there was room for optimisation. I bashed the code for a few hoursperforming tricks like table lookup removing superfluous code etc.After that the speed was increased by 35-40%.Still I was not too happy as I felt there was additional room for improvement.Bad! I was hooked.I decided to annotate my steps in this file. Perhaps it is useful to someoneor someone learns something from it.The problem===========NAND flash (at least SLC one) typically has sectors of 256 bytes.However NAND flash is not extremely reliable so some error detection(and sometimes correction) is needed.This is done by means of a Hamming code. I'll try to explain it inlaymans terms (and apologies to all the pro's in the field in case I donot use the right terminology, my coding theory class was almost 30years ago, and I must admit it was not one of my favourites).As I said before the ecc calculation is performed on sectors of 256bytes. This is done by calculating several parity bits over the rows andcolumns. The parity used is even parity which means that the parity bit = 1if the data over which the parity is calculated is 1 and the parity bit = 0if the data over which the parity is calculated is 0. So the totalnumber of bits over the data over which the parity is calculated + theparity bit is even. (see wikipedia if you can't follow this).Parity is often calculated by means of an exclusive or operation,sometimes also referred to as xor. In C the operator for xor is ^Back to ecc.Let's give a small figure:byte   0:  bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0   rp0 rp2 rp4 ... rp14byte   1:  bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0   rp1 rp2 rp4 ... rp14byte   2:  bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0   rp0 rp3 rp4 ... rp14byte   3:  bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0   rp1 rp3 rp4 ... rp14byte   4:  bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0   rp0 rp2 rp5 ... rp14....byte 254:  bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0   rp0 rp3 rp5 ... rp15byte 255:  bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0   rp1 rp3 rp5 ... rp15           cp1  cp0  cp1  cp0  cp1  cp0  cp1  cp0           cp3  cp3  cp2  cp2  cp3  cp3  cp2  cp2           cp5  cp5  cp5  cp5  cp4  cp4  cp4  cp4This figure represents a sector of 256 bytes.cp is my abbreviation for column parity, rp for row parity.Let's start to explain column parity.cp0 is the parity that belongs to all bit0, bit2, bit4, bit6.so the sum of all bit0, bit2, bit4 and bit6 values + cp0 itself is even.Similarly cp1 is the sum of all bit1, bit3, bit5 and bit7.cp2 is the parity over bit0, bit1, bit4 and bit5cp3 is the parity over bit2, bit3, bit6 and bit7.cp4 is the parity over bit0, bit1, bit2 and bit3.cp5 is the parity over bit4, bit5, bit6 and bit7.Note that each of cp0 .. cp5 is exactly one bit.Row parity actually works almost the same.rp0 is the parity of all even bytes (0, 2, 4, 6, ... 252, 254)rp1 is the parity of all odd bytes (1, 3, 5, 7, ..., 253, 255)rp2 is the parity of all bytes 0, 1, 4, 5, 8, 9, ...(so handle two bytes, then skip 2 bytes).rp3 is covers the half rp2 does not cover (bytes 2, 3, 6, 7, 10, 11, ...)for rp4 the rule is cover 4 bytes, skip 4 bytes, cover 4 bytes, skip 4 etc.so rp4 calculates parity over bytes 0, 1, 2, 3, 8, 9, 10, 11, 16, ...)and rp5 covers the other half, so bytes 4, 5, 6, 7, 12, 13, 14, 15, 20, ..The story now becomes quite boring. I guess you get the idea.rp6 covers 8 bytes then skips 8 etcrp7 skips 8 bytes then covers 8 etcrp8 covers 16 bytes then skips 16 etcrp9 skips 16 bytes then covers 16 etcrp10 covers 32 bytes then skips 32 etcrp11 skips 32 bytes then covers 32 etcrp12 covers 64 bytes then skips 64 etcrp13 skips 64 bytes then covers 64 etcrp14 covers 128 bytes then skips 128rp15 skips 128 bytes then covers 128In the end the parity bits are grouped together in three bytes asfollows:ECC    Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0ECC 0   rp07  rp06  rp05  rp04  rp03  rp02  rp01  rp00ECC 1   rp15  rp14  rp13  rp12  rp11  rp10  rp09  rp08ECC 2   cp5   cp4   cp3   cp2   cp1   cp0      1     1I detected after writing this that ST application note AN1823(http://www.st.com/stonline/) gives a muchnicer picture.(but they use line parity as term where I use row parity)Oh well, I'm graphically challenged, so suffer with me for a moment :-)And I could not reuse the ST picture anyway for copyright reasons.Attempt 0=========Implementing the parity calculation is pretty simple.In C pseudocode:for (i = 0; i < 256; i++){    if (i & 0x01)       rp1 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp1;    else       rp0 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp1;    if (i & 0x02)       rp3 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp3;    else       rp2 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp2;    if (i & 0x04)      rp5 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp5;    else      rp4 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp4;    if (i & 0x08)      rp7 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp7;    else      rp6 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp6;    if (i & 0x10)      rp9 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp9;    else      rp8 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp8;    if (i & 0x20)      rp11 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp11;    else    rp10 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp10;    if (i & 0x40)      rp13 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp13;    else      rp12 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp12;    if (i & 0x80)      rp15 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp15;    else      rp14 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp14;    cp0 = bit6 ^ bit4 ^ bit2 ^ bit0 ^ cp0;    cp1 = bit7 ^ bit5 ^ bit3 ^ bit1 ^ cp1;    cp2 = bit5 ^ bit4 ^ bit1 ^ bit0 ^ cp2;    cp3 = bit7 ^ bit6 ^ bit3 ^ bit2 ^ cp3    cp4 = bit3 ^ bit2 ^ bit1 ^ bit0 ^ cp4    cp5 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ cp5}Analysis 0==========C does have bitwise operators but not really operators to do the aboveefficiently (and most hardware has no such instructions either).Therefore without implementing this it was clear that the code above wasnot going to bring me a Nobel prize :-)Fortunately the exclusive or operation is commutative, so we can combinethe values in any order. So instead of calculating all the bitsindividually, let us try to rearrange things.For the column parity this is easy. We can just xor the bytes and in theend filter out the relevant bits. This is pretty nice as it will bringall cp calculation out of the if loop.Similarly we can first xor the bytes for the various rows.This leads to:Attempt 1=========const char parity[256] = {    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0};void ecc1(const unsigned char *buf, unsigned char *code){    int i;    const unsigned char *bp = buf;    unsigned char cur;    unsigned char rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;    unsigned char rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15;    unsigned char par;    par = 0;    rp0 = 0; rp1 = 0; rp2 = 0; rp3 = 0;    rp4 = 0; rp5 = 0; rp6 = 0; rp7 = 0;    rp8 = 0; rp9 = 0; rp10 = 0; rp11 = 0;    rp12 = 0; rp13 = 0; rp14 = 0; rp15 = 0;    for (i = 0; i < 256; i++)    {        cur = *bp++;        par ^= cur;        if (i & 0x01) rp1 ^= cur; else rp0 ^= cur;        if (i & 0x02) rp3 ^= cur; else rp2 ^= cur;        if (i & 0x04) rp5 ^= cur; else rp4 ^= cur;        if (i & 0x08) rp7 ^= cur; else rp6 ^= cur;        if (i & 0x10) rp9 ^= cur; else rp8 ^= cur;        if (i & 0x20) rp11 ^= cur; else rp10 ^= cur;        if (i & 0x40) rp13 ^= cur; else rp12 ^= cur;        if (i & 0x80) rp15 ^= cur; else rp14 ^= cur;    }    code[0] =        (parity[rp7] << 7) |        (parity[rp6] << 6) |        (parity[rp5] << 5) |        (parity[rp4] << 4) |        (parity[rp3] << 3) |        (parity[rp2] << 2) |        (parity[rp1] << 1) |        (parity[rp0]);    code[1] =        (parity[rp15] << 7) |        (parity[rp14] << 6) |        (parity[rp13] << 5) |        (parity[rp12] << 4) |        (parity[rp11] << 3) |        (parity[rp10] << 2) |        (parity[rp9]  << 1) |        (parity[rp8]);    code[2] =        (parity[par & 0xf0] << 7) |        (parity[par & 0x0f] << 6) |        (parity[par & 0xcc] << 5) |        (parity[par & 0x33] << 4) |        (parity[par & 0xaa] << 3) |        (parity[par & 0x55] << 2);    code[0] = ~code[0];    code[1] = ~code[1];    code[2] = ~code[2];}Still pretty straightforward. The last three invert statements are there togive a checksum of 0xff 0xff 0xff for an empty flash. In an empty flashall data is 0xff, so the checksum then matches.I also introduced the parity lookup. I expected this to be the fastestway to calculate the parity, but I will investigate alternatives lateron.Analysis 1==========The code works, but is not terribly efficient. On my system it tookalmost 4 times as much time as the linux driver code. But hey, if it was*that* easy this would have been done long before.No pain. no gain.Fortunately there is plenty of room for improvement.In step 1 we moved from bit-wise calculation to byte-wise calculation.However in C we can also use the unsigned long data type and virtuallyevery modern microprocessor supports 32 bit operations, so why not tryto write our code in such a way that we process data in 32 bit chunks.Of course this means some modification as the row parity is byte bybyte. A quick analysis:for the column parity we use the par variable. When extending to 32 bitswe can in the end easily calculate p0 and p1 from it.(because par now consists of 4 bytes, contributing to rp1, rp0, rp1, rp0respectively)also rp2 and rp3 can be easily retrieved from par as rp3 covers thefirst two bytes and rp2 the last two bytes.Note that of course now the loop is executed only 64 times (256/4).And note that care must taken wrt byte ordering. The way bytes areordered in a long is machine dependent, and might affect us.Anyway, if there is an issue: this code is developed on x86 (to beprecise: a DELL PC with a D920 Intel CPU)And of course the performance might depend on alignment, but I expectthat the I/O buffers in the nand driver are aligned properly (andotherwise that should be fixed to get maximum performance).Let's give it a try...Attempt 2=========extern const char parity[256];void ecc2(const unsigned char *buf, unsigned char *code){    int i;    const unsigned long *bp = (unsigned long *)buf;    unsigned long cur;    unsigned long rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;    unsigned long rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15;    unsigned long par;    par = 0;    rp0 = 0; rp1 = 0; rp2 = 0; rp3 = 0;    rp4 = 0; rp5 = 0; rp6 = 0; rp7 = 0;    rp8 = 0; rp9 = 0; rp10 = 0; rp11 = 0;    rp12 = 0; rp13 = 0; rp14 = 0; rp15 = 0;    for (i = 0; i < 64; i++)    {        cur = *bp++;        par ^= cur;        if (i & 0x01) rp5 ^= cur; else rp4 ^= cur;        if (i & 0x02) rp7 ^= cur; else rp6 ^= cur;        if (i & 0x04) rp9 ^= cur; else rp8 ^= cur;        if (i & 0x08) rp11 ^= cur; else rp10 ^= cur;        if (i & 0x10) rp13 ^= cur; else rp12 ^= cur;        if (i & 0x20) rp15 ^= cur; else rp14 ^= cur;    }    /*       we need to adapt the code generation for the fact that rp vars are now       long; also the column parity calculation needs to be changed.       we'll bring rp4 to 15 back to single byte entities by shifting and       xoring    */    rp4 ^= (rp4 >> 16); rp4 ^= (rp4 >> 8); rp4 &= 0xff;    rp5 ^= (rp5 >> 16); rp5 ^= (rp5 >> 8); rp5 &= 0xff;    rp6 ^= (rp6 >> 16); rp6 ^= (rp6 >> 8); rp6 &= 0xff;    rp7 ^= (rp7 >> 16); rp7 ^= (rp7 >> 8); rp7 &= 0xff;    rp8 ^= (rp8 >> 16); rp8 ^= (rp8 >> 8); rp8 &= 0xff;    rp9 ^= (rp9 >> 16); rp9 ^= (rp9 >> 8); rp9 &= 0xff;    rp10 ^= (rp10 >> 16); rp10 ^= (rp10 >> 8); rp10 &= 0xff;    rp11 ^= (rp11 >> 16); rp11 ^= (rp11 >> 8); rp11 &= 0xff;    rp12 ^= (rp12 >> 16); rp12 ^= (rp12 >> 8); rp12 &= 0xff;    rp13 ^= (rp13 >> 16); rp13 ^= (rp13 >> 8); rp13 &= 0xff;    rp14 ^= (rp14 >> 16); rp14 ^= (rp14 >> 8); rp14 &= 0xff;    rp15 ^= (rp15 >> 16); rp15 ^= (rp15 >> 8); rp15 &= 0xff;    rp3 = (par >> 16); rp3 ^= (rp3 >> 8); rp3 &= 0xff;    rp2 = par & 0xffff; rp2 ^= (rp2 >> 8); rp2 &= 0xff;    par ^= (par >> 16);    rp1 = (par >> 8); rp1 &= 0xff;    rp0 = (par & 0xff);    par ^= (par >> 8); par &= 0xff;    code[0] =        (parity[rp7] << 7) |        (parity[rp6] << 6) |        (parity[rp5] << 5) |        (parity[rp4] << 4) |        (parity[rp3] << 3) |        (parity[rp2] << 2) |        (parity[rp1] << 1) |        (parity[rp0]);    code[1] =        (parity[rp15] << 7) |        (parity[rp14] << 6) |        (parity[rp13] << 5) |        (parity[rp12] << 4) |        (parity[rp11] << 3) |        (parity[rp10] << 2) |        (parity[rp9]  << 1) |        (parity[rp8]);    code[2] =        (parity[par & 0xf0] << 7) |        (parity[par & 0x0f] << 6) |        (parity[par & 0xcc] << 5) |        (parity[par & 0x33] << 4) |        (parity[par & 0xaa] << 3) |        (parity[par & 0x55] << 2);    code[0] = ~code[0];    code[1] = ~code[1];    code[2] = ~code[2];}The parity array is not shown any more. Note also that for theseexamples I kinda deviated from my regular programming style by allowingmultiple statements on a line, not using { } in then and else blockswith only a single statement and by using operators like ^=Analysis 2==========The code (of course) works, and hurray: we are a little bit faster thanthe linux driver code (about 15%). But wait, don't cheer too quickly.THere is more to be gained.If we look at e.g. rp14 and rp15 we see that we either xor our data withrp14 or with rp15. However we also have par which goes over all data.This means there is no need to calculate rp14 as it can be calculated fromrp15 through rp14 = par ^ rp15;(or if desired we can avoid calculating rp15 and calculate it fromrp14).  That is why some places refer to inverse parity.Of course the same thing holds for rp4/5, rp6/7, rp8/9, rp10/11 and rp12/13.Effectively this means we can eliminate the else clause from the ifstatements. Also we can optimise the calculation in the end a little bitby going from long to byte first. Actually we can even avoid the tablelookupsAttempt 3=========Odd replaced:        if (i & 0x01) rp5 ^= cur; else rp4 ^= cur;        if (i & 0x02) rp7 ^= cur; else rp6 ^= cur;        if (i & 0x04) rp9 ^= cur; else rp8 ^= cur;        if (i & 0x08) rp11 ^= cur; else rp10 ^= cur;        if (i & 0x10) rp13 ^= cur; else rp12 ^= cur;        if (i & 0x20) rp15 ^= cur; else rp14 ^= cur;with        if (i & 0x01) rp5 ^= cur;        if (i & 0x02) rp7 ^= cur;        if (i & 0x04) rp9 ^= cur;        if (i & 0x08) rp11 ^= cur;        if (i & 0x10) rp13 ^= cur;        if (i & 0x20) rp15 ^= cur;        and outside the loop added:    rp4  = par ^ rp5;    rp6  = par ^ rp7;    rp8  = par ^ rp9;    rp10  = par ^ rp11;    rp12  = par ^ rp13;    rp14  = par ^ rp15;And after that the code takes about 30% more time, although the number ofstatements is reduced. This is also reflected in the assembly code.Analysis 3==========Very weird. Guess it has to do with caching or instruction parallellismor so. I also tried on an eeePC (Celeron, clocked at 900 Mhz). Interestingobservation was that this one is only 30% slower (according to time)executing the code as my 3Ghz D920 processor.Well, it was expected not to be easy so maybe instead move to adifferent track: let's move back to the code from attempt2 and do someloop unrolling. This will eliminate a few if statements. I'll trydifferent amounts of unrolling to see what works best.Attempt 4=========Unrolled the loop 1, 2, 3 and 4 times.For 4 the code starts with:    for (i = 0; i < 4; i++)    {        cur = *bp++;        par ^= cur;        rp4 ^= cur;        rp6 ^= cur;        rp8 ^= cur;        rp10 ^= cur;        if (i & 0x1) rp13 ^= cur; else rp12 ^= cur;        if (i & 0x2) rp15 ^= cur; else rp14 ^= cur;        cur = *bp++;        par ^= cur;        rp5 ^= cur;        rp6 ^= cur;        ...Analysis 4==========Unrolling once gains about 15%Unrolling twice keeps the gain at about 15%Unrolling three times gives a gain of 30% compared to attempt 2.Unrolling four times gives a marginal improvement compared to unrollingthree times.I decided to proceed with a four time unrolled loop anyway. It was my gutfeeling that in the next steps I would obtain additional gain from it.The next step was triggered by the fact that par contains the xor of allbytes and rp4 and rp5 each contain the xor of half of the bytes.So in effect par = rp4 ^ rp5. But as xor is commutative we can also saythat rp5 = par ^ rp4. So no need to keep both rp4 and rp5 around. We caneliminate rp5 (or rp4, but I already foresaw another optimisation).The same holds for rp6/7, rp8/9, rp10/11 rp12/13 and rp14/15.Attempt 5=========Effectively so all odd digit rp assignments in the loop were removed.This included the else clause of the if statements.Of course after the loop we need to correct things by adding code like:    rp5 = par ^ rp4;Also the initial assignments (rp5 = 0; etc) could be removed.Along the line I also removed the initialisation of rp0/1/2/3.Analysis 5==========Measurements showed this was a good move. The run-time roughly halvedcompared with attempt 4 with 4 times unrolled, and we only require 1/3rdof the processor time compared to the current code in the linux kernel.However, still I thought there was more. I didn't like all the ifstatements. Why not keep a running parity and only keep the last ifstatement. Time for yet another version!Attempt 6=========THe code within the for loop was changed to:    for (i = 0; i < 4; i++)    {        cur = *bp++; tmppar  = cur; rp4 ^= cur;        cur = *bp++; tmppar ^= cur; rp6 ^= tmppar;        cur = *bp++; tmppar ^= cur; rp4 ^= cur;        cur = *bp++; tmppar ^= cur; rp8 ^= tmppar;        cur = *bp++; tmppar ^= cur; rp4 ^= cur; rp6 ^= cur;        cur = *bp++; tmppar ^= cur; rp6 ^= cur;        cur = *bp++; tmppar ^= cur; rp4 ^= cur;        cur = *bp++; tmppar ^= cur; rp10 ^= tmppar;        cur = *bp++; tmppar ^= cur; rp4 ^= cur; rp6 ^= cur; rp8 ^= cur;        cur = *bp++; tmppar ^= cur; rp6 ^= cur; rp8 ^= cur;        cur = *bp++; tmppar ^= cur; rp4 ^= cur; rp8 ^= cur;        cur = *bp++; tmppar ^= cur; rp8 ^= cur;        cur = *bp++; tmppar ^= cur; rp4 ^= cur; rp6 ^= cur;        cur = *bp++; tmppar ^= cur; rp6 ^= cur;        cur = *bp++; tmppar ^= cur; rp4 ^= cur;        cur = *bp++; tmppar ^= cur;        par ^= tmppar;        if ((i & 0x1) == 0) rp12 ^= tmppar;        if ((i & 0x2) == 0) rp14 ^= tmppar;    }As you can see tmppar is used to accumulate the parity within a foriteration. In the last 3 statements is added to par and, if needed,to rp12 and rp14.While making the changes I also found that I could exploit that tmpparcontains the running parity for this iteration. So instead of having:rp4 ^= cur; rp6 = cur;I removed the rp6 = cur; statement and did rp6 ^= tmppar; on nextstatement. A similar change was done for rp8 and rp10Analysis 6==========Measuring this code again showed big gain. When executing the originallinux code 1 million times, this took about 1 second on my system.(using time to measure the performance). After this iteration I was backto 0.075 sec. Actually I had to decide to start measuring over 10million iterations in order not to lose too much accuracy. This onedefinitely seemed to be the jackpot!There is a little bit more room for improvement though. There are threeplaces with statements:rp4 ^= cur; rp6 ^= cur;It seems more efficient to also maintain a variable rp4_6 in the whileloop; This eliminates 3 statements per loop. Of course after the loop weneed to correct by adding:    rp4 ^= rp4_6;    rp6 ^= rp4_6Furthermore there are 4 sequential assignments to rp8. This can beencoded slightly more efficiently by saving tmppar before those 4 linesand later do rp8 = rp8 ^ tmppar ^ notrp8;(where notrp8 is the value of rp8 before those 4 lines).Again a use of the commutative property of xor.Time for a new test!Attempt 7=========The new code now looks like:    for (i = 0; i < 4; i++)    {        cur = *bp++; tmppar  = cur; rp4 ^= cur;        cur = *bp++; tmppar ^= cur; rp6 ^= tmppar;        cur = *bp++; tmppar ^= cur; rp4 ^= cur;        cur = *bp++; tmppar ^= cur; rp8 ^= tmppar;        cur = *bp++; tmppar ^= cur; rp4_6 ^= cur;        cur = *bp++; tmppar ^= cur; rp6 ^= cur;        cur = *bp++; tmppar ^= cur; rp4 ^= cur;        cur = *bp++; tmppar ^= cur; rp10 ^= tmppar;        notrp8 = tmppar;        cur = *bp++; tmppar ^= cur; rp4_6 ^= cur;        cur = *bp++; tmppar ^= cur; rp6 ^= cur;        cur = *bp++; tmppar ^= cur; rp4 ^= cur;        cur = *bp++; tmppar ^= cur;        rp8 = rp8 ^ tmppar ^ notrp8;        cur = *bp++; tmppar ^= cur; rp4_6 ^= cur;        cur = *bp++; tmppar ^= cur; rp6 ^= cur;        cur = *bp++; tmppar ^= cur; rp4 ^= cur;        cur = *bp++; tmppar ^= cur;        par ^= tmppar;        if ((i & 0x1) == 0) rp12 ^= tmppar;        if ((i & 0x2) == 0) rp14 ^= tmppar;    }    rp4 ^= rp4_6;    rp6 ^= rp4_6;Not a big change, but every penny counts :-)Analysis 7==========Actually this made things worse. Not very much, but I don't want to moveinto the wrong direction. Maybe something to investigate later. Couldhave to do with caching again.Guess that is what there is to win within the loop. Maybe unrolling onemore time will help. I'll keep the optimisations from 7 for now.Attempt 8=========Unrolled the loop one more time.Analysis 8==========This makes things worse. Let's stick with attempt 6 and continue from there.Although it seems that the code within the loop cannot be optimisedfurther there is still room to optimize the generation of the ecc codes.We can simply calculate the total parity. If this is 0 then rp4 = rp5etc. If the parity is 1, then rp4 = !rp5;But if rp4 = rp5 we do not need rp5 etc. We can just write the even bitsin the result byte and then do something like    code[0] |= (code[0] << 1);Lets test this.Attempt 9=========Changed the code but again this slightly degrades performance. Tried allkind of other things, like having dedicated parity arrays to avoid theshift after parity[rp7] << 7; No gain.Change the lookup using the parity array by using shift operators (e.g.replace parity[rp7] << 7 with:rp7 ^= (rp7 << 4);rp7 ^= (rp7 << 2);rp7 ^= (rp7 << 1);rp7 &= 0x80;No gain.The only marginal change was inverting the parity bits, so we can removethe last three invert statements.Ah well, pity this does not deliver more. Then again 10 millioniterations using the linux driver code takes between 13 and 13.5seconds, whereas my code now takes about 0.73 seconds for those 10million iterations. So basically I've improved the performance by afactor 18 on my system. Not that bad. Of course on different hardwareyou will get different results. No warranties!But of course there is no such thing as a free lunch. The codesize almosttripled (from 562 bytes to 1434 bytes). Then again, it is not that much.Correcting errors=================For correcting errors I again used the ST application note as a starter,but I also peeked at the existing code.The algorithm itself is pretty straightforward. Just xor the given andthe calculated ecc. If all bytes are 0 there is no problem. If 11 bitsare 1 we have one correctable bit error. If there is 1 bit 1, we have anerror in the given ecc code.It proved to be fastest to do some table lookups. Performance gainintroduced by this is about a factor 2 on my system when a repair had tobe done, and 1% or so if no repair had to be done.Code size increased from 330 bytes to 686 bytes for this function.(gcc 4.2, -O3)Conclusion==========The gain when calculating the ecc is tremendous. Om my development hardwarea speedup of a factor of 18 for ecc calculation was achieved. On a test on anembedded system with a MIPS core a factor 7 was obtained.On  a test with a Linksys NSLU2 (ARMv5TE processor) the speedup was a factor5 (big endian mode, gcc 4.1.2, -O3)For correction not much gain could be obtained (as bitflips are rare). Thenagain there are also much less cycles spent there.It seems there is not much more gain possible in this, at least whenprogrammed in C. Of course it might be possible to squeeze something moreout of it with an assembler program, but due to pipeline behaviour etcthis is very tricky (at least for intel hw).Author: Frans MeulenbroeksCopyright (C) 2008 Koninklijke Philips Electronics NV.

 

转载于:https://www.cnblogs.com/IT-BOY/p/3565765.html

你可能感兴趣的文章
IOS小技巧整理
查看>>
WebDriverExtensionsByC#
查看>>
我眼中的技术地图
查看>>
lc 145. Binary Tree Postorder Traversal
查看>>
sublime 配置java运行环境
查看>>
在centos上开关tomcat
查看>>
重启rabbitmq服务
查看>>
正则表达式(进阶篇)
查看>>
无人值守安装linux系统
查看>>
【传道】中国首部淘宝卖家演讲公开课:农业本该如此
查看>>
jQuery应用 代码片段
查看>>
MVC+Servlet+mysql+jsp读取数据库信息
查看>>
黑马程序员——2 注释
查看>>
用OGRE1.74搭建游戏框架(三)--加入人物控制和场景
查看>>
转化课-计算机基础及上网过程
查看>>
android dialog使用自定义布局 设置窗体大小位置
查看>>
ionic2+ 基础
查看>>
互联网模式下我们更加应该“专注”
查看>>
myeclipse集成jdk、tomcat8、maven、svn
查看>>
查询消除重复行
查看>>