JenkHash 0.2.1

Bob Jenkins hash functions for Rust with a digest-compatible API.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Implementation of the Lookup3 hash algorithm.
//!
//! Lookup3 is an improved version of Bob Jenkins' hash function, designed to be faster
//! and provide better avalanche properties than Lookup2. It processes input data and
//! produces a 32-bit hash value. Lookup3 is optimized for modern processors and is
//! widely used in various software systems.
//!
//! ## Algorithm Overview
//!
//! The Lookup3 algorithm uses an internal state of three 32-bit values that are mixed
//! and updated as data is processed. The algorithm features two main phases:
//!
//! 1. The mixing phase (`mix` function): Processes chunks of input data through a series
//!    of arithmetic and bitwise operations to achieve good avalanche properties.
//! 2. The finalization phase (`final` function): Combines the internal state to produce
//!    the final hash values.
//!
//! The algorithm can process both word-aligned data (using `hash_word`) and byte-oriented
//! data (using `hash_little` and `hash_big` for different endianness). It provides excellent
//! distribution properties and is suitable for use in hash table implementations and other
//! applications requiring fast, well-distributed hash values.

use crate::extensions::ext_slice_u8::ExtSliceU8;
use crate::utils::rot32;

/// A struct providing access to the Lookup3 hash algorithm functions.
///
/// Lookup3 is an improved version of Bob Jenkins' hash function, designed to be faster
/// and provide better avalanche properties than Lookup2. It processes input data and
/// produces 32-bit hash values with excellent distribution properties.
///
/// This struct provides access to different variants of the Lookup3 algorithm optimized
/// for different use cases and platform architectures.
///
/// # Examples
///
/// Basic usage with default seed:
///
/// ```
/// use JenkHash::Lookup3;
///
/// let data = b"hello world";
/// let (hash, _) = Lookup3::hash_little(data, 0, None);
/// println!("Hash: 0x{:08x}", hash);
/// ```
///
/// Using a custom seed:
///
/// ```
/// use JenkHash::Lookup3;
///
/// let data = b"hello world";
/// let (hash, _) = Lookup3::hash_little(data, 0x12345678, None);
/// println!("Hash with seed: 0x{:08x}", hash);
/// ```
///
/// Getting both primary and secondary hash values:
///
/// ```
/// use JenkHash::Lookup3;
///
/// let data = b"hello world";
/// let (primary_hash, secondary_hash) = Lookup3::hash_little(data, 0, Some(0x87654321));
/// println!("Primary hash: 0x{:08x}", primary_hash);
/// if let Some(secondary) = secondary_hash {
///     println!("Secondary hash: 0x{:08x}", secondary);
/// }
/// ```
pub struct Lookup3 {}

impl Lookup3 {
    /// Internal mixing function for the Lookup3 algorithm.
    ///
    /// This function performs the core mixing operations that provide the algorithm's
    /// avalanche properties. It takes three 32-bit state values and processes them
    /// through a series of arithmetic and bitwise rotation operations. The mixing
    /// function ensures that small changes in input lead to significant changes in
    /// the output hash value.
    ///
    /// # Parameters
    ///
    /// * `states` - An array of three 32-bit values representing the internal state
    ///
    /// # Returns
    ///
    /// An array of three 32-bit values representing the mixed internal state
    #[inline]
    #[rustfmt::skip]
    const fn mix(mut states: [u32; 3]) -> [u32; 3] {
        states[0] = states[0].wrapping_sub(states[2]); states[0] ^= rot32(states[2], 4); states[2] = states[2].wrapping_add(states[1]);
        states[1] = states[1].wrapping_sub(states[0]); states[1] ^= rot32(states[0], 6); states[0] = states[0].wrapping_add(states[2]);
        states[2] = states[2].wrapping_sub(states[1]); states[2] ^= rot32(states[1], 8); states[1] = states[1].wrapping_add(states[0]);
        states[0] = states[0].wrapping_sub(states[2]); states[0] ^= rot32(states[2],16); states[2] = states[2].wrapping_add(states[1]);
        states[1] = states[1].wrapping_sub(states[0]); states[1] ^= rot32(states[0],19); states[0] = states[0].wrapping_add(states[2]);
        states[2] = states[2].wrapping_sub(states[1]); states[2] ^= rot32(states[1], 4); states[1] = states[1].wrapping_add(states[0]);

        states
    }

    /// Internal finalization function for the Lookup3 algorithm.
    ///
    /// This function performs the final mixing operations to produce the output hash values.
    /// It combines the three internal state values through a series of arithmetic and
    /// bitwise rotation operations. The finalization function ensures proper mixing of
    /// all state values before producing the final hash result.
    ///
    /// # Parameters
    ///
    /// * `states` - An array of three 32-bit values representing the internal state
    ///
    /// # Returns
    ///
    /// An array of three 32-bit values with the final mixed state
    #[inline]
    #[rustfmt::skip]
    const fn r#final(mut states: [u32; 3]) -> [u32; 3] {
        states[2] ^= states[1]; states[2] = states[2].wrapping_sub(rot32(states[1],14));
        states[0] ^= states[2]; states[0] = states[0].wrapping_sub(rot32(states[2],11));
        states[1] ^= states[0]; states[1] = states[1].wrapping_sub(rot32(states[0],25));
        states[2] ^= states[1]; states[2] = states[2].wrapping_sub(rot32(states[1],16));
        states[0] ^= states[2]; states[0] = states[0].wrapping_sub(rot32(states[2],04));
        states[1] ^= states[0]; states[1] = states[1].wrapping_sub(rot32(states[0],14));
        states[2] ^= states[1]; states[2] = states[2].wrapping_sub(rot32(states[1],24));

        states
    }

    /// Computes the Lookup3 hash of an array of 32-bit words.
    ///
    /// This function processes word-aligned data for optimal performance. It implements
    /// a variant of the algorithm that works directly with 32-bit values rather than
    /// individual bytes. This approach can provide better performance when the input
    /// data is already aligned as 32-bit words.
    ///
    /// # Parameters
    ///
    /// * `data` - A slice of 32-bit words to hash
    /// * `seed1` - The primary seed value for the hash computation
    /// * `seed2` - An optional secondary seed value; if provided, a secondary hash value
    ///   is also returned
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// * The primary 32-bit hash value
    /// * An optional secondary 32-bit hash value (present if `seed2` was provided)
    pub fn hash_word(mut data: &[u32], seed1: u32, seed2: Option<u32>) -> (u32, Option<u32>) {
        let starting_length = data.len() as u32;
        let initial_state = 0xdeadbeef + ((starting_length) << 2) + seed1;
        let mut states = [initial_state, initial_state, initial_state];
        if let Some(seed) = seed2 {
            states[2] = states[2].wrapping_add(seed);
        }

        while data.len() > 3 {
            states[0] = states[0].wrapping_add(data[0]);
            states[1] = states[1].wrapping_add(data[1]);
            states[2] = states[2].wrapping_add(data[2]);
            states = Self::mix(states);
            data = &data[3..];
        }

        if data.len() == 3 {
            states[2] = states[2].wrapping_add(data[2])
        }
        if (2..=3).contains(&data.len()) {
            states[1] = states[1].wrapping_add(data[1])
        }
        if (1..=3).contains(&data.len()) {
            states[2] = states[2].wrapping_add(data[0]);
            states = Self::r#final(states);
        }

        (states[2], seed2.map(|_| states[1]))
    }

    /// Computes the Lookup3 hash of a byte slice using little-endian interpretation.
    ///
    /// This function implements the little-endian optimized variant of the Lookup3 algorithm.
    /// It processes byte-oriented input data and produces 32-bit hash values. The function
    /// interprets multi-byte values in little-endian format, making it optimal for
    /// little-endian architectures while maintaining deterministic results on all platforms.
    ///
    /// This variant is optimized for performance on little-endian architectures.
    ///
    /// # Parameters
    ///
    /// * `data` - A byte slice to hash
    /// * `seed1` - The primary seed value for the hash computation
    /// * `seed2` - An optional secondary seed value; if provided, a secondary hash value
    ///   is also returned
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// * The primary 32-bit hash value
    /// * An optional secondary 32-bit hash value (present if `seed2` was provided)
    ///
    /// # Examples
    ///
    /// ```
    /// use JenkHash::Lookup3;
    ///
    /// let data = b"hello world";
    /// let (hash, _) = Lookup3::hash_little(data, 0, None);
    /// println!("Hash: 0x{:08x}", hash);
    /// ```
    ///
    /// With a seed value:
    ///
    /// ```
    /// use JenkHash::Lookup3;
    ///
    /// let data = b"hello world";
    /// let (hash, _) = Lookup3::hash_little(data, 0x12345678, None);
    /// println!("Hash with seed: 0x{:08x}", hash);
    /// ```
    #[cfg(target_endian = "little")] #[rustfmt::skip]
    pub fn hash_little(mut data: &[u8], seed1: u32, seed2: Option<u32>) -> (u32, Option<u32>) {
        let initial_state = 0xdeadbeef + (data.len() as u32) + seed1;
        let mut states = [initial_state, initial_state, initial_state];
        if let Some(seed2) = seed2 {
            states[2] = states[2].wrapping_add(seed2)
        }

        while data.len() > 12 {
            states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le());
            states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_le());
            states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_le());
            states = Self::mix(states);
            data = &data[4 * 3..];
        }

        match data.len() {
            12 => { states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_le());                 states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_le()); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le()); }
            11 => { states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_le() & 0xffffff); states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_le()); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le()); }
            10 => { states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_le() & 0xffff);   states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_le()); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le()); }
            09 => { states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_le() & 0xff);     states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_le()); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le()); }
            08 => { states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_le());                 states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le()); }
            07 => { states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_le() & 0xffffff); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le()); }
            06 => { states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_le() & 0xffff);   states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le()); }
            05 => { states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_le() & 0xff);     states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le()); }
            04 => { states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le());                 }
            03 => { states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le() & 0xffffff); }
            02 => { states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le() & 0xffff);   }
            01 => { states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_le() & 0xff);     }
            0 => return (states[2], seed2.map(|_| states[1])),
            _ => unreachable!(),
        }

        states = Self::r#final(states);

        (states[2], seed2.map(|_| states[1]))
    }
    
    /// Computes the Lookup3 hash of a byte slice using little-endian interpretation (fallback for non-little-endian platforms).
    ///
    /// This function implements the little-endian optimized variant of the Lookup3 algorithm
    /// for platforms that are not little-endian. It processes byte-oriented input data and
    /// produces 32-bit hash values, maintaining the same deterministic results as the
    /// little-endian version by explicitly handling byte order during processing.
    ///
    /// # Parameters
    ///
    /// * `data` - A byte slice to hash
    /// * `seed1` - The primary seed value for the hash computation
    /// * `seed2` - An optional secondary seed value; if provided, a secondary hash value
    ///   is also returned
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// * The primary 32-bit hash value
    /// * An optional secondary 32-bit hash value (present if `seed2` was provided)
    #[cfg(not(target_endian = "little"))] #[rustfmt::skip]
    pub fn hash_little(mut data: &[u8], seed1: u32, seed2: Option<u32>) -> (u32, Option<u32>) {
        let initial_state = 0xdeadbeef + (data.len() as u32) + seed1;
        let mut states = [initial_state, initial_state, initial_state];
        if let Some(seed2) = seed2 {
            states[2] = states[2].wrapping_add(seed2)
        }

        while data.len() > 12 {
            states[0] = states[0] .wrapping_add(data[0] as u32);
            states[0] = states[0] .wrapping_add((data[1] as u32) << 8);
            states[0] = states[0] .wrapping_add((data[2] as u32) << 16);
            states[0] = states[0] .wrapping_add((data[3] as u32) << 24);
            states[1] = states[1] .wrapping_add(data[4] as u32);
            states[1] = states[1] .wrapping_add((data[5] as u32) << 8);
            states[1] = states[1] .wrapping_add((data[6] as u32) << 16);
            states[1] = states[1] .wrapping_add((data[7] as u32) << 24);
            states[2] = states[2] .wrapping_add(data[8] as u32);
            states[2] = states[2] .wrapping_add((data[9] as u32) << 8);
            states[2] = states[2] .wrapping_add((data[10] as u32) << 16);
            states[2] = states[2] .wrapping_add((data[11] as u32) << 24);
            states = Self::mix(states);
            data = &data[4 * 3..];
        }

        if data.len() == 12  { states[2] = states[2].wrapping_add((data[11] as u32) << 24); }
        if (11..=12).contains(&data.len()) { states[2] = states[2].wrapping_add((data[10] as u32) << 16); }
        if (10..=12).contains(&data.len()) { states[2] = states[2].wrapping_add((data[9] as u32) << 8); }
        if (09..=12).contains(&data.len()) { states[2] = states[2].wrapping_add(data[8] as u32); }
        if (08..=12).contains(&data.len()) { states[1] = states[1].wrapping_add((data[7] as u32) << 24); }
        if (07..=12).contains(&data.len()) { states[1] = states[1].wrapping_add((data[6] as u32) << 16); }
        if (06..=12).contains(&data.len()) { states[1] = states[1].wrapping_add((data[5] as u32) << 8); }
        if (05..=12).contains(&data.len()) { states[1] = states[1].wrapping_add(data[4] as u32); }
        if (04..=12).contains(&data.len()) { states[0] = states[0].wrapping_add((data[3] as u32) << 24); }
        if (03..=12).contains(&data.len()) { states[0] = states[0].wrapping_add((data[2] as u32) << 16); }
        if (02..=12).contains(&data.len()) { states[0] = states[0].wrapping_add((data[1] as u32) << 8); }
        if (01..=12).contains(&data.len()) { states[0] = states[0].wrapping_add(data[0] as u32); }
        if data.len() == 00  { return (states[2], seed2.map(|_| states[1])); }

        states = Self::r#final(states);

        (states[2], seed2.map(|_| states[1]))
    }

    /// Computes the Lookup3 hash of a byte slice using big-endian interpretation.
    ///
    /// This function implements the big-endian variant of the Lookup3 algorithm.
    /// It processes byte-oriented input data and produces 32-bit hash values. The function
    /// interprets multi-byte values in big-endian format, making it optimal for
    /// big-endian architectures. This function is primarily for compatibility with
    /// big-endian platforms.
    ///
    /// The original C implementation did not include a `hashbig2` function, so this implementation assumes the same process as `hashlittle2`.
    ///
    /// **Note:** This function is provided for completeness and big-endian compatibility.
    /// However, it has limited testing on actual big-endian platforms.
    ///
    /// # Parameters
    ///
    /// * `data` - A byte slice to hash
    /// * `seed1` - The primary seed value for the hash computation
    /// * `seed2` - An optional secondary seed value; if provided, a secondary hash value
    ///   is also returned
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// * The primary 32-bit hash value
    /// * An optional secondary 32-bit hash value (present if `seed2` was provided)
    #[cfg(target_endian = "big")] #[rustfmt::skip]
    fn hash_big(mut data: &[u8], seed1: u32, seed2: Option<u32>) -> (u32, Option<u32>) {
        let initial_state = 0xdeadbeef + (data.len() as u32) + seed1;
        let mut states = [initial_state, initial_state, initial_state];
        if let Some(seed2) = seed2 {
            states[2] = states[2].wrapping_add(seed2)
        }

        while data.len() > 12 {
            states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be());
            states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_be());
            states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_be());
            states = Self::mix(states);
            data = &data[4 * 3..];
        }

        match data.len() {
            12 => { states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_be());              states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_be()); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be()); }
            11 => { states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_be() & 0xffffff00); states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_be()); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be()); }
            10 => { states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_be() & 0xffff0000); states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_be()); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be()); }
            09 => { states[2] = states[2].wrapping_add(data[4 * 2..].read_u32_be() & 0xff000000); states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_be()); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be()); }
            08 => { states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_be());              states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be()); }
            07 => { states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_be() & 0xffffff00); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be()); }
            06 => { states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_be() & 0xffff0000); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be()); }
            05 => { states[1] = states[1].wrapping_add(data[4 * 1..].read_u32_be() & 0xff000000); states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be()); }
            04 => { states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be());              }
            03 => { states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be() & 0xffffff00); }
            02 => { states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be() & 0xffff0000); }
            01 => { states[0] = states[0].wrapping_add(data[4 * 0..].read_u32_be() & 0xff000000); }
            00 => return (states[2], seed2.map(|_| states[1])),
            _ => unreachable!(),
        }

        states = Self::r#final(states);

        (states[2], seed2.map(|_| states[1]))
    }
    
    /// Computes the Lookup3 hash of a byte slice using big-endian interpretation (fallback for non-big-endian platforms).
    ///
    /// This function implements the big-endian variant of the Lookup3 algorithm
    /// for platforms that are not big-endian. It processes byte-oriented input data and
    /// produces 32-bit hash values, maintaining the same deterministic results as the
    /// big-endian version by explicitly handling byte order during processing.
    ///
    /// The original C implementation did not include a `hashbig2` function, so this implementation assumes the same process as `hashlittle2`.
    ///
    /// **Note:** This function is provided for completeness and big-endian compatibility.
    /// However, it has limited testing on actual big-endian platforms.
    ///
    /// # Parameters
    ///
    /// * `data` - A byte slice to hash
    /// * `seed1` - The primary seed value for the hash computation
    /// * `seed2` - An optional secondary seed value; if provided, a secondary hash value
    ///   is also returned
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// * The primary 32-bit hash value
    /// * An optional secondary 32-bit hash value (present if `seed2` was provided)
    #[cfg(not(target_endian = "big"))] #[rustfmt::skip]
    fn hash_big(mut data: &[u8], seed1: u32, seed2: Option<u32>) -> (u32, Option<u32>) {
        let initial_state = 0xdeadbeef + (data.len() as u32) + seed1;
        let mut states = [initial_state, initial_state, initial_state];
        if let Some(seed2) = seed2 {
            states[2] = states[2].wrapping_add(seed2)
        }

        while data.len() > 12 {
            states[0] = states[0].wrapping_add((data[00] as u32) << 24);
            states[0] = states[0].wrapping_add((data[01] as u32) << 16);
            states[0] = states[0].wrapping_add((data[02] as u32) << 08);
            states[0] = states[0].wrapping_add((data[03] as u32));
            states[1] = states[0].wrapping_add((data[04] as u32) << 24);
            states[1] = states[0].wrapping_add((data[05] as u32) << 16);
            states[1] = states[0].wrapping_add((data[06] as u32) << 08);
            states[1] = states[0].wrapping_add((data[07] as u32));
            states[2] = states[0].wrapping_add((data[08] as u32) << 24);
            states[2] = states[0].wrapping_add((data[09] as u32) << 16);
            states[2] = states[0].wrapping_add((data[10] as u32) << 08);
            states[2] = states[0].wrapping_add((data[11] as u32));
            states = Self::mix(states);
            data = &data[4 * 3..];
        }

        if data.len() == 12 { states[2] = states[2].wrapping_add((data[11] as u32)); }
        if (11..=12).contains(&data.len()) { states[2] = states[2].wrapping_add((data[10] as u32) << 08); }
        if (10..=12).contains(&data.len()) { states[2] = states[2].wrapping_add((data[09] as u32) << 16); }
        if (09..=12).contains(&data.len()) { states[2] = states[2].wrapping_add((data[08] as u32) << 24); }
        if (08..=12).contains(&data.len()) { states[1] = states[1].wrapping_add((data[07] as u32)); }
        if (07..=12).contains(&data.len()) { states[1] = states[1].wrapping_add((data[06] as u32) << 08); }
        if (06..=12).contains(&data.len()) { states[1] = states[1].wrapping_add((data[05] as u32) << 16); }
        if (05..=12).contains(&data.len()) { states[1] = states[1].wrapping_add((data[04] as u32) << 24); }
        if (04..=12).contains(&data.len()) { states[0] = states[0].wrapping_add((data[03] as u32)); }
        if (03..=12).contains(&data.len()) { states[0] = states[0].wrapping_add((data[02] as u32) << 08); }
        if (02..=12).contains(&data.len()) { states[0] = states[0].wrapping_add((data[01] as u32) << 16); }
        if (01..=12).contains(&data.len()) { states[0] = states[0].wrapping_add((data[00] as u32) << 24); }
        if data.len() == 00 { return (states[2], seed2.map(|_| states[1])); }

        states = Self::r#final(states);

        (states[2], seed2.map(|_| states[1]))
    }
}

#[cfg(test)]
mod tests {
    use crate::Lookup3;

    #[cfg(target_endian = "little")] #[test]
    fn test_hash_little() {
        let str = b"hello world";
        let (hash, _) = Lookup3::hash_little(str, 0, None);
        assert_eq!(hash, 0x4AA94E65)
    }
    #[cfg(not(target_endian = "little"))]#[test]
    fn test_hash_little() {
        let str = b"hello world";
        let (hash, _) = Lookup3::hash_little(str, 0, None);
        assert_eq!(hash, 0x4AA94E65)
    }
    #[test]
    #[ignore = "Don't have a way to properly test this hash method. Don't have any big-endian machines"]
    #[cfg(target_endian = "big")]#[test]
    fn test_hash_big() {
        let str = b"hello world";
        let (hash, _) = Lookup3::hash_big(str, 0, None);
        assert_eq!(hash, 0xF1DFDD63)
    }
    #[test]
    #[ignore = "Don't have a way to properly test this hash method. Don't have any big-endian machines"]
    #[cfg(not(target_endian = "big"))]#[test]
    fn test_hash_big() {
        let str = b"hello world";
        let (hash, _) = Lookup3::hash_big(str, 0, None);
        assert_eq!(hash, 0xC7CE1547)
    }
}