引子: 今天上嵌入式课程时,老师讲到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.c
I felt there was room for optimisation. I bashed the code for a few hours
performing tricks like table lookup removing superfluous code etc.
After that the speed was increased by -%.
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 someone
or someone learns something from it. The problem
=========== NAND flash (at least SLC one) typically has sectors of 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 in
laymans terms (and apologies to all the pro's in the field in case I do
not use the right terminology, my coding theory class was almost
years ago, and I must admit it was not one of my favourites). As I said before the ecc calculation is performed on sectors of
bytes. This is done by calculating several parity bits over the rows and
columns. The parity used is even parity which means that the parity bit =
if the data over which the parity is calculated is and the parity bit =
if the data over which the parity is calculated is . So the total
number of bits over the data over which the parity is calculated + the
parity 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 : bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp0 rp2 rp4 ... rp14
byte : bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp1 rp2 rp4 ... rp14
byte : bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp0 rp3 rp4 ... rp14
byte : bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp1 rp3 rp4 ... rp14
byte : bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp0 rp2 rp5 ... rp14
....
byte : bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp0 rp3 rp5 ... rp15
byte : 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 cp4 This figure represents a sector of 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 bit5
cp3 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 (, , , , ... , )
rp1 is the parity of all odd bytes (, , , , ..., , )
rp2 is the parity of all bytes , , , , , , ...
(so handle two bytes, then skip bytes).
rp3 is covers the half rp2 does not cover (bytes , , , , , , ...)
for rp4 the rule is cover bytes, skip bytes, cover bytes, skip etc.
so rp4 calculates parity over bytes , , , , , , , , , ...)
and rp5 covers the other half, so bytes , , , , , , , , , ..
The story now becomes quite boring. I guess you get the idea.
rp6 covers bytes then skips etc
rp7 skips bytes then covers etc
rp8 covers bytes then skips etc
rp9 skips bytes then covers etc
rp10 covers bytes then skips etc
rp11 skips bytes then covers etc
rp12 covers bytes then skips etc
rp13 skips bytes then covers etc
rp14 covers bytes then skips
rp15 skips bytes then covers In the end the parity bits are grouped together in three bytes as
follows:
ECC Bit Bit Bit Bit Bit Bit Bit Bit
ECC rp07 rp06 rp05 rp04 rp03 rp02 rp01 rp00
ECC rp15 rp14 rp13 rp12 rp11 rp10 rp09 rp08
ECC cp5 cp4 cp3 cp2 cp1 cp0 I detected after writing this that ST application note AN1823
(http://www.st.com/stonline/) gives a much
nicer 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
========= Implementing the parity calculation is pretty simple.
In C pseudocode:
for (i = ; i < ; 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
========== C does have bitwise operators but not really operators to do the above
efficiently (and most hardware has no such instructions either).
Therefore without implementing this it was clear that the code above was
not going to bring me a Nobel prize :-) Fortunately the exclusive or operation is commutative, so we can combine
the values in any order. So instead of calculating all the bits
individually, let us try to rearrange things.
For the column parity this is easy. We can just xor the bytes and in the
end filter out the relevant bits. This is pretty nice as it will bring
all cp calculation out of the if loop. Similarly we can first xor the bytes for the various rows.
This leads to: Attempt
========= const char parity[] = {
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , ,
}; 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 = ;
rp0 = ; rp1 = ; rp2 = ; rp3 = ;
rp4 = ; rp5 = ; rp6 = ; rp7 = ;
rp8 = ; rp9 = ; rp10 = ; rp11 = ;
rp12 = ; rp13 = ; rp14 = ; rp15 = ; for (i = ; i < ; 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[] =
(parity[rp7] << ) |
(parity[rp6] << ) |
(parity[rp5] << ) |
(parity[rp4] << ) |
(parity[rp3] << ) |
(parity[rp2] << ) |
(parity[rp1] << ) |
(parity[rp0]);
code[] =
(parity[rp15] << ) |
(parity[rp14] << ) |
(parity[rp13] << ) |
(parity[rp12] << ) |
(parity[rp11] << ) |
(parity[rp10] << ) |
(parity[rp9] << ) |
(parity[rp8]);
code[] =
(parity[par & 0xf0] << ) |
(parity[par & 0x0f] << ) |
(parity[par & 0xcc] << ) |
(parity[par & 0x33] << ) |
(parity[par & 0xaa] << ) |
(parity[par & 0x55] << );
code[] = ~code[];
code[] = ~code[];
code[] = ~code[];
} Still pretty straightforward. The last three invert statements are there to
give a checksum of 0xff 0xff 0xff for an empty flash. In an empty flash
all data is 0xff, so the checksum then matches. I also introduced the parity lookup. I expected this to be the fastest
way to calculate the parity, but I will investigate alternatives later
on. Analysis
========== The code works, but is not terribly efficient. On my system it took
almost 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 we moved from bit-wise calculation to byte-wise calculation.
However in C we can also use the unsigned long data type and virtually
every modern microprocessor supports bit operations, so why not try
to write our code in such a way that we process data in bit chunks. Of course this means some modification as the row parity is byte by
byte. A quick analysis:
for the column parity we use the par variable. When extending to bits
we can in the end easily calculate p0 and p1 from it.
(because par now consists of bytes, contributing to rp1, rp0, rp1, rp0
respectively)
also rp2 and rp3 can be easily retrieved from par as rp3 covers the
first two bytes and rp2 the last two bytes. Note that of course now the loop is executed only times (/).
And note that care must taken wrt byte ordering. The way bytes are
ordered in a long is machine dependent, and might affect us.
Anyway, if there is an issue: this code is developed on x86 (to be
precise: a DELL PC with a D920 Intel CPU) And of course the performance might depend on alignment, but I expect
that the I/O buffers in the nand driver are aligned properly (and
otherwise that should be fixed to get maximum performance). Let's give it a try... Attempt
========= extern const char parity[]; 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 = ;
rp0 = ; rp1 = ; rp2 = ; rp3 = ;
rp4 = ; rp5 = ; rp6 = ; rp7 = ;
rp8 = ; rp9 = ; rp10 = ; rp11 = ;
rp12 = ; rp13 = ; rp14 = ; rp15 = ; for (i = ; i < ; 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 >> ); rp4 ^= (rp4 >> ); rp4 &= 0xff;
rp5 ^= (rp5 >> ); rp5 ^= (rp5 >> ); rp5 &= 0xff;
rp6 ^= (rp6 >> ); rp6 ^= (rp6 >> ); rp6 &= 0xff;
rp7 ^= (rp7 >> ); rp7 ^= (rp7 >> ); rp7 &= 0xff;
rp8 ^= (rp8 >> ); rp8 ^= (rp8 >> ); rp8 &= 0xff;
rp9 ^= (rp9 >> ); rp9 ^= (rp9 >> ); rp9 &= 0xff;
rp10 ^= (rp10 >> ); rp10 ^= (rp10 >> ); rp10 &= 0xff;
rp11 ^= (rp11 >> ); rp11 ^= (rp11 >> ); rp11 &= 0xff;
rp12 ^= (rp12 >> ); rp12 ^= (rp12 >> ); rp12 &= 0xff;
rp13 ^= (rp13 >> ); rp13 ^= (rp13 >> ); rp13 &= 0xff;
rp14 ^= (rp14 >> ); rp14 ^= (rp14 >> ); rp14 &= 0xff;
rp15 ^= (rp15 >> ); rp15 ^= (rp15 >> ); rp15 &= 0xff;
rp3 = (par >> ); rp3 ^= (rp3 >> ); rp3 &= 0xff;
rp2 = par & 0xffff; rp2 ^= (rp2 >> ); rp2 &= 0xff;
par ^= (par >> );
rp1 = (par >> ); rp1 &= 0xff;
rp0 = (par & 0xff);
par ^= (par >> ); par &= 0xff; code[] =
(parity[rp7] << ) |
(parity[rp6] << ) |
(parity[rp5] << ) |
(parity[rp4] << ) |
(parity[rp3] << ) |
(parity[rp2] << ) |
(parity[rp1] << ) |
(parity[rp0]);
code[] =
(parity[rp15] << ) |
(parity[rp14] << ) |
(parity[rp13] << ) |
(parity[rp12] << ) |
(parity[rp11] << ) |
(parity[rp10] << ) |
(parity[rp9] << ) |
(parity[rp8]);
code[] =
(parity[par & 0xf0] << ) |
(parity[par & 0x0f] << ) |
(parity[par & 0xcc] << ) |
(parity[par & 0x33] << ) |
(parity[par & 0xaa] << ) |
(parity[par & 0x55] << );
code[] = ~code[];
code[] = ~code[];
code[] = ~code[];
} The parity array is not shown any more. Note also that for these
examples I kinda deviated from my regular programming style by allowing
multiple statements on a line, not using { } in then and else blocks
with only a single statement and by using operators like ^= Analysis
========== The code (of course) works, and hurray: we are a little bit faster than
the linux driver code (about %). 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 with
rp14 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 from
rp15 through rp14 = par ^ rp15;
(or if desired we can avoid calculating rp15 and calculate it from
rp14). That is why some places refer to inverse parity.
Of course the same thing holds for rp4/, rp6/, rp8/, rp10/ and rp12/.
Effectively this means we can eliminate the else clause from the if
statements. Also we can optimise the calculation in the end a little bit
by going from long to byte first. Actually we can even avoid the table
lookups Attempt
========= 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 % more time, although the number of
statements is reduced. This is also reflected in the assembly code. Analysis
========== Very weird. Guess it has to do with caching or instruction parallellism
or so. I also tried on an eeePC (Celeron, clocked at Mhz). Interesting
observation was that this one is only % 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 a
different track: let's move back to the code from attempt2 and do some
loop unrolling. This will eliminate a few if statements. I'll try
different amounts of unrolling to see what works best. Attempt
========= Unrolled the loop , , and times.
For the code starts with: for (i = ; i < ; 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
========== Unrolling once gains about %
Unrolling twice keeps the gain at about %
Unrolling three times gives a gain of % compared to attempt .
Unrolling four times gives a marginal improvement compared to unrolling
three times. I decided to proceed with a four time unrolled loop anyway. It was my gut
feeling 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 all
bytes 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 say
that rp5 = par ^ rp4. So no need to keep both rp4 and rp5 around. We can
eliminate rp5 (or rp4, but I already foresaw another optimisation).
The same holds for rp6/, rp8/, rp10/ rp12/ and rp14/. Attempt
========= 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 = ; etc) could be removed.
Along the line I also removed the initialisation of rp0///. Analysis
========== Measurements showed this was a good move. The run-time roughly halved
compared with attempt with times unrolled, and we only require /3rd
of 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 if
statements. Why not keep a running parity and only keep the last if
statement. Time for yet another version! Attempt
========= THe code within the for loop was changed to: for (i = ; i < ; 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) == ) rp12 ^= tmppar;
if ((i & 0x2) == ) rp14 ^= tmppar;
} As you can see tmppar is used to accumulate the parity within a for
iteration. In the last statements is added to par and, if needed,
to rp12 and rp14. While making the changes I also found that I could exploit that tmppar
contains 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 next
statement. A similar change was done for rp8 and rp10 Analysis
========== Measuring this code again showed big gain. When executing the original
linux code million times, this took about second on my system.
(using time to measure the performance). After this iteration I was back
to 0.075 sec. Actually I had to decide to start measuring over
million iterations in order not to lose too much accuracy. This one
definitely seemed to be the jackpot! There is a little bit more room for improvement though. There are three
places with statements:
rp4 ^= cur; rp6 ^= cur;
It seems more efficient to also maintain a variable rp4_6 in the while
loop; This eliminates statements per loop. Of course after the loop we
need to correct by adding:
rp4 ^= rp4_6;
rp6 ^= rp4_6
Furthermore there are sequential assignments to rp8. This can be
encoded slightly more efficiently by saving tmppar before those lines
and later do rp8 = rp8 ^ tmppar ^ notrp8;
(where notrp8 is the value of rp8 before those lines).
Again a use of the commutative property of xor.
Time for a new test! Attempt
========= The new code now looks like: for (i = ; i < ; 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) == ) rp12 ^= tmppar;
if ((i & 0x2) == ) rp14 ^= tmppar;
}
rp4 ^= rp4_6;
rp6 ^= rp4_6; Not a big change, but every penny counts :-) Analysis
========== Actually this made things worse. Not very much, but I don't want to move
into the wrong direction. Maybe something to investigate later. Could
have to do with caching again. Guess that is what there is to win within the loop. Maybe unrolling one
more time will help. I'll keep the optimisations from 7 for now. Attempt
========= Unrolled the loop one more time. Analysis
========== 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 optimised
further there is still room to optimize the generation of the ecc codes.
We can simply calculate the total parity. If this is then rp4 = rp5
etc. If the parity is , then rp4 = !rp5;
But if rp4 = rp5 we do not need rp5 etc. We can just write the even bits
in the result byte and then do something like
code[] |= (code[] << );
Lets test this. Attempt
========= Changed the code but again this slightly degrades performance. Tried all
kind of other things, like having dedicated parity arrays to avoid the
shift after parity[rp7] << ; No gain.
Change the lookup using the parity array by using shift operators (e.g.
replace parity[rp7] << with:
rp7 ^= (rp7 << );
rp7 ^= (rp7 << );
rp7 ^= (rp7 << );
rp7 &= 0x80;
No gain. The only marginal change was inverting the parity bits, so we can remove
the last three invert statements. Ah well, pity this does not deliver more. Then again million
iterations using the linux driver code takes between and 13.5
seconds, whereas my code now takes about 0.73 seconds for those
million iterations. So basically I've improved the performance by a
factor on my system. Not that bad. Of course on different hardware
you will get different results. No warranties! But of course there is no such thing as a free lunch. The codesize almost
tripled (from bytes to 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 and
the calculated ecc. If all bytes are there is no problem. If bits
are we have one correctable bit error. If there is bit , we have an
error in the given ecc code.
It proved to be fastest to do some table lookups. Performance gain
introduced by this is about a factor on my system when a repair had to
be done, and % or so if no repair had to be done.
Code size increased from bytes to bytes for this function.
(gcc 4.2, -O3) Conclusion
========== The gain when calculating the ecc is tremendous. Om my development hardware
a speedup of a factor of for ecc calculation was achieved. On a test on an
embedded system with a MIPS core a factor was obtained.
On a test with a Linksys NSLU2 (ARMv5TE processor) the speedup was a factor
(big endian mode, gcc 4.1., -O3)
For correction not much gain could be obtained (as bitflips are rare). Then
again there are also much less cycles spent there. It seems there is not much more gain possible in this, at least when
programmed in C. Of course it might be possible to squeeze something more
out of it with an assembler program, but due to pipeline behaviour etc
this is very tricky (at least for intel hw). Author: Frans Meulenbroeks
Copyright (C) Koninklijke Philips Electronics NV.

ECC校验优化之路的更多相关文章

  1. 微博MySQL优化之路--dockone微信群分享

    微博MySQL优化之路 数据库是所有架构中不可缺少的一环,一旦数据库出现性能问题,那对整个系统都回来带灾难性的后果.并且数据库一旦出现问题,由于数据库天生有状态(分主从)带数据(一般还不小),所以出问 ...

  2. 新浪微博iOS客户端架构与优化之路

    新浪微博iOS客户端架构与优化之路   随着Facebook.Twitter.微博的崛起,向UGC.PGC.OGC,自媒体提供平台的内 容消费型App逐渐形成了独特的客户端架构模式.与电商和通讯工具类 ...

  3. ECC校验原理以及在Nand Flash中的应用

         本篇文章主要介绍ECC基本原理以及在Nand Flash中的应用,本文记录自己对ECC校验原理的理解和学习. ECC介绍      ECC,全称为Error Correcting Code, ...

  4. NAND FLASH ECC校验原理与实现

    ECC简介 由于NAND Flash的工艺不能保证NAND的Memory Array在其生命周期中保持性能的可靠,因此,在NAND的生产中及使用过程中会产生坏块.为了检测数据的可靠性,在应用NAND  ...

  5. 阿里巴巴 web前端性能优化进阶路

    Web前端性能优化WPO,相信大多数前端同学都不会陌生,在各自所负责的站点页面中,也都会或多或少的有过一定的技术实践.可以说,这个领域并不缺乏成熟技术理论和技术牛人:例如Yahoo的web站点性能优化 ...

  6. NandFlash ECC 校验

    ECC的全称是Error Checking and Correction,是一种用于Nand的差错检测和修正算法.如果操作时序和电路稳定性不存在问题的话,NAND Flash出错的时候一般不会造成整个 ...

  7. Golang 优化之路——bitset

    写在前面 开发过程中会经常处理集合这种数据结构,简单点的处理方法都是使用内置的map实现.但是如果要应对大量数据,例如,存放大量电话号码,使用map占用内存大的问题就会凸显出来.内存占用高又会带来一些 ...

  8. 说说NAND FLASH以及相关ECC校验方法

    Flash名称的由来,Flash的擦除操作是以block块为单位的,与此相对应的是其他很多存储设备,是以bit位为最小读取/写入的单位,Flash是一次性地擦除整个块:在发送一个擦除命令后,一次性地将 ...

  9. 专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路(HipHop,HHVM)

    专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路 http://www.infoq.com/cn/articles/interview-alibaba-zhaohaiping

随机推荐

  1. struts2 package元素配置(转载)

    package 元素的所有属性及对应功能: Attribute Required Description name yes key to for other packages to reference ...

  2. wiegand/韦根

    韦根 参考: 1.wiegand/韦根驱动

  3. Cabarc Overview (Microsoft TechNet)

    Original Link:  Cabarc Overview Applies To: Windows Server 2003, Windows Server 2003 R2, Windows Ser ...

  4. ubuntu lua安装

    #解压 tar -xzvf lua5.2.2.tar.gz #进入lua5.2.2文件夹 cd lua5.2.2 #执行make sudo make linux #提示如下错误: #lua.c:67: ...

  5. the evaluation period for visual studio trial edition has ended的解决方法-转发

    首先献上自己收集的Visual studio 2008序列号: Visual Studio 2008 Professional Edition: XMQ2Y-4T3V6-XJ48Y-D3K2V-6C4 ...

  6. Linux内核中的通用双向循环链表

    开发中接触Linux越来越多,休息放松之余,免不了翻看翻看神秘的Linux的内核.看到双向链表时,觉得挺有意思的,此文记下. 作为众多基础数据结构中的一员,双向循环链表在各种“教科书”中的实现是相当的 ...

  7. ASP.NET MVC Web API使用示例

    上篇博客讲解rest服务开发时,曾经提到过asp.net mvc中的rest api,由于篇幅原因,没有在上篇博客中进行讲解,这里专门拿出来进行讨论.还是一样引用上次的案例,用asp.net mvc提 ...

  8. js调用asp.net 后台属性值

    后台代码: public string title = "js调用后台属性值"; public void getContent() { return title; } 前台代码: ...

  9. 如何做到 jQuery-free?

    一.选取DOM元素 jQuery的核心是通过各种选择器,选中DOM元素,可以用querySelectorAll方法模拟这个功能. var $ = document.querySelectorAll.b ...

  10. Silverlight应用程序中调用WCF Ria Services访问数据库图片

    WCF Ria Services(通常称为RIA服务),专门设计让Silverlight应用程序访问数据库,网上有关其示例应用都是基于简单的数据显示,其中MSDN网站上有详细的解决方案介绍,地址htt ...