Optimized version S1 grows linear S2 grows quadratic only do modulo when S2 reaches halfway uint32_t and at the end of the loop. returns current Adler value
| 70 | // and at the end of the loop. |
| 71 | // returns current Adler value |
| 72 | uint32_t Adler32::addFast(uint8_t * array, uint16_t length) |
| 73 | { |
| 74 | _count += length; |
| 75 | uint32_t s1 = _s1; |
| 76 | uint32_t s2 = _s2; |
| 77 | for (uint16_t i = 0; i < length;) |
| 78 | { |
| 79 | // if S2 is halfway it is time to do modulo |
| 80 | while ((i < length) && (s2 < 2147483648ULL)) |
| 81 | { |
| 82 | s1 += array[i++]; |
| 83 | s2 += s1; |
| 84 | } |
| 85 | if (s2 >= ADLER32_MOD_PRIME) |
| 86 | { |
| 87 | s2 %= ADLER32_MOD_PRIME; |
| 88 | if (s1 >= ADLER32_MOD_PRIME) s1 %= ADLER32_MOD_PRIME; |
| 89 | } |
| 90 | } |
| 91 | _s1 = s1; |
| 92 | _s2 = s2; |
| 93 | return (s2 << 16) | s1; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | uint32_t Adler32::getAdler() |