nand_ecc.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. Introduction
  2. ============
  3. Having looked at the linux mtd/nand driver and more specific at nand_ecc.c
  4. I felt there was room for optimisation. I bashed the code for a few hours
  5. performing tricks like table lookup removing superfluous code etc.
  6. After that the speed was increased by 35-40%.
  7. Still I was not too happy as I felt there was additional room for improvement.
  8. Bad! I was hooked.
  9. I decided to annotate my steps in this file. Perhaps it is useful to someone
  10. or someone learns something from it.
  11. The problem
  12. ===========
  13. NAND flash (at least SLC one) typically has sectors of 256 bytes.
  14. However NAND flash is not extremely reliable so some error detection
  15. (and sometimes correction) is needed.
  16. This is done by means of a Hamming code. I'll try to explain it in
  17. laymans terms (and apologies to all the pro's in the field in case I do
  18. not use the right terminology, my coding theory class was almost 30
  19. years ago, and I must admit it was not one of my favourites).
  20. As I said before the ecc calculation is performed on sectors of 256
  21. bytes. This is done by calculating several parity bits over the rows and
  22. columns. The parity used is even parity which means that the parity bit = 1
  23. if the data over which the parity is calculated is 1 and the parity bit = 0
  24. if the data over which the parity is calculated is 0. So the total
  25. number of bits over the data over which the parity is calculated + the
  26. parity bit is even. (see wikipedia if you can't follow this).
  27. Parity is often calculated by means of an exclusive or operation,
  28. sometimes also referred to as xor. In C the operator for xor is ^
  29. Back to ecc.
  30. Let's give a small figure:
  31. byte 0: bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp0 rp2 rp4 ... rp14
  32. byte 1: bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp1 rp2 rp4 ... rp14
  33. byte 2: bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp0 rp3 rp4 ... rp14
  34. byte 3: bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp1 rp3 rp4 ... rp14
  35. byte 4: bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp0 rp2 rp5 ... rp14
  36. ....
  37. byte 254: bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp0 rp3 rp5 ... rp15
  38. byte 255: bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 rp1 rp3 rp5 ... rp15
  39. cp1 cp0 cp1 cp0 cp1 cp0 cp1 cp0
  40. cp3 cp3 cp2 cp2 cp3 cp3 cp2 cp2
  41. cp5 cp5 cp5 cp5 cp4 cp4 cp4 cp4
  42. This figure represents a sector of 256 bytes.
  43. cp is my abbreviation for column parity, rp for row parity.
  44. Let's start to explain column parity.
  45. cp0 is the parity that belongs to all bit0, bit2, bit4, bit6.
  46. so the sum of all bit0, bit2, bit4 and bit6 values + cp0 itself is even.
  47. Similarly cp1 is the sum of all bit1, bit3, bit5 and bit7.
  48. cp2 is the parity over bit0, bit1, bit4 and bit5
  49. cp3 is the parity over bit2, bit3, bit6 and bit7.
  50. cp4 is the parity over bit0, bit1, bit2 and bit3.
  51. cp5 is the parity over bit4, bit5, bit6 and bit7.
  52. Note that each of cp0 .. cp5 is exactly one bit.
  53. Row parity actually works almost the same.
  54. rp0 is the parity of all even bytes (0, 2, 4, 6, ... 252, 254)
  55. rp1 is the parity of all odd bytes (1, 3, 5, 7, ..., 253, 255)
  56. rp2 is the parity of all bytes 0, 1, 4, 5, 8, 9, ...
  57. (so handle two bytes, then skip 2 bytes).
  58. rp3 is covers the half rp2 does not cover (bytes 2, 3, 6, 7, 10, 11, ...)
  59. for rp4 the rule is cover 4 bytes, skip 4 bytes, cover 4 bytes, skip 4 etc.
  60. so rp4 calculates parity over bytes 0, 1, 2, 3, 8, 9, 10, 11, 16, ...)
  61. and rp5 covers the other half, so bytes 4, 5, 6, 7, 12, 13, 14, 15, 20, ..
  62. The story now becomes quite boring. I guess you get the idea.
  63. rp6 covers 8 bytes then skips 8 etc
  64. rp7 skips 8 bytes then covers 8 etc
  65. rp8 covers 16 bytes then skips 16 etc
  66. rp9 skips 16 bytes then covers 16 etc
  67. rp10 covers 32 bytes then skips 32 etc
  68. rp11 skips 32 bytes then covers 32 etc
  69. rp12 covers 64 bytes then skips 64 etc
  70. rp13 skips 64 bytes then covers 64 etc
  71. rp14 covers 128 bytes then skips 128
  72. rp15 skips 128 bytes then covers 128
  73. In the end the parity bits are grouped together in three bytes as
  74. follows:
  75. ECC Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
  76. ECC 0 rp07 rp06 rp05 rp04 rp03 rp02 rp01 rp00
  77. ECC 1 rp15 rp14 rp13 rp12 rp11 rp10 rp09 rp08
  78. ECC 2 cp5 cp4 cp3 cp2 cp1 cp0 1 1
  79. I detected after writing this that ST application note AN1823
  80. (http://www.st.com/stonline/) gives a much
  81. nicer picture.(but they use line parity as term where I use row parity)
  82. Oh well, I'm graphically challenged, so suffer with me for a moment :-)
  83. And I could not reuse the ST picture anyway for copyright reasons.
  84. Attempt 0
  85. =========
  86. Implementing the parity calculation is pretty simple.
  87. In C pseudocode:
  88. for (i = 0; i < 256; i++)
  89. {
  90. if (i & 0x01)
  91. rp1 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp1;
  92. else
  93. rp0 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp1;
  94. if (i & 0x02)
  95. rp3 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp3;
  96. else
  97. rp2 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp2;
  98. if (i & 0x04)
  99. rp5 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp5;
  100. else
  101. rp4 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp4;
  102. if (i & 0x08)
  103. rp7 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp7;
  104. else
  105. rp6 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp6;
  106. if (i & 0x10)
  107. rp9 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp9;
  108. else
  109. rp8 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp8;
  110. if (i & 0x20)
  111. rp11 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp11;
  112. else
  113. rp10 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp10;
  114. if (i & 0x40)
  115. rp13 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp13;
  116. else
  117. rp12 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp12;
  118. if (i & 0x80)
  119. rp15 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp15;
  120. else
  121. rp14 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0 ^ rp14;
  122. cp0 = bit6 ^ bit4 ^ bit2 ^ bit0 ^ cp0;
  123. cp1 = bit7 ^ bit5 ^ bit3 ^ bit1 ^ cp1;
  124. cp2 = bit5 ^ bit4 ^ bit1 ^ bit0 ^ cp2;
  125. cp3 = bit7 ^ bit6 ^ bit3 ^ bit2 ^ cp3
  126. cp4 = bit3 ^ bit2 ^ bit1 ^ bit0 ^ cp4
  127. cp5 = bit7 ^ bit6 ^ bit5 ^ bit4 ^ cp5
  128. }
  129. Analysis 0
  130. ==========
  131. C does have bitwise operators but not really operators to do the above
  132. efficiently (and most hardware has no such instructions either).
  133. Therefore without implementing this it was clear that the code above was
  134. not going to bring me a Nobel prize :-)
  135. Fortunately the exclusive or operation is commutative, so we can combine
  136. the values in any order. So instead of calculating all the bits
  137. individually, let us try to rearrange things.
  138. For the column parity this is easy. We can just xor the bytes and in the
  139. end filter out the relevant bits. This is pretty nice as it will bring
  140. all cp calculation out of the if loop.
  141. Similarly we can first xor the bytes for the various rows.
  142. This leads to:
  143. Attempt 1
  144. =========
  145. const char parity[256] = {
  146. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  147. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  148. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  149. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  150. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  151. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  152. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  153. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  154. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  155. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  156. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  157. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  158. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  159. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  160. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  161. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
  162. };
  163. void ecc1(const unsigned char *buf, unsigned char *code)
  164. {
  165. int i;
  166. const unsigned char *bp = buf;
  167. unsigned char cur;
  168. unsigned char rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
  169. unsigned char rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15;
  170. unsigned char par;
  171. par = 0;
  172. rp0 = 0; rp1 = 0; rp2 = 0; rp3 = 0;
  173. rp4 = 0; rp5 = 0; rp6 = 0; rp7 = 0;
  174. rp8 = 0; rp9 = 0; rp10 = 0; rp11 = 0;
  175. rp12 = 0; rp13 = 0; rp14 = 0; rp15 = 0;
  176. for (i = 0; i < 256; i++)
  177. {
  178. cur = *bp++;
  179. par ^= cur;
  180. if (i & 0x01) rp1 ^= cur; else rp0 ^= cur;
  181. if (i & 0x02) rp3 ^= cur; else rp2 ^= cur;
  182. if (i & 0x04) rp5 ^= cur; else rp4 ^= cur;
  183. if (i & 0x08) rp7 ^= cur; else rp6 ^= cur;
  184. if (i & 0x10) rp9 ^= cur; else rp8 ^= cur;
  185. if (i & 0x20) rp11 ^= cur; else rp10 ^= cur;
  186. if (i & 0x40) rp13 ^= cur; else rp12 ^= cur;
  187. if (i & 0x80) rp15 ^= cur; else rp14 ^= cur;
  188. }
  189. code[0] =
  190. (parity[rp7] << 7) |
  191. (parity[rp6] << 6) |
  192. (parity[rp5] << 5) |
  193. (parity[rp4] << 4) |
  194. (parity[rp3] << 3) |
  195. (parity[rp2] << 2) |
  196. (parity[rp1] << 1) |
  197. (parity[rp0]);
  198. code[1] =
  199. (parity[rp15] << 7) |
  200. (parity[rp14] << 6) |
  201. (parity[rp13] << 5) |
  202. (parity[rp12] << 4) |
  203. (parity[rp11] << 3) |
  204. (parity[rp10] << 2) |
  205. (parity[rp9] << 1) |
  206. (parity[rp8]);
  207. code[2] =
  208. (parity[par & 0xf0] << 7) |
  209. (parity[par & 0x0f] << 6) |
  210. (parity[par & 0xcc] << 5) |
  211. (parity[par & 0x33] << 4) |
  212. (parity[par & 0xaa] << 3) |
  213. (parity[par & 0x55] << 2);
  214. code[0] = ~code[0];
  215. code[1] = ~code[1];
  216. code[2] = ~code[2];
  217. }
  218. Still pretty straightforward. The last three invert statements are there to
  219. give a checksum of 0xff 0xff 0xff for an empty flash. In an empty flash
  220. all data is 0xff, so the checksum then matches.
  221. I also introduced the parity lookup. I expected this to be the fastest
  222. way to calculate the parity, but I will investigate alternatives later
  223. on.
  224. Analysis 1
  225. ==========
  226. The code works, but is not terribly efficient. On my system it took
  227. almost 4 times as much time as the linux driver code. But hey, if it was
  228. *that* easy this would have been done long before.
  229. No pain. no gain.
  230. Fortunately there is plenty of room for improvement.
  231. In step 1 we moved from bit-wise calculation to byte-wise calculation.
  232. However in C we can also use the unsigned long data type and virtually
  233. every modern microprocessor supports 32 bit operations, so why not try
  234. to write our code in such a way that we process data in 32 bit chunks.
  235. Of course this means some modification as the row parity is byte by
  236. byte. A quick analysis:
  237. for the column parity we use the par variable. When extending to 32 bits
  238. we can in the end easily calculate p0 and p1 from it.
  239. (because par now consists of 4 bytes, contributing to rp1, rp0, rp1, rp0
  240. respectively)
  241. also rp2 and rp3 can be easily retrieved from par as rp3 covers the
  242. first two bytes and rp2 the last two bytes.
  243. Note that of course now the loop is executed only 64 times (256/4).
  244. And note that care must taken wrt byte ordering. The way bytes are
  245. ordered in a long is machine dependent, and might affect us.
  246. Anyway, if there is an issue: this code is developed on x86 (to be
  247. precise: a DELL PC with a D920 Intel CPU)
  248. And of course the performance might depend on alignment, but I expect
  249. that the I/O buffers in the nand driver are aligned properly (and
  250. otherwise that should be fixed to get maximum performance).
  251. Let's give it a try...
  252. Attempt 2
  253. =========
  254. extern const char parity[256];
  255. void ecc2(const unsigned char *buf, unsigned char *code)
  256. {
  257. int i;
  258. const unsigned long *bp = (unsigned long *)buf;
  259. unsigned long cur;
  260. unsigned long rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
  261. unsigned long rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15;
  262. unsigned long par;
  263. par = 0;
  264. rp0 = 0; rp1 = 0; rp2 = 0; rp3 = 0;
  265. rp4 = 0; rp5 = 0; rp6 = 0; rp7 = 0;
  266. rp8 = 0; rp9 = 0; rp10 = 0; rp11 = 0;
  267. rp12 = 0; rp13 = 0; rp14 = 0; rp15 = 0;
  268. for (i = 0; i < 64; i++)
  269. {
  270. cur = *bp++;
  271. par ^= cur;
  272. if (i & 0x01) rp5 ^= cur; else rp4 ^= cur;
  273. if (i & 0x02) rp7 ^= cur; else rp6 ^= cur;
  274. if (i & 0x04) rp9 ^= cur; else rp8 ^= cur;
  275. if (i & 0x08) rp11 ^= cur; else rp10 ^= cur;
  276. if (i & 0x10) rp13 ^= cur; else rp12 ^= cur;
  277. if (i & 0x20) rp15 ^= cur; else rp14 ^= cur;
  278. }
  279. /*
  280. we need to adapt the code generation for the fact that rp vars are now
  281. long; also the column parity calculation needs to be changed.
  282. we'll bring rp4 to 15 back to single byte entities by shifting and
  283. xoring
  284. */
  285. rp4 ^= (rp4 >> 16); rp4 ^= (rp4 >> 8); rp4 &= 0xff;
  286. rp5 ^= (rp5 >> 16); rp5 ^= (rp5 >> 8); rp5 &= 0xff;
  287. rp6 ^= (rp6 >> 16); rp6 ^= (rp6 >> 8); rp6 &= 0xff;
  288. rp7 ^= (rp7 >> 16); rp7 ^= (rp7 >> 8); rp7 &= 0xff;
  289. rp8 ^= (rp8 >> 16); rp8 ^= (rp8 >> 8); rp8 &= 0xff;
  290. rp9 ^= (rp9 >> 16); rp9 ^= (rp9 >> 8); rp9 &= 0xff;
  291. rp10 ^= (rp10 >> 16); rp10 ^= (rp10 >> 8); rp10 &= 0xff;
  292. rp11 ^= (rp11 >> 16); rp11 ^= (rp11 >> 8); rp11 &= 0xff;
  293. rp12 ^= (rp12 >> 16); rp12 ^= (rp12 >> 8); rp12 &= 0xff;
  294. rp13 ^= (rp13 >> 16); rp13 ^= (rp13 >> 8); rp13 &= 0xff;
  295. rp14 ^= (rp14 >> 16); rp14 ^= (rp14 >> 8); rp14 &= 0xff;
  296. rp15 ^= (rp15 >> 16); rp15 ^= (rp15 >> 8); rp15 &= 0xff;
  297. rp3 = (par >> 16); rp3 ^= (rp3 >> 8); rp3 &= 0xff;
  298. rp2 = par & 0xffff; rp2 ^= (rp2 >> 8); rp2 &= 0xff;
  299. par ^= (par >> 16);
  300. rp1 = (par >> 8); rp1 &= 0xff;
  301. rp0 = (par & 0xff);
  302. par ^= (par >> 8); par &= 0xff;
  303. code[0] =
  304. (parity[rp7] << 7) |
  305. (parity[rp6] << 6) |
  306. (parity[rp5] << 5) |
  307. (parity[rp4] << 4) |
  308. (parity[rp3] << 3) |
  309. (parity[rp2] << 2) |
  310. (parity[rp1] << 1) |
  311. (parity[rp0]);
  312. code[1] =
  313. (parity[rp15] << 7) |
  314. (parity[rp14] << 6) |
  315. (parity[rp13] << 5) |
  316. (parity[rp12] << 4) |
  317. (parity[rp11] << 3) |
  318. (parity[rp10] << 2) |
  319. (parity[rp9] << 1) |
  320. (parity[rp8]);
  321. code[2] =
  322. (parity[par & 0xf0] << 7) |
  323. (parity[par & 0x0f] << 6) |
  324. (parity[par & 0xcc] << 5) |
  325. (parity[par & 0x33] << 4) |
  326. (parity[par & 0xaa] << 3) |
  327. (parity[par & 0x55] << 2);
  328. code[0] = ~code[0];
  329. code[1] = ~code[1];
  330. code[2] = ~code[2];
  331. }
  332. The parity array is not shown any more. Note also that for these
  333. examples I kinda deviated from my regular programming style by allowing
  334. multiple statements on a line, not using { } in then and else blocks
  335. with only a single statement and by using operators like ^=
  336. Analysis 2
  337. ==========
  338. The code (of course) works, and hurray: we are a little bit faster than
  339. the linux driver code (about 15%). But wait, don't cheer too quickly.
  340. THere is more to be gained.
  341. If we look at e.g. rp14 and rp15 we see that we either xor our data with
  342. rp14 or with rp15. However we also have par which goes over all data.
  343. This means there is no need to calculate rp14 as it can be calculated from
  344. rp15 through rp14 = par ^ rp15;
  345. (or if desired we can avoid calculating rp15 and calculate it from
  346. rp14). That is why some places refer to inverse parity.
  347. Of course the same thing holds for rp4/5, rp6/7, rp8/9, rp10/11 and rp12/13.
  348. Effectively this means we can eliminate the else clause from the if
  349. statements. Also we can optimise the calculation in the end a little bit
  350. by going from long to byte first. Actually we can even avoid the table
  351. lookups
  352. Attempt 3
  353. =========
  354. Odd replaced:
  355. if (i & 0x01) rp5 ^= cur; else rp4 ^= cur;
  356. if (i & 0x02) rp7 ^= cur; else rp6 ^= cur;
  357. if (i & 0x04) rp9 ^= cur; else rp8 ^= cur;
  358. if (i & 0x08) rp11 ^= cur; else rp10 ^= cur;
  359. if (i & 0x10) rp13 ^= cur; else rp12 ^= cur;
  360. if (i & 0x20) rp15 ^= cur; else rp14 ^= cur;
  361. with
  362. if (i & 0x01) rp5 ^= cur;
  363. if (i & 0x02) rp7 ^= cur;
  364. if (i & 0x04) rp9 ^= cur;
  365. if (i & 0x08) rp11 ^= cur;
  366. if (i & 0x10) rp13 ^= cur;
  367. if (i & 0x20) rp15 ^= cur;
  368. and outside the loop added:
  369. rp4 = par ^ rp5;
  370. rp6 = par ^ rp7;
  371. rp8 = par ^ rp9;
  372. rp10 = par ^ rp11;
  373. rp12 = par ^ rp13;
  374. rp14 = par ^ rp15;
  375. And after that the code takes about 30% more time, although the number of
  376. statements is reduced. This is also reflected in the assembly code.
  377. Analysis 3
  378. ==========
  379. Very weird. Guess it has to do with caching or instruction parallellism
  380. or so. I also tried on an eeePC (Celeron, clocked at 900 Mhz). Interesting
  381. observation was that this one is only 30% slower (according to time)
  382. executing the code as my 3Ghz D920 processor.
  383. Well, it was expected not to be easy so maybe instead move to a
  384. different track: let's move back to the code from attempt2 and do some
  385. loop unrolling. This will eliminate a few if statements. I'll try
  386. different amounts of unrolling to see what works best.
  387. Attempt 4
  388. =========
  389. Unrolled the loop 1, 2, 3 and 4 times.
  390. For 4 the code starts with:
  391. for (i = 0; i < 4; i++)
  392. {
  393. cur = *bp++;
  394. par ^= cur;
  395. rp4 ^= cur;
  396. rp6 ^= cur;
  397. rp8 ^= cur;
  398. rp10 ^= cur;
  399. if (i & 0x1) rp13 ^= cur; else rp12 ^= cur;
  400. if (i & 0x2) rp15 ^= cur; else rp14 ^= cur;
  401. cur = *bp++;
  402. par ^= cur;
  403. rp5 ^= cur;
  404. rp6 ^= cur;
  405. ...
  406. Analysis 4
  407. ==========
  408. Unrolling once gains about 15%
  409. Unrolling twice keeps the gain at about 15%
  410. Unrolling three times gives a gain of 30% compared to attempt 2.
  411. Unrolling four times gives a marginal improvement compared to unrolling
  412. three times.
  413. I decided to proceed with a four time unrolled loop anyway. It was my gut
  414. feeling that in the next steps I would obtain additional gain from it.
  415. The next step was triggered by the fact that par contains the xor of all
  416. bytes and rp4 and rp5 each contain the xor of half of the bytes.
  417. So in effect par = rp4 ^ rp5. But as xor is commutative we can also say
  418. that rp5 = par ^ rp4. So no need to keep both rp4 and rp5 around. We can
  419. eliminate rp5 (or rp4, but I already foresaw another optimisation).
  420. The same holds for rp6/7, rp8/9, rp10/11 rp12/13 and rp14/15.
  421. Attempt 5
  422. =========
  423. Effectively so all odd digit rp assignments in the loop were removed.
  424. This included the else clause of the if statements.
  425. Of course after the loop we need to correct things by adding code like:
  426. rp5 = par ^ rp4;
  427. Also the initial assignments (rp5 = 0; etc) could be removed.
  428. Along the line I also removed the initialisation of rp0/1/2/3.
  429. Analysis 5
  430. ==========
  431. Measurements showed this was a good move. The run-time roughly halved
  432. compared with attempt 4 with 4 times unrolled, and we only require 1/3rd
  433. of the processor time compared to the current code in the linux kernel.
  434. However, still I thought there was more. I didn't like all the if
  435. statements. Why not keep a running parity and only keep the last if
  436. statement. Time for yet another version!
  437. Attempt 6
  438. =========
  439. THe code within the for loop was changed to:
  440. for (i = 0; i < 4; i++)
  441. {
  442. cur = *bp++; tmppar = cur; rp4 ^= cur;
  443. cur = *bp++; tmppar ^= cur; rp6 ^= tmppar;
  444. cur = *bp++; tmppar ^= cur; rp4 ^= cur;
  445. cur = *bp++; tmppar ^= cur; rp8 ^= tmppar;
  446. cur = *bp++; tmppar ^= cur; rp4 ^= cur; rp6 ^= cur;
  447. cur = *bp++; tmppar ^= cur; rp6 ^= cur;
  448. cur = *bp++; tmppar ^= cur; rp4 ^= cur;
  449. cur = *bp++; tmppar ^= cur; rp10 ^= tmppar;
  450. cur = *bp++; tmppar ^= cur; rp4 ^= cur; rp6 ^= cur; rp8 ^= cur;
  451. cur = *bp++; tmppar ^= cur; rp6 ^= cur; rp8 ^= cur;
  452. cur = *bp++; tmppar ^= cur; rp4 ^= cur; rp8 ^= cur;
  453. cur = *bp++; tmppar ^= cur; rp8 ^= cur;
  454. cur = *bp++; tmppar ^= cur; rp4 ^= cur; rp6 ^= cur;
  455. cur = *bp++; tmppar ^= cur; rp6 ^= cur;
  456. cur = *bp++; tmppar ^= cur; rp4 ^= cur;
  457. cur = *bp++; tmppar ^= cur;
  458. par ^= tmppar;
  459. if ((i & 0x1) == 0) rp12 ^= tmppar;
  460. if ((i & 0x2) == 0) rp14 ^= tmppar;
  461. }
  462. As you can see tmppar is used to accumulate the parity within a for
  463. iteration. In the last 3 statements is added to par and, if needed,
  464. to rp12 and rp14.
  465. While making the changes I also found that I could exploit that tmppar
  466. contains the running parity for this iteration. So instead of having:
  467. rp4 ^= cur; rp6 = cur;
  468. I removed the rp6 = cur; statement and did rp6 ^= tmppar; on next
  469. statement. A similar change was done for rp8 and rp10
  470. Analysis 6
  471. ==========
  472. Measuring this code again showed big gain. When executing the original
  473. linux code 1 million times, this took about 1 second on my system.
  474. (using time to measure the performance). After this iteration I was back
  475. to 0.075 sec. Actually I had to decide to start measuring over 10
  476. million iterations in order not to lose too much accuracy. This one
  477. definitely seemed to be the jackpot!
  478. There is a little bit more room for improvement though. There are three
  479. places with statements:
  480. rp4 ^= cur; rp6 ^= cur;
  481. It seems more efficient to also maintain a variable rp4_6 in the while
  482. loop; This eliminates 3 statements per loop. Of course after the loop we
  483. need to correct by adding:
  484. rp4 ^= rp4_6;
  485. rp6 ^= rp4_6
  486. Furthermore there are 4 sequential assignments to rp8. This can be
  487. encoded slightly more efficiently by saving tmppar before those 4 lines
  488. and later do rp8 = rp8 ^ tmppar ^ notrp8;
  489. (where notrp8 is the value of rp8 before those 4 lines).
  490. Again a use of the commutative property of xor.
  491. Time for a new test!
  492. Attempt 7
  493. =========
  494. The new code now looks like:
  495. for (i = 0; i < 4; i++)
  496. {
  497. cur = *bp++; tmppar = cur; rp4 ^= cur;
  498. cur = *bp++; tmppar ^= cur; rp6 ^= tmppar;
  499. cur = *bp++; tmppar ^= cur; rp4 ^= cur;
  500. cur = *bp++; tmppar ^= cur; rp8 ^= tmppar;
  501. cur = *bp++; tmppar ^= cur; rp4_6 ^= cur;
  502. cur = *bp++; tmppar ^= cur; rp6 ^= cur;
  503. cur = *bp++; tmppar ^= cur; rp4 ^= cur;
  504. cur = *bp++; tmppar ^= cur; rp10 ^= tmppar;
  505. notrp8 = tmppar;
  506. cur = *bp++; tmppar ^= cur; rp4_6 ^= cur;
  507. cur = *bp++; tmppar ^= cur; rp6 ^= cur;
  508. cur = *bp++; tmppar ^= cur; rp4 ^= cur;
  509. cur = *bp++; tmppar ^= cur;
  510. rp8 = rp8 ^ tmppar ^ notrp8;
  511. cur = *bp++; tmppar ^= cur; rp4_6 ^= cur;
  512. cur = *bp++; tmppar ^= cur; rp6 ^= cur;
  513. cur = *bp++; tmppar ^= cur; rp4 ^= cur;
  514. cur = *bp++; tmppar ^= cur;
  515. par ^= tmppar;
  516. if ((i & 0x1) == 0) rp12 ^= tmppar;
  517. if ((i & 0x2) == 0) rp14 ^= tmppar;
  518. }
  519. rp4 ^= rp4_6;
  520. rp6 ^= rp4_6;
  521. Not a big change, but every penny counts :-)
  522. Analysis 7
  523. ==========
  524. Actually this made things worse. Not very much, but I don't want to move
  525. into the wrong direction. Maybe something to investigate later. Could
  526. have to do with caching again.
  527. Guess that is what there is to win within the loop. Maybe unrolling one
  528. more time will help. I'll keep the optimisations from 7 for now.
  529. Attempt 8
  530. =========
  531. Unrolled the loop one more time.
  532. Analysis 8
  533. ==========
  534. This makes things worse. Let's stick with attempt 6 and continue from there.
  535. Although it seems that the code within the loop cannot be optimised
  536. further there is still room to optimize the generation of the ecc codes.
  537. We can simply calculate the total parity. If this is 0 then rp4 = rp5
  538. etc. If the parity is 1, then rp4 = !rp5;
  539. But if rp4 = rp5 we do not need rp5 etc. We can just write the even bits
  540. in the result byte and then do something like
  541. code[0] |= (code[0] << 1);
  542. Lets test this.
  543. Attempt 9
  544. =========
  545. Changed the code but again this slightly degrades performance. Tried all
  546. kind of other things, like having dedicated parity arrays to avoid the
  547. shift after parity[rp7] << 7; No gain.
  548. Change the lookup using the parity array by using shift operators (e.g.
  549. replace parity[rp7] << 7 with:
  550. rp7 ^= (rp7 << 4);
  551. rp7 ^= (rp7 << 2);
  552. rp7 ^= (rp7 << 1);
  553. rp7 &= 0x80;
  554. No gain.
  555. The only marginal change was inverting the parity bits, so we can remove
  556. the last three invert statements.
  557. Ah well, pity this does not deliver more. Then again 10 million
  558. iterations using the linux driver code takes between 13 and 13.5
  559. seconds, whereas my code now takes about 0.73 seconds for those 10
  560. million iterations. So basically I've improved the performance by a
  561. factor 18 on my system. Not that bad. Of course on different hardware
  562. you will get different results. No warranties!
  563. But of course there is no such thing as a free lunch. The codesize almost
  564. tripled (from 562 bytes to 1434 bytes). Then again, it is not that much.
  565. Correcting errors
  566. =================
  567. For correcting errors I again used the ST application note as a starter,
  568. but I also peeked at the existing code.
  569. The algorithm itself is pretty straightforward. Just xor the given and
  570. the calculated ecc. If all bytes are 0 there is no problem. If 11 bits
  571. are 1 we have one correctable bit error. If there is 1 bit 1, we have an
  572. error in the given ecc code.
  573. It proved to be fastest to do some table lookups. Performance gain
  574. introduced by this is about a factor 2 on my system when a repair had to
  575. be done, and 1% or so if no repair had to be done.
  576. Code size increased from 330 bytes to 686 bytes for this function.
  577. (gcc 4.2, -O3)
  578. Conclusion
  579. ==========
  580. The gain when calculating the ecc is tremendous. Om my development hardware
  581. a speedup of a factor of 18 for ecc calculation was achieved. On a test on an
  582. embedded system with a MIPS core a factor 7 was obtained.
  583. On a test with a Linksys NSLU2 (ARMv5TE processor) the speedup was a factor
  584. 5 (big endian mode, gcc 4.1.2, -O3)
  585. For correction not much gain could be obtained (as bitflips are rare). Then
  586. again there are also much less cycles spent there.
  587. It seems there is not much more gain possible in this, at least when
  588. programmed in C. Of course it might be possible to squeeze something more
  589. out of it with an assembler program, but due to pipeline behaviour etc
  590. this is very tricky (at least for intel hw).
  591. Author: Frans Meulenbroeks
  592. Copyright (C) 2008 Koninklijke Philips Electronics NV.