Skip to content

Commit

Permalink
Hoist the static bounds check out of the combined if check.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 639054702
  • Loading branch information
Brotli authored and copybara-github committed May 31, 2024
1 parent fe754f3 commit a528bce
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
12 changes: 8 additions & 4 deletions c/enc/hash_longest_match64_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
}
prev_ix &= ring_buffer_mask;

if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask) {
break;
}
if (prev_ix + best_len > ring_buffer_mask ||
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
continue;
}
Expand Down Expand Up @@ -239,8 +241,10 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
break;
}
prev_ix &= ring_buffer_mask;
if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask) {
break;
}
if (prev_ix + best_len > ring_buffer_mask ||
/* compare 4 bytes ending at best_len + 1 */
BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) !=
BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) {
Expand Down
12 changes: 8 additions & 4 deletions c/enc/hash_longest_match64_simd_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,10 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
}
prev_ix &= ring_buffer_mask;

if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask) {
break;
}
if (prev_ix + best_len > ring_buffer_mask ||
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
continue;
}
Expand Down Expand Up @@ -263,8 +265,10 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
break;
}
prev_ix &= ring_buffer_mask;
if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask) {
break;
}
if (prev_ix + best_len > ring_buffer_mask ||
/* compare 4 bytes ending at best_len + 1 */
BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) !=
BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) {
Expand Down
12 changes: 8 additions & 4 deletions c/enc/hash_longest_match_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
}
prev_ix &= ring_buffer_mask;

if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask) {
break;
}
if (prev_ix + best_len > ring_buffer_mask ||
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
continue;
}
Expand Down Expand Up @@ -234,8 +236,10 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
break;
}
prev_ix &= ring_buffer_mask;
if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask) {
break;
}
if (prev_ix + best_len > ring_buffer_mask ||
/* compare 4 bytes ending at best_len + 1 */
BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) !=
BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) {
Expand Down
12 changes: 8 additions & 4 deletions c/enc/hash_longest_match_simd_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
}
prev_ix &= ring_buffer_mask;

if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask) {
break;
}
if (prev_ix + best_len > ring_buffer_mask ||
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
continue;
}
Expand Down Expand Up @@ -235,8 +237,10 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
break;
}
prev_ix &= ring_buffer_mask;
if (cur_ix_masked + best_len > ring_buffer_mask ||
prev_ix + best_len > ring_buffer_mask ||
if (cur_ix_masked + best_len > ring_buffer_mask) {
break;
}
if (prev_ix + best_len > ring_buffer_mask ||
/* compare 4 bytes ending at best_len + 1 */
BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) !=
BrotliUnalignedRead32(&data[prev_ix + best_len - 3])) {
Expand Down

0 comments on commit a528bce

Please sign in to comment.