+/*
+ * Find the longest repeat offset match.
+ *
+ * If no match of at least LZX_MIN_MATCH_LEN bytes is found, then return 0.
+ *
+ * If a match of at least LZX_MIN_MATCH_LEN bytes is found, then return its
+ * length and set *slot_ret to the index of its offset in @queue.
+ */
+static inline u32
+lzx_repsearch(const u8 * const strptr, const u32 bytes_remaining,
+ const struct lzx_lru_queue *queue, unsigned *slot_ret)
+{
+ u32 best_len = 0;
+
+ BUILD_BUG_ON(LZX_MIN_MATCH_LEN != 2);
+ if (likely(bytes_remaining >= 2)) {
+ const u32 max_len = min(LZX_MAX_MATCH_LEN, bytes_remaining);
+ const u16 str = *(const u16 *)strptr;
+ for (unsigned i = 0; i < LZX_NUM_RECENT_OFFSETS; i++) {
+ const u8 * const matchptr = strptr - queue->R[i];
+
+ /* Check the first two bytes. If they match, then
+ * extend the match to its full length. */
+ if (*(const u16 *)matchptr == str) {
+ const u32 len = lz_extend(strptr, matchptr,
+ 2, max_len);
+ if (len > best_len) {
+ best_len = len;
+ *slot_ret = i;
+ }
+ }
+ }
+ }
+ return best_len;
+}
+