straightforward implementation. max length buffer 65534. Wikipedia shows optimizations.
| 16 | // Wikipedia shows optimizations. |
| 17 | // |
| 18 | uint16_t fletcher16(uint8_t *data, uint16_t length, uint32_t s1, uint32_t s2) |
| 19 | { |
| 20 | uint32_t _s1 = s1; |
| 21 | uint32_t _s2 = s2; |
| 22 | for (uint16_t i = 0; i < length;) |
| 23 | { |
| 24 | // if _s2 is halfway it is time to do modulo |
| 25 | while ((i < length) && (_s2 < 2147483648ULL)) // MAGIC NR |
| 26 | { |
| 27 | _s1 += data[i++]; |
| 28 | _s2 += _s1; |
| 29 | } |
| 30 | // this optimization does not work due to the above "32-bit" loop. |
| 31 | // for all three functions. |
| 32 | // _s1 = (_s1 & 255) + (_s1 >> 8); |
| 33 | _s1 %= FLETCHER_16; |
| 34 | _s2 %= FLETCHER_16; |
| 35 | } |
| 36 | return (_s2 << 8) | _s1; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | uint32_t fletcher32(uint16_t *data, uint16_t length, uint32_t s1, uint32_t s2) |