2015-11-27 10:27:11 +00:00
|
|
|
/* Copyright 2010 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Function to find maximal matching prefixes of strings. */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_FIND_MATCH_LENGTH_H_
|
|
|
|
#define BROTLI_ENC_FIND_MATCH_LENGTH_H_
|
|
|
|
|
2016-08-23 12:40:33 +00:00
|
|
|
#include <brotli/types.h>
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2022-11-17 13:03:09 +00:00
|
|
|
#include "../common/platform.h"
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Separate implementation for little-endian 64-bit targets, for speed. */
|
2020-03-19 10:57:56 +00:00
|
|
|
#if defined(BROTLI_TZCNT64) && BROTLI_64_BITS && BROTLI_LITTLE_ENDIAN
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE size_t FindMatchLengthWithLimit(const uint8_t* s1,
|
|
|
|
const uint8_t* s2,
|
|
|
|
size_t limit) {
|
2023-01-27 10:16:21 +00:00
|
|
|
const uint8_t *s1_orig = s1;
|
|
|
|
for (; limit >= 8; limit -= 8) {
|
|
|
|
uint64_t x = BROTLI_UNALIGNED_LOAD64LE(s2) ^
|
|
|
|
BROTLI_UNALIGNED_LOAD64LE(s1);
|
|
|
|
s2 += 8;
|
|
|
|
if (x != 0) {
|
2020-03-19 10:57:56 +00:00
|
|
|
size_t matching_bits = (size_t)BROTLI_TZCNT64(x);
|
2023-01-27 10:16:21 +00:00
|
|
|
return (size_t)(s1 - s1_orig) + (matching_bits >> 3);
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
2023-01-27 10:16:21 +00:00
|
|
|
s1 += 8;
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
2023-01-27 10:16:21 +00:00
|
|
|
while (limit && *s1 == *s2) {
|
|
|
|
limit--;
|
|
|
|
++s2;
|
|
|
|
++s1;
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
2023-01-27 10:16:21 +00:00
|
|
|
return (size_t)(s1 - s1_orig);
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
#else
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE size_t FindMatchLengthWithLimit(const uint8_t* s1,
|
|
|
|
const uint8_t* s2,
|
|
|
|
size_t limit) {
|
2016-01-07 15:27:49 +00:00
|
|
|
size_t matched = 0;
|
2013-10-23 11:06:13 +00:00
|
|
|
const uint8_t* s2_limit = s2 + limit;
|
|
|
|
const uint8_t* s2_ptr = s2;
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Find out how long the match is. We loop over the data 32 bits at a
|
|
|
|
time until we find a 32-bit block that doesn't match; then we find
|
|
|
|
the first non-matching bit and use that to calculate the total
|
|
|
|
length of the match. */
|
2013-10-23 11:06:13 +00:00
|
|
|
while (s2_ptr <= s2_limit - 4 &&
|
2017-12-12 13:33:12 +00:00
|
|
|
BrotliUnalignedRead32(s2_ptr) ==
|
|
|
|
BrotliUnalignedRead32(s1 + matched)) {
|
2013-10-23 11:06:13 +00:00
|
|
|
s2_ptr += 4;
|
|
|
|
matched += 4;
|
|
|
|
}
|
|
|
|
while ((s2_ptr < s2_limit) && (s1[matched] == *s2_ptr)) {
|
|
|
|
++s2_ptr;
|
|
|
|
++matched;
|
|
|
|
}
|
|
|
|
return matched;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
#endif /* BROTLI_ENC_FIND_MATCH_LENGTH_H_ */
|