Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apultra_insert_forward_match optimize #20

Open
ghost opened this issue Nov 16, 2023 · 0 comments
Open

apultra_insert_forward_match optimize #20

ghost opened this issue Nov 16, 2023 · 0 comments

Comments

@ghost
Copy link

ghost commented Nov 16, 2023

I think it better code 👍

static void apultra_insert_forward_match(apultra_compressor *pCompressor, const unsigned char *pInWindow, const int i, const int nMatchOffset, const int nStartOffset, const int nEndOffset, const int nArrivalsPerPosition, const int nDepth) {

    unsigned char slots_mask = 0xFF;

   const apultra_arrival *arrival = pCompressor->arrival + ((i - nStartOffset) * nArrivalsPerPosition);
   const int *rle_len = (const int*)pCompressor->intervals /* reuse */;
   apultra_visited* visited = ((apultra_visited*)pCompressor->pos_data) - nStartOffset /* reuse */;
   int j;

   for (j = 0; j < nArrivalsPerPosition; j++) {
       if (!(slots_mask & (1 << j))) {
           if (arrival[j].follows_literal) {
               const int nRepOffset = arrival[j].rep_offset;

               if (nMatchOffset != nRepOffset) {
                   const int nRepPos = arrival[j].rep_pos;

                   if (nRepPos >= nStartOffset &&
                       (nRepPos + 1) < nEndOffset &&
                       visited[nRepPos] != nMatchOffset) {

                       visited[nRepPos] = nMatchOffset;

                       apultra_match* fwd_match = pCompressor->match + ((nRepPos - nStartOffset) << MATCHES_PER_INDEX_SHIFT);

                       if (fwd_match[NMATCHES_PER_INDEX - 1].length == 0) {
                           if (nRepPos >= nMatchOffset) {
                               const unsigned char* pInWindowStart = pInWindow + nRepPos;

                               if (!memcmp(pInWindowStart, pInWindowStart - nMatchOffset, 2)) {
                                   if (nRepOffset) {
                                       const int nLen0 = rle_len[nRepPos - nMatchOffset];
                                       const int nLen1 = rle_len[nRepPos];
                                       const int nMinLen = (nLen0 < nLen1) ? nLen0 : nLen1;

                                       unsigned int nMaxRepLen = nEndOffset - nRepPos;
                                       if (nMaxRepLen > LCP_MAX)
                                           nMaxRepLen = LCP_MAX;

                                       const unsigned char* pInWindowMax = pInWindowStart + nMaxRepLen;
                                       const unsigned char* pInWindowAtRepOffset = pInWindowStart + nMinLen;

                                       if (pInWindowAtRepOffset > pInWindowMax)
                                           pInWindowAtRepOffset = pInWindowMax;

                                       while ((pInWindowAtRepOffset + 8) < pInWindowMax && !memcmp(pInWindowAtRepOffset, pInWindowAtRepOffset - nMatchOffset, 8))
                                           pInWindowAtRepOffset += 8;
                                       while ((pInWindowAtRepOffset + 4) < pInWindowMax && !memcmp(pInWindowAtRepOffset, pInWindowAtRepOffset - nMatchOffset, 4))
                                           pInWindowAtRepOffset += 4;
                                       while (pInWindowAtRepOffset < pInWindowMax && pInWindowAtRepOffset[0] == pInWindowAtRepOffset[-nMatchOffset])
                                           pInWindowAtRepOffset++;

                                       const unsigned int nCurRepLen = (const unsigned int)(pInWindowAtRepOffset - pInWindowStart);

                                       unsigned short* fwd_depth = pCompressor->match_depth + ((nRepPos - nStartOffset) << MATCHES_PER_INDEX_SHIFT);
                                       int r;

                                       for (r = 0; fwd_match[r].length; r++) {
                                           if (fwd_match[r].offset == nMatchOffset && (fwd_depth[r] & 0x3fff) == 0) {
                                               if (fwd_match[r].length < nCurRepLen) {
                                                   fwd_match[r].length = nCurRepLen;
                                                   fwd_depth[r] = 0;
                                               }
                                               break;
                                           }
                                       }

                                       if (fwd_match[r].length == 0) {
                                           fwd_match[r].length = nCurRepLen;
                                           fwd_match[r].offset = nMatchOffset;
                                           fwd_depth[r] = 0;

                                           if (nDepth < 9)
                                               apultra_insert_forward_match(pCompressor, pInWindow, nRepPos, nMatchOffset, nStartOffset, nEndOffset, nArrivalsPerPosition, nDepth + 1);
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
       slots_mask >>= 1;
   }
}```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants