Coverage Report

Created: 2024-10-16 07:58

/rust/registry/src/index.crates.io-6f17d22bba15001f/adler-1.0.2/src/algo.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::Adler32;
2
use std::ops::{AddAssign, MulAssign, RemAssign};
3
4
impl Adler32 {
5
0
    pub(crate) fn compute(&mut self, bytes: &[u8]) {
6
0
        // The basic algorithm is, for every byte:
7
0
        //   a = (a + byte) % MOD
8
0
        //   b = (b + a) % MOD
9
0
        // where MOD = 65521.
10
0
        //
11
0
        // For efficiency, we can defer the `% MOD` operations as long as neither a nor b overflows:
12
0
        // - Between calls to `write`, we ensure that a and b are always in range 0..MOD.
13
0
        // - We use 32-bit arithmetic in this function.
14
0
        // - Therefore, a and b must not increase by more than 2^32-MOD without performing a `% MOD`
15
0
        //   operation.
16
0
        //
17
0
        // According to Wikipedia, b is calculated as follows for non-incremental checksumming:
18
0
        //   b = n×D1 + (n−1)×D2 + (n−2)×D3 + ... + Dn + n*1 (mod 65521)
19
0
        // Where n is the number of bytes and Di is the i-th Byte. We need to change this to account
20
0
        // for the previous values of a and b, as well as treat every input Byte as being 255:
21
0
        //   b_inc = n×255 + (n-1)×255 + ... + 255 + n*65520
22
0
        // Or in other words:
23
0
        //   b_inc = n*65520 + n(n+1)/2*255
24
0
        // The max chunk size is thus the largest value of n so that b_inc <= 2^32-65521.
25
0
        //   2^32-65521 = n*65520 + n(n+1)/2*255
26
0
        // Plugging this into an equation solver since I can't math gives n = 5552.18..., so 5552.
27
0
        //
28
0
        // On top of the optimization outlined above, the algorithm can also be parallelized with a
29
0
        // bit more work:
30
0
        //
31
0
        // Note that b is a linear combination of a vector of input bytes (D1, ..., Dn).
32
0
        //
33
0
        // If we fix some value k<N and rewrite indices 1, ..., N as
34
0
        //
35
0
        //   1_1, 1_2, ..., 1_k, 2_1, ..., 2_k, ..., (N/k)_k,
36
0
        //
37
0
        // then we can express a and b in terms of sums of smaller sequences kb and ka:
38
0
        //
39
0
        //   ka(j) := D1_j + D2_j + ... + D(N/k)_j where j <= k
40
0
        //   kb(j) := (N/k)*D1_j + (N/k-1)*D2_j + ... + D(N/k)_j where j <= k
41
0
        //
42
0
        //  a = ka(1) + ka(2) + ... + ka(k) + 1
43
0
        //  b = k*(kb(1) + kb(2) + ... + kb(k)) - 1*ka(2) - ...  - (k-1)*ka(k) + N
44
0
        //
45
0
        // We use this insight to unroll the main loop and process k=4 bytes at a time.
46
0
        // The resulting code is highly amenable to SIMD acceleration, although the immediate speedups
47
0
        // stem from increased pipeline parallelism rather than auto-vectorization.
48
0
        //
49
0
        // This technique is described in-depth (here:)[https://software.intel.com/content/www/us/\
50
0
        // en/develop/articles/fast-computation-of-fletcher-checksums.html]
51
0
52
0
        const MOD: u32 = 65521;
53
0
        const CHUNK_SIZE: usize = 5552 * 4;
54
0
55
0
        let mut a = u32::from(self.a);
56
0
        let mut b = u32::from(self.b);
57
0
        let mut a_vec = U32X4([0; 4]);
58
0
        let mut b_vec = a_vec;
59
0
60
0
        let (bytes, remainder) = bytes.split_at(bytes.len() - bytes.len() % 4);
61
0
62
0
        // iterate over 4 bytes at a time
63
0
        let chunk_iter = bytes.chunks_exact(CHUNK_SIZE);
64
0
        let remainder_chunk = chunk_iter.remainder();
65
0
        for chunk in chunk_iter {
66
0
            for byte_vec in chunk.chunks_exact(4) {
67
0
                let val = U32X4::from(byte_vec);
68
0
                a_vec += val;
69
0
                b_vec += a_vec;
70
0
            }
71
0
            b += CHUNK_SIZE as u32 * a;
72
0
            a_vec %= MOD;
73
0
            b_vec %= MOD;
74
0
            b %= MOD;
75
        }
76
        // special-case the final chunk because it may be shorter than the rest
77
0
        for byte_vec in remainder_chunk.chunks_exact(4) {
78
0
            let val = U32X4::from(byte_vec);
79
0
            a_vec += val;
80
0
            b_vec += a_vec;
81
0
        }
82
0
        b += remainder_chunk.len() as u32 * a;
83
0
        a_vec %= MOD;
84
0
        b_vec %= MOD;
85
0
        b %= MOD;
86
0
87
0
        // combine the sub-sum results into the main sum
88
0
        b_vec *= 4;
89
0
        b_vec.0[1] += MOD - a_vec.0[1];
90
0
        b_vec.0[2] += (MOD - a_vec.0[2]) * 2;
91
0
        b_vec.0[3] += (MOD - a_vec.0[3]) * 3;
92
0
        for &av in a_vec.0.iter() {
93
0
            a += av;
94
0
        }
95
0
        for &bv in b_vec.0.iter() {
96
0
            b += bv;
97
0
        }
98
99
        // iterate over the remaining few bytes in serial
100
0
        for &byte in remainder.iter() {
101
0
            a += u32::from(byte);
102
0
            b += a;
103
0
        }
104
105
0
        self.a = (a % MOD) as u16;
106
0
        self.b = (b % MOD) as u16;
107
0
    }
108
}
109
110
#[derive(Copy, Clone)]
111
struct U32X4([u32; 4]);
112
113
impl U32X4 {
114
0
    fn from(bytes: &[u8]) -> Self {
115
0
        U32X4([
116
0
            u32::from(bytes[0]),
117
0
            u32::from(bytes[1]),
118
0
            u32::from(bytes[2]),
119
0
            u32::from(bytes[3]),
120
0
        ])
121
0
    }
122
}
123
124
impl AddAssign<Self> for U32X4 {
125
0
    fn add_assign(&mut self, other: Self) {
126
0
        for (s, o) in self.0.iter_mut().zip(other.0.iter()) {
127
0
            *s += o;
128
0
        }
129
0
    }
130
}
131
132
impl RemAssign<u32> for U32X4 {
133
0
    fn rem_assign(&mut self, quotient: u32) {
134
0
        for s in self.0.iter_mut() {
135
0
            *s %= quotient;
136
0
        }
137
0
    }
138
}
139
140
impl MulAssign<u32> for U32X4 {
141
0
    fn mul_assign(&mut self, rhs: u32) {
142
0
        for s in self.0.iter_mut() {
143
0
            *s *= rhs;
144
0
        }
145
0
    }
146
}