Skip to content

Commit

Permalink
[sw path] Improve distance code computation logic (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcao59 authored Oct 10, 2024
1 parent 26d7671 commit 3c74801
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 65 deletions.
3 changes: 3 additions & 0 deletions sources/core-sw/src/compression/deflate_slow.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: MIT
******************************************************************************/

#include <assert.h>

#include "crc.h"
#include "deflate_defs.h"
#include "deflate_hash_table.h"
Expand Down Expand Up @@ -82,6 +84,7 @@ static inline void compute_offset_code(const struct isal_hufftables* huffman_tab

offset -= 1U;
significant_bits = own_count_significant_bits(offset);
assert(significant_bits >= 2);

number_of_extra_bits = significant_bits - 2U;
extra_bits = offset & ((1U << number_of_extra_bits) - 1U);
Expand Down
59 changes: 3 additions & 56 deletions sources/core-sw/src/compression/deflate_slow_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,6 @@ static inline uint32_t bsr(uint32_t val) {
return msb;
}

static inline uint32_t count_significant_bits(uint32_t value) {
// Variables
uint32_t significant_bits = 0;

// Main cycle
while (value > 0) {
significant_bits++;
value >>= 1U;
}

return significant_bits;
}

static inline void compute_offset_code(const struct isal_hufftables* huffman_table_ptr, uint16_t offset,
uint64_t* const code_ptr, uint32_t* const code_length_ptr) {
// Variables
uint32_t significant_bits = 0U;
uint32_t number_of_extra_bits = 0U;
uint32_t extra_bits = 0U;
uint32_t symbol = 0U;
uint32_t length = 0U;
uint32_t code = 0U;

offset -= 1U;
significant_bits = count_significant_bits(offset);
// TODO: look into possibility of exiting early in case of significant_bits <= 1

number_of_extra_bits = significant_bits - 2U;
extra_bits = offset & ((1U << (number_of_extra_bits % 32)) - 1U);
offset >>= number_of_extra_bits % 32;
symbol = offset + 2 * number_of_extra_bits;

// Extracting information from table
code = huffman_table_ptr->dcodes[symbol];
length = huffman_table_ptr->dcodes_sizes[symbol];

// Return of the calculated results
*code_ptr = code | (extra_bits << length);
*code_length_ptr = length + number_of_extra_bits;
}

static inline uint32_t own_get_offset_table_index(const uint32_t offset) {
if (offset <= 2) {
return offset - 1;
Expand Down Expand Up @@ -115,10 +74,10 @@ static void compute_distance_icf_code(uint32_t distance, uint32_t* code, uint32_

distance -= 1;
msb = bsr(distance);
assert(msb >= 1);
assert(msb >= 2U);
num_extra_bits = msb - 2;
*extra_bits = distance & ((1 << (num_extra_bits % 32)) - 1);
distance >>= num_extra_bits % 32;
*extra_bits = distance & ((1 << num_extra_bits) - 1);
distance >>= num_extra_bits;
*code = distance + 2 * num_extra_bits;
assert(*code < 30);
}
Expand Down Expand Up @@ -176,18 +135,6 @@ void get_match_length_code(const struct isal_hufftables* const huffman_table_ptr
*code_length_ptr = match_length_info & 0x1FU;
}

void get_offset_code(const struct isal_hufftables* const huffman_table_ptr, uint32_t offset, uint64_t* const code_ptr,
uint32_t* const code_length_ptr) {
if (offset <= IGZIP_DIST_TABLE_SIZE) {
const uint64_t offset_info = huffman_table_ptr->dist_table[offset - 1];

*code_ptr = offset_info >> 5U;
*code_length_ptr = offset_info & 0x1FU;
} else {
compute_offset_code(huffman_table_ptr, offset, code_ptr, code_length_ptr);
}
}

void get_literal_code(const struct isal_hufftables* const huffman_table_ptr, const uint32_t literal,
uint64_t* const code_ptr, uint32_t* const code_length_ptr) {
*code_ptr = huffman_table_ptr->lit_table[literal];
Expand Down
3 changes: 0 additions & 3 deletions sources/core-sw/src/compression/include/deflate_slow_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ void update_histogram_for_match(isal_mod_hist* const histogram_ptr, const deflat
void get_match_length_code(const struct isal_hufftables* const huffman_table_ptr, const uint32_t match_length,
uint64_t* const code_ptr, uint32_t* const code_length_ptr);

void get_offset_code(const struct isal_hufftables* const huffman_table_ptr, uint32_t offset, uint64_t* const code_ptr,
uint32_t* const code_length_ptr);

void get_literal_code(const struct isal_hufftables* const huffman_table_ptr, const uint32_t literal,
uint64_t* const code_ptr, uint32_t* const code_length_ptr);

Expand Down
12 changes: 6 additions & 6 deletions sources/isal/igzip/huffman.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ static void compute_dist_code(struct isal_hufftables* hufftables, uint16_t dist,
uint32_t code = 0U;

msb = bsr(dist);
assert(msb >= 1U);
assert(msb >= 2U);
num_extra_bits = msb - 2U;
extra_bits = dist & ((1 << (num_extra_bits % 32)) - 1);
dist >>= num_extra_bits % 32;
extra_bits = dist & ((1 << num_extra_bits) - 1);
dist >>= num_extra_bits;
sym = dist + 2U * num_extra_bits;
assert(sym < 30U);
code = hufftables->dcodes[sym - IGZIP_DECODE_OFFSET];
Expand Down Expand Up @@ -132,10 +132,10 @@ static inline void get_lit_code(struct isal_hufftables* hufftables, uint32_t lit
static void compute_dist_icf_code(uint32_t dist, uint32_t* code, uint32_t* extra_bits) {
dist -= 1U;
uint32_t msb = bsr(dist);
assert(msb >= 1U);
assert(msb >= 2U);
uint32_t num_extra_bits = msb - 2U;
*extra_bits = dist & ((1 << (num_extra_bits % 32)) - 1);
dist >>= num_extra_bits % 32;
*extra_bits = dist & ((1 << num_extra_bits) - 1);
dist >>= num_extra_bits;
*code = dist + 2U * num_extra_bits;
assert(*code < 30U);
}
Expand Down

0 comments on commit 3c74801

Please sign in to comment.